From 6cc03e3e9d1809c77d22d86ae93ba5414efc874e Mon Sep 17 00:00:00 2001 From: marta-lokhova Date: Sun, 3 Nov 2024 20:49:32 -0800 Subject: [PATCH] Adapt the rest of the codebase to the new MISC database, which can be written to independently of ledger state database --- src/database/test/DatabaseTests.cpp | 15 ++- src/herder/HerderImpl.cpp | 8 +- src/herder/HerderPersistence.h | 1 - src/herder/HerderPersistenceImpl.cpp | 76 ++++++------ src/herder/PendingEnvelopes.cpp | 5 +- src/herder/Upgrades.cpp | 20 ++-- src/herder/test/HerderTests.cpp | 11 +- src/history/HistoryManagerImpl.cpp | 17 +-- src/history/StateSnapshot.cpp | 9 +- .../BucketListIsConsistentWithDatabase.cpp | 4 +- src/ledger/LedgerHeaderUtils.cpp | 36 +++--- src/ledger/LedgerHeaderUtils.h | 3 +- src/ledger/LedgerManagerImpl.cpp | 31 +++-- src/main/Application.cpp | 8 +- src/main/ApplicationImpl.cpp | 8 +- src/main/ApplicationUtils.cpp | 4 +- src/main/ExternalQueue.cpp | 27 +++-- src/main/PersistentState.cpp | 113 ++++++++++++------ src/main/PersistentState.h | 35 ++++-- src/overlay/BanManagerImpl.cpp | 21 ++-- src/overlay/PeerManager.cpp | 26 ++-- src/overlay/test/OverlayManagerTests.cpp | 4 +- src/transactions/TransactionSQL.cpp | 7 +- 23 files changed, 287 insertions(+), 202 deletions(-) diff --git a/src/database/test/DatabaseTests.cpp b/src/database/test/DatabaseTests.cpp index 4a17cd565c..2e483cf8cd 100644 --- a/src/database/test/DatabaseTests.cpp +++ b/src/database/test/DatabaseTests.cpp @@ -32,7 +32,7 @@ transactionTest(Application::pointer app) int a0 = a + 1; int a1 = a + 2; - auto& session = app->getDatabase().getSession(); + auto& session = app->getDatabase().getRawSession(); session << "DROP TABLE IF EXISTS test"; session << "CREATE TABLE test (x INTEGER)"; @@ -104,7 +104,7 @@ checkMVCCIsolation(Application::pointer app) int s2r1 = 0, s2r2 = 0, s2r3 = 0, s2r4 = 0; - auto& sess1 = app->getDatabase().getSession(); + auto& sess1 = app->getDatabase().getRawSession(); sess1 << "DROP TABLE IF EXISTS test"; sess1 << "CREATE TABLE test (x INTEGER)"; @@ -217,7 +217,7 @@ TEST_CASE("postgres smoketest", "[db]") Application::pointer app = createTestApplication(clock, cfg); int a = 10, b = 0; - auto& session = app->getDatabase().getSession(); + auto& session = app->getDatabase().getRawSession(); SECTION("round trip") { @@ -249,7 +249,7 @@ TEST_CASE("postgres smoketest", "[db]") SECTION("postgres MVCC test") { - app->getDatabase().getSession() << "drop table if exists test"; + app->getDatabase().getRawSession() << "drop table if exists test"; checkMVCCIsolation(app); } } @@ -279,7 +279,7 @@ TEST_CASE("postgres performance", "[db][pgperf][!hide]") try { Application::pointer app = createTestApplication(clock, cfg); - auto& session = app->getDatabase().getSession(); + auto& session = app->getDatabase().getRawSession(); session << "drop table if exists txtest;"; session << "create table txtest (a bigint, b bigint, c bigint, primary " @@ -355,7 +355,6 @@ TEST_CASE("schema test", "[db]") Application::pointer app = createTestApplication(clock, cfg); auto& db = app->getDatabase(); - auto dbv = db.getDBSchemaVersion(); - auto av = db.getAppSchemaVersion(); - REQUIRE(dbv == av); + auto dbv = db.getMainDBSchemaVersion(); + REQUIRE(dbv == MAIN_SCHEMA_VERSION); } diff --git a/src/herder/HerderImpl.cpp b/src/herder/HerderImpl.cpp index d2d3565c1a..e29f5fa2f8 100644 --- a/src/herder/HerderImpl.cpp +++ b/src/herder/HerderImpl.cpp @@ -2057,16 +2057,18 @@ void HerderImpl::persistUpgrades() { ZoneScoped; + releaseAssert(threadIsMain()); auto s = mUpgrades.getParameters().toJson(); - mApp.getPersistentState().setState(PersistentState::kLedgerUpgrades, s); + mApp.getPersistentState().setState(PersistentState::kLedgerUpgrades, s, + mApp.getDatabase().getMiscSession()); } void HerderImpl::restoreUpgrades() { ZoneScoped; - std::string s = - mApp.getPersistentState().getState(PersistentState::kLedgerUpgrades); + std::string s = mApp.getPersistentState().getState( + PersistentState::kLedgerUpgrades, mApp.getDatabase().getMiscSession()); if (!s.empty()) { Upgrades::UpgradeParameters p; diff --git a/src/herder/HerderPersistence.h b/src/herder/HerderPersistence.h index e5d44b0cc5..ea82cd747b 100644 --- a/src/herder/HerderPersistence.h +++ b/src/herder/HerderPersistence.h @@ -49,6 +49,5 @@ class HerderPersistence static void dropAll(Database& db); static void deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count); - static void createQuorumTrackingTable(soci::session& sess); }; } diff --git a/src/herder/HerderPersistenceImpl.cpp b/src/herder/HerderPersistenceImpl.cpp index bd743e8072..dbc1415a9b 100644 --- a/src/herder/HerderPersistenceImpl.cpp +++ b/src/herder/HerderPersistenceImpl.cpp @@ -47,12 +47,13 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, auto usedQSets = UnorderedMap{}; auto& db = mApp.getDatabase(); + auto& sess = db.getMiscSession(); - soci::transaction txscope(db.getSession()); + soci::transaction txscope(sess.session()); { auto prepClean = db.getPreparedStatement( - "DELETE FROM scphistory WHERE ledgerseq =:l"); + "DELETE FROM scphistory WHERE ledgerseq =:l", sess); auto& st = prepClean.statement(); st.exchange(soci::use(seq)); @@ -92,7 +93,8 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, auto prepEnv = db.getPreparedStatement("INSERT INTO scphistory " "(nodeid, ledgerseq, envelope) VALUES " - "(:n, :l, :e)"); + "(:n, :l, :e)", + sess); auto& st = prepEnv.statement(); st.exchange(soci::use(nodeIDs, "n")); st.exchange(soci::use(seqs, "l")); @@ -124,7 +126,7 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, std::string qSetHHex(binToHex(qSetH)); auto prep = db.getPreparedStatement( - "UPDATE quoruminfo SET qsethash = :h WHERE nodeid = :id"); + "UPDATE quoruminfo SET qsethash = :h WHERE nodeid = :id", sess); auto& st = prep.statement(); st.exchange(soci::use(qSetHHex)); st.exchange(soci::use(nodeIDStrKey)); @@ -136,7 +138,8 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, if (st.get_affected_rows() != 1) { auto prepI = db.getPreparedStatement( - "INSERT INTO quoruminfo (nodeid, qsethash) VALUES (:id, :h)"); + "INSERT INTO quoruminfo (nodeid, qsethash) VALUES (:id, :h)", + sess); auto& stI = prepI.statement(); stI.exchange(soci::use(nodeIDStrKey)); stI.exchange(soci::use(qSetHHex)); @@ -158,7 +161,7 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, uint32_t lastSeenSeq; auto prepSelQSet = db.getPreparedStatement( - "SELECT lastledgerseq FROM scpquorums WHERE qsethash = :h"); + "SELECT lastledgerseq FROM scpquorums WHERE qsethash = :h", sess); auto& stSel = prepSelQSet.statement(); stSel.exchange(soci::into(lastSeenSeq)); stSel.exchange(soci::use(qSetH)); @@ -177,7 +180,8 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, auto prepUpQSet = db.getPreparedStatement( "UPDATE scpquorums SET " - "lastledgerseq = :l WHERE qsethash = :h"); + "lastledgerseq = :l WHERE qsethash = :h", + sess); auto& stUp = prepUpQSet.statement(); stUp.exchange(soci::use(seq)); @@ -202,7 +206,8 @@ HerderPersistenceImpl::saveSCPHistory(uint32_t seq, auto prepInsQSet = db.getPreparedStatement( "INSERT INTO scpquorums " "(qsethash, lastledgerseq, qset) VALUES " - "(:h, :l, :v);"); + "(:h, :l, :v);", + sess); auto& stIns = prepInsQSet.statement(); stIns.exchange(soci::use(qSetH)); @@ -372,38 +377,35 @@ void HerderPersistence::dropAll(Database& db) { ZoneScoped; - db.getSession() << "DROP TABLE IF EXISTS scphistory"; + db.getRawMiscSession() << "DROP TABLE IF EXISTS scphistory"; - db.getSession() << "DROP TABLE IF EXISTS scpquorums"; + db.getRawMiscSession() << "DROP TABLE IF EXISTS scpquorums"; - db.getSession() << "CREATE TABLE scphistory (" - "nodeid CHARACTER(56) NOT NULL," - "ledgerseq INT NOT NULL CHECK (ledgerseq >= 0)," - "envelope TEXT NOT NULL" - ")"; + db.getRawMiscSession() << "CREATE TABLE scphistory (" + "nodeid CHARACTER(56) NOT NULL," + "ledgerseq INT NOT NULL CHECK (ledgerseq >= 0)," + "envelope TEXT NOT NULL" + ")"; - db.getSession() << "CREATE INDEX scpenvsbyseq ON scphistory(ledgerseq)"; + db.getRawMiscSession() + << "CREATE INDEX scpenvsbyseq ON scphistory(ledgerseq)"; - db.getSession() << "CREATE TABLE scpquorums (" - "qsethash CHARACTER(64) NOT NULL," - "lastledgerseq INT NOT NULL CHECK (lastledgerseq >= 0)," - "qset TEXT NOT NULL," - "PRIMARY KEY (qsethash)" - ")"; + db.getRawMiscSession() + << "CREATE TABLE scpquorums (" + "qsethash CHARACTER(64) NOT NULL," + "lastledgerseq INT NOT NULL CHECK (lastledgerseq >= 0)," + "qset TEXT NOT NULL," + "PRIMARY KEY (qsethash)" + ")"; - db.getSession() + db.getRawMiscSession() << "CREATE INDEX scpquorumsbyseq ON scpquorums(lastledgerseq)"; - db.getSession() << "DROP TABLE IF EXISTS quoruminfo"; -} - -void -HerderPersistence::createQuorumTrackingTable(soci::session& sess) -{ - sess << "CREATE TABLE quoruminfo (" - "nodeid CHARACTER(56) NOT NULL," - "qsethash CHARACTER(64) NOT NULL," - "PRIMARY KEY (nodeid))"; + db.getRawMiscSession() << "DROP TABLE IF EXISTS quoruminfo"; + db.getRawMiscSession() << "CREATE TABLE quoruminfo (" + "nodeid CHARACTER(56) NOT NULL," + "qsethash CHARACTER(64) NOT NULL," + "PRIMARY KEY (nodeid))"; } void @@ -411,9 +413,9 @@ HerderPersistence::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { ZoneScoped; - DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, - "scphistory", "ledgerseq"); - DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, - "scpquorums", "lastledgerseq"); + DatabaseUtils::deleteOldEntriesHelper(db.getRawMiscSession(), ledgerSeq, + count, "scphistory", "ledgerseq"); + DatabaseUtils::deleteOldEntriesHelper(db.getRawMiscSession(), ledgerSeq, + count, "scpquorums", "lastledgerseq"); } } diff --git a/src/herder/PendingEnvelopes.cpp b/src/herder/PendingEnvelopes.cpp index 4db92c5619..3bd00a3a02 100644 --- a/src/herder/PendingEnvelopes.cpp +++ b/src/herder/PendingEnvelopes.cpp @@ -745,7 +745,8 @@ PendingEnvelopes::getQSet(Hash const& hash) else { auto& db = mApp.getDatabase(); - qset = HerderPersistence::getQuorumSet(db, db.getSession(), hash); + qset = + HerderPersistence::getQuorumSet(db, db.getRawMiscSession(), hash); } if (qset) { @@ -814,7 +815,7 @@ PendingEnvelopes::rebuildQuorumTrackerState() // see if we had some information for that node auto& db = mApp.getDatabase(); auto h = HerderPersistence::getNodeQuorumSet( - db, db.getSession(), id); + db, db.getRawMiscSession(), id); if (h) { res = getQSet(*h); diff --git a/src/herder/Upgrades.cpp b/src/herder/Upgrades.cpp index 689b7d0139..a24fbac2ed 100644 --- a/src/herder/Upgrades.cpp +++ b/src/herder/Upgrades.cpp @@ -690,22 +690,22 @@ Upgrades::timeForUpgrade(uint64_t time) const void Upgrades::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS upgradehistory"; - db.getSession() << "CREATE TABLE upgradehistory (" - "ledgerseq INT NOT NULL CHECK (ledgerseq >= 0), " - "upgradeindex INT NOT NULL, " - "upgrade TEXT NOT NULL, " - "changes TEXT NOT NULL, " - "PRIMARY KEY (ledgerseq, upgradeindex)" - ")"; - db.getSession() + db.getRawSession() << "DROP TABLE IF EXISTS upgradehistory"; + db.getRawSession() << "CREATE TABLE upgradehistory (" + "ledgerseq INT NOT NULL CHECK (ledgerseq >= 0), " + "upgradeindex INT NOT NULL, " + "upgrade TEXT NOT NULL, " + "changes TEXT NOT NULL, " + "PRIMARY KEY (ledgerseq, upgradeindex)" + ")"; + db.getRawSession() << "CREATE INDEX upgradehistbyseq ON upgradehistory (ledgerseq);"; } void Upgrades::dropSupportUpgradeHistory(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS upgradehistory"; + db.getRawSession() << "DROP TABLE IF EXISTS upgradehistory"; } static void diff --git a/src/herder/test/HerderTests.cpp b/src/herder/test/HerderTests.cpp index 9366adb7c3..1fd964b319 100644 --- a/src/herder/test/HerderTests.cpp +++ b/src/herder/test/HerderTests.cpp @@ -226,7 +226,7 @@ TEST_CASE_VERSIONS("standalone", "[herder][acceptance]") app->getCommandHandler().manualCmd("setcursor?id=A1&cursor=1"); app->getCommandHandler().manualCmd("maintenance?queue=true"); auto& db = app->getDatabase(); - auto& sess = db.getSession(); + auto& sess = db.getRawSession(); app->getCommandHandler().manualCmd("setcursor?id=A2&cursor=3"); app->getCommandHandler().manualCmd("maintenance?queue=true"); @@ -3395,15 +3395,15 @@ TEST_CASE("overlay parallel processing") // soroban traffic currLoadGenCount = loadGenDone.count(); auto secondLoadGenCount = secondLoadGenDone.count(); - uint32_t const classicTxCount = 200; + uint32_t const txCount = 100; // Generate Soroban txs from one node loadGen.generateLoad(GeneratedLoadConfig::txLoad( LoadGenMode::SOROBAN_UPLOAD, 50, - /* nTxs */ 500, desiredTxRate, /* offset */ 0)); + /* nTxs */ txCount, desiredTxRate, /* offset */ 0)); // Generate classic txs from another node (with offset to prevent // overlapping accounts) secondLoadGen.generateLoad(GeneratedLoadConfig::txLoad( - LoadGenMode::PAY, 50, classicTxCount, desiredTxRate, + LoadGenMode::PAY, 50, txCount, desiredTxRate, /* offset */ 50)); simulation->crankUntil( @@ -5465,7 +5465,8 @@ TEST_CASE("SCP message capture from previous ledger", "[herder]") // Prepare query auto& db = node->getDatabase(); auto prep = db.getPreparedStatement( - "SELECT envelope FROM scphistory WHERE ledgerseq = :l"); + "SELECT envelope FROM scphistory WHERE ledgerseq = :l", + db.getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(ledgerNum)); std::string envStr; diff --git a/src/history/HistoryManagerImpl.cpp b/src/history/HistoryManagerImpl.cpp index f363cbb090..5389326fea 100644 --- a/src/history/HistoryManagerImpl.cpp +++ b/src/history/HistoryManagerImpl.cpp @@ -59,8 +59,8 @@ static std::string kSQLCreateStatement = void HistoryManager::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS publishqueue;"; - soci::statement st = db.getSession().prepare << kSQLCreateStatement; + db.getRawSession() << "DROP TABLE IF EXISTS publishqueue;"; + soci::statement st = db.getRawSession().prepare << kSQLCreateStatement; st.execute(true); } @@ -149,7 +149,8 @@ HistoryManagerImpl::dropSQLBasedPublish() // Migrate all the existing queued checkpoints to the new format { std::string state; - auto prep = db.getPreparedStatement("SELECT state FROM publishqueue;"); + auto prep = + db.getPreparedStatement("SELECT state FROM publishqueue;", sess); auto& st = prep.statement(); st.exchange(soci::into(state)); st.define_and_bind(); @@ -170,9 +171,9 @@ HistoryManagerImpl::dropSQLBasedPublish() for (auto const& checkpoint : checkpointLedgers) { auto begin = firstLedgerInCheckpointContaining(checkpoint); - populateCheckpointFilesFromDB(mApp, sess, begin, freq, + populateCheckpointFilesFromDB(mApp, sess.session(), begin, freq, mCheckpointBuilder); - LedgerHeaderUtils::copyToStream(db, sess, begin, freq, + LedgerHeaderUtils::copyToStream(db, sess.session(), begin, freq, mCheckpointBuilder); // Checkpoints in publish queue are complete, so we can finalize them mCheckpointBuilder.checkpointComplete(checkpoint); @@ -184,17 +185,17 @@ HistoryManagerImpl::dropSQLBasedPublish() { // Then, reconstruct any partial checkpoints that haven't yet been // queued - populateCheckpointFilesFromDB(mApp, sess, + populateCheckpointFilesFromDB(mApp, sess.session(), firstLedgerInCheckpointContaining(lcl), freq, mCheckpointBuilder); - LedgerHeaderUtils::copyToStream(db, sess, + LedgerHeaderUtils::copyToStream(db, sess.session(), firstLedgerInCheckpointContaining(lcl), freq, mCheckpointBuilder); } db.clearPreparedStatementCache(); // Now it's safe to drop obsolete SQL tables - sess << "DROP TABLE IF EXISTS publishqueue;"; + sess.session() << "DROP TABLE IF EXISTS publishqueue;"; dropSupportTxHistory(db); dropSupportTxSetHistory(db); } diff --git a/src/history/StateSnapshot.cpp b/src/history/StateSnapshot.cpp index 3995aeea1b..2dfd293d4e 100644 --- a/src/history/StateSnapshot.cpp +++ b/src/history/StateSnapshot.cpp @@ -55,10 +55,11 @@ StateSnapshot::writeSCPMessages() const { ZoneScoped; std::unique_ptr snapSess( - mApp.getDatabase().canUsePool() - ? std::make_unique(mApp.getDatabase().getPool()) - : nullptr); - soci::session& sess(snapSess ? *snapSess : mApp.getDatabase().getSession()); + (mApp.getDatabase().canUsePool() + ? std::make_unique(mApp.getDatabase().getMiscPool()) + : nullptr)); + soci::session& sess(snapSess ? *snapSess + : mApp.getDatabase().getRawMiscSession()); soci::transaction tx(sess); // The current "history block" is stored in _four_ files, one just ledger diff --git a/src/invariant/BucketListIsConsistentWithDatabase.cpp b/src/invariant/BucketListIsConsistentWithDatabase.cpp index e12da7b724..2e4e5d9b8c 100644 --- a/src/invariant/BucketListIsConsistentWithDatabase.cpp +++ b/src/invariant/BucketListIsConsistentWithDatabase.cpp @@ -8,6 +8,7 @@ #include "bucket/BucketList.h" #include "bucket/BucketManager.h" #include "crypto/Hex.h" +#include "database/Database.h" #include "history/HistoryArchive.h" #include "invariant/InvariantManager.h" #include "ledger/LedgerManager.h" @@ -251,7 +252,8 @@ BucketListIsConsistentWithDatabase::checkEntireBucketlist() } if (mApp.getConfig().isUsingBucketListDB() && - mApp.getPersistentState().getState(PersistentState::kDBBackend) != + mApp.getPersistentState().getState(PersistentState::kDBBackend, + mApp.getDatabase().getSession()) != BucketIndex::DB_BACKEND_STATE) { throw std::runtime_error("BucketListDB enabled but BucketListDB flag " diff --git a/src/ledger/LedgerHeaderUtils.cpp b/src/ledger/LedgerHeaderUtils.cpp index 0835439355..41eca7352e 100644 --- a/src/ledger/LedgerHeaderUtils.cpp +++ b/src/ledger/LedgerHeaderUtils.cpp @@ -43,7 +43,7 @@ isValid(LedgerHeader const& lh) } void -storeInDatabase(Database& db, LedgerHeader const& header) +storeInDatabase(Database& db, LedgerHeader const& header, SessionWrapper& sess) { ZoneScoped; if (!isValid(header)) @@ -64,7 +64,8 @@ storeInDatabase(Database& db, LedgerHeader const& header) "INSERT INTO ledgerheaders " "(ledgerhash, prevhash, bucketlisthash, ledgerseq, closetime, data) " "VALUES " - "(:h, :ph, :blh, :seq, :ct, :data)"); + "(:h, :ph, :blh, :seq, :ct, :data)", + sess); auto& st = prep.statement(); st.exchange(soci::use(hash)); st.exchange(soci::use(prevHash)); @@ -112,7 +113,8 @@ loadByHash(Database& db, Hash const& hash) std::string headerEncoded; auto prep = db.getPreparedStatement("SELECT data FROM ledgerheaders " - "WHERE ledgerhash = :h"); + "WHERE ledgerhash = :h", + db.getSession()); auto& st = prep.statement(); st.exchange(soci::into(headerEncoded)); st.exchange(soci::use(hash_s)); @@ -144,8 +146,8 @@ loadMaxLedgerSeq(Database& db) ZoneScoped; uint32_t seq = 0; soci::indicator maxIndicator; - auto prep = - db.getPreparedStatement("SELECT MAX(ledgerseq) FROM ledgerheaders"); + auto prep = db.getPreparedStatement( + "SELECT MAX(ledgerseq) FROM ledgerheaders", db.getSession()); auto& st = prep.statement(); st.exchange(soci::into(seq, maxIndicator)); st.define_and_bind(); @@ -191,7 +193,7 @@ void deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { ZoneScoped; - DatabaseUtils::deleteOldEntriesHelper(db.getSession(), ledgerSeq, count, + DatabaseUtils::deleteOldEntriesHelper(db.getRawSession(), ledgerSeq, count, "ledgerheaders", "ledgerseq"); } @@ -232,17 +234,17 @@ dropAll(Database& db) { std::string coll = db.getSimpleCollationClause(); - db.getSession() << "DROP TABLE IF EXISTS ledgerheaders;"; - db.getSession() << "CREATE TABLE ledgerheaders (" - << "ledgerhash CHARACTER(64) " << coll - << " PRIMARY KEY," - << "prevhash CHARACTER(64) NOT NULL," - "bucketlisthash CHARACTER(64) NOT NULL," - "ledgerseq INT UNIQUE CHECK (ledgerseq >= 0)," - "closetime BIGINT NOT NULL CHECK (closetime >= 0)," - "data TEXT NOT NULL" - ");"; - db.getSession() + db.getRawSession() << "DROP TABLE IF EXISTS ledgerheaders;"; + db.getRawSession() + << "CREATE TABLE ledgerheaders (" + << "ledgerhash CHARACTER(64) " << coll << " PRIMARY KEY," + << "prevhash CHARACTER(64) NOT NULL," + "bucketlisthash CHARACTER(64) NOT NULL," + "ledgerseq INT UNIQUE CHECK (ledgerseq >= 0)," + "closetime BIGINT NOT NULL CHECK (closetime >= 0)," + "data TEXT NOT NULL" + ");"; + db.getRawSession() << "CREATE INDEX ledgersbyseq ON ledgerheaders ( ledgerseq );"; } } diff --git a/src/ledger/LedgerHeaderUtils.h b/src/ledger/LedgerHeaderUtils.h index e165570e1a..d67bed277d 100644 --- a/src/ledger/LedgerHeaderUtils.h +++ b/src/ledger/LedgerHeaderUtils.h @@ -18,7 +18,8 @@ uint32_t getFlags(LedgerHeader const& lh); bool isValid(LedgerHeader const& lh); -void storeInDatabase(Database& db, LedgerHeader const& header); +void storeInDatabase(Database& db, LedgerHeader const& header, + SessionWrapper& sess); LedgerHeader decodeFromData(std::string const& data); diff --git a/src/ledger/LedgerManagerImpl.cpp b/src/ledger/LedgerManagerImpl.cpp index 8e7c5841b2..9b67b95d42 100644 --- a/src/ledger/LedgerManagerImpl.cpp +++ b/src/ledger/LedgerManagerImpl.cpp @@ -290,8 +290,8 @@ LedgerManagerImpl::loadLastKnownLedger(bool restoreBucketlist, ZoneScoped; // Step 1. Load LCL state from the DB and extract latest ledger hash - string lastLedger = - mApp.getPersistentState().getState(PersistentState::kLastClosedLedger); + string lastLedger = mApp.getPersistentState().getState( + PersistentState::kLastClosedLedger, mApp.getDatabase().getSession()); if (lastLedger.empty()) { @@ -434,12 +434,16 @@ LedgerManagerImpl::setupInMemoryStateRebuild() LedgerHeader lh; HistoryArchiveState has; auto& ps = mApp.getPersistentState(); - ps.setState(PersistentState::kLastClosedLedger, - binToHex(xdrSha256(lh))); - ps.setState(PersistentState::kHistoryArchiveState, has.toString()); - ps.setState(PersistentState::kLastSCPData, ""); - ps.setState(PersistentState::kLastSCPDataXDR, ""); - ps.setState(PersistentState::kLedgerUpgrades, ""); + ps.setState(PersistentState::kLastClosedLedger, binToHex(xdrSha256(lh)), + getDatabase().getSession()); + ps.setState(PersistentState::kHistoryArchiveState, has.toString(), + getDatabase().getSession()); + ps.setState(PersistentState::kLastSCPData, "", + getDatabase().getMiscSession()); + ps.setState(PersistentState::kLastSCPDataXDR, "", + getDatabase().getMiscSession()); + ps.setState(PersistentState::kLedgerUpgrades, "", + getDatabase().getMiscSession()); mRebuildInMemoryState = true; } } @@ -541,7 +545,7 @@ LedgerManagerImpl::getLastClosedLedgerHAS() ZoneScoped; string hasString = mApp.getPersistentState().getState( - PersistentState::kHistoryArchiveState); + PersistentState::kHistoryArchiveState, mApp.getDatabase().getSession()); HistoryArchiveState has; has.fromString(hasString); return has; @@ -1106,7 +1110,7 @@ LedgerManagerImpl::deleteOldEntries(Database& db, uint32_t ledgerSeq, uint32_t count) { ZoneScoped; - soci::transaction txscope(db.getSession()); + soci::transaction txscope(db.getRawSession()); db.clearPreparedStatementCache(); LedgerHeaderUtils::deleteOldEntries(db, ledgerSeq, count); HerderPersistence::deleteOldEntries(db, ledgerSeq, count); @@ -1621,8 +1625,9 @@ LedgerManagerImpl::storeCurrentLedger(LedgerHeader const& header, Hash hash = xdrSha256(header); releaseAssert(!isZero(hash)); + auto& sess = mApp.getLedgerTxnRoot().getSession(); mApp.getPersistentState().setState(PersistentState::kLastClosedLedger, - binToHex(hash)); + binToHex(hash), sess); BucketList bl; if (mApp.getConfig().MODE_ENABLES_BUCKETLIST) @@ -1635,11 +1640,11 @@ LedgerManagerImpl::storeCurrentLedger(LedgerHeader const& header, mApp.getConfig().NETWORK_PASSPHRASE); mApp.getPersistentState().setState(PersistentState::kHistoryArchiveState, - has.toString()); + has.toString(), sess); if (mApp.getConfig().MODE_STORES_HISTORY_LEDGERHEADERS && storeHeader) { - LedgerHeaderUtils::storeInDatabase(mApp.getDatabase(), header); + LedgerHeaderUtils::storeInDatabase(mApp.getDatabase(), header, sess); if (appendToCheckpoint) { mApp.getHistoryManager().appendLedgerHeader(header); diff --git a/src/main/Application.cpp b/src/main/Application.cpp index 991c11f48a..8ff31b3b4f 100644 --- a/src/main/Application.cpp +++ b/src/main/Application.cpp @@ -4,6 +4,7 @@ #include "Application.h" #include "ApplicationImpl.h" +#include "database/Database.h" #include "util/GlobalChecks.h" #include @@ -21,12 +22,13 @@ validateNetworkPassphrase(Application::pointer app) } auto& persistentState = app->getPersistentState(); - std::string prevNetworkPassphrase = - persistentState.getState(PersistentState::kNetworkPassphrase); + std::string prevNetworkPassphrase = persistentState.getState( + PersistentState::kNetworkPassphrase, app->getDatabase().getSession()); if (prevNetworkPassphrase.empty()) { persistentState.setState(PersistentState::kNetworkPassphrase, - networkPassphrase); + networkPassphrase, + app->getDatabase().getSession()); } else if (networkPassphrase != prevNetworkPassphrase) { diff --git a/src/main/ApplicationImpl.cpp b/src/main/ApplicationImpl.cpp index 639cdb50d8..10617f51d3 100644 --- a/src/main/ApplicationImpl.cpp +++ b/src/main/ApplicationImpl.cpp @@ -214,7 +214,7 @@ maybeRebuildLedger(Application& app, bool applyBuckets) if (!app.getConfig().MODE_USES_IN_MEMORY_LEDGER) { app.getDatabase().clearPreparedStatementCache(); - soci::transaction tx(app.getDatabase().getSession()); + soci::transaction tx(app.getDatabase().getRawSession()); auto loopEntries = [&](auto const& entryTypeSet, bool shouldRebuild) { for (auto let : entryTypeSet) @@ -772,7 +772,8 @@ ApplicationImpl::validateAndLogConfig() if (mConfig.DEPRECATED_SQL_LEDGER_STATE) { - if (mPersistentState->getState(PersistentState::kDBBackend) == + if (mPersistentState->getState(PersistentState::kDBBackend, + getDatabase().getSession()) == BucketIndex::DB_BACKEND_STATE) { throw std::invalid_argument( @@ -790,7 +791,8 @@ ApplicationImpl::validateAndLogConfig() if (mConfig.isUsingBucketListDB()) { mPersistentState->setState(PersistentState::kDBBackend, - BucketIndex::DB_BACKEND_STATE); + BucketIndex::DB_BACKEND_STATE, + getDatabase().getSession()); auto pageSizeExp = mConfig.BUCKETLIST_DB_INDEX_PAGE_SIZE_EXPONENT; if (pageSizeExp != 0) { diff --git a/src/main/ApplicationUtils.cpp b/src/main/ApplicationUtils.cpp index 5f33fce2a5..376012804f 100644 --- a/src/main/ApplicationUtils.cpp +++ b/src/main/ApplicationUtils.cpp @@ -318,8 +318,8 @@ applyBucketsForLCL(Application& app, std::function onlyApply) { auto has = app.getLedgerManager().getLastClosedLedgerHAS(); - auto lclHash = - app.getPersistentState().getState(PersistentState::kLastClosedLedger); + auto lclHash = app.getPersistentState().getState( + PersistentState::kLastClosedLedger, app.getDatabase().getSession()); auto maxProtocolVersion = app.getConfig().LEDGER_PROTOCOL_VERSION; auto currentLedger = diff --git a/src/main/ExternalQueue.cpp b/src/main/ExternalQueue.cpp index 61578971cb..511b38f95a 100644 --- a/src/main/ExternalQueue.cpp +++ b/src/main/ExternalQueue.cpp @@ -31,9 +31,9 @@ ExternalQueue::ExternalQueue(Application& app) : mApp(app) void ExternalQueue::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS pubsub;"; + db.getRawMiscSession() << "DROP TABLE IF EXISTS pubsub;"; - soci::statement st = db.getSession().prepare << kSQLCreateStatement; + soci::statement st = db.getRawMiscSession().prepare << kSQLCreateStatement; st.execute(true); } @@ -74,7 +74,8 @@ ExternalQueue::setCursorForResource(std::string const& resid, uint32 cursor) { ZoneNamedN(insertPubsubZone, "insert pubsub", true); auto prep = mApp.getDatabase().getPreparedStatement( - "INSERT INTO pubsub (resid, lastread) VALUES (:n, :v);"); + "INSERT INTO pubsub (resid, lastread) VALUES (:n, :v);", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(resid)); st.exchange(soci::use(cursor)); @@ -88,7 +89,8 @@ ExternalQueue::setCursorForResource(std::string const& resid, uint32 cursor) else { auto prep = mApp.getDatabase().getPreparedStatement( - "UPDATE pubsub SET lastread = :v WHERE resid = :n;"); + "UPDATE pubsub SET lastread = :v WHERE resid = :n;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(cursor)); @@ -114,7 +116,8 @@ ExternalQueue::getCursorForResource(std::string const& resid, auto& db = mApp.getDatabase(); auto prep = - db.getPreparedStatement("SELECT resid, lastread FROM pubsub;"); + db.getPreparedStatement("SELECT resid, lastread FROM pubsub;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::into(n)); st.exchange(soci::into(v)); @@ -151,7 +154,8 @@ ExternalQueue::deleteCursor(std::string const& resid) { ZoneNamedN(deletePubsubZone, "delete pubsub", true); auto prep = mApp.getDatabase().getPreparedStatement( - "DELETE FROM pubsub WHERE resid = :n;"); + "DELETE FROM pubsub WHERE resid = :n;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(resid)); st.define_and_bind(); @@ -166,9 +170,9 @@ ExternalQueue::deleteOldEntries(uint32 count) auto& db = mApp.getDatabase(); int m; soci::indicator minIndicator; - soci::statement st = - (db.getSession().prepare << "SELECT MIN(lastread) FROM pubsub", - soci::into(m, minIndicator)); + soci::statement st = (db.getMiscSession().session().prepare + << "SELECT MIN(lastread) FROM pubsub", + soci::into(m, minIndicator)); { ZoneNamedN(selectPubsubZone, "select pubsub", true); st.execute(true); @@ -224,8 +228,9 @@ ExternalQueue::getCursor(std::string const& resid) std::string res; auto& db = mApp.getDatabase(); - auto prep = db.getPreparedStatement( - "SELECT lastread FROM pubsub WHERE resid = :n;"); + auto prep = + db.getPreparedStatement("SELECT lastread FROM pubsub WHERE resid = :n;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::into(res)); st.exchange(soci::use(resid)); diff --git a/src/main/PersistentState.cpp b/src/main/PersistentState.cpp index 150d3f62ab..efe84acbf2 100644 --- a/src/main/PersistentState.cpp +++ b/src/main/PersistentState.cpp @@ -18,11 +18,13 @@ namespace stellar using namespace std; -std::string PersistentState::mapping[kLastEntry] = { - "lastclosedledger", "historyarchivestate", "lastscpdata", - "databaseschema", "networkpassphrase", "ledgerupgrades", - "rebuildledger", "lastscpdataxdr", "txset", - "dbbackend"}; +std::string PersistentState::mainMapping[kLastEntryMain] = { + "lastclosedledger", "historyarchivestate", "databaseschema", + "networkpassphrase", "rebuildledger", "dbbackend"}; + +std::string PersistentState::miscMapping[kLastEntry] = { + "lastscpdata", "ledgerupgrades", "lastscpdataxdr", "txset", + "miscdatabaseschema"}; std::string PersistentState::kSQLCreateStatement = "CREATE TABLE IF NOT EXISTS storestate (" @@ -37,12 +39,13 @@ PersistentState::PersistentState(Application& app) : mApp(app) void PersistentState::deleteTxSets(std::unordered_set hashesToDelete) { - soci::transaction tx(mApp.getDatabase().getSession()); + soci::transaction tx(mApp.getDatabase().getRawMiscSession()); for (auto const& hash : hashesToDelete) { auto name = getStoreStateNameForTxSet(hash); auto prep = mApp.getDatabase().getPreparedStatement( - "DELETE FROM storestate WHERE statename = :n;"); + "DELETE FROM storestate WHERE statename = :n;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(name)); @@ -55,20 +58,46 @@ PersistentState::deleteTxSets(std::unordered_set hashesToDelete) void PersistentState::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS storestate;"; + auto drop = [](SessionWrapper& session) { + session.session() << "DROP TABLE IF EXISTS storestate;"; + soci::statement st = session.session().prepare << kSQLCreateStatement; + st.execute(true); + }; + + drop(db.getSession()); + if (db.canUsePool() && db.isSqlite()) + { + drop(db.getMiscSession()); + } +} - soci::statement st = db.getSession().prepare << kSQLCreateStatement; +void +PersistentState::dropMisc(Database& db) +{ + db.getRawMiscSession() << "DROP TABLE IF EXISTS storestate;"; + soci::statement st = db.getRawMiscSession().prepare << kSQLCreateStatement; st.execute(true); } std::string -PersistentState::getStoreStateName(PersistentState::Entry n, uint32 subscript) +PersistentState::getStoreStateName(PersistentState::Entry n, + uint32 subscript) const { if (n < 0 || n >= kLastEntry) { throw out_of_range("unknown entry"); } - auto res = mapping[n]; + + std::string res; + if (n < kLastEntryMain) + { + res = mainMapping[n]; + } + else + { + res = miscMapping[n - kLastEntryMain - 1]; + } + if (((n == kLastSCPData || n == kLastSCPDataXDR) && subscript > 0) || n == kRebuildLedger) { @@ -80,7 +109,7 @@ PersistentState::getStoreStateName(PersistentState::Entry n, uint32 subscript) std::string PersistentState::getStoreStateNameForTxSet(Hash const& txSetHash) { - auto res = mapping[kTxSet]; + auto res = miscMapping[kTxSet - kLastEntryMain - 1]; res += binToHex(txSetHash); return res; } @@ -88,22 +117,23 @@ PersistentState::getStoreStateNameForTxSet(Hash const& txSetHash) bool PersistentState::hasTxSet(Hash const& txSetHash) { - return entryExists(getStoreStateNameForTxSet(txSetHash)); + return entryExists(getStoreStateNameForTxSet(txSetHash), + mApp.getDatabase().getMiscSession()); } std::string -PersistentState::getState(PersistentState::Entry entry) +PersistentState::getState(PersistentState::Entry entry, SessionWrapper& session) { ZoneScoped; - return getFromDb(getStoreStateName(entry)); + return getFromDb(getStoreStateName(entry), session); } void PersistentState::setState(PersistentState::Entry entry, - std::string const& value) + std::string const& value, SessionWrapper& session) { ZoneScoped; - updateDb(getStoreStateName(entry), value); + updateDb(getStoreStateName(entry), value, session); } std::vector @@ -114,7 +144,8 @@ PersistentState::getSCPStateAllSlots() std::vector states; for (uint32 i = 0; i <= mApp.getConfig().MAX_SLOTS_TO_REMEMBER; i++) { - auto val = getFromDb(getStoreStateName(kLastSCPDataXDR, i)); + auto val = getFromDb(getStoreStateName(kLastSCPDataXDR, i), + mApp.getDatabase().getMiscSession()); if (!val.empty()) { states.push_back(val); @@ -130,7 +161,8 @@ PersistentState::setSCPStateForSlot(uint64 slot, std::string const& value) ZoneScoped; auto slotIdx = static_cast( slot % (mApp.getConfig().MAX_SLOTS_TO_REMEMBER + 1)); - updateDb(getStoreStateName(kLastSCPDataXDR, slotIdx), value); + updateDb(getStoreStateName(kLastSCPDataXDR, slotIdx), value, + mApp.getDatabase().getMiscSession()); } void @@ -138,12 +170,13 @@ PersistentState::setSCPStateV1ForSlot( uint64 slot, std::string const& value, std::unordered_map const& txSets) { - soci::transaction tx(mApp.getDatabase().getSession()); + soci::transaction tx(mApp.getDatabase().getRawMiscSession()); setSCPStateForSlot(slot, value); for (auto const& txSet : txSets) { - updateDb(getStoreStateNameForTxSet(txSet.first), txSet.second); + updateDb(getStoreStateNameForTxSet(txSet.first), txSet.second, + mApp.getDatabase().getMiscSession()); } tx.commit(); } @@ -152,14 +185,17 @@ bool PersistentState::shouldRebuildForType(LedgerEntryType let) { ZoneScoped; - return !getFromDb(getStoreStateName(kRebuildLedger, let)).empty(); + return !getFromDb(getStoreStateName(kRebuildLedger, let), + mApp.getDatabase().getSession()) + .empty(); } void PersistentState::clearRebuildForType(LedgerEntryType let) { ZoneScoped; - updateDb(getStoreStateName(kRebuildLedger, let), ""); + updateDb(getStoreStateName(kRebuildLedger, let), "", + mApp.getDatabase().getSession()); } void @@ -174,15 +210,17 @@ PersistentState::setRebuildForType(LedgerEntryType let) return; } - updateDb(getStoreStateName(kRebuildLedger, let), "1"); + updateDb(getStoreStateName(kRebuildLedger, let), "1", + mApp.getDatabase().getSession()); } void -PersistentState::updateDb(std::string const& entry, std::string const& value) +PersistentState::updateDb(std::string const& entry, std::string const& value, + SessionWrapper& sess) { ZoneScoped; auto prep = mApp.getDatabase().getPreparedStatement( - "UPDATE storestate SET state = :v WHERE statename = :n;"); + "UPDATE storestate SET state = :v WHERE statename = :n;", sess); auto& st = prep.statement(); st.exchange(soci::use(value)); @@ -193,11 +231,11 @@ PersistentState::updateDb(std::string const& entry, std::string const& value) st.execute(true); } - if (st.get_affected_rows() != 1 && getFromDb(entry).empty()) + if (st.get_affected_rows() != 1 && getFromDb(entry, sess).empty()) { ZoneNamedN(insertStoreStateZone, "insert storestate", true); auto prep2 = mApp.getDatabase().getPreparedStatement( - "INSERT INTO storestate (statename, state) VALUES (:n, :v);"); + "INSERT INTO storestate (statename, state) VALUES (:n, :v);", sess); auto& st2 = prep2.statement(); st2.exchange(soci::use(entry)); st2.exchange(soci::use(value)); @@ -217,11 +255,11 @@ PersistentState::getTxSetsForAllSlots() std::vector result; std::string val; - std::string pattern = mapping[kTxSet] + "%"; + std::string pattern = miscMapping[kTxSet - kLastEntryMain - 1] + "%"; std::string statementStr = "SELECT state FROM storestate WHERE statename LIKE :n;"; auto& db = mApp.getDatabase(); - auto prep = db.getPreparedStatement(statementStr); + auto prep = db.getPreparedStatement(statementStr, db.getMiscSession()); auto& st = prep.statement(); st.exchange(soci::into(val)); st.exchange(soci::use(pattern)); @@ -247,11 +285,11 @@ PersistentState::getTxSetHashesForAllSlots() std::unordered_set result; std::string val; - std::string pattern = mapping[kTxSet] + "%"; + std::string pattern = miscMapping[kTxSet - kLastEntryMain - 1] + "%"; std::string statementStr = "SELECT statename FROM storestate WHERE statename LIKE :n;"; auto& db = mApp.getDatabase(); - auto prep = db.getPreparedStatement(statementStr); + auto prep = db.getPreparedStatement(statementStr, db.getMiscSession()); auto& st = prep.statement(); st.exchange(soci::into(val)); st.exchange(soci::use(pattern)); @@ -261,7 +299,7 @@ PersistentState::getTxSetHashesForAllSlots() st.execute(true); } - size_t offset = mapping[kTxSet].size(); + size_t offset = miscMapping[kTxSet - kLastEntryMain - 1].size(); Hash hash; size_t len = binToHex(hash).size(); @@ -275,14 +313,14 @@ PersistentState::getTxSetHashesForAllSlots() } std::string -PersistentState::getFromDb(std::string const& entry) +PersistentState::getFromDb(std::string const& entry, SessionWrapper& sess) { ZoneScoped; std::string res; auto& db = mApp.getDatabase(); auto prep = db.getPreparedStatement( - "SELECT state FROM storestate WHERE statename = :n;"); + "SELECT state FROM storestate WHERE statename = :n;", sess); auto& st = prep.statement(); st.exchange(soci::into(res)); st.exchange(soci::use(entry)); @@ -301,14 +339,14 @@ PersistentState::getFromDb(std::string const& entry) } bool -PersistentState::entryExists(std::string const& entry) +PersistentState::entryExists(std::string const& entry, SessionWrapper& session) { ZoneScoped; int res = 0; auto& db = mApp.getDatabase(); auto prep = db.getPreparedStatement( - "SELECT COUNT(*) FROM storestate WHERE statename = :n;"); + "SELECT COUNT(*) FROM storestate WHERE statename = :n;", session); auto& st = prep.statement(); st.exchange(soci::into(res)); st.exchange(soci::use(entry)); @@ -317,4 +355,5 @@ PersistentState::entryExists(std::string const& entry) return res > 0; } + } diff --git a/src/main/PersistentState.h b/src/main/PersistentState.h index c22cd59e57..ba06ecaeaf 100644 --- a/src/main/PersistentState.h +++ b/src/main/PersistentState.h @@ -19,23 +19,30 @@ class PersistentState enum Entry { - kLastClosedLedger = 0, + // Main database entries + kLastClosedLedger, kHistoryArchiveState, - kLastSCPData, kDatabaseSchema, kNetworkPassphrase, - kLedgerUpgrades, kRebuildLedger, + kDBBackend, + kLastEntryMain, // Marker for the end of main database entries + + // Misc database entries + kLastSCPData = kLastEntryMain + 1, + kLedgerUpgrades, kLastSCPDataXDR, kTxSet, - kDBBackend, - kLastEntry, + kMiscDatabaseSchema, + kLastEntry // Marker for the end of misc database entries }; static void dropAll(Database& db); + static void dropMisc(Database& db); - std::string getState(Entry stateName); - void setState(Entry stateName, std::string const& value); + std::string getState(Entry stateName, SessionWrapper& session); + void setState(Entry stateName, std::string const& value, + SessionWrapper& session); // Special methods for SCP state (multiple slots) std::vector getSCPStateAllSlots(); @@ -52,19 +59,23 @@ class PersistentState bool hasTxSet(Hash const& txSetHash); void deleteTxSets(std::unordered_set hashesToDelete); + std::string getStoreStateName(Entry n, uint32 subscript = 0) const; private: static std::string kSQLCreateStatement; - static std::string mapping[kLastEntry]; + static std::string mainMapping[kLastEntryMain]; + static std::string miscMapping[kLastEntry]; Application& mApp; - std::string getStoreStateName(Entry n, uint32 subscript = 0); std::string getStoreStateNameForTxSet(Hash const& txSetHash); void setSCPStateForSlot(uint64 slot, std::string const& value); - void updateDb(std::string const& entry, std::string const& value); - std::string getFromDb(std::string const& entry); - bool entryExists(std::string const& entry); + void updateDb(std::string const& entry, std::string const& value, + SessionWrapper& session); + + std::string getFromDb(std::string const& entry, SessionWrapper& session); + + bool entryExists(std::string const& entry, SessionWrapper& session); }; } diff --git a/src/overlay/BanManagerImpl.cpp b/src/overlay/BanManagerImpl.cpp index a433ef94e0..26938001e5 100644 --- a/src/overlay/BanManagerImpl.cpp +++ b/src/overlay/BanManagerImpl.cpp @@ -44,7 +44,8 @@ BanManagerImpl::banNode(NodeID nodeID) { ZoneNamedN(insertBanZone, "insert ban", true); auto prep = mApp.getDatabase().getPreparedStatement( - "INSERT INTO ban (nodeid) VALUES(:n)"); + "INSERT INTO ban (nodeid) VALUES(:n)", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(nodeIDString)); st.define_and_bind(); @@ -61,7 +62,8 @@ BanManagerImpl::unbanNode(NodeID nodeID) { ZoneNamedN(deleteBanZone, "delete ban", true); auto prep = mApp.getDatabase().getPreparedStatement( - "DELETE FROM ban WHERE nodeid = :n;"); + "DELETE FROM ban WHERE nodeid = :n;", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::use(nodeIDString)); st.define_and_bind(); @@ -77,7 +79,8 @@ BanManagerImpl::isBanned(NodeID nodeID) { ZoneNamedN(selectBanZone, "select ban", true); auto prep = mApp.getDatabase().getPreparedStatement( - "SELECT count(*) FROM ban WHERE nodeid = :n"); + "SELECT count(*) FROM ban WHERE nodeid = :n", + mApp.getDatabase().getMiscSession()); uint32_t count; auto& st = prep.statement(); st.exchange(soci::into(count)); @@ -96,8 +99,8 @@ BanManagerImpl::getBans() std::string nodeIDString; { ZoneNamedN(selectBanZone, "select ban", true); - auto prep = - mApp.getDatabase().getPreparedStatement("SELECT nodeid FROM ban"); + auto prep = mApp.getDatabase().getPreparedStatement( + "SELECT nodeid FROM ban", mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(soci::into(nodeIDString)); st.define_and_bind(); @@ -114,10 +117,10 @@ BanManagerImpl::getBans() void BanManager::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS ban"; + db.getRawMiscSession() << "DROP TABLE IF EXISTS ban"; - db.getSession() << "CREATE TABLE ban (" - "nodeid CHARACTER(56) NOT NULL PRIMARY KEY" - ")"; + db.getRawMiscSession() << "CREATE TABLE ban (" + "nodeid CHARACTER(56) NOT NULL PRIMARY KEY" + ")"; } } diff --git a/src/overlay/PeerManager.cpp b/src/overlay/PeerManager.cpp index 4abfbc946c..7f02772f5a 100644 --- a/src/overlay/PeerManager.cpp +++ b/src/overlay/PeerManager.cpp @@ -96,7 +96,7 @@ PeerManager::loadRandomPeers(PeerQuery const& query, size_t size) size = std::max(size, BATCH_SIZE); // if we ever start removing peers from db, we may need to enable this - // soci::transaction sqltx(mApp.getDatabase().getSession()); + // soci::transaction sqltx(mApp.getDatabase().getMiscSession()); // mApp.getDatabase().setCurrentTransactionReadOnly(); std::vector conditions; @@ -180,7 +180,8 @@ PeerManager::removePeersWithManyFailures(size_t minNumFailures, sql += " AND ip = :ip"; } - auto prep = db.getPreparedStatement(sql); + auto prep = + db.getPreparedStatement(sql, mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(use(minNumFailures)); @@ -237,7 +238,8 @@ PeerManager::load(PeerBareAddress const& address) { auto prep = mApp.getDatabase().getPreparedStatement( "SELECT numfailures, nextattempt, type FROM peers " - "WHERE ip = :v1 AND port = :v2"); + "WHERE ip = :v1 AND port = :v2", + mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(into(result.mNumFailures)); st.exchange(into(result.mNextAttempt)); @@ -294,7 +296,8 @@ PeerManager::store(PeerBareAddress const& address, PeerRecord const& peerRecord, try { - auto prep = mApp.getDatabase().getPreparedStatement(query); + auto prep = mApp.getDatabase().getPreparedStatement( + query, mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(use(peerRecord.mNextAttempt)); st.exchange(use(peerRecord.mNumFailures)); @@ -503,7 +506,8 @@ PeerManager::countPeers(std::string const& where, { std::string sql = "SELECT COUNT(*) FROM peers WHERE " + where; - auto prep = mApp.getDatabase().getPreparedStatement(sql); + auto prep = mApp.getDatabase().getPreparedStatement( + sql, mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); bind(st); @@ -533,7 +537,8 @@ PeerManager::loadPeers(size_t limit, size_t offset, std::string const& where, "FROM peers WHERE " + where + " LIMIT :limit OFFSET :offset"; - auto prep = mApp.getDatabase().getPreparedStatement(sql); + auto prep = mApp.getDatabase().getPreparedStatement( + sql, mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); bind(st); @@ -570,8 +575,8 @@ PeerManager::loadPeers(size_t limit, size_t offset, std::string const& where, void PeerManager::dropAll(Database& db) { - db.getSession() << "DROP TABLE IF EXISTS peers;"; - db.getSession() << kSQLCreateStatement; + db.getRawMiscSession() << "DROP TABLE IF EXISTS peers;"; + db.getRawMiscSession() << kSQLCreateStatement; } std::vector> @@ -588,7 +593,8 @@ PeerManager::loadAllPeers() int port; PeerRecord record; - auto prep = mApp.getDatabase().getPreparedStatement(sql); + auto prep = mApp.getDatabase().getPreparedStatement( + sql, mApp.getDatabase().getMiscSession()); auto& st = prep.statement(); st.exchange(into(ip)); @@ -621,7 +627,7 @@ void PeerManager::storePeers( std::vector> peers) { - soci::transaction tx(mApp.getDatabase().getSession()); + soci::transaction tx(mApp.getDatabase().getRawMiscSession()); for (auto const& peer : peers) { store(peer.first, peer.second, /* inDatabase */ false); diff --git a/src/overlay/test/OverlayManagerTests.cpp b/src/overlay/test/OverlayManagerTests.cpp index 5255100fba..afba967e8b 100644 --- a/src/overlay/test/OverlayManagerTests.cpp +++ b/src/overlay/test/OverlayManagerTests.cpp @@ -165,7 +165,7 @@ class OverlayManagerTests pm.storeConfigPeers(); } - rowset rs = app->getDatabase().getSession().prepare + rowset rs = app->getDatabase().getRawSession().prepare << "SELECT ip,port,type FROM peers ORDER BY ip, port"; auto& ppeers = pm.mConfigurationPreferredPeers; @@ -213,7 +213,7 @@ class OverlayManagerTests pm.mResolvedPeers.wait(); pm.tick(); - rowset rs = app->getDatabase().getSession().prepare + rowset rs = app->getDatabase().getRawSession().prepare << "SELECT ip,port,type FROM peers ORDER BY ip, port"; int found = 0; diff --git a/src/transactions/TransactionSQL.cpp b/src/transactions/TransactionSQL.cpp index c35a725f36..29c9b34a3d 100644 --- a/src/transactions/TransactionSQL.cpp +++ b/src/transactions/TransactionSQL.cpp @@ -348,20 +348,21 @@ void dropSupportTransactionFeeHistory(Database& db) { ZoneScoped; - db.getSession() << "DROP TABLE IF EXISTS txfeehistory"; + db.getRawSession() << "DROP TABLE IF EXISTS txfeehistory"; } void dropSupportTxSetHistory(Database& db) { ZoneScoped; - db.getSession() << "DROP TABLE IF EXISTS txsethistory"; + db.getRawSession() << "DROP TABLE IF EXISTS txsethistory"; } void dropSupportTxHistory(Database& db) { + releaseAssert(threadIsMain()); ZoneScoped; - db.getSession() << "DROP TABLE IF EXISTS txhistory"; + db.getRawSession() << "DROP TABLE IF EXISTS txhistory"; } }