-
Notifications
You must be signed in to change notification settings - Fork 41
/
uds_storage.go
215 lines (192 loc) · 6.39 KB
/
uds_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package udf
import (
"bytes"
"errors"
"fmt"
"gopkg.in/sensorbee/sensorbee.v0/core"
"io"
"io/ioutil"
"strings"
"sync"
)
// UDSStorage is an interface to support saving and loading UDSs.
type UDSStorage interface {
// Save returns a writer to write the state data. Save doesn't discard the
// previously saved data until UDSStorageWriter.Commit is called.
//
// Save can be called while a state is being loaded. In such case, behavior
// is up to each storage. Some possible implementations are: (1) Save
// creates a new entry for the state and conflicting Loads continue to read
// the previous data, (2) Save blocks until conflicting Loads finish, and
// (3) Save returns an error. Implementation (3) isn't recommended because
// it might result in starvation in highly concurrent workload.
//
// Either Commit or Abort of a UDSStorageWriter returned from this method
// has to be called. When Commit is called, the data is persisted. When
// Abort is called, the data is discarded and the previous data remains.
//
// Save can write header information or other data such as a space for
// storing checksum later to UDSStorageWriter before returning it. Save can
// also manipulate the written data as long as the data can be loaded again.
//
// A caller can assign a tag to the saved state so that multiple versions of
// the UDS can be managed with unique names. When a tag is an empty string,
// "default" will be used. The valid format of tags is same as node names,
// which is validated by core.ValidateSymbol.
Save(topology, state, tag string) (UDSStorageWriter, error)
// Load loads the previously saved data of the state. io.ReadCloser.Close
// has to be called when it gets unnecessary.
//
// Load can be called while a state is being saved. In such case, behavior
// is up to each storage. A storage's Load can block until Save is done,
// can return an error, or can even return a reader of the previously saved
// data.
//
// Load returns core.NotExistError when the state doesn't exist.
//
// When a tag is an empty string, "default" will be used.
Load(topology, state, tag string) (io.ReadCloser, error)
// ListTopologies returns a list of topologies that have saved states.
ListTopologies() ([]string, error)
// List returns a list of names of saved states in a topology as a map
// whose key is a name of a UDS. Each value contains tags assigned to
// the state as an array.
List(topology string) (map[string][]string, error)
}
// UDSStorageWriter is used to save a state. An instance of UDSStorageWriter
// doesn't have to be thread-safe. It means that an instance may not be able to
// be used from multiple goroutines. However, different instances can be used
// concurrently so that multiple states can be saved simultaneously.
type UDSStorageWriter interface {
io.Writer
// Commit persists the data written to the writer so far and closes it.
// Write cannot be called once the data is committed.
Commit() error
// Abort discard the data written to the writer. Write cannot be called
// after calling Abort.
Abort() error
}
type inMemoryUDSStorage struct {
m sync.RWMutex
topologies map[string]*topologyUDSStorage
}
// NewInMemoryUDSStorage creates a new UDSStorage which store all data in
// memory. This storage should only be used for experiment or test purpose.
func NewInMemoryUDSStorage() UDSStorage {
return &inMemoryUDSStorage{
topologies: map[string]*topologyUDSStorage{},
}
}
func (s *inMemoryUDSStorage) Save(topology, state, tag string) (UDSStorageWriter, error) {
if tag == "" || strings.ToLower(tag) == "default" {
tag = "default"
} else if err := core.ValidateSymbol(tag); err != nil {
return nil, fmt.Errorf("tag is ill-formatted: %v", err)
}
s.m.Lock()
defer s.m.Unlock()
t, ok := s.topologies[topology]
if !ok {
t = &topologyUDSStorage{
topologyName: topology,
states: map[string]map[string][]byte{},
}
s.topologies[topology] = t
}
return &inMemoryUDSStorageWriter{
storage: t,
buf: bytes.NewBuffer(nil),
stateName: state,
tag: tag,
}, nil
}
func (s *inMemoryUDSStorage) Load(topology, state, tag string) (io.ReadCloser, error) {
if tag == "" || strings.ToLower(tag) == "default" {
tag = "default"
} else if err := core.ValidateSymbol(tag); err != nil {
return nil, fmt.Errorf("tag is ill-formatted: %v", err)
}
s.m.RLock()
defer s.m.RUnlock()
t, ok := s.topologies[topology]
if !ok {
return nil, core.NotExistError(fmt.Errorf("a topology '%v' was not found", topology))
}
st, ok := t.states[state]
if !ok {
return nil, core.NotExistError(fmt.Errorf("a UDS '%v' was not found", state))
}
data, ok := st[tag]
if !ok {
return nil, core.NotExistError(fmt.Errorf("a UDS '%v' doesn't have a tag '%v'", state, tag))
}
return ioutil.NopCloser(bytes.NewReader(data)), nil
}
func (s *inMemoryUDSStorage) ListTopologies() ([]string, error) {
s.m.RLock()
defer s.m.RUnlock()
res := make([]string, 0, len(s.topologies))
for name := range s.topologies {
res = append(res, name)
}
return res, nil
}
func (s *inMemoryUDSStorage) List(topology string) (map[string][]string, error) {
s.m.RLock()
defer s.m.RUnlock()
t, ok := s.topologies[topology]
if !ok {
return nil, core.NotExistError(fmt.Errorf("a topology '%v' was not found", topology))
}
return t.list(), nil
}
type topologyUDSStorage struct {
m sync.RWMutex
topologyName string
states map[string]map[string][]byte
}
func (t *topologyUDSStorage) list() map[string][]string {
t.m.RLock()
defer t.m.RUnlock()
res := map[string][]string{}
for name, tags := range t.states {
for tag := range tags {
res[name] = append(res[name], tag)
}
}
return res
}
type inMemoryUDSStorageWriter struct {
storage *topologyUDSStorage
buf *bytes.Buffer
stateName string
tag string
}
func (w *inMemoryUDSStorageWriter) Write(data []byte) (int, error) {
if w.buf == nil {
return 0, errors.New("writer is already closed")
}
return w.buf.Write(data)
}
func (w *inMemoryUDSStorageWriter) Commit() error {
if w.buf == nil {
return errors.New("writer is already closed")
}
w.storage.m.Lock()
defer w.storage.m.Unlock()
m := w.storage.states[w.stateName]
if m == nil {
m = map[string][]byte{}
}
m[w.tag] = w.buf.Bytes()
w.storage.states[w.stateName] = m
w.buf = nil
return nil
}
func (w *inMemoryUDSStorageWriter) Abort() error {
if w.buf == nil {
return errors.New("writer is already closed")
}
w.buf = nil
return nil
}