@@ -31,13 +31,10 @@ import (
3131 "github.com/coinbase/rosetta-sdk-go/types"
3232 sdkUtils "github.com/coinbase/rosetta-sdk-go/utils"
3333 "github.com/dgraph-io/badger/v2"
34+ "github.com/dgraph-io/badger/v2/options"
3435)
3536
3637const (
37- // DefaultIndexCacheSize is the default size of the indexer cache. The larger
38- // the index cache size, the better the performance.
39- DefaultIndexCacheSize = 5 << 30 // 5 GB
40-
4138 // indexPlaceholder is provided to the syncer
4239 // to indicate we should both start from the
4340 // last synced block and that we should sync
@@ -56,10 +53,6 @@ const (
5653 // this is the estimated memory overhead for each
5754 // block fetched by the indexer.
5855 sizeMultiplier = 15
59-
60- // Other BadgerDB options overrides
61- defaultBlockSize = 1 << 20 // use large blocks so less table indexes (1 MB)
62- defaultValueThreshold = 0 // put almost everything in value logs (only use table for key)
6356)
6457
6558var (
@@ -114,23 +107,51 @@ func (i *Indexer) CloseDatabase(ctx context.Context) {
114107}
115108
116109// defaultBadgerOptions returns a set of badger.Options optimized
117- // for running a Rosetta implementation. After extensive research
118- // and profiling, we determined that the bottleneck to high RPC
119- // load was fetching table indexes from disk on an index cache miss.
120- // Thus, we increased the default block size to be much larger than
121- // the Badger default so we have a lot less indexes to store. We also
122- // ensure all values are stored in value log files (as to minimize
123- // table bloat at the cost of some performance).
110+ // for running a Rosetta implementation.
124111func defaultBadgerOptions (
125- path string ,
126- indexCacheSize int64 ,
112+ dir string ,
127113) badger.Options {
128- defaultOps := storage .DefaultBadgerOptions (path )
129- defaultOps .BlockSize = defaultBlockSize
130- defaultOps .ValueThreshold = defaultValueThreshold
131- defaultOps .IndexCacheSize = indexCacheSize
114+ opts := badger .DefaultOptions (dir )
115+
116+ // By default, we do not compress the table at all. Doing so can
117+ // significantly increase memory usage.
118+ opts .Compression = options .None
119+
120+ // Load tables into memory and memory map value logs.
121+ opts .TableLoadingMode = options .MemoryMap
122+ opts .ValueLogLoadingMode = options .MemoryMap
123+
124+ // Use an extended table size for larger commits.
125+ opts .MaxTableSize = storage .DefaultMaxTableSize
126+
127+ // Smaller value log sizes means smaller contiguous memory allocations
128+ // and less RAM usage on cleanup.
129+ opts .ValueLogFileSize = storage .DefaultLogValueSize
130+
131+ // To allow writes at a faster speed, we create a new memtable as soon as
132+ // an existing memtable is filled up. This option determines how many
133+ // memtables should be kept in memory.
134+ opts .NumMemtables = 1
135+
136+ // Don't keep multiple memtables in memory. With larger
137+ // memtable size, this explodes memory usage.
138+ opts .NumLevelZeroTables = 1
139+ opts .NumLevelZeroTablesStall = 2
140+
141+ // This option will have a significant effect the memory. If the level is kept
142+ // in-memory, read are faster but the tables will be kept in memory. By default,
143+ // this is set to false.
144+ opts .KeepL0InMemory = false
145+
146+ // We don't compact L0 on close as this can greatly delay shutdown time.
147+ opts .CompactL0OnClose = false
148+
149+ // LoadBloomsOnOpen=false will improve the db startup speed. This is also
150+ // a waste to enable with a limited index cache size (as many of the loaded bloom
151+ // filters will be immediately discarded from the cache).
152+ opts .LoadBloomsOnOpen = false
132153
133- return defaultOps
154+ return opts
134155}
135156
136157// Initialize returns a new Indexer.
@@ -139,15 +160,13 @@ func Initialize(
139160 cancel context.CancelFunc ,
140161 config * configuration.Configuration ,
141162 client Client ,
142- indexCacheSize int64 ,
143163) (* Indexer , error ) {
144164 localStore , err := storage .NewBadgerStorage (
145165 ctx ,
146166 config .IndexerPath ,
147167 storage .WithCompressorEntries (config .Compressors ),
148168 storage .WithCustomSettings (defaultBadgerOptions (
149169 config .IndexerPath ,
150- indexCacheSize ,
151170 )),
152171 )
153172 if err != nil {
0 commit comments