Skip to content

Commit a89cb68

Browse files
committed
Some cleanup post-rebase, refresh comments
1 parent 575063d commit a89cb68

17 files changed

+78
-61
lines changed

src/bucket/BucketManager.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ledger/LedgerManager.h"
1919
#include "ledger/LedgerTxn.h"
2020
#include "ledger/LedgerTypeUtils.h"
21+
#include "ledger/NetworkConfig.h"
2122
#include "main/Application.h"
2223
#include "main/Config.h"
2324
#include "util/Fs.h"
@@ -1080,7 +1081,7 @@ EvictedStateVectors
10801081
BucketManager::resolveBackgroundEvictionScan(
10811082
AbstractLedgerTxn& ltx, uint32_t ledgerSeq,
10821083
LedgerKeySet const& modifiedKeys, uint32_t ledgerVers,
1083-
SorobanNetworkConfig& networkConfig)
1084+
SorobanNetworkConfig const& networkConfig)
10841085
{
10851086
ZoneScoped;
10861087
releaseAssert(mEvictionStatistics);

src/bucket/BucketManager.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class BucketSnapshotManager;
3636
class SearchableLiveBucketListSnapshot;
3737
struct BucketEntryCounters;
3838
enum class LedgerEntryTypeAndDurability : uint32_t;
39+
class SorobanNetworkConfig;
3940

4041
struct HistoryArchiveState;
4142

@@ -307,7 +308,7 @@ class BucketManager : NonMovableOrCopyable
307308
resolveBackgroundEvictionScan(AbstractLedgerTxn& ltx, uint32_t ledgerSeq,
308309
LedgerKeySet const& modifiedKeys,
309310
uint32_t ledgerVers,
310-
SorobanNetworkConfig& networkConfig);
311+
SorobanNetworkConfig const& networkConfig);
311312

312313
medida::Meter& getBloomMissMeter() const;
313314
medida::Meter& getBloomLookupMeter() const;

