Skip to content

Commit

Permalink
Removed LedgerEntry SQL code
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTyson committed Dec 14, 2024
1 parent 70b2228 commit da4b3d5
Show file tree
Hide file tree
Showing 22 changed files with 129 additions and 4,514 deletions.
170 changes: 74 additions & 96 deletions src/bucket/test/BucketManagerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,112 +189,90 @@ TEST_CASE("skip list", "[bucket][bucketmanager]")

TEST_CASE_VERSIONS("bucketmanager ownership", "[bucket][bucketmanager]")
{
auto test = [&](bool bucketListDB) {
VirtualClock clock;
Config cfg = getTestConfig();

// Make sure all Buckets serialize indexes to disk for test
cfg.BUCKETLIST_DB_INDEX_CUTOFF = 0;
cfg.MANUAL_CLOSE = false;

if (bucketListDB)
{
// Enable BucketListDB with persistent indexes
cfg.NODE_IS_VALIDATOR = false;
cfg.FORCE_SCP = false;
}

for_versions_with_differing_bucket_logic(cfg, [&](Config const& cfg) {
Application::pointer app = createTestApplication(clock, cfg);

std::vector<LedgerEntry> live(
LedgerTestUtils::generateValidUniqueLedgerEntriesWithExclusions(
{CONFIG_SETTING}, 10));
std::vector<LedgerKey> dead{};

std::shared_ptr<LiveBucket> b1;

{
std::shared_ptr<LiveBucket> b2 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
b1 = b2;

// Bucket is referenced by b1, b2 and the BucketManager.
CHECK(b1.use_count() == 3);

std::shared_ptr<LiveBucket> b3 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
std::shared_ptr<LiveBucket> b4 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
// Bucket is referenced by b1, b2, b3, b4 and the BucketManager.
CHECK(b1.use_count() == 5);
}

// Take pointer by reference to not mess up use_count()
auto dropBucket = [&](std::shared_ptr<LiveBucket>& b) {
std::string filename = b->getFilename().string();
std::string indexFilename =
app->getBucketManager().bucketIndexFilename(b->getHash());
CHECK(fs::exists(filename));
if (bucketListDB)
{
CHECK(fs::exists(indexFilename));
}
VirtualClock clock;
Config cfg = getTestConfig();

b.reset();
app->getBucketManager().forgetUnreferencedBuckets();
CHECK(!fs::exists(filename));
CHECK(!fs::exists(indexFilename));
};
// Make sure all Buckets serialize indexes to disk for test
cfg.BUCKETLIST_DB_INDEX_CUTOFF = 0;
cfg.MANUAL_CLOSE = false;

// Bucket is now only referenced by b1 and the BucketManager.
CHECK(b1.use_count() == 2);
for_versions_with_differing_bucket_logic(cfg, [&](Config const& cfg) {
Application::pointer app = createTestApplication(clock, cfg);

// Drop bucket ourselves then purge bucketManager.
dropBucket(b1);
std::vector<LedgerEntry> live(
LedgerTestUtils::generateValidUniqueLedgerEntriesWithExclusions(
{CONFIG_SETTING}, 10));
std::vector<LedgerKey> dead{};

// Try adding a bucket to the BucketManager's bucketlist
auto& bl = app->getBucketManager().getLiveBucketList();
bl.addBatch(*app, 1, getAppLedgerVersion(app), {}, live, dead);
clearFutures(app, bl);
b1 = bl.getLevel(0).getCurr();
std::shared_ptr<LiveBucket> b1;

// Bucket should be referenced by bucketlist itself, BucketManager
// cache and b1.
CHECK(b1.use_count() == 3);
{
std::shared_ptr<LiveBucket> b2 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
b1 = b2;

// This shouldn't change if we forget unreferenced buckets since
// it's referenced by bucketlist.
app->getBucketManager().forgetUnreferencedBuckets();
// Bucket is referenced by b1, b2 and the BucketManager.
CHECK(b1.use_count() == 3);

// But if we mutate the curr bucket of the bucketlist, it should.
live[0] = LedgerTestUtils::generateValidLedgerEntryWithExclusions(
{CONFIG_SETTING});
bl.addBatch(*app, 1, getAppLedgerVersion(app), {}, live, dead);
clearFutures(app, bl);
CHECK(b1.use_count() == 2);

// Drop it again.
dropBucket(b1);
});
};
std::shared_ptr<LiveBucket> b3 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
std::shared_ptr<LiveBucket> b4 = LiveBucket::fresh(
app->getBucketManager(), getAppLedgerVersion(app), {}, live,
dead, /*countMergeEvents=*/true, clock.getIOContext(),
/*doFsync=*/true);
// Bucket is referenced by b1, b2, b3, b4 and the BucketManager.
CHECK(b1.use_count() == 5);
}

SECTION("BucketListDB")
{
test(true);
}
// Take pointer by reference to not mess up use_count()
auto dropBucket = [&](std::shared_ptr<LiveBucket>& b) {
std::string filename = b->getFilename().string();
std::string indexFilename =
app->getBucketManager().bucketIndexFilename(b->getHash());
CHECK(fs::exists(filename));
CHECK(fs::exists(indexFilename));

SECTION("SQL")
{
test(false);
}
b.reset();
app->getBucketManager().forgetUnreferencedBuckets();
CHECK(!fs::exists(filename));
CHECK(!fs::exists(indexFilename));
};

// Bucket is now only referenced by b1 and the BucketManager.
CHECK(b1.use_count() == 2);

// Drop bucket ourselves then purge bucketManager.
dropBucket(b1);

// Try adding a bucket to the BucketManager's bucketlist
auto& bl = app->getBucketManager().getLiveBucketList();
bl.addBatch(*app, 1, getAppLedgerVersion(app), {}, live, dead);
clearFutures(app, bl);
b1 = bl.getLevel(0).getCurr();

// Bucket should be referenced by bucketlist itself, BucketManager
// cache and b1.
CHECK(b1.use_count() == 3);

// This shouldn't change if we forget unreferenced buckets since
// it's referenced by bucketlist.
app->getBucketManager().forgetUnreferencedBuckets();
CHECK(b1.use_count() == 3);

// But if we mutate the curr bucket of the bucketlist, it should.
live[0] = LedgerTestUtils::generateValidLedgerEntryWithExclusions(
{CONFIG_SETTING});
bl.addBatch(*app, 1, getAppLedgerVersion(app), {}, live, dead);
clearFutures(app, bl);
CHECK(b1.use_count() == 2);

// Drop it again.
dropBucket(b1);
});
}

