-
Notifications
You must be signed in to change notification settings - Fork 30
/
pebble.go
494 lines (420 loc) · 10 KB
/
pebble.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
package db
import (
"bytes"
"fmt"
"path/filepath"
"github.com/cockroachdb/pebble"
"github.com/spf13/cast"
)
// ForceSync
/*
This is set at compile time. Could be 0 or 1, defaults is 0.
It will force using Sync for NoSync functions (Set, Delete, Write)
Used as a workaround for chain-upgrade issue: At the upgrade-block, the sdk will panic without flushing data to disk or
closing dbs properly.
Upgrade guide:
1. After seeing `UPGRADE "xxxx" NEED at height....`, restart current version with `-X github.com/tendermint/tm-db.ForceSync=1`
2. Restart new version as normal
Example: Upgrading sifchain from v0.14.0 to v0.15.0
# log:
panic: UPGRADE "0.15.0" NEEDED at height: 8170210: {"binaries":{"linux/amd64":"https://github.com/Sifchain/sifnode/releases/download/v0.15.0/sifnoded-v0.15.0-linux-amd64.zip?checksum=0c03b5846c5a13dcc0d9d3127e4f0cee0aeddcf2165177b2f2e0d60dbcf1a5ea"}}
# step1
git reset --hard
git checkout v0.14.0
go mod edit -replace github.com/tendermint/tm-db=github.com/baabeetaa/tm-db@pebble
go mod tidy
go install -ldflags "-w -s -X github.com/cosmos/cosmos-sdk/types.DBBackend=pebbledb -X github.com/tendermint/tm-db.ForceSync=1" ./cmd/sifnoded
$HOME/go/bin/sifnoded start --db_backend=pebbledb
# step 2
git reset --hard
git checkout v0.15.0
go mod edit -replace github.com/tendermint/tm-db=github.com/baabeetaa/tm-db@pebble
go mod tidy
go install -ldflags "-w -s -X github.com/cosmos/cosmos-sdk/types.DBBackend=pebbledb" ./cmd/sifnoded
$HOME/go/bin/sifnoded start --db_backend=pebbledb
*/
var (
ForceSync = "0"
isForceSync = false
)
func init() {
dbCreator := func(name string, dir string, opts Options) (DB, error) {
return NewPebbleDB(name, dir, opts)
}
registerDBCreator(PebbleDBBackend, dbCreator, false)
if ForceSync == "1" {
isForceSync = true
}
}
// PebbleDB is a PebbleDB backend.
type PebbleDB struct {
db *pebble.DB
}
var _ DB = (*PebbleDB)(nil)
func NewPebbleDB(name string, dir string, opts Options) (DB, error) {
do := &pebble.Options{
MaxConcurrentCompactions: func() int { return 3 }, // default 1
}
do.EnsureDefaults()
if opts != nil {
files := cast.ToInt(opts.Get("maxopenfiles"))
if files > 0 {
do.MaxOpenFiles = files
}
}
dbPath := filepath.Join(dir, name+DBFileSuffix)
p, err := pebble.Open(dbPath, do)
if err != nil {
return nil, err
}
return &PebbleDB{
db: p,
}, err
}
// Get implements DB.
func (db *PebbleDB) Get(key []byte) ([]byte, error) {
// fmt.Println("PebbleDB.Get")
if len(key) == 0 {
return nil, errKeyEmpty
}
res, closer, err := db.db.Get(key)
if err != nil {
if err == pebble.ErrNotFound {
return nil, nil
}
return nil, err
}
defer closer.Close()
return cp(res), nil
}
// Has implements DB.
func (db *PebbleDB) Has(key []byte) (bool, error) {
// fmt.Println("PebbleDB.Has")
if len(key) == 0 {
return false, errKeyEmpty
}
bytes, err := db.Get(key)
if err != nil {
return false, err
}
return bytes != nil, nil
}
// Set implements DB.
func (db *PebbleDB) Set(key []byte, value []byte) error {
// fmt.Println("PebbleDB.Set")
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
wopts := pebble.NoSync
if isForceSync {
wopts = pebble.Sync
}
err := db.db.Set(key, value, wopts)
if err != nil {
return err
}
return nil
}
// SetSync implements DB.
func (db *PebbleDB) SetSync(key []byte, value []byte) error {
// fmt.Println("PebbleDB.SetSync")
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
err := db.db.Set(key, value, pebble.Sync)
if err != nil {
return err
}
return nil
}
// Delete implements DB.
func (db *PebbleDB) Delete(key []byte) error {
// fmt.Println("PebbleDB.Delete")
if len(key) == 0 {
return errKeyEmpty
}
wopts := pebble.NoSync
if isForceSync {
wopts = pebble.Sync
}
return db.db.Delete(key, wopts)
}
// DeleteSync implements DB.
func (db PebbleDB) DeleteSync(key []byte) error {
// fmt.Println("PebbleDB.DeleteSync")
if len(key) == 0 {
return errKeyEmpty
}
return db.db.Delete(key, pebble.Sync)
}
func (db *PebbleDB) DB() *pebble.DB {
return db.db
}
// Close implements DB.
func (db PebbleDB) Close() error {
// fmt.Println("PebbleDB.Close")
db.db.Close()
return nil
}
// Print implements DB.
func (db *PebbleDB) Print() error {
itr, err := db.Iterator(nil, nil)
if err != nil {
return err
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
value := itr.Value()
fmt.Printf("[%X]:\t[%X]\n", key, value)
}
return nil
}
// Stats implements DB.
func (db *PebbleDB) Stats() map[string]string {
/*
keys := []string{"rocksdb.stats"}
stats := make(map[string]string, len(keys))
for _, key := range keys {
stats[key] = db.(key)
}
*/
return nil
}
// NewBatch implements DB.
func (db *PebbleDB) NewBatch() Batch {
return newPebbleDBBatch(db)
}
// NewBatchWithSize implements DB.
// It does the same thing as NewBatch because we can't pre-allocate pebbleDBBatch
func (db *PebbleDB) NewBatchWithSize(size int) Batch {
return newPebbleDBBatch(db)
}
// Iterator implements DB.
func (db *PebbleDB) Iterator(start, end []byte) (Iterator, error) {
// fmt.Println("PebbleDB.Iterator")
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
o := pebble.IterOptions{
LowerBound: start,
UpperBound: end,
}
itr, err := db.db.NewIter(&o)
if err != nil {
return nil, err
}
itr.First()
return newPebbleDBIterator(itr, start, end, false), nil
}
// ReverseIterator implements DB.
func (db *PebbleDB) ReverseIterator(start, end []byte) (Iterator, error) {
// fmt.Println("PebbleDB.ReverseIterator")
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}
o := pebble.IterOptions{
LowerBound: start,
UpperBound: end,
}
itr, err := db.db.NewIter(&o)
if err != nil {
return nil, err
}
itr.Last()
return newPebbleDBIterator(itr, start, end, true), nil
}
var _ Batch = (*pebbleDBBatch)(nil)
type pebbleDBBatch struct {
db *PebbleDB
batch *pebble.Batch
}
var _ Batch = (*pebbleDBBatch)(nil)
func newPebbleDBBatch(db *PebbleDB) *pebbleDBBatch {
return &pebbleDBBatch{
batch: db.db.NewBatch(),
}
}
// Set implements Batch.
func (b *pebbleDBBatch) Set(key, value []byte) error {
// fmt.Println("pebbleDBBatch.Set")
if len(key) == 0 {
return errKeyEmpty
}
if value == nil {
return errValueNil
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Set(key, value, nil)
return nil
}
// Delete implements Batch.
func (b *pebbleDBBatch) Delete(key []byte) error {
// fmt.Println("pebbleDBBatch.Delete")
if len(key) == 0 {
return errKeyEmpty
}
if b.batch == nil {
return errBatchClosed
}
b.batch.Delete(key, nil)
return nil
}
// Write implements Batch.
func (b *pebbleDBBatch) Write() error {
// fmt.Println("pebbleDBBatch.Write")
if b.batch == nil {
return errBatchClosed
}
wopts := pebble.NoSync
if isForceSync {
wopts = pebble.Sync
}
err := b.batch.Commit(wopts)
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
return b.Close()
}
// WriteSync implements Batch.
func (b *pebbleDBBatch) WriteSync() error {
// fmt.Println("pebbleDBBatch.WriteSync")
if b.batch == nil {
return errBatchClosed
}
err := b.batch.Commit(pebble.Sync)
if err != nil {
return err
}
// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
return b.Close()
}
// Close implements Batch.
func (b *pebbleDBBatch) Close() error {
// fmt.Println("pebbleDBBatch.Close")
if b.batch != nil {
err := b.batch.Close()
if err != nil {
return err
}
b.batch = nil
}
return nil
}
// GetByteSize implements Batch
func (b *pebbleDBBatch) GetByteSize() (int, error) {
if b.batch == nil {
return 0, errBatchClosed
}
return b.batch.Len(), nil
}
type pebbleDBIterator struct {
source *pebble.Iterator
start, end []byte
isReverse bool
isInvalid bool
}
var _ Iterator = (*pebbleDBIterator)(nil)
func newPebbleDBIterator(source *pebble.Iterator, start, end []byte, isReverse bool) *pebbleDBIterator {
if isReverse {
if end == nil {
source.Last()
}
} else {
if start == nil {
source.First()
}
}
return &pebbleDBIterator{
source: source,
start: start,
end: end,
isReverse: isReverse,
isInvalid: false,
}
}
// Domain implements Iterator.
func (itr *pebbleDBIterator) Domain() ([]byte, []byte) {
// fmt.Println("pebbleDBIterator.Domain")
return itr.start, itr.end
}
// Valid implements Iterator.
func (itr *pebbleDBIterator) Valid() bool {
// fmt.Println("pebbleDBIterator.Valid")
// Once invalid, forever invalid.
if itr.isInvalid {
return false
}
// If source has error, invalid.
if err := itr.source.Error(); err != nil {
itr.isInvalid = true
return false
}
// If source is invalid, invalid.
if !itr.source.Valid() {
itr.isInvalid = true
return false
}
// If key is end or past it, invalid.
start := itr.start
end := itr.end
key := itr.source.Key()
if itr.isReverse {
if start != nil && bytes.Compare(key, start) < 0 {
itr.isInvalid = true
return false
}
} else {
if end != nil && bytes.Compare(end, key) <= 0 {
itr.isInvalid = true
return false
}
}
// It's valid.
return true
}
// Key implements Iterator.
func (itr *pebbleDBIterator) Key() []byte {
// fmt.Println("pebbleDBIterator.Key")
itr.assertIsValid()
return cp(itr.source.Key())
}
// Value implements Iterator.
func (itr *pebbleDBIterator) Value() []byte {
// fmt.Println("pebbleDBIterator.Value")
itr.assertIsValid()
return cp(itr.source.Value())
}
// Next implements Iterator.
func (itr pebbleDBIterator) Next() {
// fmt.Println("pebbleDBIterator.Next")
itr.assertIsValid()
if itr.isReverse {
itr.source.Prev()
} else {
itr.source.Next()
}
}
// Error implements Iterator.
func (itr *pebbleDBIterator) Error() error {
return itr.source.Error()
}
// Close implements Iterator.
func (itr *pebbleDBIterator) Close() error {
// fmt.Println("pebbleDBIterator.Close")
return itr.source.Close()
}
func (itr *pebbleDBIterator) assertIsValid() {
if !itr.Valid() {
panic("iterator is invalid")
}
}