From faed9c6d745ebbad8e7cdae835161ecdc650600a Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 11 Jan 2022 10:51:22 +0800 Subject: [PATCH] core/rawdb: address comments from martin and sina --- core/rawdb/accessors_chain.go | 2 +- core/rawdb/freezer.go | 16 +++------------- core/rawdb/freezer_meta.go | 25 +++++++------------------ core/rawdb/freezer_table.go | 8 ++++---- 4 files changed, 15 insertions(+), 36 deletions(-) diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 7e4a06df924b9..f9c224dfa8f86 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -83,7 +83,7 @@ type NumberHash struct { Hash common.Hash } -// ReadAllHashesInRange retrieves all the hashes assigned to blocks at a certain +// ReadAllHashesInRange retrieves all the hashes assigned to blocks at certain // heights, both canonical and reorged forks included. // This method considers both limits to be _inclusive_. func ReadAllHashesInRange(db ethdb.Iteratee, first, last uint64) []*NumberHash { diff --git a/core/rawdb/freezer.go b/core/rawdb/freezer.go index 503ccbbb98512..34c832b4009ee 100644 --- a/core/rawdb/freezer.go +++ b/core/rawdb/freezer.go @@ -59,7 +59,7 @@ const ( freezerRecheckInterval = time.Minute // freezerBatchLimit is the maximum number of blocks to freeze in one batch - // before doing a fsync and deleting it from the key-value store. + // before doing an fsync and deleting it from the key-value store. freezerBatchLimit = 30000 // freezerTableSize defines the maximum size of freezer data files. @@ -291,17 +291,12 @@ func (f *freezer) TruncateHead(items uint64) error { if atomic.LoadUint64(&f.frozen) <= items { return nil } - var frozen uint64 for _, table := range f.tables { if err := table.truncateHead(items); err != nil { return err } - // Tables should be aligned, only check the first table. - if frozen == 0 { - frozen = atomic.LoadUint64(&table.items) - } } - atomic.StoreUint64(&f.frozen, frozen) + atomic.StoreUint64(&f.frozen, items) return nil } @@ -316,17 +311,12 @@ func (f *freezer) TruncateTail(tail uint64) error { if atomic.LoadUint64(&f.tail) >= tail { return nil } - var truncated uint64 for _, table := range f.tables { if err := table.truncateTail(tail); err != nil { return err } - if truncated == 0 { - // Tables should be aligned, only check the first table. - truncated = table.tail() - } } - atomic.StoreUint64(&f.tail, truncated) + atomic.StoreUint64(&f.tail, tail) return nil } diff --git a/core/rawdb/freezer_meta.go b/core/rawdb/freezer_meta.go index 4df449d53587d..a77e563f9a39a 100644 --- a/core/rawdb/freezer_meta.go +++ b/core/rawdb/freezer_meta.go @@ -151,27 +151,16 @@ func loadMetadata(index *os.File) (*freezerTableMeta, error) { // upgradeV0TableIndex extracts the indexes from version-0 index file and // encodes/stores them into the latest version index file. func upgradeV0TableIndex(index *os.File) error { - // Create a temporary offset buffer to read indexEntry info - buffer := make([]byte, indexEntrySize) - - // Read index zero, determine what file is the earliest - // and how many entries are deleted from the freezer table. - var first indexEntry - if _, err := index.ReadAt(buffer, 0); err != nil { - return err - } - first.unmarshalBinary(buffer) - - encoded, err := encodeMetadata(newMetadata(first.filenum, uint64(first.offset), 0)) - if err != nil { - return err - } // Close the origin index file. if err := index.Close(); err != nil { return err } return copyFrom(index.Name(), index.Name(), indexEntrySize, func(f *os.File) error { - _, err := f.Write(encoded) + encoded, err := encodeMetadata(newMetadata(0, 0, 0)) + if err != nil { + return err + } + _, err = f.Write(encoded) return err }) } @@ -201,8 +190,8 @@ func upgradeTableIndex(index *os.File, version uint16) (*os.File, *freezerTableM } // repairTableIndex repairs the given index file of freezer table and returns -// the stored metadata inside. If the index file is be rewritten, the function -// should be responsible for closing the origin one and return the new handler. +// the stored metadata inside. If the index file is to be rewritten, the function +// should be responsible for closing the origin one and returning the new handler. // If the table is empty, commit the empty metadata; // If the table is legacy, upgrade it to the latest version; func repairTableIndex(index *os.File) (*os.File, *freezerTableMeta, error) { diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index f1baca9aac6be..6b0fef645187d 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -94,10 +94,10 @@ type freezerTable struct { items uint64 // Number of items stored in the table (including items removed from tail) itemOffset uint64 // Number of items removed from the table - // itemHidden is the number of items marked as deleted they are not removed - // from the table yet. Since the tail deletion is only supported at file level - // which means the actual deletion will be delayed until the total "marked as - // deleted" data reach the threshold. Before that these items will be hidden + // itemHidden is the number of items marked as deleted which are not removed + // from the table yet. Tail deletion is only supported at file level which + // means the actual deletion will be delayed until the total "marked as + // deleted" data reaches the threshold. Before that these items will be hidden // to prevent being visited again. itemHidden uint64