OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
uhs/atomizer/atomizer/state_machine.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_ATOMIZER_STATE_MACHINE_H_
7#define OPENCBDC_TX_SRC_ATOMIZER_STATE_MACHINE_H_
8
9#include "atomizer.hpp"
10#include "messages.hpp"
11
12#include <libnuraft/nuraft.hxx>
13#include <shared_mutex>
14
15namespace cbdc::atomizer {
20 class state_machine : public nuraft::state_machine {
21 public:
27 state_machine(size_t stxo_cache_depth, std::string snapshot_dir);
28
34
37 = std::variant<make_block_response, get_block_response, errors>;
38
45 [[nodiscard]] auto commit(nuraft::ulong log_idx, nuraft::buffer& data)
46 -> nuraft::ptr<nuraft::buffer> override;
47
50 void commit_config(
51 nuraft::ulong log_idx,
52 nuraft::ptr<nuraft::cluster_config>& /*new_conf*/) override;
53
65 [[nodiscard]] auto
66 read_logical_snp_obj(nuraft::snapshot& s,
67 void*& user_snp_ctx,
68 nuraft::ulong obj_id,
69 nuraft::ptr<nuraft::buffer>& data_out,
70 bool& is_last_obj) -> int override;
71
82 void save_logical_snp_obj(nuraft::snapshot& s,
83 nuraft::ulong& obj_id,
84 nuraft::buffer& data,
85 bool is_first_obj,
86 bool is_last_obj) override;
87
92 [[nodiscard]] auto apply_snapshot(nuraft::snapshot& s)
93 -> bool override;
94
97 [[nodiscard]] auto last_snapshot()
98 -> nuraft::ptr<nuraft::snapshot> override;
99
102 [[nodiscard]] auto last_commit_index() -> nuraft::ulong override;
103
108 void create_snapshot(
109 nuraft::snapshot& s,
110 nuraft::async_result<bool>::handler_type& when_done) override;
111
115 [[nodiscard]] auto tx_notify_count() -> uint64_t;
116
118 using blockstore_t
119 = std::unordered_map<uint64_t, cbdc::atomizer::block>;
120
123 struct snapshot {
125 std::shared_ptr<cbdc::atomizer::atomizer> m_atomizer;
127 nuraft::ptr<nuraft::snapshot> m_snp{};
129 std::shared_ptr<blockstore_t> m_blocks{};
130 };
131
132 private:
133 [[nodiscard]] auto get_snapshot_path(uint64_t idx) const
134 -> std::string;
135
136 [[nodiscard]] auto get_tmp_path() const -> std::string;
137
138 [[nodiscard]] auto read_snapshot(uint64_t idx)
139 -> std::optional<snapshot>;
140
141 static constexpr auto m_tmp_file = "tmp";
142
143 std::atomic<uint64_t> m_last_committed_idx{0};
144
145 std::shared_ptr<cbdc::atomizer::atomizer> m_atomizer;
146 std::shared_ptr<blockstore_t> m_blocks;
147
148 std::atomic<uint64_t> m_tx_notify_count{0};
149
150 std::string m_snapshot_dir;
151
152 size_t m_stxo_cache_depth{};
153
154 std::shared_mutex m_snp_mut;
155 };
156}
157#endif // OPENCBDC_TX_SRC_ATOMIZER_STATE_MACHINE_H_
Raft state machine for managing a replicated atomizer.
auto commit(nuraft::ulong log_idx, nuraft::buffer &data) -> nuraft::ptr< nuraft::buffer > override
Executes the committed the raft log entry at the given index and return the state machine execution r...
std::variant< make_block_response, get_block_response, errors > response
Atomizer state machine response.
std::unordered_map< uint64_t, cbdc::atomizer::block > blockstore_t
Maps block heights to blocks.
auto tx_notify_count() -> uint64_t
Returns the total number of transaction notifications which the state machine has processed.
state_machine(size_t stxo_cache_depth, std::string snapshot_dir)
Constructor.
auto last_commit_index() -> nuraft::ulong override
Returns the index of the most recently committed log entry.
void create_snapshot(nuraft::snapshot &s, nuraft::async_result< bool >::handler_type &when_done) override
Creates a snapshot with the given metadata.
std::variant< aggregate_tx_notify_request, make_block_request, get_block_request, prune_request > request
Atomizer state machine request.
auto apply_snapshot(nuraft::snapshot &s) -> bool override
Replaces the state of the state machine with the state stored in the snapshot referenced by the given...
auto read_logical_snp_obj(nuraft::snapshot &s, void *&user_snp_ctx, nuraft::ulong obj_id, nuraft::ptr< nuraft::buffer > &data_out, bool &is_last_obj) -> int override
Read the portion of the state machine snapshot associated with the given metadata and object ID into ...
void commit_config(nuraft::ulong log_idx, nuraft::ptr< nuraft::cluster_config > &) override
Handler for the raft cluster configuration changes.
void save_logical_snp_obj(nuraft::snapshot &s, nuraft::ulong &obj_id, nuraft::buffer &data, bool is_first_obj, bool is_last_obj) override
Saves the portion of the state machine snapshot associated with the given metadata and object ID into...
auto last_snapshot() -> nuraft::ptr< nuraft::snapshot > override
Returns the most recent snapshot metadata.
Batch of aggregate transaction notifications.
Placeholder struct for a make block state machine request.
Prune blocks request for RPC and state machine.
Represents a snapshot of the state machine with associated metadata.
std::shared_ptr< blockstore_t > m_blocks
Pointer to the state of the block cache.
std::shared_ptr< cbdc::atomizer::atomizer > m_atomizer
Pointer to the atomizer instance.
nuraft::ptr< nuraft::snapshot > m_snp
Pointer to the nuraft snapshot metadata.