Bug #120471 binary log violates read after write (commit) consistency

Submitted: 14 May 0:08 Modified: 14 Oct 9:16
Reporter: Morgan Tocker Email Updates:
Status: Not a Bug Impact on me:
None 
Category:MySQL Server: Replication Severity:S1 (Critical)
Version:8.0.43, 8.0.46, 8.4.9, 9.7.0 OS:Any (reproduced on macOS and Linux)
Assigned to: CPU Architecture:Any

[14 May 0:08] Morgan Tocker
Description:
Under `binlog_order_commits=ON` (default) an entry in the binary log should mean that the rows are visible to new sessions if they were to be read back. That is because the binary log contains _committed_ changes.

However, there is a race which I can consistently reproduce with the minimal testcase.

This is critical to tools such as those for online schema change. We detected this in Spirit and have had to workaround it.

I have reproduced on MySQL 8.0.43, 8.0.46, 8.4.9, 9.7.0. Originally I thought it was Linux specific, but I can reproduce on macOS with ARM hardware as well.

How to repeat:
Concurrently execute in parallel:

BEGIN;
INSERT INTO t (payload) VALUES ('x');
UPDATE t SET payload = 'x2' WHERE id = LAST_INSERT_ID();
COMMIT;

.. then try to read the data back.

I will upload a shell script which can be used to reproduce.
[14 May 0:09] Morgan Tocker
repro.sh

Attachment: repro.sh (text/x-sh), 9.27 KiB.

[14 May 0:09] Morgan Tocker
Results from running the attached script:

mtocker@BLKD5234QXY40 read-after-write-violation % ./repro.sh -t 240 -w 8 -u msandbox -p msandbox
Server settings:
  version=8.0.43
  binlog_format=ROW
  binlog_row_image=FULL
  binlog_order_commits=1
  sync_binlog=1
  innodb_flush_log_at_trx_commit=1
starting binlog tail from BLKD5234QXY40-bin.000027
writers=8 max_duration=240s recheck_delay=100ms

first miss observed: id=931 (row event delivered, fresh SELECT does not see the row) -- re-checking after 100ms
MISSING (delayed)   id=931 -- row became visible after 100ms

SUMMARY: writers=8 events_observed=931
FAIL: binlog_order_commits=ON contract was violated.
Row event was observed in the binlog stream but the corresponding row
only became visible to a fresh SELECT after 100ms (id=931).
The visibility lag is transient but it is still a contract violation.

mtocker@BLKD5234QXY40 read-after-write-violation % ./repro.sh -t 240 -w 8 -u msandbox -p msandbox -P 8046
Server settings:
  version=8.0.46
  binlog_format=ROW
  binlog_row_image=FULL
  binlog_order_commits=1
  sync_binlog=1
  innodb_flush_log_at_trx_commit=1
starting binlog tail from binlog.000001
writers=8 max_duration=240s recheck_delay=100ms

first miss observed: id=1736 (row event delivered, fresh SELECT does not see the row) -- re-checking after 100ms
MISSING (delayed)   id=1736 -- row became visible after 100ms

SUMMARY: writers=8 events_observed=1736
FAIL: binlog_order_commits=ON contract was violated.
Row event was observed in the binlog stream but the corresponding row
only became visible to a fresh SELECT after 100ms (id=1736).
The visibility lag is transient but it is still a contract violation.

mtocker@BLKD5234QXY40 read-after-write-violation % ./repro.sh -t 240 -w 8 -u msandbox -p msandbox -P 8409
Server settings:
  version=8.4.9
  binlog_format=ROW
  binlog_row_image=FULL
  binlog_order_commits=1
  sync_binlog=1
  innodb_flush_log_at_trx_commit=1
starting binlog tail from binlog.000001
writers=8 max_duration=240s recheck_delay=100ms

first miss observed: id=929 (row event delivered, fresh SELECT does not see the row) -- re-checking after 100ms
MISSING (delayed)   id=929 -- row became visible after 100ms

