rpl_idempotency.test 11 KB
Newer Older
1 2 3 4
# Testing various forms of idempotency for replication that should
# work the same way under statement based as under row based.

source include/master-slave.inc;
5 6 7 8
connection master;
source include/have_innodb.inc;
connection slave;
source include/have_innodb.inc;
9

10
# Add suppression for expected warning(s) in slaves error log
11 12
call mtr.add_suppression("Slave: Can't find record in 't.' Error_code: 1032");
call mtr.add_suppression("Slave: Cannot delete or update a parent row: a foreign key constraint fails .* Error_code: 1451");
13
call mtr.add_suppression("Slave: Cannot add or update a child row: a foreign key constraint fails .* Error_code: 1452");
14

15 16
SET @old_slave_exec_mode= @@global.slave_exec_mode;

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
connection master;
CREATE TABLE t1 (a INT PRIMARY KEY);
CREATE TABLE t2 (a INT);
INSERT INTO t1 VALUES (-1),(-2),(-3);
INSERT INTO t2 VALUES (-1),(-2),(-3);
sync_slave_with_master;

# A delete for a row that does not exist, the statement is
# deliberately written to be idempotent for statement-based
# replication as well. We test this towards both a table with a
# primary key and without a primary key.

connection slave;
DELETE FROM t1 WHERE a = -2;
DELETE FROM t2 WHERE a = -2;
connection master;
DELETE FROM t1 WHERE a = -2;
DELETE FROM t2 WHERE a = -2;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
sync_slave_with_master;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

# An insert of a row that already exists. Since we are replacing the
# row if it already exists, the most apropriate representation is
# INSERT IGNORE. We only test this towards a table with a primary key,
# since the other case does not make sense.

INSERT IGNORE INTO t1 VALUES (-2);
connection master;
INSERT IGNORE INTO t1 VALUES (-2);
SELECT * FROM t1 ORDER BY a;
sync_slave_with_master;
SELECT * FROM t1 ORDER BY a;
let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

# BUG#19958: RBR idempotency issue for UPDATE and DELETE

# Statement-based and row-based replication have different behaviour
# when updating a row with an explicit WHERE-clause that matches
# exactly one row (or no row at all). For statement-based replication,
# the statement is idempotent since the first time it is executed, it
# will update exactly one row, and the second time it will not update
# any row at all.  This was not the case for row-based replication, so
# we test under both row-based and statement-based replication both
# for tables with and without primary keys.

connection slave;
UPDATE t1 SET a = 1 WHERE a = -1;
UPDATE t2 SET a = 1 WHERE a = -1;
connection master;
UPDATE t1 SET a = 1 WHERE a = -1;
UPDATE t2 SET a = 1 WHERE a = -1;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
sync_slave_with_master;
SELECT * FROM t1 ORDER BY a;
SELECT * FROM t2 ORDER BY a;
let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

connection master;
DROP TABLE t1, t2;
sync_slave_with_master;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

# bug#31609 Not all RBR slave errors reported as errors
# bug#31552 Replication breaks when deleting rows from out-of-sync table
#           without PK

#
# Idempotent applying is not default any longer.
# The default for slave-exec-mode option and server
# variable slave_exec_mode  is 'STRICT'.
# When 'STRICT' mode is set, the slave SQL thread will stop whenever
# the row to change is not found. In 'IDEMPOTENT' mode, the SQL thread
# will continue running and apply the row - replace if it's Write_rows event -
# or skip to the next event.

# the previous part of the tests was with IDEMPOTENT slave's mode.


#
# Other than above idempotent errors dealing with foreign keys constraint
#

select @@global.slave_exec_mode /* must be IDEMPOTENT */;

connection master;

create table ti1 (b int primary key) engine = innodb;
create table ti2 (a int primary key, b int, foreign key (b) references ti1(b))
     engine = innodb;
set foreign_key_checks=1 /* ensure the check */;

insert into ti1 values (1),(2),(3);
insert into ti2 set a=2, b=2;

sync_slave_with_master;

#connection slave;
select * from ti1 order by b /* must be (1),(2),(3) */;
insert into ti2 set a=1, b=1;
select * from ti2 order by b /* must be (1,1) (2,2) */;

connection master;

# from now on checking rbr specific idempotent errors
set @save_binlog_format= @@session.binlog_format;
set @@session.binlog_format= row;
delete from ti1 where b=1;

select * from ti1 order by b /* must be (2),(3) */;

# slave must catch up (expect some warnings in error.log)
sync_slave_with_master;

#connection slave;
select * from ti1 order by b /* must stays as were on master (1),(2),(3) */;

delete from ti1 where b=3;

connection master;
insert into ti2 set a=3, b=3;

# slave must catch up (expect some warnings in error.log)
sync_slave_with_master;

#connection slave;
select * from ti2 order by b /* must be (1,1),(2,2) - not inserted */;


#
# Checking the new global sys variable
#

connection slave;

set global slave_exec_mode='IDEMPOTENT';
set global slave_exec_mode='STRICT';

