Skip to content

Commit

Permalink
core/rawdb: update
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Dec 13, 2021
1 parent 00e43e9 commit 09afc60
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/rawdb/accessors_chain.go
Expand Up @@ -298,7 +298,7 @@ func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValu
// It's ok to request block 0, 1 item
count = number + 1
}
limit, _ := db.Ancients()
limit, _ := db.Ancients(ChainFreezer)
// First read live blocks
if i >= limit {
// If we need to read live blocks, we need to figure out the hash first
Expand All @@ -319,7 +319,7 @@ func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValu
}
// read remaining from ancients
max := count * 700
data, err := db.AncientRange(freezerHeaderTable, i+1-count, count, max)
data, err := db.AncientRange(ChainFreezer, freezerHeaderTable, i+1-count, count, max)
if err == nil && uint64(len(data)) == count {
// the data is on the order [h, h+1, .., n] -- reordering needed
for i := range data {
Expand Down
16 changes: 13 additions & 3 deletions core/rawdb/freezer.go
Expand Up @@ -291,12 +291,17 @@ 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, items)
atomic.StoreUint64(&f.frozen, frozen)
return nil
}

Expand All @@ -315,12 +320,17 @@ func (f *freezer) TruncateTail(tail uint64) error {
// there will be some or all tables that do not actually perform the truncate
// action because tail deletion can only do in file level. These deletions
// will be delayed until the whole data file is deletable.
var truncated uint64
for _, table := range f.tables {
if err := table.truncateTail(tail); err != nil {
return err
}
if truncated == 0 {
itemOffset, hidden := atomic.LoadUint64(&table.itemOffset), atomic.LoadUint64(&table.itemHidden)
truncated = itemOffset + hidden
}
}
atomic.StoreUint64(&f.tail, tail)
atomic.StoreUint64(&f.tail, truncated)
return nil
}

Expand All @@ -338,7 +348,7 @@ func (f *freezer) Sync() error {
return nil
}

// repair truncates all data tables to the same length.
// repair truncates all data tables to the same length, both tail and head.
func (f *freezer) repair() error {
var (
head = uint64(math.MaxUint64)
Expand Down
16 changes: 12 additions & 4 deletions core/rawdb/freezer_table.go
Expand Up @@ -396,12 +396,20 @@ func (t *freezerTable) truncateHead(items uint64) error {
var (
offset uint64 // the offset which points to the last index
itemOffset = atomic.LoadUint64(&t.itemOffset)
itemHidden = atomic.LoadUint64(&t.itemHidden)
tail = itemOffset + itemHidden
newHead uint64
)
if items < itemOffset {
offset, items = 0, itemOffset
offset = 0
} else {
offset = items - itemOffset
}
if items < tail {
newHead = tail
} else {
newHead = items
}
if err := truncateFreezerFile(t.index, int64(offset+1)*indexEntrySize); err != nil {
return err
}
Expand Down Expand Up @@ -437,9 +445,9 @@ func (t *freezerTable) truncateHead(items uint64) error {
// All data files truncated, set internal counters and return
t.headBytes = int64(expected.offset)

atomic.StoreUint64(&t.items, items)
if items < itemOffset+atomic.LoadUint64(&t.itemHidden) {
atomic.StoreUint64(&t.itemHidden, items-itemOffset)
atomic.StoreUint64(&t.items, newHead)
if newHead < itemOffset+atomic.LoadUint64(&t.itemHidden) {
atomic.StoreUint64(&t.itemHidden, newHead-itemOffset)
}
// Retrieve the new size and update the total size counter
newSize, err := t.sizeNolock()
Expand Down

0 comments on commit 09afc60

Please sign in to comment.