OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
parsec/runtime_locking_shard/state_machine.cpp
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#include "state_machine.hpp"
7
8#include "format.hpp"
9#include "util/raft/util.hpp"
10#include "util/rpc/format.hpp"
13
15 auto state_machine::commit(uint64_t log_idx, nuraft::buffer& data)
16 -> nuraft::ptr<nuraft::buffer> {
17 auto maybe_req = from_buffer<rpc::replicated_request>(data);
18 if(!maybe_req.has_value()) {
19 // TODO: This would only happen if there was a deserialization
20 // error with the request. Maybe we should abort here as such an
21 // event would imply a bug in the coordinator.
22 return nullptr;
23 }
24
25 auto&& req = maybe_req.value();
26 auto resp = process_request(req);
27
29 nuraft::ptr<nuraft::buffer>>(resp);
30
31 m_last_committed_idx = log_idx;
32
33 return resp_buf;
34 }
35
36 auto state_machine::apply_snapshot(nuraft::snapshot& /* s */) -> bool {
37 return false;
38 }
39
40 auto state_machine::last_snapshot() -> nuraft::ptr<nuraft::snapshot> {
41 return nullptr;
42 }
43
45 return m_last_committed_idx;
46 }
47
49 nuraft::snapshot& /* s */,
50 nuraft::async_result<bool>::handler_type& when_done) {
51 nuraft::ptr<std::exception> except(nullptr);
52 bool ret = false;
53 when_done(ret, except);
54 }
55
56 auto state_machine::process_request(const rpc::replicated_request& req)
58 auto ret = rpc::replicated_response();
59 [[maybe_unused]] auto success = std::visit(
61 [&](const rpc::replicated_prepare_request& msg) {
62 return m_shard->prepare(
63 msg.m_ticket_number,
64 msg.m_broker_id,
65 msg.m_state_update,
67 ret = res;
68 });
69 },
70 [&](rpc::commit_request msg) {
71 return m_shard->commit(
72 msg.m_ticket_number,
74 ret = res;
75 });
76 },
77 [&](rpc::finish_request msg) {
78 return m_shard->finish(
79 msg.m_ticket_number,
81 ret = res;
82 });
83 },
84 [&](rpc::replicated_get_tickets_request /* msg */) {
85 return m_shard->get_tickets(
87 res) {
88 ret = res;
89 });
90 }},
91 req);
92 assert(success);
93 return ret;
94 }
95
97 -> std::shared_ptr<replicated_shard> {
98 return m_shard;
99 }
100}
std::variant< tickets_type, error_code > get_tickets_return_type
Return type from a get tickets operation.
std::optional< error_code > return_type
Return type from a prepare operation. An error, if applicable.
Implementation of the replicated shard interface.
auto apply_snapshot(nuraft::snapshot &) -> bool override
Not implemented for runtime locking shard.
auto last_snapshot() -> nuraft::ptr< nuraft::snapshot > override
Not implemented for runtime locking shard.
void create_snapshot(nuraft::snapshot &, nuraft::async_result< bool >::handler_type &) override
Not implemented for runtime locking shard.
auto get_shard() const -> std::shared_ptr< replicated_shard >
Returns the replicated shard implementation managed by the state machine.
auto last_commit_index() -> uint64_t override
Returns the most recently committed log entry index.
auto commit(uint64_t log_idx, nuraft::buffer &data) -> nuraft::ptr< nuraft::buffer > override
Commit the given raft log entry at the given log index, and return the result.
std::variant< replicated_prepare_request, commit_request, finish_request, replicated_get_tickets_request > replicated_request
Shard replicated state machine request type.
std::variant< replicated_shard_interface::return_type, replicated_shard_interface::get_tickets_return_type > replicated_response
Shard replicated state machine response type.
auto from_buffer(nuraft::buffer &buf) -> std::optional< T >
Deserialize object of given type from a nuraft::buffer.
auto make_buffer(const T &obj) -> std::enable_if_t< std::is_same_v< B, nuraft::ptr< nuraft::buffer > >, nuraft::ptr< nuraft::buffer > >
Serialize object into nuraft::buffer using a cbdc::nuraft_serializer.
Variant handler template.