From 94754becac0f764f043389cc2b3c8e71580a0bbe Mon Sep 17 00:00:00 2001 From: Anthony Fieroni Date: Wed, 11 Dec 2019 20:29:37 +0200 Subject: [PATCH] Try commit changes in a minute Signed-off-by: Anthony Fieroni --- src/claimtrie/sqlite.h | 23 +++++++++++++++++++++++ src/claimtrie/trie.cpp | 24 +++++------------------- src/txdb.cpp | 21 +++++++++++++++++---- 3 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/claimtrie/sqlite.h b/src/claimtrie/sqlite.h index bbf6e5410..8fda04ac7 100644 --- a/src/claimtrie/sqlite.h +++ b/src/claimtrie/sqlite.h @@ -5,6 +5,9 @@ #include #include +#include +#include + namespace sqlite { inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const CUint160& val) { @@ -53,6 +56,26 @@ namespace sqlite std::memcpy(ret.begin(), ptr, bytes); return ret; } + + inline int commit(database& db, std::size_t attempts = 60) + { + int code = SQLITE_OK; + for (auto i = 0u; i < attempts; ++i) { + try { + db << "commit"; + } catch (const sqlite_exception& e) { + code = e.get_code(); + if (code == SQLITE_LOCKED || code == SQLITE_BUSY) { + using namespace std::chrono_literals; + std::this_thread::sleep_for(1s); + continue; + } + return code; + } + return SQLITE_OK; + } + return code; + } } #endif // SQLITE_H diff --git a/src/claimtrie/trie.cpp b/src/claimtrie/trie.cpp index a7f975b5b..a53c57063 100644 --- a/src/claimtrie/trie.cpp +++ b/src/claimtrie/trie.cpp @@ -6,9 +6,7 @@ #include #include -#include #include -#include #define logPrint CLogPrint::global() @@ -467,23 +465,11 @@ bool CClaimTrieCacheBase::flush() { if (transacting) { getMerkleHash(); - do { - try { - db << "commit"; - } catch (const sqlite::sqlite_exception& e) { - auto code = e.get_code(); - if (code == SQLITE_LOCKED || code == SQLITE_BUSY) { - logPrint << "Retrying the commit in one second." << Clog::endl; - using namespace std::chrono_literals; - std::this_thread::sleep_for(1s); - continue; - } else { - logPrint << "ERROR in ClaimTrieCache::" << __func__ << "(): " << e.what() << Clog::endl; - return false; - } - } - break; - } while (true); + auto code = sqlite::commit(db); + if (code != SQLITE_OK) { + logPrint << "ERROR in CClaimTrieCacheBase::" << __func__ << "(): SQLite code: " << code << Clog::endl; + return false; + } transacting = false; } base->nNextHeight = nNextHeight; diff --git a/src/txdb.cpp b/src/txdb.cpp index 5d681a5cb..4f0adca03 100644 --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -131,7 +132,11 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo db << "INSERT OR REPLACE INTO marker VALUES('best_block', ?)" << hashBlock; db << "DELETE FROM marker WHERE name = 'head_block'"; - db << "commit"; + auto code = sqlite::commit(db); + if (code != SQLITE_OK) { + LogPrint(BCLog::COINDB, "Error committing transaction outputs changes to coin database. SQLite error: %d\n", code); + return false; + } 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); @@ -304,7 +309,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector query++; } query.used(true); - db << "commit"; + auto code = sqlite::commit(db); + if (code != SQLITE_OK) { + LogPrintf("%s: Error committing tx to database. SQLite error: %d\n", __func__, code); + return false; + } return true; } @@ -391,4 +404,4 @@ bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) { return true; } return false; -} \ No newline at end of file +}