[Tarantool-patches] [PATCH v3 09/13] tmx: introduce prepare sequence number

Aleksandr Lyapunov alyapunov at tarantool.org
Wed Jul 15 16:55:32 MSK 2020


Prepare sequence number is a monotonically increasing ID that is
assigned to any prepared transaction. This ID is suitable for
serialization order resolution: the bigger is ID - the latter the
transaction exist in the serialization order of transactions.

Note that id of transactions has quite different order in case
when transaction could yield - an younger (bigger id) transaction
can prepare/commit first (lower psn) while older tx sleeps in vain.

Also it should be mentioned that LSN has the same order as PSN,
but it has two general differences:
1. The LSN sequence has no holes, i.e. it is a natural number
sequence. This property is useless for transaction engine.
2. The LSN sequence is provided by WAL writer and thus LSN is not
available for TX thas was prepared and haven't been committed yet.
That feature makes psn more suitable sequence for transactions as
it allows to order prepared but not committed transaction and
allows, for example, to create a read view between prepared
transactions.

Part of #4897
---
 src/box/txn.c | 5 +++++
 src/box/txn.h | 5 +++++
 2 files changed, 10 insertions(+)

diff --git a/src/box/txn.c b/src/box/txn.c
index d647fc9..7a15986 100644
--- a/src/box/txn.c
+++ b/src/box/txn.c
@@ -40,6 +40,8 @@
 
 struct tx_manager
 {
+	/** Last prepare-sequence-number that was assigned to prepared TX. */
+	int64_t last_psn;
 };
 
 /** That's a definition, see declaration for description. */
@@ -242,6 +244,7 @@ txn_begin(void)
 	txn->flags = 0;
 	txn->in_sub_stmt = 0;
 	txn->id = ++tsn;
+	txn->psn = 0;
 	txn->status = TXN_INPROGRESS;
 	txn->signature = TXN_SIGNATURE_ROLLBACK;
 	txn->engine = NULL;
@@ -650,6 +653,8 @@ txn_journal_entry_new(struct txn *txn)
 static int
 txn_prepare(struct txn *txn)
 {
+	txn->psn = ++txm.last_psn;
+
 	if (txn_has_flag(txn, TXN_IS_ABORTED_BY_YIELD)) {
 		assert(!txn_has_flag(txn, TXN_CAN_YIELD));
 		diag_set(ClientError, ER_TRANSACTION_YIELD);
diff --git a/src/box/txn.h b/src/box/txn.h
index a2374f3..b5b94fd 100644
--- a/src/box/txn.h
+++ b/src/box/txn.h
@@ -270,6 +270,11 @@ struct txn {
 	 * Valid IDs start from 1.
 	 */
 	int64_t id;
+	/**
+	 * A sequential ID that is assigned when the TX become prepared.
+	 * Transactions are committed in that order.
+	 */
+	int64_t psn;
 	/** Status of the TX */
 	enum txn_status status;
 	/** List of statements in a transaction. */
-- 
2.7.4



More information about the Tarantool-patches mailing list