Skip to content

Commit

Permalink
core/rawdb: address comments from martin and sina
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Feb 8, 2022
1 parent 597aa61 commit 2a1d49f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 45 deletions.
2 changes: 1 addition & 1 deletion core/rawdb/accessors_chain.go
Expand Up @@ -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 {
Expand Down
16 changes: 3 additions & 13 deletions core/rawdb/freezer.go
Expand Up @@ -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.
Expand Down Expand Up @@ -298,17 +298,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
}

Expand All @@ -323,17 +318,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
}

Expand Down
25 changes: 7 additions & 18 deletions core/rawdb/freezer_meta.go
Expand Up @@ -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
})
}
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions core/rawdb/freezer_table.go
Expand Up @@ -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

Expand Down
18 changes: 9 additions & 9 deletions core/rawdb/freezer_table_test.go
Expand Up @@ -663,7 +663,7 @@ func TestTruncateTail(t *testing.T) {
fname := fmt.Sprintf("truncate-tail-%d", rand.Uint64())

// Fill table
f, err := newTable(os.TempDir(), fname, rm, wm, sg, 40, true)
f, err := newTable(os.TempDir(), fname, rm, wm, sg, 40, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -709,7 +709,7 @@ func TestTruncateTail(t *testing.T) {

// Reopen the table, the deletion information should be persisted as well
f.Close()
f, err = newTable(os.TempDir(), fname, rm, wm, sg, 40, true)
f, err = newTable(os.TempDir(), fname, rm, wm, sg, 40, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -741,7 +741,7 @@ func TestTruncateTail(t *testing.T) {

// Reopen the table, the above testing should still pass
f.Close()
f, err = newTable(os.TempDir(), fname, rm, wm, sg, 40, true)
f, err = newTable(os.TempDir(), fname, rm, wm, sg, 40, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -780,7 +780,7 @@ func TestTruncateHeadBelowTail(t *testing.T) {
fname := fmt.Sprintf("truncate-head-blow-tail-%d", rand.Uint64())

// Fill table
f, err := newTable(os.TempDir(), fname, rm, wm, sg, 40, true)
f, err := newTable(os.TempDir(), fname, rm, wm, sg, 40, true, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -838,8 +838,8 @@ func TestUpgradeLegacyFreezerTable(t *testing.T) {
defer os.Remove(f.Name())

index := &indexEntry{
filenum: 100,
offset: 200,
filenum: 0,
offset: 0,
}
encoded := index.append(nil)
f.Write(encoded)
Expand All @@ -851,10 +851,10 @@ func TestUpgradeLegacyFreezerTable(t *testing.T) {
if newf.Name() != f.Name() {
t.Fatal("Unexpected file name")
}
if meta.tailId != 100 {
t.Fatal("Unexpected tail file")
if meta.tailId != 0 {
t.Fatal("Unexpected tail file", meta.tailId)
}
if meta.deleted != 200 {
if meta.deleted != 0 {
t.Fatal("Unexpected deleted items")
}
if meta.hidden != 0 {
Expand Down

0 comments on commit 2a1d49f

Please sign in to comment.