Skip to content

Commit

Permalink
Return hlc timestamp for a write txn (facebook#1104) (facebook#1104)
Browse files Browse the repository at this point in the history
Summary:
The HLC timestamp associated with a GTID of a txn is returned as part of OK
packets. The HLC timestamp is stored in session response attributes tracker
field of the OK packet. The HLC is stored as a key-value pair in session
response attribute - the key is 'hlc_ts' and the value is the HLC timestamp.
Setting the HLC timestamp in OK packets is gated by a sysvar
'session_track_hlc'.

Only a RW transaction (i.e a transaction that genarates a GTID) will have a HLC
timestamp retuned in its OK packet.

Reference Patch: facebook@7bfac75

Pull Request resolved: facebook#1104

Reviewed By: luqun

Differential Revision: D19859262

Pulled By: bhatvinay
  • Loading branch information
bhatvinay authored and inikep committed Jan 28, 2022
1 parent ca1e38c commit 8535328
Show file tree
Hide file tree
Showing 10 changed files with 548 additions and 1 deletion.
14 changes: 14 additions & 0 deletions mysql-test/include/have_gtid.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ==== Purpose ====
#
# Ensure that the server is running with GTID support on.
#
# ==== Usage ====
#
# --source include/have_gtid.inc

--source include/have_log_bin.inc

if ( `SELECT @@GLOBAL.GTID_MODE = "OFF"` )
{
--skip Test requires GTID_MODE=ON.
}
8 changes: 8 additions & 0 deletions mysql-test/r/mysqld--help-notwin.result
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,13 @@ The following options may be given as the first argument:
between 0 and the real lag when the IO thread is the
bottleneck.
(Defaults to on; use --skip-reset-seconds-behind-master to disable.)
--response-attrs-contain-hlc
If this is enabled, then the HLC timestamp of a RW
transaction is sent back to clients as part of OK packet
in session response attribute. HLC is sent as a key-value
pair - 'hlc_ts' is the key and the value is the
stringified HLC timestamp. Note that HLC should be
enabled by setting enable_binlog_hlc
--rpl-read-size=# The size for reads done from the binlog and relay log. It
must be a multiple of 4kb. Making it larger might help
with IO stalls while reading these files when they are
Expand Down Expand Up @@ -1991,6 +1998,7 @@ report-port 0
report-user (No default value)
require-secure-transport FALSE
reset-seconds-behind-master TRUE
response-attrs-contain-hlc FALSE
rpl-read-size 8192
rpl-receive-buffer-size 2097152
rpl-send-buffer-size 2097152
Expand Down
161 changes: 161 additions & 0 deletions mysql-test/suite/binlog_gtid/r/session_attrs_contain_hlc.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
flush logs;
purge binary logs to 'binlog';
SET SESSION DEBUG="+d,allow_long_hlc_drift_for_tests";
SET @@global.minimum_hlc_ns = 2538630000000000000;
SET @@session.session_track_response_attributes = on;
SET @@global.enable_binlog_hlc = true;
SET @@session.response_attrs_contain_hlc = true;
Case 1: Single statement txn. Commit ts needs to be updated at commit time
USE test;
-- Tracker : SESSION_TRACK_SCHEMA
-- test

-- Tracker : SESSION_TRACK_RESP_ATTR
-- change_db
-- test

CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8));
-- Tracker : SESSION_TRACK_RESP_ATTR
-- hlc_ts
-- 2538630000000000001

INSERT INTO t1 VALUES(1, 'a');
-- Tracker : SESSION_TRACK_RESP_ATTR
-- hlc_ts
-- 2538630000000000002

INSERT INTO t1 VALUES(2, 'b');
-- Tracker : SESSION_TRACK_RESP_ATTR
-- hlc_ts
-- 2538630000000000003

include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog.000002 # Metadata # # HLC time: 2538630000000000001
binlog.000002 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8))
binlog.000002 # Metadata # # HLC time: 2538630000000000002
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000003
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
Case 2: Multi statement txn. Commit ts needs to be updated at commit time
BEGIN;
-- Tracker : SESSION_TRACK_TRANSACTION_STATE
-- T_______

INSERT INTO t1 VALUES(3, 'c');
INSERT INTO t1 VALUES(4, 'd');
COMMIT;
-- Tracker : SESSION_TRACK_TRANSACTION_STATE
-- ________

-- Tracker : SESSION_TRACK_RESP_ATTR
-- hlc_ts
-- 2538630000000000004

