From 55730549afc3af18977d81bacc068bf98f9212d7 Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Thu, 30 Jan 2020 17:42:21 +0200 Subject: [PATCH] Increase attempts to database sync Signed-off-by: Anthony Fieroni --- src/claimtrie/sqlite.h | 15 +++++++++++++++ src/claimtrie/trie.cpp | 3 +-- src/txdb.cpp | 14 ++++++++++---- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/claimtrie/sqlite.h b/src/claimtrie/sqlite.h index 7f2b8d534..bb9be35c4 100644 --- a/src/claimtrie/sqlite.h +++ b/src/claimtrie/sqlite.h @@ -88,6 +88,21 @@ namespace sqlite } return code; } + + inline int sync(database& db, std::size_t attempts = 200) + { + int code = SQLITE_OK; + for (auto i = 0u; i < attempts; ++i) { + code = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); + if (code != SQLITE_OK) { + using namespace std::chrono_literals; + std::this_thread::sleep_for(100ms); + continue; + } + break; + } + return code; + } } #endif // SQLITE_H diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index 51c0254f0..7f6449cf6 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -131,8 +131,7 @@ CClaimTrieCacheBase::~CClaimTrieCacheBase() bool CClaimTrie::SyncToDisk() { // alternatively, switch to full sync after we are caught up on the chain - auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); - return rc == SQLITE_OK; + return sqlite::sync(db) == SQLITE_OK; } bool CClaimTrie::empty() // only used for testing diff --git a/src/txdb.cpp b/src/txdb.cpp index 6ecc2ff71..09e7ad83c 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -158,8 +158,11 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo } LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count); if (sync) { - auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); - return rc == SQLITE_OK; + auto code = sqlite::sync(db); + if (code != SQLITE_OK) { + LogPrint(BCLog::COINDB, "Error syncing coin database. SQLite error: %d\n", code); + return false; + } } return true; } @@ -337,8 +340,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector