Skip to content

Commit 4e16098

Browse files
committed
Added indexing to ApplyBucketsWork and fixes
1 parent 98229f3 commit 4e16098

File tree

4 files changed

+59
-20
lines changed

4 files changed

+59
-20
lines changed

src/bucket/BucketApplicator.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ BucketApplicator::BucketApplicator(Application& app,
2323
std::shared_ptr<Bucket const> bucket,
2424
std::function<bool(LedgerEntryType)> filter,
2525
bool newOffersOnly,
26-
UnorderedSet<LedgerKey> seenKeys)
26+
UnorderedSet<LedgerKey>& seenKeys)
2727
: mApp(app)
2828
, mMaxProtocolVersion(maxProtocolVersion)
2929
, mMinProtocolVersionSeen(minProtocolVersionSeen)
@@ -42,7 +42,7 @@ BucketApplicator::BucketApplicator(Application& app,
4242
protocolVersion, mMaxProtocolVersion));
4343
}
4444

45-
if (newOffersOnly)
45+
if (newOffersOnly && !bucket->isEmpty())
4646
{
4747
releaseAssertOrThrow(mApp.getConfig().isUsingBucketListDB());
4848
auto [lowOffset, highOffset] = bucket->getOfferRange();

src/bucket/BucketApplicator.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class BucketApplicator
7676
uint32_t minProtocolVersionSeen, uint32_t level,
7777
std::shared_ptr<Bucket const> bucket,
7878
std::function<bool(LedgerEntryType)> filter,
79-
bool newOffersOnly, UnorderedSet<LedgerKey> seenKeys);
79+
bool newOffersOnly, UnorderedSet<LedgerKey>& seenKeys);
8080
operator bool() const;
8181
size_t advance(Counters& counters);
8282

src/catchup/ApplyBucketsWork.cpp

+52-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "bucket/BucketManager.h"
1010
#include "catchup/AssumeStateWork.h"
1111
#include "catchup/CatchupManager.h"
12+
#include "catchup/IndexBucketsWork.h"
1213
#include "crypto/Hex.h"
1314
#include "crypto/SecretKey.h"
1415
#include "historywork/Progress.h"
@@ -108,7 +109,7 @@ ApplyBucketsWork::getBucketLevel(uint32_t level)
108109
return mApp.getBucketManager().getBucketList().getLevel(level);
109110
}
110111

111-
std::shared_ptr<Bucket const>
112+
std::shared_ptr<Bucket>
112113
ApplyBucketsWork::getBucket(std::string const& hash)
113114
{
114115
auto i = mBuckets.find(hash);
@@ -134,17 +135,18 @@ ApplyBucketsWork::doReset()
134135
mLastPos = 0;
135136
mMinProtocolVersionSeen = UINT32_MAX;
136137
mSeenKeys.clear();
137-
138-
if (mOffersOnly)
139-
{
140-
// The current size of this set is 1.6 million during BucketApply (as of
141-
// 12/20/23). There's not a great way to estimate this, so reserving
142-
// with some extra wiggle room
143-
mSeenKeys.reserve(2'000'000);
144-
}
138+
mBucketsToIndex.clear();
145139

146140
if (!isAborting())
147141
{
142+
if (mOffersOnly)
143+
{
144+
// The current size of this set is 1.6 million during BucketApply
145+
// (as of 12/20/23). There's not a great way to estimate this, so
146+
// reserving with some extra wiggle room
147+
mSeenKeys.reserve(2'000'000);
148+
}
149+
148150
// When applying buckets with accounts, we have to make sure that the
149151
// root account has been removed. This comes into play, for example,
150152
// when applying buckets from genesis the root account already exists.
@@ -164,12 +166,17 @@ ApplyBucketsWork::doReset()
164166
}
165167
}
166168

