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 <uints.h>
#include <chrono>
#include <thread>
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

View file

@ -6,9 +6,7 @@
#include <trie.h>
#include <algorithm>
#include <chrono>
#include <memory>
#include <thread>
#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;

View file

@ -6,6 +6,7 @@
#include <txdb.h>
#include <chainparams.h>
#include <claimtrie/sqlite.h>
#include <hash.h>
#include <random.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 << "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);
@ -305,7 +310,11 @@ bool CBlockTreeDB::BatchWrite(const std::vector<std::pair<int, const CBlockFileI
ibi++;
}
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:
if (sync) {
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.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;
}
}