Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

Commit 37c20d5

Browse files
authored
MemIAVL should only only keep 1 snapshot (#110)
* MemIAVL should only keep 1 current snapshot * Fix comment * Fix locking issue * Use numcpu as default
1 parent 7eaf603 commit 37c20d5

4 files changed

Lines changed: 24 additions & 18 deletions

File tree

config/config.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package config
22

33
const (
4-
DefaultSnapshotInterval = 10000
5-
DefaultSnapshotKeepRecent = 1
6-
DefaultSnapshotWriterLimit = 1
7-
DefaultAsyncCommitBuffer = 100
8-
DefaultCacheSize = 100000
9-
DefaultSSKeepRecent = 100000
10-
DefaultSSPruneInterval = 600
11-
DefaultSSImportWorkers = 1
12-
DefaultSSAsyncBuffer = 100
13-
DefaultSSHashRange = 1000000
4+
DefaultSnapshotInterval = 10000
5+
DefaultSnapshotKeepRecent = 0 // set to 0 to only keep one current snapshot
6+
DefaultAsyncCommitBuffer = 100
7+
DefaultCacheSize = 100000
8+
DefaultSSKeepRecent = 100000
9+
DefaultSSPruneInterval = 600
10+
DefaultSSImportWorkers = 1
11+
DefaultSSAsyncBuffer = 100
12+
DefaultSSHashRange = 1000000
1413
)
1514

1615
type StateCommitConfig struct {

sc/memiavl/db.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"runtime"
910
"sort"
1011
"strconv"
1112
"strings"
@@ -17,7 +18,6 @@ import (
1718
errorutils "github.com/sei-protocol/sei-db/common/errors"
1819
"github.com/sei-protocol/sei-db/common/logger"
1920
"github.com/sei-protocol/sei-db/common/utils"
20-
"github.com/sei-protocol/sei-db/config"
2121
"github.com/sei-protocol/sei-db/proto"
2222
"github.com/sei-protocol/sei-db/stream/changelog"
2323
"github.com/sei-protocol/sei-db/stream/types"
@@ -209,8 +209,10 @@ func OpenDB(logger logger.Logger, targetVersion int64, opts Options) (*DB, error
209209
return nil, errorutils.Join(err, db.Close())
210210
}
211211
}
212-
if db.streamHandler == nil {
213-
fmt.Println("[Debug] DB steam handler is nil??")
212+
213+
// We need to prune snapshots during start up to avoid snapshot leaks
214+
if !db.readOnly {
215+
db.pruneSnapshots()
214216
}
215217
return db, nil
216218
}
@@ -617,7 +619,8 @@ func (db *DB) Close() error {
617619
db.mtx.Lock()
618620
defer db.mtx.Unlock()
619621
errs := []error{}
620-
622+
db.pruneSnapshotLock.Lock()
623+
defer db.pruneSnapshotLock.Unlock()
621624
// Close stream handler
622625
if db.streamHandler != nil {
623626
err := db.streamHandler.Close()
@@ -801,7 +804,8 @@ func initEmptyDB(dir string, initialVersion uint32) error {
801804
tmp := NewEmptyMultiTree(initialVersion, 0)
802805
snapshotDir := snapshotName(0)
803806
// create tmp worker pool
804-
pool := pond.New(config.DefaultSnapshotWriterLimit, config.DefaultSnapshotWriterLimit*10)
807+
concurrency := runtime.NumCPU()
808+
pool := pond.New(concurrency, concurrency*10)
805809
defer pool.Stop()
806810

807811
if err := tmp.WriteSnapshot(context.Background(), filepath.Join(dir, snapshotDir), pool); err != nil {

sc/memiavl/export.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import (
55
"errors"
66
"fmt"
77
"path/filepath"
8+
"runtime"
89

910
errorutils "github.com/sei-protocol/sei-db/common/errors"
1011
"github.com/sei-protocol/sei-db/common/logger"
11-
"github.com/sei-protocol/sei-db/config"
1212
"github.com/sei-protocol/sei-db/sc/types"
1313
)
1414

@@ -36,7 +36,7 @@ func NewMultiTreeExporter(dir string, version uint32, onlyAllowExportOnSnapshotV
3636
Dir: dir,
3737
ZeroCopy: true,
3838
ReadOnly: true,
39-
SnapshotWriterLimit: config.DefaultSnapshotWriterLimit,
39+
SnapshotWriterLimit: runtime.NumCPU(),
4040
})
4141
if err != nil {
4242
return nil, fmt.Errorf("invalid height: %d, %w", version, err)

sc/memiavl/opts.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package memiavl
22

33
import (
44
"errors"
5+
"runtime"
56

67
"github.com/sei-protocol/sei-db/config"
78
)
@@ -55,10 +56,12 @@ func (opts *Options) FillDefaults() {
5556
}
5657

5758
if opts.SnapshotWriterLimit <= 0 {
58-
opts.SnapshotWriterLimit = config.DefaultSnapshotWriterLimit
59+
opts.SnapshotWriterLimit = runtime.NumCPU()
5960
}
6061

6162
if opts.CacheSize < 0 {
6263
opts.CacheSize = config.DefaultCacheSize
6364
}
65+
66+
opts.SnapshotKeepRecent = config.DefaultSnapshotKeepRecent
6467
}

0 commit comments

Comments
 (0)