Skip to content

Commit eb5a449

Browse files
committed
core/rawdb: change freezerTableMeta.flushOffset to int64
The flushOffset is a file position, which is usually tracked as int64 in Go. It's better to keep it as the same type to avoid conversion. Overall, this change removes some conversions, and introduces some. The new conversions are in cases where flushOffset is compared with numEntries*indexEntrySize computations.
1 parent 791988f commit eb5a449

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

core/rawdb/freezer_meta.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ package rawdb
1919
import (
2020
"errors"
2121
"io"
22+
"math"
2223
"os"
2324

25+
"github.com/ethereum/go-ethereum/log"
2426
"github.com/ethereum/go-ethereum/rlp"
2527
)
2628

@@ -62,7 +64,7 @@ type freezerTableMeta struct {
6264
//
6365
// The offset could be moved forward by applying sync operation, or be moved
6466
// backward in cases of head/tail truncation, etc.
65-
flushOffset uint64
67+
flushOffset int64
6668
}
6769

6870
// decodeV1 attempts to decode the metadata structure in v1 format. If fails or
@@ -109,11 +111,15 @@ func decodeV2(file *os.File) *freezerTableMeta {
109111
if o.Version != freezerTableV2 {
110112
return nil
111113
}
114+
if o.Offset > math.MaxInt64 {
115+
log.Error("Invalid flushOffset %d in freezer metadata", o.Offset, "file", file.Name())
116+
return nil
117+
}
112118
return &freezerTableMeta{
113119
file: file,
114120
version: freezerTableV2,
115121
virtualTail: o.Tail,
116-
flushOffset: o.Offset,
122+
flushOffset: int64(o.Offset),
117123
}
118124
}
119125

@@ -152,7 +158,7 @@ func (m *freezerTableMeta) setVirtualTail(tail uint64, sync bool) error {
152158
}
153159

