Skip to content

Commit 8225c64

Browse files
santoshbandajtolmer
authored andcommitted
Option to run triggers on slave for row-based events
Summary: 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. Test Plan: mtr tests Reviewers: jtolmer Reviewed By: jtolmer
1 parent 7696c4a commit 8225c64

23 files changed

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

mysql-test/r/mysqld--help-notwin-profiling.result

+10
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,15 @@ The following options may be given as the first argument:
10251025
the slave will always pick the most suitable algorithm
10261026
for any given scenario. (Default: INDEX_SCAN,
10271027
TABLE_SCAN).
1028+
--slave-run-triggers-for-rbr=name
1029+
Modes for how triggers in row-base replication on slave
1030+
side will be executed. Legal values are NO (default), YES
1031+
and LOGGING. NO means that trigger for RBR will not be
1032+
running on slave. YES and LOGGING means that triggers
1033+
will be running on slave, if there was not triggers
1034+
running on the master for the statement. LOGGING also
1035+
means results of that the executed triggers work will be
1036+
written to the binlog.
10281037
--slave-skip-errors=name
10291038
Tells the slave thread to continue replication when a
10301039
query event returns an error from the provided list
@@ -1462,6 +1471,7 @@ slave-net-timeout 3600
14621471
slave-parallel-workers 0
14631472
slave-pending-jobs-size-max 16777216
14641473
slave-rows-search-algorithms TABLE_SCAN,INDEX_SCAN
1474+
slave-run-triggers-for-rbr NO
14651475
slave-skip-errors (No default value)
14661476
slave-sql-verify-checksum TRUE
14671477
slave-transaction-retries 10

mysql-test/r/mysqld--help-notwin.result

