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"
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"
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>
61 using RawLedgerObjectType = org::xrpl::rpc::v1::RawLedgerObject;
62 using GetLedgerResponseType = org::xrpl::rpc::v1::GetLedgerResponse;
63 using OptionalGetLedgerResponseType = std::optional<GetLedgerResponseType>;
66 static constexpr std::uint32_t kDEFAULT_DOWNLOAD_RANGES = 16;
71 std::optional<util::ResponseExpirationCache> forwardingCache_;
72 std::optional<std::string> forwardingXUserValue_;
74 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator_;
76 std::vector<SourcePtr> sources_;
77 std::optional<ETLState> etlState_;
78 std::uint32_t downloadRanges_ = kDEFAULT_DOWNLOAD_RANGES;
81 struct ForwardingCounters {
82 std::reference_wrapper<util::prometheus::CounterInt> successDuration;
83 std::reference_wrapper<util::prometheus::CounterInt> failDuration;
84 std::reference_wrapper<util::prometheus::CounterInt> retries;
85 std::reference_wrapper<util::prometheus::CounterInt> cacheHit;
86 std::reference_wrapper<util::prometheus::CounterInt> cacheMiss;
87 } forwardingCounters_;
118 boost::asio::io_context& ioc,
119 std::shared_ptr<BackendInterface> backend,
120 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
121 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
122 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
123 SourceFactory sourceFactory = makeSource
138 static std::shared_ptr<LoadBalancerInterface>
141 boost::asio::io_context& ioc,
142 std::shared_ptr<BackendInterface> backend,
143 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
144 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
145 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
146 SourceFactory sourceFactory = makeSource
164 std::chrono::steady_clock::duration retryAfter
180 OptionalGetLedgerResponseType
182 uint32_t ledgerSequence,
184 bool getObjectNeighbors,
185 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
205 std::expected<boost::json::object, rpc::CombinedError>
207 boost::json::object
const& request,
208 std::optional<std::string>
const& clientIp,
210 boost::asio::yield_context yield
217 std::optional<ETLState>
227 stop(boost::asio::yield_context yield) override;
244 template <typename Func>
248 uint32_t ledgerSequence,
249 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
256 chooseForwardingSource();
std::expected< std::vector< std::string >, InitialLedgerLoadError > InitialLedgerLoadResult
The result type of the initial ledger load.
Definition LoadBalancerInterface.hpp:35
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:40
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 &observer, 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
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:381
boost::json::value toJson() const override
Represent the state of this load balancer as a JSON object.
Definition LoadBalancer.cpp:322
void stop(boost::asio::yield_context yield) override
Stop the load balancer. This will stop all subscription sources.
Definition LoadBalancer.cpp:391
static constexpr std::string_view kUSER_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding user requests.
Definition LoadBalancer.hpp:103
static constexpr std::string_view kADMIN_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding admin requests.
Definition LoadBalancer.hpp:98
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:77
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
Definition LoadBalancer.hpp:49
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