forked from couchbaselabs/cbgb
/
vbucket.go
484 lines (410 loc) · 11.7 KB
/
vbucket.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package main
import (
"bytes"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"hash/crc32"
"io"
"log"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/dustin/go-broadcast"
"github.com/dustin/gomemcached"
)
const (
CHANGES_SINCE = gomemcached.CommandCode(0x60)
GET_VBMETA = gomemcached.CommandCode(0x61)
SET_VBMETA = gomemcached.CommandCode(0x62)
COLL_SUFFIX_KEYS = ".k" // This suffix sorts before CHANGES suffix.
COLL_SUFFIX_CHANGES = ".s" // The changes is like a "sequence" stream.
COLL_VBMETA = "vbm"
MAX_VBID = 0x0000ffff // Due to uint16.
MAX_ITEM_KEY_LENGTH = 250
MAX_ITEM_DATA_LENGTH = 1024 * 1024
MAX_ITEM_EXP = 0x7fffffff
DELETION_EXP = 0x80000000 // Deletion sentinel exp.
DELETION_FLAG = 0xffffffff // Deletion sentinel flag.
)
var ignore = errors.New("not-an-error/sentinel")
var expirerPeriod = newPeriodically(time.Minute*5, 2)
func VBucketIdForKey(key []byte, numVBuckets int) uint16 {
return uint16((crc32.ChecksumIEEE(key) >> uint32(16)) & uint32(numVBuckets-1))
}
type VBucket struct {
parent Bucket
vbid uint16
meta unsafe.Pointer // *VBMeta
bs *bucketstore
ps *partitionstore
lock sync.Mutex
stats Stats
available chan bool
observer broadcast.Broadcaster
bucketItemBytes *int64
staleness int64 // To track view freshness.
viewsStore *bucketstore
viewsLock sync.Mutex
}
func (v VBucket) String() string {
return fmt.Sprintf("{vbucket %v}", v.vbid)
}
type dispatchFun func(v *VBucket, w io.Writer,
req *gomemcached.MCRequest) *gomemcached.MCResponse
var dispatchTable = [256]dispatchFun{
gomemcached.GET: vbGet,
gomemcached.GETK: vbGet,
gomemcached.GETQ: vbGet,
gomemcached.GETKQ: vbGet,
gomemcached.SET: vbMutate,
gomemcached.SETQ: vbMutate,
gomemcached.DELETE: vbDelete,
gomemcached.DELETEQ: vbDelete,
gomemcached.ADD: vbMutate,
gomemcached.ADDQ: vbMutate,
gomemcached.REPLACE: vbMutate,
gomemcached.REPLACEQ: vbMutate,
gomemcached.APPEND: vbMutate,
gomemcached.APPENDQ: vbMutate,
gomemcached.PREPEND: vbMutate,
gomemcached.PREPENDQ: vbMutate,
gomemcached.INCREMENT: vbMutate,
gomemcached.INCREMENTQ: vbMutate,
gomemcached.DECREMENT: vbMutate,
gomemcached.DECREMENTQ: vbMutate,
gomemcached.RGET: vbRGet,
// TODO: Replace CHANGES_SINCE with enhanced TAP.
CHANGES_SINCE: vbChangesSince,
// TODO: Move new command codes to gomemcached one day.
GET_VBMETA: vbGetVBMeta,
SET_VBMETA: vbSetVBMeta,
}
func newVBucket(parent Bucket, vbid uint16, bs *bucketstore,
bucketItemBytes *int64) (rv *VBucket, err error) {
rv = &VBucket{
parent: parent,
vbid: vbid,
meta: unsafe.Pointer(&VBMeta{Id: vbid, State: VBDead.String()}),
bs: bs,
ps: bs.getPartitionStore(vbid),
observer: broadcastMux.Sub(),
available: make(chan bool),
bucketItemBytes: bucketItemBytes,
}
return rv, nil
}
func (v *VBucket) Meta() *VBMeta {
return (*VBMeta)(atomic.LoadPointer(&v.meta))
}
func (v *VBucket) Close() error {
if v == nil {
return nil
}
close(v.available)
return v.observer.Close()
}
func (v *VBucket) Dispatch(w io.Writer, req *gomemcached.MCRequest) *gomemcached.MCResponse {
atomic.AddInt64(&v.stats.Ops, 1)
f := dispatchTable[req.Opcode]
if f == nil {
atomic.AddInt64(&v.stats.Unknowns, 1)
return &gomemcached.MCResponse{
Status: gomemcached.UNKNOWN_COMMAND,
Body: []byte(fmt.Sprintf("Unknown command %v", req.Opcode)),
}
}
return f(v, w, req)
}
func (v *VBucket) get(key []byte) *gomemcached.MCResponse {
return v.Dispatch(nil, &gomemcached.MCRequest{
Opcode: gomemcached.GET,
Key: key,
VBucket: v.vbid,
})
}
func (v *VBucket) Apply(fun func()) {
v.lock.Lock()
defer v.lock.Unlock()
fun()
}
func (v *VBucket) GetVBState() (res VBState) {
return parseVBState(v.Meta().State)
}
func (v *VBucket) SetVBState(newState VBState,
cb func(prevState VBState)) (prevState VBState, err error) {
prevState = VBDead
// The bs.apply() ensures we're not compacting/flushing while
// changing vbstate, which is good for atomicity and to avoid
// deadlock when the compactor wants to swap collections.
v.bs.apply(func() {
v.Apply(func() {
prevMeta := v.Meta()
prevState = parseVBState(prevMeta.State)
casMeta := atomic.AddUint64(&prevMeta.LastCas, 1)
newMeta := prevMeta.Copy()
newMeta.State = newState.String()
newMeta.MetaCas = casMeta
err = v.setVBMeta(newMeta)
if err != nil {
return
}
if cb != nil {
cb(prevState)
}
})
})
return prevState, err
}
func (v *VBucket) setVBMeta(newMeta *VBMeta) (err error) {
// This should only be called when holding the bucketstore
// service/apply "lock", to ensure a Flush between changes stream
// update and COLL_VBMETA update is atomic.
var j []byte
j, err = json.Marshal(newMeta)
if err != nil {
return err
}
k := []byte(fmt.Sprintf("%d", v.vbid))
i := &item{
key: nil, // A nil key means it's a VBMeta change.
cas: newMeta.MetaCas,
data: j,
}
deltaItemBytes, err := v.ps.set(i, nil)
if err != nil {
return err
}
if err = v.bs.collMeta(COLL_VBMETA).Set(k, j); err != nil {
return err
}
atomic.StorePointer(&v.meta, unsafe.Pointer(newMeta))
atomic.AddInt64(&v.stats.ItemBytes, deltaItemBytes)
atomic.AddInt64(v.bucketItemBytes, deltaItemBytes)
return nil
}
func (v *VBucket) load() (err error) {
v.Apply(func() {
meta := v.Meta().Copy()
x, err := v.bs.collMeta(COLL_VBMETA).GetItem(
[]byte(fmt.Sprintf("%v", v.vbid)), true)
if err != nil {
return
}
if x == nil || x.Val == nil {
err = errors.New("missing COLL_VBMETA")
return
}
if err = json.Unmarshal(x.Val, meta); err != nil {
return
}
_, changes := v.ps.colls()
i, err := changes.MaxItem(true)
if err != nil {
return
}
if i != nil {
var lastCas uint64
lastCas, err = casBytesParse(i.Key)
if err != nil {
return
}
if meta.LastCas < lastCas {
meta.LastCas = lastCas
}
}
atomic.StorePointer(&v.meta, unsafe.Pointer(meta))
numItems, numItemBytes, err := v.ps.getTotals()
if err == nil {
atomic.StoreInt64(&v.stats.Items, int64(numItems))
atomic.StoreInt64(&v.stats.ItemBytes, int64(numItemBytes))
atomic.AddInt64(v.bucketItemBytes, int64(numItemBytes))
}
// TODO: What if we're loading something out of allowed range?
})
return err
}
func (v *VBucket) AddStatsTo(dest *Stats, key string) {
if parseVBState(v.Meta().State) == VBActive { // TODO: handle stats sub-key.
dest.Add(&v.stats)
}
}
func vbGet(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (res *gomemcached.MCResponse) {
atomic.AddInt64(&v.stats.Gets, 1)
i, err := v.getUnexpired(req.Key, time.Now())
if err != nil {
return &gomemcached.MCResponse{
Status: gomemcached.TMPFAIL,
Body: []byte(fmt.Sprintf("Store get error %v", err)),
}
}
if i == nil {
atomic.AddInt64(&v.stats.GetMisses, 1)
if req.Opcode.IsQuiet() {
return nil
}
return &gomemcached.MCResponse{Status: gomemcached.KEY_ENOENT}
}
res = &gomemcached.MCResponse{
Cas: i.cas,
Extras: make([]byte, 4),
Body: i.data,
}
binary.BigEndian.PutUint32(res.Extras, i.flag)
wantsKey := (req.Opcode == gomemcached.GETK || req.Opcode == gomemcached.GETKQ)
if wantsKey {
res.Key = req.Key
}
atomic.AddInt64(&v.stats.OutgoingValueBytes, int64(len(i.data)))
return res
}
// Responds with the changes since the req.Cas, with the last response
// in the response stream having no key.
// TODO: Support a limit on changes-since, perhaps in the req.Extras.
func vbChangesSince(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (res *gomemcached.MCResponse) {
res = &gomemcached.MCResponse{Opcode: req.Opcode, Cas: req.Cas}
var err error
ch, errs := transmitPackets(w)
visitor := func(i *item) bool {
if i.cas > req.Cas {
ch <- &gomemcached.MCResponse{
Opcode: req.Opcode,
Key: i.key,
Cas: i.cas,
// TODO: Extras.
// TODO: Should changes-since respond with item value?
}
select {
case err = <-errs:
return false
default:
}
}
return true
}
errVisit := v.ps.visitChanges(casBytes(req.Cas), true, visitor)
close(ch)
if errVisit != nil {
return &gomemcached.MCResponse{Fatal: true}
}
if err == nil {
err = <-errs
}
if err != nil {
log.Printf("Error sending changes-since: %v", err)
return &gomemcached.MCResponse{Fatal: true}
}
// TODO: Update stats.
return res
}
func vbGetVBMeta(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (res *gomemcached.MCResponse) {
v.Apply(func() {
if j, err := json.Marshal(v.Meta()); err == nil {
res = &gomemcached.MCResponse{Body: j}
} else {
res = &gomemcached.MCResponse{Body: []byte("{}")}
}
})
return res
}
// TODO: Don't allow clients to change vb state/meta for initial cbgb
// version, until (perhaps) one day when we have auth checking.
var allow_vbSetVBMeta bool = false
func vbSetVBMeta(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (res *gomemcached.MCResponse) {
res = &gomemcached.MCResponse{Status: gomemcached.EINVAL}
if !allow_vbSetVBMeta {
return
}
if req.Body == nil {
return
}
newMeta := &VBMeta{}
if err := json.Unmarshal(req.Body, newMeta); err != nil {
// XXX: The test requires that we don't report what's wrong
// res.Body = []byte(err.Error())
return
}
// The bs.apply() ensures we're not compacting/flushing while
// changing vbstate, which is good for atomicity and to avoid
// deadlock when the compactor wants to swap collections.
v.bs.apply(func() {
v.Apply(func() {
prevMeta := v.Meta()
casMeta := atomic.AddUint64(&prevMeta.LastCas, 1)
newMeta = prevMeta.Copy().update(newMeta)
newMeta.MetaCas = casMeta
if err := v.setVBMeta(newMeta); err != nil {
res = &gomemcached.MCResponse{
Status: gomemcached.TMPFAIL,
Body: []byte(fmt.Sprintf("setVBMeta error %v", err)),
}
return
}
// TODO: Unlike couchbase, we flush before returning
// to client; which might not be what some management
// use cases want (ability to switch vbstate even if
// dirty queues are huge).
if _, err := v.bs.flush_unlocked(); err != nil {
res = &gomemcached.MCResponse{
Status: gomemcached.TMPFAIL,
Body: []byte(fmt.Sprintf("setVBMeta flush error %v", err)),
}
return
}
res = &gomemcached.MCResponse{}
})
})
return res
}
func vbRGet(v *VBucket, w io.Writer, req *gomemcached.MCRequest) (
res *gomemcached.MCResponse) {
// From http://code.google.com/p/memcached/wiki/RangeOps
// Extras field Bits
// ------------------
// End key len 16
// Reserved 8
// Flags 8
// Max results 32
res = &gomemcached.MCResponse{
Opcode: req.Opcode,
Cas: req.Cas,
}
extras := make([]byte, 4)
visitRGetResults := int64(0)
visitOutgoingValueBytes := int64(0)
visitor := func(i *item) bool {
if bytes.Compare(i.key, req.Key) >= 0 {
// TODO: Need to hide expired items from range scan.
binary.BigEndian.PutUint32(extras, i.flag)
r := gomemcached.MCResponse{
Opcode: req.Opcode,
Key: i.key,
Cas: i.cas,
Extras: extras,
Body: i.data,
}
err := r.Transmit(w)
if err != nil {
res = &gomemcached.MCResponse{Fatal: true}
return false
}
visitRGetResults++
visitOutgoingValueBytes += int64(len(i.data))
}
return true
}
if err := v.ps.visitItems(req.Key, true, visitor); err != nil {
res = &gomemcached.MCResponse{Fatal: true}
}
atomic.AddInt64(&v.stats.RGets, 1)
atomic.AddInt64(&v.stats.RGetResults, visitRGetResults)
atomic.AddInt64(&v.stats.OutgoingValueBytes, visitOutgoingValueBytes)
return res
}
func (v *VBucket) Visit(start []byte, visitor func(key []byte, data []byte) bool) error {
return v.ps.visitItems(start, true, func(i *item) bool {
return visitor(i.key, i.data)
})
}