Skip to content

Commit 73de9e4

Browse files
pdrobnjakclaude
andcommitted
refactor(sei-db): return cleanup closure from newTSReadOptions
newTSReadOptions now returns a cleanup function alongside the ReadOptions, making it harder to forget Destroy() — the compiler forces callers to use or explicitly discard the second return value. The iterator stores a cleanup func() instead of *ReadOptions, called on Close(). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8f3bc86 commit 73de9e4

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

sei-db/db_engine/rocksdb/mvcc/db.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ func OpenDB(dataDir string, config config.StateStoreConfig) (*Database, error) {
127127
}
128128

129129
func (db *Database) getSlice(storeKey string, version int64, key []byte) (*grocksdb.Slice, error) {
130-
readOpts := newTSReadOptions(version)
131-
defer readOpts.Destroy()
130+
readOpts, cleanup := newTSReadOptions(version)
131+
defer cleanup()
132132
return db.storage.GetCF(
133133
readOpts,
134134
db.cfHandle,
@@ -324,9 +324,9 @@ func (db *Database) Iterator(storeKey string, version int64, start, end []byte)
324324
prefix := storePrefix(storeKey)
325325
start, end = util.IterateWithPrefix(prefix, start, end)
326326

327-
readOpts := newTSReadOptions(version)
327+
readOpts, cleanup := newTSReadOptions(version)
328328
itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
329-
return NewRocksDBIterator(itr, readOpts, prefix, start, end, version, db.earliestVersion, false), nil
329+
return NewRocksDBIterator(itr, cleanup, prefix, start, end, version, db.earliestVersion, false), nil
330330
}
331331

332332
func (db *Database) ReverseIterator(storeKey string, version int64, start, end []byte) (types.DBIterator, error) {
@@ -341,9 +341,9 @@ func (db *Database) ReverseIterator(storeKey string, version int64, start, end [
341341
prefix := storePrefix(storeKey)
342342
start, end = util.IterateWithPrefix(prefix, start, end)
343343

344-
readOpts := newTSReadOptions(version)
344+
readOpts, cleanup := newTSReadOptions(version)
345345
itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
346-
return NewRocksDBIterator(itr, readOpts, prefix, start, end, version, db.earliestVersion, true), nil
346+
return NewRocksDBIterator(itr, cleanup, prefix, start, end, version, db.earliestVersion, true), nil
347347
}
348348

349349
// Import loads the initial version of the state in parallel with numWorkers goroutines
@@ -416,7 +416,7 @@ func (db *Database) RawIterate(storeKey string, fn func(key []byte, value []byte
416416
readOpts.SetTimestamp(endTs[:])
417417

418418
itr := db.storage.NewIteratorCF(readOpts, db.cfHandle)
419-
rocksItr := NewRocksDBIterator(itr, readOpts, prefix, start, end, latestVersion, 1, false)
419+
rocksItr := NewRocksDBIterator(itr, func() { readOpts.Destroy() }, prefix, start, end, latestVersion, 1, false)
420420
defer func() { _ = rocksItr.Close() }()
421421

422422
for rocksItr.Valid() {
@@ -443,15 +443,17 @@ func (db *Database) GetLatestMigratedModule() (string, error) {
443443
panic("not implemented")
444444
}
445445

446-
// newTSReadOptions returns ReadOptions used in the RocksDB column family read.
447-
func newTSReadOptions(version int64) *grocksdb.ReadOptions {
446+
// newTSReadOptions returns ReadOptions used in the RocksDB column family read
447+
// and a cleanup function that destroys them. The caller must ensure cleanup is
448+
// called when the ReadOptions are no longer needed.
449+
func newTSReadOptions(version int64) (*grocksdb.ReadOptions, func()) {
448450
var ts [TimestampSize]byte
449451
binary.LittleEndian.PutUint64(ts[:], uint64(version))
450452

451453
readOpts := grocksdb.NewDefaultReadOptions()
452454
readOpts.SetTimestamp(ts[:])
453455

454-
return readOpts
456+
return readOpts, func() { readOpts.Destroy() }
455457
}
456458

457459
func storePrefix(storeKey string) []byte {

sei-db/db_engine/rocksdb/mvcc/iterator.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,25 @@ var _ types.DBIterator = (*iterator)(nil)
1414

1515
type iterator struct {
1616
source *grocksdb.Iterator
17-
readOpts *grocksdb.ReadOptions
17+
cleanup func()
1818
prefix, start, end []byte
1919
version int64
2020
reverse bool
2121
invalid bool
2222
}
2323

24-
func NewRocksDBIterator(source *grocksdb.Iterator, readOpts *grocksdb.ReadOptions, prefix, start, end []byte, version int64, earliestVersion int64, reverse bool) *iterator {
24+
func NewRocksDBIterator(source *grocksdb.Iterator, cleanup func(), prefix, start, end []byte, version int64, earliestVersion int64, reverse bool) *iterator {
2525
// Return invalid iterator if requested iterator height is lower than earliest version after pruning
2626
if version < earliestVersion {
2727
return &iterator{
28-
source: source,
29-
readOpts: readOpts,
30-
prefix: prefix,
31-
start: start,
32-
end: end,
33-
version: version,
34-
reverse: reverse,
35-
invalid: true,
28+
source: source,
29+
cleanup: cleanup,
30+
prefix: prefix,
31+
start: start,
32+
end: end,
33+
version: version,
34+
reverse: reverse,
35+
invalid: true,
3636
}
3737
}
3838

@@ -60,14 +60,14 @@ func NewRocksDBIterator(source *grocksdb.Iterator, readOpts *grocksdb.ReadOption
6060
}
6161

6262
return &iterator{
63-
source: source,
64-
readOpts: readOpts,
65-
prefix: prefix,
66-
start: start,
67-
end: end,
68-
version: version,
69-
reverse: reverse,
70-
invalid: !source.Valid(),
63+
source: source,
64+
cleanup: cleanup,
65+
prefix: prefix,
66+
start: start,
67+
end: end,
68+
version: version,
69+
reverse: reverse,
70+
invalid: !source.Valid(),
7171
}
7272
}
7373

@@ -160,9 +160,9 @@ func (itr *iterator) Error() error {
160160
func (itr *iterator) Close() error {
161161
itr.source.Close()
162162
itr.source = nil
163-
if itr.readOpts != nil {
164-
itr.readOpts.Destroy()
165-
itr.readOpts = nil
163+
if itr.cleanup != nil {
164+
itr.cleanup()
165+
itr.cleanup = nil
166166
}
167167
itr.invalid = true
168168
return nil

0 commit comments

Comments
 (0)