mariadb/mysql-test/suite/binlog_in_engine/xa_innodb_crash.result
Kristian Nielsen 3fbb3591bc Binlog-in-engine: Implement internal 2pc and user XA recovery.
When using the InnoDB-implemented binlog with another transactional storage
engine, or with explicit user XA transactions, recover such transactions
consistently from the binlog at server startup.

When a transaction is prepared with an XID, the binlog records a "prepare"
record containing the XID and link to the out-of-band replication event
data.

When a previously prepared transaction is committed, the commit record links
to the oob data referenced from the prepare record, and the record is
preceeded by an "XA complete" record containing the XID.

If instead a prepared transaction is rolled back, just an "XA complete"
record is binlogged with the XID and a "rollback" flag.

While any prepared XA transactions are active, maintain in-memory reference
counts in each binlog file, and in each binlog file record the file_no of
the earliest binlog file containing any XID records of still active
transactions.

When the server restarts (possibly after crash), look up the file_no of the
earliest binlog file that may contain active XID records, if any. Scan the
binlogs from that point and record any XID prepare or complete records.

For any XID prepare record, record oob data and reference count, recovering
the in-memory state present before the server restart. Return a hash to the
server layer containing each active XID in the binlog and its state
(prepared, committed, rolled back).

On the server layer, ask each engine for a list of pending XID in prepared
state. If the binlog state of an XID is committed, commit in the engine. If
the binlog state is rolled back or is missing, roll back in the engine. If
the binlog state is prepared, _and_ all participating engines have the
transaction prepared also, then leave the transaction prepared. If a binlog
prepared transaction is missing from an engine, then roll it back in any
other engines and in the binlog (this is to handle a crash in the middle of
an XA PREPARE).

The result is that multi-engine (or non-InnoDB) transactions, as well as
user XA transactions, will be recovered after a crash consisent with the
binlog content.

Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
2026-01-16 23:05:04 +01:00

167 lines
6.2 KiB
Text

