Skip to content

Commit

Permalink
svae
Browse files Browse the repository at this point in the history
  • Loading branch information
awskii committed Dec 12, 2024
1 parent b2c5a67 commit b79b76e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
4 changes: 3 additions & 1 deletion erigon-lib/kv/mdbx/kv_mdbx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -1268,6 +1268,8 @@ func (c *MdbxCursor) Close() {
}
}

func (c *MdbxCursor) IsClosed() bool { return c.c == nil }

type MdbxDupSortCursor struct {
*MdbxCursor
}
Expand Down
34 changes: 23 additions & 11 deletions erigon-lib/state/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/erigontech/erigon-lib/kv/mdbx"
"math"
"path/filepath"
"sort"
Expand Down Expand Up @@ -1708,31 +1709,42 @@ 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)
}
}
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 {
Expand Down
4 changes: 4 additions & 0 deletions erigon-lib/state/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit b79b76e

Please sign in to comment.