22#include "data/BackendInterface.hpp"
23#include "etl/ETLState.hpp"
25#include "etl/Source.hpp"
26#include "etlng/InitialLoadObserverInterface.hpp"
28#include "feed/SubscriptionManagerInterface.hpp"
30#include "util/Assert.hpp"
31#include "util/Mutex.hpp"
32#include "util/Random.hpp"
33#include "util/ResponseExpirationCache.hpp"
34#include "util/config/ConfigDefinition.hpp"
35#include "util/log/Logger.hpp"
36#include "util/prometheus/Counter.hpp"
38#include <boost/asio.hpp>
39#include <boost/asio/io_context.hpp>
40#include <boost/asio/spawn.hpp>
41#include <boost/json/object.hpp>
42#include <boost/json/value.hpp>
43#include <grpcpp/grpcpp.h>
44#include <org/xrpl/rpc/v1/get_ledger.pb.h>
45#include <org/xrpl/rpc/v1/ledger.pb.h>
46#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
81 using RawLedgerObjectType = org::xrpl::rpc::v1::RawLedgerObject;
82 using GetLedgerResponseType = org::xrpl::rpc::v1::GetLedgerResponse;
83 using OptionalGetLedgerResponseType = std::optional<GetLedgerResponseType>;
86 static constexpr std::uint32_t kDEFAULT_DOWNLOAD_RANGES = 16;
90 std::optional<util::ResponseExpirationCache> forwardingCache_;
91 std::optional<std::string> forwardingXUserValue_;
93 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator_;
95 std::vector<SourcePtr> sources_;
96 std::optional<ETLState> etlState_;
97 std::uint32_t downloadRanges_ =
98 kDEFAULT_DOWNLOAD_RANGES;
100 struct ForwardingCounters {
101 std::reference_wrapper<util::prometheus::CounterInt> successDuration;
102 std::reference_wrapper<util::prometheus::CounterInt> failDuration;
103 std::reference_wrapper<util::prometheus::CounterInt> retries;
104 std::reference_wrapper<util::prometheus::CounterInt> cacheHit;
105 std::reference_wrapper<util::prometheus::CounterInt> cacheMiss;
106 } forwardingCounters_;
136 boost::asio::io_context& ioc,
137 std::shared_ptr<BackendInterface> backend,
138 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
139 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
140 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
156 static std::shared_ptr<LoadBalancerInterface>
159 boost::asio::io_context& ioc,
160 std::shared_ptr<BackendInterface> backend,
161 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
162 std::unique_ptr<util::RandomGeneratorInterface> randomGenerator,
163 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
175 std::vector<std::string>
178 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
192 [[maybe_unused]] uint32_t sequence,
194 [[maybe_unused]] std::chrono::steady_clock::duration retryAfter
197 ASSERT(
false,
"Not available for old ETL");
214 OptionalGetLedgerResponseType
216 uint32_t ledgerSequence,
218 bool getObjectNeighbors,
219 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
239 std::expected<boost::json::object, rpc::CombinedError>
241 boost::json::object
const& request,
242 std::optional<std::string>
const& clientIp,
244 boost::asio::yield_context yield
251 std::optional<ETLState>
261 stop(boost::asio::yield_context yield) override;
276 template <typename Func>
278 execute(Func f, uint32_t ledgerSequence, std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2});
284 chooseForwardingSource();
std::expected< std::vector< std::string >, InitialLedgerLoadError > InitialLedgerLoadResult
The result type of the initial ledger load.
Definition LoadBalancerInterface.hpp:54
This class is used to manage connections to transaction processing processes.
Definition LoadBalancer.hpp:79
std::vector< std::string > loadInitialLedger(uint32_t sequence, std::chrono::steady_clock::duration retryAfter=std::chrono::seconds{2}) override
Load the initial ledger, writing data to the queue.
Definition LoadBalancer.cpp:216
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:269
etlng::InitialLedgerLoadResult loadInitialLedger(uint32_t sequence, etlng::InitialLoadObserverInterface &observer, std::chrono::steady_clock::duration retryAfter) override
Load the initial ledger, writing data to the queue.
Definition LoadBalancer.hpp:191
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:89
std::optional< ETLState > getETLState() noexcept override
Return state of ETL nodes.
Definition LoadBalancer.cpp:375
boost::json::value toJson() const override
Represent the state of this load balancer as a JSON object.
Definition LoadBalancer.cpp:323
void stop(boost::asio::yield_context yield) override
Stop the load balancer. This will stop all subscription sources.
Definition LoadBalancer.cpp:385
static constexpr std::string_view kUSER_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding user requests.
Definition LoadBalancer.hpp:121
static constexpr std::string_view kADMIN_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding admin requests.
Definition LoadBalancer.hpp:116
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:239
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:68
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:59
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
A container for data that is protected by a mutex. Inspired by Mutex in Rust.
Definition Mutex.hpp:96
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
Definition LoadBalancer.hpp:70
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:39
SourcePtr makeSource(util::config::ObjectView const &config, boost::asio::io_context &ioc, std::shared_ptr< BackendInterface > backend, 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:41
A tag class to help identify LoadBalancer in templated code.
Definition LoadBalancer.hpp:65
The interface for observing the initial ledger load.
Definition InitialLoadObserverInterface.hpp:36