/
kvdisk.go
714 lines (675 loc) · 17.7 KB
/
kvdisk.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bytes"
"crypto/md5"
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
"runtime"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/cockroachdb/pebble"
sm "github.com/lni/dragonboat/v4/statemachine"
)
//
// Note: this is an example demonstrating how to use the on disk state machine
// feature in Dragonboat. it assumes the underlying db only supports Get, Put
// and TakeSnapshot operations. this is not a demonstration on how to build a
// distributed key-value database.
//
func syncDir(dir string) (err error) {
if runtime.GOOS == "windows" {
return nil
}
fileInfo, err := os.Stat(dir)
if err != nil {
return err
}
if !fileInfo.IsDir() {
panic("not a dir")
}
df, err := os.Open(filepath.Clean(dir))
if err != nil {
return err
}
defer func() {
if cerr := df.Close(); err == nil {
err = cerr
}
}()
return df.Sync()
}
// pebbledb is a wrapper to ensure lookup() and close() can be concurrently
// invoked. IOnDiskStateMachine.Update() and close() will never be concurrently
// invoked.
type pebbledb struct {
mu sync.RWMutex
db *pebble.DB
ro *pebble.IterOptions
wo *pebble.WriteOptions
syncwo *pebble.WriteOptions
closed bool
}
func (db *pebbledb) scan(query string) (map[string][]byte, error) {
db.mu.RLock()
defer db.mu.RUnlock()
if db.closed {
return nil, errors.New("db already closed")
}
items := make(map[string][]byte)
it := db.db.NewIter(db.ro)
defer it.Close()
qb := []byte(query)
it.SeekGE(qb)
for ; it.Valid(); it.Next() {
key := it.Key()
val := it.Value()
if !bytes.HasPrefix(key, qb) {
break
}
v := make([]byte, len(val))
copy(v, val)
items[string(key)] = v
}
return items, nil
}
func (r *pebbledb) lookup(query []byte) ([]byte, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if r.closed {
return nil, errors.New("db already closed")
}
val, closer, err := r.db.Get(query)
if err != nil {
return nil, err
}
defer closer.Close()
if len(val) == 0 {
return []byte(""), nil
}
buf := make([]byte, len(val))
copy(buf, val)
return buf, nil
}
func (r *pebbledb) close() {
r.mu.Lock()
defer r.mu.Unlock()
r.closed = true
if r.db != nil {
r.db.Close()
}
}
func (db *pebbledb) delete(query []byte) error {
db.mu.Lock()
defer db.mu.Unlock()
if db.closed {
return errors.New("db already closed")
}
return db.db.Delete(query, db.wo)
}
// createDB creates a PebbleDB DB in the specified directory.
func createDB(dbdir string) (*pebbledb, error) {
ro := &pebble.IterOptions{}
wo := &pebble.WriteOptions{Sync: false}
syncwo := &pebble.WriteOptions{Sync: true}
cache := pebble.NewCache(0)
opts := &pebble.Options{
MaxManifestFileSize: 1024 * 32,
MemTableSize: 1024 * 32,
Cache: cache,
}
if err := os.MkdirAll(dbdir, 0755); err != nil {
return nil, err
}
db, err := pebble.Open(dbdir, opts)
if err != nil {
return nil, err
}
cache.Unref()
return &pebbledb{
db: db,
ro: ro,
wo: wo,
syncwo: syncwo,
}, nil
}
// functions below are used to manage the current data directory of Pebble DB.
func isNewRun(dir string) bool {
fp := filepath.Join(dir, currentDBFilename)
if _, err := os.Stat(fp); os.IsNotExist(err) {
return true
}
return false
}
func getNodeDBDirName(clusterID uint64, ReplicaID uint64) string {
part := fmt.Sprintf("%d_%d", clusterID, ReplicaID)
return filepath.Join(testDBDirName, part)
}
func getNewRandomDBDirName(dir string) string {
part := "%d_%d"
rn := rand.Uint64()
ct := time.Now().UnixNano()
return filepath.Join(dir, fmt.Sprintf(part, rn, ct))
}
func replaceCurrentDBFile(dir string) error {
fp := filepath.Join(dir, currentDBFilename)
tmpFp := filepath.Join(dir, updatingDBFilename)
if err := os.Rename(tmpFp, fp); err != nil {
return err
}
return syncDir(dir)
}
func saveCurrentDBDirName(dir string, dbdir string) error {
h := md5.New()
if _, err := h.Write([]byte(dbdir)); err != nil {
return err
}
fp := filepath.Join(dir, updatingDBFilename)
f, err := os.Create(fp)
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
if err := syncDir(dir); err != nil {
panic(err)
}
}()
if _, err := f.Write(h.Sum(nil)[:8]); err != nil {
return err
}
if _, err := f.Write([]byte(dbdir)); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
return nil
}
func getCurrentDBDirName(dir string) (string, error) {
fp := filepath.Join(dir, currentDBFilename)
f, err := os.OpenFile(fp, os.O_RDONLY, 0755)
if err != nil {
return "", err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
data, err := ioutil.ReadAll(f)
if err != nil {
return "", err
}
if len(data) <= 8 {
panic("corrupted content")
}
crc := data[:8]
content := data[8:]
h := md5.New()
if _, err := h.Write(content); err != nil {
return "", err
}
if !bytes.Equal(crc, h.Sum(nil)[:8]) {
panic("corrupted content with not matched crc")
}
return string(content), nil
}
func createNodeDataDir(dir string) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
return syncDir(filepath.Dir(dir))
}
func cleanupNodeDataDir(dir string) error {
os.RemoveAll(filepath.Join(dir, updatingDBFilename))
dbdir, err := getCurrentDBDirName(dir)
if err != nil {
return err
}
files, err := ioutil.ReadDir(dir)
if err != nil {
return err
}
for _, fi := range files {
if !fi.IsDir() {
continue
}
fmt.Printf("dbdir %s, fi.name %s, dir %s\n", dbdir, fi.Name(), dir)
toDelete := filepath.Join(dir, fi.Name())
if toDelete != dbdir {
fmt.Printf("removing %s\n", toDelete)
if err := os.RemoveAll(toDelete); err != nil {
return err
}
}
}
return nil
}
// DiskKV is a state machine that implements the IOnDiskStateMachine interface.
// DiskKV stores key-value pairs in the underlying PebbleDB key-value store. As
// it is used as an example, it is implemented using the most basic features
// common in most key-value stores. This is NOT a benchmark program.
type DiskKV struct {
clusterID uint64
ReplicaID uint64
lastApplied uint64
db unsafe.Pointer
closed bool
aborted bool
}
// NewDiskKV creates a new disk kv test state machine.
func NewDiskKV(clusterID uint64, ReplicaID uint64) sm.IOnDiskStateMachine {
d := &DiskKV{
clusterID: clusterID,
ReplicaID: ReplicaID,
}
return d
}
func (d *DiskKV) queryAppliedIndex(db *pebbledb) (uint64, error) {
val, closer, err := db.db.Get([]byte(appliedIndexKey))
if err != nil && err != pebble.ErrNotFound {
return 0, err
}
defer func() {
if closer != nil {
closer.Close()
}
}()
if len(val) == 0 {
return 0, nil
}
return binary.LittleEndian.Uint64(val), nil
}
// Open opens the state machine and return the index of the last Raft Log entry
// already updated into the state machine.
func (d *DiskKV) Open(stopc <-chan struct{}) (uint64, error) {
dir := getNodeDBDirName(d.clusterID, d.ReplicaID)
if err := createNodeDataDir(dir); err != nil {
panic(err)
}
var dbdir string
if !isNewRun(dir) {
if err := cleanupNodeDataDir(dir); err != nil {
return 0, err
}
var err error
dbdir, err = getCurrentDBDirName(dir)
if err != nil {
return 0, err
}
if _, err := os.Stat(dbdir); err != nil {
if os.IsNotExist(err) {
panic("db dir unexpectedly deleted")
}
}
} else {
dbdir = getNewRandomDBDirName(dir)
if err := saveCurrentDBDirName(dir, dbdir); err != nil {
return 0, err
}
if err := replaceCurrentDBFile(dir); err != nil {
return 0, err
}
}
db, err := createDB(dbdir)
if err != nil {
return 0, err
}
atomic.SwapPointer(&d.db, unsafe.Pointer(db))
appliedIndex, err := d.queryAppliedIndex(db)
if err != nil {
panic(err)
}
d.lastApplied = appliedIndex
return appliedIndex, nil
}
// Lookup queries the state machine.
func (d *DiskKV) Lookup(cmd interface{}) (interface{}, error) {
actionKV := &KVAction{Data: &KVData{}} //Set the deafult lookup to a byte query
if c, ok := cmd.(string); ok {
actionKV.Action = GET
actionKV.Data.Key = c
}
if c, ok := cmd.(*KVData); ok {
actionKV.Action = GET
actionKV.Data = c
}
if c, ok := cmd.(*KVAction); ok {
actionKV = c
}
if c, ok := cmd.([]byte); ok {
//is action
if err := json.Unmarshal(c, actionKV); err != nil || actionKV.Action == "" {
//is kv
if err := json.Unmarshal(c, actionKV.Data); err == nil && actionKV.Data.Key != "" {
actionKV.Action = GET
} else {
//is something else
actionKV.Action = GET
actionKV.Data.Key = string(c[:])
}
}
}
switch actionKV.Action {
case SCAN:
return d.Scan(actionKV.Data.Key)
case REVERSE_SCAN:
return d.ReverseScan(actionKV.Data.Key, actionKV.Data.Val)
default:
return d.Get(actionKV.Data.Key)
}
}
// Lookup queries the state machine.
func (d *DiskKV) Get(key interface{}) (interface{}, error) {
db := (*pebbledb)(atomic.LoadPointer(&d.db))
if db != nil {
var kb []byte
if k, ok := key.([]byte); ok {
kb = k
} else if k, ok := key.(string); ok {
kb = []byte(k)
} else {
return nil, fmt.Errorf("[ERROR] Unsupported query type for key: %s", key)
}
v, err := db.lookup(kb)
if err == nil && d.closed {
panic("lookup returned valid result when DiskKV is already closed")
}
if err == pebble.ErrNotFound {
return v, nil
}
return v, err
}
return nil, errors.New("db closed")
}
// Gets all records prefixed with key
func (d *DiskKV) Scan(key string) (map[string][]byte, error) {
db := (*pebbledb)(atomic.LoadPointer(&d.db))
if db != nil {
v, err := db.scan(key)
if err == nil && d.closed {
panic("scan returned valid result when DiskKV is already closed")
}
return v, err
}
return nil, errors.New("db closed")
}
func (d *DiskKV) ReverseScan(key string, val []byte) (map[string][]byte, error) {
db := (*pebbledb)(atomic.LoadPointer(&d.db))
values := make(map[string][]byte, 0)
if db != nil {
iter := db.db.NewIter(&pebble.IterOptions{
LowerBound: val,
UpperBound: []byte(key),
})
defer iter.Close()
for iter.First(); iteratorIsValid(iter); iter.Next() {
values[string(iter.Key())] = iter.Value()
}
return values, nil
}
return nil, fmt.Errorf("Could not connect to db")
}
// Update updates the state machine. In this example, all updates are put into
// a PebbleDB write batch and then atomically written to the DB together with
// the index of the last Raft Log entry. For simplicity, we always Sync the
// writes (db.wo.Sync=True). To get higher throughput, you can implement the
// Sync() method below and choose not to synchronize for every Update(). Sync()
// will periodically called by Dragonboat to synchronize the state.
func (d *DiskKV) Update(ents []sm.Entry) ([]sm.Entry, error) {
if d.aborted {
panic("update() called after abort set to true")
}
if d.closed {
panic("update called after Close()")
}
db := (*pebbledb)(atomic.LoadPointer(&d.db))
wb := db.db.NewBatch()
defer wb.Close()
for idx, e := range ents {
batchKV := &KVBatch{}
if errb := json.Unmarshal(e.Cmd, batchKV); errb == nil && batchKV.Batch != nil {
for _, a := range batchKV.Batch {
if a.Val == nil {
wb.Delete([]byte(a.Key), db.wo)
} else {
wb.Set([]byte(a.Key), []byte(a.Val), db.wo)
}
}
ents[idx].Result = sm.Result{Value: uint64(len(ents[idx].Cmd))}
continue
}
actionKV := &KVAction{Data: &KVData{}}
erru := json.Unmarshal(e.Cmd, actionKV)
if erru != nil || actionKV.Data.Key == "" {
erru = json.Unmarshal(e.Cmd, actionKV.Data)
if erru != nil || actionKV.Data.Key == "" {
rlog.Errorf("[ERROR] Couldn't update empty/unknown key for %s. Error: %v\n", string(e.Cmd), erru)
continue
}
}
if actionKV.Data.Val == nil {
wb.Delete([]byte(actionKV.Data.Key), db.wo)
} else {
wb.Set([]byte(actionKV.Data.Key), []byte(actionKV.Data.Val), db.wo)
}
ents[idx].Result = sm.Result{Value: uint64(len(ents[idx].Cmd))}
}
// save the applied index to the DB.
appliedIndex := make([]byte, 8)
binary.LittleEndian.PutUint64(appliedIndex, ents[len(ents)-1].Index)
wb.Set([]byte(appliedIndexKey), appliedIndex, db.wo)
if err := db.db.Apply(wb, db.syncwo); err != nil {
return ents, err
}
if d.lastApplied >= ents[len(ents)-1].Index {
panic("lastApplied not moving forward")
}
d.lastApplied = ents[len(ents)-1].Index
return ents, nil
}
// Sync synchronizes all in-core state of the state machine. Since the Update
// method in this example already does that every time when it is invoked, the
// Sync method here is a NoOP.
func (d *DiskKV) Sync() error {
return nil
}
type diskKVCtx struct {
db *pebbledb
snapshot *pebble.Snapshot
}
// PrepareSnapshot prepares snapshotting. PrepareSnapshot is responsible to
// capture a state identifier that identifies a point in time state of the
// underlying data. In this example, we use Pebble's snapshot feature to
// achieve that.
func (d *DiskKV) PrepareSnapshot() (interface{}, error) {
if d.closed {
panic("prepare snapshot called after Close()")
}
if d.aborted {
panic("prepare snapshot called after abort")
}
db := (*pebbledb)(atomic.LoadPointer(&d.db))
return &diskKVCtx{
db: db,
snapshot: db.db.NewSnapshot(),
}, nil
}
func iteratorIsValid(iter *pebble.Iterator) bool {
return iter.Valid()
}
// saveToWriter saves all existing key-value pairs to the provided writer.
// As an example, we use the most straight forward way to implement this.
func (d *DiskKV) saveToWriter(db *pebbledb,
ss *pebble.Snapshot, w io.Writer) error {
iter := ss.NewIter(db.ro)
defer iter.Close()
values := make([]*KVData, 0)
for iter.First(); iteratorIsValid(iter); iter.Next() {
kv := &KVData{
Key: string(iter.Key()),
Val: iter.Value(),
}
values = append(values, kv)
}
count := uint64(len(values))
sz := make([]byte, 8)
binary.LittleEndian.PutUint64(sz, count)
if _, err := w.Write(sz); err != nil {
return err
}
for _, dataKv := range values {
data, err := json.Marshal(dataKv)
if err != nil {
panic(err)
}
binary.LittleEndian.PutUint64(sz, uint64(len(data)))
if _, err := w.Write(sz); err != nil {
return err
}
if _, err := w.Write(data); err != nil {
return err
}
}
return nil
}
// SaveSnapshot saves the state machine state identified by the state
// identifier provided by the input ctx parameter. Note that SaveSnapshot
// is not suppose to save the latest state.
func (d *DiskKV) SaveSnapshot(ctx interface{},
w io.Writer, done <-chan struct{}) error {
if d.closed {
panic("prepare snapshot called after Close()")
}
if d.aborted {
panic("prepare snapshot called after abort")
}
ctxdata := ctx.(*diskKVCtx)
db := ctxdata.db
db.mu.RLock()
defer db.mu.RUnlock()
ss := ctxdata.snapshot
defer ss.Close()
return d.saveToWriter(db, ss, w)
}
// RecoverFromSnapshot recovers the state machine state from snapshot. The
// snapshot is recovered into a new DB first and then atomically swapped with
// the existing DB to complete the recovery.
func (d *DiskKV) RecoverFromSnapshot(r io.Reader,
done <-chan struct{}) error {
if d.closed {
panic("recover from snapshot called after Close()")
}
dir := getNodeDBDirName(d.clusterID, d.ReplicaID)
dbdir := getNewRandomDBDirName(dir)
oldDirName, err := getCurrentDBDirName(dir)
if err != nil {
return err
}
db, err := createDB(dbdir)
if err != nil {
return err
}
sz := make([]byte, 8)
if _, err := io.ReadFull(r, sz); err != nil {
return err
}
total := binary.LittleEndian.Uint64(sz)
wb := db.db.NewBatch()
defer wb.Close()
for i := uint64(0); i < total; i++ {
if _, err := io.ReadFull(r, sz); err != nil {
return err
}
toRead := binary.LittleEndian.Uint64(sz)
data := make([]byte, toRead)
if _, err := io.ReadFull(r, data); err != nil {
return err
}
dataKv := &KVData{}
if err := json.Unmarshal(data, dataKv); err != nil {
panic(err)
}
wb.Set([]byte(dataKv.Key), []byte(dataKv.Val), db.wo)
}
if err := db.db.Apply(wb, db.syncwo); err != nil {
return err
}
if err := saveCurrentDBDirName(dir, dbdir); err != nil {
return err
}
if err := replaceCurrentDBFile(dir); err != nil {
return err
}
newLastApplied, err := d.queryAppliedIndex(db)
if err != nil {
panic(err)
}
// when d.lastApplied == newLastApplied, it probably means there were some
// dummy entries or membership change entries as part of the new snapshot
// that never reached the SM and thus never moved the last applied index
// in the SM snapshot.
if d.lastApplied > newLastApplied {
panic("last applied not moving forward")
}
d.lastApplied = newLastApplied
old := (*pebbledb)(atomic.SwapPointer(&d.db, unsafe.Pointer(db)))
if old != nil {
old.close()
}
parent := filepath.Dir(oldDirName)
if err := os.RemoveAll(oldDirName); err != nil {
return err
}
return syncDir(parent)
}
// Close closes the state machine.
func (d *DiskKV) Close() error {
db := (*pebbledb)(atomic.SwapPointer(&d.db, unsafe.Pointer(nil)))
if db != nil {
d.closed = true
db.close()
} else {
if d.closed {
panic("close called twice")
}
}
return nil
}
// GetHash returns a hash value representing the state of the state machine.
func (d *DiskKV) GetHash() (uint64, error) {
h := md5.New()
db := (*pebbledb)(atomic.LoadPointer(&d.db))
ss := db.db.NewSnapshot()
db.mu.RLock()
defer db.mu.RUnlock()
if err := d.saveToWriter(db, ss, h); err != nil {
return 0, err
}
md5sum := h.Sum(nil)
return binary.LittleEndian.Uint64(md5sum[:8]), nil
}