From 599c6f44d4b471716accb204012a34c19679464c Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Thu, 4 Jul 2024 22:32:55 +0800 Subject: [PATCH 01/10] fix: prune ancient restart from last frozen position --- core/rawdb/prunedfreezer.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index 51c7729758..bce99ca0e1 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -74,6 +74,10 @@ func (f *prunedfreezer) repair(datadir string) error { if err != nil { return err } + // blob sidecar can not be same as ancient-tables, it may smaller than others + if name == ChainFreezerBlobSidecarTable { + continue + } items := table.items.Load() if min > items { min = items From 8ba3eae5cc32ce1fbaf7de6e703d31b7bb3eca2e Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jul 2024 11:06:51 +0800 Subject: [PATCH 02/10] fix: prunedfreezer compatibility with EIP-4844 --- core/rawdb/freezer_table.go | 5 +++++ core/rawdb/prunedfreezer.go | 41 +++++++++++++++++++++++++++++-------- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 2158bc4b0e..74152ee7be 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -127,6 +127,11 @@ func newFreezerTable(path, name string, disableSnappy, readonly bool) (*freezerT return newTable(path, name, metrics.NilMeter{}, metrics.NilMeter{}, metrics.NilGauge{}, freezerTableSize, disableSnappy, readonly) } +// newAdditionTable opens the given path as a addition table. +func newAdditionTable(path, name string, disableSnappy, readonly bool) (*freezerTable, error) { + return openAdditionTable(path, name, metrics.NilMeter{}, metrics.NilMeter{}, metrics.NilGauge{}, freezerTableSize, disableSnappy, readonly) +} + // newTable opens a freezer table, creating the data and index files if they are // non-existent. Both files are truncated to the shortest common length to ensure // they don't go out of sync. diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index bce99ca0e1..f6a7b5f357 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -8,6 +8,8 @@ import ( "sync/atomic" "time" + "golang.org/x/exp/slices" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/log" @@ -68,24 +70,47 @@ func newPrunedFreezer(datadir string, db ethdb.KeyValueStore, offset uint64) (*p func (f *prunedfreezer) repair(datadir string) error { offset := atomic.LoadUint64(&f.frozen) // compatible freezer - min := uint64(math.MaxUint64) + var ( + head = uint64(math.MaxUint64) + tail = uint64(0) + ) for name, disableSnappy := range chainFreezerNoSnappy { - table, err := newFreezerTable(datadir, name, disableSnappy, false) + var ( + table *freezerTable + err error + ) + if slices.Contains(additionTables, name) { + table, err = newAdditionTable(datadir, name, disableSnappy, false) + } else { + table, err = newFreezerTable(datadir, name, disableSnappy, false) + } if err != nil { return err } - // blob sidecar can not be same as ancient-tables, it may smaller than others - if name == ChainFreezerBlobSidecarTable { + // addition tables only align head + if slices.Contains(additionTables, name) { + if EmptyTable(table) { + continue + } + items := table.items.Load() + if head > items { + head = items + } continue } items := table.items.Load() - if min > items { - min = items + if head > items { + head = items + } + hidden := table.itemHidden.Load() + if hidden > tail { + tail = hidden } table.Close() } - log.Info("Read ancientdb item counts", "items", min) - offset += min + items := head - tail + log.Info("Read ancientdb item counts", "items", items) + offset += items if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { offset = frozen From 2fa163d28e7e877aa8d9e6d5a24ed15591e2027a Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jul 2024 11:08:41 +0800 Subject: [PATCH 03/10] fix: prunedfreezer compatibility with EIP-4844 --- core/rawdb/prunedfreezer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index f6a7b5f357..38cf31b50d 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -108,7 +108,7 @@ func (f *prunedfreezer) repair(datadir string) error { } table.Close() } - items := head - tail + items := head - tail + 1 log.Info("Read ancientdb item counts", "items", items) offset += items From 26c3a6869b429dbea76c0db95442f4b9e311b40e Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jul 2024 11:11:29 +0800 Subject: [PATCH 04/10] fix: prunedfreezer compatibility with EIP-4844 --- core/rawdb/prunedfreezer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index 38cf31b50d..f6a7b5f357 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -108,7 +108,7 @@ func (f *prunedfreezer) repair(datadir string) error { } table.Close() } - items := head - tail + 1 + items := head - tail log.Info("Read ancientdb item counts", "items", items) offset += items From 749b3afac64a2a1a45b3791be0c31092dca57148 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Mon, 8 Jul 2024 11:19:00 +0800 Subject: [PATCH 05/10] fix: add more log --- core/rawdb/prunedfreezer.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index f6a7b5f357..a9c9b71200 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -109,7 +109,7 @@ func (f *prunedfreezer) repair(datadir string) error { table.Close() } items := head - tail - log.Info("Read ancientdb item counts", "items", items) + log.Info("Read ancientdb item counts", "head", head, "tail", tail, "items", items) offset += items if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { From ed52afd714931ae4abc61d82094fa16d7009ed50 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Tue, 9 Jul 2024 11:58:49 +0800 Subject: [PATCH 06/10] fix: fix prunedfreezer repair skip empty table --- core/rawdb/prunedfreezer.go | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index a9c9b71200..fcef342197 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -70,10 +70,7 @@ func newPrunedFreezer(datadir string, db ethdb.KeyValueStore, offset uint64) (*p func (f *prunedfreezer) repair(datadir string) error { offset := atomic.LoadUint64(&f.frozen) // compatible freezer - var ( - head = uint64(math.MaxUint64) - tail = uint64(0) - ) + min := uint64(math.MaxUint64) for name, disableSnappy := range chainFreezerNoSnappy { var ( table *freezerTable @@ -92,25 +89,15 @@ func (f *prunedfreezer) repair(datadir string) error { if EmptyTable(table) { continue } - items := table.items.Load() - if head > items { - head = items - } - continue } items := table.items.Load() - if head > items { - head = items - } - hidden := table.itemHidden.Load() - if hidden > tail { - tail = hidden + if min > items { + min = items } table.Close() } - items := head - tail - log.Info("Read ancientdb item counts", "head", head, "tail", tail, "items", items) - offset += items + log.Info("Read ancientdb item counts", "items", min) + offset += min if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { offset = frozen From c2758d3a3a5db4616884badc6566f02702da75cd Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Tue, 9 Jul 2024 18:20:13 +0800 Subject: [PATCH 07/10] fix: temp version --- core/rawdb/prunedfreezer.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index fcef342197..6a1c7a8877 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -68,9 +68,8 @@ func newPrunedFreezer(datadir string, db ethdb.KeyValueStore, offset uint64) (*p // repair init frozen , compatible disk-ancientdb and pruner-block-tool. func (f *prunedfreezer) repair(datadir string) error { - offset := atomic.LoadUint64(&f.frozen) // compatible freezer - min := uint64(math.MaxUint64) + minItems := uint64(math.MaxUint64) for name, disableSnappy := range chainFreezerNoSnappy { var ( table *freezerTable @@ -91,14 +90,13 @@ func (f *prunedfreezer) repair(datadir string) error { } } items := table.items.Load() - if min > items { - min = items + if minItems > items { + minItems = items } table.Close() } - log.Info("Read ancientdb item counts", "items", min) - offset += min - + log.Info("Read ancientdb item counts", "items", minItems) + offset := minItems if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { offset = frozen } From b374f900795b33352726b557e817e1c7ac30f8e3 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Tue, 16 Jul 2024 15:56:15 +0800 Subject: [PATCH 08/10] fix: repair offset --- core/rawdb/prunedfreezer.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index 6a1c7a8877..1d2206463c 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -68,6 +68,7 @@ func newPrunedFreezer(datadir string, db ethdb.KeyValueStore, offset uint64) (*p // repair init frozen , compatible disk-ancientdb and pruner-block-tool. func (f *prunedfreezer) repair(datadir string) error { + offset := atomic.LoadUint64(&f.frozen) // compatible freezer minItems := uint64(math.MaxUint64) for name, disableSnappy := range chainFreezerNoSnappy { @@ -95,8 +96,15 @@ func (f *prunedfreezer) repair(datadir string) error { } table.Close() } - log.Info("Read ancientdb item counts", "items", minItems) - offset := minItems + + // If minItems is non-zero, it indicates that the chain freezer was previously enabled, and we should use minItems as the current frozen value. + // If minItems is zero, it indicates that the pruneAncient was previously enabled, and we should continue using frozen + // (retrieved from CurrentAncientFreezer) as the current frozen value. + if minItems != 0 { + offset = minItems + } + log.Info("Read ancientdb item counts", "items", minItems, "offset", offset) + if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { offset = frozen } From 6436146b15d4f43ec3de471cd2355fdc75135184 Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Tue, 16 Jul 2024 17:38:21 +0800 Subject: [PATCH 09/10] fix: change some code styles --- core/rawdb/prunedfreezer.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index 1d2206463c..46e85ec44a 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -68,7 +68,6 @@ func newPrunedFreezer(datadir string, db ethdb.KeyValueStore, offset uint64) (*p // repair init frozen , compatible disk-ancientdb and pruner-block-tool. func (f *prunedfreezer) repair(datadir string) error { - offset := atomic.LoadUint64(&f.frozen) // compatible freezer minItems := uint64(math.MaxUint64) for name, disableSnappy := range chainFreezerNoSnappy { @@ -100,16 +99,19 @@ func (f *prunedfreezer) repair(datadir string) error { // If minItems is non-zero, it indicates that the chain freezer was previously enabled, and we should use minItems as the current frozen value. // If minItems is zero, it indicates that the pruneAncient was previously enabled, and we should continue using frozen // (retrieved from CurrentAncientFreezer) as the current frozen value. + var offset uint64 if minItems != 0 { offset = minItems + } else { + offset = atomic.LoadUint64(&f.frozen) } log.Info("Read ancientdb item counts", "items", minItems, "offset", offset) - if frozen := ReadFrozenOfAncientFreezer(f.db); frozen > offset { - offset = frozen - } + // FrozenOfAncientFreezer is the progress of the last prune-freezer freeze. + frozenInDB := ReadFrozenOfAncientFreezer(f.db) + maxOffset := max(offset, frozenInDB) - atomic.StoreUint64(&f.frozen, offset) + atomic.StoreUint64(&f.frozen, maxOffset) if err := f.Sync(); err != nil { return nil } From 4436e15810bb952cfcbbe24584d1ceafaf79a4aa Mon Sep 17 00:00:00 2001 From: Chris Li <271678682li@gmail.com> Date: Tue, 16 Jul 2024 18:47:45 +0800 Subject: [PATCH 10/10] fix: change some code styles --- core/rawdb/prunedfreezer.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/rawdb/prunedfreezer.go b/core/rawdb/prunedfreezer.go index 46e85ec44a..c42b3c090c 100644 --- a/core/rawdb/prunedfreezer.go +++ b/core/rawdb/prunedfreezer.go @@ -99,10 +99,9 @@ func (f *prunedfreezer) repair(datadir string) error { // If minItems is non-zero, it indicates that the chain freezer was previously enabled, and we should use minItems as the current frozen value. // If minItems is zero, it indicates that the pruneAncient was previously enabled, and we should continue using frozen // (retrieved from CurrentAncientFreezer) as the current frozen value. - var offset uint64 - if minItems != 0 { - offset = minItems - } else { + offset := minItems + if offset == 0 { + // no item in ancientDB, init `offset` to the `f.frozen` offset = atomic.LoadUint64(&f.frozen) } log.Info("Read ancientdb item counts", "items", minItems, "offset", offset)