Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] Close cursor if it's opened by different tx #12546

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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
9 changes: 6 additions & 3 deletions erigon-lib/state/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/erigontech/erigon-lib/commitment"
"math"
"math/rand"
"os"
Expand All @@ -33,6 +32,8 @@ import (
"testing"
"time"

"github.com/erigontech/erigon-lib/commitment"

"github.com/erigontech/erigon-lib/common/background"

"github.com/c2h5oh/datasize"
Expand Down Expand Up @@ -103,14 +104,16 @@ func TestAggregatorV3_Merge(t *testing.T) {
var v [8]byte
binary.BigEndian.PutUint64(v[:], txNum)
if txNum%135 == 0 {
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey2, nil, rwTx)
// pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey2, nil, rwTx)
pv, step, err := domains.DomainGet(kv.CommitmentDomain, commKey2, nil)
require.NoError(t, err)

err = domains.DomainPut(kv.CommitmentDomain, commKey2, nil, v[:], pv, step)
require.NoError(t, err)
otherMaxWrite = txNum
} else {
pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey1, nil, rwTx)
// pv, step, _, err := ac.GetLatest(kv.CommitmentDomain, commKey1, nil, rwTx)
pv, step, err := domains.DomainGet(kv.CommitmentDomain, commKey1, nil)
require.NoError(t, err)

err = domains.DomainPut(kv.CommitmentDomain, commKey1, nil, v[:], pv, step)
Expand Down
19 changes: 18 additions & 1 deletion erigon-lib/state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/erigontech/erigon-lib/metrics"
btree2 "github.com/tidwall/btree"
Expand Down Expand Up @@ -760,7 +761,8 @@ type DomainRoTx struct {
keyBuf [60]byte // 52b key and 8b for inverted step
comBuf []byte

valsC kv.Cursor
valsC kv.Cursor
vcParentPtr atomic.Uintptr

getFromFileCache *DomainGetFromFileCache
}
Expand Down Expand Up @@ -1792,6 +1794,11 @@ func (dt *DomainRoTx) Close() {
if dt.files == nil { // invariant: it's safe to call Close multiple times
return
}
if dt.valsC != nil {
dt.valsC.Close()
dt.vcParentPtr.Store(0)
dt.valsC = nil
}
files := dt.files
dt.files = nil
for i := range files {
Expand Down Expand Up @@ -1859,10 +1866,20 @@ func (dt *DomainRoTx) statelessBtree(i int) *BtIndex {
return r
}

var sdTxImmutabilityInvariant = errors.New("tx passed into ShredDomains is immutable")

func (dt *DomainRoTx) valsCursor(tx kv.Tx) (c kv.Cursor, err error) {
eface := *(*[2]uintptr)(unsafe.Pointer(&tx))
if dt.valsC != nil {
if !dt.vcParentPtr.CompareAndSwap(eface[1], eface[1]) { // cant swap when parent ptr is different
panic(fmt.Errorf("%w: cursor parent tx %x; current tx %x", sdTxImmutabilityInvariant, dt.vcParentPtr.Load(), eface[1])) // cursor opened by different tx, invariant broken
}
return dt.valsC, nil
}
// initialise parent pointer tracking
if !dt.vcParentPtr.CompareAndSwap(0, eface[1]) {
panic(fmt.Errorf("%w: cursor parent tx %x; current tx %x", sdTxImmutabilityInvariant, dt.vcParentPtr.Load(), eface[1])) // cursor opened by different tx, invariant broken
}

if dt.d.largeVals {
dt.valsC, err = tx.Cursor(dt.d.valsTable)
Expand Down
25 changes: 14 additions & 11 deletions erigon-lib/state/domain_shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {

ac = agg.BeginFilesRo()
defer ac.Close()
domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
wrwTx := WrapTxWithCtx(rwTx, ac)
domains, err := NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()

Expand Down Expand Up @@ -268,15 +269,15 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {
require.NoError(err)
domains.Close()

domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
require.Equal(int(stepSize), iterCount(domains))
}
{ // delete marker is in RAM
require.NoError(domains.Flush(ctx, rwTx))
domains.Close()
domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
require.Equal(int(stepSize), iterCount(domains))
Expand Down Expand Up @@ -306,7 +307,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {
require.NoError(err)
domains.Close()

domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
require.Equal(int(stepSize*2+2-2), iterCount(domains))
Expand All @@ -327,7 +328,9 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {

_, err := ac.Prune(ctx, rwTx, 0, nil)
require.NoError(err)
domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())

wrwTx = WrapTxWithCtx(rwTx, ac)
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
require.Equal(int(stepSize*2+2-2), iterCount(domains))
Expand All @@ -336,7 +339,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {
{ // delete/update more keys in RAM
require.NoError(domains.Flush(ctx, rwTx))
domains.Close()
domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()

Expand All @@ -356,7 +359,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {
require.NoError(err)
domains.Close()

domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
require.Equal(int(stepSize*2+2-3), iterCount(domains))
Expand All @@ -366,7 +369,7 @@ func TestSharedDomain_IteratePrefix(t *testing.T) {
require.NoError(err)
domains.Close()

domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wrwTx, log.New())
require.NoError(err)
defer domains.Close()
domains.SetTxNum(domains.TxNum() + 1)
Expand All @@ -392,14 +395,15 @@ func TestSharedDomain_StorageIter(t *testing.T) {
ac := agg.BeginFilesRo()
defer ac.Close()

domains, err := NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
wtxRw := WrapTxWithCtx(rwTx, ac)
domains, err := NewSharedDomains(wtxRw, log.New())
require.NoError(t, err)
defer domains.Close()

maxTx := 3*stepSize + 10
hashes := make([][]byte, maxTx)

domains, err = NewSharedDomains(WrapTxWithCtx(rwTx, ac), log.New())
domains, err = NewSharedDomains(wtxRw, log.New())
require.NoError(t, err)
defer domains.Close()

Expand Down Expand Up @@ -513,7 +517,6 @@ func TestSharedDomain_StorageIter(t *testing.T) {
require.Zero(t, missed)
require.Zero(t, notRemoved)
}
fmt.Printf("deleted\n")

err = domains.Flush(ctx, rwTx)
require.NoError(t, err)
Expand Down
47 changes: 47 additions & 0 deletions erigon-lib/state/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,53 @@ func TestDomain_ScanFiles(t *testing.T) {
checkHistory(t, db, d, txs)
}

func TestDomainRoTx_CursorParentCheck(t *testing.T) {
t.Parallel()

logger := log.New()
db, d := testDbAndDomain(t, logger)
ctx, require := context.Background(), require.New(t)
tx, err := db.BeginRw(ctx)
require.NoError(err)
defer tx.Rollback()

dc := d.BeginFilesRo()
defer dc.Close()
writer := dc.NewWriter()
defer writer.close()

val := []byte("value1")
writer.SetTxNum(1)
writer.addValue([]byte("key1"), nil, val)

err = writer.Flush(ctx, tx)
require.NoError(err)
err = tx.Commit()
require.NoError(err)

tx, err = db.BeginRw(ctx)
require.NoError(err)
defer tx.Rollback()

cursor, err := dc.valsCursor(tx)
require.NoError(err)
require.NotNil(cursor)
tx.Rollback()

otherTx, err := db.BeginRw(ctx)
require.NoError(err)
defer otherTx.Rollback()

defer func() {
r := recover()
require.NotNil(r)
re := r.(error)
require.ErrorIs(re, sdTxImmutabilityInvariant)
}()

dc.GetLatest([]byte("key1"), nil, otherTx)
}

func TestDomain_Delete(t *testing.T) {
t.Parallel()

Expand Down
Loading