154160
// setFlushOffset sets the flush offset and flushes the metadata if sync is true.
155-
func (m *freezerTableMeta) setFlushOffset(offset uint64, sync bool) error {
161+
func (m *freezerTableMeta) setFlushOffset(offset int64, sync bool) error {
156162
m.flushOffset = offset
157163
return m.write(sync)
158164
}
@@ -167,7 +173,7 @@ func (m *freezerTableMeta) write(sync bool) error {
167173
var o obj
168174
o.Version = freezerVersion // forcibly use the current version
169175
o.Tail = m.virtualTail
170-
o.Offset = m.flushOffset
176+
o.Offset = uint64(m.flushOffset)
171177

172178
_, err := m.file.Seek(0, io.SeekStart)
173179
if err != nil {

core/rawdb/freezer_meta_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestUpgradeMetadata(t *testing.T) {
7979
if meta.virtualTail != uint64(100) {
8080
t.Fatal("Unexpected virtual tail field")
8181
}
82-
if meta.flushOffset != uint64(0) {
82+
if meta.flushOffset != 0 {
8383
t.Fatal("Unexpected flush offset field")
8484
}
8585

@@ -95,7 +95,7 @@ func TestUpgradeMetadata(t *testing.T) {
9595
if meta.virtualTail != uint64(100) {
9696
t.Fatal("Unexpected virtual tail field")
9797
}
98-
if meta.flushOffset != uint64(100) {
98+
if meta.flushOffset != 100 {
9999
t.Fatal("Unexpected flush offset field")
100100
}
101101
}

core/rawdb/freezer_table.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ func (t *freezerTable) repair() error {
320320
// offset is updated, leaving a dangling reference that points to a position
321321
// outside the file. If so, the offset will be reset to the new end of the
322322
// file during the next run.
323-
if t.metadata.flushOffset > uint64(newOffset) {
324-
if err := t.metadata.setFlushOffset(uint64(newOffset), true); err != nil {
323+
if t.metadata.flushOffset > newOffset {
324+
if err := t.metadata.setFlushOffset(newOffset, true); err != nil {
325325
return err
326326
}
327327
}
@@ -406,31 +406,31 @@ func (t *freezerTable) repairIndex() error {
406406
// index file to avoid clearing the entire table.
407407
if t.metadata.version == freezerTableV1 {
408408
t.logger.Info("Recovering freezer flushOffset for legacy table", "offset", size)
409-
return t.metadata.setFlushOffset(uint64(size), true)
409+
return t.metadata.setFlushOffset(size, true)
410410
}
411411

412412
switch {
413413
case size == indexEntrySize && t.metadata.flushOffset == 0:
414414
// It's a new freezer table with no content.
415415
// Move the flush offset to the end of the file.
416-
return t.metadata.setFlushOffset(uint64(size), true)
416+
return t.metadata.setFlushOffset(size, true)
417417

418-
case size == int64(t.metadata.flushOffset):
418+
case size == t.metadata.flushOffset:
419419
// flushOffset is aligned with the index file, all is well.
420420
return nil
421421

422-
case size > int64(t.metadata.flushOffset):
422+
case size > t.metadata.flushOffset:
423423
// Extra index items have been detected beyond the flush offset. Since these
424424
// entries correspond to data that has not been fully flushed to disk in the
425425
// last run (because of unclean shutdown), their integrity cannot be guaranteed.
426426
// To ensure consistency, these index items will be truncated, as there is no
427427
// reliable way to validate or recover their associated data.
428-
extraSize := size - int64(t.metadata.flushOffset)
428+
extraSize := size - t.metadata.flushOffset
429429
if t.readonly {
430430
return fmt.Errorf("index file(path: %s, name: %s) contains %d garbage data bytes", t.path, t.name, extraSize)
431431
}
432432
t.logger.Warn("Truncating freezer items after flushOffset", "size", extraSize)
433-
return truncateFreezerFile(t.index, int64(t.metadata.flushOffset))
433+
return truncateFreezerFile(t.index, t.metadata.flushOffset)
434434

435435
default: // size < flushOffset
436436
// Flush offset refers to a position larger than index file. The only
@@ -442,7 +442,7 @@ func (t *freezerTable) repairIndex() error {
442442
return nil // do nothing in read only mode
443443
}
444444
t.logger.Warn("Rewinding freezer flushOffset", "old", t.metadata.flushOffset, "new", size)
445-
return t.metadata.setFlushOffset(uint64(size), true)
445+
return t.metadata.setFlushOffset(size, true)
446446
}
447447
}
448448

@@ -629,8 +629,8 @@ func (t *freezerTable) truncateHead(items uint64) error {
629629
// offset is updated, leaving a dangling reference that points to a position
630630
// outside the file. If so, the offset will be reset to the new end of the
631631
// file during the next run.
632-
if t.metadata.flushOffset > newOffset {
633-
if err := t.metadata.setFlushOffset(newOffset, true); err != nil {
632+
if t.metadata.flushOffset > int64(newOffset) {
633+
if err := t.metadata.setFlushOffset(int64(newOffset), true); err != nil {
634634
return err
635635
}
636636
}
@@ -811,7 +811,7 @@ func (t *freezerTable) truncateTail(items uint64) error {
811811
//
812812
// Note, both the index and head data file has been persisted before performing
813813
// tail truncation and all the items in these files are regarded as complete.
814-
shorten := indexEntrySize * (newDeleted - deleted)
814+
shorten := indexEntrySize * int64(newDeleted-deleted)
815815
if t.metadata.flushOffset <= shorten {
816816
return fmt.Errorf("invalid index flush offset: %d, shorten: %d", t.metadata.flushOffset, shorten)
817817
} else {
@@ -1197,7 +1197,7 @@ func (t *freezerTable) syncWithNoLock() error {
11971197
return err
11981198
}
11991199
offset := stat.Size()
1200-
trackError(t.metadata.setFlushOffset(uint64(offset), true))
1200+
trackError(t.metadata.setFlushOffset(offset, true))
12011201
return err
12021202
}
12031203

core/rawdb/freezer_table_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ func TestFlushOffsetTracking(t *testing.T) {
14321432

14331433
var cases = []struct {
14341434
op func(*freezerTable)
1435-
offset uint64
1435+
offset int64
14361436
}{
14371437
{
14381438
// Data files:

0 commit comments

Comments
 (0)