Skip to content

Commit

Permalink
Refactored SearchableBucketList
Browse files Browse the repository at this point in the history
  • Loading branch information
SirTyson committed Nov 14, 2024
1 parent ef175c0 commit b820547
Show file tree
Hide file tree
Showing 17 changed files with 234 additions and 235 deletions.
1 change: 0 additions & 1 deletion src/bucket/BucketBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "bucket/BucketBase.h"
#include "bucket/BucketIndex.h"
#include "bucket/BucketInputIterator.h"
#include "bucket/BucketListSnapshot.h"
#include "bucket/BucketManager.h"
#include "bucket/BucketOutputIterator.h"
#include "bucket/BucketUtils.h"
Expand Down
162 changes: 162 additions & 0 deletions src/bucket/BucketListSnapshotBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// Copyright 2024 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "bucket/BucketListSnapshotBase.h"
#include "bucket/BucketListBase.h"
#include "bucket/LiveBucket.h"
#include "crypto/SecretKey.h" // IWYU pragma: keep
#include "ledger/LedgerTxn.h"

#include "util/GlobalChecks.h"
#include <optional>
#include <vector>

namespace stellar
{
template <class BucketT>
BucketListSnapshot<BucketT>::BucketListSnapshot(
BucketListBase<BucketT> const& bl, LedgerHeader header)
: mHeader(std::move(header))
{
releaseAssert(threadIsMain());

for (uint32_t i = 0; i < BucketListBase<BucketT>::kNumLevels; ++i)
{
auto const& level = bl.getLevel(i);
mLevels.emplace_back(BucketLevelSnapshot<BucketT>(level));
}
}

template <class BucketT>
BucketListSnapshot<BucketT>::BucketListSnapshot(
BucketListSnapshot<BucketT> const& snapshot)
: mLevels(snapshot.mLevels), mHeader(snapshot.mHeader)
{
}

template <class BucketT>
std::vector<BucketLevelSnapshot<BucketT>> const&
BucketListSnapshot<BucketT>::getLevels() const
{
return mLevels;
}

template <class BucketT>
uint32_t
BucketListSnapshot<BucketT>::getLedgerSeq() const
{
return mHeader.ledgerSeq;
}

template <class BucketT>
LedgerHeader const&
SearchableBucketListSnapshotBase<BucketT>::getLedgerHeader()
{
releaseAssert(mSnapshot);
mSnapshotManager.maybeUpdateSnapshot(mSnapshot, mHistoricalSnapshots);
return mSnapshot->getLedgerHeader();
}

template <class BucketT>
void
SearchableBucketListSnapshotBase<BucketT>::loopAllBuckets(
std::function<Loop(BucketSnapshotT const&)> f,
BucketListSnapshot<BucketT> const& snapshot) const
{
for (auto const& lev : snapshot.getLevels())
{
auto processBucket = [f](BucketSnapshotT const& b) {
if (b.isEmpty())
{
return Loop::INCOMPLETE;
}

return f(b);
};

if (processBucket(lev.curr) == Loop::COMPLETE ||
processBucket(lev.snap) == Loop::COMPLETE)
{
return;
}
}
}

template <class BucketT>
std::shared_ptr<typename BucketT::LoadT>
SearchableBucketListSnapshotBase<BucketT>::load(LedgerKey const& k)
{
ZoneScoped;

std::shared_ptr<typename BucketT::LoadT> result{};
auto sawBloomMiss = false;

// Search function called on each Bucket in BucketList until we find the key
auto loadKeyBucketLoop = [&](auto const& b) {
auto [be, bloomMiss] = b.getBucketEntry(k);
sawBloomMiss = sawBloomMiss || bloomMiss;

if (be)
{
result = BucketT::bucketEntryToLoadResult(be);
return Loop::COMPLETE;
}
else
{
return Loop::INCOMPLETE;
}
};

mSnapshotManager.maybeUpdateSnapshot(mSnapshot, mHistoricalSnapshots);
if (threadIsMain())
{
mSnapshotManager.startPointLoadTimer();
loopAllBuckets(loadKeyBucketLoop, *mSnapshot);
mSnapshotManager.endPointLoadTimer(k.type(), sawBloomMiss);
return result;
}
else
{
// TODO: Background metrics
loopAllBuckets(loadKeyBucketLoop, *mSnapshot);
return result;
}
}

template <class BucketT>
std::optional<std::vector<typename BucketT::LoadT>>
SearchableBucketListSnapshotBase<BucketT>::loadKeysFromLedger(
std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys, uint32_t ledgerSeq)
{
return loadKeysInternal(inKeys, /*lkMeter=*/nullptr, ledgerSeq);
}

template <class BucketT>
BucketLevelSnapshot<BucketT>::BucketLevelSnapshot(
BucketLevel<BucketT> const& level)
: curr(level.getCurr()), snap(level.getSnap())
{
}

template <class BucketT>
SearchableBucketListSnapshotBase<BucketT>::SearchableBucketListSnapshotBase(
BucketSnapshotManager const& snapshotManager)
: mSnapshotManager(snapshotManager), mHistoricalSnapshots()
{

mSnapshotManager.maybeUpdateSnapshot(mSnapshot, mHistoricalSnapshots);
}

template <class BucketT>
SearchableBucketListSnapshotBase<BucketT>::~SearchableBucketListSnapshotBase()
{
}

template struct BucketLevelSnapshot<LiveBucket>;
template struct BucketLevelSnapshot<HotArchiveBucket>;
template class BucketListSnapshot<LiveBucket>;
template class BucketListSnapshot<HotArchiveBucket>;
template class SearchableBucketListSnapshotBase<LiveBucket>;
template class SearchableBucketListSnapshotBase<HotArchiveBucket>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,46 +125,4 @@ class SearchableBucketListSnapshotBase : public NonMovableOrCopyable

std::shared_ptr<typename BucketT::LoadT> load(LedgerKey const& k);
};

