[PATCH 2/4] txn: reverse commit trigger list only before running commit triggers

Vladimir Davydov vdavydov.dev at gmail.com
Fri Jul 19 21:08:40 MSK 2019


Commit triggers must be run in the same order they are added, see commit
013432641283 ("txn: fix execution order of commit triggers"). To achieve
that we added a new trigger method, trigger_add_tail(), which adds new
triggers to the trigger list tail rather than to the head, and now we
use this new method for adding commit triggers.

Come to think of it now, that solution was rather confusing. First,
commit triggers are still added to the head of the list from Lua.
Second, to revert triggers on rollback-to-savepoint it would be really
more convenient to have both commit and rollback trigger lists have the
same order.

So this patch reverts the above-mentioned commit and instead simply
applies rlist_reverse to the commit trigger list before running them.
---
 src/box/txn.c                  | 10 +++++++++-
 src/box/txn.h                  | 13 ++-----------
 src/lib/core/trigger.h         | 11 -----------
 test/engine/transaction.result |  4 ++--
 4 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/src/box/txn.c b/src/box/txn.c
index 73eaee73..0a42d440 100644
--- a/src/box/txn.c
+++ b/src/box/txn.c
@@ -381,8 +381,16 @@ txn_complete(struct txn *txn)
 		/* Commit the transaction. */
 		if (txn->engine != NULL)
 			engine_commit(txn->engine, txn);
-		if (txn->has_triggers)
+		if (txn->has_triggers) {
+			/*
+			 * Commit triggers must be run in the same
+			 * order they were added. Since triggers
+			 * are always added to the list head, we
+			 * need to reverse the list.
+			 */
+			rlist_reverse(&txn->on_commit);
 			txn_run_triggers(txn, &txn->on_commit);
+		}
 
 		double stop_tm = ev_monotonic_now(loop());
 		if (stop_tm - txn->start_tm > too_long_threshold) {
diff --git a/src/box/txn.h b/src/box/txn.h
index baeaa0ed..df173924 100644
--- a/src/box/txn.h
+++ b/src/box/txn.h
@@ -203,16 +203,7 @@ struct txn {
 	 * in case a fiber stops (all engines).
 	 */
 	struct trigger fiber_on_stop;
-	/**
-	 * Commit and rollback triggers.
-	 *
-	 * Note, we commit triggers are added to the tail of
-	 * the list while rollback triggers are added to the
-	 * head, see txn_on_commit() and txn_on_rollback().
-	 * This ensures that the triggers are fired in the
-	 * same order as statements that added them, both on
-	 * commit and on rollback.
-	 */
+	/** Commit and rollback triggers. */
 	struct rlist on_commit, on_rollback;
 	struct sql_txn *psql_txn;
 };
@@ -282,7 +273,7 @@ static inline void
 txn_on_commit(struct txn *txn, struct trigger *trigger)
 {
 	txn_init_triggers(txn);
-	trigger_add_tail(&txn->on_commit, trigger);
+	trigger_add(&txn->on_commit, trigger);
 }
 
 static inline void
diff --git a/src/lib/core/trigger.h b/src/lib/core/trigger.h
index 4bc4da1a..d3ffb9a8 100644
--- a/src/lib/core/trigger.h
+++ b/src/lib/core/trigger.h
@@ -84,17 +84,6 @@ trigger_add(struct rlist *list, struct trigger *trigger)
 	rlist_add_entry(list, trigger, link);
 }
 
-/**
- * Same as trigger_add(), but adds a trigger to the tail of the list
- * rather than to the head. Use it when you need to ensure a certain
- * execution order among triggers.
- */
-static inline void
-trigger_add_tail(struct rlist *list, struct trigger *trigger)
-{
-	rlist_add_tail_entry(list, trigger, link);
-}
-
 static inline void
 trigger_add_unique(struct rlist *list, struct trigger *trigger)
 {
diff --git a/test/engine/transaction.result b/test/engine/transaction.result
index b18b025c..910abb58 100644
--- a/test/engine/transaction.result
+++ b/test/engine/transaction.result
@@ -377,10 +377,10 @@ inspector:cmd("setopt delimiter ''");
 -- Yes, the order is reversed. Like all other Lua triggers.
 call_list
 ---
-- - on_commit_3
+- - on_commit_1
+  - on_commit_3
   - on_commit_3
   - on_commit_3
-  - on_commit_1
 ...
 funcs
 ---
-- 
2.11.0




More information about the Tarantool-patches mailing list