-
Notifications
You must be signed in to change notification settings - Fork 137
/
Copy pathbadger_database.go
909 lines (776 loc) · 24.3 KB
/
badger_database.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
// Copyright 2024 Coinbase, Inc.
//
// 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 database
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"sync"
"time"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/options"
"github.com/fatih/color"
"github.com/coinbase/rosetta-sdk-go/storage/encoder"
storageErrs "github.com/coinbase/rosetta-sdk-go/storage/errors"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/coinbase/rosetta-sdk-go/utils"
)
const (
// DefaultBlockCacheSize is 0 MB.
DefaultBlockCacheSize = 0
// DefaultIndexCacheSize is 2 GB.
DefaultIndexCacheSize = 2000 << 20
// TinyIndexCacheSize is 10 MB.
TinyIndexCacheSize = 10 << 20
// DefaultMaxTableSize is 256 MB. The larger
// this value is, the larger database transactions
// storage can handle (~15% of the max table size
// == max commit size).
DefaultMaxTableSize = 256 << 20
// DefaultLogValueSize is 64 MB.
DefaultLogValueSize = 64 << 20
// PerformanceMaxTableSize is 3072 MB. The larger
// this value is, the larger database transactions
// storage can handle (~15% of the max table size
// == max commit size).
PerformanceMaxTableSize = 3072 << 20
// PerformanceLogValueSize is 256 MB.
PerformanceLogValueSize = 256 << 20
// AllInMemoryTableSize is 2048 MB.
AllInMemoryTableSize = 2048 << 20
// PerformanceLogValueSize is 512 MB.
AllInMemoryLogValueSize = 512 << 20
// DefaultCompressionMode is the default block
// compression setting.
DefaultCompressionMode = options.None
// logModulo determines how often we should print
// logs while scanning data.
logModulo = 5000
// Default GC settings for reclaiming
// space in value logs.
defaultGCInterval = 1 * time.Minute
defualtGCDiscardRatio = 0.1
defaultGCSleep = 10 * time.Second
)
// BadgerDatabase is a wrapper around Badger DB
// that implements the Database interface.
type BadgerDatabase struct {
badgerOptions badger.Options
compressorEntries []*encoder.CompressorEntry
pool *encoder.BufferPool
db *badger.DB
encoder *encoder.Encoder
compress bool
writer *utils.MutexMap
writerShards int
metaData string
// Track the closed status to ensure we exit garbage
// collection when the db closes.
closed chan struct{}
}
// DefaultBadgerOptions are the default options used to initialized
// a new BadgerDB. These settings override many of the default BadgerDB
// settings to restrict memory usage to ~6 GB. If constraining memory
// usage is not desired for your use case, you can provide your own
// BadgerDB settings with the configuration option WithCustomSettings.
//
// There are many threads about optimizing memory usage in Badger (which
// can grow to many GBs if left untuned). Our own research indicates
// that each MB increase in MaxTableSize and/or ValueLogFileSize corresponds
// to a 10 MB increase in RAM usage (all other settings equal). Our primary
// concern is large database transaction size, so we configure MaxTableSize
// to be 4 times the size of ValueLogFileSize (if we skewed any further to
// MaxTableSize, we would quickly hit the default open file limit on many OSes).
func DefaultBadgerOptions(dir string) badger.Options {
opts := badger.DefaultOptions(dir)
// By default, we do not compress the table at all. Doing so can
// significantly increase memory usage.
opts.Compression = DefaultCompressionMode
// Use an extended table size for larger commits.
opts.MaxTableSize = DefaultMaxTableSize
opts.ValueLogFileSize = DefaultLogValueSize
// Don't load tables into memory.
opts.TableLoadingMode = options.FileIO
opts.ValueLogLoadingMode = options.FileIO
// To allow writes at a faster speed, we create a new memtable as soon as
// an existing memtable is filled up. This option determines how many
// memtables should be kept in memory.
opts.NumMemtables = 1
// Don't keep multiple memtables in memory. With larger
// memtable size, this explodes memory usage.
opts.NumLevelZeroTables = 1
opts.NumLevelZeroTablesStall = 2
// This option will have a significant effect the memory. If the level is kept
// in-memory, read are faster but the tables will be kept in memory. By default,
// this is set to false.
opts.KeepL0InMemory = false
// We don't compact L0 on close as this can greatly delay shutdown time.
opts.CompactL0OnClose = false
// LoadBloomsOnOpen=false will improve the db startup speed. This is also
// a waste to enable with a limited index cache size (as many of the loaded bloom
// filters will be immediately discarded from the cache).
opts.LoadBloomsOnOpen = false
// This value specifies how much memory should be used by table indices. These
// indices include the block offsets and the bloomfilters. Badger uses bloom
// filters to speed up lookups. Each table has its own bloom
// filter and each bloom filter is approximately of 5 MB. This defaults
// to an unlimited size (and quickly balloons to GB with a large DB).
opts.IndexCacheSize = DefaultIndexCacheSize
// Don't cache blocks in memory. All reads should go to disk.
opts.BlockCacheSize = DefaultBlockCacheSize
return opts
}
// PerformanceBadgerOptions are performance geared
// BadgerDB options that use much more RAM than the
// default settings.
func PerformanceBadgerOptions(dir string) badger.Options {
opts := badger.DefaultOptions(dir)
// By default, we do not compress the table at all. Doing so can
// significantly increase memory usage.
opts.Compression = DefaultCompressionMode
// Use an extended table size for larger commits.
opts.MaxTableSize = PerformanceMaxTableSize
opts.ValueLogFileSize = PerformanceLogValueSize
// Load tables into memory and memory map value logs.
opts.TableLoadingMode = options.MemoryMap
opts.ValueLogLoadingMode = options.MemoryMap
// This option will have a significant effect the memory. If the level is kept
// in-memory, read are faster but the tables will be kept in memory. By default,
// this is set to false.
opts.KeepL0InMemory = true
// We don't compact L0 on close as this can greatly delay shutdown time.
opts.CompactL0OnClose = false
// LoadBloomsOnOpen=false will improve the db startup speed. This is also
// a waste to enable with a limited index cache size (as many of the loaded bloom
// filters will be immediately discarded from the cache).
opts.LoadBloomsOnOpen = true
return opts
}
// AllInMemoryBadgerOptions are performance geared
// BadgerDB options that use much more RAM than the
// default settings and PerformanceBadger settings
func AllInMemoryBadgerOptions(dir string) badger.Options {
opts := badger.DefaultOptions("")
// By default, we do not compress the table at all. Doing so can
// significantly increase memory usage.
opts.Compression = DefaultCompressionMode
// Use an extended table size for larger commits.
opts.MaxTableSize = AllInMemoryTableSize
opts.ValueLogFileSize = AllInMemoryLogValueSize
// Load tables into memory and memory map value logs.
opts.TableLoadingMode = options.MemoryMap
opts.ValueLogLoadingMode = options.MemoryMap
// This option will have a significant effect the memory. If all the levels are kept
// in-memory, read are faster but the tables will be kept in memory. By default,
// this is set to false.
opts.InMemory = true
// We don't compact L0 on close as this can greatly delay shutdown time.
opts.CompactL0OnClose = false
// LoadBloomsOnOpen=false will improve the db startup speed. This is also
// a waste to enable with a limited index cache size (as many of the loaded bloom
// filters will be immediately discarded from the cache).
opts.LoadBloomsOnOpen = true
return opts
}
// NewBadgerDatabase creates a new BadgerDatabase.
func NewBadgerDatabase(
ctx context.Context,
dir string,
storageOptions ...BadgerOption,
) (Database, error) {
dir = path.Clean(dir)
b := &BadgerDatabase{
badgerOptions: DefaultBadgerOptions(dir),
closed: make(chan struct{}),
pool: encoder.NewBufferPool(),
compress: true,
writerShards: utils.DefaultShards,
}
for _, opt := range storageOptions {
opt(b)
}
// Initialize utis.MutexMap used to track granular
// write transactions.
b.writer = utils.NewMutexMap(b.writerShards)
db, err := badger.Open(b.badgerOptions)
if err != nil {
err = fmt.Errorf("unable to open database: %w%s", err, b.metaData)
color.Red(err.Error())
return nil, err
}
b.db = db
encoder, err := encoder.NewEncoder(b.compressorEntries, b.pool, b.compress)
if err != nil {
err = fmt.Errorf("unable to load compressor: %w%s", err, b.metaData)
color.Red(err.Error())
return nil, err
}
b.encoder = encoder
// Start periodic ValueGC goroutine (up to user of BadgerDB to call
// periodically to reclaim value logs on-disk).
go b.periodicGC(ctx)
return b, nil
}
// Close closes the database to prevent corruption.
// The caller should defer this in main.
func (b *BadgerDatabase) Close(ctx context.Context) error {
// Trigger shutdown for the garabage collector
close(b.closed)
if err := b.db.Close(); err != nil {
err = fmt.Errorf("unable to close badger database: %w%s", err, b.metaData)
color.Red(err.Error())
return err
}
return nil
}
// periodicGC attempts to reclaim storage every
// defaultGCInterval.
//
// Inspired by:
// https://github.com/ipfs/go-ds-badger/blob/a69f1020ba3954680900097e0c9d0181b88930ad/datastore.go#L173-L199
func (b *BadgerDatabase) periodicGC(ctx context.Context) {
// We start the timeout with the default sleep to aggressively check
// for space to reclaim on startup.
gcTimeout := time.NewTimer(defaultGCSleep)
defer func() {
gcTimeout.Stop()
}()
for {
select {
case <-b.closed:
// Exit the periodic gc thread if the database is closed.
return
case <-ctx.Done():
return
case <-gcTimeout.C:
start := time.Now()
err := b.db.RunValueLogGC(defualtGCDiscardRatio)
switch err {
case badger.ErrNoRewrite, badger.ErrRejected:
// No rewrite means we've fully garbage collected.
// Rejected means someone else is running a GC
// or we're closing.
gcTimeout.Reset(defaultGCInterval)
case nil:
// Nil error means that we've successfully garbage
// collected. We should sleep instead of waiting
// the full GC collection interval to see if there
// is anything else to collect.
msg := fmt.Sprintf(
"successful value log garbage collection (%s)%s",
time.Since(start),
b.metaData,
)
color.Cyan(msg)
log.Print(msg)
gcTimeout.Reset(defaultGCSleep)
default:
// Not much we can do on a random error but log it and continue.
msg := fmt.Sprintf("error during a GC cycle: %s%s\n", err.Error(), b.metaData)
color.Cyan(msg)
log.Print(msg)
gcTimeout.Reset(defaultGCInterval)
}
}
}
}
// Encoder returns the BadgerDatabase encoder.
func (b *BadgerDatabase) Encoder() *encoder.Encoder {
return b.encoder
}
// BadgerTransaction is a wrapper around a Badger
// DB transaction that implements the DatabaseTransaction
// interface.
type BadgerTransaction struct {
db *BadgerDatabase
txn *badger.Txn
rwLock sync.RWMutex
holdGlobal bool
identifier string
// We MUST wait to reclaim any memory until after
// the transaction is committed or discarded.
// Source: https://godoc.org/github.com/dgraph-io/badger#Txn.Set
//
// It is also CRITICALLY IMPORTANT that the same
// buffer is not added to the BufferPool multiple
// times. This will almost certainly lead to a panic.
reclaimLock sync.Mutex
buffersToReclaim []*bytes.Buffer
}
// Transaction creates a new exclusive write BadgerTransaction.
func (b *BadgerDatabase) Transaction(
ctx context.Context,
) Transaction {
b.writer.GLock()
return &BadgerTransaction{
db: b,
txn: b.db.NewTransaction(true),
holdGlobal: true,
buffersToReclaim: []*bytes.Buffer{},
}
}
// ReadTransaction creates a new read BadgerTransaction.
func (b *BadgerDatabase) ReadTransaction(
ctx context.Context,
) Transaction {
return &BadgerTransaction{
db: b,
txn: b.db.NewTransaction(false),
buffersToReclaim: []*bytes.Buffer{},
}
}
// WriteTransaction creates a new write BadgerTransaction
// for a particular identifier.
func (b *BadgerDatabase) WriteTransaction(
ctx context.Context,
identifier string,
priority bool,
) Transaction {
b.writer.Lock(identifier, priority)
return &BadgerTransaction{
db: b,
txn: b.db.NewTransaction(true),
identifier: identifier,
buffersToReclaim: []*bytes.Buffer{},
}
}
func (b *BadgerTransaction) releaseLocks() {
if b.holdGlobal {
b.holdGlobal = false
b.db.writer.GUnlock()
}
if len(b.identifier) > 0 {
b.db.writer.Unlock(b.identifier)
b.identifier = ""
}
}
// Commit attempts to commit and discard the transaction.
func (b *BadgerTransaction) Commit(context.Context) error {
err := b.txn.Commit()
// Reclaim all allocated buffers for future work.
b.reclaimLock.Lock()
for _, buf := range b.buffersToReclaim {
b.db.pool.Put(buf)
}
// Ensure we don't attempt to reclaim twice.
b.buffersToReclaim = nil
b.reclaimLock.Unlock()
// It is possible that we may accidentally call commit twice.
// In this case, we only unlock if we hold the lock to avoid a panic.
b.releaseLocks()
if err != nil {
err = fmt.Errorf("unable to commit transaction: %w%s", err, b.db.metaData)
color.Red(err.Error())
return err
}
return nil
}
// Discard discards an open transaction. All transactions
// must be either discarded or committed.
func (b *BadgerTransaction) Discard(context.Context) {
b.txn.Discard()
// Reclaim all allocated buffers for future work.
b.reclaimLock.Lock()
for _, buf := range b.buffersToReclaim {
b.db.pool.Put(buf)
}
// Ensure we don't attempt to reclaim twice.
b.buffersToReclaim = nil
b.reclaimLock.Unlock()
b.releaseLocks()
}
// Set changes the value of the key to the value within a transaction.
func (b *BadgerTransaction) Set(
ctx context.Context,
key []byte,
value []byte,
reclaimValue bool,
) error {
b.rwLock.Lock()
defer b.rwLock.Unlock()
if reclaimValue {
b.buffersToReclaim = append(
b.buffersToReclaim,
bytes.NewBuffer(value),
)
}
return b.txn.Set(key, value)
}
// Get accesses the value of the key within a transaction.
// It is up to the caller to reclaim any memory returned.
func (b *BadgerTransaction) Get(
ctx context.Context,
key []byte,
) (bool, []byte, error) {
b.rwLock.RLock()
defer b.rwLock.RUnlock()
value := b.db.pool.Get()
item, err := b.txn.Get(key)
if err == badger.ErrKeyNotFound {
return false, nil, nil
} else if err != nil {
err = fmt.Errorf("unable to get the item of key %s within a transaction: %w%s", string(key), err, b.db.metaData)
color.Red(err.Error())
return false, nil, err
}
err = item.Value(func(v []byte) error {
_, err := value.Write(v)
return err
})
if err != nil {
err = fmt.Errorf(
" %s: %w%s",
string(key),
err,
b.db.metaData,
)
color.Red(err.Error())
return false, nil, err
}
return true, value.Bytes(), nil
}
// Delete removes the key and its value within the transaction.
func (b *BadgerTransaction) Delete(ctx context.Context, key []byte) error {
b.rwLock.Lock()
defer b.rwLock.Unlock()
return b.txn.Delete(key)
}
// Scan calls a worker for each item in a scan instead
// of reading all items into memory.
func (b *BadgerTransaction) Scan(
ctx context.Context,
prefix []byte,
seekStart []byte,
worker func([]byte, []byte) error,
logEntries bool,
reverse bool, // reverse == true means greatest to least
) (int, error) {
b.rwLock.RLock()
defer b.rwLock.RUnlock()
entries := 0
opts := badger.DefaultIteratorOptions
opts.Reverse = reverse
it := b.txn.NewIterator(opts)
defer it.Close()
for it.Seek(seekStart); it.ValidForPrefix(prefix); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) error {
if err := worker(k, v); err != nil {
return fmt.Errorf("worker failed for key %s: %w", string(k), err)
}
return nil
})
if err != nil {
return -1, fmt.Errorf(
"unable to get the value from the item for key %s: %w",
string(k),
err,
)
}
entries++
if logEntries && entries%logModulo == 0 {
log.Printf("scanned %d entries for %s\n", entries, string(prefix))
}
}
return entries, nil
}
func decompressAndSave(
encoder *encoder.Encoder,
namespace string,
tmpDir string,
k []byte,
v []byte,
) (float64, float64, error) {
// We use a compressor.DecodeRaw here because the v may be
// encoded using dictionary compression.
decompressed, err := encoder.DecodeRaw(namespace, v)
if err != nil {
return -1, -1, fmt.Errorf(
"unable to decompress for namespace %s and input %s: %w",
namespace,
string(v),
err,
)
}
err = os.WriteFile(
path.Join(tmpDir, types.Hash(string(k))),
decompressed,
os.FileMode(utils.DefaultFilePermissions),
)
if err != nil {
return -1, -1, fmt.Errorf(
"unable to write decompress file %s: %w",
path.Join(tmpDir, types.Hash(string(k))),
err,
)
}
return float64(len(decompressed)), float64(len(v)), nil
}
// GetInfo returns customized metaData for db's metaData
func (b *BadgerDatabase) GetMetaData() string {
return b.metaData
}
func decompressAndEncode(
path string,
namespace string,
encoder *encoder.Encoder,
) (float64, float64, float64, error) {
decompressed, err := os.ReadFile(path) // #nosec G304
if err != nil {
return -1, -1, -1, fmt.Errorf(
"unable to read decompress file %s: %w",
path,
err,
)
}
normalCompress, err := encoder.EncodeRaw("", decompressed)
if err != nil {
return -1, -1, -1, fmt.Errorf("unable to compress normal: %w", err)
}
dictCompress, err := encoder.EncodeRaw(namespace, decompressed)
if err != nil {
return -1, -1, -1, fmt.Errorf("unable to compress with dictionary: %w", err)
}
// Ensure dict works
decompressedDict, err := encoder.DecodeRaw(namespace, dictCompress)
if err != nil {
return -1, -1, -1, fmt.Errorf("unable to decompress with dictionary: %w", err)
}
if types.Hash(decompressed) != types.Hash(decompressedDict) {
return -1, -1, -1, storageErrs.ErrDecompressOutputMismatch
}
return float64(len(decompressed)), float64(len(normalCompress)), float64(len(dictCompress)), nil
}
// recompress compares the new compressor versus
// what is already on-chain. It returns the old
// on-disk size vs the new on-disk size with the new
// compressor.
func recompress(
ctx context.Context,
badgerDb Database,
namespace string,
restrictedNamespace string,
newCompressor *encoder.Encoder,
) (float64, float64, error) {
onDiskSize := float64(0)
newSize := float64(0)
txn := badgerDb.ReadTransaction(ctx)
defer txn.Discard(ctx)
_, err := txn.Scan(
ctx,
[]byte(restrictedNamespace),
[]byte(restrictedNamespace),
func(k []byte, v []byte) error {
decompressed, err := badgerDb.Encoder().DecodeRaw(namespace, v)
if err != nil {
return fmt.Errorf(
"unable to decompress for namespace %s and input %s: %w",
namespace,
string(v),
err,
)
}
newCompressed, err := newCompressor.EncodeRaw(namespace, decompressed)
if err != nil {
return fmt.Errorf("unable to compress with dictionary: %w", err)
}
onDiskSize += float64(len(v))
newSize += float64(len(newCompressed))
return nil
},
true,
false,
)
if err != nil {
return -1, -1, fmt.Errorf("unable to recompress: %w", err)
}
// Negative savings here means that the new dictionary
// is worse.
savings := (onDiskSize - newSize) / onDiskSize
log.Printf(
"[OUT OF SAMPLE] Savings: %f%%)",
savings*utils.OneHundred,
)
return onDiskSize, newSize, nil
}
// BadgerTrain creates a zstd dictionary for a given BadgerDatabase DB namespace.
// Optionally, you can specify the maximum number of entries to load into
// storage (if -1 is provided, then all possible are loaded).
func BadgerTrain(
ctx context.Context,
namespace string,
db string,
output string,
maxEntries int,
compressorEntries []*encoder.CompressorEntry,
) (float64, float64, error) {
badgerDb, err := NewBadgerDatabase(
ctx,
path.Clean(db),
WithCompressorEntries(compressorEntries),
)
if err != nil {
return -1, -1, fmt.Errorf("unable to load database: %w", err)
}
defer badgerDb.Close(ctx)
// Create directory to store uncompressed files for training
tmpDir, err := utils.CreateTempDir()
if err != nil {
return -1, -1, fmt.Errorf("unable to create temporary directory: %w", err)
}
defer utils.RemoveTempDir(tmpDir)
// We must use a restricted namespace or we will inadvertently
// fetch all namespaces that contain the namespace we care about.
restrictedNamespace := fmt.Sprintf("%s/", namespace)
totalUncompressedSize := float64(0)
totalDiskSize := float64(0)
entriesSeen := 0
txn := badgerDb.ReadTransaction(ctx)
defer txn.Discard(ctx)
_, err = txn.Scan(
ctx,
[]byte(restrictedNamespace),
[]byte(restrictedNamespace),
func(k []byte, v []byte) error {
decompressedSize, diskSize, err := decompressAndSave(
badgerDb.Encoder(),
namespace,
tmpDir,
k,
v,
)
if err != nil {
return fmt.Errorf("unable to decompress and save: %w", err)
}
totalUncompressedSize += decompressedSize
totalDiskSize += diskSize
entriesSeen++
if entriesSeen > maxEntries-1 && maxEntries != -1 {
return storageErrs.ErrMaxEntries
}
return nil
},
true,
false,
)
if err != nil && !errors.Is(err, storageErrs.ErrMaxEntries) {
return -1, -1, fmt.Errorf("%w for %s: %v", storageErrs.ErrScanFailed, namespace, err)
}
if entriesSeen == 0 {
return -1, -1, fmt.Errorf("%w %s", storageErrs.ErrNoEntriesFoundInNamespace, namespace)
}
log.Printf(
"found %d entries for %s (average uncompressed size: %fB)\n",
entriesSeen,
namespace,
totalUncompressedSize/float64(entriesSeen),
)
log.Printf(
"found %d entries for %s (average disk size: %fB)\n",
entriesSeen,
namespace,
totalDiskSize/float64(entriesSeen),
)
// Invoke ZSTD
dictPath := path.Clean(output)
log.Printf("creating dictionary %s\n", dictPath)
cmd := exec.Command(
"zstd",
"--train",
"-r",
tmpDir,
"-o",
dictPath,
) // #nosec G204
if err := cmd.Start(); err != nil {
return -1, -1, fmt.Errorf("unable to start zstd: %w", err)
}
if err := cmd.Wait(); err != nil {
return -1, -1, fmt.Errorf("unable to train zstd: %w", err)
}
encoder, err := encoder.NewEncoder([]*encoder.CompressorEntry{
{
Namespace: namespace,
DictionaryPath: dictPath,
},
}, encoder.NewBufferPool(), true)
if err != nil {
return -1, -1, fmt.Errorf("unable to load compressor: %w", err)
}
sizeUncompressed := float64(0)
sizeNormal := float64(0)
sizeDictionary := float64(0)
err = filepath.Walk(tmpDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
decompressed, normalCompress, dictCompress, err := decompressAndEncode(
path,
namespace,
encoder,
)
if err != nil {
return fmt.Errorf("unable to decompress and encode: %w", err)
}
sizeUncompressed += decompressed
sizeNormal += normalCompress
sizeDictionary += dictCompress
return nil
})
if err != nil {
return -1, -1, fmt.Errorf("unable to walk files: %w", err)
}
log.Printf(
"[IN SAMPLE] Total Size Uncompressed: %fMB",
utils.BtoMb(sizeUncompressed),
)
normalSize := sizeNormal / sizeUncompressed
log.Printf(
"[IN SAMPLE] Total Size Compressed: %fMB (%% of original size %f%%)",
utils.BtoMb(sizeNormal),
normalSize*utils.OneHundred,
)
if len(compressorEntries) > 0 {
oldDictionarySize := totalDiskSize / sizeUncompressed
log.Printf(
"[IN SAMPLE] Total Size Compressed (with old dictionary): %fMB (%% of original size %f%%)",
utils.BtoMb(totalDiskSize),
oldDictionarySize*utils.OneHundred,
)
}
dictionarySize := sizeDictionary / sizeUncompressed
log.Printf(
"[IN SAMPLE] Total Size Compressed (with new dictionary): %fMB (%% of original size %f%%)",
utils.BtoMb(sizeDictionary),
dictionarySize*utils.OneHundred,
)
// Run through all values in scan and compare size
return recompress(
ctx,
badgerDb,
namespace,
restrictedNamespace,
encoder,
)
}