Commit b3473961 authored by Sujatha's avatar Sujatha

MDEV-11094: Blackhole table updates on slave fail when row annotation is enabled

Problem:
=======
rpl_blackhole.test fails when executed with following options
mysqld=--binlog_annotate_row_events=1, mysqld=--replicate_annotate_row_events=1

Test output:
------------
worker[1] Using MTR_BUILD_THREAD 300, with reserved ports 16000..16019
rpl.rpl_blackhole_bug 'mix'              [ pass ]    791
rpl.rpl_blackhole_bug 'row'              [ fail ]
Replicate_Wild_Ignore_Table
Last_Errno	1032
Last_Error	Could not execute Update_rows_v1 event on table test.t1; Can't find
record in 't1', Error_code: 1032; handler error HA_ERR_END_OF_FILE; the event's
master log master-bin.000001, end_log_pos 1510

Analysis:
=========
Enabling "replicate_annotate_row_events" on slave, Tells the slave to write
annotate rows events received from the master to its own binary log. The
received annotate events are applied after the Gtid event as shown below.
thd->query() will be set to the actual query received from the master, through
annotate event. Annotate_rows event should not be deleted after the event is
applied as the thd->query will be used to generate new Annotate_rows event
during applying the subsequent Rows events. After the last Rows event has been
applied, the saved Annotate_rows event (if any) will be deleted.

In balckhole engine all the DML operations are noops as they donot store any
data. They simply return success without doing any operation. But the existing
strictly expects thd->query() to be 'NULL' to identify that row based
replication is in use. This assumption will fail when row annotations are
enabled as the query is not 'NULL'. Hence various row based operations like
'update', 'delete', 'index lookup' will fail when row annotations are enabled.

