Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion sei-db/bootstrap/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,9 @@ func TestRecoverReplaysForwardFromTheStoreOwnHeight(t *testing.T) {
require.Equal(t, int64(9), manager.SC().Version())
requireWALTail(t, manager, 9)
require.DirExists(t, scSnapshotDir(cfg.FlatKVConfig.DataDir, 1))
marker, ok := manager.SC().OpenView().Get(evm.EVMStoreKey, evmNonceKey(108))
view := manager.SC().OpenView()
defer view.Close()
marker, ok := view.Get(evm.EVMStoreKey, evmNonceKey(108))
require.True(t, ok, "SC replayed forward from 8, so the working copy it held there is still open")
require.Equal(t, evmNonce(108), marker)
}
Expand Down
109 changes: 109 additions & 0 deletions sei-db/common/utils/must_close.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package utils

import (
"fmt"
"runtime"
"strings"
"sync/atomic"
"testing"
"weak"

"github.com/sei-protocol/seilog"
)

var logger = seilog.NewLogger("db", "common", "utils")

// The deepest call stack recorded for where an object passed to MustClose() was created.
const mustCloseStackDepth = 32

// CloseMarker records whether the object it was issued for by MustClose() has been closed. Copies of a marker
// share its state, so it may be stored by value.
type CloseMarker[T any] struct {
// The state every copy of this marker shares. Nil for a marker not issued by MustClose().
state *closeState[T]
}

// closeState is the state a CloseMarker and its copies share.
type closeState[T any] struct {
// The object this marker was issued for.
owner weak.Pointer[T]

// Set by CloseMarker.Close().
closed atomic.Bool

// What the object is, named in reports.
description string

// Where the object was registered. Recorded only in a test binary.
createdAt []uintptr
}

// MustClose reports obj as leaked if it becomes unreachable before the returned marker is closed. It logs an error,
// or in a test binary panics naming where obj was created. Every path that closes obj must call the marker's
// Close(). An object reachable from its own running goroutines is never reported.
func MustClose[T any](obj *T, description string) CloseMarker[T] {
state := &closeState[T]{
owner: weak.Make(obj),
description: description,
}
if testing.Testing() {
createdAt := make([]uintptr, mustCloseStackDepth)
// Skips runtime.Callers() and MustClose() itself.
state.createdAt = createdAt[:runtime.Callers(2, createdAt)]
}
runtime.AddCleanup(obj, reportIfOpen[T], state)
return CloseMarker[T]{state: state}
}

// Close records that owner has been closed. owner must be the object this marker was issued for. Idempotent.
func (m CloseMarker[T]) Close(owner *T) {
state := m.issuedState()
if weak.Make(owner) != state.owner {
state.report("was closed through a marker issued for another object")
}
state.closed.Store(true)
// owner must stay reachable until it reads as closed, or its cleanup could run in between.
runtime.KeepAlive(owner)
}

// IsClosed reports whether Close() has been called.
func (m CloseMarker[T]) IsClosed() bool {
return m.issuedState().closed.Load()
}

// issuedState returns the marker's shared state, panicking if the marker was not issued by MustClose().
func (m CloseMarker[T]) issuedState() *closeState[T] {
if m.state == nil {
panic("close marker was not issued by MustClose()")
}
return m.state
}

// reportIfOpen reports the object state was issued for as leaked, unless it has been closed.
func reportIfOpen[T any](state *closeState[T]) {
if state.closed.Load() {
return
}
state.report("became unreachable without being closed")
}

// report panics with problem and the object's creation stack in a test binary, and logs problem otherwise.
func (s *closeState[T]) report(problem string) {
if testing.Testing() {
panic(fmt.Sprintf("%s %s; created at:\n%s", s.description, problem, formatStack(s.createdAt)))
}
logger.Error("object "+problem, "object", s.description)
}

// formatStack renders the program counters recorded by runtime.Callers() as one function and file:line per frame.
func formatStack(pcs []uintptr) string {
var b strings.Builder
frames := runtime.CallersFrames(pcs)
for {
frame, more := frames.Next()
fmt.Fprintf(&b, " %s\n %s:%d\n", frame.Function, frame.File, frame.Line)
if !more {
return b.String()
}
}
}
43 changes: 43 additions & 0 deletions sei-db/common/utils/must_close_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package utils

