go-ethereum/triedb/pathdb
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
..
database_test.go triedb/pathdb: fix flaky test in pathdb (#29901) 2024-06-28 21:15:54 +08:00
database.go core/rawdb, triedb, cmd: create an isolated disk namespace for verkle (#30105) 2024-07-16 16:17:58 +03:00
difflayer_test.go core/rawdb, trie: improve db APIs for accessing trie nodes (#29362) 2024-04-30 16:25:35 +02:00
difflayer.go core: add an end-to-end verkle test (#29262) 2024-03-26 21:25:41 +01:00
disklayer.go core/rawdb: freezer index repair (#29792) 2024-10-01 18:16:16 +02:00
errors.go all: remove duplicate word in comments (#29531) 2024-04-15 08:34:31 +02:00
execute.go trie: relocate state execution logic into pathdb package (#29861) 2024-06-27 20:30:39 +08:00
history_inspect.go core/rawdb: implement in-memory freezer (#29135) 2024-04-30 11:33:22 +02:00
history_test.go core/rawdb, triedb, cmd: create an isolated disk namespace for verkle (#30105) 2024-07-16 16:17:58 +03:00
history.go triedb/pathdb: use maps.Clone and maps.Keys (#29985) 2024-06-17 17:09:29 +02:00
journal.go core/rawdb, trie: improve db APIs for accessing trie nodes (#29362) 2024-04-30 16:25:35 +02:00
layertree.go all: remove the dependency from trie to triedb (#28824) 2024-02-13 14:49:53 +01:00
metrics.go core: add an end-to-end verkle test (#29262) 2024-03-26 21:25:41 +01:00
nodebuffer.go core/rawdb: freezer index repair (#29792) 2024-10-01 18:16:16 +02:00
reader.go triedb/pathdb: print out all trie owner and hash information (#30200) 2024-07-24 20:32:28 +08:00