OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
parsec/runtime_locking_shard/interface.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_PARSEC_RUNTIME_LOCKING_SHARD_INTERFACE_H_
7#define OPENCBDC_TX_SRC_PARSEC_RUNTIME_LOCKING_SHARD_INTERFACE_H_
8
12
13#include <functional>
14#include <unordered_map>
15
24 using broker_id_type = size_t;
25
27 using state_update_type = std::
28 unordered_map<key_type, value_type, hashing::const_sip_hash<key_type>>;
29
31 enum class lock_type : uint8_t {
33 read = 0,
35 write = 1,
36 };
37
65
74
75 // An error occurring
76 struct shard_error {
79
81 std::optional<wounded_details> m_wounded_details;
82 };
83
85 enum class ticket_state : uint8_t {
87 begun,
89 wounded,
94 };
95
109 class interface {
110 public:
111 virtual ~interface() = default;
112
113 interface() = default;
114 interface(const interface&) = delete;
115 auto operator=(const interface&) -> interface& = delete;
116 interface(interface&&) = delete;
117 auto operator=(interface&&) -> interface& = delete;
118
121 using try_lock_return_type = std::variant<value_type, shard_error>;
124 = std::function<void(try_lock_return_type)>;
125
139 virtual auto try_lock(ticket_number_type ticket_number,
140 broker_id_type broker_id,
141 key_type key,
142 lock_type locktype,
143 bool first_lock,
144 try_lock_callback_type result_callback) -> bool
145 = 0;
146
148 using prepare_return_type = std::optional<shard_error>;
150 using prepare_callback_type = std::function<void(prepare_return_type)>;
151
160 virtual auto prepare(ticket_number_type ticket_number,
161 broker_id_type broker_id,
162 state_update_type state_update,
163 prepare_callback_type result_callback) -> bool
164 = 0;
165
167 using commit_return_type = std::optional<shard_error>;
169 using commit_callback_type = std::function<void(commit_return_type)>;
170
177 virtual auto commit(ticket_number_type ticket_number,
178 commit_callback_type result_callback) -> bool
179 = 0;
180
183 using rollback_return_type = std::optional<shard_error>;
186 = std::function<void(rollback_return_type)>;
187
195 virtual auto rollback(ticket_number_type ticket_number,
196 rollback_callback_type result_callback) -> bool
197 = 0;
200 using finish_return_type = std::optional<shard_error>;
202 using finish_callback_type = std::function<void(finish_return_type)>;
203
211 virtual auto finish(ticket_number_type ticket_number,
212 finish_callback_type result_callback) -> bool
213 = 0;
214
218 = std::unordered_map<ticket_number_type, ticket_state>;
222 = std::variant<get_tickets_success_type, error_code>;
225 = std::function<void(get_tickets_return_type)>;
231 virtual auto get_tickets(broker_id_type broker_id,
232 get_tickets_callback_type result_callback)
233 -> bool
234 = 0;
235 };
236}
237
238#endif
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
std::function< void(prepare_return_type)> prepare_callback_type
Callback function type for the result of a prepare operation.
std::optional< shard_error > prepare_return_type
Return type from a prepare operation. An error, if applicable.
virtual auto rollback(ticket_number_type ticket_number, rollback_callback_type result_callback) -> bool=0
Releases any locks held by a ticket and returns it to a clean state.
std::optional< shard_error > commit_return_type
Return type from a commit operation. An error code, if applicable.
std::optional< shard_error > finish_return_type
Return type from a finish operation. An error code, if applicable.
virtual auto get_tickets(broker_id_type broker_id, get_tickets_callback_type result_callback) -> bool=0
Returns all unfinished tickets managed with the given broker ID.
std::unordered_map< ticket_number_type, ticket_state > get_tickets_success_type
Return type from a successful get tickets operation.
virtual auto commit(ticket_number_type ticket_number, commit_callback_type result_callback) -> bool=0
Commits the state updates from a previously prepared ticket.
virtual auto try_lock(ticket_number_type ticket_number, broker_id_type broker_id, key_type key, lock_type locktype, bool first_lock, try_lock_callback_type result_callback) -> bool=0
Requests a lock on the given key and returns the value associated with the key.
std::function< void(get_tickets_return_type)> get_tickets_callback_type
Callback function type for the result of a get tickets operation.
auto operator=(const interface &) -> interface &=delete
virtual auto finish(ticket_number_type ticket_number, finish_callback_type result_callback) -> bool=0
Removes a ticket from the shard's internal state.
auto operator=(interface &&) -> interface &=delete
std::variant< value_type, shard_error > try_lock_return_type
Return type from a try lock operation.
std::optional< shard_error > rollback_return_type
Return type from a rollback operation.
std::function< void(rollback_return_type)> rollback_callback_type
Callback function type for the result of a rollback operation.
virtual auto prepare(ticket_number_type ticket_number, broker_id_type broker_id, state_update_type state_update, prepare_callback_type result_callback) -> bool=0
Prepares a ticket with the given state updates to be applied if the ticket is subsequently committed.
std::function< void(finish_return_type)> finish_callback_type
Callback function type for the result of a finish operation.
std::variant< get_tickets_success_type, error_code > get_tickets_return_type
Return type from a get tickets operation.
std::function< void(try_lock_return_type)> try_lock_callback_type
Function type for try lock operation results.
std::function< void(commit_return_type)> commit_callback_type
Callback function type for the result of a commit operation.
parsec::ticket_machine::ticket_number_type ticket_number_type
Type for a ticket number.
error_code
Error codes returned by methods on shards.
@ wounded
Request invalid because ticket is in the wounded state.
@ lock_not_held
Cannot apply requested state update because the ticket does not hold a write lock on the given key.
@ internal_error
Request failed because of a transient internal error.
@ not_committed
Request invalid because ticket is not in the committed state.
@ lock_held
The ticket already holds the requested lock.
@ lock_queued
The requested lock is already queued for the given ticket.
@ prepared
Request invalid because ticket is in the prepared state.
@ state_update_with_read_lock
Cannot apply requested state update because the ticket only holds a read lock on the given key.
@ not_prepared
Cannot commit the ticket because the ticket has not been prepared.
@ unknown_ticket
The given ticket number is not known to this shard.
@ committed
Request invalid because ticket is in the committed state.
std:: unordered_map< key_type, value_type, hashing::const_sip_hash< key_type > > state_update_type
Type for state updates to a shard. A map of keys and their new values.
ticket_state
Ticket states returned by shards for broker recovery purposes.
@ begun
Begun, may still hold locks or be rolled-back.
@ read
Read lock. Multiple readers can hold a lock for the same key.
@ write
Write lock. Only one ticket can hold this lock at a time.
uint64_t ticket_number_type
Type alias for a ticket number.
@ buffer
A singular RLP value (byte array)
std::optional< wounded_details > m_wounded_details
Optional details about the wounded error code.
key_type m_wounding_key
The key that triggered the other ticket to wound.
ticket_number_type m_wounding_ticket
The ticket that caused wounding.