22#include "data/BackendInterface.hpp"
23#include "data/LedgerCacheInterface.hpp"
24#include "data/Types.hpp"
25#include "etl/CacheLoaderInterface.hpp"
26#include "etl/CacheLoaderSettings.hpp"
27#include "etl/impl/CacheLoader.hpp"
28#include "etl/impl/CursorFromAccountProvider.hpp"
29#include "etl/impl/CursorFromDiffProvider.hpp"
30#include "etl/impl/CursorFromFixDiffNumProvider.hpp"
31#include "util/Assert.hpp"
32#include "util/Profiler.hpp"
33#include "util/async/context/BasicExecutionContext.hpp"
34#include "util/config/ConfigDefinition.hpp"
35#include "util/log/Logger.hpp"
54template <
typename ExecutionContextType = util::async::CoroExecutionContext>
59 std::shared_ptr<BackendInterface> backend_;
60 std::reference_wrapper<data::LedgerCacheInterface> cache_;
63 ExecutionContextType ctx_;
64 std::unique_ptr<CacheLoaderType> loader_;
76 std::shared_ptr<BackendInterface> backend,
79 : backend_{std::move(backend)}
81 , settings_{makeCacheLoaderSettings(config)}
82 , ctx_{settings_.numThreads}
95 load(uint32_t
const seq)
override
97 ASSERT(not cache_.get().isFull(),
"Cache must not be full. seq = {}", seq);
99 if (settings_.isDisabled()) {
100 cache_.get().setDisabled();
101 LOG(log_.warn()) <<
"Cache is disabled. Not loading";
105 if (loadCacheFromFile()) {
109 std::shared_ptr<impl::BaseCursorProvider> provider;
110 if (settings_.numCacheCursorsFromDiff != 0) {
111 LOG(log_.info()) <<
"Loading cache with cursor from num_cursors_from_diff="
112 << settings_.numCacheCursorsFromDiff;
113 provider = std::make_shared<impl::CursorFromDiffProvider>(backend_, settings_.numCacheCursorsFromDiff);
114 }
else if (settings_.numCacheCursorsFromAccount != 0) {
115 LOG(log_.info()) <<
"Loading cache with cursor from num_cursors_from_account="
116 << settings_.numCacheCursorsFromAccount;
117 provider = std::make_shared<impl::CursorFromAccountProvider>(
118 backend_, settings_.numCacheCursorsFromAccount, settings_.cachePageFetchSize
121 LOG(log_.info()) <<
"Loading cache with cursor from num_diffs=" << settings_.numCacheDiffs;
122 provider = std::make_shared<impl::CursorFromFixDiffNumProvider>(backend_, settings_.numCacheDiffs);
125 loader_ = std::make_unique<CacheLoaderType>(
130 settings_.numCacheMarkers,
131 settings_.cachePageFetchSize,
132 provider->getCursors(seq)
135 if (settings_.isSync()) {
137 ASSERT(cache_.get().isFull(),
"Cache must be full after sync load. seq = {}", seq);
147 if (loader_ !=
nullptr)
157 if (loader_ !=
nullptr)
169 auto const minLatestSequence =
170 backend_->fetchLedgerRange()
172 return std::max(range.maxSequence - settings_.
cacheFileSettings->maxAge, range.minSequence);
176 auto const [success, duration_ms] =
util::timed([&]() {
177 return cache_.get().loadFromFile(settings_.cacheFileSettings->path, minLatestSequence);
180 if (not success.has_value()) {
181 LOG(log_.warn()) <<
"Error loading cache from file: " << success.error();
185 LOG(log_.info()) <<
"Loaded cache from file in " << duration_ms
186 <<
" ms. Latest sequence: " << cache_.get().latestLedgerSequence();
187 backend_->forceUpdateRange(cache_.get().latestLedgerSequence());
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:40
void load(uint32_t const seq) override
Load the cache for the given sequence number.
Definition CacheLoader.hpp:95
void stop() noexcept override
Requests the loader to stop asap.
Definition CacheLoader.hpp:145
void wait() noexcept override
Waits for the loader to finish background work.
Definition CacheLoader.hpp:155
CacheLoader(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > backend, data::LedgerCacheInterface &cache)
Construct a new Cache Loader object.
Definition CacheLoader.hpp:74
Definition CacheLoader.hpp:49
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:95
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:480
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:50
auto timed(FnType &&func)
Profiler function to measure the time a function execution consumes.
Definition Profiler.hpp:40
Stores a range of sequences as a min and max pair.
Definition Types.hpp:247
An interface for the Cache Loader.
Definition CacheLoaderInterface.hpp:29
Settings for the cache loader.
Definition CacheLoaderSettings.hpp:34
std::optional< CacheFileSettings > cacheFileSettings
Definition CacheLoaderSettings.hpp:55