OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
uhs/twophase/coordinator/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_COORDINATOR_STATE_MACHINE_H_
7#define OPENCBDC_TX_SRC_COORDINATOR_STATE_MACHINE_H_
8
11
12#include <libnuraft/nuraft.hxx>
13#include <unordered_map>
14#include <unordered_set>
15
16namespace cbdc::coordinator {
21 class state_machine final : public nuraft::state_machine {
22 public:
27 explicit state_machine(std::shared_ptr<logging::log> logger);
28
30 enum class command : uint8_t {
31 prepare = 0,
32 commit = 1,
33 discard = 2,
34 done = 3,
35 get = 4
36 };
37
44 std::unordered_map<hash_t,
45 nuraft::ptr<nuraft::buffer>,
50 std::unordered_map<hash_t,
51 nuraft::ptr<nuraft::buffer>,
55 std::unordered_set<hash_t, cbdc::hashing::const_sip_hash<hash_t>>
57 };
58
65 auto commit(uint64_t log_idx, nuraft::buffer& data)
66 -> nuraft::ptr<nuraft::buffer> override;
67
70 void commit_config(
71 nuraft::ulong log_idx,
72 nuraft::ptr<nuraft::cluster_config>& /*new_conf*/) override;
73
76 auto apply_snapshot(nuraft::snapshot& /* s */) -> bool override;
77
80 auto last_snapshot() -> nuraft::ptr<nuraft::snapshot> override;
81
83 auto last_commit_index() -> uint64_t override;
84
86 void create_snapshot(
87 nuraft::snapshot& /* s */,
88 nuraft::async_result<bool>::handler_type& when_done) override;
89
90 private:
91 std::atomic<uint64_t> m_last_committed_idx{0};
92 coordinator_state m_state{};
93 std::shared_ptr<logging::log> m_logger;
94 };
95}
96
97#endif // OPENCBDC_TX_SRC_COORDINATOR_STATE_MACHINE_H_
Raft state machine for managing a replicated coordinator.
auto apply_snapshot(nuraft::snapshot &) -> bool override
Not implemented for coordinators.
void commit_config(nuraft::ulong log_idx, nuraft::ptr< nuraft::cluster_config > &) override
Handler for the raft cluster configuration changes.
command
Types of command the state machine can process.
@ prepare
Stores a dtx in the prepare phase.
@ done
Clears the dtx from the coordinator state.
@ discard
Moves a dtx from commit to discard.
@ get
Retrieves all active dtxs.
@ commit
Moves a dtx from prepare to commit.
state_machine(std::shared_ptr< logging::log > logger)
Constructor.
auto last_snapshot() -> nuraft::ptr< nuraft::snapshot > override
Not implemented for coordinators.
auto last_commit_index() -> uint64_t override
Returns the index of the last-committed command.
void create_snapshot(nuraft::snapshot &, nuraft::async_result< bool >::handler_type &when_done) override
Not implemented for coordinators.
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
Used to store dtxs, which phase they are in and relevant data require for recovery.
std::unordered_set< hash_t, cbdc::hashing::const_sip_hash< hash_t > > m_discard_txs
Set of dtx IDs in the discard phase.
std::unordered_map< hash_t, nuraft::ptr< nuraft::buffer >, cbdc::hashing::const_sip_hash< hash_t > > m_prepare_txs
Maps dtx IDs in the prepare phase to a byte array containing relevant data for recovery.
std::unordered_map< hash_t, nuraft::ptr< nuraft::buffer >, cbdc::hashing::const_sip_hash< hash_t > > m_commit_txs
Maps dtx IDs in the commit phase to a byte array containing relevant data for recovery.
SipHash function to generate STL data structure hash keys for system IDs.
Definition hashmap.hpp:27