SELECT * FROM t1;
a b
1 a
2 b
3 c
4 d
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog.000002 # Metadata # # HLC time: 2538630000000000001
binlog.000002 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8))
binlog.000002 # Metadata # # HLC time: 2538630000000000002
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000003
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000004
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
Case 3: Turning off binlog_hlc should stop sending hlc in ok packets
SET @@global.enable_binlog_hlc = false;
SET @@session.response_attrs_contain_hlc = true;
INSERT INTO t1 VALUES(5, 'e');
COMMIT;
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog.000002 # Metadata # # HLC time: 2538630000000000001
binlog.000002 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8))
binlog.000002 # Metadata # # HLC time: 2538630000000000002
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000003
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000004
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
Case 4: Turning off response_attrs_contain_hlc should stop sending hlc in ok packets
SET @@global.enable_binlog_hlc = true;
SET @@session.response_attrs_contain_hlc = false;
INSERT INTO t1 VALUES(6, 'f');
COMMIT;
include/show_binlog_events.inc
Log_name Pos Event_type Server_id End_log_pos Info
binlog.000002 # Metadata # # HLC time: 2538630000000000001
binlog.000002 # Query # # use `test`; CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8))
binlog.000002 # Metadata # # HLC time: 2538630000000000002
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000003
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000004
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
binlog.000002 # Metadata # # HLC time: 2538630000000000005
binlog.000002 # Query # # BEGIN
binlog.000002 # Table_map # # table_id: # (test.t1)
binlog.000002 # Write_rows # # table_id: # flags: STMT_END_F
binlog.000002 # Xid # # COMMIT /* XID */
USE test;
-- Tracker : SESSION_TRACK_SCHEMA
-- test

-- Tracker : SESSION_TRACK_RESP_ATTR
-- change_db
-- test

DROP TABLE t1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--force-restart
81 changes: 81 additions & 0 deletions mysql-test/suite/binlog_gtid/t/session_attrs_contain_hlc.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
--source include/have_binlog_format_row.inc
--source include/have_debug.inc
--source include/have_gtid.inc

--enable_session_track_info

--let $saved_minimum_hlc_ns= `SELECT @@global.minimum_hlc_ns`
--let $saved_enable_binlog_hlc= `SELECT @@global.enable_binlog_hlc`
--let $saved_response_attrs_contain_hlc= `SELECT @@session.response_attrs_contain_hlc`

# Cleanup old binlog
flush logs;
let $binlog= query_get_value(SHOW MASTER STATUS, File, 1);
replace_result $binlog binlog;
eval purge binary logs to '$binlog';

# Setup
# Set minimum_hlc_ns to a high value. Subsequent txn's should see monotonically
# increasing timestamp from this point
SET SESSION DEBUG="+d,allow_long_hlc_drift_for_tests";
SET @@global.minimum_hlc_ns = 2538630000000000000; # ~2050 AD

# Enable tracking response attribute
SET @@session.session_track_response_attributes = on;

# Enable binlog_hlc
SET @@global.enable_binlog_hlc = true;

# Enable session to track HLC
SET @@session.response_attrs_contain_hlc = true;

# Case 1: Enable binlog HLC and run a single statement txn
--echo Case 1: Single statement txn. Commit ts needs to be updated at commit time
USE test;

CREATE TABLE t1 (a INT PRIMARY KEY, b CHAR(8));
INSERT INTO t1 VALUES(1, 'a');
INSERT INTO t1 VALUES(2, 'b');
--source include/show_binlog_events.inc

# Case 2: Enable binlog HLC and run multi statement txn
--echo Case 2: Multi statement txn. Commit ts needs to be updated at commit time
BEGIN;
INSERT INTO t1 VALUES(3, 'c');
INSERT INTO t1 VALUES(4, 'd');
COMMIT;

SELECT * FROM t1;

--source include/show_binlog_events.inc

# Case 3: Turning off binlog_hlc should stop sending hlc in ok packets
--echo Case 3: Turning off binlog_hlc should stop sending hlc in ok packets
# Disable binlog_hlc
SET @@global.enable_binlog_hlc = false;

# Enable session to track HLC
SET @@session.response_attrs_contain_hlc = true;
INSERT INTO t1 VALUES(5, 'e');
COMMIT;

--source include/show_binlog_events.inc

# Case 4: Turning off response_attrs_contain_hlc should stop sending hlc in ok packets even though binlog_hlc is enabled
--echo Case 4: Turning off response_attrs_contain_hlc should stop sending hlc in ok packets
SET @@global.enable_binlog_hlc = true;
SET @@session.response_attrs_contain_hlc = false;
INSERT INTO t1 VALUES(6, 'f');
COMMIT;

--source include/show_binlog_events.inc

# Cleanup
USE test;
DROP TABLE t1;
--disable_query_log
--eval SET GLOBAL minimum_hlc_ns = $saved_minimum_hlc_ns
--eval SET GLOBAL enable_binlog_hlc = $saved_enable_binlog_hlc
--eval SET SESSION response_attrs_contain_hlc = $saved_response_attrs_contain_hlc
--eval SET SESSION DEBUG="-d,allow_long_hlc_drift_for_tests"
--enable_query_log
Loading

0 comments on commit 8535328

Please sign in to comment.