include/reset_master.inc
*** Test XA PREPARE, COMMIT, ROLLBACK with crashes at various points.
CREATE TABLE t1(a INT NOT NULL, b INT NOT NULL, c LONGBLOB, PRIMARY KEY(a, b)) ENGINE=InnoDB;
INSERT INTO t1 VALUES (0, 1, '');
INSERT INTO t1 VALUES (0, 2, '');
INSERT INTO t1 VALUES (0, 3, '');
*** x1: prepared in binlog but not in engine/tables.
connect con1,localhost,root,,;
XA BEGIN 'x1';
INSERT INTO t1 VALUES (1, 1, '');
INSERT INTO t1 VALUES (1, 2, REPEAT('<x1>', 10000));
*** x2: prepared in binlog and engine.
connect con2,localhost,root,,;
XA BEGIN 'x2';
INSERT INTO t1 VALUES (2, 1, REPEAT('<x2>', 10000));
*** x3: committed in binlog, not in engine.
connect con3,localhost,root,,;
XA BEGIN 'x3';
INSERT INTO t1 VALUES (3, 1, '');
connection default;
FLUSH BINARY LOGS;
*** x4: committed in binlog and in engine.
connect con4,localhost,root,,;
XA BEGIN 'x4';
INSERT INTO t1 VALUES (4, 1, REPEAT('<x4>', 10000));
*** x5: rolled back in binlog, not in engine.
connect con5,localhost,root,,;
XA BEGIN 'x5';
INSERT INTO t1 VALUES (5, 1, '');
*** x6: rolled back in binlog and in engine.
connect con6,localhost,root,,;
XA BEGIN 'x6';
INSERT INTO t1 VALUES (6, 1, REPEAT('<x4>', 10000));
connection default;
FLUSH BINARY LOGS;
connection con3;
INSERT INTO t1 VALUES (3, 2, '');
XA END 'x3';
XA PREPARE 'x3';
SET debug_sync= 'ibb_after_commit_redo_log SIGNAL con3_rdy WAIT_FOR crash';
XA COMMIT 'x3';
connection default;
SET debug_sync= 'now WAIT_FOR con3_rdy';
connection con1;
INSERT INTO t1 VALUES (1, 3, '');
XA END 'x1';
SET debug_sync= 'ibb_after_prepare_redo_log SIGNAL con1_rdy WAIT_FOR crash';
XA PREPARE 'x1';
connection default;
SET debug_sync= 'now WAIT_FOR con1_rdy';
connection con6;
INSERT INTO t1 VALUES (6, 2, '');
XA END 'x6';
XA PREPARE 'x6';
SET debug_sync= 'rollback_handlerton_after SIGNAL con6_rdy WAIT_FOR crash';
XA ROLLBACK 'x6';
connection default;
SET debug_sync= 'now WAIT_FOR con6_rdy';
connection con5;
INSERT INTO t1 VALUES (5, 2, '');
XA END 'x5';
XA PREPARE 'x5';
SET debug_sync= 'ibb_after_rollback_redo_log SIGNAL con5_rdy WAIT_FOR crash';
XA ROLLBACK 'x5';
connection default;
SET debug_sync= 'now WAIT_FOR con5_rdy';
FLUSH BINARY LOGS;
connection con2;
INSERT INTO t1 VALUES (2, 2, '');
INSERT INTO t1 VALUES (2, 3, REPEAT('<x2>', 10001));
XA END 'x2';
SET debug_sync= 'at_unlog_xa_prepare SIGNAL con2_rdy WAIT_FOR crash';
XA PREPARE 'x2';
connection default;
SET debug_sync= 'now WAIT_FOR con2_rdy';
connection con4;
INSERT INTO t1 VALUES (4, 2, '');
XA END 'x4';
XA PREPARE 'x4';
SET debug_sync= 'commit_handlerton_after SIGNAL con4_rdy WAIT_FOR crash';
XA COMMIT 'x4';
connection default;
SET debug_sync= 'now WAIT_FOR con4_rdy';
# restart
disconnect con1;
disconnect con2;
disconnect con3;
disconnect con4;
disconnect con5;
disconnect con6;
connection default;
XA RECOVER;
formatID gtrid_length bqual_length data
1 2 0 x2
XA COMMIT 'x2';
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog-000000.ibb # Gtid # # GTID #-#-#
binlog-000000.ibb # Query # # use `test`; CREATE TABLE t1(a INT NOT NULL, b INT NOT NULL, c LONGBLOB, PRIMARY KEY(a, b)) ENGINE=InnoDB
binlog-000000.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000000.ibb # Annotate_rows # # INSERT INTO t1 VALUES (0, 1, '')
binlog-000000.ibb # Table_map # # table_id: # (test.t1)
binlog-000000.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000000.ibb # Xid # # COMMIT /* XID */
binlog-000000.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000000.ibb # Annotate_rows # # INSERT INTO t1 VALUES (0, 2, '')
binlog-000000.ibb # Table_map # # table_id: # (test.t1)
binlog-000000.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000000.ibb # Xid # # COMMIT /* XID */
binlog-000000.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000000.ibb # Annotate_rows # # INSERT INTO t1 VALUES (0, 3, '')
binlog-000000.ibb # Table_map # # table_id: # (test.t1)
binlog-000000.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000000.ibb # Xid # # COMMIT /* XID */
include/show_binlog_events.inc
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog-000002.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000002.ibb # Annotate_rows # # INSERT INTO t1 VALUES (3, 1, '')
binlog-000002.ibb # Table_map # # table_id: # (test.t1)
binlog-000002.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000002.ibb # Annotate_rows # # INSERT INTO t1 VALUES (3, 2, '')
binlog-000002.ibb # Table_map # # table_id: # (test.t1)
binlog-000002.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000002.ibb # Query # # COMMIT
FLUSH BINARY LOGS;
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog-000003.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000003.ibb # Annotate_rows # # INSERT INTO t1 VALUES (4, 1, REPEAT('<x4>', 10000))
binlog-000003.ibb # Table_map # # table_id: # (test.t1)
binlog-000003.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000003.ibb # Annotate_rows # # INSERT INTO t1 VALUES (4, 2, '')
binlog-000003.ibb # Table_map # # table_id: # (test.t1)
binlog-000003.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000003.ibb # Query # # COMMIT
binlog-000003.ibb # Format_desc # # SERVER_VERSION, BINLOG_VERSION
binlog-000003.ibb # Gtid # # BEGIN GTID #-#-#
binlog-000003.ibb # Annotate_rows # # INSERT INTO t1 VALUES (2, 1, REPEAT('<x2>', 10000))
binlog-000003.ibb # Table_map # # table_id: # (test.t1)
binlog-000003.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000003.ibb # Annotate_rows # # INSERT INTO t1 VALUES (2, 2, '')
binlog-000003.ibb # Table_map # # table_id: # (test.t1)
binlog-000003.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000003.ibb # Annotate_rows # # INSERT INTO t1 VALUES (2, 3, REPEAT('<x2>', 10001))
binlog-000003.ibb # Table_map # # table_id: # (test.t1)
binlog-000003.ibb # Write_rows_v1 # # table_id: # flags: STMT_END_F
binlog-000003.ibb # Query # # COMMIT
SELECT a, b, LENGTH(c) FROM t1 ORDER BY a, b;
a b LENGTH(c)
0 1 0
0 2 0
0 3 0
2 1 40000
2 2 0
2 3 40004
3 1 0
3 2 0
4 1 40000
4 2 0
*** Test that we can purge, no leaked XA/OOB refs.
SET @old_min_slaves= @@GLOBAL.slave_connections_needed_for_purge;
SET GLOBAL slave_connections_needed_for_purge= 0;
FLUSH BINARY LOGS;
PURGE BINARY LOGS TO 'binlog-000004.ibb';
SET GLOBAL slave_connections_needed_for_purge= @old_min_slaves;
DROP TABLE t1;