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/spawn.hpp>
31#include <boost/asio/strand.hpp>
32#include <boost/asio/thread_pool.hpp>
33#include <boost/uuid/uuid.hpp>
34
35#include <chrono>
36#include <memory>
37#include <vector>
38
39namespace cluster {
40
46 "cluster_nodes_total_number",
47 {},
48 "Total number of nodes this node can detect in the cluster."
49 );
51 "cluster_communication_is_healthy",
52 {},
53 "Whether cluster communicaton service is operating healthy (1 - healthy, 0 - we have a problem)"
54 );
55
56 // TODO: Use util::async::CoroExecutionContext after https://github.com/XRPLF/clio/issues/1973 is implemented
57 boost::asio::thread_pool ctx_{1};
58 boost::asio::strand<boost::asio::thread_pool::executor_type> strand_ = boost::asio::make_strand(ctx_);
59
60 util::Logger log_{"ClusterCommunication"};
61
62 std::shared_ptr<data::BackendInterface> backend_;
63
64 std::chrono::steady_clock::duration readInterval_;
65 std::chrono::steady_clock::duration writeInterval_;
66
67 ClioNode selfData_;
68 std::vector<ClioNode> otherNodesData_;
69
70 bool stopped_ = false;
71
72public:
73 static constexpr std::chrono::milliseconds kDEFAULT_READ_INTERVAL{2100};
74 static constexpr std::chrono::milliseconds kDEFAULT_WRITE_INTERVAL{1200};
83 std::shared_ptr<data::BackendInterface> backend,
84 std::chrono::steady_clock::duration readInterval = kDEFAULT_READ_INTERVAL,
85 std::chrono::steady_clock::duration writeInterval = kDEFAULT_WRITE_INTERVAL
86 );
87
89
93 void
94 run();
95
99 void
100 stop();
101
105 operator=(ClusterCommunicationService&&) = delete;
107 operator=(ClusterCommunicationService const&) = delete;
108
114 std::shared_ptr<boost::uuids::uuid>
115 selfUuid() const;
116
123 selfData() const override;
124
130 std::vector<ClioNode>
131 clusterData() const override;
132
133private:
134 void
135 doRead(boost::asio::yield_context yield);
136
137 void
138 doWrite();
139};
140
141} // 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:188
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:210
Interface for the cluster communication service.
Definition ClusterCommunicationServiceInterface.hpp:31
Service to post and read messages to/from the cluster. It uses a backend to communicate with the clus...
Definition ClusterCommunicationService.hpp:44
std::shared_ptr< boost::uuids::uuid > selfUuid() const
Get the UUID of the current node.
Definition ClusterCommunicationService.cpp:100
void run()
Start the service.
Definition ClusterCommunicationService.cpp:62
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:44
std::vector< ClioNode > clusterData() const override
Get the data of all nodes in the cluster (including self).
Definition ClusterCommunicationService.cpp:115
void stop()
Stop the service.
Definition ClusterCommunicationService.cpp:89
ClioNode selfData() const override
Get the data of the current node.
Definition ClusterCommunicationService.cpp:107
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
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