xrpld
Loading...
Searching...
No Matches
PeerfinderManager.cpp
1#include <xrpld/peerfinder/PeerfinderManager.h>
2
3#include <xrpld/peerfinder/Slot.h>
4#include <xrpld/peerfinder/detail/Checker.h>
5#include <xrpld/peerfinder/detail/Logic.h>
6#include <xrpld/peerfinder/detail/SlotImp.h>
7#include <xrpld/peerfinder/detail/SourceStrings.h>
8#include <xrpld/peerfinder/detail/StoreSqdb.h>
9
10#include <xrpl/beast/insight/Collector.h>
11#include <xrpl/beast/insight/Gauge.h>
12#include <xrpl/beast/insight/Hook.h>
13#include <xrpl/beast/net/IPEndpoint.h>
14#include <xrpl/beast/utility/Journal.h>
15#include <xrpl/beast/utility/PropertyStream.h>
16#include <xrpl/config/BasicConfig.h>
17#include <xrpl/protocol/PublicKey.h>
18
19#include <boost/asio/executor_work_guard.hpp>
20#include <boost/asio/io_context.hpp>
21#include <boost/asio/ip/tcp.hpp>
22
23#include <functional>
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::PeerFinder {
33
34class ManagerImp : public Manager
35{
36public:
37 // NOLINTBEGIN(readability-identifier-naming)
38 boost::asio::io_context& io_context_;
44 Logic<decltype(checker_)> logic_;
46 // NOLINTEND(readability-identifier-naming)
47
48 //--------------------------------------------------------------------------
49
51 boost::asio::io_context& ioContext,
52 clock_type& clock,
53 beast::Journal journal,
54 BasicConfig const& config,
55 beast::insight::Collector::ptr const& collector)
56 : io_context_(ioContext)
57 , work_(std::in_place, boost::asio::make_work_guard(io_context_))
58 , clock_(clock)
59 , journal_(journal)
60 , store_(journal)
62 , logic_(clock, store_, checker_, journal)
64 , stats_(std::bind(&ManagerImp::collectMetrics, this), collector)
65 {
66 }
67
68 ~ManagerImp() override
69 {
70 stop();
71 }
72
73 void
74 stop() override
75 {
76 if (work_)
77 {
78 work_.reset();
79 checker_.stop();
80 logic_.stop();
81 }
82 }
83
84 //--------------------------------------------------------------------------
85 //
86 // PeerFinder
87 //
88 //--------------------------------------------------------------------------
89
90 void
91 setConfig(Config const& config) override
92 {
93 logic_.config(config);
94 }
95
96 Config
97 config() override
98 {
99 return logic_.config();
100 }
101
102 void
104 {
105 logic_.addFixedPeer(name, addresses);
106 }
107
108 void
110 {
111 logic_.addStaticSource(SourceStrings::make(name, strings));
112 }
113
114 void
116 {
117 // VFALCO TODO This needs to be implemented
118 }
119
120 //--------------------------------------------------------------------------
121
124 beast::IP::Endpoint const& localEndpoint,
125 beast::IP::Endpoint const& remoteEndpoint) override
126 {
127 return logic_.newInboundSlot(localEndpoint, remoteEndpoint);
128 }
129
131 newOutboundSlot(beast::IP::Endpoint const& remoteEndpoint) override
132 {
133 return logic_.newOutboundSlot(remoteEndpoint);
134 }
135
136 void
137 onEndpoints(std::shared_ptr<Slot> const& slot, Endpoints const& endpoints) override
138 {
140 logic_.onEndpoints(impl, endpoints);
141 }
142
143 void
144 onClosed(std::shared_ptr<Slot> const& slot) override
145 {
147 logic_.onClosed(impl);
148 }
149
150 void
151 onFailure(std::shared_ptr<Slot> const& slot) override
152 {
154 logic_.onFailure(impl);
155 }
156
157 void
159 boost::asio::ip::tcp::endpoint const& remoteAddress,
161 {
162 logic_.onRedirects(eps.begin(), eps.end(), remoteAddress);
163 }
164
165 //--------------------------------------------------------------------------
166
167 bool
168 onConnected(std::shared_ptr<Slot> const& slot, beast::IP::Endpoint const& localEndpoint)
169 override
170 {
172 return logic_.onConnected(impl, localEndpoint);
173 }
174
175 Result
176 activate(std::shared_ptr<Slot> const& slot, PublicKey const& key, bool reserved) override
177 {
179 return logic_.activate(impl, key, reserved);
180 }
181
183 redirect(std::shared_ptr<Slot> const& slot) override
184 {
186 return logic_.redirect(impl);
187 }
188
190 autoconnect() override
191 {
192 return logic_.autoconnect();
193 }
194
195 void
196 oncePerSecond() override
197 {
198 logic_.oncePerSecond();
199 }
200
203 {
204 return logic_.buildEndpointsForPeers();
205 }
206
207 void
208 start() override
209 {
210 store_.open(config_);
211 logic_.load();
212 }
213
214 //--------------------------------------------------------------------------
215 //
216 // PropertyStream
217 //
218 //--------------------------------------------------------------------------
219
220 void
222 {
223 logic_.onWrite(map);
224 }
225
226private:
227 struct Stats
228 {
229 template <class Handler>
230 Stats(Handler const& handler, beast::insight::Collector::ptr const& collector)
231 : hook(collector->makeHook(handler))
232 , activeInboundPeers(collector->makeGauge("Peer_Finder", "Active_Inbound_Peers"))
233 , activeOutboundPeers(collector->makeGauge("Peer_Finder", "Active_Outbound_Peers"))
234 {
235 }
236
240 };
241
244
245 void
247 {
248 std::scoped_lock const lock(statsMutex_);
249 stats_.activeInboundPeers = logic_.counts().inboundActive();
250 stats_.activeOutboundPeers = logic_.counts().outActive();
251 }
252};
253
254//------------------------------------------------------------------------------
255
256Manager::Manager() noexcept : beast::PropertyStream::Source("peerfinder")
257{
258}
259
262 boost::asio::io_context& ioContext,
263 clock_type& clock,
264 beast::Journal journal,
265 BasicConfig const& config,
266 beast::insight::Collector::ptr const& collector)
267{
268 return std::make_unique<ManagerImp>(ioContext, clock, journal, config, collector);
269}
270
271} // namespace xrpl::PeerFinder
T begin(T... args)
A version-independent IP address and port combination.
Definition IPEndpoint.h:17
A generic endpoint for log messages.
Definition Journal.h:38
std::string const & name() const
Returns the name of this source.
std::shared_ptr< Collector > ptr
Definition Collector.h:26
A metric for measuring an integral value.
Definition Gauge.h:20
A reference to a handler for performing polled collection.
Definition Hook.h:12
Holds unparsed configuration information.
Tests remote listening sockets to make sure they are connectable.
Definition Checker.h:18
The Logic for maintaining the list of Slot addresses.
void addFixedPeer(std::string_view name, std::vector< beast::IP::Endpoint > const &addresses) override
Add a peer that should always be connected.
void setConfig(Config const &config) override
Set the configuration for the manager.
void start() override
Transition to the started state, synchronously.
Config config() override
Returns the configuration for the manager.
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.
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::vector< Endpoint > redirect(std::shared_ptr< Slot > const &slot) override
Returns a set of endpoints suitable for redirection.
void onFailure(std::shared_ptr< Slot > const &slot) override
Called when an outbound connection is deemed to have failed.
Logic< decltype(checker_)> logic_
void addFallbackURL(std::string const &name, std::string const &url)
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
Checker< boost::asio::ip::tcp > checker_
void stop() override
Transition to the stopped state, synchronously.
void onClosed(std::shared_ptr< Slot > const &slot) override
Called when the slot is closed.
void oncePerSecond() override
Perform periodic activity.
void onEndpoints(std::shared_ptr< Slot > const &slot, Endpoints const &endpoints) override
Called when mtENDPOINTS is received.
ManagerImp(boost::asio::io_context &ioContext, clock_type &clock, beast::Journal journal, BasicConfig const &config, 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< beast::IP::Endpoint > autoconnect() override
Return a set of addresses we should connect to.
Result activate(std::shared_ptr< Slot > const &slot, PublicKey const &key, bool reserved) override
Request an active slot type.
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
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::optional< boost::asio::executor_work_guard< boost::asio::io_context::executor_type > > work_
std::shared_ptr< SlotImp > ptr
Definition SlotImp.h:16
static std::shared_ptr< Source > make(std::string const &name, Strings const &strings)
Database persistence for PeerFinder using SQLite.
Definition StoreSqdb.h:12
A public key.
Definition PublicKey.h:42
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, BasicConfig const &config, beast::insight::Collector::ptr const &collector)
Create a new Manager.
std::vector< Endpoint > Endpoints
A set of Endpoint used for connecting.
Result
Possible results from activating a slot.
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)