SUMMARY: writers=8 events_observed=929
FAIL: binlog_order_commits=ON contract was violated.
Row event was observed in the binlog stream but the corresponding row
only became visible to a fresh SELECT after 100ms (id=929).
The visibility lag is transient but it is still a contract violation.

mtocker@BLKD5234QXY40 read-after-write-violation % ./repro.sh -t 240 -w 8 -u msandbox -p msandbox -P 9700
Server settings:
  version=9.7.0
  binlog_format=ROW
  binlog_row_image=FULL
  binlog_order_commits=1
  sync_binlog=1
  innodb_flush_log_at_trx_commit=1
starting binlog tail from binlog.000002
writers=8 max_duration=240s recheck_delay=100ms

first miss observed: id=920 (row event delivered, fresh SELECT does not see the row) -- re-checking after 100ms
MISSING (delayed)   id=920 -- row became visible after 100ms

SUMMARY: writers=8 events_observed=920
FAIL: binlog_order_commits=ON contract was violated.
Row event was observed in the binlog stream but the corresponding row
only became visible to a fresh SELECT after 100ms (id=920).
The visibility lag is transient but it is still a contract violation.
[14 Oct 8:39] Karolina Szczepankiewicz
Posted by developer:
 
The reported behavior is consistent with MySQL's existing commit protocol and does not demonstrate a violation of read-after-write consistency.

For transactional InnoDB changes, the server first prepares the transaction. It then writes and synchronizes the binary log according to its durability settings, before committing the transaction in InnoDB. Binlog consumers can receive the transaction's events before the InnoDB commit makes its changes visible to new reads on the source.

Setting `binlog_order_commits=ON` guarantees that storage-engine commits occur in the same order as transactions *appear* in the binary log. It does *not* guarantee that receiving a transaction's binlog events means its changes are already *visible* on the source. The documentation describes the binary-log write as happening before the storage-engine commit [MySQL documentation](https://dev.mysql.com/doc/refman/8.0/en/replication-options-binary-log.html).

Receiving an XID/COMMIT event does not mean that InnoDB has finished committing the transaction on the source either. With semisynchronous replication using AFTER_SYNC, the source sends the transaction to a replica and waits for its acknowledgement before committing in the source's storage engine [Semisynchronous replication documentation](https://dev.mysql.com/doc/refman/8.0/en/replication-semisync-interface.html).

A successful COMMIT response (OK packet) to the writing client means the engine commit has completed. A read on the same source, using a snapshot created after the commit, can see the committed changes. A read using a snapshot created before the commit may still not see those changes.

The provided `repro.sh` checks whether a row is visible after receiving its INSERT row event. The writers run independently in the background (lines 154-166). The parser forwards the inserted ID as soon as it decodes the row event (lines 199-206), and the verifier opens a new connection and runs a SELECT against the same host and port (line 231). It does not wait for the writer's COMMIT OK.

The new connection avoids an old transaction snapshot, but the SELECT can still run before InnoDB finishes committing. The script therefore does not demonstrate a missing row after a successful COMMIT response. Its introductory comment (lines 13-16) assumes that row-event delivery guarantees completed InnoDB commit. `binlog_order_commits` does *not* provide that guarantee.

A consumer can use GTIDs to wait for a transaction to finish committing. In the normal InnoDB commit path, the server adds the transaction's GTID to @@GLOBAL.gtid_executed *after* the storage-engine commit. The consumer can call WAIT_FOR_EXECUTED_GTID_SET() for that GTID on the source. After it returns 0 (success), a read using a new snapshot on that same server can see the committed changes. Enabling GTIDs or receiving a GTID event alone is not enough; the consumer must wait for the GTID to become executed [GTID function documentation](https://dev.mysql.com/doc/refman/8.0/en/gtid-functions.html).

The reported observation alone does not demonstrate a violation of `binlog_order_commits` or read-after-write consistency. A tool that needs to read a transaction's committed changes on the source must wait for commit completion, for example with the GTID wait described above. Receiving the binlog events alone is not enough.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.041 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.