OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
node.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef OPENCBDC_TX_SRC_RAFT_NODE_H_
7#define OPENCBDC_TX_SRC_RAFT_NODE_H_
8
9#include "console_logger.hpp"
10#include "state_manager.hpp"
13
14#include <libnuraft/nuraft.hxx>
15
16namespace cbdc::raft {
18 using result_type = nuraft::cmd_result<nuraft::ptr<nuraft::buffer>>;
19
22 = std::function<void(result_type& r,
23 nuraft::ptr<std::exception>& err)>;
24
32 class node {
33 public:
34 node() = delete;
35 node(const node&) = delete;
36 auto operator=(const node&) -> node& = delete;
37 node(node&&) = delete;
38 auto operator=(node&&) -> node& = delete;
39
54 node(int node_id,
55 std::vector<network::endpoint_t> raft_endpoints,
56 const std::string& node_type,
57 bool blocking,
58 nuraft::ptr<nuraft::state_machine> sm,
59 size_t asio_thread_pool_size,
60 std::shared_ptr<logging::log> logger,
61 nuraft::cb_func::func_type raft_cb);
62
63 ~node();
64
69 auto init(const nuraft::raft_params& raft_params) -> bool;
70
73 [[nodiscard]] auto is_leader() const -> bool;
74
81 [[nodiscard]] auto replicate(nuraft::ptr<nuraft::buffer> new_log,
82 const callback_type& result_fn) const
83 -> bool;
84
91 [[nodiscard]] auto
92 replicate_sync(const nuraft::ptr<nuraft::buffer>& new_log) const
93 -> std::optional<nuraft::ptr<nuraft::buffer>>;
94
97 [[nodiscard]] auto last_log_idx() const -> uint64_t;
98
102 [[nodiscard]] auto get_sm() const -> nuraft::state_machine*;
103
105 void stop();
106
107 private:
108 uint32_t m_node_id;
109 bool m_blocking;
110 int m_port;
111
112 nuraft::ptr<nuraft::logger> m_raft_logger;
113 nuraft::ptr<state_manager> m_smgr;
114 nuraft::ptr<nuraft::state_machine> m_sm;
115 nuraft::raft_launcher m_launcher;
116 nuraft::ptr<nuraft::raft_server> m_raft_instance;
117
118 nuraft::asio_service::options m_asio_opt;
119 nuraft::raft_server::init_options m_init_opts;
120
121 std::shared_ptr<logging::log> m_log;
122 };
123}
124
125#endif // OPENCBDC_TX_SRC_RAFT_NODE_H_
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
A node in a raft cluster.
Definition node.hpp:31
void stop()
Shut down the NuRaft instance.
Definition node.cpp:139
auto replicate(nuraft::ptr< nuraft::buffer > new_log, const callback_type &result_fn) const -> bool
Replicates the given log entry in the cluster.
Definition node.cpp:87
auto get_sm() const -> nuraft::state_machine *
Returns a pointer to the state machine replicated by this raft node.
Definition node.cpp:135
auto init(const nuraft::raft_params &raft_params) -> bool
Initializes the NuRaft instance with the given state machine and raft parameters.
Definition node.cpp:38
auto last_log_idx() const -> uint64_t
Returns the last replicated log index.
Definition node.cpp:131
auto operator=(const node &) -> node &=delete
auto replicate_sync(const nuraft::ptr< nuraft::buffer > &new_log) const -> std::optional< nuraft::ptr< nuraft::buffer > >
Replicates the provided log entry and returns the results from the state machine if the replication w...
Definition node.cpp:101
node(const node &)=delete
auto is_leader() const -> bool
Indicates whether this node is the current raft leader.
Definition node.cpp:83
Implementation of nuraft::state_mgr using a file.
Tools for reading options from a configuration file and building application-specific parameter sets ...
nuraft::cmd_result< nuraft::ptr< nuraft::buffer > > result_type
A NuRaft state machine execution result.
Definition node.hpp:18
std::function< void(result_type &r, nuraft::ptr< std::exception > &err)> callback_type
Function type for raft state machine execution result callbacks.
Definition node.hpp:21