Skip to content

Commit

Permalink
fix: fix pbss async node buffer force flush buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
VM committed Feb 10, 2025
1 parent afe4ba0 commit fdb13ef
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
12 changes: 12 additions & 0 deletions triedb/pathdb/asyncnodebuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ func (a *asyncnodebuffer) getSize() (uint64, uint64) {
return a.current.size(), a.background.size()
}

func (a *asyncnodebuffer) forceFlush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, clean *fastcache.Cache, id uint64) error {
for {
if atomic.LoadUint64(&a.background.immutable) == 1 {
time.Sleep(time.Duration(DefaultBackgroundFlushInterval) * time.Second)
log.Info("Waiting background memory table flushed into disk for forcing flush node buffer")
continue
}
atomic.StoreUint64(&a.current.immutable, 1)
return a.current.flush(db, freezer, clean, id, true)
}
}

type nodecache struct {
*buffer
immutable uint64 // The flag equal 1, flush nodes to disk background
Expand Down
5 changes: 5 additions & 0 deletions triedb/pathdb/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package pathdb

import (
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -188,3 +189,7 @@ func (b *buffer) getLayers() uint64 {
func (b *buffer) getSize() (uint64, uint64) {
return b.size(), 0
}

func (b *buffer) forceFlush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, clean *fastcache.Cache, id uint64) error {
return errors.New("unsupported method")
}
9 changes: 5 additions & 4 deletions triedb/pathdb/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,17 +520,18 @@ func TestDatabaseRecoverable(t *testing.T) {
}
}

// TODO(joey): fail when using asyncbuffer
//
//nolint:unused
func testDisable(t *testing.T) {
func TestDisable(t *testing.T) {
// Redefine the diff layer depth allowance for faster testing.
maxDiffLayers = 4
defer func() {
maxDiffLayers = 128
}()

tester := newTester(t, 0, false, 32)
bottom := tester.db.tree.bottom()
if err := bottom.buffer.forceFlush(tester.db.diskdb, tester.db.freezer, nil, bottom.id); err != nil {
t.Fatalf("Failed to force flush: %v", err)
}
defer tester.release()

stored := crypto.Keccak256Hash(rawdb.ReadAccountTrieNode(tester.db.diskdb, nil))
Expand Down
3 changes: 3 additions & 0 deletions triedb/pathdb/disklayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type trienodebuffer interface {

// getSize return the trienodebuffer used size.
getSize() (uint64, uint64)

// forceFlush used for asyncnodebuffer unit test
forceFlush(db ethdb.KeyValueStore, freezer ethdb.AncientWriter, clean *fastcache.Cache, id uint64) error
}

func NewTrieNodeBuffer(sync bool, limit int, nodes *nodeSet, states *stateSet, layers uint64) trienodebuffer {
Expand Down

0 comments on commit fdb13ef

Please sign in to comment.