TEST_CASE("bucketmanager missing buckets fail", "[bucket][bucketmanager]")
Expand Down
148 changes: 4 additions & 144 deletions src/ledger/LedgerTxn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,66 +2015,11 @@ LedgerTxn::deleteOffersModifiedOnOrAfterLedger(uint32_t ledger) const
}

void
LedgerTxn::dropAccounts(bool rebuild)
{
throw std::runtime_error("called dropAccounts on non-root LedgerTxn");
}

void
LedgerTxn::dropData(bool rebuild)
{
throw std::runtime_error("called dropData on non-root LedgerTxn");
}

void
LedgerTxn::dropOffers(bool rebuild)
LedgerTxn::dropOffers()
{
throw std::runtime_error("called dropOffers on non-root LedgerTxn");
}

void
LedgerTxn::dropTrustLines(bool rebuild)
{
throw std::runtime_error("called dropTrustLines on non-root LedgerTxn");
}

void
LedgerTxn::dropClaimableBalances(bool rebuild)
{
throw std::runtime_error(
"called dropClaimableBalances on non-root LedgerTxn");
}

void
LedgerTxn::dropLiquidityPools(bool rebuild)
{
throw std::runtime_error("called dropLiquidityPools on non-root LedgerTxn");
}

void
LedgerTxn::dropContractData(bool rebuild)
{
throw std::runtime_error("called dropContractData on non-root LedgerTxn");
}

void
LedgerTxn::dropContractCode(bool rebuild)
{
throw std::runtime_error("called dropContractCode on non-root LedgerTxn");
}

void
LedgerTxn::dropConfigSettings(bool rebuild)
{
throw std::runtime_error("called dropConfigSettings on non-root LedgerTxn");
}

void
LedgerTxn::dropTTL(bool rebuild)
{
throw std::runtime_error("called dropTTL on non-root LedgerTxn");
}

double
LedgerTxn::getPrefetchHitRate() const
{
Expand Down Expand Up @@ -2717,36 +2662,6 @@ LedgerTxnRoot::Impl::commitChild(EntryIterator iter,
mPrefetchMisses = 0;
}

std::string
LedgerTxnRoot::Impl::tableFromLedgerEntryType(LedgerEntryType let)
{
switch (let)
{
case ACCOUNT:
return "accounts";
case DATA:
return "accountdata";
case OFFER:
return "offers";
case TRUSTLINE:
return "trustlines";
case CLAIMABLE_BALANCE:
return "claimablebalance";
case LIQUIDITY_POOL:
return "liquiditypool";
case CONTRACT_DATA:
return "contractdata";
case CONTRACT_CODE:
return "contractcode";
case CONFIG_SETTING:
return "configsettings";
case TTL:
return "ttl";
default:
throw std::runtime_error("Unknown ledger entry type");
}
}

uint64_t
LedgerTxnRoot::countOffers(LedgerRange const& ledgers) const
{
Expand Down Expand Up @@ -2783,69 +2698,14 @@ LedgerTxnRoot::Impl::deleteOffersModifiedOnOrAfterLedger(uint32_t ledger) const
mEntryCache.clear();
mBestOffers.clear();

std::string query = "DELETE FROM " + tableFromLedgerEntryType(OFFER) +
" WHERE lastmodified >= :v1";
std::string query = "DELETE FROM offers WHERE lastmodified >= :v1";
mApp.getDatabase().getSession() << query, use(ledger);
}

void
LedgerTxnRoot::dropAccounts(bool rebuild)
{
mImpl->dropAccounts(rebuild);
}

void
LedgerTxnRoot::dropData(bool rebuild)
{
mImpl->dropData(rebuild);
}

void
LedgerTxnRoot::dropOffers(bool rebuild)
{
mImpl->dropOffers(rebuild);
}

void
LedgerTxnRoot::dropTrustLines(bool rebuild)
{
mImpl->dropTrustLines(rebuild);
}

void
LedgerTxnRoot::dropClaimableBalances(bool rebuild)
{
mImpl->dropClaimableBalances(rebuild);
}

void
LedgerTxnRoot::dropLiquidityPools(bool rebuild)
{
mImpl->dropLiquidityPools(rebuild);
}

void
LedgerTxnRoot::dropContractData(bool rebuild)
{
mImpl->dropContractData(rebuild);
}

void
LedgerTxnRoot::dropContractCode(bool rebuild)
{
mImpl->dropContractCode(rebuild);
}

void
LedgerTxnRoot::dropConfigSettings(bool rebuild)
{
mImpl->dropConfigSettings(rebuild);
}

void
LedgerTxnRoot::dropTTL(bool rebuild)
LedgerTxnRoot::dropOffers()
{
mImpl->dropTTL(rebuild);
mImpl->dropOffers();
}

uint32_t
Expand Down
Loading

0 comments on commit da4b3d5

Please sign in to comment.