Clio  develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LoadBalancer.hpp
1#pragma once
2
3#include "data/BackendInterface.hpp"
4#include "etl/ETLState.hpp"
5#include "etl/InitialLoadObserverInterface.hpp"
8#include "etl/Source.hpp"
9#include "feed/SubscriptionManagerInterface.hpp"
10#include "rpc/Errors.hpp"
11#include "util/Mutex.hpp"
12#include "util/Random.hpp"
13#include "util/ResponseExpirationCache.hpp"
14#include "util/config/ConfigDefinition.hpp"
15#include "util/log/Logger.hpp"
16#include "util/prometheus/Counter.hpp"
17
18#include <boost/asio.hpp>
19#include <boost/asio/io_context.hpp>
20#include <boost/asio/spawn.hpp>
21#include <boost/json/object.hpp>
22#include <boost/json/value.hpp>
23#include <grpcpp/grpcpp.h>
24#include <org/xrpl/rpc/v1/get_ledger.pb.h>
25#include <org/xrpl/rpc/v1/ledger.pb.h>
26#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
27
28#include <chrono>
29#include <concepts>
30#include <cstdint>
31#include <expected>
32#include <functional>
33#include <memory>
34#include <optional>
35#include <string>
36#include <string_view>
37#include <vector>
38
39namespace etl {
40
45 virtual ~LoadBalancerTag() = default;
46};
47
53template <typename T>
54concept SomeLoadBalancer = std::derived_from<T, LoadBalancerTag>;
55
65public:
69 using RawLedgerObjectType = org::xrpl::rpc::v1::RawLedgerObject;
73 using GetLedgerResponseType = org::xrpl::rpc::v1::GetLedgerResponse;
77 using OptionalGetLedgerResponseType = std::optional<GetLedgerResponseType>;
78
79private:
80 static constexpr std::uint32_t kDefaultDownloadRanges = 16;
81
82 util::Logger log_{"ETL"};
83 // Forwarding cache must be destroyed after sources because sources have a callback to
84 // invalidate cache
85 std::optional<util::ResponseExpirationCache> forwardingCache_;
86 std::optional<std::string> forwardingXUserValue_;
87
88 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator_;
89
90 std::vector<SourcePtr> sources_;
91 std::optional<ETLState> etlState_;
92 std::uint32_t downloadRanges_ = kDefaultDownloadRanges; /*< The number of markers to use when
93 downloading initial ledger */
94
95 struct ForwardingCounters {
96 std::reference_wrapper<util::prometheus::CounterInt> successDuration;
97 std::reference_wrapper<util::prometheus::CounterInt> failDuration;
98 std::reference_wrapper<util::prometheus::CounterInt> retries;
99 std::reference_wrapper<util::prometheus::CounterInt> cacheHit;
100 std::reference_wrapper<util::prometheus::CounterInt> cacheMiss;
101 } forwardingCounters_;
102
103 // Using mutex instead of atomic_bool because choosing a new source to
104 // forward messages should be done with a mutual exclusion otherwise there will be a race
105 // condition
106 util::Mutex<bool> hasForwardingSource_{false};
107
108public:
112 static constexpr std::string_view kAdminForwardingXUserValue = "clio_admin";
113
117 static constexpr std::string_view kUserForwardingXUserValue = "clio_user";
118
132 boost::asio::io_context& ioc,
133 std::shared_ptr<BackendInterface> backend,
134 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
135 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
136 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
137 SourceFactory sourceFactory = makeSource
138 );
139
152 static std::shared_ptr<LoadBalancerInterface>
155 boost::asio::io_context& ioc,
156 std::shared_ptr<BackendInterface> backend,
157 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
158 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
159 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
160 SourceFactory sourceFactory = makeSource
161 );
162
176 uint32_t sequence,
177 InitialLoadObserverInterface& loadObserver,
178 std::chrono::steady_clock::duration retryAfter
179 ) override;
180
196 uint32_t ledgerSequence,
197 bool getObjects,
198 bool getObjectNeighbors,
199 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
200 ) override;
201
207 boost::json::value
208 toJson() const override;
209
219 std::expected<boost::json::object, rpc::CombinedError>
221 boost::json::object const& request,
222 std::optional<std::string> const& clientIp,
223 bool isAdmin,
224 boost::asio::yield_context yield
225 ) override;
226
231 std::optional<ETLState>
232 getETLState() noexcept override;
233
240 void
241 stop(boost::asio::yield_context yield) override;
242
243private:
258 template <typename Func>
259 void
260 execute(
261 Func f,
262 uint32_t ledgerSequence,
263 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
264 );
265
269 void
270 chooseForwardingSource();
271
272 bool
273 shouldUseCache(bool isAdmin) const;
274};
275
276} // namespace etl
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:40
org::xrpl::rpc::v1::GetLedgerResponse GetLedgerResponseType
gRPC response containing the data of a single ledger
Definition LoadBalancer.hpp:73
static constexpr std::string_view kUserForwardingXUserValue
Value for the X-User header when forwarding user requests.
Definition LoadBalancer.hpp:117
static std::shared_ptr< LoadBalancerInterface > makeLoadBalancer(util::config::ClioConfigDefinition const &config, boost::asio::io_context &ioc, std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::unique_ptr< util::RandomGeneratorInterface > randomGenerator, std::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, SourceFactory sourceFactory=makeSource)
A factory function for the load balancer.
Definition LoadBalancer.cpp:50
InitialLedgerLoadResult loadInitialLedger(uint32_t sequence, InitialLoadObserverInterface &loadObserver, std::chrono::steady_clock::duration retryAfter) override
Load the initial ledger, writing data to the queue.
Definition LoadBalancer.cpp:204
std::expected< boost::json::object, rpc::CombinedError > forwardToRippled(boost::json::object const &request, std::optional< std::string > const &clientIp, bool isAdmin, boost::asio::yield_context yield) override
Forward a JSON RPC request to a randomly selected rippled node.
Definition LoadBalancer.cpp:267
std::optional< GetLedgerResponseType > OptionalGetLedgerResponseType
Optional ledger response, as returned by fetchLedger().
Definition LoadBalancer.hpp:77
LoadBalancer(util::config::ClioConfigDefinition const &config, boost::asio::io_context &ioc, std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::unique_ptr< util::RandomGeneratorInterface > randomGenerator, std::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, SourceFactory sourceFactory=makeSource)
Create an instance of the load balancer.
Definition LoadBalancer.cpp:71
std::optional< ETLState > getETLState() noexcept override
Return state of ETL nodes.
Definition LoadBalancer.cpp:384
boost::json::value toJson() const override
Represent the state of this load balancer as a JSON object.
Definition LoadBalancer.cpp:325
static constexpr std::string_view kAdminForwardingXUserValue
Value for the X-User header when forwarding admin requests.
Definition LoadBalancer.hpp:112
void stop(boost::asio::yield_context yield) override
Stop the load balancer. This will stop all subscription sources.
Definition LoadBalancer.cpp:394
org::xrpl::rpc::v1::RawLedgerObject RawLedgerObjectType
Raw ledger object as received over gRPC.
Definition LoadBalancer.hpp:69
OptionalGetLedgerResponseType fetchLedger(uint32_t ledgerSequence, bool getObjects, bool getObjectNeighbors, std::chrono::steady_clock::duration retryAfter=std::chrono::seconds{2}) override
Fetch data for a specific ledger.
Definition LoadBalancer.cpp:234
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:82
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
Concept for load balancer implementations.
Definition LoadBalancer.hpp:54
This namespace contains everything to do with the ETL and ETL sources.
Definition AmendmentBlockHandlerInterface.hpp:6
std::function< SourcePtr( util::config::ObjectView const &config, boost::asio::io_context &ioc, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, std::chrono::steady_clock::duration forwardingTimeout, SourceBase::OnConnectHook onConnect, SourceBase::OnDisconnectHook onDisconnect, SourceBase::OnLedgerClosedHook onLedgerClosed)> SourceFactory
Factory function creating an ETL source from its configuration and hooks.
Definition Source.hpp:160
SourcePtr makeSource(util::config::ObjectView const &config, boost::asio::io_context &ioc, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, std::chrono::steady_clock::duration forwardingTimeout, SourceBase::OnConnectHook onConnect, SourceBase::OnDisconnectHook onDisconnect, SourceBase::OnLedgerClosedHook onLedgerClosed)
Create a source.
Definition Source.cpp:21
std::expected< std::vector< std::string >, InitialLedgerLoadError > InitialLedgerLoadResult
The result type of the initial ledger load.
Definition LoadBalancerInterface.hpp:35
The interface for observing the initial ledger load.
Definition InitialLoadObserverInterface.hpp:17
A tag class to help identify LoadBalancer in templated code.
Definition LoadBalancer.hpp:44