You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Merge bitcoin#30611: validation: write chainstate to disk every hour
e976bd3 validation: add randomness to periodic write interval (Andrew Toth)
2e2f410 refactor: replace m_last_write with m_next_write (Andrew Toth)
b557fa7 refactor: rename fDoFullFlush to should_write (Andrew Toth)
d73bd9f validation: write chainstate to disk every hour (Andrew Toth)
0ad7d7a test: chainstate write test for periodic chainstate flush (Andrew Toth)
Pull request description:
Since bitcoin#28233, periodically writing the chainstate to disk every 24 hours does not clear the dbcache. Since bitcoin#28280, periodically writing the chainstate to disk is proportional only to the amount of dirty entries in the cache. Due to these changes, it is no longer beneficial to only write the chainstate to disk every 24 hours. The periodic flush interval was necessary because every write of the chainstate would clear the dbcache. Now, we can get rid of the periodic flush interval and simply write the chainstate along with blocks and block index at least every hour.
Three benefits of doing this:
1. For IBD or reindex-chainstate with a combination of large dbcache setting, slow CPU, slow internet speed/unreliable peers, it could be up to 24 hours until the chainstate is persisted to disk. A power outage or crash could potentially lose up to 24 hours of progress. If there is a very large amount of dirty cache entries, writing to disk when a flush finally does occur will take a very long time. Crashing during this window of writing can cause bitcoin#11600. By syncing every hour in unison with the block index we avoid this problem. Only a maximum of one hour of progress can be lost, and the window for crashing during writing is much smaller. For IBD with lower dbcache settings, faster CPU, or better internet speed/reliable peers, chainstate writes are already triggered more often than every hour so this change will have no effect on IBD.
2. Based on discussion in bitcoin#28280, writing only once every 24 hours during long running operation of a node causes IO spikes. Writing smaller chainstate changes every hour like we do with blocks and block index will reduce IO spikes.
3. Faster shutdown speeds. All dirty chainstate entries must be persisted to disk on shutdown. If we have a lot of dirty entries, such as when close to 24 hours or if we sync with a large dbcache, it can take a long time to shutdown. By keeping the chainstate clean we avoid this problem.
Inspired by [this comment](bitcoin#28280 (comment)).
Resolvesbitcoin#11600
ACKs for top commit:
achow101:
ACK e976bd3
davidgumberg:
utACK bitcoin@e976bd3
sipa:
utACK e976bd3
l0rinc:
ACK e976bd3
Tree-SHA512: 5bccd8f1dea47f9820a3fd32fe3bb6841c0167b3d6870cc8f3f7e2368f124af1a914bca6acb06889cd7183638a8dbdbace54d3237c3683f2b567eb7355e015ee
// It's been a while since we wrote the block index and chain state to disk. Do this frequently, so we don't need to redownload or reindex after a crash.
// Flush best chain related state. This can only be done if the blocks / block index write was also done.
2907
-
if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
2908
-
if (coins_mem_usage >= WARN_FLUSH_COINS_SIZE) LogWarning("Flushing large (%d GiB) UTXO set to disk, it may take several minutes", coins_mem_usage >> 30);
2909
-
LOG_TIME_MILLIS_WITH_CATEGORY(strprintf("write coins cache to disk (%d coins, %.2fKiB)",
if (empty_cache ? !CoinsTip().Flush() : !CoinsTip().Sync()) {
2923
-
returnFatalError(m_chainman.GetNotifications(), state, _("Failed to write to coin database."));
2896
+
2897
+
if (!CoinsTip().GetBestBlock().IsNull()) {
2898
+
if (coins_mem_usage >= WARN_FLUSH_COINS_SIZE) LogWarning("Flushing large (%d GiB) UTXO set to disk, it may take several minutes", coins_mem_usage >> 30);
2899
+
LOG_TIME_MILLIS_WITH_CATEGORY(strprintf("write coins cache to disk (%d coins, %.2fKiB)",
0 commit comments