1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-07 00:04:31 +03:00

MDEV-31273: Precompute binlog checksums

Compute binlog checksums (when enabled) already when writing events
into the statement or transaction caches, where before it was done
when the caches are copied to the real binlog file. This moves the
checksum computation outside of holding LOCK_log, improving
scalabitily.

At stmt/trx cache write time, the final end_log_pos values are not
known, so with this patch these will be set to 0. Events that are
written directly to the binlog file (not through stmt/trx cache) keep
the correct end_log_pos value. The GTID and COMMIT/XID events at the
start and end of event groups are written directly, so the zero
end_log_pos is only for events in the middle of event groups, which
do not negatively affect replication.

An option --binlog-legacy-event-pos, off by default, is provided to
disable this behavior to provide backwards compatibility with any
external applications that might rely on end_log_pos in events in the
middle of event groups.

Checksums cannot be pre-computed when binlog encryption is enabled, as
encryption relies on correct end_log_pos to provide part of the
nonce/IV.

Checksum pre-computation is also disabled for WSREP/Galera, as it uses
events differently in its write-sets and so on. Extending pre-computation of
checksums to Galera where it makes sense could be added in a future patch.

The current --binlog-checksum configuration is saved in
binlog_cache_data at transaction start and used to pre-compute
checksums in cache, if applicable. When the cache is later copied to
the binlog, a check is made if the saved value still matches the
configured global value; if so, the events are block-copied directly
into the binlog file. If --binlog-checksum was changed during the
transaction, events are re-written to the binlog file one-by-one and
the checksums recomputed/discarded as appropriate.

Reviewed-by: Monty <monty@mariadb.org>
Signed-off-by: Kristian Nielsen <knielsen@knielsen-hq.org>
This commit is contained in:
Kristian Nielsen
2023-06-13 11:41:44 +02:00
parent 24c923d498
commit b8f9f796ff
24 changed files with 440 additions and 126 deletions

View File

@@ -121,13 +121,54 @@ connection slave;
include/diff_tables.inc [master:test.t1, slave:test.t1]
include/diff_tables.inc [master:test.t2, slave:test.t2]
include/diff_tables.inc [master:test.t3, slave:test.t3]
*** Test switching checksum algorithm while ongoing transactions have pre-computed checksum in their binlog cache ***
connection master;
CREATE TABLE t4 (a INT, b INT, c VARCHAR(1024), PRIMARY KEY (a,b)) ENGINE=InnoDB;
BEGIN;
INSERT INTO t4 VALUES (1, 1, "small, pre-computed checksums");
connection server_1;
BEGIN;
INSERT INTO t4 VALUES (2, 1, "big, pre-computed checksums");
set @@global.binlog_checksum = NONE;
connection master;
INSERT INTO t4 VALUES (1, 2, "done");
COMMIT;
connection server_1;
INSERT INTO t4 VALUES (2, 22, "done");
COMMIT;
connection master;
BEGIN;
INSERT INTO t4 VALUES (3, 1, "small, no pre-computed checksums");
connection server_1;
BEGIN;
INSERT INTO t4 VALUES (4, 1, "big, no pre-computed checksums");
set @@global.binlog_checksum = CRC32;
connection master;
INSERT INTO t4 VALUES (3, 2, "done");
COMMIT;
connection server_1;
INSERT INTO t4 VALUES (4, 22, "done");
COMMIT;
connection slave;
*** Test the --binlog-legacy-event-pos option.
connection master;
FLUSH BINARY LOGS;
BEGIN;
INSERT INTO t4 VALUES (5, 1, "Zero end_log_pos");
COMMIT;
set @@global.binlog_legacy_event_pos= 1;
BEGIN;
INSERT INTO t4 VALUES (6, 1, "Non-zero end_log_pos");
COMMIT;
set @@global.binlog_legacy_event_pos= 0;
connection slave;
connection master;
begin;
delete from t1;
delete from t2;
delete from t3;
commit;
drop table t1, t2, t3;
drop table t1, t2, t3, t4;
set @@global.binlog_cache_size = @save_binlog_cache_size;
set @@global.binlog_checksum = @save_binlog_checksum;
set @@global.master_verify_checksum = @save_master_verify_checksum;

