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 <string>
38#include <vector>
39
40namespace cluster {
41
47 "cluster_nodes_total_number",
48 {},
49 "Total number of nodes this node can detect in the cluster."
50 );
52 "cluster_communication_is_healthy",
53 {},
54 "Whether cluster communication service is operating healthy (1 - healthy, 0 - we have a problem)"
55 );
56
57 // TODO: Use util::async::CoroExecutionContext after https://github.com/XRPLF/clio/issues/1973 is implemented
58 boost::asio::thread_pool ctx_{1};
59 boost::asio::strand<boost::asio::thread_pool::executor_type> strand_ = boost::asio::make_strand(ctx_);
60
61 util::Logger log_{"ClusterCommunication"};
62
63 std::shared_ptr<data::BackendInterface> backend_;
64
65 std::chrono::steady_clock::duration readInterval_;
66 std::chrono::steady_clock::duration writeInterval_;
67
68 ClioNode selfData_;
69 std::vector<ClioNode> otherNodesData_;
70
71 bool stopped_ = false;
72
73public:
74 static constexpr std::chrono::milliseconds kDEFAULT_READ_INTERVAL{2100};
75 static constexpr std::chrono::milliseconds kDEFAULT_WRITE_INTERVAL{1200};
84 std::shared_ptr<data::BackendInterface> backend,
85 std::chrono::steady_clock::duration readInterval = kDEFAULT_READ_INTERVAL,
86 std::chrono::steady_clock::duration writeInterval = kDEFAULT_WRITE_INTERVAL
87 );
88
90
94 void
95 run();
96
100 void
101 stop();
102
106 operator=(ClusterCommunicationService&&) = delete;
108 operator=(ClusterCommunicationService const&) = delete;
109
115 std::shared_ptr<boost::uuids::uuid>
116 selfUuid() const;
117
124 selfData() const override;
125
131 std::expected<std::vector<ClioNode>, std::string>
132 clusterData() const override;
133
134private:
135 void
136 doRead(boost::asio::yield_context yield);
137
138 void
139 doWrite();
140};
141
142} // 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:33
Service to post and read messages to/from the cluster. It uses a backend to communicate with the clus...
Definition ClusterCommunicationService.hpp:45
std::shared_ptr< boost::uuids::uuid > selfUuid() const
Get the UUID of the current node.
Definition ClusterCommunicationService.cpp:101
void run()
Start the service.
Definition ClusterCommunicationService.cpp:63
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:45
std::expected< std::vector< ClioNode >, std::string > clusterData() const override
Get the data of all nodes in the cluster (including self).
Definition ClusterCommunicationService.cpp:116
void stop()
Stop the service.
Definition ClusterCommunicationService.cpp:90
ClioNode selfData() const override
Get the data of the current node.
Definition ClusterCommunicationService.cpp:108
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