Skip to content

Commit 44e1c27

Browse files
santoshbandainikep
authored andcommitted
FB8-48: Option to run triggers on slave for row-based events (facebook#946) (facebook#946)
Summary: JIRA: https://jira.percona.com/browse/FB8-48 Reference Patch: facebook@8225c64 Reference Patch: facebook@f5466d6 Reference Patch: facebook@87e3650 Port the slave_run_triggers_for_rbr feature from mariadb 10.1.1. When using statement based replication slave executes the sql statments which runs the slave side triggers. Since in row based replication, slave applies the row events directly to the storage engine, triggers on the slave table are not executed. Add functionality to run triggers on slave side when executing row based events. The following triggers are invoked: * Update_row_event runs an UPDATE trigger * Delete_row_event runs a DELETE trigger * Write_row_event action depends on whether the operation will require foreign key checks: 1) when FK checks are not necessary, the operation will invoke a DELETE trigger if the record to be modified existed in the table. After that, an INSERT trigger will be invoked. 2) when FK checks are necessary, either an UPDATE or or a combination of DELETE and INSERT triggers will be invoked. slave_run_triggers_for_rbr option controls the feature. Default value is NO which don't invoke trigger for row-based events; Setting the option to YES will cause the SQL slave thread to invoke triggers for row based events; setting it to LOGGING will also cause the changes made by the triggers to be written into the binary log. There is a basic protection against triggers being invoked both on the master and slave. If the master modifies a table that has triggers, it will produce row-based binlog events with the "triggers were invoked for this event" flag. The slave will not invoke any triggers for flagged events. optionally disable binlogging while executing triggers Online schema change (OSC) creates triggers only on master side. Since RBR logs row changes made by triggers, sql_thread will hit errors due to missing tables on slaves. Add a session variable SQL_LOG_BIN_TRIGGERS to optionally disable binlogging for trigger statements so that RBR and OSC are fine with each other. disable_sql_log_bin_triggers flag is added in the TABLE_LIST struct to track tables that are opened during trigger execution. This flag is used to skip writing Table_map_log_events for such tables. With sql_log_bin_triggers enabled, the trigger changes which may be necessary for slave are not propagated. To avoid this, slave_run_triggers_for_rbr option must be enabled on slave. Pull Request resolved: facebook#946 Reviewed By: lloyd Differential Revision: D13972562 Pulled By: lth
1 parent 4f2a98e commit 44e1c27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1232
-111
lines changed

Diff for: mysql-test/include/have_rbr_triggers.inc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
if (`select count(*) = 0 from performance_schema.session_variables where variable_name = 'slave_run_triggers_for_rbr'`)
2+
{
3+
skip RBR triggers are not available;
4+
}

Diff for: mysql-test/r/mysqld--help-notwin.result

+15
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,15 @@ The following options may be given as the first argument:
20552055
This option is deprecated. Use
20562056
replica_preserve_commit_order instead.
20572057
(Defaults to on; use --skip-slave-preserve-commit-order to disable.)
2058+
--slave-run-triggers-for-rbr=name
2059+
Modes for how triggers in row-base replication on slave
2060+
side will be executed. Legal values are NO (default), YES
2061+
and LOGGING. NO means that trigger for RBR will not be
2062+
running on slave. YES and LOGGING means that triggers
2063+
will be running on slave, if there was not triggers
2064+
running on the master for the statement. LOGGING also
2065+
means results of that the executed triggers work will be
2066+
written to the binlog.
20582067
--slave-skip-errors=name
20592068
This option is deprecated. Use replica_skip_errors
20602069
instead.
@@ -2095,6 +2104,10 @@ The following options may be given as the first argument:
20952104
When set, if a table is created without a primary key
20962105
then server generates invisible auto-increment column as
20972106
a primary key for the table.
2107+
--sql-log-bin-triggers
2108+
The row changes generated by execution of triggers are
2109+
not logged inbinlog if this option is false.
2110+
(Defaults to on; use --skip-sql-log-bin-triggers to disable.)
20982111
--sql-mode=name Syntax: sql-mode=mode[,mode[,mode...]]. See the manual
20992112
for the complete list of valid sql modes
21002113
--sql-require-primary-key
@@ -2835,6 +2848,7 @@ slave-parallel-type LOGICAL_CLOCK
28352848
slave-parallel-workers 4
28362849
slave-pending-jobs-size-max 134217728
28372850
slave-preserve-commit-order TRUE
2851+
slave-run-triggers-for-rbr NO
28382852
slave-skip-errors (No default value)
28392853
slave-sql-verify-checksum TRUE
28402854
slave-transaction-retries 10
@@ -2845,6 +2859,7 @@ sort-buffer-size 262144
28452859
source-verify-checksum FALSE
28462860
sporadic-binlog-dump-fail FALSE
28472861
sql-generate-invisible-primary-key FALSE
2862+
sql-log-bin-triggers TRUE
28482863
sql-mode ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
28492864
sql-require-primary-key FALSE
28502865
stored-program-cache 256

Diff for: mysql-test/suite/binlog_nogtid/r/binlog_persist_only_variables.result

+4
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ SET PERSIST_ONLY slave_preserve_commit_order = @@GLOBAL.slave_preserve_commit_or
239239
Warnings:
240240
Warning 1287 '@@slave_preserve_commit_order' is deprecated and will be removed in a future release. Please use replica_preserve_commit_order instead.
241241
Warning 1287 '@@slave_preserve_commit_order' is deprecated and will be removed in a future release. Please use replica_preserve_commit_order instead.
242+
SET PERSIST_ONLY slave_run_triggers_for_rbr = @@GLOBAL.slave_run_triggers_for_rbr;
242243
SET PERSIST_ONLY slave_skip_errors = @@GLOBAL.slave_skip_errors;
243244
Warnings:
244245
Warning 1287 '@@slave_skip_errors' is deprecated and will be removed in a future release. Please use replica_skip_errors instead.
@@ -256,6 +257,7 @@ Warnings:
256257
Warning 1287 '@@slave_type_conversions' is deprecated and will be removed in a future release. Please use replica_type_conversions instead.
257258
Warning 1287 '@@slave_type_conversions' is deprecated and will be removed in a future release. Please use replica_type_conversions instead.
258259
SET PERSIST_ONLY source_verify_checksum = @@GLOBAL.source_verify_checksum;
260+
SET PERSIST_ONLY sql_log_bin_triggers = @@GLOBAL.sql_log_bin_triggers;
259261
SET PERSIST_ONLY sql_replica_skip_counter = @@GLOBAL.sql_replica_skip_counter;
260262
SET PERSIST_ONLY sql_slave_skip_counter = @@GLOBAL.sql_slave_skip_counter;
261263
Warnings:
@@ -380,7 +382,9 @@ RESET PERSIST rpl_stop_replica_timeout;
380382
RESET PERSIST session_track_gtids;
381383
RESET PERSIST skip_replica_start;
382384
RESET PERSIST slave_compression_lib;
385+
RESET PERSIST slave_run_triggers_for_rbr;
383386
RESET PERSIST source_verify_checksum;
387+
RESET PERSIST sql_log_bin_triggers;
384388
RESET PERSIST sql_replica_skip_counter;
385389
RESET PERSIST sync_binlog;
386390
RESET PERSIST sync_relay_log;

Diff for: mysql-test/suite/binlog_nogtid/r/binlog_persist_variables.result

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ SET PERSIST slave_preserve_commit_order = @@GLOBAL.slave_preserve_commit_order;
218218
Warnings:
219219
Warning 1287 '@@slave_preserve_commit_order' is deprecated and will be removed in a future release. Please use replica_preserve_commit_order instead.
220220
Warning 1287 '@@slave_preserve_commit_order' is deprecated and will be removed in a future release. Please use replica_preserve_commit_order instead.
221+
SET PERSIST slave_run_triggers_for_rbr = @@GLOBAL.slave_run_triggers_for_rbr;
221222
SET PERSIST slave_skip_errors = @@GLOBAL.slave_skip_errors;
222223
ERROR HY000: Variable 'slave_skip_errors' is a read only variable
223224
SET PERSIST slave_sql_verify_checksum = @@GLOBAL.slave_sql_verify_checksum;
@@ -233,6 +234,7 @@ Warnings:
233234
Warning 1287 '@@slave_type_conversions' is deprecated and will be removed in a future release. Please use replica_type_conversions instead.
234235
Warning 1287 '@@slave_type_conversions' is deprecated and will be removed in a future release. Please use replica_type_conversions instead.
235236
SET PERSIST source_verify_checksum = @@GLOBAL.source_verify_checksum;
237+
SET PERSIST sql_log_bin_triggers = @@GLOBAL.sql_log_bin_triggers;
236238
SET PERSIST sql_replica_skip_counter = @@GLOBAL.sql_replica_skip_counter;
237239
SET PERSIST sql_slave_skip_counter = @@GLOBAL.sql_slave_skip_counter;
238240
Warnings:
@@ -435,6 +437,7 @@ Warning 3615 Variable slave_pending_jobs_size_max does not exist in persisted co
435437
RESET PERSIST IF EXISTS slave_preserve_commit_order;
436438
Warnings:
437439
Warning 3615 Variable slave_preserve_commit_order does not exist in persisted config file
440+
RESET PERSIST IF EXISTS slave_run_triggers_for_rbr;
438441
RESET PERSIST IF EXISTS slave_skip_errors;
439442
Warnings:
440443
Warning 3615 Variable slave_skip_errors does not exist in persisted config file
@@ -450,6 +453,7 @@ Warning 3615 Variable slave_type_conversions does not exist in persisted config
450453
RESET PERSIST IF EXISTS source_verify_checksum;
451454
Warnings:
452455
Warning 3615 Variable source_verify_checksum does not exist in persisted config file
456+
RESET PERSIST IF EXISTS sql_log_bin_triggers;
453457
RESET PERSIST IF EXISTS sql_replica_skip_counter;
454458
RESET PERSIST IF EXISTS sql_slave_skip_counter;
455459
Warnings:

Diff for: mysql-test/suite/binlog_nogtid/t/binlog_persist_only_variables.test

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ while ( $varid <= $countvars )
108108
--eval SET PERSIST_ONLY $varname = @@GLOBAL.$varname
109109

110110
# TODO: Remove/update this once Bug#27322592 is FIXED.
111-
if (`SELECT '$varname' IN ('binlog_trx_meta_data', 'binlog_direct_non_transactional_updates', 'binlog_order_commits', 'binlog_rows_query_log_events', 'log_bin_trust_function_creators', 'log_slow_replica_statements', 'log_statements_unsafe_for_binlog', 'source_verify_checksum', 'replica_allow_batching', 'replica_compressed_protocol', 'replica_preserve_commit_order', 'replica_sql_verify_checksum', 'relay_log_purge', 'rpl_semi_sync_source_enabled', 'rpl_semi_sync_source_wait_no_replica', 'rpl_semi_sync_replica_enabled', 'binlog_gtid_simple_recovery', 'log_replica_updates', 'relay_log_recovery', 'binlog_rotate_encryption_master_key_at_startup', 'binlog_transaction_compression')`)
111+
if (`SELECT '$varname' IN ('sql_log_bin_triggers', 'binlog_trx_meta_data', 'binlog_direct_non_transactional_updates', 'binlog_order_commits', 'binlog_rows_query_log_events', 'log_bin_trust_function_creators', 'log_slow_replica_statements', 'log_statements_unsafe_for_binlog', 'source_verify_checksum', 'replica_allow_batching', 'replica_compressed_protocol', 'replica_preserve_commit_order', 'replica_sql_verify_checksum', 'relay_log_purge', 'rpl_semi_sync_source_enabled', 'rpl_semi_sync_source_wait_no_replica', 'rpl_semi_sync_replica_enabled', 'binlog_gtid_simple_recovery', 'log_replica_updates', 'relay_log_recovery', 'binlog_rotate_encryption_master_key_at_startup', 'binlog_transaction_compression')`)
112112
{
113113
--disable_query_log
114114
--eval SELECT varvalue INTO @varvalue FROM rplvars WHERE id=$varid

Diff for: mysql-test/suite/rocksdb/r/rpl_row_triggers.result

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
include/master-slave.inc
22
Warnings:
33
Note #### Sending passwords in plain text without SSL/TLS is extremely insecure.
4-
Note #### Storing MySQL user name or password information in the master info repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START SLAVE; see the 'START SLAVE Syntax' in the MySQL Manual for more information.
4+
Note #### Storing MySQL user name or password information in the connection metadata repository is not secure and is therefore not recommended. Please consider using the USER and PASSWORD connection options for START REPLICA; see the 'START REPLICA Syntax' in the MySQL Manual for more information.
55
[connection master]
66
# Test of row replication with triggers on the slave side
77
CREATE TABLE t1 (C1 CHAR(1) primary key, C2 CHAR(1));

0 commit comments

Comments
 (0)