import (
"testing"

"github.com/stretchr/testify/require"
)

// mustCloseTestObject is an object registered with MustClose() by these tests. It holds a pointer so the runtime
// does not batch it into a shared allocation, where its cleanup might never run.
type mustCloseTestObject struct {
// Unused; present only to give the object a pointer.
payload *int
}

// A marker closed with an object other than the one it was issued for is reported.
func TestCloseMarkerRejectsAnotherOwner(t *testing.T) {
owner := &mustCloseTestObject{}
marker := MustClose(owner, "test object")

require.Panics(t, func() { marker.Close(&mustCloseTestObject{}) })
require.False(t, marker.IsClosed())

marker.Close(owner)
require.True(t, marker.IsClosed())
}

// Every copy of a marker reports the close made through any of them.
func TestCloseMarkerCopiesShareState(t *testing.T) {
owner := &mustCloseTestObject{}
marker := MustClose(owner, "test object")
markerCopy := marker

markerCopy.Close(owner)
require.True(t, marker.IsClosed())
}

// A marker that MustClose() did not issue refuses to be used.
func TestCloseMarkerNotIssuedPanics(t *testing.T) {
var marker CloseMarker[mustCloseTestObject]
require.Panics(t, func() { marker.Close(&mustCloseTestObject{}) })
require.Panics(t, func() { marker.IsClosed() })
}
12 changes: 10 additions & 2 deletions sei-db/db_engine/litt/disktable/disk_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type DiskTable struct {
flushCoordinator *flushCoordinator
}