# checking mutual exclusion for the options
--error ER_SLAVE_AMBIGOUS_EXEC_MODE
set global slave_exec_mode='IDEMPOTENT,STRICT';

select @@global.slave_exec_mode /* must be STRICT */;

#
# Checking stops.
# In the following sections strict slave sql thread is going to
# stop when faces an idempotent error. In order to proceed
# the mode is temporarily switched to indempotent.
#

#
--echo *** foreign keys errors as above now forces to stop
#

connection master;

set foreign_key_checks=0;
drop table ti2, ti1;

create table ti1 (b int primary key) engine = innodb;
create table ti2 (a int primary key, b int, foreign key (b) references ti1(b))
     engine = innodb;
set foreign_key_checks=1 /* ensure the check */;

insert into ti1 values (1),(2),(3);
insert into ti2 set a=2, b=2;

sync_slave_with_master;

#connection slave;
select * from ti1 order by b /* must be (1),(2),(3) */;
--echo *** conspire future problem
insert into ti2 set a=1, b=1;
select * from ti2 order by b /* must be (1,1) (2,2) */;

connection master;

delete from ti1 where b=1 /* offending delete event */;
select * from ti1 order by b /* must be (2),(3) */;

# foreign key: row is referenced

212
--echo *** slave must stop (Trying to delete a referenced foreing key)
213
connection slave;
214
source include/wait_for_slave_sql_to_stop.inc;
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

select * from ti1 order by b /* must be (1),(2),(3) - not deleted */;
set foreign_key_checks= 0;
delete from ti2 where b=1;
set foreign_key_checks= 1;
set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';

connection master;

sync_slave_with_master;

#connection slave;
--echo *** conspire the following insert failure
# foreign key: no referenced row

--echo *** conspire future problem
delete from ti1 where b=3;

connection master;
insert into ti2 set a=3, b=3 /* offending write event */;

246
--echo *** slave must stop (Trying to insert an invalid foreign key)
247
connection slave;
248
source include/wait_for_slave_sql_to_stop.inc;
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

select * from ti2 order by b /* must be (2,2) */;
set foreign_key_checks= 0;
insert into ti1 set b=3;
set foreign_key_checks= 1;
set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';

connection master;

sync_slave_with_master;

select * from ti2 order by b /* must be (2,2),(3,3) */;

# 
--echo *** other errors
# 

# dup key insert

#connection slave;
--echo *** conspiring query
insert into ti1 set b=1;

connection master;
insert into ti1 set b=1 /* offending write event */;

285
--echo *** slave must stop (Trying to insert a dupliacte key)
286
connection slave;
287
source include/wait_for_slave_sql_to_stop.inc;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

set foreign_key_checks= 0;
delete from ti1 where b=1;
set foreign_key_checks= 1;
set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';

# key not found

connection master;

CREATE TABLE t1 (a INT PRIMARY KEY);
CREATE TABLE t2 (a INT);
INSERT INTO t1 VALUES (-1),(-2),(-3);
INSERT INTO t2 VALUES (-1),(-2),(-3);
sync_slave_with_master;

#connection slave;
DELETE FROM t1 WHERE a = -2;
DELETE FROM t2 WHERE a = -2;
connection master;
DELETE FROM t1 WHERE a = -2;

320
--echo *** slave must stop (Key was not found)
321
connection slave;
322
source include/wait_for_slave_sql_to_stop.inc;
323 324 325 326 327 328 329 330 331 332 333 334 335 336

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';

connection master;
337 338
DELETE FROM t2 WHERE a = -2; 
--echo *** slave must stop (Key was not found)
339
connection slave;
340
source include/wait_for_slave_sql_to_stop.inc;
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';

UPDATE t1 SET a = 1 WHERE a = -1;
UPDATE t2 SET a = 1 WHERE a = -1;

connection master;
UPDATE t1 SET a = 1 WHERE a = -1;

360
--echo *** slave must stop (Key was not found)
361
connection slave;
362
source include/wait_for_slave_sql_to_stop.inc;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
set global slave_exec_mode='STRICT';


connection master;
UPDATE t2 SET a = 1 WHERE a = -1;

380
--echo *** slave must stop (Key was not found)
381
connection slave;
382
source include/wait_for_slave_sql_to_stop.inc;
383 384 385 386 387 388 389 390 391 392 393

let $last_error = query_get_value("SHOW SLAVE STATUS", Last_SQL_Errno, 1);
disable_query_log;
eval SELECT "$last_error" AS Last_SQL_Error;
enable_query_log;

set global slave_exec_mode='IDEMPOTENT';
start slave sql_thread;
connection master;
sync_slave_with_master;
#connection slave;
394
SET @@global.slave_exec_mode= @old_slave_exec_mode;
395 396 397 398 399 400 401

# cleanup for bug#31609 tests

connection master;
set @@session.binlog_format= @save_binlog_format;
drop table t1,t2,ti2,ti1;

402
--source include/master-slave-end.inc
403 404

--echo *** end of tests