From b79b76ec075034593e00e431bcb7d87e9b18dbb9 Mon Sep 17 00:00:00 2001 From: awskii Date: Thu, 12 Dec 2024 17:05:44 +0000 Subject: [PATCH] svae --- erigon-lib/kv/mdbx/kv_mdbx.go | 4 +++- erigon-lib/state/domain.go | 34 ++++++++++++++++++++++----------- erigon-lib/state/domain_test.go | 4 ++++ 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/erigon-lib/kv/mdbx/kv_mdbx.go b/erigon-lib/kv/mdbx/kv_mdbx.go index beb06688d96..c70c671f0e9 100644 --- a/erigon-lib/kv/mdbx/kv_mdbx.go +++ b/erigon-lib/kv/mdbx/kv_mdbx.go @@ -1119,7 +1119,7 @@ func (tx *MdbxTx) stdCursor(bucket string) (kv.RwCursor, error) { if tx.toCloseMap == nil { tx.toCloseMap = make(map[uint64]kv.Closer) } - tx.toCloseMap[c.id] = c.c + tx.toCloseMap[c.id] = c return c, nil } @@ -1268,6 +1268,8 @@ func (c *MdbxCursor) Close() { } } +func (c *MdbxCursor) IsClosed() bool { return c.c == nil } + type MdbxDupSortCursor struct { *MdbxCursor } diff --git a/erigon-lib/state/domain.go b/erigon-lib/state/domain.go index ba1ec20e141..8b439e84909 100644 --- a/erigon-lib/state/domain.go +++ b/erigon-lib/state/domain.go @@ -22,6 +22,7 @@ import ( "encoding/binary" "errors" "fmt" + "github.com/erigontech/erigon-lib/kv/mdbx" "math" "path/filepath" "sort" @@ -1708,9 +1709,8 @@ var sdTxImmutabilityInvariant = errors.New("tx passed into ShredDomains is immut func (dt *DomainRoTx) closeValsCursor() { if dt.valsC != nil { dt.valsC.Close() - fmt.Printf("close view %d [%s]\n", dt.valCViewID, dt.d.filenameBase) dt.valCViewID = 0 - //dt.valsC = nil + dt.valsC = nil // dt.vcParentPtr.Store(0) } } @@ -1718,21 +1718,33 @@ func (dt *DomainRoTx) valsCursor(tx kv.Tx) (c kv.Cursor, err error) { // eface := *(*[2]uintptr)(unsafe.Pointer(&tx)) if dt.valsC != nil { // run in assert mode only - //if tx.ViewID() != dt.valCViewID { - // // 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 - // panic(fmt.Errorf("%w: cursor parent tx %x; current tx %x", sdTxImmutabilityInvariant, dt.valCViewID, tx.ViewID())) // cursor opened by different tx, invariant broken - //} - // fmt.Printf("cmp e1=%x e2=%x s=%x d=%s\n", eface[0], eface[1], dt.vcParentPtr.Load(), dt.d.filenameBase) - fmt.Printf("cmp dvi=%d txvi=%d [%s] ptr %x\n", dt.valCViewID, tx.ViewID(), dt.d.filenameBase, dt.valsC) - return dt.valsC, nil + if tx.ViewID() != dt.valCViewID { + // 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 + panic(fmt.Errorf("%w: cursor parent tx %x; current tx %x", sdTxImmutabilityInvariant, dt.valCViewID, tx.ViewID())) // cursor opened by different tx, invariant broken + } + if dt.d.largeValues { + if mc, ok := dt.valsC.(*mdbx.MdbxCursor); ok && !mc.IsClosed() { + //fmt.Printf("reuse %d closed=%t\n", tx.ViewID(), mc.IsClosed()) + return mc, nil + } + } else { + if mc, ok := dt.valsC.(*mdbx.MdbxDupSortCursor); ok && !mc.IsClosed() { + //fmt.Printf("dsreuse %d closed=%t\n", tx.ViewID(), mc.IsClosed()) + return mc, nil + } + } + dt.closeValsCursor() + + //fmt.Printf("cmp dvi=%d txvi=%d [%s] ptr %x\n", dt.valCViewID, tx.ViewID(), dt.d.filenameBase, dt.valsC) + //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 // } // fmt.Printf("set e1=%x e2=%x d=%s\n", eface[0], eface[1], dt.d.filenameBase) - fmt.Printf("set vid=%d [%s]\n", tx.ViewID(), dt.d.filenameBase) + //fmt.Printf("set vid=%d [%s]\n", tx.ViewID(), dt.d.filenameBase) dt.valCViewID = tx.ViewID() if dt.d.largeValues { diff --git a/erigon-lib/state/domain_test.go b/erigon-lib/state/domain_test.go index 186cfb7b242..aa617a07582 100644 --- a/erigon-lib/state/domain_test.go +++ b/erigon-lib/state/domain_test.go @@ -651,11 +651,15 @@ func TestDomainRoTx_CursorParentCheck(t *testing.T) { cursor, err := dc.valsCursor(tx) require.NoError(err) require.NotNil(cursor) + fmt.Printf("roolback\n") tx.Rollback() + fmt.Printf("eroolback\n") otherTx, err := db.BeginRw(ctx) require.NoError(err) defer otherTx.Rollback() + //dc.valsC.Close() + //dc.valsC = nil _, _, _, err = dc.GetLatest([]byte("key1"), otherTx) //defer func() {