// NewDiskTable creates a new DiskTable.
// NewDiskTable creates a new DiskTable, which takes ownership of keymap. If it fails, keymap is stopped.
func NewDiskTable(
config *litt.Config,
runtimeConfig *litt.RuntimeConfig,
Expand All @@ -117,7 +117,15 @@ func NewDiskTable(
keymapTypeFile *keymap.KeymapTypeFile,
roots []string,
reloadKeymap bool,
metrics *metrics.LittDBMetrics) (litt.ManagedTable, error) {
metrics *metrics.LittDBMetrics) (_ litt.ManagedTable, err error) {

defer func() {
if err != nil {
if stopErr := keymap.Stop(); stopErr != nil {
err = errors.Join(err, fmt.Errorf("failed to stop keymap: %w", stopErr))
}
}
}()

if config.GCPeriod <= 0 {
return nil, errors.New("garbage collection period must be greater than 0")
Expand Down
27 changes: 17 additions & 10 deletions sei-db/db_engine/litt/disktable/forward_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package disktable
import (
"fmt"

"github.com/sei-protocol/sei-chain/sei-db/common/utils"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt/disktable/segment"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt/types"
Expand Down Expand Up @@ -51,8 +52,8 @@ type forwardIterator struct {
// readerSeg is the segment that reader was created for, so we can detect when to recreate it.
readerSeg *segment.Segment

// closed is true once Close has been called.
closed bool
// closed records whether Close has been called.
closed utils.CloseMarker[forwardIterator]

// groupValid is true once a primary key in the current group has been visited, meaning groupAddr and
// groupValue describe that group's primary value.
Expand All @@ -69,11 +70,13 @@ type forwardIterator struct {
// newForwardIterator creates a forward iterator over the given snapshot of sealed segments, owned by a
// live table.
func newForwardIterator(table *DiskTable, segs []*segment.Segment) *forwardIterator {
return &forwardIterator{
it := &forwardIterator{
onClose: closeLiveIterator(table, segs),
segs: segs,
segPos: 0,
}
it.closed = utils.MustClose(it, "littdb forward iterator")
return it
}

// newForwardIteratorAt creates a forward iterator over the given snapshot positioned so that the first
Expand All @@ -87,32 +90,36 @@ func newForwardIteratorAt(
keys []*types.ScopedKey,
keyPos int,
) *forwardIterator {
return &forwardIterator{
it := &forwardIterator{
onClose: closeLiveIterator(table, segs),
segs: segs,
segPos: segPos,
keys: keys,
keyPos: keyPos,
}
it.closed = utils.MustClose(it, "littdb forward iterator")
return it
}

// NewOfflineForwardIterator creates a forward iterator over the given snapshot of segments, gathered
// directly from disk rather than from a live table. release is called once, by Close, in place of the
// live path's segment-reservation release and control-loop notification.
func NewOfflineForwardIterator(segs []*segment.Segment, release func()) litt.Iterator {
return &forwardIterator{
it := &forwardIterator{
onClose: func() error {
release()
return nil
},
segs: segs,
segPos: 0,
}
it.closed = utils.MustClose(it, "littdb forward iterator")
return it
}

// Next advances the iterator to the next key in insertion order.
func (it *forwardIterator) Next() (bool, error) {
if it.closed {
if it.closed.IsClosed() {
return false, fmt.Errorf("iterator is closed")
}

Expand Down Expand Up @@ -159,7 +166,7 @@ func (it *forwardIterator) Next() (bool, error) {

// GetKey returns the current key and whether it is a primary key.
func (it *forwardIterator) GetKey() (key []byte, isPrimary bool, err error) {
if it.closed {
if it.closed.IsClosed() {
return nil, false, fmt.Errorf("iterator is closed")
}
if it.current == nil {
Expand All @@ -170,7 +177,7 @@ func (it *forwardIterator) GetKey() (key []byte, isPrimary bool, err error) {

// GetValue reads and returns the value associated with the current key.
func (it *forwardIterator) GetValue() (value []byte, err error) {
if it.closed {
if it.closed.IsClosed() {
// Close released the snapshot's segment reservations, so reading now would open a new
// reader on segments GC is free to have deleted.
return nil, fmt.Errorf("iterator is closed")
Expand Down Expand Up @@ -256,10 +263,10 @@ func (it *forwardIterator) secondaryWithinGroup(addr types.Address) bool {

// Close releases the resources held by the iterator, via onClose.
func (it *forwardIterator) Close() error {
if it.closed {
if it.closed.IsClosed() {
return nil
}
it.closed = true
it.closed.Close(it)

// Close the buffered reader.
var readerErr error
Expand Down
2 changes: 2 additions & 0 deletions sei-db/db_engine/litt/disktable/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,8 @@ func TestIteratorAccessorsRejectMisuse(t *testing.T) {
_, err = it.GetValue()
require.ErrorContains(t, err, "not positioned")

require.NoError(t, it.Close())

// Closed. Re-position first, so the guard being tested is `closed` and not exhaustion.
it, err = table.Iterator(reverse)
require.NoError(t, err)
Expand Down
5 changes: 5 additions & 0 deletions sei-db/db_engine/litt/disktable/keymap/pebble_db_keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cockroachdb/pebble/v2"
"github.com/cockroachdb/pebble/v2/bloom"
"github.com/sei-protocol/sei-chain/sei-db/common/unit"
"github.com/sei-protocol/sei-chain/sei-db/common/utils"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt/types"
"github.com/sei-protocol/sei-chain/sei-db/db_engine/litt/util"
)
Expand All @@ -25,6 +26,8 @@ type PebbleDBKeymap struct {
doubleWriteProtection bool
keymapPath string
alive atomic.Bool
// closed records whether Stop has been called.
closed utils.CloseMarker[PebbleDBKeymap]
// This is a "test mode only" flag. Should be true in production use cases or anywhere that data consistency
// is critical. Unit tests write lots of little values, and syncing each one is slow, so it may be desirable
// to set this to false in some tests.
Expand Down Expand Up @@ -85,6 +88,7 @@ func newPebbleDBKeymap(
syncWrites: syncWrites,
}
kmap.alive.Store(true)
kmap.closed = utils.MustClose(kmap, "littdb pebble keymap")

return kmap, requiresReload, nil
}
Expand Down Expand Up @@ -200,6 +204,7 @@ func (p *PebbleDBKeymap) Stop() error {
if !alive {
return nil
}
p.closed.Close(p)

err := p.db.Close()
if err != nil {
Expand Down
Loading
Loading