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>
80 static constexpr std::uint32_t kDefaultDownloadRanges = 16;
85 std::optional<util::ResponseExpirationCache> forwardingCache_;
86 std::optional<std::string> forwardingXUserValue_;
88 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator_;
90 std::vector<SourcePtr> sources_;
91 std::optional<ETLState> etlState_;
92 std::uint32_t downloadRanges_ = kDefaultDownloadRanges;
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_;
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,
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,
178 std::chrono::steady_clock::duration retryAfter
196 uint32_t ledgerSequence,
198 bool getObjectNeighbors,
199 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
219 std::expected<boost::json::object, rpc::CombinedError>
221 boost::json::object
const& request,
222 std::optional<std::string>
const& clientIp,
224 boost::asio::yield_context yield
231 std::optional<ETLState>
241 stop(boost::asio::yield_context yield) override;
258 template <typename Func>
262 uint32_t ledgerSequence,
263 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
270 chooseForwardingSource();
273 shouldUseCache(
bool isAdmin)
const;
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