Skip to content

Commit 1f26d08

Browse files
committed
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.
1 parent f01c613 commit 1f26d08

File tree

4 files changed

+182
-23
lines changed

4 files changed

+182
-23
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 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.
5+
[connection master]
6+
[connection slave]
7+
set @prior_slave_rows_search_algorithms=@@global.slave_rows_search_algorithms;
8+
Warnings:
9+
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release.
10+
set @@global.slave_rows_search_algorithms= 'HASH_SCAN';
11+
Warnings:
12+
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release.
13+
[connection master]
14+
CREATE TABLE t1 (i INT, INDEX t1_i(i));
15+
CREATE FUNCTION f1 () RETURNS INT BEGIN
16+
UPDATE t1 SET i = 11 WHERE i = 10;
17+
UPDATE t1 SET i = 12 WHERE i = 11;
18+
RETURN 0;
19+
END|
20+
include/sync_slave_sql_with_master.inc
21+
[connection master]
22+
INSERT INTO t1 VALUES (10);
23+
SELECT f1();
24+
f1()
25+
0
26+
include/sync_slave_sql_with_master.inc
27+
include/assert.inc ['There is only one row in table t1']
28+
[connection master]
29+
include/diff_tables.inc [master:test.t1, slave:test.t1]
30+
CREATE FUNCTION f2 () RETURNS INT BEGIN
31+
UPDATE t1 SET i = 11 WHERE i = 12;
32+
UPDATE t1 SET i = 13 WHERE i = 11;
33+
RETURN 0;
34+
END|
35+
include/sync_slave_sql_with_master.inc
36+
[connection master]
37+
SELECT f2();
38+
f2()
39+
0
40+
include/sync_slave_sql_with_master.inc
41+
include/assert.inc ['There is only one row in table t1']
42+
[connection master]
43+
include/diff_tables.inc [master:test.t1, slave:test.t1]
44+
[connection master]
45+
SELECT * FROM t1;
46+
i
47+
13
48+
DROP FUNCTION f1;
49+
DROP FUNCTION f2;
50+
DROP TABLE t1;
51+
[connection slave]
52+
set @@global.slave_rows_search_algorithms= @prior_slave_rows_search_algorithms;
53+
Warnings:
54+
Warning 1287 '@@slave_rows_search_algorithms' is deprecated and will be removed in a future release.
55+
include/rpl_end.inc
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Bug#115741 Test whether setting slave_rows_search_algorithms to HASH_SCAN works properly
2+
# when multiple updates are targeted to the same row within a single update rows log event
3+
4+
--source include/have_binlog_format_row.inc
5+
--source include/set_privilege_checks_user_as_system_user.inc
6+
--source include/master-slave.inc
7+
8+
--source include/rpl_connection_slave.inc
9+
set @prior_slave_rows_search_algorithms=@@global.slave_rows_search_algorithms;
10+
set @@global.slave_rows_search_algorithms= 'HASH_SCAN';
11+
12+
# Scenarios considered in this test case involve a table with no primary key but an index on the column
13+
# When slave_rows_search_algorithms is set to HASH_SCAN, a list "m_distinct_keys" is created internally
14+
# to store the unique key values which, later will be used to perform a table scan to apply the events
15+
# for the rows present in the table.
16+
--source include/rpl_connection_master.inc
17+
# Create a table, with no primary key and an index.
18+
CREATE TABLE t1 (i INT, INDEX t1_i(i));
19+
20+
# Scenario no.1
21+
# Sorting the elements of m_distinct_keys doesn't alter the original order of updates
22+
# Create a stored function so that only one Update_rows_log_event is generated.
23+
--delimiter |
24+
CREATE FUNCTION f1 () RETURNS INT BEGIN
25+
UPDATE t1 SET i = 11 WHERE i = 10;
26+
UPDATE t1 SET i = 12 WHERE i = 11;
27+
RETURN 0;
28+
END|
29+
--delimiter ;
30+
--source include/sync_slave_sql_with_master.inc
31+
32+
--source include/rpl_connection_master.inc
33+
INSERT INTO t1 VALUES (10);
34+
SELECT f1();
35+
36+
--source include/sync_slave_sql_with_master.inc
37+
--let $assert_text= 'There is only one row in table t1'
38+
--let $assert_cond= [SELECT COUNT(*) FROM t1] = 1
39+
--source include/assert.inc
40+
41+
--source include/rpl_connection_master.inc
42+
43+
# Verify that there is no difference between tables of master and slave.
44+
--let diff_tables=master:test.t1, slave:test.t1
45+
--source include/diff_tables.inc
46+
47+
48+
# Scenario no.2
49+
# Sorting the elements of m_distinct_keys ALTERs the original order of updates causing
50+
# the case where the key value from the list doesn't exist in the table yet until other
51+
# UPDATEs are executed.
52+
--delimiter |
53+
CREATE FUNCTION f2 () RETURNS INT BEGIN
54+
UPDATE t1 SET i = 11 WHERE i = 12;
55+
UPDATE t1 SET i = 13 WHERE i = 11;
56+
RETURN 0;
57+
END|
58+
--delimiter ;
59+
--source include/sync_slave_sql_with_master.inc
60+
61+
--source include/rpl_connection_master.inc
62+
SELECT f2();
63+
64+
--source include/sync_slave_sql_with_master.inc
65+
--let $assert_text= 'There is only one row in table t1'
66+
--let $assert_cond= [SELECT COUNT(*) FROM t1] = 1
67+
--source include/assert.inc
68+
69+
--source include/rpl_connection_master.inc
70+
71+
# Verify that there is no difference between tables of master and slave.
72+
--let diff_tables=master:test.t1, slave:test.t1
73+
--source include/diff_tables.inc
74+
75+
# Cleanup
76+
--source include/rpl_connection_master.inc
77+
SELECT * FROM t1;
78+
DROP FUNCTION f1;
79+
DROP FUNCTION f2;
80+
DROP TABLE t1;
81+
82+
--source include/rpl_connection_slave.inc
83+
set @@global.slave_rows_search_algorithms= @prior_slave_rows_search_algorithms;
84+
--source include/rpl_end.inc

