forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
573 lines (519 loc) · 15.6 KB
/
db.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
package lotusdb
import (
"context"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
"reflect"
"sync"
"syscall"
"time"
"github.com/dgraph-io/badger/v4/y"
"github.com/gofrs/flock"
"github.com/rosedblabs/diskhash"
"github.com/rosedblabs/wal"
"golang.org/x/sync/errgroup"
)
const (
fileLockName = "FLOCK"
)
// DB is the main structure of the LotusDB database.
// It contains all the information needed to operate the database.
//
// DB is thread-safe, and can be used by multiple goroutines.
// But you can not open multiple DBs with the same directory path at the same time.
// ErrDatabaseIsUsing will be returned if you do so.
//
// LotusDB is the most advanced key-value database written in Go.
// It combines the advantages of LSM tree and B+ tree, read and write are both very fast.
// It is also very memory efficient, and can store billions of key-value pairs in a single machine.
type DB struct {
activeMem *memtable // Active memtable for writing.
immuMems []*memtable // Immutable memtables, waiting to be flushed to disk.
index Index // index is multi-partition indexes to store key and chunk position.
vlog *valueLog // vlog is the value log.
fileLock *flock.Flock // fileLock to prevent multiple processes from using the same database directory.
flushChan chan *memtable // flushChan is used to notify the flush goroutine to flush memtable to disk.
flushLock sync.Mutex // flushLock is to prevent flush running while compaction doesn't occur
mu sync.RWMutex
closed bool
closeChan chan struct{}
options Options
batchPool sync.Pool // batchPool is a pool of batch, to reduce the cost of memory allocation.
}
// Open a database with the specified options.
// If the database directory does not exist, it will be created automatically.
//
// Multiple processes can not use the same database directory at the same time,
// otherwise it will return ErrDatabaseIsUsing.
//
// It will first open the wal to rebuild the memtable, then open the index and value log.
// Return the DB object if succeeded, otherwise return the error.
func Open(options Options) (*DB, error) {
// check whether all options are valid
if err := validateOptions(&options); err != nil {
return nil, err
}
// create data directory if not exist
if _, err := os.Stat(options.DirPath); err != nil {
if err := os.MkdirAll(options.DirPath, os.ModePerm); err != nil {
return nil, err
}
}
// create file lock, prevent multiple processes from using the same database directory
fileLock := flock.New(filepath.Join(options.DirPath, fileLockName))
hold, err := fileLock.TryLock()
if err != nil {
return nil, err
}
if !hold {
return nil, ErrDatabaseIsUsing
}
// open all memtables
memtables, err := openAllMemtables(options)
if err != nil {
return nil, err
}
// open index
index, err := openIndex(indexOptions{
indexType: options.IndexType,
dirPath: options.DirPath,
partitionNum: options.PartitionNum,
keyHashFunction: options.KeyHashFunction,
})
if err != nil {
return nil, err
}
// open value log
vlog, err := openValueLog(valueLogOptions{
dirPath: options.DirPath,
segmentSize: options.ValueLogFileSize,
blockCache: options.BlockCache,
partitionNum: uint32(options.PartitionNum),
hashKeyFunction: options.KeyHashFunction,
compactBatchCount: options.CompactBatchCount,
})
if err != nil {
return nil, err
}
db := &DB{
activeMem: memtables[len(memtables)-1],
immuMems: memtables[:len(memtables)-1],
index: index,
vlog: vlog,
fileLock: fileLock,
flushChan: make(chan *memtable, options.MemtableNums-1),
closeChan: make(chan struct{}),
options: options,
batchPool: sync.Pool{New: makeBatch},
}
// if there are some immutable memtables when opening the database, flush them to disk
if len(db.immuMems) > 0 {
for _, table := range db.immuMems {
db.flushMemtable(table)
}
}
// start flush memtables goroutine asynchronously,
// memtables with new coming writes will be flushed to disk if the active memtable is full.
go db.listenMemtableFlush()
return db, nil
}
// Close the database, close all data files and release file lock.
// Set the closed flag to true.
// The DB instance cannot be used after closing.
func (db *DB) Close() error {
db.mu.Lock()
defer db.mu.Unlock()
close(db.flushChan)
<-db.closeChan
// close all memtables
for _, table := range db.immuMems {
if err := table.close(); err != nil {
return err
}
}
if err := db.activeMem.close(); err != nil {
return err
}
// close index
if err := db.index.Close(); err != nil {
return err
}
// close value log
if err := db.vlog.close(); err != nil {
return err
}
// release file lock
if err := db.fileLock.Unlock(); err != nil {
return err
}
db.closed = true
return nil
}
// Sync all data files to the underlying storage.
func (db *DB) Sync() error {
db.mu.Lock()
defer db.mu.Unlock()
// sync all wal of memtables
for _, table := range db.immuMems {
if err := table.sync(); err != nil {
return err
}
}
if err := db.activeMem.sync(); err != nil {
return err
}
// sync index
if err := db.index.Sync(); err != nil {
return err
}
// sync value log
if err := db.vlog.sync(); err != nil {
return err
}
return nil
}
// Put a key-value pair into the database.
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Put operation.
func (db *DB) Put(key []byte, value []byte, options *WriteOptions) error {
batch := db.batchPool.Get().(*Batch)
defer func() {
batch.reset()
db.batchPool.Put(batch)
}()
// This is a single put operation, we can set Sync to false.
// Because the data will be written to the WAL,
// and the WAL file will be synced to disk according to the DB options.
batch.init(false, false, db).withPendingWrites()
if err := batch.Put(key, value); err != nil {
batch.unlock()
return err
}
return batch.Commit(options)
}
// Get the value of the specified key from the database.
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Get operation.
func (db *DB) Get(key []byte) ([]byte, error) {
batch := db.batchPool.Get().(*Batch)
batch.init(true, false, db)
defer func() {
_ = batch.Commit(nil)
batch.reset()
db.batchPool.Put(batch)
}()
return batch.Get(key)
}
// Delete the specified key from the database.
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Delete operation.
func (db *DB) Delete(key []byte, options *WriteOptions) error {
batch := db.batchPool.Get().(*Batch)
defer func() {
batch.reset()
db.batchPool.Put(batch)
}()
// This is a single delete operation, we can set Sync to false.
// Because the data will be written to the WAL,
// and the WAL file will be synced to disk according to the DB options.
batch.init(false, false, db).withPendingWrites()
if err := batch.Delete(key); err != nil {
batch.unlock()
return err
}
return batch.Commit(options)
}
// Exist checks if the specified key exists in the database.
// Actually, it will open a new batch and commit it.
// You can think the batch has only one Exist operation.
func (db *DB) Exist(key []byte) (bool, error) {
batch := db.batchPool.Get().(*Batch)
batch.init(true, false, db)
defer func() {
_ = batch.Commit(nil)
batch.reset()
db.batchPool.Put(batch)
}()
return batch.Exist(key)
}
// validateOptions validates the given options.
func validateOptions(options *Options) error {
if options.DirPath == "" {
return ErrDBDirectoryISEmpty
}
if options.MemtableSize <= 0 {
options.MemtableSize = 64 << 20 // 64MB
}
if options.MemtableNums <= 0 {
options.MemtableNums = 15
}
if options.PartitionNum <= 0 {
options.PartitionNum = 5
}
if options.ValueLogFileSize <= 0 {
options.ValueLogFileSize = 1 << 30 // 1GB
}
// assure ValueLogFileSize >= MemtableSize
if options.ValueLogFileSize < int64(options.MemtableSize) {
options.ValueLogFileSize = int64(options.MemtableSize)
}
return nil
}
// get all memtables, including active memtable and immutable memtables.
// must be called with db.mu held.
func (db *DB) getMemTables() []*memtable {
var tables []*memtable
tables = append(tables, db.activeMem)
last := len(db.immuMems) - 1
for i := range db.immuMems {
tables = append(tables, db.immuMems[last-i])
}
return tables
}
// waitMemtableSpace waits for space in the memtable.
// If the active memtable is full, it will be flushed to disk by the background goroutine.
// But if the flush speed is slower than the write speed, there may be no space in the memtable.
// So the write operation will wait for space in the memtable, and the timeout is specified by WaitMemSpaceTimeout.
func (db *DB) waitMemtableSpace() error {
if !db.activeMem.isFull() {
return nil
}
timer := time.NewTimer(db.options.WaitMemSpaceTimeout)
defer timer.Stop()
select {
case db.flushChan <- db.activeMem:
db.immuMems = append(db.immuMems, db.activeMem)
options := db.activeMem.options
options.tableId++
// open a new memtable for writing
table, err := openMemtable(options)
if err != nil {
return err
}
db.activeMem = table
case <-timer.C:
return ErrWaitMemtableSpaceTimeOut
}
return nil
}
// flushMemtable flushes the specified memtable to disk.
// Following steps will be done:
// 1. Iterate all records in memtable, divide them into deleted keys and log records.
// 2. Write the log records to value log, get the positions of keys.
// 3. Write all keys and positions to index.
// 4. Delete the deleted keys from index.
// 5. Delete the wal.
func (db *DB) flushMemtable(table *memtable) {
db.flushLock.Lock()
defer db.flushLock.Unlock()
sklIter := table.skl.NewIterator()
var deletedKeys [][]byte
var logRecords []*ValueLogRecord
// iterate all records in memtable, divide them into deleted keys and log records
for sklIter.SeekToFirst(); sklIter.Valid(); sklIter.Next() {
key, valueStruct := y.ParseKey(sklIter.Key()), sklIter.Value()
if valueStruct.Meta == LogRecordDeleted {
deletedKeys = append(deletedKeys, key)
} else {
logRecord := ValueLogRecord{key: key, value: valueStruct.Value}
logRecords = append(logRecords, &logRecord)
}
}
_ = sklIter.Close()
// write to value log, get the positions of keys
keyPos, err := db.vlog.writeBatch(logRecords)
if err != nil {
log.Println("vlog writeBatch failed:", err)
return
}
// sync the value log
if err := db.vlog.sync(); err != nil {
log.Println("vlog sync failed:", err)
return
}
// write all keys and positions to index
var putMatchKeys []diskhash.MatchKeyFunc
if db.options.IndexType == Hash && len(keyPos) > 0 {
putMatchKeys = make([]diskhash.MatchKeyFunc, len(keyPos))
for i := range putMatchKeys {
putMatchKeys[i] = MatchKeyFunc(db, keyPos[i].key, nil, nil)
}
}
if err := db.index.PutBatch(keyPos, putMatchKeys...); err != nil {
log.Println("index PutBatch failed:", err)
return
}
// delete the deleted keys from index
var deleteMatchKeys []diskhash.MatchKeyFunc
if db.options.IndexType == Hash && len(deletedKeys) > 0 {
deleteMatchKeys = make([]diskhash.MatchKeyFunc, len(deletedKeys))
for i := range deleteMatchKeys {
deleteMatchKeys[i] = MatchKeyFunc(db, deletedKeys[i], nil, nil)
}
}
if err := db.index.DeleteBatch(deletedKeys, deleteMatchKeys...); err != nil {
log.Println("index DeleteBatch failed:", err)
return
}
// sync the index
if err := db.index.Sync(); err != nil {
log.Println("index sync failed:", err)
return
}
// delete the wal
if err := table.deleteWAl(); err != nil {
log.Println("delete wal failed:", err)
return
}
// delete old memtable kept in memory
db.mu.Lock()
if table == db.activeMem {
options := db.activeMem.options
options.tableId++
// open a new memtable for writing
table, err := openMemtable(options)
if err != nil {
panic("flush activate memtable wrong")
}
db.activeMem = table
} else {
if len(db.immuMems) == 1 {
db.immuMems = db.immuMems[:0]
} else {
db.immuMems = db.immuMems[1:]
}
}
db.mu.Unlock()
}
func (db *DB) listenMemtableFlush() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
select {
case table, ok := <-db.flushChan:
if ok {
db.flushMemtable(table)
} else {
db.closeChan <- struct{}{}
return
}
case <-sig:
return
}
}
}
// Compact will iterate all values in vlog, and write the valid values to a new vlog file.
// Then replace the old vlog file with the new one, and delete the old one.
func (db *DB) Compact() error {
db.flushLock.Lock()
defer db.flushLock.Unlock()
openVlogFile := func(part int, ext string) *wal.WAL {
walFile, err := wal.Open(wal.Options{
DirPath: db.vlog.options.dirPath,
SegmentSize: db.vlog.options.segmentSize,
SegmentFileExt: fmt.Sprintf(ext, part),
BlockCache: db.vlog.options.blockCache,
Sync: false, // we will sync manually
BytesPerSync: 0, // the same as Sync
})
if err != nil {
_ = walFile.Delete()
panic(err)
}
return walFile
}
g, _ := errgroup.WithContext(context.Background())
for i := 0; i < int(db.vlog.options.partitionNum); i++ {
part := i
g.Go(func() error {
newVlogFile := openVlogFile(part, tempValueLogFileExt)
validRecords := make([]*ValueLogRecord, 0, db.vlog.options.compactBatchCount)
reader := db.vlog.walFiles[part].NewReader()
count := 0
// iterate all records in wal, find the valid records
for {
count++
chunk, pos, err := reader.Next()
if err != nil {
if err == io.EOF {
break
}
_ = newVlogFile.Delete()
return err
}
record := decodeValueLogRecord(chunk)
var hashTableKeyPos *KeyPosition
var matchKey func(diskhash.Slot) (bool, error)
if db.options.IndexType == Hash {
matchKey = MatchKeyFunc(db, record.key, &hashTableKeyPos, nil)
}
keyPos, err := db.index.Get(record.key, matchKey)
if err != nil {
_ = newVlogFile.Delete()
return err
}
if db.options.IndexType == Hash {
keyPos = hashTableKeyPos
}
if keyPos == nil {
continue
}
if keyPos.partition == uint32(part) && reflect.DeepEqual(keyPos.position, pos) {
validRecords = append(validRecords, record)
}
if count%db.vlog.options.compactBatchCount == 0 {
err := db.rewriteValidRecords(newVlogFile, validRecords, part)
if err != nil {
_ = newVlogFile.Delete()
return err
}
validRecords = validRecords[:0]
}
}
if len(validRecords) > 0 {
err := db.rewriteValidRecords(newVlogFile, validRecords, part)
if err != nil {
_ = newVlogFile.Delete()
return err
}
}
// replace the wal with the new one.
_ = db.vlog.walFiles[part].Delete()
_ = newVlogFile.Close()
if err := newVlogFile.RenameFileExt(fmt.Sprintf(valueLogFileExt, part)); err != nil {
return err
}
db.vlog.walFiles[part] = openVlogFile(part, valueLogFileExt)
return nil
})
}
return g.Wait()
}
func (db *DB) rewriteValidRecords(walFile *wal.WAL, validRecords []*ValueLogRecord, part int) error {
for _, record := range validRecords {
walFile.PendingWrites(encodeValueLogRecord(record))
}
walChunkPositions, err := walFile.WriteAll()
if err != nil {
return err
}
positions := make([]*KeyPosition, len(walChunkPositions))
for i, walChunkPosition := range walChunkPositions {
positions[i] = &KeyPosition{
key: validRecords[i].key,
partition: uint32(part),
position: walChunkPosition,
}
}
matchKeys := make([]diskhash.MatchKeyFunc, len(positions))
if db.options.IndexType == Hash {
for i := range matchKeys {
matchKeys[i] = MatchKeyFunc(db, positions[i].key, nil, nil)
}
}
return db.index.PutBatch(positions, matchKeys...)
}