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 "etl/CacheLoaderSettings.hpp"
25#include "etl/impl/CacheLoader.hpp"
26#include "etl/impl/CursorFromAccountProvider.hpp"
27#include "etl/impl/CursorFromDiffProvider.hpp"
28#include "etl/impl/CursorFromFixDiffNumProvider.hpp"
29#include "util/Assert.hpp"
30#include "util/async/context/BasicExecutionContext.hpp"
31#include "util/log/Logger.hpp"
32
33#include <cstdint>
34#include <functional>
35#include <memory>
36
37namespace etl {
38
48template <typename ExecutionContextType = util::async::CoroExecutionContext>
51
52 util::Logger log_{"ETL"};
53 std::shared_ptr<BackendInterface> backend_;
54 std::reference_wrapper<data::LedgerCacheInterface> cache_;
55
56 CacheLoaderSettings settings_;
57 ExecutionContextType ctx_;
58 std::unique_ptr<CacheLoaderType> loader_;
59
60public:
70 std::shared_ptr<BackendInterface> const& backend,
72 )
73 : backend_{backend}, cache_{cache}, settings_{makeCacheLoaderSettings(config)}, ctx_{settings_.numThreads}
74 {
75 }
76
85 void
86 load(uint32_t const seq)
87 {
88 ASSERT(not cache_.get().isFull(), "Cache must not be full. seq = {}", seq);
89
90 if (settings_.isDisabled()) {
91 cache_.get().setDisabled();
92 LOG(log_.warn()) << "Cache is disabled. Not loading";
93 return;
94 }
95
96 std::shared_ptr<impl::BaseCursorProvider> provider;
97 if (settings_.numCacheCursorsFromDiff != 0) {
98 LOG(log_.info()) << "Loading cache with cursor from num_cursors_from_diff="
99 << settings_.numCacheCursorsFromDiff;
100 provider = std::make_shared<impl::CursorFromDiffProvider>(backend_, settings_.numCacheCursorsFromDiff);
101 } else if (settings_.numCacheCursorsFromAccount != 0) {
102 LOG(log_.info()) << "Loading cache with cursor from num_cursors_from_account="
103 << settings_.numCacheCursorsFromAccount;
104 provider = std::make_shared<impl::CursorFromAccountProvider>(
105 backend_, settings_.numCacheCursorsFromAccount, settings_.cachePageFetchSize
106 );
107 } else {
108 LOG(log_.info()) << "Loading cache with cursor from num_diffs=" << settings_.numCacheDiffs;
109 provider = std::make_shared<impl::CursorFromFixDiffNumProvider>(backend_, settings_.numCacheDiffs);
110 }
111
112 loader_ = std::make_unique<CacheLoaderType>(
113 ctx_,
114 backend_,
115 cache_,
116 seq,
117 settings_.numCacheMarkers,
118 settings_.cachePageFetchSize,
119 provider->getCursors(seq)
120 );
121
122 if (settings_.isSync()) {
123 loader_->wait();
124 ASSERT(cache_.get().isFull(), "Cache must be full after sync load. seq = {}", seq);
125 }
126 }
127
131 void
132 stop() noexcept
133 {
134 if (loader_ != nullptr)
135 loader_->stop();
136 }
137
141 void
142 wait() noexcept
143 {
144 if (loader_ != nullptr)
145 loader_->wait();
146 }
147};
148
149} // namespace etl
Cache for an entire ledger.
Definition LedgerCacheInterface.hpp:38
Cache loading interface.
Definition CacheLoader.hpp:49
void stop() noexcept
Requests the loader to stop asap.
Definition CacheLoader.hpp:132
void wait() noexcept
Waits for the loader to finish background work.
Definition CacheLoader.hpp:142
CacheLoader(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > const &backend, data::LedgerCacheInterface &cache)
Construct a new Cache Loader object.
Definition CacheLoader.hpp:68
void load(uint32_t const seq)
Load the cache for the given sequence number.
Definition CacheLoader.hpp:86
Definition CacheLoader.hpp:49
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump warn(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::WRN severity.
Definition Logger.cpp:224
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:219
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:37
CacheLoaderSettings makeCacheLoaderSettings(util::config::ClioConfigDefinition const &config)
Create a CacheLoaderSettings object from a Config object.
Definition CacheLoaderSettings.cpp:51
Settings for the cache loader.
Definition CacheLoaderSettings.hpp:31
bool isDisabled() const
Definition CacheLoaderSettings.cpp:45
bool isSync() const
Definition CacheLoaderSettings.cpp:33
size_t numCacheMarkers
Definition CacheLoaderSettings.hpp:36
size_t numCacheCursorsFromAccount
Definition CacheLoaderSettings.hpp:40
size_t cachePageFetchSize
Definition CacheLoaderSettings.hpp:37
size_t numCacheDiffs
Definition CacheLoaderSettings.hpp:35
size_t numCacheCursorsFromDiff
Definition CacheLoaderSettings.hpp:39