167-
auto addBucket = [this](std::shared_ptr<Bucket const> const& bucket) {
169+
auto addBucket = [this](std::shared_ptr<Bucket> const& bucket) {
168170
if (bucket->getSize() > 0)
169171
{
170172
mTotalBuckets++;
171173
mTotalSize += bucket->getSize();
172174
}
175+
176+
if (mOffersOnly)
177+
{
178+
mBucketsToIndex.emplace_back(bucket);
179+
}
173180
};
174181

175182
for (auto const& hsb : mApplyState.currentBuckets)
@@ -280,7 +287,34 @@ ApplyBucketsWork::doWork()
280287
{
281288
ZoneScoped;
282289

283-
// Step 1: apply buckets. Step 2: assume state
290+
// Step 1: index buckets. Step 2: apply buckets. Step 3: assume state
291+
if (!mSpawnedIndexBucketsWork)
292+
{
293+
if (mOffersOnly)
294+
{
295+
addWork<IndexBucketsWork>(mBucketsToIndex);
296+
}
297+
else
298+
{
299+
mFinishedIndexBucketsWork = true;
300+
}
301+
302+
mSpawnedIndexBucketsWork = true;
303+
}
304+
305+
if (!mFinishedIndexBucketsWork)
306+
{
307+
auto status = checkChildrenStatus();
308+
if (status == BasicWork::State::WORK_SUCCESS)
309+
{
310+
mFinishedIndexBucketsWork = true;
311+
}
312+
else
313+
{
314+
return status;
315+
}
316+
}
317+
284318
if (!mSpawnedAssumeStateWork)
285319
{
286320
if (mApp.getLedgerManager().rebuildingInMemoryState() && !mDelayChecked)
@@ -319,8 +353,8 @@ ApplyBucketsWork::doWork()
319353
return State::WORK_RUNNING;
320354
}
321355
mApp.getInvariantManager().checkOnBucketApply(
322-
mFirstBucket, mApplyState.currentLedger, mLevel, false,
323-
mEntryTypeFilter);
356+
mFirstBucket, mApplyState.currentLedger, mLevel,
357+
/*isCurr=*/mOffersOnly, mEntryTypeFilter);
324358
mFirstBucketApplicator.reset();
325359
mFirstBucket.reset();
326360
mApp.getCatchupManager().bucketsApplied();
@@ -335,8 +369,8 @@ ApplyBucketsWork::doWork()
335369
return State::WORK_RUNNING;
336370
}
337371
mApp.getInvariantManager().checkOnBucketApply(
338-
mSecondBucket, mApplyState.currentLedger, mLevel, true,
339-
mEntryTypeFilter);
372+
mSecondBucket, mApplyState.currentLedger, mLevel,
373+
/*isCurr=*/!mOffersOnly, mEntryTypeFilter);
340374
mSecondBucketApplicator.reset();
341375
mSecondBucket.reset();
342376
mApp.getCatchupManager().bucketsApplied();
@@ -412,7 +446,9 @@ ApplyBucketsWork::isLevelComplete()
412446
std::string
413447
ApplyBucketsWork::getStatus() const
414448
{
415-
if (!mSpawnedAssumeStateWork)
449+
// This status string only applies to step 2 when we actually apply the
450+
// buckets.
451+
if (mFinishedIndexBucketsWork && !mSpawnedAssumeStateWork)
416452
{
417453
auto size = mTotalSize == 0 ? 0 : (100 * mAppliedSize / mTotalSize);
418454
return fmt::format(

src/catchup/ApplyBucketsWork.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ApplyBucketsWork : public Work
2424

2525
bool mApplying{false};
2626
bool mSpawnedAssumeStateWork{false};
27+
bool mSpawnedIndexBucketsWork{false};
28+
bool mFinishedIndexBucketsWork{false};
2729
size_t mTotalBuckets{0};
2830
size_t mAppliedBuckets{0};
2931
size_t mAppliedEntries{0};
@@ -40,11 +42,12 @@ class ApplyBucketsWork : public Work
4042
std::unique_ptr<BucketApplicator> mFirstBucketApplicator;
4143
std::unique_ptr<BucketApplicator> mSecondBucketApplicator;
4244
UnorderedSet<LedgerKey> mSeenKeys;
45+
std::vector<std::shared_ptr<Bucket>> mBucketsToIndex;
4346

4447
BucketApplicator::Counters mCounters;
4548

4649
void advance(std::string const& name, BucketApplicator& applicator);
47-
std::shared_ptr<Bucket const> getBucket(std::string const& bucketHash);
50+
std::shared_ptr<Bucket> getBucket(std::string const& bucketHash);
4851
BucketLevel& getBucketLevel(uint32_t level);
4952
void startLevel();
5053
bool isLevelComplete();

0 commit comments

Comments
 (0)