Skip to content

Commit b82552c

Browse files
YifanYifan
Yifan
authored and
Yifan
committed
core/rawdb: Implemented size reporting for live items in freezer_table.go to account for hidden items due to tail deletions (ethereum#27483)
1 parent c5b7cfa commit b82552c

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

core/rawdb/freezer_table.go

+37-8
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,31 @@ func (t *freezerTable) truncateHead(items uint64) error {
467467
return nil
468468
}
469469

470+
// readIndexEntry reads the index entry at the given index.
471+
func (t *freezerTable) readIndexEntry(index uint64) (entry indexEntry, err error) {
472+
buffer := make([]byte, indexEntrySize)
473+
if _, err := t.index.ReadAt(buffer, int64(index*indexEntrySize)); err != nil {
474+
return indexEntry{}, err
475+
}
476+
entry.unmarshalBinary(buffer)
477+
return entry, nil
478+
}
479+
480+
// hiddenBytes calculates the current size of hidden items in bytes
481+
func (t *freezerTable) hiddenBytes() (uint32, error) {
482+
itemHidden := t.itemHidden.Load()
483+
itemOffset := t.itemOffset.Load()
484+
// no hidden items if the two markers are the same
485+
if itemHidden == itemOffset {
486+
return 0, nil
487+
}
488+
itemHiddenIndexEntry, errHidden := t.readIndexEntry(itemHidden - itemOffset)
489+
if errHidden != nil {
490+
return 0, fmt.Errorf("failed to read index entry, itemHidden: %d, err: %v", itemHidden, errHidden)
491+
}
492+
return itemHiddenIndexEntry.offset, nil
493+
}
494+
470495
// truncateTail discards any recent data before the provided threshold number.
471496
func (t *freezerTable) truncateTail(items uint64) error {
472497
t.lock.Lock()
@@ -495,6 +520,12 @@ func (t *freezerTable) truncateTail(items uint64) error {
495520
newTail.unmarshalBinary(buffer)
496521
newTailId = newTail.filenum
497522
}
523+
// Save the old size for metrics tracking. This needs to be done
524+
// before any updates to either itemHidden or itemOffset.
525+
oldSize, err := t.sizeNolock()
526+
if err != nil {
527+
return err
528+
}
498529
// Update the virtual tail marker and hidden these entries in table.
499530
t.itemHidden.Store(items)
500531
if err := writeMetadata(t.meta, newMetadata(items)); err != nil {
@@ -509,18 +540,12 @@ func (t *freezerTable) truncateTail(items uint64) error {
509540
if t.tailId > newTailId {
510541
return fmt.Errorf("invalid index, tail-file %d, item-file %d", t.tailId, newTailId)
511542
}
512-
// Hidden items exceed the current tail file, drop the relevant
513-
// data files. We need to truncate, save the old size for metrics
514-
// tracking.
515-
oldSize, err := t.sizeNolock()
516-
if err != nil {
517-
return err
518-
}
519543
// Count how many items can be deleted from the file.
520544
var (
521545
newDeleted = items
522546
deleted = t.itemOffset.Load()
523547
)
548+
// Hidden items exceed the current tail file, drop the relevant data files.
524549
for current := items - 1; current >= deleted; current -= 1 {
525550
if _, err := t.index.ReadAt(buffer, int64((current-deleted+1)*indexEntrySize)); err != nil {
526551
return err
@@ -877,7 +902,11 @@ func (t *freezerTable) sizeNolock() (uint64, error) {
877902
if err != nil {
878903
return 0, err
879904
}
880-
total := uint64(t.maxFileSize)*uint64(t.headId-t.tailId) + uint64(t.headBytes) + uint64(stat.Size())
905+
hiddenBytes, err := t.hiddenBytes()
906+
if err != nil {
907+
return 0, err
908+
}
909+
total := uint64(t.maxFileSize)*uint64(t.headId-t.tailId) + uint64(t.headBytes) + uint64(stat.Size()) - uint64(hiddenBytes)
881910
return total, nil
882911
}
883912

core/rawdb/freezer_table_test.go

+31
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,13 @@ func TestFreezerOffset(t *testing.T) {
658658
}
659659
}
660660

661+
func assertTableSize(t *testing.T, f *freezerTable, size int) {
662+
t.Helper()
663+
if size_, err := f.size(); size_ != uint64(size) {
664+
t.Fatalf("expected size of %d bytes, got %d, err: %v", size, size_, err)
665+
}
666+
}
667+
661668
func TestTruncateTail(t *testing.T) {
662669
t.Parallel()
663670
rm, wm, sg := metrics.NewMeter(), metrics.NewMeter(), metrics.NewGauge()
@@ -692,6 +699,9 @@ func TestTruncateTail(t *testing.T) {
692699
5: getChunk(20, 0xaa),
693700
6: getChunk(20, 0x11),
694701
})
702+
// maxFileSize*fileCount + headBytes + indexFileSize - hiddenBytes
703+
expected := 40*3 + 20 + 48 - 0
704+
assertTableSize(t, f, expected)
695705

696706
// truncate single element( item 0 ), deletion is only supported at file level
697707
f.truncateTail(1)
@@ -707,6 +717,8 @@ func TestTruncateTail(t *testing.T) {
707717
5: getChunk(20, 0xaa),
708718
6: getChunk(20, 0x11),
709719
})
720+
expected = 40*3 + 20 + 48 - 20
721+
assertTableSize(t, f, expected)
710722

711723
// Reopen the table, the deletion information should be persisted as well
712724
f.Close()
@@ -739,6 +751,8 @@ func TestTruncateTail(t *testing.T) {
739751
5: getChunk(20, 0xaa),
740752
6: getChunk(20, 0x11),
741753
})
754+
expected = 40*2 + 20 + 36 - 0
755+
assertTableSize(t, f, expected)
742756

743757
// Reopen the table, the above testing should still pass
744758
f.Close()
@@ -760,6 +774,23 @@ func TestTruncateTail(t *testing.T) {
760774
6: getChunk(20, 0x11),
761775
})
762776

777+
// truncate 3 more elements( item 2, 3, 4), the file 1 should be deleted
778+
// file 2 should only contain item 5
779+
f.truncateTail(5)
780+
checkRetrieveError(t, f, map[uint64]error{
781+
0: errOutOfBounds,
782+
1: errOutOfBounds,
783+
2: errOutOfBounds,
784+
3: errOutOfBounds,
785+
4: errOutOfBounds,
786+
})
787+
checkRetrieve(t, f, map[uint64][]byte{
788+
5: getChunk(20, 0xaa),
789+
6: getChunk(20, 0x11),
790+
})
791+
expected = 40*1 + 20 + 24 - 20
792+
assertTableSize(t, f, expected)
793+
763794
// truncate all, the entire freezer should be deleted
764795
f.truncateTail(7)
765796
checkRetrieveError(t, f, map[uint64]error{

0 commit comments

Comments
 (0)