[PATCH 3/5] txn: fix execution order of commit triggers

Vladimir Davydov vdavydov.dev at gmail.com
Fri Jul 5 23:25:29 MSK 2019


Both commit and rollback triggers are currently added to the list head.
As a result, they are both run in the reverse order. This is correct for
rollback triggers, because this matches the order in which statements
that added the triggers are rolled back, but this is wrong for commit
triggers. For example, suppose we create a space and then create an
index for it in the same transaction. We expect that on success we first
run the trigger that commits the space and only then the trigger that
commits the index, not vice versa. That said, reverse the order of
commit triggers in the scope of preparations for transactional DDL.
---
 src/box/txn.h          | 13 +++++++++++--
 src/lib/core/trigger.h | 11 +++++++++++
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/box/txn.h b/src/box/txn.h
index a19becce..d1ef220a 100644
--- a/src/box/txn.h
+++ b/src/box/txn.h
@@ -198,7 +198,16 @@ struct txn {
 	 * in case a fiber stops (all engines).
 	 */
 	struct trigger fiber_on_stop;
-	 /** Commit and rollback triggers */
+	/**
+	 * 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.
+	 */
 	struct rlist on_commit, on_rollback;
 	struct sql_txn *psql_txn;
 };
@@ -279,7 +288,7 @@ static inline void
 txn_on_commit(struct txn *txn, struct trigger *trigger)
 {
 	txn_init_triggers(txn);
-	trigger_add(&txn->on_commit, trigger);
+	trigger_add_tail(&txn->on_commit, trigger);
 }
 
 static inline void
diff --git a/src/lib/core/trigger.h b/src/lib/core/trigger.h
index d3ffb9a8..4bc4da1a 100644
--- a/src/lib/core/trigger.h
+++ b/src/lib/core/trigger.h
@@ -84,6 +84,17 @@ 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)
 {
-- 
2.11.0




More information about the Tarantool-patches mailing list