From a327cecee6b58e3070209dc3a7a7ca328de0505b Mon Sep 17 00:00:00 2001 From: Edward Hennis Date: Fri, 4 Mar 2016 14:15:15 -0500 Subject: [PATCH] Create new Validations columns properly: * Thread-safe preparation of Validations cleanup query * Followup to RIPD-870 --- src/ripple/app/main/Application.cpp | 36 ++++++++++++++++++++++++++ src/ripple/app/main/DBInit.cpp | 7 ----- src/ripple/app/misc/SHAMapStoreImp.cpp | 4 +-- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/ripple/app/main/Application.cpp b/src/ripple/app/main/Application.cpp index 148ab09523..84fcafde03 100644 --- a/src/ripple/app/main/Application.cpp +++ b/src/ripple/app/main/Application.cpp @@ -923,6 +923,7 @@ public: private: void addTxnSeqField(); + void addValidationSeqFields(); void updateTables (); void startGenesisLedger (); @@ -1705,6 +1706,39 @@ void ApplicationImp::addTxnSeqField () tr.commit (); } +void ApplicationImp::addValidationSeqFields () +{ + if (schemaHas(getLedgerDB(), "Validations", 0, "LedgerSeq", m_journal)) + { + assert(schemaHas(getLedgerDB(), "Validations", 0, "InitialSeq", m_journal)); + return; + } + + JLOG(m_journal.warning) << "Validation sequence fields are missing"; + assert(!schemaHas(getLedgerDB(), "Validations", 0, "InitialSeq", m_journal)); + + auto& session = getLedgerDB().getSession(); + + soci::transaction tr(session); + + JLOG(m_journal.info) << "Altering table"; + session << "ALTER TABLE Validations " + "ADD COLUMN LedgerSeq BIGINT UNSIGNED;"; + session << "ALTER TABLE Validations " + "ADD COLUMN InitialSeq BIGINT UNSIGNED;"; + + // Create the indexes, too, so we don't have to + // wait for the next startup, which may be a while. + // These should be identical to those in LedgerDBInit + JLOG(m_journal.info) << "Building new indexes"; + session << "CREATE INDEX IF NOT EXISTS " + "ValidationsBySeq ON Validations(LedgerSeq);"; + session << "CREATE INDEX IF NOT EXISTS ValidationsByInitialSeq " + "ON Validations(InitialSeq, LedgerSeq);"; + + tr.commit(); +} + void ApplicationImp::updateTables () { if (config_->section (ConfigSection::nodeDatabase ()).empty ()) @@ -1724,6 +1758,8 @@ void ApplicationImp::updateTables () exitWithCode(1); } + addValidationSeqFields (); + if (config_->doImport) { NodeStore::DummyScheduler scheduler; diff --git a/src/ripple/app/main/DBInit.cpp b/src/ripple/app/main/DBInit.cpp index 2a404fa3db..2583a1a1e8 100644 --- a/src/ripple/app/main/DBInit.cpp +++ b/src/ripple/app/main/DBInit.cpp @@ -100,13 +100,6 @@ const char* LedgerDBInit[] = SignTime BIGINT UNSIGNED, \ RawData BLOB \ );", - // This will error out if the column already exists, - // but DatabaseCon intentionally ignores errors. - "ALTER TABLE Validations \ - ADD COLUMN LedgerSeq BIGINT UNSIGNED;", - "ALTER TABLE Validations \ - ADD COLUMN InitialSeq BIGINT UNSIGNED;", - "CREATE INDEX IF NOT EXISTS ValidationsByHash ON \ Validations(LedgerHash);", "CREATE INDEX IF NOT EXISTS ValidationsBySeq ON \ diff --git a/src/ripple/app/misc/SHAMapStoreImp.cpp b/src/ripple/app/misc/SHAMapStoreImp.cpp index 3eeb001a95..205ae18c0a 100644 --- a/src/ripple/app/misc/SHAMapStoreImp.cpp +++ b/src/ripple/app/misc/SHAMapStoreImp.cpp @@ -664,10 +664,10 @@ SHAMapStoreImp::clearPrior (LedgerIndex lastRotated) << deleteBatch << " rows."; long long totalRowsAffected = 0; long long rowsAffected; - soci::statement st = [&] + auto st = [&] { auto db = ledgerDb_->checkoutDb(); - return (db->prepare << deleteQuery); + return soci::statement(db->prepare << deleteQuery); }(); if (health()) return;