View File

@@ -243,6 +243,102 @@ let $diff_tables=master:test.t3, slave:test.t3;
source include/diff_tables.inc;
--echo *** Test switching checksum algorithm while ongoing transactions have pre-computed checksum in their binlog cache ***
--connection master
CREATE TABLE t4 (a INT, b INT, c VARCHAR(1024), PRIMARY KEY (a,b)) ENGINE=InnoDB;
# Create a couple transactions that will precompute checksums but commit
# without them.
BEGIN;
INSERT INTO t4 VALUES (1, 1, "small, pre-computed checksums");
--connection server_1
BEGIN;
INSERT INTO t4 VALUES (2, 1, "big, pre-computed checksums");
--let $i= 20
--disable_query_log
while ($i) {
eval INSERT INTO t4 VALUES (2, 22-$i, REPEAT("x", FLOOR(RAND()*100) + 831));
dec $i;
}
--enable_query_log
# Disable checksums dynamically, so MYSQL_BIN_LOG::write_cache() will have
# to drop the pre-computed checksums.
set @@global.binlog_checksum = NONE;
--connection master
INSERT INTO t4 VALUES (1, 2, "done");
COMMIT;
--connection server_1
INSERT INTO t4 VALUES (2, 22, "done");
COMMIT;
# Create a couple transactions that will not precompute checksums but commit
# with them.
--connection master
BEGIN;
INSERT INTO t4 VALUES (3, 1, "small, no pre-computed checksums");
--connection server_1
BEGIN;
INSERT INTO t4 VALUES (4, 1, "big, no pre-computed checksums");
--let $i= 20
--disable_query_log
while ($i) {
eval INSERT INTO t4 VALUES (4, 22-$i, REPEAT("x", FLOOR(RAND()*100) + 853));
dec $i;
}
--enable_query_log
# Ebable checksums dynamically, so MYSQL_BIN_LOG::write_cache() will have
# to recompute the checksums.
set @@global.binlog_checksum = CRC32;
--connection master
INSERT INTO t4 VALUES (3, 2, "done");
COMMIT;
--connection server_1
INSERT INTO t4 VALUES (4, 22, "done");
COMMIT;
sync_slave_with_master;
--echo *** Test the --binlog-legacy-event-pos option.
--connection master
FLUSH BINARY LOGS;
--source include/wait_for_binlog_checkpoint.inc
--let $query_file= query_get_value(SHOW MASTER STATUS, File, 1)
--let $query_pos= query_get_value(SHOW MASTER STATUS, Position, 1)
BEGIN;
INSERT INTO t4 VALUES (5, 1, "Zero end_log_pos");
COMMIT;
--let $end_log_pos= query_get_value(SHOW BINLOG EVENTS IN "$query_file" FROM $query_pos LIMIT 3, End_log_pos, 2)
if ($end_log_pos!=0) {
eval SHOW BINLOG EVENTS IN "$query_file";
--die Wrong End_log_pos=$end_log_pos, expected zero.
}
set @@global.binlog_legacy_event_pos= 1;
--let $query_pos= query_get_value(SHOW MASTER STATUS, Position, 1)
BEGIN;
INSERT INTO t4 VALUES (6, 1, "Non-zero end_log_pos");
COMMIT;
--let $end_log_pos= query_get_value(SHOW BINLOG EVENTS IN "$query_file" FROM $query_pos LIMIT 3, End_log_pos, 2)
if ($end_log_pos==0) {
eval SHOW BINLOG EVENTS IN "$query_file";
--die Wrong End_log_pos=$end_log_pos, expected non-zero.
}
set @@global.binlog_legacy_event_pos= 0;
sync_slave_with_master;
connection master;
begin;
@@ -251,7 +347,7 @@ delete from t2;
delete from t3;
commit;
drop table t1, t2, t3;
drop table t1, t2, t3, t4;
set @@global.binlog_cache_size = @save_binlog_cache_size;
set @@global.binlog_checksum = @save_binlog_checksum;
set @@global.master_verify_checksum = @save_master_verify_checksum;