xrpld
Loading...
Searching...
No Matches
PeerfinderManager.cpp
1#include <xrpl/peerfinder/PeerfinderManager.h>
2
3#include <xrpl/beast/insight/Collector.h>
4#include <xrpl/beast/insight/Gauge.h>
5#include <xrpl/beast/insight/Hook.h>
6#include <xrpl/beast/net/IPEndpoint.h>
7#include <xrpl/beast/utility/Journal.h>
8#include <xrpl/beast/utility/PropertyStream.h>
9#include <xrpl/peerfinder/Config.h>
10#include <xrpl/peerfinder/Slot.h>
11#include <xrpl/peerfinder/Types.h>
12#include <xrpl/peerfinder/detail/Checker.h>
13#include <xrpl/peerfinder/detail/Logic.h>
14#include <xrpl/peerfinder/detail/SlotImp.h>
15#include <xrpl/peerfinder/detail/SourceStrings.h>
16#include <xrpl/peerfinder/detail/Store.h>
17#include <xrpl/peerfinder/make_Manager.h>
18#include <xrpl/protocol/PublicKey.h>
19
20#include <boost/asio/executor_work_guard.hpp>
21#include <boost/asio/io_context.hpp>
22#include <boost/asio/ip/tcp.hpp>
23
24#include <memory>
25#include <mutex>
26#include <optional>
27#include <string>
28#include <string_view>
29#include <utility>
30#include <vector>
31
32namespace xrpl::peer_finder {
33
34class ManagerImp : public Manager
35{
36public:
37 // NOLINTBEGIN(readability-identifier-naming)
38 boost::asio::io_context& io_context_;
44 Logic<decltype(checker_)> logic_;
45 // NOLINTEND(readability-identifier-naming)
46
47 //--------------------------------------------------------------------------
48
50 boost::asio::io_context& ioContext,
51 clock_type& clock,
52 beast::Journal journal,
53 Store& store,
54 beast::insight::Collector::ptr const& collector)
55 : io_context_(ioContext)
56 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
57 , clock_(clock)
58 , journal_(journal)
59 , store_(store)
61 , logic_(clock, store_, checker_, journal)
62 , stats_([this] { collectMetrics(); }, collector)
63 {
64 }
65
66 ~ManagerImp() override
67 {
68 stop();
69 }
70
71 void
72 stop() override
73 {
74 if (work_)
75 {
76 work_.reset();
77 checker_.stop();
78 logic_.stop();
79 }
80 }
81
82 //--------------------------------------------------------------------------
83 //
84 // PeerFinder
85 //
86 //--------------------------------------------------------------------------
87
88 void
89 setConfig(Config const& config) override
90 {
91 logic_.config(config);
92 }
93
94 Config
95 config() override
96 {
97 return logic_.config();
98 }
99
100 void
102 {
103 logic_.addFixedPeer(name, addresses);
104 }
105
106 void
108 {
109 logic_.addStaticSource(SourceStrings::make(name, strings));
110 }
111
112 void
114 {
115 // VFALCO TODO This needs to be implemented
116 }
117
118 //--------------------------------------------------------------------------
119
122 beast::ip::Endpoint const& localEndpoint,
123 beast::ip::Endpoint const& remoteEndpoint) override
124 {
125 return logic_.newInboundSlot(localEndpoint, remoteEndpoint);
126 }
127
129 newOutboundSlot(beast::ip::Endpoint const& remoteEndpoint) override
130 {
131 return logic_.newOutboundSlot(remoteEndpoint);
132 }
133
134 void
135 onEndpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) override
136 {
138 logic_.onEndpoints(impl, endpoints);
139 }
140
141 void
142 onClosed(std::shared_ptr<Slot> const& slot) override
143 {
145 logic_.onClosed(impl);
146 }
147
148 void
149 onFailure(std::shared_ptr<Slot> const& slot) override
150 {
152 logic_.onFailure(impl);
153 }
154
155 void
157 boost::asio::ip::tcp::endpoint const& remoteAddress,
159 {
160 logic_.onRedirects(eps.begin(), eps.end(), remoteAddress);
161 }
162
163 //--------------------------------------------------------------------------
164
165 bool
166 onConnected(std::shared_ptr<Slot> const& slot, beast::ip::Endpoint const& localEndpoint)
167 override
168 {
170 return logic_.onConnected(impl, localEndpoint);
171 }
172
173 Result
174 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) override
175 {
177 return logic_.activate(impl, key, reserved);
178 }
179
181 redirect(std::shared_ptr<Slot> const& slot) override
182 {
184 return logic_.redirect(impl);
185 }
186
188 autoconnect() override
189 {
190 return logic_.autoconnect();
191 }
192
193 void
194 oncePerSecond() override
195 {
196 logic_.oncePerSecond();
197 }
198
201 {
202 return logic_.buildEndpointsForPeers();
203 }
204
205 void
206 start() override
207 {
208 logic_.load();
209 }
210
211 //--------------------------------------------------------------------------
212 //
213 // PropertyStream
214 //
215 //--------------------------------------------------------------------------
216
217 void
219 {
220 logic_.onWrite(map);
221 }
222
223private:
224 struct Stats
225 {
226 template <class Handler>
227 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
228 : hook(collector->makeHook(handler))
229 , activeInboundPeers(collector->makeGauge("Peer_Finder", "Active_Inbound_Peers"))
230 , activeOutboundPeers(collector->makeGauge("Peer_Finder", "Active_Outbound_Peers"))
231 {
232 }
233
237 };
238
241
242 void
244 {
245 std::scoped_lock const lock(statsMutex_);
246 stats_.activeInboundPeers = logic_.counts().inboundActive();
247 stats_.activeOutboundPeers = logic_.counts().outActive();
248 }
249};
250
251//------------------------------------------------------------------------------
252
253Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
254{
255}
256
259 boost::asio::io_context& ioContext,
260 clock_type& clock,
261 beast::Journal journal,
262 Store& store,
263 beast::insight::Collector::ptr const& collector)
264{
265 return std::make_unique<ManagerImp>(ioContext, clock, journal, store, collector);
266}
267
268} // namespace xrpl::peer_finder
T begin(T... args)
A generic endpoint for log messages.
Definition Journal.h:44
std::string const & name() const
Returns the name of this source.
std::shared_ptr< Collector > ptr
Definition Collector.h:29
A metric for measuring an integral value.
Definition Gauge.h:21
A reference to a handler for performing polled collection.
Definition Hook.h:14
A version-independent IP address and port combination.
Definition IPEndpoint.h:24
A public key.
Definition PublicKey.h:53
Tests remote listening sockets to make sure they are connectable.
Definition Checker.h:21
The Logic for maintaining the list of Slot addresses.
ManagerImp(boost::asio::io_context &ioContext, clock_type &clock, beast::Journal journal, Store &store, beast::insight::Collector::ptr const &collector)
void addFallbackStrings(std::string const &name, std::vector< std::string > const &strings) override
Add a set of strings as fallback ip::Endpoint sources.
std::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot) override
Returns a set of endpoints suitable for redirection.
void addFallbackURL(std::string const &name, std::string const &url)
void addFixedPeer(std::string_view name, std::vector< beast::ip::Endpoint > const &addresses) override
Add a peer that should always be connected.
Logic< decltype(checker_)> logic_
void onEndpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints) override
Called when mtENDPOINTS is received.
void onFailure(std::shared_ptr< Slot > const &slot) override
Called when an outbound connection is deemed to have failed.
void onClosed(std::shared_ptr< Slot > const &slot) override
Called when the slot is closed.
void setConfig(Config const &config) override
Set the configuration for the manager.
bool onConnected(std::shared_ptr< Slot > const &slot, beast::ip::Endpoint const &localEndpoint) override
Called when an outbound connection attempt succeeds.
boost::asio::io_context & io_context_
std::pair< std::shared_ptr< Slot >, Result > newOutboundSlot(beast::ip::Endpoint const &remoteEndpoint) override
Create a new outbound slot with the specified remote endpoint.
std::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
std::pair< std::shared_ptr< Slot >, Result > newInboundSlot(beast::ip::Endpoint const &localEndpoint, beast::ip::Endpoint const &remoteEndpoint) override
Add a URL as a fallback location to obtain ip::Endpoint sources.
std::vector< std::pair< std::shared_ptr< Slot >, std::vector< Endpoint > > > buildEndpointsForPeers() override
void oncePerSecond() override
Perform periodic activity.
void start() override
Transition to the started state, synchronously.
std::vector< beast::ip::Endpoint > autoconnect() override
Return a set of addresses we should connect to.
Config config() override
Returns the configuration for the manager.
void stop() override
Transition to the stopped state, synchronously.
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
void onRedirects(boost::asio::ip::tcp::endpoint const &remoteAddress, std::vector< boost::asio::ip::tcp::endpoint > const &eps) override
Called when we received redirect IPs from a busy peer.
Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved) override
Request an active slot type.
Checker< boost::asio::ip::tcp > checker_
std::shared_ptr< SlotImp > ptr
Definition SlotImp.h:20
static std::shared_ptr< Source > make(std::string const &name, Strings const &strings)
Abstract persistence for PeerFinder data.
Definition Store.h:15
T end(T... args)
T make_unique(T... args)
STL namespace.
std::unique_ptr< Manager > makeManager(boost::asio::io_context &ioContext, clock_type &clock, beast::Journal journal, Store &store, beast::insight::Collector::ptr const &collector)
Create a new Manager.
Result
Possible results from activating a slot.
std::vector< Endpoint > Endpoints
A set of Endpoint used for connecting.
beast::AbstractClock< std::chrono::steady_clock > clock_type
T dynamic_pointer_cast(T... args)
PeerFinder configuration settings.
Stats(Handler const &handler, beast::insight::Collector::ptr const &collector)