forked from couchbase/gocbcore
/
agent_collections.go
386 lines (328 loc) · 9.55 KB
/
agent_collections.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package gocbcore
import (
"encoding/binary"
"encoding/json"
"fmt"
"strconv"
"sync"
"github.com/opentracing/opentracing-go"
)
const (
unknownCid = uint32(0xFFFFFFFF)
pendingCid = uint32(0xFFFFFFFE)
invalidCid = uint32(0xFFFFFFFD)
)
// ManifestCollection is the representation of a collection within a manifest.
type ManifestCollection struct {
UID uint32
Name string
}
// UnmarshalJSON is a custom implementation of json unmarshaling.
func (item *ManifestCollection) UnmarshalJSON(data []byte) error {
decData := struct {
UID string `json:"uid"`
Name string `json:"name"`
}{}
if err := json.Unmarshal(data, &decData); err != nil {
return err
}
decUID, err := strconv.ParseUint(decData.UID, 16, 32)
if err != nil {
return err
}
item.UID = uint32(decUID)
item.Name = decData.Name
return nil
}
// ManifestScope is the representation of a scope within a manifest.
type ManifestScope struct {
UID uint32
Name string
Collections []ManifestCollection
}
// UnmarshalJSON is a custom implementation of json unmarshaling.
func (item *ManifestScope) UnmarshalJSON(data []byte) error {
decData := struct {
UID string `json:"uid"`
Name string `json:"name"`
Collections []ManifestCollection `json:"collections"`
}{}
if err := json.Unmarshal(data, &decData); err != nil {
return err
}
decUID, err := strconv.ParseUint(decData.UID, 16, 32)
if err != nil {
return err
}
item.UID = uint32(decUID)
item.Name = decData.Name
item.Collections = decData.Collections
return nil
}
// Manifest is the representation of a collections manifest.
type Manifest struct {
UID uint64
Scopes []ManifestScope
}
// UnmarshalJSON is a custom implementation of json unmarshaling.
func (item *Manifest) UnmarshalJSON(data []byte) error {
decData := struct {
UID string `json:"uid"`
Scopes []ManifestScope `json:"scopes"`
}{}
if err := json.Unmarshal(data, &decData); err != nil {
return err
}
decUID, err := strconv.ParseUint(decData.UID, 16, 64)
if err != nil {
return err
}
item.UID = decUID
item.Scopes = decData.Scopes
return nil
}
// ManifestCallback is invoked upon completion of a GetCollectionManifest operation.
type ManifestCallback func(manifest []byte, err error)
// GetCollectionManifest fetches the current server manifest. This function will not update the client's collection
// id cache.
func (agent *Agent) GetCollectionManifest(cb ManifestCallback) (PendingOp, error) {
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
if err != nil {
cb(nil, err)
return
}
cb(resp.Value, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdCollectionsGetManifest,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: nil,
Value: nil,
},
Callback: handler,
}
return agent.dispatchOp(req)
}
// CollectionIdCallback is invoked upon completion of a GetCollectionID operation.
type CollectionIdCallback func(manifestID uint64, collectionID uint32, err error)
// GetCollectionIDOptions are the options available to the GetCollectionID command.
type GetCollectionIDOptions struct {
TraceContext opentracing.SpanContext
}
// GetCollectionID fetches the collection id and manifest id that the collection belongs to, given a scope name
// and collection name. This function will also prime the client's collection id cache.
func (agent *Agent) GetCollectionID(scopeName string, collectionName string, opts GetCollectionIDOptions, cb CollectionIdCallback) (PendingOp, error) {
tracer := agent.createOpTrace("GetCollectionID", opts.TraceContext)
handler := func(resp *memdQResponse, req *memdQRequest, err error) {
cidCache, ok := agent.cidMgr.Get(scopeName, collectionName)
if !ok {
cidCache = agent.cidMgr.newCollectionIdCache()
agent.cidMgr.Add(cidCache, scopeName, collectionName)
}
if err != nil {
cidCache.lock.Lock()
cidCache.id = invalidCid
cidCache.err = err
cidCache.lock.Unlock()
tracer.Finish()
cb(0, 0, err)
return
}
manifestID := binary.BigEndian.Uint64(resp.Extras[0:])
collectionID := binary.BigEndian.Uint32(resp.Extras[8:])
cidCache.lock.Lock()
cidCache.id = collectionID
cidCache.lock.Unlock()
tracer.Finish()
cb(manifestID, collectionID, nil)
}
req := &memdQRequest{
memdPacket: memdPacket{
Magic: reqMagic,
Opcode: cmdCollectionsGetID,
Datatype: 0,
Cas: 0,
Extras: nil,
Key: []byte(fmt.Sprintf("%s.%s", scopeName, collectionName)),
Value: nil,
Vbucket: 0,
},
RootTraceContext: opts.TraceContext,
ReplicaIdx: -1,
}
req.Callback = handler
return agent.dispatchOp(req)
}
func (cidMgr *collectionIdManager) createKey(scopeName, collectionName string) string {
return fmt.Sprintf("%s.%s", scopeName, collectionName)
}
type collectionIdManager struct {
idMap map[string]*collectionIdCache
mapLock sync.Mutex
agent *Agent
maxQueueSize int
}
func newCollectionIdManager(agent *Agent, maxQueueSize int) *collectionIdManager {
cidMgr := &collectionIdManager{
agent: agent,
idMap: make(map[string]*collectionIdCache),
maxQueueSize: maxQueueSize,
}
return cidMgr
}
func (cidMgr *collectionIdManager) Add(id *collectionIdCache, scopeName, collectionName string) {
key := cidMgr.createKey(scopeName, collectionName)
cidMgr.mapLock.Lock()
cidMgr.idMap[key] = id
cidMgr.mapLock.Unlock()
}
func (cidMgr *collectionIdManager) Get(scopeName, collectionName string) (*collectionIdCache, bool) {
cidMgr.mapLock.Lock()
id, ok := cidMgr.idMap[cidMgr.createKey(scopeName, collectionName)]
cidMgr.mapLock.Unlock()
if !ok {
return nil, false
}
return id, true
}
func (cidMgr *collectionIdManager) Remove(scopeName, collectionName string) {
cidMgr.mapLock.Lock()
delete(cidMgr.idMap, cidMgr.createKey(scopeName, collectionName))
cidMgr.mapLock.Unlock()
}
func (cidMgr *collectionIdManager) newCollectionIdCache() *collectionIdCache {
return &collectionIdCache{
agent: cidMgr.agent,
maxQueueSize: cidMgr.maxQueueSize,
}
}
type collectionIdCache struct {
opQueue *memdOpQueue
id uint32
collectionName string
scopeName string
agent *Agent
lock sync.Mutex
err error
maxQueueSize int
}
func (cid *collectionIdCache) sendWithCid(req *memdQRequest) error {
cid.lock.Lock()
req.CollectionID = cid.id
cid.lock.Unlock()
return cid.agent.dispatchDirect(req)
}
func (cid *collectionIdCache) rejectRequest(req *memdQRequest) error {
return cid.err
}
func (cid *collectionIdCache) queueRequest(req *memdQRequest) error {
return cid.opQueue.Push(req, cid.maxQueueSize)
}
func (cid *collectionIdCache) refreshCid(req *memdQRequest) error {
err := cid.opQueue.Push(req, cid.maxQueueSize)
if err != nil {
return err
}
_, err = cid.agent.GetCollectionID(req.ScopeName, req.CollectionName, GetCollectionIDOptions{TraceContext: req.RootTraceContext},
func(manifestID uint64, collectionID uint32, err error) {
// GetCollectionID will handle updating the id cache so we don't need to do it here
if err != nil {
cid.opQueue.Close()
cid.opQueue.Drain(func(request *memdQRequest) {
request.tryCallback(nil, err)
})
cid.opQueue = nil
return
}
cid.opQueue.Close()
cid.opQueue.Drain(func(request *memdQRequest) {
request.CollectionID = collectionID
cid.agent.requeueDirect(request)
})
},
)
return err
}
func (cid *collectionIdCache) dispatch(req *memdQRequest) error {
cid.lock.Lock()
// if the cid is unknown then mark the request pending and refresh cid first
// if it's pending then queue the request
// if it's invalid then reject the request
// otherwise send the request
switch cid.id {
case unknownCid:
cid.id = pendingCid
cid.opQueue = newMemdOpQueue()
cid.lock.Unlock()
return cid.refreshCid(req)
case pendingCid:
cid.lock.Unlock()
return cid.queueRequest(req)
case invalidCid:
cid.lock.Unlock()
return cid.rejectRequest(req)
default:
cid.lock.Unlock()
return cid.sendWithCid(req)
}
}
func (cidMgr *collectionIdManager) dispatch(req *memdQRequest) (PendingOp, error) {
if !cidMgr.agent.HasCollectionsSupport() {
if (req.CollectionName != "" || req.ScopeName != "") && (req.CollectionName != "_default" || req.ScopeName != "_default") {
return nil, ErrCollectionsUnsupported
}
err := cidMgr.agent.dispatchDirect(req)
if err != nil {
return nil, err
}
return req, nil
}
// some operations do not support cid
if req.CollectionName == "" && req.ScopeName == "" {
err := cidMgr.agent.dispatchDirect(req)
if err != nil {
return nil, err
}
return req, nil
}
if req.CollectionName == "_default" && req.ScopeName == "_default" {
req.CollectionID = 0
err := cidMgr.agent.dispatchDirect(req)
if err != nil {
return nil, err
}
return req, nil
}
cidCache, ok := cidMgr.Get(req.ScopeName, req.CollectionName)
if !ok {
cidCache = cidMgr.newCollectionIdCache()
cidCache.id = unknownCid
cidMgr.Add(cidCache, req.ScopeName, req.CollectionName)
}
err := cidCache.dispatch(req)
if err != nil {
return nil, err
}
return req, nil
}
func (cidMgr *collectionIdManager) requeue(req *memdQRequest) {
cidCache, ok := cidMgr.Get(req.ScopeName, req.CollectionName)
if !ok {
cidCache = cidMgr.newCollectionIdCache()
cidCache.id = unknownCid
cidMgr.Add(cidCache, req.ScopeName, req.CollectionName)
}
cidCache.lock.Lock()
if cidCache.id != unknownCid && cidCache.id != pendingCid && cidCache.id != invalidCid {
cidCache.id = unknownCid
}
cidCache.lock.Unlock()
err := cidCache.dispatch(req)
if err != nil {
req.tryCallback(nil, err)
}
}