Skip to content

Commit 67708ac

Browse files
committed
Write block index more frequently than cache flushes
1 parent b3ed423 commit 67708ac

File tree

2 files changed

+44
-17
lines changed

2 files changed

+44
-17
lines changed

src/main.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1880,6 +1880,8 @@ enum FlushStateMode {
18801880
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
18811881
LOCK2(cs_main, cs_LastBlockFile);
18821882
static int64_t nLastWrite = 0;
1883+
static int64_t nLastFlush = 0;
1884+
static int64_t nLastSetChain = 0;
18831885
std::set<int> setFilesToPrune;
18841886
bool fFlushForPrune = false;
18851887
try {
@@ -1893,16 +1895,36 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
18931895
}
18941896
}
18951897
}
1896-
if ((mode == FLUSH_STATE_ALWAYS) ||
1897-
((mode == FLUSH_STATE_PERIODIC || mode == FLUSH_STATE_IF_NEEDED) && pcoinsTip->DynamicMemoryUsage() > nCoinCacheUsage) ||
1898-
(mode == FLUSH_STATE_PERIODIC && GetTimeMicros() > nLastWrite + DATABASE_WRITE_INTERVAL * 1000000) ||
1899-
fFlushForPrune) {
1900-
// Typical CCoins structures on disk are around 100 bytes in size.
1898+
int64_t nNow = GetTimeMicros();
1899+
// Avoid writing/flushing immediately after startup.
1900+
if (nLastWrite == 0) {
1901+
nLastWrite = nNow;
1902+
}
1903+
if (nLastFlush == 0) {
1904+
nLastFlush = nNow;
1905+
}
1906+
if (nLastSetChain == 0) {
1907+
nLastSetChain = nNow;
1908+
}
1909+
size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1910+
// The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
1911+
bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
1912+
// The cache is over the limit, we have to write now.
1913+
bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
1914+
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
1915+
bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
1916+
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
1917+
bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
1918+
// Combine all conditions that result in a full cache flush.
1919+
bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
1920+
// Write blocks and block index to disk.
1921+
if (fDoFullFlush || fPeriodicWrite) {
1922+
// Typical CCoins structures on disk are around 128 bytes in size.
19011923
// Pushing a new one to the database can cause it to be written
19021924
// twice (once in the log, and once in the tables). This is already
19031925
// an overestimation, as most will delete an existing entry or
19041926
// overwrite one. Still, use a conservative safety factor of 2.
1905-
if (!CheckDiskSpace(100 * 2 * 2 * pcoinsTip->GetCacheSize()))
1927+
if (fDoFullFlush && !CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
19061928
return state.Error("out of disk space");
19071929
// First make sure all block and undo data is flushed to disk.
19081930
FlushBlockFile();
@@ -1924,21 +1946,24 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
19241946
return state.Abort("Files to write to block index database");
19251947
}
19261948
}
1927-
// Flush the chainstate (which may refer to block index entries).
1928-
if (!pcoinsTip->Flush())
1929-
return state.Abort("Failed to write to coin database");
1930-
19311949
// Finally remove any pruned files
19321950
if (fFlushForPrune) {
19331951
UnlinkPrunedFiles(setFilesToPrune);
19341952
fCheckForPruning = false;
19351953
}
1936-
1954+
nLastWrite = nNow;
1955+
}
1956+
// Flush best chain related state. This can only be done if the blocks / block index write was also done.
1957+
if (fDoFullFlush) {
1958+
// Flush the chainstate (which may refer to block index entries).
1959+
if (!pcoinsTip->Flush())
1960+
return state.Abort("Failed to write to coin database");
1961+
nLastFlush = nNow;
1962+
}
1963+
if ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000) {
19371964
// Update best block in wallet (so we can detect restored wallets).
1938-
if (mode != FLUSH_STATE_IF_NEEDED) {
1939-
GetMainSignals().SetBestChain(chainActive.GetLocator());
1940-
}
1941-
nLastWrite = GetTimeMicros();
1965+
GetMainSignals().SetBestChain(chainActive.GetLocator());
1966+
nLastSetChain = nNow;
19421967
}
19431968
} catch (const std::runtime_error& e) {
19441969
return state.Abort(std::string("System error while flushing: ") + e.what());

src/main.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ static const unsigned int MAX_HEADERS_RESULTS = 2000;
8282
* degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning
8383
* harder). We'll probably want to make this a per-peer adaptive value at some point. */
8484
static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
85-
/** Time to wait (in seconds) between writing blockchain state to disk. */
86-
static const unsigned int DATABASE_WRITE_INTERVAL = 3600;
85+
/** Time to wait (in seconds) between writing blocks/block index to disk. */
86+
static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
87+
/** Time to wait (in seconds) between flushing chainstate to disk. */
88+
static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
8789
/** Maximum length of reject messages. */
8890
static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
8991

0 commit comments

Comments
 (0)