Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reusable module cache #4621

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/stellar-core_example.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ CATCHUP_RECENT=0
# merging and vertification.
WORKER_THREADS=11

# COMPILATION_THREADS (integer) default 6
# Number of threads launched temporarily when compiling contracts at
# startup. These are short lived, CPU-bound threads that are not
# in competition with the worker threads.
COMPILATION_THREADS=6

# QUORUM_INTERSECTION_CHECKER (boolean) default true
# Enable/disable computation of quorum intersection monitoring
QUORUM_INTERSECTION_CHECKER=true
Expand Down
6 changes: 6 additions & 0 deletions src/bucket/BucketBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ BucketBase::getOfferRange() const
return getIndex().getOfferRange();
}

std::optional<std::pair<std::streamoff, std::streamoff>>
BucketBase::getContractCodeRange() const
{
return getIndex().getContractCodeRange();
}

void
BucketBase::setIndex(std::unique_ptr<BucketIndex const>&& index)
{
Expand Down
5 changes: 5 additions & 0 deletions src/bucket/BucketBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class BucketBase : public NonMovableOrCopyable
std::optional<std::pair<std::streamoff, std::streamoff>>
getOfferRange() const;

// Returns [lowerBound, upperBound) of file offsets for all contract code
// entries in the bucket, or std::nullopt if no contract code exists
std::optional<std::pair<std::streamoff, std::streamoff>>
getContractCodeRange() const;

// Sets index, throws if index is already set
void setIndex(std::unique_ptr<BucketIndex const>&& index);

Expand Down
5 changes: 5 additions & 0 deletions src/bucket/BucketIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class BucketIndex : public NonMovableOrCopyable
virtual std::optional<std::pair<std::streamoff, std::streamoff>>
getOfferRange() const = 0;

// Returns lower bound and upper bound for contract code entry positions in
// the given bucket, or std::nullopt if no contract code entries exist
virtual std::optional<std::pair<std::streamoff, std::streamoff>>
getContractCodeRange() const = 0;

// Returns page size for index. InidividualIndex returns 0 for page size
virtual std::streamoff getPageSize() const = 0;

Expand Down
14 changes: 14 additions & 0 deletions src/bucket/BucketIndexImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,20 @@ BucketIndexImpl<IndexT>::getOfferRange() const
return getOffsetBounds(lowerBound, upperBound);
}

template <class IndexT>
std::optional<std::pair<std::streamoff, std::streamoff>>
BucketIndexImpl<IndexT>::getContractCodeRange() const
{
// Get the smallest and largest possible contract code keys
LedgerKey lowerBound(CONTRACT_CODE);
lowerBound.contractCode().hash.fill(std::numeric_limits<uint8_t>::min());

LedgerKey upperBound(CONTRACT_CODE);
upperBound.contractCode().hash.fill(std::numeric_limits<uint8_t>::max());

return getOffsetBounds(lowerBound, upperBound);
}

#ifdef BUILD_TESTS
template <class IndexT>
bool
Expand Down
3 changes: 3 additions & 0 deletions src/bucket/BucketIndexImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ template <class IndexT> class BucketIndexImpl : public BucketIndex
virtual std::optional<std::pair<std::streamoff, std::streamoff>>
getOfferRange() const override;

virtual std::optional<std::pair<std::streamoff, std::streamoff>>
getContractCodeRange() const override;

virtual std::streamoff
getPageSize() const override
{
Expand Down
37 changes: 37 additions & 0 deletions src/bucket/BucketSnapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,43 @@ LiveBucketSnapshot::scanForEviction(
return Loop::INCOMPLETE;
}

// Scans contract code entries in the bucket.
Loop
LiveBucketSnapshot::scanForContractCode(
std::function<Loop(BucketEntry const&)> callback) const
{
ZoneScoped;
if (isEmpty())
{
return Loop::INCOMPLETE;
}

auto range = mBucket->getContractCodeRange();
if (!range)
{
return Loop::INCOMPLETE;
}

auto& stream = getStream();
stream.seek(range->first);

BucketEntry be;
while (stream.pos() < range->second && stream.readOne(be))
{

if (((be.type() == LIVEENTRY || be.type() == INITENTRY) &&
be.liveEntry().data.type() == CONTRACT_CODE) ||
(be.type() == DEADENTRY && be.deadEntry().type() == CONTRACT_CODE))
{
if (callback(be) == Loop::COMPLETE)
{
return Loop::COMPLETE;
}
}
}
return Loop::INCOMPLETE;
}

template <class BucketT>
XDRInputFileStream&
BucketSnapshotBase<BucketT>::getStream() const
Expand Down
4 changes: 4 additions & 0 deletions src/bucket/BucketSnapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class LiveBucketSnapshot : public BucketSnapshotBase<LiveBucket>
std::list<EvictionResultEntry>& evictableKeys,
SearchableLiveBucketListSnapshot const& bl,
uint32_t ledgerVers) const;