+10
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,15 @@ The following options may be given as the first argument:
10231023
the slave will always pick the most suitable algorithm
10241024
for any given scenario. (Default: INDEX_SCAN,
10251025
TABLE_SCAN).
1026+
--slave-run-triggers-for-rbr=name
1027+
Modes for how triggers in row-base replication on slave
1028+
side will be executed. Legal values are NO (default), YES
1029+
and LOGGING. NO means that trigger for RBR will not be
1030+
running on slave. YES and LOGGING means that triggers
1031+
will be running on slave, if there was not triggers
1032+
running on the master for the statement. LOGGING also
1033+
means results of that the executed triggers work will be
1034+
written to the binlog.
10261035
--slave-skip-errors=name
10271036
Tells the slave thread to continue replication when a
10281037
query event returns an error from the provided list
@@ -1459,6 +1468,7 @@ slave-net-timeout 3600
14591468
slave-parallel-workers 0
14601469
slave-pending-jobs-size-max 16777216
14611470
slave-rows-search-algorithms TABLE_SCAN,INDEX_SCAN
1471+
slave-run-triggers-for-rbr NO
14621472
slave-skip-errors (No default value)
14631473
slave-sql-verify-checksum TRUE
14641474
slave-transaction-retries 10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
include/master-slave.inc
2+
Warnings:
3+
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.
5+
[connection master]
6+
# Test of row replication with triggers on the slave side
7+
CREATE TABLE t1 (C1 CHAR(1) primary key, C2 CHAR(1));
8+
SELECT * FROM t1;
9+
C1 C2
10+
SET @old_slave_exec_mode= @@global.slave_exec_mode;
11+
SET @old_slave_run_triggers_for_rbr= @@global.slave_run_triggers_for_rbr;
12+
SET @@global.slave_exec_mode= IDEMPOTENT;
13+
SET @@global.slave_run_triggers_for_rbr= YES;
14+
SELECT * FROM t1;
15+
C1 C2
16+
create table t2 (id char(2) primary key, cnt int, o char(1), n char(1));
17+
insert into t2 values
18+
('u0', 0, ' ', ' '),('u1', 0, ' ', ' '),
19+
('d0', 0, ' ', ' '),('d1', 0, ' ', ' '),
20+
('i0', 0, ' ', ' '),('i1', 0, ' ', ' ');
21+
create trigger t1_cnt_b before update on t1 for each row
22+
update t2 set cnt=cnt+1, o=old.C1, n=new.C1 where id = 'u0';
23+
create trigger t1_cnt_db before delete on t1 for each row
24+
update t2 set cnt=cnt+1, o=old.C1, n=' ' where id = 'd0';
25+
create trigger t1_cnt_ib before insert on t1 for each row
26+
update t2 set cnt=cnt+1, n=new.C1, o=' ' where id = 'i0';
27+
create trigger t1_cnt_a after update on t1 for each row
28+
update t2 set cnt=cnt+1, o=old.C1, n=new.C1 where id = 'u1';
29+
create trigger t1_cnt_da after delete on t1 for each row
30+
update t2 set cnt=cnt+1, o=old.C1, n=' ' where id = 'd1';
31+
create trigger t1_cnt_ia after insert on t1 for each row
32+
update t2 set cnt=cnt+1, n=new.C1, o=' ' where id = 'i1';
33+
SELECT * FROM t2 order by id;
34+
id cnt o n
35+
d0 0
36+
d1 0
37+
i0 0
38+
i1 0
39+
u0 0
40+
u1 0
41+
# INSERT triggers test
42+
insert into t1 values ('a','b');
43+
SELECT * FROM t2 order by id;
44+
id cnt o n
45+
d0 0
46+
d1 0
47+
i0 1 a
48+
i1 1 a
49+
u0 0
50+
u1 0
51+
# UPDATE triggers test
52+
update t1 set C1= 'd';
53+
SELECT * FROM t2 order by id;
54+
id cnt o n
55+
d0 0
56+
d1 0
57+
i0 1 a
58+
i1 1 a
59+
u0 1 a d
60+
u1 1 a d
61+
# DELETE triggers test
62+
delete from t1 where C1='d';
63+
SELECT * FROM t2 order by id;
64+
id cnt o n
65+
d0 1 d
66+
d1 1 d
67+
i0 1 a
68+
i1 1 a
69+
u0 1 a d
70+
u1 1 a d
71+
# INSERT triggers which cause also UPDATE test (insert duplicate row)
72+
insert into t1 values ('0','1');
73+
SELECT * FROM t2 order by id;
74+
id cnt o n
75+
d0 1 d
76+
d1 1 d
77+
i0 2 0
78+
i1 2 0
79+
u0 1 a d
80+
u1 1 a d
81+
insert into t1 values ('0','1');
82+
SELECT * FROM t2 order by id;
83+
id cnt o n
84+
d0 1 d
85+
d1 1 d
86+
i0 3 0
87+
i1 3 0
88+
u0 2 0 0
89+
u1 2 0 0
90+
# INSERT triggers which cause also DELETE test
91+
# (insert duplicate row in table referenced by foreign key)
92+
insert into t1 values ('1','1');
93+
CREATE TABLE t3 (C1 CHAR(1) primary key, FOREIGN KEY (C1) REFERENCES t1(C1) );
94+
insert into t1 values ('1','1');
95+
SELECT * FROM t2 order by id;
96+
id cnt o n
97+
d0 2 1
98+
d1 2 1
99+
i0 5 1
100+
i1 5 1
101+
u0 2 0 0
102+
u1 2 0 0
103+
drop table t3,t1;
104+
SET @@global.slave_exec_mode= @old_slave_exec_mode;
105+
SET @@global.slave_run_triggers_for_rbr= @old_slave_run_triggers_for_rbr;
106+
drop table t2;
107+
CREATE TABLE t1 (i INT) ENGINE=InnoDB;
108+
CREATE TABLE t2 (i INT) ENGINE=InnoDB;
109+
SET @old_slave_run_triggers_for_rbr= @@global.slave_run_triggers_for_rbr;
110+
SET GLOBAL slave_run_triggers_for_rbr=YES;
111+
CREATE TRIGGER tr AFTER INSERT ON t1 FOR EACH ROW
112+
INSERT INTO t2 VALUES (new.i);
113+
BEGIN;
114+
INSERT INTO t1 VALUES (1);
115+
INSERT INTO t1 VALUES (2);
116+
COMMIT;
117+
select * from t2;
118+
i
119+
1
120+
2
121+
SET @@global.slave_run_triggers_for_rbr= @old_slave_run_triggers_for_rbr;
122+
drop tables t2,t1;
123+
# Triggers on slave do not work if master has some
124+
CREATE TABLE t1 (C1 CHAR(1) primary key, C2 CHAR(1));
125+
SELECT * FROM t1;
126+
C1 C2
127+
create trigger t1_dummy before delete on t1 for each row
128+
set @dummy= 1;
129+
SET @old_slave_exec_mode= @@global.slave_exec_mode;
130+
SET @old_slave_run_triggers_for_rbr= @@global.slave_run_triggers_for_rbr;
131+
SET @@global.slave_exec_mode= IDEMPOTENT;
132+
SET @@global.slave_run_triggers_for_rbr= YES;
133+
SELECT * FROM t1;
134+
C1 C2
135+
create table t2 (id char(2) primary key, cnt int, o char(1), n char(1));
136+
insert into t2 values
137+
('u0', 0, ' ', ' '),('u1', 0, ' ', ' '),
138+
('d0', 0, ' ', ' '),('d1', 0, ' ', ' '),
139+
('i0', 0, ' ', ' '),('i1', 0, ' ', ' ');
140+
create trigger t1_cnt_b before update on t1 for each row
141+
update t2 set cnt=cnt+1, o=old.C1, n=new.C1 where id = 'u0';
142+
create trigger t1_cnt_ib before insert on t1 for each row
143+
update t2 set cnt=cnt+1, n=new.C1, o=' ' where id = 'i0';
144+
create trigger t1_cnt_a after update on t1 for each row
145+
update t2 set cnt=cnt+1, o=old.C1, n=new.C1 where id = 'u1';
146+
create trigger t1_cnt_da after delete on t1 for each row
147+
update t2 set cnt=cnt+1, o=old.C1, n=' ' where id = 'd1';
148+
create trigger t1_cnt_ia after insert on t1 for each row
149+
update t2 set cnt=cnt+1, n=new.C1, o=' ' where id = 'i1';
150+
SELECT * FROM t2 order by id;
151+
id cnt o n
152+
d0 0
153+
d1 0
154+
i0 0
155+
i1 0
156+
u0 0
157+
u1 0
158+
# INSERT triggers test
159+
insert into t1 values ('a','b');
160+
SELECT * FROM t2 order by id;
161+
id cnt o n
162+
d0 0
163+
d1 0
164+
i0 0
165+
i1 0
166+
u0 0
167+
u1 0
168+
# UPDATE triggers test
169+
update t1 set C1= 'd';
170+
SELECT * FROM t2 order by id;
171+
id cnt o n
172+
d0 0
173+
d1 0
174+
i0 0
175+
i1 0
176+
u0 0
177+
u1 0
178+
# DELETE triggers test
179+
delete from t1 where C1='d';
180+
SELECT * FROM t2 order by id;
181+
id cnt o n
182+
d0 0
183+
d1 0
184+
i0 0
185+
i1 0
186+
u0 0
187+
u1 0
188+
# INSERT triggers which cause also UPDATE test (insert duplicate row)
189+
insert into t1 values ('0','1');
190+
SELECT * FROM t2 order by id;
191+
id cnt o n
192+
d0 0
193+
d1 0
194+
i0 1 0
195+
i1 1 0
196+
u0 0
197+
u1 0
198+
insert into t1 values ('0','1');
199+
SELECT * FROM t2 order by id;
200+
id cnt o n
201+
d0 0
202+
d1 0
203+
i0 1 0
204+
i1 1 0
205+
u0 0
206+
u1 0
207+
# INSERT triggers which cause also DELETE test
208+
# (insert duplicate row in table referenced by foreign key)
209+
insert into t1 values ('1','1');
210+
CREATE TABLE t3 (C1 CHAR(1) primary key, FOREIGN KEY (C1) REFERENCES t1(C1) );
211+
insert into t1 values ('1','1');
212+
SELECT * FROM t2 order by id;
213+
id cnt o n
214+
d0 0
215+
d1 0
216+
i0 2 1
217+
i1 2 1
218+
u0 0
219+
u1 0
220+
drop table t3,t1;
221+
SET @@global.slave_exec_mode= @old_slave_exec_mode;
222+
SET @@global.slave_run_triggers_for_rbr= @old_slave_run_triggers_for_rbr;
223+
drop table t2;
224+
#
225+
# MDEV-5513: Trigger is applied to the rows after first one
226+
#
227+
create table t1 (a int, b int);
228+
create table tlog (a int);
229+
set sql_log_bin=0;
230+
create trigger tr1 after insert on t1 for each row insert into tlog values (1);
231+
set sql_log_bin=1;
232+
set @slave_run_triggers_for_rbr.saved = @@slave_run_triggers_for_rbr;
233+
set global slave_run_triggers_for_rbr=1;
234+
create trigger tr2 before insert on t1 for each row set new.b = new.a;
235+
insert into t1 values (1,10),(2,20),(3,30);
236+
select * from t1;
237+
a b
238+
1 10
239+
2 20
240+
3 30
241+
#
242+
# Verify slave skips running triggers if master ran and logged the row events for triggers
243+
#
244+
create table t4(a int, b int);
245+
delete from tlog;
246+
create trigger tr4 before insert on t4 for each row insert into tlog values (1);
247+
insert into t4 values (1, 10),(2, 20);
248+
select * from t4;
249+
a b
250+
1 10
251+
2 20
252+
select * from tlog;
253+
a
254+
1
255+
1
256+
set global slave_run_triggers_for_rbr = @slave_run_triggers_for_rbr.saved;
257+
drop table t1, tlog, t4;
258+
include/rpl_end.inc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include/master-slave.inc
2+
Warnings:
3+
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.
5+
[connection master]
6+
set binlog_format = row;
7+
create table t1 (i int);
8+
create table t2 (i int);
9+
SET @old_slave_run_triggers_for_rbr= @@global.slave_run_triggers_for_rbr;
10+
set global slave_run_triggers_for_rbr=YES;
11+
create trigger tr_before before insert on t1 for each row
12+
insert into t2 values (1);
13+
insert into t1 values (1);
14+
include/wait_for_slave_sql_error_and_skip.inc [errno=1666]
15+
drop tables t1,t2;
16+
SET @@global.slave_run_triggers_for_rbr= @old_slave_run_triggers_for_rbr;
17+
include/rpl_end.inc

0 commit comments

Comments
 (0)