From 0b2c175b60b79fdaed2f5371c405b9dff34eca09 Mon Sep 17 00:00:00 2001 From: Michal Jankowski Date: Tue, 10 Nov 2026 18:01:26 +0100 Subject: [PATCH] PXC-5370: Galera FATAL "unexpected trx release state" after replay of a no-op INSERT ... ON DUPLICATE KEY UPDATE https://perconadev.atlassian.net/browse/PXC-5370 Problem: An INSERT ... ON DUPLICATE KEY UPDATE that matches an existing row but does not change it generates no row events. wsrep_compare_records() still appends certification keys for the unchanged row, so a write set with keys but an empty payload is replicated and certified. If such a transaction is BF-aborted after certification (e.g. by a conflicting applier blocked on its next-key lock), it is replayed. Applying the empty payload registers no storage engine, so trans_commit() does not run the wsrep commit hooks and the transaction never enters or leaves the commit order. Wsrep_high_priority_service::commit() already has a fallback that cycles through the commit hooks for this case, but only for the s_executing state (CTAS with an empty result set). A replayer is in s_replaying, so the fallback was skipped. Galera kept the trx in the REPLAYING state and failed in galera_release() with: unexpected trx release state ... MUST_REPLAY->REPLAYING (FATAL) Debug builds hit the assertion ts.state() == TrxHandle::S_COMMITTED in ReplicatorSMM::replay_trx(). Resolution: Apply the same fallback to the s_replaying state, so the replayed empty write set goes through before_commit(), ordered_commit() and after_commit(), which release the commit order and let the provider commit the trx. wsrep-lib already supports s_replaying in before_commit() for high priority clients. Added galera.galera_replay_no_op_trx, which uses Galera sync points to BF-abort and replay a no-op ODKU transaction deterministically. Co-Authored-By: Claude Opus 5.5 --- .../galera/r/galera_replay_no_op_trx.result | 48 ++++++++ .../galera/t/galera_replay_no_op_trx.test | 105 ++++++++++++++++++ sql/wsrep_high_priority_service.cc | 10 +- 3 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 mysql-test/suite/galera/r/galera_replay_no_op_trx.result create mode 100644 mysql-test/suite/galera/t/galera_replay_no_op_trx.test diff --git a/mysql-test/suite/galera/r/galera_replay_no_op_trx.result b/mysql-test/suite/galera/r/galera_replay_no_op_trx.result new file mode 100644 index 00000000000..838f506729b --- /dev/null +++ b/mysql-test/suite/galera/r/galera_replay_no_op_trx.result @@ -0,0 +1,48 @@ +CREATE TABLE t1 (id BIGINT AUTO_INCREMENT PRIMARY KEY, k INT NOT NULL, v INT NOT NULL, UNIQUE KEY uk(k)); +INSERT INTO t1 (k, v) VALUES (10, 100), (20, 100); +START TRANSACTION; +INSERT INTO t1 (k, v) VALUES (20, 100) ON DUPLICATE KEY UPDATE v = VALUES(v); +Warnings: +Warning 1287 'VALUES function' is deprecated and will be removed in a future release. Please use an alias (INSERT INTO ... VALUES (...) AS alias) and replace VALUES(col) in the ON DUPLICATE KEY UPDATE clause with alias.col instead +SET SESSION wsrep_sync_wait = 0; +SET GLOBAL wsrep_provider_options = 'dbug=d,apply_monitor_slave_enter_sync'; +INSERT INTO t1 (k, v) VALUES (15, 1); +SET SESSION wsrep_on = 0; +SET SESSION wsrep_on = 1; +SET GLOBAL wsrep_provider_options = 'dbug='; +SET GLOBAL wsrep_provider_options = 'dbug=d,commit_monitor_master_enter_sync'; +COMMIT; +SET SESSION wsrep_on = 0; +SET SESSION wsrep_on = 1; +SET GLOBAL wsrep_provider_options = 'dbug='; +SET GLOBAL wsrep_provider_options = 'dbug=d,abort_trx_end'; +SET GLOBAL wsrep_provider_options = 'signal=apply_monitor_slave_enter_sync'; +SET SESSION wsrep_on = 0; +SET SESSION wsrep_on = 1; +SET GLOBAL wsrep_provider_options = 'dbug='; +SET GLOBAL wsrep_provider_options = 'signal=abort_trx_end'; +SET GLOBAL wsrep_provider_options = 'signal=commit_monitor_master_enter_sync'; +wsrep_local_replays +1 +SELECT k, v FROM t1 ORDER BY k; +k v +10 100 +15 1 +20 100 +INSERT INTO t1 (k, v) VALUES (30, 1); +SELECT k, v FROM t1 ORDER BY k; +k v +10 100 +15 1 +20 100 +30 1 +INSERT INTO t1 (k, v) VALUES (40, 1); +SELECT k, v FROM t1 ORDER BY k; +k v +10 100 +15 1 +20 100 +30 1 +40 1 +include/assert_grep.inc [No "unexpected trx release state" error in the node_1 error log] +DROP TABLE t1; diff --git a/mysql-test/suite/galera/t/galera_replay_no_op_trx.test b/mysql-test/suite/galera/t/galera_replay_no_op_trx.test new file mode 100644 index 00000000000..6ba0bad4a29 --- /dev/null +++ b/mysql-test/suite/galera/t/galera_replay_no_op_trx.test @@ -0,0 +1,105 @@ +# +# Replay of a local transaction that appended certification keys but made +# no row changes (INSERT ... ON DUPLICATE KEY UPDATE with identical values). +# +# The transaction holds an InnoDB next-key lock on the duplicate unique key +# record, so a remote INSERT into the gap before that record conflicts with +# it in InnoDB, although the two write sets do not conflict in certification. +# If the local transaction already has a higher seqno and waits for commit +# order, the applier BF-aborts it and the transaction must be replayed. +# +# The replayed write set carries no row events. The replayer must still +# go through commit ordering, otherwise Galera keeps the transaction in the +# REPLAYING state and galera_release() reports +# "Internal library error: unexpected trx release state". +# + +--source include/galera_cluster.inc +--source include/have_debug_sync.inc +--source suite/galera/include/galera_have_debug_sync.inc + +--let $wsrep_local_replays_old = `SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'wsrep_local_replays'` + +CREATE TABLE t1 (id BIGINT AUTO_INCREMENT PRIMARY KEY, k INT NOT NULL, v INT NOT NULL, UNIQUE KEY uk(k)); +INSERT INTO t1 (k, v) VALUES (10, 100), (20, 100); + +# Victim: no-op ODKU, locks uk(k=20) and the gap (10, 20) +--connection node_1 +START TRANSACTION; +INSERT INTO t1 (k, v) VALUES (20, 100) ON DUPLICATE KEY UPDATE v = VALUES(v); + +# Block the applier on node #1 before it applies the conflicting write set +--connect node_1a, 127.0.0.1, root, , test, $NODE_MYPORT_1 +SET SESSION wsrep_sync_wait = 0; +--let $galera_sync_point = apply_monitor_slave_enter_sync +--source include/galera_set_sync_point.inc + +# Conflicting transaction: inserts into the gap locked by the victim +--connection node_2 +INSERT INTO t1 (k, v) VALUES (15, 1); + +--connection node_1a +--source include/galera_wait_sync_point.inc +--source include/galera_clear_sync_point.inc + +# The victim gets a higher seqno; block it before the commit monitor +--let $galera_sync_point = commit_monitor_master_enter_sync +--source include/galera_set_sync_point.inc + +--connection node_1 +--send COMMIT + +--connection node_1a +--let $galera_sync_point = apply_monitor_slave_enter_sync commit_monitor_master_enter_sync +--source include/galera_wait_sync_point.inc +--source include/galera_clear_sync_point.inc + +# Let the applier proceed until it BF-aborts the victim +--let $galera_sync_point = abort_trx_end +--source include/galera_set_sync_point.inc +--let $galera_sync_point = apply_monitor_slave_enter_sync +--source include/galera_signal_sync_point.inc +--let $galera_sync_point = abort_trx_end commit_monitor_master_enter_sync +--source include/galera_wait_sync_point.inc + +# Let both transactions proceed; the victim is replayed +--source include/galera_clear_sync_point.inc +--let $galera_sync_point = abort_trx_end +--source include/galera_signal_sync_point.inc +--let $galera_sync_point = commit_monitor_master_enter_sync +--source include/galera_signal_sync_point.inc + +# The replayed commit succeeds +--connection node_1 +--reap + +--let $wsrep_local_replays_new = `SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'wsrep_local_replays'` +--disable_query_log +--eval SELECT $wsrep_local_replays_new - $wsrep_local_replays_old = 1 AS wsrep_local_replays +--enable_query_log + +SELECT k, v FROM t1 ORDER BY k; + +# The node must still be able to commit, apply and replicate +INSERT INTO t1 (k, v) VALUES (30, 1); + +--connection node_2 +--let $wait_condition = SELECT COUNT(*) = 4 FROM t1 +--source include/wait_condition.inc +SELECT k, v FROM t1 ORDER BY k; +INSERT INTO t1 (k, v) VALUES (40, 1); + +--connection node_1 +--let $wait_condition = SELECT COUNT(*) = 5 FROM t1 +--source include/wait_condition.inc +SELECT k, v FROM t1 ORDER BY k; + +# Galera must not report an unexpected release state for the replayed trx +--let $assert_text = No "unexpected trx release state" error in the node_1 error log +--let $assert_file = $MYSQLTEST_VARDIR/log/mysqld.1.err +--let $assert_select = unexpected trx release state +--let $assert_count = 0 +--source include/assert_grep.inc + +DROP TABLE t1; +--disconnect node_1a diff --git a/sql/wsrep_high_priority_service.cc b/sql/wsrep_high_priority_service.cc index 5875911229a..ae97c1edb5e 100644 --- a/sql/wsrep_high_priority_service.cc +++ b/sql/wsrep_high_priority_service.cc @@ -363,13 +363,19 @@ int Wsrep_high_priority_service::commit(const wsrep::ws_handle &ws_handle, if (!is_ordered) { m_thd->wsrep_cs().before_rollback(); m_thd->wsrep_cs().after_rollback(); - } else if (m_thd->wsrep_trx().state() == wsrep::transaction::s_executing) { + } else if (m_thd->wsrep_trx().state() == wsrep::transaction::s_executing || + m_thd->wsrep_trx().state() == wsrep::transaction::s_replaying) { /* Wsrep commit was ordered but it did not go through commit time hooks and remains active. Cycle through commit hooks to release commit order and to make cleanup happen in after_applying() call. - This is a workaround for CTAS with empty result set. + This is a workaround for CTAS with empty result set, and for + write sets without row events, e.g. a replayed INSERT ... ON DUPLICATE + KEY UPDATE which did not change the row but appended certification + keys (see wsrep_compare_records()). Without it the replayed + transaction would never enter commit order and the provider would + keep it in the REPLAYING state. */ WSREP_DEBUG("Commit not finished for applier %u", thd->thread_id()); ret = ret || m_thd->wsrep_cs().before_commit() ||