Skip to content

Commit

Permalink
Drop txmeta from txhistory when BucketDB enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTyson committed Apr 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent ad7b164 commit 31dabd1
Showing 5 changed files with 80 additions and 15 deletions.
45 changes: 44 additions & 1 deletion src/database/Database.cpp
Original file line number Diff line number Diff line change
@@ -250,10 +250,53 @@ Database::upgradeToCurrentSchema()
applySchemaUpgrade(vers);
putSchemaVersion(vers);
}

// While not really a schema upgrade, we need to upgrade the DB when
// BucketListDB is enabled.
if (mApp.getConfig().isUsingBucketListDB())
{
// Tx meta column no longer supported in BucketListDB
dropTxMetaIfExists();
}

CLOG_INFO(Database, "DB schema is in current version");
releaseAssert(vers == SCHEMA_VERSION);
}

void
Database::dropTxMetaIfExists()
{
int txMetaExists{};
std::string selectStr;
if (isSqlite())
{
selectStr = "SELECT EXISTS ("
"SELECT 1 "
"FROM pragma_table_info('txhistory') "
"WHERE name = 'txmeta');";
}
else
{
selectStr = "SELECT EXISTS ("
"SELECT 1 "
"FROM information_schema.columns "
"WHERE "
"table_name = 'txhistory' AND "
"column_name = 'txmeta');";
}

auto& st = getPreparedStatement(selectStr).statement();
st.exchange(soci::into(txMetaExists));
st.define_and_bind();
st.execute(true);

if (txMetaExists)
{
CLOG_INFO(Database, "Dropping txmeta column from txhistory table");
getSession() << "ALTER TABLE txhistory DROP COLUMN txmeta;";
}
}

void
Database::putSchemaVersion(unsigned long vers)
{
@@ -431,7 +474,7 @@ Database::initialize()
PersistentState::dropAll(*this);
ExternalQueue::dropAll(*this);
LedgerHeaderUtils::dropAll(*this);
dropTransactionHistory(*this);
dropTransactionHistory(*this, mApp.getConfig());
HistoryManager::dropAll(*this);
HerderPersistence::dropAll(*this);
BanManager::dropAll(*this);
2 changes: 2 additions & 0 deletions src/database/Database.h
Original file line number Diff line number Diff line change
@@ -173,6 +173,8 @@ class Database : NonMovableOrCopyable
// Check schema version and apply any upgrades if necessary.
void upgradeToCurrentSchema();

void dropTxMetaIfExists();

// Access the underlying SOCI session object
soci::session& getSession();

2 changes: 1 addition & 1 deletion src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
@@ -1548,7 +1548,7 @@ LedgerManagerImpl::applyTransactions(
{
auto ledgerSeq = ltx.loadHeader().current().ledgerSeq;
storeTransaction(mApp.getDatabase(), ledgerSeq, tx, tm.getXDR(),
txResultSet);
txResultSet, mApp.getConfig());
}
}

42 changes: 31 additions & 11 deletions src/transactions/TransactionSQL.cpp
Original file line number Diff line number Diff line change
@@ -350,7 +350,7 @@ writeTxSetToStream(
void
storeTransaction(Database& db, uint32_t ledgerSeq,
TransactionFrameBasePtr const& tx, TransactionMeta const& tm,
TransactionResultSet const& resultSet)
TransactionResultSet const& resultSet, Config const& cfg)
{
ZoneScoped;
std::string txBody =
@@ -362,18 +362,34 @@ storeTransaction(Database& db, uint32_t ledgerSeq,
std::string txIDString = binToHex(tx->getContentsHash());
uint32_t txIndex = static_cast<uint32_t>(resultSet.results.size());

auto prep = db.getPreparedStatement(
"INSERT INTO txhistory "
"( txid, ledgerseq, txindex, txbody, txresult, txmeta) VALUES "
"(:id, :seq, :txindex, :txb, :txres, :meta)");
std::string sqlStr;
if (cfg.isUsingBucketListDB())
{
sqlStr = "INSERT INTO txhistory "
"( txid, ledgerseq, txindex, txbody, txresult) VALUES "
"(:id, :seq, :txindex, :txb, :txres)";
}
else
{
sqlStr =
"INSERT INTO txhistory "
"( txid, ledgerseq, txindex, txbody, txresult, txmeta) VALUES "
"(:id, :seq, :txindex, :txb, :txres, :meta)";
}

auto prep = db.getPreparedStatement(sqlStr);
auto& st = prep.statement();
st.exchange(soci::use(txIDString));
st.exchange(soci::use(ledgerSeq));
st.exchange(soci::use(txIndex));
st.exchange(soci::use(txBody));
st.exchange(soci::use(txResult));
st.exchange(soci::use(meta));

if (!cfg.isUsingBucketListDB())
{
st.exchange(soci::use(meta));
}

st.define_and_bind();
{
auto timer = db.getInsertTimer("txhistory");
@@ -615,22 +631,26 @@ createTxSetHistoryTable(Database& db)
}

void
dropTransactionHistory(Database& db)
dropTransactionHistory(Database& db, Config const& cfg)
{
ZoneScoped;
db.getSession() << "DROP TABLE IF EXISTS txhistory";

db.getSession() << "DROP TABLE IF EXISTS txfeehistory";

// txmeta only supported when BucketListDB is not enabled
std::string txMetaColumn =
cfg.isUsingBucketListDB() ? "" : "txmeta TEXT NOT NULL,";

db.getSession() << "CREATE TABLE txhistory ("
"txid CHARACTER(64) NOT NULL,"
"ledgerseq INT NOT NULL CHECK (ledgerseq >= 0),"
"txindex INT NOT NULL,"
"txbody TEXT NOT NULL,"
"txresult TEXT NOT NULL,"
"txmeta TEXT NOT NULL,"
"PRIMARY KEY (ledgerseq, txindex)"
")";
"txresult TEXT NOT NULL," +
txMetaColumn +
"PRIMARY KEY (ledgerseq, txindex)"
")";

db.getSession() << "CREATE INDEX histbyseq ON txhistory (ledgerseq);";

4 changes: 2 additions & 2 deletions src/transactions/TransactionSQL.h
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ class XDROutputFileStream;
void storeTransaction(Database& db, uint32_t ledgerSeq,
TransactionFrameBasePtr const& tx,
TransactionMeta const& tm,
TransactionResultSet const& resultSet);
TransactionResultSet const& resultSet, Config const& cfg);

void storeTxSet(Database& db, uint32_t ledgerSeq, TxSetXDRFrame const& txSet);

@@ -38,7 +38,7 @@ size_t copyTransactionsToStream(Application& app, soci::session& sess,

void createTxSetHistoryTable(Database& db);

void dropTransactionHistory(Database& db);
void dropTransactionHistory(Database& db, Config const& cfg);

void deleteOldTransactionHistoryEntries(Database& db, uint32_t ledgerSeq,
uint32_t count);

5 comments on commit 31dabd1

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from marta-lokhova
at SirTyson@31dabd1

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SirTyson/stellar-core/drop-meta-table = 31dabd1 merged ok, testing candidate = 75c597b

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging SirTyson/stellar-core/drop-meta-table = 31dabd1 into auto

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 75c597b

Please sign in to comment.