Skip to content

Commit

Permalink
fix: ignore truncation target range as unclean shutdown happens (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
krish-nr authored Jul 24, 2024
1 parent 887404f commit 0c45fd0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
18 changes: 12 additions & 6 deletions trie/triedb/pathdb/disklayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,20 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
if overflow {
if _, ok := dl.buffer.(*nodebufferlist); ok {
persistentID := rawdb.ReadPersistentStateID(dl.db.diskdb)
if persistentID > limit {
oldest = persistentID - limit + 1
log.Info("Forcing prune ancient under nodebufferlist", "disk_persistent_state_id",
persistentID, "truncate_tail", oldest)
} else {
log.Info("No prune ancient under nodebufferlist, less than db config state history limit")
if limit >= persistentID {
log.Debug("No prune ancient under nodebufferlist, less than db config state history limit", "persistent_id", persistentID, "limit", limit)
return ndl, nil
}
targetOldest := persistentID - limit + 1
realOldest, err := dl.db.freezer.Tail()
if err == nil && targetOldest <= realOldest {
log.Info("No prune ancient under nodebufferlist due to truncate oldest less than real oldest, which maybe happened in abnormal restart",
"tartget_oldest_id", targetOldest, "real_oldest_id", realOldest, "error", err)
return ndl, nil
}
oldest = targetOldest
log.Info("Forcing prune ancient under nodebufferlist", "disk_persistent_state_id",
persistentID, "truncate_tail", oldest)
}

pruned, err := truncateFromTail(ndl.db.diskdb, ndl.db.freezer, oldest-1)
Expand Down
3 changes: 2 additions & 1 deletion trie/triedb/pathdb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,8 @@ func truncateFromTail(db ethdb.Batcher, freezer *rawdb.ResettableFreezer, ntail
}
// Ensure that the truncation target falls within the specified range.
if otail > ntail || ntail > ohead {
return 0, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", otail, ohead, ntail)
log.Warn("truncate from tail out of range", "tail:", otail, "head:", ohead, "target:", ntail)
return 0, nil
}
// Short circuit if nothing to truncate.
if otail == ntail {
Expand Down
4 changes: 2 additions & 2 deletions trie/triedb/pathdb/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ func TestTruncateOutOfRange(t *testing.T) {
{0, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)},
{0, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)},
{1, tail, nil}, // nothing to delete
{1, head + 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, head+1)},
{1, tail - 1, fmt.Errorf("out of range, tail: %d, head: %d, target: %d", tail, head, tail-1)},
{1, head + 1, nil},
{1, tail - 1, nil},
}
for _, c := range cases {
var gotErr error
Expand Down

0 comments on commit 0c45fd0

Please sign in to comment.