class SearchableLiveBucketListSnapshot
: public SearchableBucketListSnapshotBase<LiveBucket>
{
SearchableLiveBucketListSnapshot(
BucketSnapshotManager const& snapshotManager);

public:
std::vector<LedgerEntry>
loadPoolShareTrustLinesByAccountAndAsset(AccountID const& accountID,
Asset const& asset);

std::vector<InflationWinner> loadInflationWinners(size_t maxWinners,
int64_t minBalance);

std::vector<LedgerEntry>
loadKeysWithLimits(std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys,
LedgerKeyMeter* lkMeter);

EvictionResult scanForEviction(uint32_t ledgerSeq,
EvictionCounters& counters,
EvictionIterator evictionIter,
std::shared_ptr<EvictionStatistics> stats,
StateArchivalSettings const& sas);

friend std::shared_ptr<SearchableLiveBucketListSnapshot>
BucketSnapshotManager::copySearchableLiveBucketListSnapshot() const;
};

class SearchableHotArchiveBucketListSnapshot
: public SearchableBucketListSnapshotBase<HotArchiveBucket>
{
SearchableHotArchiveBucketListSnapshot(
BucketSnapshotManager const& snapshotManager);

public:
std::vector<HotArchiveBucketEntry>
loadKeys(std::set<LedgerKey, LedgerEntryIdCmp> const& inKeys);

friend std::shared_ptr<SearchableHotArchiveBucketListSnapshot>
BucketSnapshotManager::copySearchableHotArchiveBucketListSnapshot() const;
};
}
2 changes: 1 addition & 1 deletion src/bucket/BucketManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include "bucket/BucketManager.h"
#include "bucket/BucketInputIterator.h"
#include "bucket/BucketListSnapshot.h"
#include "bucket/BucketManager.h"
#include "bucket/BucketOutputIterator.h"
#include "bucket/BucketSnapshotManager.h"
#include "bucket/BucketUtils.h"
#include "bucket/HotArchiveBucket.h"
#include "bucket/LiveBucket.h"
#include "bucket/SearchableBucketList.h"
#include "crypto/BLAKE2.h"
#include "crypto/Hex.h"
#include "history/HistoryManager.h"
Expand Down
2 changes: 1 addition & 1 deletion src/bucket/BucketSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "bucket/BucketSnapshot.h"
#include "bucket/BucketIndex.h"
#include "bucket/BucketListSnapshot.h"
#include "bucket/HotArchiveBucket.h"
#include "bucket/LiveBucket.h"
#include "bucket/SearchableBucketList.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTypeUtils.h"
#include "util/XDRStream.h"
Expand Down
2 changes: 1 addition & 1 deletion src/bucket/BucketSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
#include "bucket/LedgerCmp.h"
#include "bucket/LiveBucket.h"
#include "util/NonCopyable.h"
#include "util/XDRStream.h"
#include "xdr/Stellar-ledger-entries.h"
#include <list>
#include <set>

namespace stellar
{

class XDRInputFileStream;
struct EvictionResultEntry;
class LedgerKeyMeter;
class SearchableLiveBucketListSnapshot;
Expand Down
2 changes: 1 addition & 1 deletion src/bucket/BucketSnapshotManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "bucket/BucketSnapshotManager.h"
#include "bucket/BucketListSnapshot.h"
#include "bucket/BucketUtils.h"
#include "bucket/HotArchiveBucket.h"
#include "bucket/LiveBucket.h"
#include "bucket/SearchableBucketList.h"
#include "main/Application.h"
#include "util/GlobalChecks.h"
#include "util/XDRStream.h" // IWYU pragma: keep
Expand Down
Loading

0 comments on commit b820547

Please sign in to comment.