[Tarantool-patches] [PATCH 1/3] txn: introduce on_wal_write trigger

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sun Jul 5 18:20:14 MSK 2020


With synchronous replication a sycn transaction passes 2 stages:
WAL write + commit. These are separate events on the contrary with
async transactions, where WAL write == commit.

The WAL write event is needed on non-leader nodes to be able to
send an ACK to the master.

Part of #5100
---
 src/box/txn.c | 29 ++++++++++++++++++++++++++++-
 src/box/txn.h | 10 +++++++++-
 2 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/src/box/txn.c b/src/box/txn.c
index 6c333cbed..2fe763c1c 100644
--- a/src/box/txn.c
+++ b/src/box/txn.c
@@ -422,6 +422,25 @@ txn_run_rollback_triggers(struct txn *txn, struct rlist *triggers)
 	}
 }
 
+/* A helper function to process on_wal_write triggers. */
+static void
+txn_run_wal_write_triggers(struct txn *txn)
+{
+	/* Is zero during recovery. */
+	assert(txn->signature >= 0);
+	if (trigger_run(&txn->on_wal_write, txn) != 0) {
+		/*
+		 * As transaction couldn't handle a trigger error so
+		 * there is no option except panic.
+		 */
+		diag_log();
+		unreachable();
+		panic("wal_write trigger failed");
+	}
+	/* WAL write happens only once. */
+	trigger_destroy(&txn->on_wal_write);
+}
+
 /**
  * Complete transaction processing.
  */
@@ -446,8 +465,14 @@ txn_complete(struct txn *txn)
 		/* Commit the transaction. */
 		if (txn->engine != NULL)
 			engine_commit(txn->engine, txn);
-		if (txn_has_flag(txn, TXN_HAS_TRIGGERS))
+		if (txn_has_flag(txn, TXN_HAS_TRIGGERS)) {
 			txn_run_commit_triggers(txn, &txn->on_commit);
+			/*
+			 * For async transactions WAL write ==
+			 * commit.
+			 */
+			txn_run_wal_write_triggers(txn);
+		}
 
 		double stop_tm = ev_monotonic_now(loop());
 		if (stop_tm - txn->start_tm > too_long_threshold) {
@@ -458,6 +483,8 @@ txn_complete(struct txn *txn)
 					     stop_tm - txn->start_tm);
 		}
 	} else {
+		if (txn_has_flag(txn, TXN_HAS_TRIGGERS))
+			txn_run_wal_write_triggers(txn);
 		/*
 		 * Complete is called on every WAL operation
 		 * authored by this transaction. And it not always
diff --git a/src/box/txn.h b/src/box/txn.h
index f88c3815e..c1f06dbdc 100644
--- a/src/box/txn.h
+++ b/src/box/txn.h
@@ -259,7 +259,7 @@ struct txn {
 	 */
 	struct trigger fiber_on_stop;
 	/** Commit and rollback triggers. */
-	struct rlist on_commit, on_rollback;
+	struct rlist on_commit, on_rollback, on_wal_write;
 	/**
 	 * This member represents counter of deferred foreign key
 	 * violations within transaction. DEFERRED mode means
@@ -370,6 +370,7 @@ txn_init_triggers(struct txn *txn)
 	if (!txn_has_flag(txn, TXN_HAS_TRIGGERS)) {
 		rlist_create(&txn->on_commit);
 		rlist_create(&txn->on_rollback);
+		rlist_create(&txn->on_wal_write);
 		txn_set_flag(txn, TXN_HAS_TRIGGERS);
 	}
 }
@@ -388,6 +389,13 @@ txn_on_rollback(struct txn *txn, struct trigger *trigger)
 	trigger_add(&txn->on_rollback, trigger);
 }
 
+static inline void
+txn_on_wal_write(struct txn *txn, struct trigger *trigger)
+{
+	txn_init_triggers(txn);
+	trigger_add(&txn->on_wal_write, trigger);
+}
+
 /**
  * Most statements don't have triggers, and txn objects
  * are created on every access to data, so statements
-- 
2.21.1 (Apple Git-122.3)



More information about the Tarantool-patches mailing list