-
Notifications
You must be signed in to change notification settings - Fork 1
/
kv_rocksdb.go
331 lines (310 loc) · 8.4 KB
/
kv_rocksdb.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
// 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 logdb
import (
"bytes"
"github.com/LiuzhouChan/go-paxos/internal/logdb/gorocksdb"
"github.com/LiuzhouChan/go-paxos/internal/settings"
"github.com/LiuzhouChan/go-paxos/internal/utils/fileutil"
"github.com/LiuzhouChan/go-paxos/paxosio"
)
const (
// LogDBType is the logdb type name
LogDBType = "sharded-rocksdb"
)
var (
logDBLRUCacheSize = int(settings.Soft.RDBLRUCacheSize)
maxBackgroundCompactions = int(settings.Soft.RDBMaxBackgroundCompactions)
maxBackgroundFlushes = int(settings.Soft.RDBMaxBackgroundFlushes)
)
type rocksdbKV struct {
directory string
bbto *gorocksdb.BlockBasedTableOptions
cache *gorocksdb.Cache
db *gorocksdb.DB
ro *gorocksdb.ReadOptions
wo *gorocksdb.WriteOptions
opts *gorocksdb.Options
}
func newKVStore(dir string, wal string) (IKvStore, error) {
return openRocksDB(dir, wal)
}
// FIXME:
// move these option parameters to the settings package to make it configurable
func getRocksDBOptions(directory string,
walDirectory string) (*gorocksdb.Options,
*gorocksdb.BlockBasedTableOptions, *gorocksdb.Cache) {
bbto := gorocksdb.NewDefaultBlockBasedTableOptions()
bbto.SetWholeKeyFiltering(true)
bbto.SetBlockSize(32 * 1024)
var cache *gorocksdb.Cache
if inMonkeyTesting {
cache = gorocksdb.NewLRUCache(1024 * 512)
bbto.SetBlockCache(cache)
} else {
if logDBLRUCacheSize > 0 {
cache = gorocksdb.NewLRUCache(logDBLRUCacheSize)
bbto.SetBlockCache(cache)
} else {
bbto.SetNoBlockCache(true)
}
}
opts := gorocksdb.NewDefaultOptions()
opts.SetMaxManifestFileSize(1024 * 1024 * 128)
opts.SetBlockBasedTableFactory(bbto)
opts.SetCreateIfMissing(true)
opts.SetUseFsync(true)
if len(walDirectory) > 0 {
opts.SetWalDir(walDirectory)
}
ddio := directIOSupported(directory)
wdio := directIOSupported(walDirectory)
if ddio && wdio {
opts.SetUseDirectIOForFlushAndCompaction(true)
}
opts.SetCompression(gorocksdb.NoCompression)
if inMonkeyTesting {
opts.SetWriteBufferSize(512 * 1024)
} else {
opts.SetWriteBufferSize(256 * 1024 * 1024)
}
// in normal mode, by default, we try to minimize write amplifcation, we have
// L0 size = 256MBytes * 2 (min_write_buffer_number_to_merge) * \
// 8 (level0_file_num_compaction_trigger)
// = 4GBytes
// L1 size close to L0, 4GBytes, max_bytes_for_level_base = 4GBytes,
// max_bytes_for_level_multiplier = 2
// L2 size is 8G, L3 is 16G, L4 is 32G, L5 64G...
//
// note this is the size of a shard, and the content of the rdb is expected
// to be compacted by raft.
//
// these three options will be noop when not using patched rocksdb.
opts.SetMaxSubCompactions(2)
opts.EnablePipelinedWrite(true)
// FIXME
// this seems to cause problem when used together with DeleteRange.
// see dragonboat-doc/known-issues.md for details.
// opts.SetMemtableInsertWithHintFixedLengthPrefixExtractor(20)
opts.SetMinWriteBufferNumberToMerge(2)
opts.SetLevel0FileNumCompactionTrigger(8)
opts.SetLevel0SlowdownWritesTrigger(17)
opts.SetLevel0StopWritesTrigger(24)
opts.SetMaxWriteBufferNumber(25)
opts.SetNumLevels(7)
// MaxBytesForLevelBase is the total size of L1, should be close to the size
// of L0
opts.SetMaxBytesForLevelBase(4 * 1024 * 1024 * 1024)
opts.SetMaxBytesForLevelMultiplier(2)
// files in L1 will have TargetFileSizeBase bytes
opts.SetTargetFileSizeBase(256 * 1024 * 1024)
opts.SetTargetFileSizeMultiplier(1)
// IO parallism
opts.SetMaxBackgroundCompactions(maxBackgroundCompactions)
opts.SetMaxBackgroundFlushes(maxBackgroundFlushes)
return opts, bbto, cache
}
func iteratorIsValid(iter *gorocksdb.Iterator) bool {
v, err := iter.IsValid()
if err != nil {
panic(err)
}
return v
}
func openRocksDB(dir string, wal string) (*rocksdbKV, error) {
// gorocksdb.OpenDb allows the main db directory to be created on open
// but WAL directory must exist before calling Open.
if len(wal) > 0 && !fileutil.Exist(wal) {
if err := fileutil.MkdirAll(wal); err != nil {
plog.Panicf("cannot create dir for RDB WAL (%v)", err)
}
}
if !fileutil.Exist(dir) {
if err := fileutil.MkdirAll(dir); err != nil {
plog.Panicf("cannot create dir (%v)", err)
}
}
opts, bbto, cache := getRocksDBOptions(dir, wal)
db, err := gorocksdb.OpenDb(opts, dir)
if err != nil {
return nil, err
}
wo := gorocksdb.NewDefaultWriteOptions()
wo.SetSync(true)
ro := gorocksdb.NewDefaultReadOptions()
ro.SetFillCache(false)
ro.SetTotalOrderSeek(true)
ro.IgnoreRangeDeletions(true)
return &rocksdbKV{
directory: dir,
bbto: bbto,
cache: cache,
db: db,
ro: ro,
wo: wo,
opts: opts,
}, nil
}
func (r *rocksdbKV) Name() string {
return "rocksdb"
}
// Close closes the RDB object.
func (r *rocksdbKV) Close() error {
if r.db != nil {
r.db.Close()
}
if r.cache != nil {
r.cache.Destroy()
}
if r.bbto != nil {
r.bbto.Destroy()
}
if r.opts != nil {
r.opts.Destroy()
}
if r.wo != nil {
r.wo.Destroy()
}
if r.ro != nil {
r.ro.Destroy()
}
r.db = nil
return nil
}
func (r *rocksdbKV) IterateValue(fk []byte, lk []byte, inc bool,
op func(key []byte, data []byte) (bool, error)) {
iter := r.db.NewIterator(r.ro)
defer iter.Close()
for iter.Seek(fk); iteratorIsValid(iter); iter.Next() {
key, ok := iter.OKey()
if !ok {
panic("failed to get key")
}
keyData := key.Data()
if inc {
if bytes.Compare(keyData, lk) > 0 {
key.Free()
return
}
} else {
if bytes.Compare(keyData, lk) >= 0 {
key.Free()
return
}
}
key.Free()
val, ok := iter.OValue()
if !ok {
panic("failed to get value")
}
valData := val.Data()
cont, err := op(keyData, valData)
if err != nil {
panic(err)
}
if !cont {
return
}
}
}
func (r *rocksdbKV) GetValue(key []byte,
op func([]byte) error) error {
val, err := r.db.Get(r.ro, key)
if err != nil {
return err
}
defer val.Free()
return op(val.Data())
}
func (r *rocksdbKV) SaveValue(key []byte, value []byte) error {
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
wb.Put(key, value)
return r.db.Write(r.wo, wb)
}
func (r *rocksdbKV) DeleteValue(key []byte) error {
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
wb.Delete(key)
return r.db.Write(r.wo, wb)
}
func (r *rocksdbKV) GetWriteBatch(ctx paxosio.IContext) IWriteBatch {
if ctx != nil {
wb := ctx.GetWriteBatch()
if wb != nil {
return ctx.GetWriteBatch().(*gorocksdb.WriteBatch)
}
}
return gorocksdb.NewWriteBatch()
}
func (r *rocksdbKV) CommitWriteBatch(wb IWriteBatch) error {
rocksdbwb, ok := wb.(*gorocksdb.WriteBatch)
if !ok {
panic("unknown type")
}
return r.db.Write(r.wo, rocksdbwb)
}
func (r *rocksdbKV) RemoveEntries(firstKey []byte, lastKey []byte) error {
if err := r.db.DeleteFileInRange(firstKey, lastKey); err != nil {
return err
}
return r.deleteRange(firstKey, lastKey)
}
func (r *rocksdbKV) Compaction(firstKey []byte, lastKey []byte) error {
opts := gorocksdb.NewCompactionOptions()
opts.SetExclusiveManualCompaction(false)
opts.SetForceBottommostLevelCompaction()
opts.SetChangeLevel(true)
opts.SetTargetLevel(-1)
defer opts.Destroy()
rng := gorocksdb.Range{
Start: firstKey,
Limit: lastKey,
}
r.db.CompactRangeWithOptions(opts, rng)
return nil
}
func (r *rocksdbKV) deleteRange(firstKey []byte, lastKey []byte) error {
iter := r.db.NewIterator(r.ro)
defer iter.Close()
toDelete := make([][]byte, 0)
for iter.Seek(firstKey); iteratorIsValid(iter); iter.Next() {
if done := func() bool {
key, ok := iter.OKey()
if !ok {
panic("failed to get key")
}
defer key.Free()
kd := key.Data()
if bytes.Compare(kd, lastKey) >= 0 {
return true
}
v := make([]byte, len(kd))
copy(v, kd)
toDelete = append(toDelete, v)
return false
}(); done {
break
}
}
wb := gorocksdb.NewWriteBatch()
defer wb.Destroy()
for _, key := range toDelete {
wb.Delete(key)
}
if wb.Count() > 0 {
return r.db.Write(r.wo, wb)
}
return nil
}