Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
LoadBalancer.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2022, 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/ETLState.hpp"
25#include "etl/Source.hpp"
26#include "etlng/InitialLoadObserverInterface.hpp"
28#include "feed/SubscriptionManagerInterface.hpp"
29#include "rpc/Errors.hpp"
30#include "util/Assert.hpp"
31#include "util/Mutex.hpp"
32#include "util/ResponseExpirationCache.hpp"
33#include "util/log/Logger.hpp"
34#include "util/newconfig/ConfigDefinition.hpp"
35
36#include <boost/asio.hpp>
37#include <boost/asio/io_context.hpp>
38#include <boost/asio/spawn.hpp>
39#include <boost/json/object.hpp>
40#include <boost/json/value.hpp>
41#include <grpcpp/grpcpp.h>
42#include <org/xrpl/rpc/v1/get_ledger.pb.h>
43#include <org/xrpl/rpc/v1/ledger.pb.h>
44#include <xrpl/proto/org/xrpl/rpc/v1/xrp_ledger.grpc.pb.h>
45
46#include <chrono>
47#include <concepts>
48#include <cstdint>
49#include <expected>
50#include <memory>
51#include <optional>
52#include <string>
53#include <string_view>
54#include <utility>
55#include <vector>
56
57namespace etl {
58
63 virtual ~LoadBalancerTag() = default;
64};
65
66template <typename T>
67concept SomeLoadBalancer = std::derived_from<T, LoadBalancerTag>;
68
77public:
78 using RawLedgerObjectType = org::xrpl::rpc::v1::RawLedgerObject;
79 using GetLedgerResponseType = org::xrpl::rpc::v1::GetLedgerResponse;
80 using OptionalGetLedgerResponseType = std::optional<GetLedgerResponseType>;
81
82private:
83 static constexpr std::uint32_t kDEFAULT_DOWNLOAD_RANGES = 16;
84
85 util::Logger log_{"ETL"};
86 // Forwarding cache must be destroyed after sources because sources have a callback to invalidate cache
87 std::optional<util::ResponseExpirationCache> forwardingCache_;
88 std::optional<std::string> forwardingXUserValue_;
89
90 std::vector<SourcePtr> sources_;
91 std::optional<ETLState> etlState_;
92 std::uint32_t downloadRanges_ =
93 kDEFAULT_DOWNLOAD_RANGES; /*< The number of markers to use when downloading initial ledger */
94
95 // Using mutext instead of atomic_bool because choosing a new source to
96 // forward messages should be done with a mutual exclusion otherwise there will be a race condition
97 util::Mutex<bool> hasForwardingSource_{false};
98
99public:
103 static constexpr std::string_view kADMIN_FORWARDING_X_USER_VALUE = "clio_admin";
104
108 static constexpr std::string_view kUSER_FORWARDING_X_USER_VALUE = "clio_user";
109
122 boost::asio::io_context& ioc,
123 std::shared_ptr<BackendInterface> backend,
124 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
125 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
126 SourceFactory sourceFactory = makeSource
127 );
128
140 static std::shared_ptr<LoadBalancerInterface>
143 boost::asio::io_context& ioc,
144 std::shared_ptr<BackendInterface> backend,
145 std::shared_ptr<feed::SubscriptionManagerInterface> subscriptions,
146 std::shared_ptr<NetworkValidatedLedgersInterface> validatedLedgers,
147 SourceFactory sourceFactory = makeSource
148 );
149
150 ~LoadBalancer() override;
151
160 std::vector<std::string>
161 loadInitialLedger(uint32_t sequence, std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2})
162 override;
163
173 std::vector<std::string>
175 [[maybe_unused]] uint32_t sequence,
176 [[maybe_unused]] etlng::InitialLoadObserverInterface& observer,
177 [[maybe_unused]] std::chrono::steady_clock::duration retryAfter
178 ) override
179 {
180 ASSERT(false, "Not available for old ETL");
181 std::unreachable();
182 }
183
197 OptionalGetLedgerResponseType
199 uint32_t ledgerSequence,
200 bool getObjects,
201 bool getObjectNeighbors,
202 std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2}
203 ) override;
204
210 boost::json::value
211 toJson() const override;
212
222 std::expected<boost::json::object, rpc::CombinedError>
224 boost::json::object const& request,
225 std::optional<std::string> const& clientIp,
226 bool isAdmin,
227 boost::asio::yield_context yield
228 ) override;
229
234 std::optional<ETLState>
235 getETLState() noexcept override;
236
243 void
244 stop(boost::asio::yield_context yield) override;
245
246private:
259 template <typename Func>
260 void
261 execute(Func f, uint32_t ledgerSequence, std::chrono::steady_clock::duration retryAfter = std::chrono::seconds{2});
262
266 void
267 chooseForwardingSource();
268
269 std::expected<boost::json::object, rpc::CombinedError>
270 forwardToRippledImpl(
271 boost::json::object const& request,
272 std::optional<std::string> const& clientIp,
273 bool isAdmin,
274 boost::asio::yield_context yield
275 );
276};
277
278} // namespace etl
This class is used to manage connections to transaction processing processes.
Definition LoadBalancer.hpp:76
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:179
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::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, SourceFactory sourceFactory=makeSource)
A factory function for the load balancer.
Definition LoadBalancer.cpp:65
LoadBalancer(util::config::ClioConfigDefinition const &config, boost::asio::io_context &ioc, std::shared_ptr< BackendInterface > backend, std::shared_ptr< feed::SubscriptionManagerInterface > subscriptions, std::shared_ptr< NetworkValidatedLedgersInterface > validatedLedgers, SourceFactory sourceFactory=makeSource)
Create an instance of the load balancer.
Definition LoadBalancer.cpp:79
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:232
std::optional< ETLState > getETLState() noexcept override
Return state of ETL nodes.
Definition LoadBalancer.cpp:329
std::vector< std::string > 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:174
boost::json::value toJson() const override
Represent the state of this load balancer as a JSON object.
Definition LoadBalancer.cpp:277
void stop(boost::asio::yield_context yield) override
Stop the load balancer. This will stop all subscription sources.
Definition LoadBalancer.cpp:339
static constexpr std::string_view kUSER_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding user requests.
Definition LoadBalancer.hpp:108
static constexpr std::string_view kADMIN_FORWARDING_X_USER_VALUE
Value for the X-User header when forwarding admin requests.
Definition LoadBalancer.hpp:103
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:202
An interface for LoadBalancer.
Definition LoadBalancerInterface.hpp:45
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:67
This namespace contains everything to do with the ETL and ETL sources.
Definition CacheLoader.hpp:37
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:62
The interface for observing the initial ledger load.
Definition InitialLoadObserverInterface.hpp:36