-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PS-9391 Fixes replication break error because of HASH_SCAN
https://perconadev.atlassian.net/browse/PS-9391 Problem ======= When a replica's slave_rows_search_algorithms is set to HASH_SCAN, the replication may break with HA_ERR_KEY_NOT_FOUND. Analysis ======== When a replica's slave_rows_search_algorithms is set to HASH_SCAN, it prepares a unique key list for all the rows in a particular Row_event. The same unique key list will later be used to retrieve all tuples associated to each key in the list from storage engine. In the case of multiple updates targeted at the same row like how it is shown in the testcase, it may happen that this unique key list filled with entries which don't exist yet in the table. This is a problem when there is an intermediate update which changes the value of the index column to a lesser value than the original entry and that changed value is used in another update as shown in the second part of the testcase. It is an issue because the unique key list is a std::set which internally sorts it's entries. When this sorting happens, the first entry of the list could potentially be a value which doesn't exist in the table and the when it is searched in next_record_scan() method, it fails returning HA_ERR_KEY_NOT_FOUND error. Solution ======== Instead of using std::set to store the distinct keys, a combination of unordered_set and a list is used to preserve the original order of updates and avoid duplicates at the same time which prevents the side effects of sorting.
- Loading branch information
1 parent
d37bf16
commit 92d965a
Showing
4 changed files
with
179 additions
and
23 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
mysql-test/suite/rpl/r/rpl_row_multi_update_of_same_row_sort_before_apply.result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
include/master-slave.inc | ||
Warnings: | ||
Note #### Sending passwords in plain text without SSL/TLS is extremely insecure. | ||
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. | ||
[connection master] | ||
[connection slave] | ||
set @prior_slave_rows_search_algorithms=@@global.slave_rows_search_algorithms; | ||
Warnings: | ||
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release. | ||
set @@global.slave_rows_search_algorithms= 'HASH_SCAN'; | ||
Warnings: | ||
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release. | ||
[connection master] | ||
CREATE TABLE t1 (i INT, INDEX t1_i(i)); | ||
CREATE FUNCTION f1 () RETURNS INT BEGIN | ||
UPDATE t1 SET i = 11 WHERE i = 10; | ||
UPDATE t1 SET i = 12 WHERE i = 11; | ||
RETURN 0; | ||
END| | ||
include/sync_slave_sql_with_master.inc | ||
[connection master] | ||
INSERT INTO t1 VALUES (10); | ||
SELECT f1(); | ||
f1() | ||
0 | ||
include/sync_slave_sql_with_master.inc | ||
include/assert.inc ['There is only one row in table t1'] | ||
[connection master] | ||
include/diff_tables.inc [master:test.t1, slave:test.t1] | ||
CREATE FUNCTION f2 () RETURNS INT BEGIN | ||
UPDATE t1 SET i = 11 WHERE i = 12; | ||
UPDATE t1 SET i = 13 WHERE i = 11; | ||
RETURN 0; | ||
END| | ||
include/sync_slave_sql_with_master.inc | ||
[connection master] | ||
SELECT f2(); | ||
f2() | ||
0 | ||
include/sync_slave_sql_with_master.inc | ||
include/assert.inc ['There is only one row in table t1'] | ||
[connection master] | ||
include/diff_tables.inc [master:test.t1, slave:test.t1] | ||
[connection master] | ||
SELECT * FROM t1; | ||
i | ||
13 | ||
DROP FUNCTION f1; | ||
DROP FUNCTION f2; | ||
DROP TABLE t1; | ||
[connection slave] | ||
set @@global.slave_rows_search_algorithms= @prior_slave_rows_search_algorithms; | ||
Warnings: | ||
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release. | ||
include/rpl_end.inc |
84 changes: 84 additions & 0 deletions
84
mysql-test/suite/rpl/t/rpl_row_multi_update_of_same_row_sort_before_apply.test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Bug#115741 Test whether setting slave_rows_search_algorithms to HASH_SCAN works properly | ||
# when multiple updates are targeted to the same row within a single update rows log event | ||
|
||
--source include/have_binlog_format_row.inc | ||
--source include/set_privilege_checks_user_as_system_user.inc | ||
--source include/master-slave.inc | ||
|
||
--source include/rpl_connection_slave.inc | ||
set @prior_slave_rows_search_algorithms=@@global.slave_rows_search_algorithms; | ||
set @@global.slave_rows_search_algorithms= 'HASH_SCAN'; | ||
|
||
# Scenarios considered in this test case involve a table with no primary key but an index on the column | ||
# When slave_rows_search_algorithms is set to HASH_SCAN, a list "m_distinct_keys" is created internally | ||
# to store the unique key values which, later will be used to perform a table scan to apply the events | ||
# for the rows present in the table. | ||
--source include/rpl_connection_master.inc | ||
# Create a table, with no primary key and an index. | ||
CREATE TABLE t1 (i INT, INDEX t1_i(i)); | ||
|
||
# Scenario no.1 | ||
# Sorting the elements of m_distinct_keys doesn't alter the original order of updates | ||
# Create a stored function so that only one Update_rows_log_event is generated. | ||
--delimiter | | ||
CREATE FUNCTION f1 () RETURNS INT BEGIN | ||
UPDATE t1 SET i = 11 WHERE i = 10; | ||
UPDATE t1 SET i = 12 WHERE i = 11; | ||
RETURN 0; | ||
END| | ||
--delimiter ; | ||
--source include/sync_slave_sql_with_master.inc | ||
|
||
--source include/rpl_connection_master.inc | ||
INSERT INTO t1 VALUES (10); | ||
SELECT f1(); | ||
|
||
--source include/sync_slave_sql_with_master.inc | ||
--let $assert_text= 'There is only one row in table t1' | ||
--let $assert_cond= [SELECT COUNT(*) FROM t1] = 1 | ||
--source include/assert.inc | ||
|
||
--source include/rpl_connection_master.inc | ||
|
||
# Verify that there is no difference between tables of master and slave. | ||
--let diff_tables=master:test.t1, slave:test.t1 | ||
--source include/diff_tables.inc | ||
|
||
|
||
# Scenario no.2 | ||
# Sorting the elements of m_distinct_keys ALTERs the original order of updates causing | ||
# the case where the key value from the list doesn't exist in the table yet until other | ||
# UPDATEs are executed. | ||
--delimiter | | ||
CREATE FUNCTION f2 () RETURNS INT BEGIN | ||
UPDATE t1 SET i = 11 WHERE i = 12; | ||
UPDATE t1 SET i = 13 WHERE i = 11; | ||
RETURN 0; | ||
END| | ||
--delimiter ; | ||
--source include/sync_slave_sql_with_master.inc | ||
|
||
--source include/rpl_connection_master.inc | ||
SELECT f2(); | ||
|
||
--source include/sync_slave_sql_with_master.inc | ||
--let $assert_text= 'There is only one row in table t1' | ||
--let $assert_cond= [SELECT COUNT(*) FROM t1] = 1 | ||
--source include/assert.inc | ||
|
||
--source include/rpl_connection_master.inc | ||
|
||
# Verify that there is no difference between tables of master and slave. | ||
--let diff_tables=master:test.t1, slave:test.t1 | ||
--source include/diff_tables.inc | ||
|
||
# Cleanup | ||
--source include/rpl_connection_master.inc | ||
SELECT * FROM t1; | ||
DROP FUNCTION f1; | ||
DROP FUNCTION f2; | ||
DROP TABLE t1; | ||
|
||
--source include/rpl_connection_slave.inc | ||
set @@global.slave_rows_search_algorithms= @prior_slave_rows_search_algorithms; | ||
--source include/rpl_end.inc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters