-
Notifications
You must be signed in to change notification settings - Fork 66
/
lookup.go
292 lines (274 loc) · 7.16 KB
/
lookup.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package mprovider
import (
"bytes"
"context"
"fmt"
"github.com/BTrDB/btrdb-server/bte"
etcd "github.com/coreos/etcd/clientv3"
)
const cursorBufferSize = 3
type cursor struct {
ctx context.Context
ec *etcd.Client
idx int
prefix string
startFrom string
desiredValue []byte
optional bool
buffer *etcd.GetResponse
traversed int
}
func (c *cursor) keyToHead(b []byte) []byte {
if b == nil {
return b
}
return b[len(c.prefix):]
}
func validateCollectionTagsAndAnns(collection string, tags map[string]*string, annotations map[string]*string) bte.BTE {
if len(collection) > 0 && !isValidCollection(collection) {
return bte.Err(bte.InvalidCollection, fmt.Sprintf("collection %q is invalid", collection))
}
for k, v := range tags {
if !isValidTagKey(k) {
return bte.Err(bte.InvalidTagKey, fmt.Sprintf("tag key %q is invalid", k))
}
if v != nil && !isValidTagValue(*v) {
return bte.Err(bte.InvalidTagValue, fmt.Sprintf("tag value for key %q is invalid", k))
}
}
for k, v := range annotations {
if !isValidAnnKey(k) {
return bte.Err(bte.InvalidTagKey, fmt.Sprintf("annotation key %q is invalid", k))
}
if v != nil && !isValidAnnotationValue(*v) {
return bte.Err(bte.InvalidTagValue, fmt.Sprintf("annotation value for key %q is invalid", k))
}
}
return nil
}
//advance the cursor until the head is greater than or equal to the given uuid
func (c *cursor) advanceUntilGTE(uu []byte) (gt bool, head []byte, finished bool, err bte.BTE) {
for {
head, fin, err := c.head()
if err != nil || fin {
return false, nil, fin, err
}
cmp := bytes.Compare(head, uu)
if cmp >= 0 {
return cmp > 0, head, false, nil
} else {
_, fin, err := c.pop()
if err != nil || fin {
return false, nil, fin, err
}
}
}
}
func (c *cursor) head() (head []byte, finished bool, err bte.BTE) {
for {
if c.idx < 0 || c.idx >= len(c.buffer.Kvs) {
return c.pop()
}
rv := c.buffer.Kvs[c.idx]
return c.keyToHead(rv.Key), false, nil
}
}
func (c *cursor) pop() (head []byte, finished bool, err bte.BTE) {
for {
head, value, fin, err := c.rawpop()
if err != nil || fin {
return nil, fin, err
}
if c.optional || bytes.Equal(value, c.desiredValue) {
return head, false, nil
}
}
}
func (c *cursor) rawpop() (head []byte, value []byte, finished bool, err bte.BTE) {
if c.buffer == nil {
//We need a new buffer
c.refill()
//Normally we ignore first entry in buffer, but this time
//its relevant
c.idx = -1
} else if c.idx >= len(c.buffer.Kvs)-1 && c.buffer.More {
c.refill()
}
c.idx += 1
if c.idx >= len(c.buffer.Kvs) {
return nil, nil, true, nil
}
rv := c.buffer.Kvs[c.idx]
return c.keyToHead(rv.Key), rv.Value, false, nil
}
func (c *cursor) refill() (berr bte.BTE) {
c.idx = 0
rv, err := c.ec.Get(c.ctx, c.startFrom,
etcd.WithSort(etcd.SortByKey, etcd.SortAscend),
etcd.WithRange(etcd.GetPrefixRangeEnd(c.prefix)),
etcd.WithLimit(cursorBufferSize),
etcd.WithSerializable())
if err != nil {
return bte.ErrW(bte.EtcdFailure, "could not refill cursor", err)
}
c.buffer = rv
if len(c.buffer.Kvs) > 0 {
c.startFrom = string(c.buffer.Kvs[len(c.buffer.Kvs)-1].Key)
}
return nil
}
//The cursor must start before the first result, such that pop() or advanceUntilGTE will advance to the first result
func (em *etcdMetadataProvider) openCursor(ctx context.Context, category string, collection string, isCollectionPrefix bool, key string, val *string) (*cursor, bte.BTE) {
skey := fmt.Sprintf("%s/%s/%s/%s", em.pfx, category, key, collection)
if !isCollectionPrefix {
skey += "/"
}
rv := &cursor{ec: em.ec,
prefix: skey,
startFrom: skey,
idx: -1,
ctx: ctx,
buffer: nil}
if val == nil {
rv.optional = true
} else {
rv.desiredValue = []byte(*val)
}
return rv, nil
}
func headToUU(b []byte) []byte {
if b == nil {
return nil
}
return b[len(b)-16:]
}
func (em *etcdMetadataProvider) fastPathCollectionsOnly(ctx context.Context, collection string, isCollectionPrefix bool) (chan *LookupResult, chan bte.BTE) {
lrchan := make(chan *LookupResult, 100)
errchan := make(chan bte.BTE, 1)
go func() {
collection = fmt.Sprintf("%s/s/%s", em.pfx, collection)
if !isCollectionPrefix {
collection += "/"
}
fromkey := collection
endkey := etcd.GetPrefixRangeEnd(collection)
skip := false
for {
rv, err := em.ec.Get(ctx, fromkey,
etcd.WithSort(etcd.SortByKey, etcd.SortAscend),
etcd.WithRange(endkey),
etcd.WithLimit(cursorBufferSize),
etcd.WithSerializable())
if err != nil {
errchan <- bte.ErrW(bte.EtcdFailure, "could not refill cxcursor", err)
return
}
for _, kv := range rv.Kvs {
if skip {
skip = false
continue
}
lr, err := em.GetStreamInfo(ctx, kv.Value)
if err != nil {
errchan <- err
return
}
lrchan <- lr
}
if rv.More {
skip = true
fromkey = string(rv.Kvs[len(rv.Kvs)-1].Key)
} else {
break
}
}
close(lrchan)
return
}()
return lrchan, errchan
}
// functions must not close error channel
// functions must not close value channel if there was an error
// functions must not write to error channel if they are blocking on sending to value channel (avoid leak)
// functions must treat a context cancel as an error and obey the above rules
func (em *etcdMetadataProvider) LookupStreams(pctx context.Context, collection string, isCollectionPrefix bool, tags map[string]*string, anns map[string]*string) (chan *LookupResult, chan bte.BTE) {
//Check all the inputs are ok
if err := validateCollectionTagsAndAnns(collection, tags, anns); err != nil {
return nil, bte.Chan(err)
}
if len(tags) == 0 && len(anns) == 0 {
return em.fastPathCollectionsOnly(pctx, collection, isCollectionPrefix)
}
lrchan := make(chan *LookupResult, 100)
errchan := make(chan bte.BTE, 1)
go func() {
ctx, cancel := context.WithCancel(pctx)
defer cancel()
//Open all the cursors
cz := []*cursor{}
for k, v := range tags {
cur, err := em.openCursor(ctx, "t", collection, isCollectionPrefix, k, v)
if err != nil {
errchan <- err
return
}
cz = append(cz, cur)
}
for k, v := range anns {
cur, err := em.openCursor(ctx, "a", collection, isCollectionPrefix, k, v)
if err != nil {
errchan <- err
return
}
cz = append(cz, cur)
}
//advance the first cursor and curhead
curhead, fin, err := cz[0].pop()
if fin {
close(lrchan)
return
}
if err != nil {
errchan <- err
return
}
outer:
for {
for i := 0; i < len(cz); i++ {
gt, thishead, fin, err := cz[i].advanceUntilGTE(curhead)
if fin {
close(lrchan)
return
}
if err != nil {
errchan <- err
return
}
if gt {
//This cursor is now greater. Repeat the search
curhead = thishead
continue outer
}
}
//All the cursors are equal. This stream is a match. Emit the uuid
lr, err := em.GetStreamInfo(ctx, headToUU(curhead))
if err != nil {
errchan <- err
return
}
lrchan <- lr
//Now advance the first cursor and curhead
thishead, fin, err := cz[0].pop()
if fin {
close(lrchan)
return
}
if err != nil {
errchan <- err
return
}
curhead = thishead
}
}()
return lrchan, errchan
}