Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
CacheLoader.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#pragma once
21
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"
36
37#include <algorithm>
38#include <cstdint>
39#include <functional>
40#include <memory>
41#include <utility>
42
43namespace etl {
44
54template <typename ExecutionContextType = util::async::CoroExecutionContext>
57
58 util::Logger log_{"ETL"};
59 std::shared_ptr<BackendInterface> backend_;
60 std::reference_wrapper<data::LedgerCacheInterface> cache_;
61
62 CacheLoaderSettings settings_;
63 ExecutionContextType ctx_;
64 std::unique_ptr<CacheLoaderType> loader_;
65
66public:
76 std::shared_ptr<BackendInterface> backend,
78 )
79 : backend_{std::move(backend)}
80 , cache_{cache}
81 , settings_{makeCacheLoaderSettings(config)}
82 , ctx_{settings_.numThreads}
83 {
84 }
85
94 void
95 load(uint32_t const seq) override
96 {
97 ASSERT(not cache_.get().isFull(), "Cache must not be full. seq = {}", seq);
98
99 if (settings_.isDisabled()) {
100 cache_.get().setDisabled();
101 LOG(log_.warn()) << "Cache is disabled. Not loading";
102 return;
103 }
104
105 if (loadCacheFromFile()) {
106 return;
107 }
108
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
119 );
120 } else {
121 LOG(log_.info()) << "Loading cache with cursor from num_diffs=" << settings_.numCacheDiffs;
122 provider = std::make_shared<impl::CursorFromFixDiffNumProvider>(backend_, settings_.numCacheDiffs);
123 }
124
125 loader_ = std::make_unique<CacheLoaderType>(
126 ctx_,
127 backend_,
128 cache_,
129 seq,
130 settings_.numCacheMarkers,
131 settings_.cachePageFetchSize,
132 provider->getCursors(seq)
133 );
134
135 if (settings_.isSync()) {
136 loader_->wait();
137 ASSERT(cache_.get().isFull(), "Cache must be full after sync load. seq = {}", seq);
138 }
139 }
140
144 void
145 stop() noexcept override
146 {
147 if (loader_ != nullptr)
148 loader_->stop();
149 }
150
154 void
155 wait() noexcept override
156 {
157 if (loader_ != nullptr)
158 loader_->wait();
159 }
160
161private:
162 bool
163 loadCacheFromFile()
164 {
165 if (not settings_.cacheFileSettings.has_value()) {
166 return false;
167 }
168 LOG(log_.info()) << "Loading ledger cache from " << settings_.cacheFileSettings->path;
169 auto const minLatestSequence =
170 backend_->fetchLedgerRange()
171 .transform([this](data::LedgerRange const& range) {
172 return std::max(range.maxSequence - settings_.cacheFileSettings->maxAge, range.minSequence);
173 })
174 .value_or(0);
175
176 auto const [success, duration_ms] = util::timed([&]() {
177 return cache_.get().loadFromFile(settings_.cacheFileSettings->path, minLatestSequence);
178 });
179
180 if (not success.has_value()) {
181 LOG(log_.warn()) << "Error loading cache from file: " << success.error();
182 return false;
183 }
184
185 LOG(log_.info()) << "Loaded cache from file in " << duration_ms
186 << " ms. Latest sequence: " << cache_.get().latestLedgerSequence();
187 backend_->forceUpdateRange(cache_.get().latestLedgerSequence());
188 return true;
189 }
190};
191
192} // namespace etl
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