// Scans contract code entries in the bucket.
Loop
scanForContractCode(std::function<Loop(BucketEntry const&)> callback) const;
};

class HotArchiveBucketSnapshot : public BucketSnapshotBase<HotArchiveBucket>
Expand Down
12 changes: 12 additions & 0 deletions src/bucket/SearchableBucketList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ SearchableLiveBucketListSnapshot::scanForEviction(
return result;
}

void
SearchableLiveBucketListSnapshot::scanForContractCode(
std::function<Loop(BucketEntry const&)> callback) const
{
ZoneScoped;
releaseAssert(mSnapshot);
auto f = [&callback](auto const& b) {
return b.scanForContractCode(callback);
};
loopAllBuckets(f, *mSnapshot);
}

template <class BucketT>
std::optional<std::vector<typename BucketT::LoadT>>
SearchableBucketListSnapshotBase<BucketT>::loadKeysInternal(
Expand Down
3 changes: 3 additions & 0 deletions src/bucket/SearchableBucketList.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class SearchableLiveBucketListSnapshot
std::shared_ptr<EvictionStatistics> stats,
StateArchivalSettings const& sas, uint32_t ledgerVers) const;

void
scanForContractCode(std::function<Loop(BucketEntry const&)> callback) const;

friend SearchableSnapshotConstPtr
BucketSnapshotManager::copySearchableLiveBucketListSnapshot() const;
};
Expand Down
5 changes: 5 additions & 0 deletions src/catchup/AssumeStateWork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "crypto/Hex.h"
#include "history/HistoryArchive.h"
#include "invariant/InvariantManager.h"
#include "ledger/LedgerManager.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This should be no longer necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

#include "work/WorkSequence.h"
#include "work/WorkWithCallback.h"

Expand Down Expand Up @@ -78,6 +79,10 @@ AssumeStateWork::doWork()
// now referenced by BucketList
buckets.clear();

// Compile all live BL entries once we assume a new state.
app.getLedgerManager().compileAllContractsInLedger(
maxProtocolVersion);

// Check invariants after state has been assumed
app.getInvariantManager().checkAfterAssumeState(has.currentLedger);

Expand Down
7 changes: 7 additions & 0 deletions src/ledger/LedgerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "catchup/LedgerApplyManager.h"
#include "history/HistoryManager.h"
#include "ledger/NetworkConfig.h"
#include "rust/RustBridge.h"
#include <memory>

namespace stellar
Expand Down Expand Up @@ -200,6 +201,12 @@ class LedgerManager
virtual void manuallyAdvanceLedgerHeader(LedgerHeader const& header) = 0;

virtual SorobanMetrics& getSorobanMetrics() = 0;
virtual rust_bridge::SorobanModuleCache& getModuleCache() = 0;

// Compiles all contracts in the current ledger, for ledger protocols
// starting at minLedgerVersion and running through to
// Config::CURRENT_LEDGER_PROTOCOL_VERSION (to enable upgrades).
virtual void compileAllContractsInLedger(uint32_t minLedgerVersion) = 0;

virtual ~LedgerManager()
{
Expand Down
98 changes: 98 additions & 0 deletions src/ledger/LedgerManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
#include "history/HistoryManager.h"
#include "ledger/FlushAndRotateMetaDebugWork.h"
#include "ledger/LedgerHeaderUtils.h"
#include "ledger/LedgerManager.h"
#include "ledger/LedgerTxn.h"
#include "ledger/LedgerTxnEntry.h"
#include "ledger/LedgerTxnHeader.h"
#include "ledger/SharedModuleCacheCompiler.h"
#include "main/Application.h"
#include "main/Config.h"
#include "main/ErrorMessages.h"
#include "rust/RustBridge.h"
#include "transactions/MutableTransactionResult.h"
#include "transactions/OperationFrame.h"
#include "transactions/TransactionFrameBase.h"
Expand All @@ -41,8 +44,10 @@
#include "util/XDRCereal.h"
#include "util/XDRStream.h"
#include "work/WorkScheduler.h"
#include "xdr/Stellar-ledger-entries.h"
#include "xdrpp/printer.h"

#include <cstdint>
#include <fmt/format.h>

#include "xdr/Stellar-ledger-entries.h"
Expand Down Expand Up @@ -125,6 +130,27 @@ LedgerManager::ledgerAbbrev(LedgerHeaderHistoryEntry const& he)
return ledgerAbbrev(he.header, he.hash);
}

