OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
runtime_locking_shard/impl.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_IMPL_H_
7#define OPENCBDC_TX_SRC_PARSEC_RUNTIME_LOCKING_SHARD_IMPL_H_
8
9#include "interface.hpp"
10#include "replicated_shard.hpp"
13
14#include <map>
15#include <memory>
16#include <unordered_map>
17#include <unordered_set>
18
22 class impl : public interface {
23 public:
26 explicit impl(std::shared_ptr<logging::log> logger);
27
38 auto try_lock(ticket_number_type ticket_number,
39 broker_id_type broker_id,
40 key_type key,
41 lock_type locktype,
42 bool first_lock,
43 try_lock_callback_type result_callback) -> bool override;
44
51 auto prepare(ticket_number_type ticket_number,
52 broker_id_type broker_id,
53 state_update_type state_update,
54 prepare_callback_type result_callback) -> bool override;
55
61 auto commit(ticket_number_type ticket_number,
62 commit_callback_type result_callback) -> bool override;
63
69 auto rollback(ticket_number_type ticket_number,
70 rollback_callback_type result_callback) -> bool override;
71
76 auto finish(ticket_number_type ticket_number,
77 finish_callback_type result_callback) -> bool override;
78
83 auto get_tickets(broker_id_type broker_id,
84 get_tickets_callback_type result_callback)
85 -> bool override;
86
92 auto recover(const replicated_shard::state_type& state,
93 const replicated_shard::tickets_type& tickets) -> bool;
94
95 private:
96 struct lock_queue_element_type {
97 lock_type m_type;
98 try_lock_callback_type m_callback;
99 };
100
101 struct rw_lock_type {
102 std::optional<ticket_number_type> m_writer;
103 std::unordered_set<ticket_number_type> m_readers;
104 std::map<ticket_number_type, lock_queue_element_type> m_queue;
105 };
106
107 struct state_element_type {
108 value_type m_value;
109 rw_lock_type m_lock;
110 };
111
112 using key_set_type
113 = std::unordered_set<key_type, hashing::const_sip_hash<key_type>>;
114
115 struct ticket_state_type {
117 std::unordered_map<key_type,
118 lock_type,
120 m_locks_held;
121 key_set_type m_queued_locks;
122 state_update_type m_state_update;
123 broker_id_type m_broker_id{};
124 std::optional<wounded_details> m_wounded_details{};
125 };
126
127 struct pending_callback_element_type {
128 try_lock_callback_type m_callback;
129 try_lock_return_type m_returning;
130 ticket_number_type m_ticket_number;
131 };
132
133 using pending_callbacks_list_type
134 = std::vector<pending_callback_element_type>;
135
136 mutable std::mutex m_mut;
137 std::shared_ptr<logging::log> m_log;
138
139 std::unordered_map<key_type,
140 state_element_type,
142 m_state;
143 std::unordered_map<ticket_number_type, ticket_state_type> m_tickets;
144
145 auto
146 wound_tickets(key_type key,
147 const std::vector<ticket_number_type>& blocking_tickets,
148 ticket_number_type blocked_ticket)
149 -> pending_callbacks_list_type;
150
151 static auto get_waiting_on(ticket_number_type ticket_number,
152 lock_type locktype,
153 rw_lock_type& lock)
154 -> std::vector<ticket_number_type>;
155
156 auto release_locks(ticket_number_type ticket_number,
157 ticket_state_type& ticket)
158 -> std::pair<pending_callbacks_list_type, key_set_type>;
159
160 auto acquire_locks(const key_set_type& keys)
161 -> pending_callbacks_list_type;
162
163 auto acquire_lock(const key_type& key,
164 pending_callbacks_list_type& callbacks) -> bool;
165 };
166}
167
168#endif
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
Implementation of a runtime locking shard.
auto rollback(ticket_number_type ticket_number, rollback_callback_type result_callback) -> bool override
Rolls back an uncommitted ticket.
auto recover(const replicated_shard::state_type &state, const replicated_shard::tickets_type &tickets) -> bool
Restores the state of another shard instance.
auto finish(ticket_number_type ticket_number, finish_callback_type result_callback) -> bool override
Deletes a committed or rolled-back ticket.
auto prepare(ticket_number_type ticket_number, broker_id_type broker_id, state_update_type state_update, prepare_callback_type result_callback) -> bool override
Prepares a ticket with the given state updates.
auto commit(ticket_number_type ticket_number, commit_callback_type result_callback) -> bool override
Commits a previously prepared ticket.
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 override
Locks the given key for a ticket and returns the associated value.
auto get_tickets(broker_id_type broker_id, get_tickets_callback_type result_callback) -> bool override
Returns tickets managed by the given broker.
impl(std::shared_ptr< logging::log > logger)
Constructor.
std::function< void(prepare_return_type)> prepare_callback_type
Callback function type for the result of a prepare operation.
std::function< void(get_tickets_return_type)> get_tickets_callback_type
Callback function type for the result of a get tickets operation.
std::variant< value_type, shard_error > try_lock_return_type
Return type from a try lock operation.
std::function< void(rollback_return_type)> rollback_callback_type
Callback function type for the result of a rollback operation.
std::function< void(finish_return_type)> finish_callback_type
Callback function type for the result of a finish 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.
std::unordered_map< ticket_number_type, ticket_type > tickets_type
Type for the tickets list returned by the state machine.
std::unordered_map< key_type, value_type, hashing::const_sip_hash< key_type > > state_type
Type for state updates to a shard.
parsec::ticket_machine::ticket_number_type ticket_number_type
Type for a ticket number.
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.
SipHash function to generate STL data structure hash keys for system IDs.
Definition hashmap.hpp:27