Skip to content

Commit 69ca558

Browse files
committed
Fixes after rebase
1 parent 7fc85f8 commit 69ca558

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

src/bucket/BucketUtils.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,10 @@ EvictionResultCandidates::isValid(uint32_t currLedgerSeq,
147147
// affect evictions scans.
148148
if (protocolVersionIsBefore(
149149
initialLedgerVers,
150-
BucketBase::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION) &&
150+
LiveBucket::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION) &&
151151
protocolVersionStartsFrom(
152152
currLedgerVers,
153-
BucketBase::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION))
153+
LiveBucket::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION))
154154
{
155155
return false;
156156
}

src/history/test/HistoryTests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1250,10 +1250,10 @@ TEST_CASE("Catchup with protocol upgrade", "[catchup][history]")
12501250
{
12511251
if (protocolVersionEquals(
12521252
Config::CURRENT_LEDGER_PROTOCOL_VERSION,
1253-
BucketBase::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION))
1253+
LiveBucket::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION))
12541254
{
12551255
testUpgrade(
1256-
BucketBase::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION);
1256+
LiveBucket::FIRST_PROTOCOL_SUPPORTING_PERSISTENT_EVICTION);
12571257
}
12581258
}
12591259
}

src/historywork/DownloadBucketsWork.cpp

+32-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "historywork/GetAndUnzipRemoteFileWork.h"
1212
#include "historywork/VerifyBucketWork.h"
1313
#include "work/WorkWithCallback.h"
14-
#include "xdr/Stellar-contract-config-setting.h"
1514
#include <Tracy.hpp>
1615
#include <fmt/format.h>
1716

@@ -73,13 +72,13 @@ DownloadBucketsWork::resetIter()
7372
mNextHotBucketIter = mHotHashes.begin();
7473
}
7574

76-
template<typename BucketT>
75+
template <typename BucketT>
7776
bool
7877
DownloadBucketsWork::onSuccessCb(
7978
Application& app, FileTransferInfo const& ft, std::string const& hash,
80-
int currId,
81-
std::map<std::string, std::shared_ptr<BucketT>>& buckets,
82-
std::map<int, std::unique_ptr<typename BucketT::IndexT const>>& indexMap)
79+
int currId, std::map<std::string, std::shared_ptr<BucketT>>& buckets,
80+
std::map<int, std::unique_ptr<typename BucketT::IndexT const>>& indexMap,
81+
std::lock_guard<std::mutex> const& indexMapLock)
8382
{
8483
auto bucketPath = ft.localPath_nogz();
8584
auto indexIter = indexMap.find(currId);
@@ -140,29 +139,47 @@ DownloadBucketsWork::yieldMoreWork()
140139
if (isHotHash)
141140
{
142141
auto currId = mHotIndexId++;
142+
mHotIndexMapMutex.lock();
143143
auto [indexIter, inserted] = mHotIndexMap.emplace(currId, nullptr);
144+
mHotIndexMapMutex.unlock();
144145
releaseAssertOrThrow(inserted);
145146
verifyWork = std::make_shared<VerifyBucketWork<HotArchiveBucket>>(mApp, ft.localPath_nogz(),
146147
hexToBin256(hash),
147148
indexIter->second, failureCb);
148-
adoptBucketCb = [this, &ft, hash, currId](Application& app) {
149-
return onSuccessCb<HotArchiveBucket>(app, ft, hash, currId,
150-
mHotBuckets, mHotIndexMap);
149+
adoptBucketCb = [weakSelf, ft, hash, currId](Application& app) {
150+
auto self = weakSelf.lock();
151+
if (self)
152+
{
153+
std::lock_guard lock(self->mHotIndexMapMutex);
154+
return onSuccessCb<HotArchiveBucket>(app, ft, hash, currId,
155+
self->mHotBuckets,
156+
self->mHotIndexMap, lock);
157+
}
158+
return true;
151159
};
152160

153161
mNextHotBucketIter++;
154162
}
155163
else
156164
{
157165
auto currId = mLiveIndexId++;
166+
mLiveIndexMapMutex.lock();
158167
auto [indexIter, inserted] = mLiveIndexMap.emplace(currId, nullptr);
168+
mLiveIndexMapMutex.unlock();
159169
releaseAssertOrThrow(inserted);
160170
verifyWork = std::make_shared<VerifyBucketWork<LiveBucket>>(mApp, ft.localPath_nogz(),
161171
hexToBin256(hash),
162172
indexIter->second, failureCb);
163-
adoptBucketCb = [this, &ft, hash, currId](Application& app) {
164-
return onSuccessCb<LiveBucket>(app, ft, hash, currId,
165-
mLiveBuckets, mLiveIndexMap);
173+
adoptBucketCb = [weakSelf, ft, hash, currId](Application& app) {
174+
auto self = weakSelf.lock();
175+
if (self)
176+
{
177+
std::lock_guard lock(self->mLiveIndexMapMutex);
178+
return onSuccessCb<LiveBucket>(app, ft, hash, currId,
179+
self->mLiveBuckets,
180+
self->mLiveIndexMap, lock);
181+
}
182+
return true;
166183
};
167184

168185
mNextLiveBucketIter++;
@@ -181,10 +198,12 @@ DownloadBucketsWork::yieldMoreWork()
181198
template bool DownloadBucketsWork::onSuccessCb<LiveBucket>(
182199
Application&, FileTransferInfo const&, std::string const&, int,
183200
std::map<std::string, std::shared_ptr<LiveBucket>>&,
184-
std::map<int, std::unique_ptr<LiveBucketIndex const>>&);
201+
std::map<int, std::unique_ptr<LiveBucketIndex const>>&,
202+
std::lock_guard<std::mutex> const&);
185203

186204
template bool DownloadBucketsWork::onSuccessCb<HotArchiveBucket>(
187205
Application&, FileTransferInfo const&, std::string const&, int,
188206
std::map<std::string, std::shared_ptr<HotArchiveBucket>>&,
189-
std::map<int, std::unique_ptr<HotArchiveBucketIndex const>>&);
207+
std::map<int, std::unique_ptr<HotArchiveBucketIndex const>>&,
208+
std::lock_guard<std::mutex> const&);
190209
}

src/historywork/DownloadBucketsWork.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ class DownloadBucketsWork : public BatchWork
2727
TmpDir const& mDownloadDir;
2828
std::shared_ptr<HistoryArchive> mArchive;
2929

30-
// Store indexes of downloaded buckets
30+
// Store indexes of downloaded buckets. Child processes will actually create
31+
// the indexes, but DownloadBucketsWork needs to maintain actual ownership
32+
// of the pointers so that the success callback can pass them to the
33+
// BucketManager. Must be protected by a mutex to avoid race conditions.
3134
std::map<int, std::unique_ptr<LiveBucketIndex const>> mLiveIndexMap;
3235
std::map<int, std::unique_ptr<HotArchiveBucketIndex const>> mHotIndexMap;
36+
std::mutex mLiveIndexMapMutex;
37+
std::mutex mHotIndexMapMutex;
3338
int mLiveIndexId{0};
3439
int mHotIndexId{0};
3540

@@ -39,7 +44,8 @@ class DownloadBucketsWork : public BatchWork
3944
std::string const& hash, int currId,
4045
std::map<std::string, std::shared_ptr<BucketT>>& buckets,
4146
std::map<int, std::unique_ptr<typename BucketT::IndexT const>>&
42-
indexMap);
47+
indexMap,
48+
std::lock_guard<std::mutex> const& indexMapLock);
4349

4450
public:
4551
// Note: hashes must contain both live and hot archive bucket hashes

0 commit comments

Comments
 (0)