From c2dfcbe3436d3ccedde17e88ce8f183cf5063a11 Mon Sep 17 00:00:00 2001 From: Pedro Pinto Date: Wed, 14 Nov 2018 20:56:41 +0000 Subject: [PATCH] Made sql store a bit more tolerant to errors. --- src/lib/persistence/SqliteStore.cpp | 70 ++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/src/lib/persistence/SqliteStore.cpp b/src/lib/persistence/SqliteStore.cpp index 814ae95f1..1c4a2c833 100644 --- a/src/lib/persistence/SqliteStore.cpp +++ b/src/lib/persistence/SqliteStore.cpp @@ -2,12 +2,13 @@ #include "lib/logging/logging.h" #include "lib/os/os.h" +#include "lib/persistence/persistence_utils.h" #include "lib/utils/Sqlite_utils.h" #include "lib/utils/bts_assert.h" +#include "lib/utils/bts_chrono.h" #include "lib/utils/bts_no_copy.h" #include "lib/utils/defer.h" #include "lib/utils/file_utils.h" -#include "lib/persistence/persistence_utils.h" #include #include @@ -15,10 +16,9 @@ namespace bts { namespace persistence { - struct SqliteStore::PImpl { PImpl(std::string const &store_root, std::string const &db_name) - : db(db_name), db_path(persistence::storage_location(db,store_root)), + : db(db_name), db_path(persistence::storage_location(db, store_root)), conn(nullptr, sqlite3_close), insert(nullptr, sqlite3_finalize), remove(nullptr, sqlite3_finalize), remove_all(nullptr, sqlite3_finalize), @@ -155,7 +155,22 @@ void SqliteStore::remove(std::string const &ns, SQLITE_CHK(sqlite3_bind_text(me->remove.get(), 2, k.c_str(), k.size(), SQLITE_TRANSIENT), me->conn); - bts_assert(sqlite3_step(me->remove.get()) == SQLITE_DONE); + auto r = sqlite3_step(me->remove.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Could not delete row in store. Will retry once. error_code: {} " + "ns:{}, k: {}", + r, ns, k); + sleep_for(2s); + r = sqlite3_step(me->remove.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Failed a second time to delete row. Bailing out. error_code: " + "{}", + r); + bts_assert(r == SQLITE_DONE); + } + } SQLITE_CHK(sqlite3_reset(me->remove.get()), me->conn); } } @@ -165,7 +180,22 @@ void SqliteStore::remove_all(std::string const &ns) { SQLITE_CHK(sqlite3_bind_text(me->remove_all.get(), 1, ns.c_str(), ns.size(), SQLITE_TRANSIENT), me->conn); - bts_assert(sqlite3_step(me->remove_all.get()) == SQLITE_DONE); + auto r = sqlite3_step(me->remove_all.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Could not delete all rows in store. Will retry once. error_code: {} " + "ns:{}", + r, ns); + sleep_for(2s); + r = sqlite3_step(me->remove_all.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Failed a second time to delete all rows. Bailing out. error_code: " + "{}", + r); + bts_assert(r == SQLITE_DONE); + } + } SQLITE_CHK(sqlite3_reset(me->remove_all.get()), me->conn); } @@ -181,7 +211,20 @@ void SqliteStore::insert(std::string const &ns, std::string const &k, SQLITE_TRANSIENT), me->conn); auto r = sqlite3_step(me->insert.get()); - bts_assert(r == SQLITE_DONE); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Could not insert row in store. Will retry once. error_code: {} ns: " + "{}, k: {}, v: {}", + r, ns, k, v); + sleep_for(2s); + r = sqlite3_step(me->insert.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Failed a second time to insert row. Bailing out. error_code: {}", + r); + bts_assert(r == SQLITE_DONE); + } + } SQLITE_CHK(sqlite3_reset(me->insert.get()), me->conn); } @@ -197,7 +240,20 @@ void SqliteStore::update(std::string const &ns, std::string const &k, SQLITE_TRANSIENT), me->conn); auto r = sqlite3_step(me->update.get()); - bts_assert(r == SQLITE_DONE); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Could not update row in store. Will retry once. error_code: {} ns: " + "{}, k: {}, v: {}", + r, ns, k, v); + sleep_for(2s); + r = sqlite3_step(me->update.get()); + if (r != SQLITE_DONE) { + LOG(persist, Error, + "Failed a second time to update row. Bailing out. error_code: {}", + r); + bts_assert(r == SQLITE_DONE); + } + } SQLITE_CHK(sqlite3_reset(me->update.get()), me->conn); }