static std::vector<uint32_t>
getModuleCacheProtocols()
{
std::vector<uint32_t> ledgerVersions;
for (uint32_t i = (uint32_t)REUSABLE_SOROBAN_MODULE_CACHE_PROTOCOL_VERSION;
i <= Config::CURRENT_LEDGER_PROTOCOL_VERSION; i++)
{
ledgerVersions.push_back(i);
}
auto extra = getenv("SOROBAN_TEST_EXTRA_PROTOCOL");
if (extra)
{
uint32_t proto = static_cast<uint32_t>(atoi(extra));
if (proto > 0)
{
ledgerVersions.push_back(proto);
}
}
return ledgerVersions;
}

LedgerManagerImpl::LedgerManagerImpl(Application& app)
: mApp(app)
, mSorobanMetrics(app.getMetrics())
Expand Down Expand Up @@ -157,6 +183,8 @@ LedgerManagerImpl::LedgerManagerImpl(Application& app)
, mCatchupDuration(
app.getMetrics().NewTimer({"ledger", "catchup", "duration"}))
, mState(LM_BOOTING_STATE)
, mModuleCache(::rust_bridge::new_module_cache())
, mModuleCacheProtocols(getModuleCacheProtocols())

{
setupLedgerCloseMetaStream();
Expand Down Expand Up @@ -405,6 +433,9 @@ LedgerManagerImpl::loadLastKnownLedger(bool restoreBucketlist)
updateNetworkConfig(ltx);
mSorobanNetworkConfigReadOnly = mSorobanNetworkConfigForApply;
}

// Prime module cache with ledger content.
compileAllContractsInLedger(latestLedgerHeader->ledgerVersion);
}

Database&
Expand Down Expand Up @@ -568,6 +599,30 @@ LedgerManagerImpl::getSorobanMetrics()
return mSorobanMetrics;
}

rust_bridge::SorobanModuleCache&
LedgerManagerImpl::getModuleCache()
{
return *mModuleCache;
}

void
LedgerManagerImpl::compileAllContractsInLedger(uint32_t minLedgerVersion)
{
auto& moduleCache = getModuleCache();
moduleCache.clear();
std::vector<uint32_t> versions;
for (auto const& v : mModuleCacheProtocols)
{
if (v >= minLedgerVersion)
{
versions.push_back(v);
}
}
auto compiler = std::make_shared<SharedModuleCacheCompiler>(
mApp, moduleCache, versions);
compiler->run();
}

void
LedgerManagerImpl::publishSorobanMetrics()
{
Expand Down Expand Up @@ -1812,6 +1867,8 @@ LedgerManagerImpl::transferLedgerEntriesToBucketList(
ledgerCloseMeta->populateEvictedEntries(evictedState);
}

evictFromModuleCache(lh.ledgerVersion, evictedState);

ltxEvictions.commit();
}

Expand All @@ -1822,6 +1879,8 @@ LedgerManagerImpl::transferLedgerEntriesToBucketList(
ltx.getAllEntries(initEntries, liveEntries, deadEntries);
if (blEnabled)
{
addAnyContractsToModuleCache(lh.ledgerVersion, initEntries);
dmkozh marked this conversation as resolved.
Show resolved Hide resolved
addAnyContractsToModuleCache(lh.ledgerVersion, liveEntries);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this expected to be a no-op now, or do we have the restored entries in liveEntries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know -- I figured it'd be fairly future-proof to just assume both live and init might carry contracts. @SirTyson do you know off hand?

mApp.getBucketManager().addLiveBatch(mApp, lh, initEntries, liveEntries,
deadEntries);
}
Expand Down Expand Up @@ -1882,4 +1941,43 @@ LedgerManagerImpl::ledgerClosed(

return res;
}

void
LedgerManagerImpl::evictFromModuleCache(uint32_t ledgerVersion,
EvictedStateVectors const& evictedState)
{
::rust::Vec<CxxBuf> contractCodeKeys;
for (auto const& entry : evictedState.archivedEntries)
{
if (entry.data.type() == CONTRACT_CODE)
{
Hash const& hash = entry.data.contractCode().hash;
::rust::Slice<uint8_t const> slice{hash.data(), hash.size()};
mModuleCache->evict_contract_code(slice);
}
}
}

void
LedgerManagerImpl::addAnyContractsToModuleCache(
uint32_t ledgerVersion, std::vector<LedgerEntry> const& le)
{
for (auto const& e : le)
{
if (e.data.type() == CONTRACT_CODE)
{
for (auto const& v : mModuleCacheProtocols)
{
if (v >= ledgerVersion)
{
auto const& wasm = e.data.contractCode().code;
auto slice =
rust::Slice<const uint8_t>(wasm.data(), wasm.size());
mModuleCache->compile(v, slice);
}
}
}
}
}

}
Loading
Loading