Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ClusterCommunicationService.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2025, 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 "cluster/ClioNode.hpp"
23#include "cluster/ClusterCommunicationServiceInterface.hpp"
24#include "data/BackendInterface.hpp"
25#include "util/log/Logger.hpp"
26#include "util/prometheus/Bool.hpp"
27#include "util/prometheus/Gauge.hpp"
28#include "util/prometheus/Prometheus.hpp"
29
30#include <boost/asio/cancellation_signal.hpp>
31#include <boost/asio/spawn.hpp>
32#include <boost/asio/strand.hpp>
33#include <boost/asio/thread_pool.hpp>
34#include <boost/uuid/uuid.hpp>
35
36#include <atomic>
37#include <chrono>
38#include <latch>
39#include <memory>
40#include <string>
41#include <vector>
42
43namespace cluster {
44
50 "cluster_nodes_total_number",
51 {},
52 "Total number of nodes this node can detect in the cluster."
53 );
55 "cluster_communication_is_healthy",
56 {},
57 "Whether cluster communication service is operating healthy (1 - healthy, 0 - we have a problem)"
58 );
59
60 // TODO: Use util::async::CoroExecutionContext after https://github.com/XRPLF/clio/issues/1973 is implemented
61 boost::asio::thread_pool ctx_{1};
62 boost::asio::strand<boost::asio::thread_pool::executor_type> strand_ = boost::asio::make_strand(ctx_);
63
64 util::Logger log_{"ClusterCommunication"};
65
66 std::shared_ptr<data::BackendInterface> backend_;
67
68 std::chrono::steady_clock::duration readInterval_;
69 std::chrono::steady_clock::duration writeInterval_;
70
71 boost::asio::cancellation_signal cancelSignal_;
72 std::latch finishedCountdown_;
73 std::atomic_bool running_ = false;
74 bool stopped_ = false;
75
76 ClioNode selfData_;
77 std::vector<ClioNode> otherNodesData_;
78
79public:
80 static constexpr std::chrono::milliseconds kDEFAULT_READ_INTERVAL{2100};
81 static constexpr std::chrono::milliseconds kDEFAULT_WRITE_INTERVAL{1200};
90 std::shared_ptr<data::BackendInterface> backend,
91 std::chrono::steady_clock::duration readInterval = kDEFAULT_READ_INTERVAL,
92 std::chrono::steady_clock::duration writeInterval = kDEFAULT_WRITE_INTERVAL
93 );
94
96
100 void
101 run();
102
106 void
107 stop();
108
112 operator=(ClusterCommunicationService&&) = delete;
114 operator=(ClusterCommunicationService const&) = delete;
115
121 std::shared_ptr<boost::uuids::uuid>
122 selfUuid() const;
123
130 selfData() const override;
131
137 std::expected<std::vector<ClioNode>, std::string>
138 clusterData() const override;
139
140private:
141 void
142 doRead(boost::asio::yield_context yield);
143
144 void
145 doWrite();
146};
147
148} // namespace cluster
static util::prometheus::Bool boolMetric(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get a bool based metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:194
static util::prometheus::GaugeInt & gaugeInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:216
Interface for the cluster communication service.
Definition ClusterCommunicationServiceInterface.hpp:33
Service to post and read messages to/from the cluster. It uses a backend to communicate with the clus...
Definition ClusterCommunicationService.hpp:48
std::shared_ptr< boost::uuids::uuid > selfUuid() const
Get the UUID of the current node.
Definition ClusterCommunicationService.cpp:143
void run()
Start the service.
Definition ClusterCommunicationService.cpp:75
ClusterCommunicationService(std::shared_ptr< data::BackendInterface > backend, std::chrono::steady_clock::duration readInterval=kDEFAULT_READ_INTERVAL, std::chrono::steady_clock::duration writeInterval=kDEFAULT_WRITE_INTERVAL)
Construct a new Cluster Communication Service object.
Definition ClusterCommunicationService.cpp:56
std::expected< std::vector< ClioNode >, std::string > clusterData() const override
Get the data of all nodes in the cluster (including self).
Definition ClusterCommunicationService.cpp:158
void stop()
Stop the service.
Definition ClusterCommunicationService.cpp:122
ClioNode selfData() const override
Get the data of the current node.
Definition ClusterCommunicationService.cpp:150
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:87
Represents a node in the cluster.
Definition ClioNode.hpp:34
A prometheus gauge metric implementation. It can be increased, decreased or set to a value.
Definition Gauge.hpp:39