src/bucket/LiveBucketList.h

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
namespace stellar
1111
{
1212

13-
class SorobanNetworkConfig;
14-
1513
// The LiveBucketList stores the current canonical state of the ledger. It is
1614
// made up of LiveBucket buckets, which in turn store individual entries of type
1715
// BucketEntry. When an entry is "evicted" from the ledger, it is removed from

src/bucket/test/BucketListTests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ TEST_CASE_VERSIONS("network config snapshots BucketList size", "[bucketlist]")
869869
LedgerManagerForBucketTests& lm = app->getLedgerManager();
870870

871871
auto& networkConfig =
872-
app->getLedgerManager().getMutableSorobanNetworkConfig();
872+
app->getLedgerManager().getSorobanNetworkConfigReadOnly();
873873

874874
uint32_t windowSize = networkConfig.stateArchivalSettings()
875875
.bucketListSizeWindowSampleSize;

src/bucket/test/BucketTestUtils.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ closeLedger(Application& app, std::optional<SecretKey> skToSignValue,
101101
app.getHerder().externalizeValue(TxSetXDRFrame::makeEmpty(lcl), ledgerNum,
102102
lcl.header.scpValue.closeTime, upgrades,
103103
skToSignValue);
104-
testutil::crankUntil(
105-
app,
106-
[&lm, ledgerNum]() { return lm.getLastClosedLedgerNum() == ledgerNum; },
107-
std::chrono::seconds(10));
108104
return lm.getLastClosedLedgerHeader().hash;
109105
}
110106

@@ -238,7 +234,7 @@ LedgerManagerForBucketTests::transferLedgerEntriesToBucketList(
238234
mApp.getBucketManager().resolveBackgroundEvictionScan(
239235
ltxEvictions, lh.ledgerSeq, keys, initialLedgerVers,
240236
mApp.getLedgerManager()
241-
.getMutableSorobanNetworkConfig());
237+
.getSorobanNetworkConfigForApply());
242238

243239
if (protocolVersionStartsFrom(
244240
initialLedgerVers,

src/catchup/CatchupWork.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ CatchupWork::runCatchupStep()
524524
// In this case we should actually have been caught-up during
525525
// the replay process and, if judged successful, our LCL should
526526
// be the one provided as well.
527-
auto lastClosed =
527+
auto& lastClosed =
528528
mApp.getLedgerManager().getLastClosedLedgerHeader();
529529
releaseAssert(mLastApplied.hash == lastClosed.hash);
530530
releaseAssert(mLastApplied.header == lastClosed.header);

src/database/Database.h

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@ class Database : NonMovableOrCopyable
141141
// Save `vers` as schema version.
142142
void putSchemaVersion(unsigned long vers);
143143

144+
// Prepared statements cache may be accessed by mutliple threads (each using
145+
// a different session), so use a mutex to synchronize access.
144146
std::mutex mutable mStatementsMutex;
145147

146148
public:

src/herder/HerderImpl.cpp

+16-3
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,17 @@ HerderImpl::valueExternalized(uint64 slotIndex, StellarValue const& value,
480480

481481
// Check to see if quorums have changed and we need to reanalyze.
482482
checkAndMaybeReanalyzeQuorumMap();
483+
484+
// heart beat *after* doing all the work (ensures that we do not include
485+
// the overhead of externalization in the way we track SCP)
486+
// Note: this only makes sense in the context of synchronous ledger
487+
// application on the main thread.
488+
if (!mApp.getConfig().parallelLedgerClose())
489+
{
490+
// heart beat *after* doing all the work (ensures that we do not
491+
// include the overhead of externalization in the way we track SCP)
492+
trackingHeartBeat();
493+
}
483494
}
484495
else
485496
{
@@ -1156,7 +1167,7 @@ HerderImpl::lastClosedLedgerIncreased(bool latest, TxSetXDRFrameConstPtr txSet)
11561167
{
11571168
// Re-start heartbeat tracking _after_ applying the most up-to-date
11581169
// ledger. This guarantees out-of-sync timer won't fire while we have
1159-
// ledgers to apply.
1170+
// ledgers to apply (applicable during parallel ledger close).
11601171
trackingHeartBeat();
11611172

11621173
// Ensure out of sync recovery did not get triggered while we were
@@ -1369,6 +1380,8 @@ HerderImpl::triggerNextLedger(uint32_t ledgerSeqToTrigger,
13691380
// If applying, the next ledger will trigger voting
13701381
if (mLedgerManager.isApplying())
13711382
{
1383+
// This can only happen when closing ledgers in parallel
1384+
releaseAssert(mApp.getConfig().parallelLedgerClose());
13721385
CLOG_DEBUG(Herder, "triggerNextLedger: skipping (applying) : {}",
13731386
mApp.getStateHuman());
13741387
return;
@@ -1560,7 +1573,7 @@ HerderImpl::getUpgradesJson()
15601573
void
15611574
HerderImpl::forceSCPStateIntoSyncWithLastClosedLedger()
15621575
{
1563-
auto header = mLedgerManager.getLastClosedLedgerHeader().header;
1576+
auto const& header = mLedgerManager.getLastClosedLedgerHeader().header;
15641577
setTrackingSCPState(header.ledgerSeq, header.scpValue,
15651578
/* isTrackingNetwork */ true);
15661579
}
@@ -2360,7 +2373,7 @@ HerderImpl::herderOutOfSync()
23602373
// are no ledgers queued to be applied. If there are ledgers queued, it's
23612374
// possible the rest of the network is waiting for this node to vote. In
23622375
// this case we should _still_ remain in tracking and emit nomination; If
2363-
// the nodes does not hear anything from the network after that, then node
2376+
// the node does not hear anything from the network after that, then node
23642377
// can go into out of sync recovery.
23652378
releaseAssert(threadIsMain());
23662379
releaseAssert(!mLedgerManager.isApplying());

src/herder/HerderSCPDriver.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -221,15 +221,15 @@ HerderSCPDriver::validateValueHelper(uint64_t slotIndex, StellarValue const& b,
221221
}
222222
}
223223

224-
auto lhhe = mLedgerManager.getLastClosedLedgerHeader();
224+
auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
225225
// when checking close time, start with what we have locally
226-
lastCloseTime = lhhe.header.scpValue.closeTime;
226+
lastCloseTime = lcl.header.scpValue.closeTime;
227227

228228
// if this value is not for our local state,
229229
// perform as many checks as we can
230-
if (slotIndex != (lhhe.header.ledgerSeq + 1))
230+
if (slotIndex != (lcl.header.ledgerSeq + 1))
231231
{
232-
if (slotIndex == lhhe.header.ledgerSeq)
232+
if (slotIndex == lcl.header.ledgerSeq)
233233
{
234234
// previous ledger
235235
if (b.closeTime != lastCloseTime)
@@ -240,7 +240,7 @@ HerderSCPDriver::validateValueHelper(uint64_t slotIndex, StellarValue const& b,
240240
return SCPDriver::kInvalidValue;
241241
}
242242
}
243-
else if (slotIndex < lhhe.header.ledgerSeq)
243+
else if (slotIndex < lcl.header.ledgerSeq)
244244
{
245245
// basic sanity check on older value
246246
if (b.closeTime >= lastCloseTime)
@@ -323,7 +323,7 @@ HerderSCPDriver::validateValueHelper(uint64_t slotIndex, StellarValue const& b,
323323

324324
res = SCPDriver::kInvalidValue;
325325
}
326-
else if (!checkAndCacheTxSetValid(*txSet, lhhe, closeTimeOffset))
326+
else if (!checkAndCacheTxSetValid(*txSet, lcl, closeTimeOffset))
327327
{
328328
CLOG_DEBUG(Herder,
329329
"HerderSCPDriver::validateValue i: {} invalid txSet {}",
@@ -614,6 +614,7 @@ HerderSCPDriver::combineCandidates(uint64_t slotIndex,
614614
std::set<TransactionFramePtr> aggSet;
615615

616616
releaseAssert(!mLedgerManager.isApplying());
617+
releaseAssert(threadIsMain());
617618
auto const& lcl = mLedgerManager.getLastClosedLedgerHeader();
618619

619620
Hash candidatesHash;

src/herder/TxSetFrame.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ phaseTxsAreValid(TxSetTransactions const& phase, Application& app,
233233
uint64_t upperBoundCloseTimeOffset)
234234
{
235235
ZoneScoped;
236+
releaseAssert(threadIsMain());
236237
// This is done so minSeqLedgerGap is validated against the next
237238
// ledgerSeq, which is what will be used at apply time
238239

src/invariant/BucketListIsConsistentWithDatabase.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "bucket/BucketManager.h"
88
#include "bucket/LiveBucket.h"
99
#include "bucket/LiveBucketList.h"
10-
#include "crypto/Hex.h"
1110
#include "database/Database.h"
1211
#include "history/HistoryArchive.h"
1312
#include "invariant/InvariantManager.h"

src/ledger/LedgerManagerImpl.cpp

+29-17
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ LedgerManagerImpl::startNewLedger(LedgerHeader const& genesisLedger)
252252
CLOG_INFO(Ledger, "Root account seed: {}", skey.getStrKeySeed().value);
253253
auto output =
254254
ledgerClosed(ltx, /*ledgerCloseMeta*/ nullptr, /*initialLedgerVers*/ 0);
255-
updateCurrentLedgerState(output);
255+
advanceLedgerPointers(output);
256256

257257
ltx.commit();
258258
}
@@ -385,8 +385,8 @@ LedgerManagerImpl::loadLastKnownLedger(bool restoreBucketlist)
385385
}
386386

387387
// Step 4. Restore LedgerManager's internal state
388-
auto output = advanceLedgerPointers(*latestLedgerHeader, has);
389-
updateCurrentLedgerState(output);
388+
auto output = advanceLedgerStateSnapshot(*latestLedgerHeader, has);
389+
advanceLedgerPointers(output);
390390

391391
// Maybe truncate checkpoint files if we're restarting after a crash
392392
// in closeLedger (in which case any modifications to the ledger state have
@@ -766,7 +766,10 @@ LedgerManagerImpl::ledgerCloseComplete(uint32_t lcl, bool calledViaExternalize,
766766
releaseAssert(latestQueuedToApply <= latestHeardFromNetwork);
767767
}
768768

769-
if (lcl == latestQueuedToApply)
769+
// Without parallel ledger close, this should always be true
770+
bool doneApplying = lcl == latestQueuedToApply;
771+
releaseAssert(doneApplying || mApp.getConfig().parallelLedgerClose());
772+
if (doneApplying)
770773
{
771774
mCurrentlyApplyingLedger = false;
772775
}
@@ -816,8 +819,17 @@ LedgerManagerImpl::closeLedger(LedgerCloseData const& ledgerData,
816819

817820
LedgerTxn ltx(mApp.getLedgerTxnRoot());
818821
auto header = ltx.loadHeader();
819-
auto prevHeader =
820-
threadIsMain() ? getLastClosedLedgerHeader().header : header.current();
822+
// Note: closeLedger should be able to work correctly based on ledger header
823+
// stored in LedgerTxn. The issue is that in tests LedgerTxn is sometimes
824+
// modified manually, which changes ledger header hash compared to the
825+
// cached one and causes tests to fail.
826+
LedgerHeader prevHeader = header.current();
827+
#ifdef BUILD_TESTS
828+
if (threadIsMain())
829+
{
830+
prevHeader = getLastClosedLedgerHeader().header;
831+
}
832+
#endif
821833
auto prevHash = xdrSha256(prevHeader);
822834

823835
auto initialLedgerVers = header.current().ledgerVersion;
@@ -830,7 +842,8 @@ LedgerManagerImpl::closeLedger(LedgerCloseData const& ledgerData,
830842

831843
auto now = mApp.getClock().now();
832844
mLedgerAgeClosed.Update(now - mLastClose);
833-
// mLastClose is only accessed by a single thread
845+
// mLastClose is only accessed by a single thread, so no synchronization
846+
// needed
834847
mLastClose = now;
835848
mLedgerAge.set_count(0);
836849

@@ -1016,7 +1029,7 @@ LedgerManagerImpl::closeLedger(LedgerCloseData const& ledgerData,
10161029
emitNextMeta();
10171030
}
10181031

1019-
// The next 5 steps happen in a relatively non-obvious, subtle order.
1032+
// The next 7 steps happen in a relatively non-obvious, subtle order.
10201033
// This is unfortunate and it would be nice if we could make it not
10211034
// be so subtle, but for the time being this is where we are.
10221035
//
@@ -1075,7 +1088,7 @@ LedgerManagerImpl::closeLedger(LedgerCloseData const& ledgerData,
10751088
[this, txs, ledgerSeq, calledViaExternalize, ledgerData,
10761089
ledgerOutput = std::move(closeLedgerResult)]() mutable {
10771090
releaseAssert(threadIsMain());
1078-
updateCurrentLedgerState(ledgerOutput);
1091+
advanceLedgerPointers(ledgerOutput);
10791092

10801093
// Step 5. Maybe kick off publishing on complete checkpoint files
10811094
auto& hm = mApp.getHistoryManager();
@@ -1144,7 +1157,7 @@ LedgerManagerImpl::setLastClosedLedger(
11441157
ltx.commit();
11451158

11461159
mRebuildInMemoryState = false;
1147-
updateCurrentLedgerState(advanceLedgerPointers(lastClosed.header, has));
1160+
advanceLedgerPointers(advanceLedgerStateSnapshot(lastClosed.header, has));
11481161

11491162
LedgerTxn ltx2(mApp.getLedgerTxnRoot());
11501163
if (protocolVersionStartsFrom(ltx2.loadHeader().current().ledgerVersion,
@@ -1167,8 +1180,8 @@ LedgerManagerImpl::manuallyAdvanceLedgerHeader(LedgerHeader const& header)
11671180
has.fromString(mApp.getPersistentState().getState(
11681181
PersistentState::kHistoryArchiveState,
11691182
mApp.getDatabase().getSession()));
1170-
auto output = advanceLedgerPointers(header, has, false);
1171-
updateCurrentLedgerState(output);
1183+
auto output = advanceLedgerStateSnapshot(header, has);
1184+
advanceLedgerPointers(output);
11721185
}
11731186

11741187
void
@@ -1315,7 +1328,7 @@ LedgerManagerImpl::getCurrentLedgerStateSnaphot()
13151328
}
13161329

13171330
void
1318-
LedgerManagerImpl::updateCurrentLedgerState(CloseLedgerOutput const& output)
1331+
LedgerManagerImpl::advanceLedgerPointers(CloseLedgerOutput const& output)
13191332
{
13201333
releaseAssert(threadIsMain());
13211334
CLOG_DEBUG(
@@ -1330,9 +1343,8 @@ LedgerManagerImpl::updateCurrentLedgerState(CloseLedgerOutput const& output)
13301343
}
13311344

13321345
LedgerManagerImpl::CloseLedgerOutput
1333-
LedgerManagerImpl::advanceLedgerPointers(LedgerHeader const& header,
1334-
HistoryArchiveState const& has,
1335-
bool debugLog)
1346+
LedgerManagerImpl::advanceLedgerStateSnapshot(LedgerHeader const& header,
1347+
HistoryArchiveState const& has)
13361348
{
13371349
auto ledgerHash = xdrSha256(header);
13381350

@@ -1839,7 +1851,7 @@ LedgerManagerImpl::ledgerClosed(
18391851
mApp.getBucketManager().snapshotLedger(lh);
18401852
auto has = storeCurrentLedger(lh, /* storeHeader */ true,
18411853
/* appendToCheckpoint */ true);
1842-
res = advanceLedgerPointers(lh, has);
1854+
res = advanceLedgerStateSnapshot(lh, has);
18431855
});
18441856

18451857
return res;

src/ledger/LedgerManagerImpl.h

+14-9
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ class LedgerManagerImpl : public LedgerManager
5757
std::filesystem::path mMetaDebugPath;
5858

5959
private:
60-
// Cache LCL state, updates once a ledger (synchronized with
61-
// mLedgerStateMutex)
60+
// Cache LCL state, accessible only from main thread
6261
LedgerHeaderHistoryEntry mLastClosedLedger;
6362

6463
// Read-only Soroban network configuration, accessible by main thread only.
@@ -74,6 +73,8 @@ class LedgerManagerImpl : public LedgerManager
7473
// variable is not synchronized, since it should only be used by one thread
7574
// (main or ledger close).
7675
std::shared_ptr<SorobanNetworkConfig> mSorobanNetworkConfigForApply;
76+
77+
// Cache most recent HAS, accessible only from main thread
7778
HistoryArchiveState mLastClosedLedgerHAS;
7879

7980
SorobanMetrics mSorobanMetrics;
@@ -94,13 +95,15 @@ class LedgerManagerImpl : public LedgerManager
9495
bool mRebuildInMemoryState{false};
9596
SearchableSnapshotConstPtr mReadOnlyLedgerStateSnapshot;
9697

97-
// Use mutex to guard read access to LCL and Soroban network config
98+
// Use mutex to guard ledger state during apply
9899
mutable std::recursive_mutex mLedgerStateMutex;
99100

100101
medida::Timer& mCatchupDuration;
101102

102103
std::unique_ptr<LedgerCloseMetaFrame> mNextMetaToEmit;
103104

105+
// Use in the context of parallel ledger close to indicate background thread
106+
// is currently closing a ledger or has ledgers queued to apply.
104107
bool mCurrentlyApplyingLedger{false};
105108

106109
static std::vector<MutableTxResultPtr> processFeesSeqNums(
@@ -150,7 +153,8 @@ class LedgerManagerImpl : public LedgerManager
150153
// as the actual ledger usage.
151154
void publishSorobanMetrics();
152155

153-
void updateCurrentLedgerState(CloseLedgerOutput const& output);
156+
// Update cached ledger state values managed by this class.
157+
void advanceLedgerPointers(CloseLedgerOutput const& output);
154158

155159
protected:
156160
// initialLedgerVers must be the ledger version at the start of the ledger
@@ -166,11 +170,12 @@ class LedgerManagerImpl : public LedgerManager
166170
std::unique_ptr<LedgerCloseMetaFrame> const& ledgerCloseMeta,
167171
LedgerHeader lh, uint32_t initialLedgerVers);
168172

169-
// Update in-memory cached LCL state (this only happens at the end of ledger
170-
// close)
171-
CloseLedgerOutput advanceLedgerPointers(LedgerHeader const& header,
172-
HistoryArchiveState const& has,
173-
bool debugLog = true);
173+
// Update ledger state snapshot, and construct CloseLedgerOutput return
174+
// value, which contains all information relevant to ledger state (HAS,
175+
// ledger header, network config, bucketlist snapshot).
176+
CloseLedgerOutput
177+
advanceLedgerStateSnapshot(LedgerHeader const& header,
178+
HistoryArchiveState const& has);
174179
void logTxApplyMetrics(AbstractLedgerTxn& ltx, size_t numTxs,
175180
size_t numOps);
176181

0 commit comments

Comments
 (0)