Fix:
===
Extend the row based replication check to include row annotations as well.
i.e Either the thd->query() is NULL or thd->query() points to query and row
annotations are in use.
parent 8358c6f0
......@@ -11,7 +11,7 @@
# executing statement. If difference is >0, then something was
# written to the binary log on the slave.
connection slave;
# On Connection Slave
let $before = query_get_value("SHOW MASTER STATUS", Position, 1);
--echo [on master]
......
# PURPOSE. Test that blackhole works with replication in all three
# modes: STATEMENT, MIXED, and ROW.
#
# METHOD. We start by creating a table on the master and then change
# the engine to use blackhole on the slave.
#
# After insert/update/delete of one or more rows, the test the
# proceeds to check that replication is running after replicating an
# change, that the blackhole engine does not contain anything (which
# is just a check that the correct engine is used), and that something
# is written to the binary log.
#
# Whe check INSERT, UPDATE, and DELETE statement for tables with no
# key (forcing a range search on the slave), primary keys (using a
# primary key lookup), and index/key with multiple matches (forcing an
# index search).
# We start with no primary key
CREATE TABLE t1 (a INT, b INT, c INT);
CREATE TABLE t2 (a INT, b INT, c INT);
sync_slave_with_master;
ALTER TABLE t1 ENGINE=BLACKHOLE;
connection master;
INSERT INTO t2 VALUES (1,9,1), (2,9,2), (3,9,3), (4,9,4);
sync_slave_with_master;
# Test insert, no primary key
let $statement = INSERT INTO t1 VALUES (1,1,1),(2,1,2),(3,1,3),(4,1,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, no primary key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 1;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, no primary key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 1;
source extra/rpl_tests/rpl_blackhole.test;
# Test INSERT-SELECT into Blackhole, no primary key
let $statement = INSERT INTO t1 SELECT * FROM t2;
source extra/rpl_tests/rpl_blackhole.test;
#
# The MASTER has MyISAM as the engine for both tables. The SLAVE has Blackhole
# on t1 (transactional engine) and MyISAM on t2 (non-transactional engine).
#
# In MIXED mode, the command "INSERT INTO t2 SELECT * FROM t1" is logged as
# statement on the master. On the slave, it is tagged as unsafe because the
# statement mixes both transactional and non-transactional engines and as such
# its changes are logged as rows. However, due to the nature of the blackhole
# engine, no rows are returned and thus any chain replication would make the
# next master on the chain diverge.
#
# Fo this reason, we have disabled the statement.
#
# Test INSERT-SELECT from Blackhole, no primary key
# let $statement = INSERT INTO t2 SELECT * FROM t1;
# source extra/rpl_tests/rpl_blackhole.test;
#
connection master;
ALTER TABLE t1 ADD PRIMARY KEY pk_t1 (a,b);
sync_slave_with_master;
# Test insert, primary key
let $statement = INSERT INTO t1 VALUES (1,2,1),(2,2,2),(3,2,3),(4,2,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, primary key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 2;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, primary key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 2;
source extra/rpl_tests/rpl_blackhole.test;
connection master;
ALTER TABLE t1 DROP PRIMARY KEY, ADD KEY key_t1 (a);
sync_slave_with_master;
# Test insert, key
let $statement = INSERT INTO t1 VALUES (1,3,1),(2,3,2),(3,3,3),(4,3,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 3;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 3;
source extra/rpl_tests/rpl_blackhole.test;
connection master;
DROP TABLE t1,t2;
sync_slave_with_master;
This diff is collapsed.
......@@ -20,81 +20,6 @@ source include/master-slave.inc;
call mtr.add_suppression("Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT");
# We start with no primary key
CREATE TABLE t1 (a INT, b INT, c INT);
CREATE TABLE t2 (a INT, b INT, c INT);
source extra/rpl_tests/rpl_blackhole_basic.test;
sync_slave_with_master;
ALTER TABLE t1 ENGINE=BLACKHOLE;
connection master;
INSERT INTO t2 VALUES (1,9,1), (2,9,2), (3,9,3), (4,9,4);
sync_slave_with_master;
# Test insert, no primary key
let $statement = INSERT INTO t1 VALUES (1,1,1),(2,1,2),(3,1,3),(4,1,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, no primary key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 1;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, no primary key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 1;
source extra/rpl_tests/rpl_blackhole.test;
# Test INSERT-SELECT into Blackhole, no primary key
let $statement = INSERT INTO t1 SELECT * FROM t2;
source extra/rpl_tests/rpl_blackhole.test;
#
# The MASTER has MyISAM as the engine for both tables. The SLAVE has Blackhole
# on t1 (transactional engine) and MyISAM on t2 (non-transactional engine).
#
# In MIXED mode, the command "INSERT INTO t2 SELECT * FROM t1" is logged as
# statement on the master. On the slave, it is tagged as unsafe because the
# statement mixes both transactional and non-transactional engines and as such
# its changes are logged as rows. However, due to the nature of the blackhole
# engine, no rows are returned and thus any chain replication would make the
# next master on the chain diverge.
#
# Fo this reason, we have disabled the statement.
#
# Test INSERT-SELECT from Blackhole, no primary key
# let $statement = INSERT INTO t2 SELECT * FROM t1;
# source extra/rpl_tests/rpl_blackhole.test;
#
connection master;
ALTER TABLE t1 ADD PRIMARY KEY pk_t1 (a,b);
# Test insert, primary key
let $statement = INSERT INTO t1 VALUES (1,2,1),(2,2,2),(3,2,3),(4,2,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, primary key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 2;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, primary key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 2;
source extra/rpl_tests/rpl_blackhole.test;
connection master;
ALTER TABLE t1 DROP PRIMARY KEY, ADD KEY key_t1 (a);
# Test insert, key
let $statement = INSERT INTO t1 VALUES (1,3,1),(2,3,2),(3,3,3),(4,3,4);
source extra/rpl_tests/rpl_blackhole.test;
# Test update, key
let $statement = UPDATE t1 SET c = 2*c WHERE a % 2 = 0 AND b = 3;
source extra/rpl_tests/rpl_blackhole.test;
# Test delete, key
let $statement = DELETE FROM t1 WHERE a % 2 = 0 AND b = 3;
source extra/rpl_tests/rpl_blackhole.test;
connection master;
DROP TABLE t1,t2;
--source include/rpl_end.inc
--binlog_annotate_row_events --timezone=GMT-3
--binlog_annotate_row_events --replicate_annotate_row_events
# ==== Purpose ====
#
# Test verifies that when "replicate_annotate_row_events" are enabled on slave
# the DML operations on blackhole engine will be successful. It also ensures
# that Annotate events are logged into slave's binary log.
#
# ==== Implementation ====
#
# Steps:
# 0 - Enable "replicate_annotate_row_events" on slave and do DML operations
# on master.
# 1 - Slave server will successfully apply the DML operations and it is in
# sync with master.
# 2 - Verify that the "show binlog events" prints all annotate events.
# 3 - Stream the slave's binary log using "mysqlbinlog" tool and verify
# that the Annotate events are being displayed.
#
# ==== References ====
#
# MDEV-11094: Blackhole table updates on slave fail when row annotation is
# enabled
source include/have_blackhole.inc;
source include/have_binlog_format_row.inc;
source include/binlog_start_pos.inc;
source include/master-slave.inc;
SET timestamp=1000000000;
RESET MASTER;
connection slave;
SET timestamp=1000000000;
RESET MASTER;
connection master;
source extra/rpl_tests/rpl_blackhole_basic.test;
# Verify on slave.
connection slave;
FLUSH LOGS;
--replace_column 2 # 5 #
--replace_result $binlog_start_pos <start_pos>
--replace_regex /table_id: [0-9]+/table_id: #/ /\/\* xid=.* \*\//\/* xid= *\//
--eval show binlog events in 'slave-bin.000001' from $binlog_start_pos
let $MYSQLD_DATADIR= `select @@datadir`;
--replace_regex /server id [0-9]*/server id #/ /server v [^ ]*/server v #.##.##/ /exec_time=[0-9]*/exec_time=#/ /thread_id=[0-9]*/thread_id=#/ /table id [0-9]*/table id #/ /mapped to number [0-9]*/mapped to number #/ /end_log_pos [0-9]*/end_log_pos #/ /# at [0-9]*/# at #/
--exec $MYSQL_BINLOG --base64-output=decode-rows $MYSQLD_DATADIR/slave-bin.000001
source include/rpl_end.inc;
......@@ -25,6 +25,16 @@
#include "ha_blackhole.h"
#include "sql_class.h" // THD, SYSTEM_THREAD_SLAVE_SQL
static bool is_row_based_replication(THD *thd)
{
/*
A row event which has its thd->query() == NULL or a row event which has
replicate_annotate_row_events enabled. In the later case the thd->query()
will be pointing to the query, received through replicated annotate event
from master.
*/
return ((thd->query() == NULL) || thd->variables.binlog_annotate_row_events);
}
/* Static declarations for handlerton */
static handler *blackhole_create_handler(handlerton *hton,
......@@ -109,7 +119,8 @@ int ha_blackhole::update_row(const uchar *old_data, uchar *new_data)
{
DBUG_ENTER("ha_blackhole::update_row");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
DBUG_RETURN(0);
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
......@@ -118,7 +129,8 @@ int ha_blackhole::delete_row(const uchar *buf)
{
DBUG_ENTER("ha_blackhole::delete_row");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
DBUG_RETURN(0);
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
......@@ -135,7 +147,8 @@ int ha_blackhole::rnd_next(uchar *buf)
int rc;
DBUG_ENTER("ha_blackhole::rnd_next");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
rc= 0;
else
rc= HA_ERR_END_OF_FILE;
......@@ -220,7 +233,8 @@ int ha_blackhole::index_read_map(uchar * buf, const uchar * key,
int rc;
DBUG_ENTER("ha_blackhole::index_read");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
rc= 0;
else
rc= HA_ERR_END_OF_FILE;
......@@ -235,7 +249,8 @@ int ha_blackhole::index_read_idx_map(uchar * buf, uint idx, const uchar * key,
int rc;
DBUG_ENTER("ha_blackhole::index_read_idx");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
rc= 0;
else
rc= HA_ERR_END_OF_FILE;
......@@ -249,7 +264,8 @@ int ha_blackhole::index_read_last_map(uchar * buf, const uchar * key,
int rc;
DBUG_ENTER("ha_blackhole::index_read_last");
THD *thd= ha_thd();
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL && thd->query() == NULL)
if (thd->system_thread == SYSTEM_THREAD_SLAVE_SQL &&
is_row_based_replication(thd))
rc= 0;
else
rc= HA_ERR_END_OF_FILE;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment