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