-
Notifications
You must be signed in to change notification settings - Fork 127
/
badger.go
89 lines (79 loc) · 2.24 KB
/
badger.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
package storage
import (
"sync"
"time"
"github.com/MixinNetwork/mixin/config"
"github.com/MixinNetwork/mixin/logger"
"github.com/dgraph-io/badger/v4"
"github.com/dgraph-io/badger/v4/options"
)
type BadgerStore struct {
custom *config.Custom
snapshotsDB *badger.DB
cacheDB *badger.DB
mutex *sync.RWMutex
closing bool
}
func NewBadgerStore(custom *config.Custom, dir string) (*BadgerStore, error) {
snapshotsDB, err := openDB(dir+"/snapshots", true, custom)
if err != nil {
return nil, err
}
cacheDB, err := openDB(dir+"/cache", false, custom)
if err != nil {
return nil, err
}
return &BadgerStore{
custom: custom,
snapshotsDB: snapshotsDB,
cacheDB: cacheDB,
mutex: new(sync.RWMutex),
closing: false,
}, nil
}
func (store *BadgerStore) Close() error {
store.closing = true
err := store.snapshotsDB.Close()
if err != nil {
return err
}
return store.cacheDB.Close()
}
func openDB(dir string, sync bool, custom *config.Custom) (*badger.DB, error) {
opts := badger.DefaultOptions(dir)
opts = opts.WithSyncWrites(sync)
opts = opts.WithCompression(options.None)
opts = opts.WithBlockCacheSize(0)
opts = opts.WithIndexCacheSize(0)
opts = opts.WithMetricsEnabled(false)
opts = opts.WithLoggingLevel(badger.WARNING)
// these three options control the maximum database size
// for level up to max levels: sum(base * (multiplier ** level))
// increase the level to 8 when data grows big to execeed 16TB
// the memory usage will increase for hours to compact when level up
// the max levels can not be decreased once up, so be cautious
opts = opts.WithBaseLevelSize(16 << 20)
opts = opts.WithLevelSizeMultiplier(16)
opts = opts.WithMaxLevels(7)
if custom != nil && custom.Storage.MaxCompactionLevels > 0 {
opts = opts.WithMaxLevels(custom.Storage.MaxCompactionLevels)
}
db, err := badger.Open(opts)
if err != nil {
return nil, err
}
if custom != nil && custom.Storage.ValueLogGC {
go func() {
for {
lsm, vlog := db.Size()
logger.Printf("Badger LSM %d VLOG %d\n", lsm, vlog)
if lsm > 1024*1024*8 || vlog > 1024*1024*32 {
err := db.RunValueLogGC(0.5)
logger.Printf("Badger RunValueLogGC %v\n", err)
}
time.Sleep(5 * time.Minute)
}
}()
}
return db, nil
}