go-ethereum/core/rawdb
rjl493456442 eff0bed91b
core/rawdb: freezer index repair (#29792)
This pull request removes the `fsync` of index files in freezer.ModifyAncients function for 
performance gain.

Originally, fsync is added after each freezer write operation to ensure
the written data is truly transferred into disk. Unfortunately, it turns 
out `fsync` can be relatively slow, especially on
macOS (see https://github.com/ethereum/go-ethereum/issues/28754 for more
information). 

In this pull request, fsync for index file is removed as it turns out
index file can be recovered even after a unclean shutdown. But fsync for data file is still kept, as
we have no meaningful way to validate the data correctness after unclean shutdown.

---

**But why do we need the `fsync` in the first place?** 

As it's necessary for freezer to survive/recover after the machine crash
(e.g. power failure).
In linux, whenever the file write is performed, the file metadata update
and data update are
not necessarily performed at the same time. Typically, the metadata will
be flushed/journalled
ahead of the file data. Therefore, we make the pessimistic assumption
that the file is first
extended with invalid "garbage" data (normally zero bytes) and that
afterwards the correct
data replaces the garbage. 

We have observed that the index file of the freezer often contain
garbage entry with zero value
(filenumber = 0, offset = 0) after a machine power failure. It proves
that the index file is extended
without the data being flushed. And this corruption can destroy the
whole freezer data eventually.

Performing fsync after each write operation can reduce the time window
for data to be transferred
to the disk and ensure the correctness of the data in the disk to the
greatest extent.

---

**How can we maintain this guarantee without relying on fsync?**

Because the items in the index file are strictly in order, we can
leverage this characteristic to
detect the corruption and truncate them when freezer is opened.
Specifically these validation
rules are performed for each index file:

For two consecutive index items:

- If their file numbers are the same, then the offset of the latter one
MUST not be less than that of the former.
- If the file number of the latter one is equal to that of the former
plus one, then the offset of the latter one MUST not be 0.
- If their file numbers are not equal, and the latter's file number is
not equal to the former plus 1, the latter one is valid

And also, for the first non-head item, it must refer to the earliest
data file, or the next file if the
earliest file is not sufficient to place the first item(very special
case, only theoretical possible
in tests)

With these validation rules, we can detect the invalid item in index
file with greatest possibility.

--- 

But unfortunately, these scenarios are not covered and could still lead
to a freezer corruption if it occurs:

**All items in index file are in zero value**

It's impossible to distinguish if they are truly zero (e.g. all the data
entries maintained in freezer
are zero size) or just the garbage left by OS. In this case, these index
items will be kept by truncating
the entire data file, namely the freezer is corrupted.

However, we can consider that the probability of this situation
occurring is quite low, and even
if it occurs, the freezer can be considered to be close to an empty
state. Rerun the state sync
should be acceptable.

**Index file is integral while relative data file is corrupted**

It might be possible the data file is corrupted whose file size is
extended correctly with garbage
filled (e.g. zero bytes). In this case, it's impossible to detect the
corruption by index validation.

We can either choose to `fsync` the data file, or blindly believe that
if index file is integral then
the data file could be integral with very high chance. In this pull
request, the first option is taken.
2024-10-01 18:16:16 +02:00
..
ancienttest core/rawdb: implement in-memory freezer (#29135) 2024-04-30 11:33:22 +02:00
testdata core/rawdb: avoid unnecessary receipt processing for log filtering (#23147) 2021-09-28 12:54:49 +02:00
accessors_chain.go core/rawdb: remove unused deriveLogFields (#29913) 2024-06-05 21:05:00 +08:00
accessors_chain_test.go core/rawdb: remove unused deriveLogFields (#29913) 2024-06-05 21:05:00 +08:00
accessors_indexes.go core/types, params: add blob transaction type, RLP encoded for now (#27049) 2023-04-21 12:52:02 +03:00
accessors_indexes_test.go all: refactor so `NewBlock`, `WithBody` take `types.Body` (#29482) 2024-04-30 14:55:08 +02:00
accessors_metadata.go Revert "core/rawdb: remove unused transition status state accessors" (#30449) 2024-09-18 11:53:50 +03:00
accessors_snapshot.go all: fix various typos (#29542) 2024-04-16 15:44:00 +03:00
accessors_state.go all: implement path-based state scheme (#25963) 2023-08-01 15:17:32 +03:00
accessors_sync.go core, accounts, eth, trie: handle genesis state missing (#28171) 2023-09-28 10:00:53 +03:00
accessors_trie.go core/rawdb: make sure specified state scheme is valid (#30499) 2024-09-24 09:26:29 +02:00
ancient_scheme.go core/rawdb, triedb, cmd: create an isolated disk namespace for verkle (#30105) 2024-07-16 16:17:58 +03:00
ancient_utils.go core/rawdb, triedb, cmd: create an isolated disk namespace for verkle (#30105) 2024-07-16 16:17:58 +03:00
chain_freezer.go core/rawdb: implement in-memory freezer (#29135) 2024-04-30 11:33:22 +02:00
chain_iterator.go core, core/rawdb, eth/sync: no tx indexing during snap sync (#28703) 2024-01-22 21:05:18 +01:00
chain_iterator_test.go all: refactor so `NewBlock`, `WithBody` take `types.Body` (#29482) 2024-04-30 14:55:08 +02:00
database.go core/rawdb: drop MigrateTable (#30331) 2024-08-20 14:06:00 +02:00
database_test.go all: update license headers and AUTHORS from git history (#24947) 2022-05-24 20:39:40 +02:00
freezer.go core/rawdb: more accurate description of freezer in docs (#30393) 2024-09-13 11:17:40 +02:00
freezer_batch.go core/rawdb: freezer index repair (#29792) 2024-10-01 18:16:16 +02:00
freezer_memory.go core/rawdb: drop MigrateTable (#30331) 2024-08-20 14:06:00 +02:00
freezer_memory_test.go core/rawdb: implement in-memory freezer (#29135) 2024-04-30 11:33:22 +02:00
freezer_meta.go all: update license headers and AUTHORS from git history (#24947) 2022-05-24 20:39:40 +02:00
freezer_meta_test.go all: simplify tests using t.TempDir() (#30150) 2024-07-15 15:26:58 +02:00
freezer_resettable.go core/rawdb: drop MigrateTable (#30331) 2024-08-20 14:06:00 +02:00
freezer_resettable_test.go core/rawdb: implement in-memory freezer (#29135) 2024-04-30 11:33:22 +02:00
freezer_table.go core/rawdb: freezer index repair (#29792) 2024-10-01 18:16:16 +02:00
freezer_table_test.go core/rawdb: freezer index repair (#29792) 2024-10-01 18:16:16 +02:00
freezer_test.go core/rawdb: drop MigrateTable (#30331) 2024-08-20 14:06:00 +02:00
freezer_utils.go core/rawdb: fsync the index file after each freezer write (#28483) 2023-11-10 12:56:39 +02:00
freezer_utils_test.go all: more linters (#24783) 2022-06-13 16:24:45 +02:00
key_length_iterator.go core/rawdb: add specified key length iterator (#24535) 2022-03-15 10:28:26 +02:00
key_length_iterator_test.go core/rawdb: add specified key length iterator (#24535) 2022-03-15 10:28:26 +02:00
schema.go core/rawdb, triedb, cmd: create an isolated disk namespace for verkle (#30105) 2024-07-16 16:17:58 +03:00
table.go core/rawdb: drop MigrateTable (#30331) 2024-08-20 14:06:00 +02:00
table_test.go all: simplify and fix database iteration with prefix/start (#20808) 2020-04-15 14:08:53 +03:00