OpenCBDC Transaction Processor
|
Class to manage a single distributed transaction (dtx) batch between shards. More...
#include <distributed_tx.hpp>
Public Types | |
enum class | dtx_state { start , prepare , commit , discard , done , failed } |
using | discard_cb_t = std::function<bool(const hash_t&)> |
using | done_cb_t = std::function<bool(const hash_t&)> |
using | commit_cb_t |
using | prepare_cb_t |
Public Member Functions | |
distributed_tx (const hash_t &dtx_id, std::vector< std::shared_ptr< locking_shard::interface > > shards, std::shared_ptr< logging::log > logger) | |
Constructs a new transaction coordinator instance. | |
auto | execute () -> std::optional< std::vector< bool > > |
Executes the dtx batch to completion or failure, either from start, or an intermediate state if one of the recover functions were used. | |
auto | add_tx (const transaction::compact_tx &tx) -> size_t |
Adds a TX to the batch managed by this coordinator and dtx ID. | |
auto | get_id () const -> hash_t |
Returns the dtx ID associated with this coordinator instance. | |
void | set_prepare_cb (const prepare_cb_t &cb) |
Registers a callback to be called before starting the prepare phase of the dtx. | |
void | set_commit_cb (const commit_cb_t &cb) |
Registers a callback to be called before starting the commit phase of the dtx. | |
void | set_discard_cb (const discard_cb_t &cb) |
Registers a callback to be called before the discard phase of the dtx is started. | |
void | set_done_cb (const done_cb_t &cb) |
Registers a callback to be called before the done phase of the dtx is started. | |
void | recover_prepare (const std::vector< transaction::compact_tx > &txs) |
Sets the state of the dtx to prepare and re-adds all the txs included in the batch. | |
void | recover_commit (const std::vector< bool > &complete_txs, const std::vector< std::vector< uint64_t > > &tx_idxs) |
Sets the state of the dtx to commit and sets the state from the end of the prepare phase so that execute() will continue the commit phase. | |
void | recover_discard () |
Sets the state of the dtx to discard so that execute() will start from the discard phase. | |
auto | size () const -> size_t |
Returns the number of transactions in the dtx. | |
auto | get_state () const -> dtx_state |
Returns the current state of the dtx. | |
Class to manage a single distributed transaction (dtx) batch between shards.
Capable of recovering previously failed dtxs and sharing the results of each dtx phase with callback functions (usually for replication).
Definition at line 23 of file distributed_tx.hpp.
Definition at line 55 of file distributed_tx.hpp.
using cbdc::coordinator::distributed_tx::discard_cb_t = std::function<bool(const hash_t&)> |
Definition at line 53 of file distributed_tx.hpp.
using cbdc::coordinator::distributed_tx::done_cb_t = std::function<bool(const hash_t&)> |
Definition at line 54 of file distributed_tx.hpp.
Definition at line 58 of file distributed_tx.hpp.
|
strong |
Definition at line 121 of file distributed_tx.hpp.
cbdc::coordinator::distributed_tx::distributed_tx | ( | const hash_t & | dtx_id, |
std::vector< std::shared_ptr< locking_shard::interface > > | shards, | ||
std::shared_ptr< logging::log > | logger ) |
Constructs a new transaction coordinator instance.
dtx_id | dtx ID for this transaction batch |
shards | vector of locking shards that will participate in the dtx. If recovering a previous dtx, the list must refer to the same shards in the same order. |
logger | logger for messages. |
Definition at line 11 of file distributed_tx.cpp.
auto cbdc::coordinator::distributed_tx::add_tx | ( | const transaction::compact_tx & | tx | ) | -> size_t |
Adds a TX to the batch managed by this coordinator and dtx ID.
Should not be used after calling execute().
tx | compact transaction to add |
Definition at line 147 of file distributed_tx.cpp.
Referenced by recover_prepare().
|
nodiscard |
Executes the dtx batch to completion or failure, either from start, or an intermediate state if one of the recover functions were used.
Definition at line 109 of file distributed_tx.cpp.
References cbdc::to_string().
|
nodiscard |
Returns the dtx ID associated with this coordinator instance.
Definition at line 218 of file distributed_tx.cpp.
|
nodiscard |
Returns the current state of the dtx.
Definition at line 262 of file distributed_tx.cpp.
void cbdc::coordinator::distributed_tx::recover_commit | ( | const std::vector< bool > & | complete_txs, |
const std::vector< std::vector< uint64_t > > & | tx_idxs ) |
Sets the state of the dtx to commit and sets the state from the end of the prepare phase so that execute() will continue the commit phase.
complete_txs | vector of flags indicating which transactions in the batch to complete or cancel. The return value from the prepare phase. |
tx_idxs | vector for each shard indicating which txs in the complete_txs vector apply to each shard. Generated before the prepare phase. |
Definition at line 246 of file distributed_tx.cpp.
References commit.
void cbdc::coordinator::distributed_tx::recover_discard | ( | ) |
Sets the state of the dtx to discard so that execute() will start from the discard phase.
Definition at line 254 of file distributed_tx.cpp.
References discard.
void cbdc::coordinator::distributed_tx::recover_prepare | ( | const std::vector< transaction::compact_tx > & | txs | ) |
Sets the state of the dtx to prepare and re-adds all the txs included in the batch.
txs | list of txs included in the dtx batch |
Definition at line 238 of file distributed_tx.cpp.
References add_tx(), and prepare.
void cbdc::coordinator::distributed_tx::set_commit_cb | ( | const commit_cb_t & | cb | ) |
Registers a callback to be called before starting the commit phase of the dtx.
cb | callback function taking the dtx ID, a vector of flags indicating which transactions to complete, and a vector of vectors indicating the indexes for each shard included in the complete transactions vector. Returns true if the callback operation was successful. Returning false halts further execution of the dtx and sets the dtx state to failed. |
Definition at line 226 of file distributed_tx.cpp.
void cbdc::coordinator::distributed_tx::set_discard_cb | ( | const discard_cb_t & | cb | ) |
Registers a callback to be called before the discard phase of the dtx is started.
cb | callback function taking the dtx ID as argument and returning true if the callback operation was successful. Returning false halts further execution of the dtx and sets the dtx state to failed. |
Definition at line 230 of file distributed_tx.cpp.
void cbdc::coordinator::distributed_tx::set_done_cb | ( | const done_cb_t & | cb | ) |
Registers a callback to be called before the done phase of the dtx is started.
cb | callback function taking the dtx ID as argument and returning true if the callback operation was successful. Returning false halts further execution of the dtx and sets the dtx state to failed. |
Definition at line 234 of file distributed_tx.cpp.
void cbdc::coordinator::distributed_tx::set_prepare_cb | ( | const prepare_cb_t & | cb | ) |
Registers a callback to be called before starting the prepare phase of the dtx.
cb | callback function taking the dtx ID and a vector of transactions in the dtx. Returns true if the callback operation was successful. Returning false halts further execution of the dtx and sets the dtx state to failed. |
Definition at line 222 of file distributed_tx.cpp.
|
nodiscard |
Returns the number of transactions in the dtx.
Definition at line 258 of file distributed_tx.cpp.