sql/log_event.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7870,7 +7870,7 @@ Rows_log_event::Rows_log_event(THD *thd_arg, TABLE *tbl_arg,
78707870
m_curr_row_end(nullptr),
78717871
m_key(nullptr),
78727872
m_key_info(nullptr),
7873-
m_distinct_keys(Key_compare(&m_key_info)),
7873+
m_distinct_keys(0, Key_hash(&m_key_info), Key_equal(&m_key_info)),
78747874
m_distinct_key_spare_buf(nullptr) {
78757875
DBUG_TRACE;
78767876
common_header->type_code = event_type;
@@ -7961,7 +7961,7 @@ Rows_log_event::Rows_log_event(
79617961
m_curr_row_end(nullptr),
79627962
m_key(nullptr),
79637963
m_key_info(nullptr),
7964-
m_distinct_keys(Key_compare(&m_key_info)),
7964+
m_distinct_keys(0, Key_hash(&m_key_info), Key_equal(&m_key_info)),
79657965
m_distinct_key_spare_buf(nullptr)
79667966
#endif
79677967
{
@@ -9052,9 +9052,9 @@ int Rows_log_event::next_record_scan(bool first_read) {
90529052
marker according to the next key value that we have in the list.
90539053
*/
90549054
if (error) {
9055-
if (m_itr != m_distinct_keys.end()) {
9056-
m_key = *m_itr;
9057-
m_itr++;
9055+
if (m_distinct_key_idx != m_distinct_keys_original_order.size()) {
9056+
m_key = m_distinct_keys_original_order[m_distinct_key_idx];
9057+
m_distinct_key_idx++;
90589058
first_read = true;
90599059
} else {
90609060
if (!is_trx_retryable_upon_engine_error(error))
@@ -9088,14 +9088,13 @@ int Rows_log_event::open_record_scan() {
90889088

90899089
if (m_key_index < MAX_KEY) {
90909090
if (m_rows_lookup_algorithm == ROW_LOOKUP_HASH_SCAN) {
9091-
/* initialize the iterator over the list of distinct keys that we have */
9092-
m_itr = m_distinct_keys.begin();
9093-
9094-
/* get the first element from the list of keys and increment the
9095-
iterator
9091+
/* Initialize the index to go over the vector of distinct keys */
9092+
m_distinct_key_idx = 0;
9093+
/* get the first element from the vector of keys and increment the
9094+
index
90969095
*/
9097-
m_key = *m_itr;
9098-
m_itr++;
9096+
m_key = m_distinct_keys_original_order[m_distinct_key_idx];
9097+
m_distinct_key_idx++;
90999098
} else {
91009099
/* this is an INDEX_SCAN we need to store the key in m_key */
91019100
assert((m_rows_lookup_algorithm == ROW_LOOKUP_INDEX_SCAN) && m_key);
@@ -9142,9 +9141,9 @@ int Rows_log_event::add_key_to_distinct_keyset() {
91429141
DBUG_TRACE;
91439142
assert(m_key_index < MAX_KEY);
91449143
key_copy(m_distinct_key_spare_buf, m_table->record[0], m_key_info, 0);
9145-
std::pair<std::set<uchar *, Key_compare>::iterator, bool> ret =
9146-
m_distinct_keys.insert(m_distinct_key_spare_buf);
9144+
auto ret = m_distinct_keys.insert(m_distinct_key_spare_buf);
91479145
if (ret.second) {
9146+
m_distinct_keys_original_order.push_back(m_distinct_key_spare_buf);
91489147
/* Insert is successful, so allocate a new buffer for next key */
91499148
m_distinct_key_spare_buf = (uchar *)thd->alloc(m_key_info->key_length);
91509149
if (!m_distinct_key_spare_buf) {

sql/log_event.h

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
#include <functional>
4040
#include <list>
4141
#include <map>
42-
#include <set>
4342
#include <string>
4443
#include <string_view>
44+
#include <unordered_set>
4545

4646
#include "my_aes.h"
4747
#include "libbinlogevents/include/binlog_event.h"
@@ -2882,30 +2882,51 @@ class Rows_log_event : public virtual binary_log::Rows_event, public Log_event {
28822882
uchar *m_key; /* Buffer to keep key value during searches */
28832883
uint m_key_index;
28842884
KEY *m_key_info; /* Points to description of index #m_key_index */
2885-
class Key_compare {
2885+
class Key_equal {
28862886
public:
28872887
/**
28882888
@param ki Where to find KEY description
28892889
@note m_distinct_keys is instantiated when Rows_log_event is constructed;
2890-
it stores a Key_compare object internally. However at that moment, the
2890+
it stores a Key_equal object internally. However at that moment, the
28912891
index (KEY*) to use for comparisons, is not yet known. So, at
2892-
instantiation, we indicate the Key_compare the place where it can
2892+
instantiation, we indicate the Key_equal the place where it can
28932893
find the KEY* when needed (this place is Rows_log_event::m_key_info),
2894-
Key_compare remembers the place in member m_key_info.
2894+
Key_equal remembers the place in member m_key_info.
28952895
Before we need to do comparisons - i.e. before we need to insert
28962896
elements, we update Rows_log_event::m_key_info once for all.
28972897
*/
2898-
Key_compare(KEY **ki = nullptr) : m_key_info(ki) {}
2898+
Key_equal(KEY **ki = nullptr) : m_key_info(ki) {}
28992899
bool operator()(uchar *k1, uchar *k2) const {
29002900
return key_cmp2((*m_key_info)->key_part, k1, (*m_key_info)->key_length,
2901-
k2, (*m_key_info)->key_length) < 0;
2901+
k2, (*m_key_info)->key_length) == 0;
29022902
}
29032903

29042904
private:
29052905
KEY **m_key_info;
29062906
};
2907-
std::set<uchar *, Key_compare> m_distinct_keys;
2908-
std::set<uchar *, Key_compare>::iterator m_itr;
2907+
class Key_hash {
2908+
public:
2909+
Key_hash(KEY **ki = nullptr) : m_key_info(ki) {}
2910+
size_t operator()(uchar *ptr) const {
2911+
size_t hash = 0;
2912+
if (ptr) {
2913+
std::string_view sv{reinterpret_cast<const char *>(ptr),
2914+
(*m_key_info)->key_length};
2915+
return std::hash<std::string_view>{}(sv);
2916+
}
2917+
return hash;
2918+
}
2919+
2920+
private:
2921+
KEY **m_key_info;
2922+
};
2923+
std::unordered_set<uchar *, Key_hash, Key_equal> m_distinct_keys;
2924+
2925+
/**
2926+
A linear list to store the distinct keys preserving the insert order
2927+
*/
2928+
std::vector<uchar *> m_distinct_keys_original_order;
2929+
std::size_t m_distinct_key_idx = 0;
29092930
/**
29102931
A spare buffer which will be used when saving the distinct keys
29112932
for doing an index scan with HASH_SCAN search algorithm.

0 commit comments

Comments
 (0)