Skip to content

Commit 09afc60

Browse files
committed
core/rawdb: update
1 parent 00e43e9 commit 09afc60

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

core/rawdb/accessors_chain.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValu
298298
// It's ok to request block 0, 1 item
299299
count = number + 1
300300
}
301-
limit, _ := db.Ancients()
301+
limit, _ := db.Ancients(ChainFreezer)
302302
// First read live blocks
303303
if i >= limit {
304304
// If we need to read live blocks, we need to figure out the hash first
@@ -319,7 +319,7 @@ func ReadHeaderRange(db ethdb.Reader, number uint64, count uint64) []rlp.RawValu
319319
}
320320
// read remaining from ancients
321321
max := count * 700
322-
data, err := db.AncientRange(freezerHeaderTable, i+1-count, count, max)
322+
data, err := db.AncientRange(ChainFreezer, freezerHeaderTable, i+1-count, count, max)
323323
if err == nil && uint64(len(data)) == count {
324324
// the data is on the order [h, h+1, .., n] -- reordering needed
325325
for i := range data {

core/rawdb/freezer.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,17 @@ func (f *freezer) TruncateHead(items uint64) error {
291291
if atomic.LoadUint64(&f.frozen) <= items {
292292
return nil
293293
}
294+
var frozen uint64
294295
for _, table := range f.tables {
295296
if err := table.truncateHead(items); err != nil {
296297
return err
297298
}
299+
// Tables should be aligned, only check the first table.
300+
if frozen == 0 {
301+
frozen = atomic.LoadUint64(&table.items)
302+
}
298303
}
299-
atomic.StoreUint64(&f.frozen, items)
304+
atomic.StoreUint64(&f.frozen, frozen)
300305
return nil
301306
}
302307

@@ -315,12 +320,17 @@ func (f *freezer) TruncateTail(tail uint64) error {
315320
// there will be some or all tables that do not actually perform the truncate
316321
// action because tail deletion can only do in file level. These deletions
317322
// will be delayed until the whole data file is deletable.
323+
var truncated uint64
318324
for _, table := range f.tables {
319325
if err := table.truncateTail(tail); err != nil {
320326
return err
321327
}
328+
if truncated == 0 {
329+
itemOffset, hidden := atomic.LoadUint64(&table.itemOffset), atomic.LoadUint64(&table.itemHidden)
330+
truncated = itemOffset + hidden
331+
}
322332
}
323-
atomic.StoreUint64(&f.tail, tail)
333+
atomic.StoreUint64(&f.tail, truncated)
324334
return nil
325335
}
326336

@@ -338,7 +348,7 @@ func (f *freezer) Sync() error {
338348
return nil
339349
}
340350

341-
// repair truncates all data tables to the same length.
351+
// repair truncates all data tables to the same length, both tail and head.
342352
func (f *freezer) repair() error {
343353
var (
344354
head = uint64(math.MaxUint64)

core/rawdb/freezer_table.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,20 @@ func (t *freezerTable) truncateHead(items uint64) error {
396396
var (
397397
offset uint64 // the offset which points to the last index
398398
itemOffset = atomic.LoadUint64(&t.itemOffset)
399+
itemHidden = atomic.LoadUint64(&t.itemHidden)
400+
tail = itemOffset + itemHidden
401+
newHead uint64
399402
)
400403
if items < itemOffset {
401-
offset, items = 0, itemOffset
404+
offset = 0
402405
} else {
403406
offset = items - itemOffset
404407
}
408+
if items < tail {
409+
newHead = tail
410+
} else {
411+
newHead = items
412+
}
405413
if err := truncateFreezerFile(t.index, int64(offset+1)*indexEntrySize); err != nil {
406414
return err
407415
}
@@ -437,9 +445,9 @@ func (t *freezerTable) truncateHead(items uint64) error {
437445
// All data files truncated, set internal counters and return
438446
t.headBytes = int64(expected.offset)
439447

440-
atomic.StoreUint64(&t.items, items)
441-
if items < itemOffset+atomic.LoadUint64(&t.itemHidden) {
442-
atomic.StoreUint64(&t.itemHidden, items-itemOffset)
448+
atomic.StoreUint64(&t.items, newHead)
449+
if newHead < itemOffset+atomic.LoadUint64(&t.itemHidden) {
450+
atomic.StoreUint64(&t.itemHidden, newHead-itemOffset)
443451
}
444452
// Retrieve the new size and update the total size counter
445453
newSize, err := t.sizeNolock()

0 commit comments

Comments
 (0)