Try commit changes in a minute

Signed-off-by: Anthony Fieroni <bvbfan@abv.bg>
This commit is contained in:
Anthony Fieroni 2019-12-11 20:29:37 +02:00 committed by Brannon King
parent bb240e290b
commit b074ed0d79
3 changed files with 45 additions and 23 deletions

View file

@ -5,6 +5,9 @@
#include <sqlite/sqlite3.h> #include <sqlite/sqlite3.h>
#include <uints.h> #include <uints.h>
#include <chrono>
#include <thread>
namespace sqlite namespace sqlite
{ {
inline int bind_col_in_db(sqlite3_stmt* stmt, int inx, const CUint160& val) { 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); std::memcpy(ret.begin(), ptr, bytes);
return ret; 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 #endif // SQLITE_H

View file

@ -6,9 +6,7 @@
#include <trie.h> #include <trie.h>
#include <algorithm> #include <algorithm>
#include <chrono>
#include <memory> #include <memory>
#include <thread>
#define logPrint CLogPrint::global() #define logPrint CLogPrint::global()
@ -467,23 +465,11 @@ bool CClaimTrieCacheBase::flush()
{ {
if (transacting) { if (transacting) {
getMerkleHash(); getMerkleHash();
do { auto code = sqlite::commit(db);
try { if (code != SQLITE_OK) {
db << "commit"; logPrint << "ERROR in CClaimTrieCacheBase::" << __func__ << "(): SQLite code: " << code << Clog::endl;
} catch (const sqlite::sqlite_exception& e) { return false;
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);
transacting = false; transacting = false;
} }
base->nNextHeight = nNextHeight; base->nNextHeight = nNextHeight;

View file

@ -6,6 +6,7 @@
#include <txdb.h> #include <txdb.h>
#include <chainparams.h> #include <chainparams.h>
#include <claimtrie/sqlite.h>
#include <hash.h> #include <hash.h>
#include <random.h> #include <random.h>
#include <pow.h> #include <pow.h>
@ -132,7 +133,11 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, boo
db << "INSERT OR REPLACE INTO marker VALUES('best_block', ?)" << hashBlock; db << "INSERT OR REPLACE INTO marker VALUES('best_block', ?)" << hashBlock;
db << "DELETE FROM marker WHERE name = 'head_block'"; 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); LogPrint(BCLog::COINDB, "Committed %u changed transaction outputs (out of %u) to coin database...\n", (unsigned int)changed, (unsigned int)count);
if (sync) { if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
@ -305,7 +310,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileI
ibi++; ibi++;
} }
ibi.used(true); ibi.used(true);
db << "commit"; auto code = sqlite::commit(db);
if (code != SQLITE_OK) {
LogPrintf("%s: Error committing block info to database. SQLite error: %d\n", __func__, code);
return false;
}
// by Sync they mean disk sync: // by Sync they mean disk sync:
if (sync) { if (sync) {
auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr); auto rc = sqlite3_wal_checkpoint_v2(db.connection().get(), nullptr, SQLITE_CHECKPOINT_FULL, nullptr, nullptr);
@ -380,7 +389,11 @@ bool CBlockTreeDB::WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos>
query++; query++;
} }
query.used(true); 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; return true;
} }
@ -391,4 +404,4 @@ bool CBlockTreeDB::ReadTxIndex(const uint256 &txid, CDiskTxPos &pos) {
return true; return true;
} }
return false; return false;
} }