Skip to content

Commit f37d380

Browse files
MDEV-34124: Improve sequences replication with Galera
- use shared key for sequence update certification - employ native replication's code to apply changes for sequences which handles all corner cases properly - fix the tests to allow more transactions using sequences to be accepted That way the sequence is always updated to the maximum value independent of the order of updates, and shared certification keys allow to improve acceptance ratio of concurrent transactions that use sequences. It's reflected in the test changes.
1 parent 814787f commit f37d380

File tree

9 files changed

+57
-35
lines changed

9 files changed

+57
-35
lines changed

mysql-test/suite/galera/r/galera_sequences.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ t CREATE TABLE `t` (
206206
) ENGINE=InnoDB SEQUENCE=1
207207
connection node_1;
208208
DROP SEQUENCE t;
209-
CREATE SEQUENCE t INCREMENT BY 1 NOCACHE ENGINE=INNODB;
209+
CREATE SEQUENCE t INCREMENT BY 0 CACHE 1 ENGINE=INNODB;
210210
CREATE TABLE t1(a int not null primary key default nextval(t), b int) engine=innodb;
211211
connection node_2;
212212
# Wait DDL to replicate

mysql-test/suite/galera/r/galera_sequences_transaction.result

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ LASTVAL(s)
7272
connection node_1a;
7373
SELECT LASTVAL(s);
7474
LASTVAL(s)
75-
79
75+
81
7676
connection node_1;
7777
SELECT * FROM t1;
7878
f1 f2
@@ -106,7 +106,6 @@ f1 f2
106106
56 1
107107
58 1
108108
60 1
109-
61 1
110109
63 1
111110
65 1
112111
67 1
@@ -116,6 +115,7 @@ f1 f2
116115
75 1
117116
77 1
118117
79 1
118+
81 1
119119
connection node_2;
120120
SELECT * FROM t1;
121121
f1 f2
@@ -149,7 +149,6 @@ f1 f2
149149
56 1
150150
58 1
151151
60 1
152-
61 1
153152
63 1
154153
65 1
155154
67 1
@@ -159,6 +158,7 @@ f1 f2
159158
75 1
160159
77 1
161160
79 1
161+
81 1
162162
connection node_1;
163163
DROP TABLE t1;
164164
DROP SEQUENCE s;
@@ -299,10 +299,8 @@ connection node_1a;
299299
ROLLBACK;
300300
connection node_2;
301301
COMMIT;
302-
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
303302
connection node_2a;
304303
ROLLBACK;
305-
ERROR 40001: Deadlock found when trying to get lock; try restarting transaction
306304
connection node_2;
307305
SELECT LASTVAL(s);
308306
LASTVAL(s)
@@ -332,6 +330,16 @@ f1 f2
332330
15 1
333331
17 1
334332
19 1
333+
22 1
334+
24 1
335+
26 1
336+
28 1
337+
30 1
338+
32 1
339+
34 1
340+
36 1
341+
38 1
342+
40 1
335343
connection node_2;
336344
SELECT * FROM t1;
337345
f1 f2
@@ -345,6 +353,16 @@ f1 f2
345353
15 1
346354
17 1
347355
19 1
356+
22 1
357+
24 1
358+
26 1
359+
28 1
360+
30 1
361+
32 1
362+
34 1
363+
36 1
364+
38 1
365+
40 1
348366
connection node_1;
349367
DROP TABLE t1;
350368
DROP SEQUENCE s;

mysql-test/suite/galera/r/mdev-22063.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SELECT * FROM t1;
2222
a
2323
SELECT * FROM s;
2424
next_not_cached_value minimum_value maximum_value start_value increment cache_size cycle_option cycle_count
25-
1 1 9223372036854775806 1 1 1000 0 0
25+
1 1 9223372036854775806 1 1 0 0 0
2626
connection node_1;
2727
SET GLOBAL WSREP_MODE='REPLICATE_ARIA,REPLICATE_MYISAM';
2828
DROP TABLE t1;

mysql-test/suite/galera/t/galera_sequences.test

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ SHOW CREATE TABLE t;
163163

164164
--connection node_1
165165
DROP SEQUENCE t;
166-
CREATE SEQUENCE t INCREMENT BY 1 NOCACHE ENGINE=INNODB;
166+
# Don't use NOCACHE as it may produce duplicate entries from multiple nodes
167+
CREATE SEQUENCE t INCREMENT BY 0 CACHE 1 ENGINE=INNODB;
167168
CREATE TABLE t1(a int not null primary key default nextval(t), b int) engine=innodb;
168169

169170
--connection node_2
@@ -193,15 +194,11 @@ SET SESSION wsrep_sync_wait=0;
193194
while ($count)
194195
{
195196
--connection node_1
196-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
197197
INSERT INTO t1(b) values (1);
198198
--connection node_2
199-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
200199
INSERT INTO t1(b) values (2);
201-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
202200
INSERT INTO t1(b) values (2);
203201
--connection node_1
204-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
205202
INSERT INTO t1(b) values (1);
206203
--dec $count
207204
}
@@ -251,15 +248,11 @@ SET SESSION wsrep_sync_wait=0;
251248
while ($count)
252249
{
253250
--connection node_1
254-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
255251
INSERT INTO t1(b) values (1),(2),(3),(4),(5),(6),(7),(8),(9);
256252
--connection node_2
257-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
258253
INSERT INTO t1(b) values (21),(22),(23),(24),(25),(26),(27),(28),(29);
259-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
260254
INSERT INTO t1(b) values (21),(22),(23),(24),(25),(26),(27),(28),(29);
261255
--connection node_1
262-
--error 0,ER_LOCK_WAIT_TIMEOUT,ER_LOCK_DEADLOCK
263256
INSERT INTO t1(b) values (1),(2),(3),(4),(5),(6),(7),(8),(9);
264257
--dec $count
265258
}

mysql-test/suite/galera/t/galera_sequences_transaction.test

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,8 @@ COMMIT;
230230
--connection node_1a
231231
ROLLBACK;
232232
--connection node_2
233-
--error ER_LOCK_DEADLOCK
234233
COMMIT;
235234
--connection node_2a
236-
--error ER_LOCK_DEADLOCK
237235
ROLLBACK;
238236

239237
--connection node_2

sql/log_event_server.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7928,15 +7928,21 @@ int Rows_log_event::update_sequence()
79287928
bool old_master= false;
79297929
int err= 0;
79307930

7931-
if (!bitmap_is_set(table->rpl_write_set, MIN_VALUE_FIELD_NO) ||
7932-
(
7933-
#if defined(WITH_WSREP)
7934-
! WSREP(thd) &&
7931+
rpl_group_info *table_rgi=
7932+
#ifdef WITH_WSREP
7933+
WSREP(thd) ? thd->wsrep_rgi :
79357934
#endif
7936-
table->in_use->rgi_slave &&
7937-
!(table->in_use->rgi_slave->gtid_ev_flags2 & Gtid_log_event::FL_DDL) &&
7935+
table->in_use->rgi_slave;
7936+
rpl_group_info *thd_rgi=
7937+
#ifdef WITH_WSREP
7938+
WSREP(thd) ? thd->wsrep_rgi :
7939+
#endif
7940+
thd->rgi_slave;
7941+
if (!bitmap_is_set(table->rpl_write_set, MIN_VALUE_FIELD_NO) ||
7942+
(table_rgi &&
7943+
!(table_rgi->gtid_ev_flags2 & Gtid_log_event::FL_DDL) &&
79387944
!(old_master=
7939-
rpl_master_has_bug(thd->rgi_slave->rli,
7945+
rpl_master_has_bug(thd_rgi->rli,
79407946
29621, FALSE, FALSE, FALSE, TRUE))))
79417947
{
79427948
/* This event come from a setval function executed on the master.

storage/innobase/handler/ha_innodb.cc

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8473,12 +8473,15 @@ wsrep_calc_row_hash(
84738473
return(0);
84748474
}
84758475

8476-
/** Append table-level exclusive key.
8476+
/** Append table-level exclusive/shared key.
84778477
@param thd MySQL thread handle
84788478
@param table table
8479+
@param exclusive Exclusive not shared certification key.
84798480
@retval false on success
84808481
@retval true on failure */
8481-
ATTRIBUTE_COLD bool wsrep_append_table_key(MYSQL_THD thd, const dict_table_t &table)
8482+
ATTRIBUTE_COLD bool wsrep_append_table_key(MYSQL_THD thd,
8483+
const dict_table_t &table,
8484+
bool exclusive)
84828485
{
84838486
char db_buf[NAME_LEN + 1];
84848487
char tbl_buf[NAME_LEN + 1];
@@ -8491,9 +8494,11 @@ ATTRIBUTE_COLD bool wsrep_append_table_key(MYSQL_THD thd, const dict_table_t &ta
84918494
return true;
84928495
}
84938496

8494-
/* Append table-level exclusive key */
8495-
const int rcode = wsrep_thd_append_table_key(thd, db_buf,
8496-
tbl_buf, WSREP_SERVICE_KEY_EXCLUSIVE);
8497+
/* Append table-level key */
8498+
const enum Wsrep_service_key_type key_type = exclusive
8499+
? WSREP_SERVICE_KEY_EXCLUSIVE
8500+
: WSREP_SERVICE_KEY_SHARED;
8501+
const int rcode = wsrep_thd_append_table_key(thd, db_buf, tbl_buf, key_type);
84978502
if (rcode)
84988503
{
84998504
WSREP_ERROR("Appending table key failed: %s, %d",
@@ -8656,10 +8661,10 @@ ha_innobase::update_row(
86568661
&& (thd_sql_command(m_user_thd) != SQLCOM_LOAD ||
86578662
thd_binlog_format(m_user_thd) == BINLOG_FORMAT_ROW)) {
86588663

8659-
/* We use table-level exclusive key for SEQUENCES
8664+
/* We use table-level shared key for SEQUENCES
86608665
and normal key append for others. */
86618666
if (table->s->table_type == TABLE_TYPE_SEQUENCE) {
8662-
if (wsrep_append_table_key(m_user_thd, *m_prebuilt->table))
8667+
if (wsrep_append_table_key(m_user_thd, *m_prebuilt->table, false))
86638668
DBUG_RETURN(HA_ERR_INTERNAL_ERROR);
86648669
} else if (wsrep_append_keys(m_user_thd,
86658670
wsrep_protocol_version >= 4

storage/innobase/include/ha_prototypes.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,15 @@ char *dict_table_lookup(LEX_CSTRING db, LEX_CSTRING name,
406406
dict_table_t **table, mem_heap_t *heap) noexcept;
407407

408408
#ifdef WITH_WSREP
409-
/** Append table-level exclusive key.
409+
/** Append table-level exclusive/shared key.
410410
@param thd MySQL thread handle
411411
@param table table
412+
@param exclusive Exclusive not shared certification key.
412413
@retval false on success
413414
@retval true on failure */
414415
struct dict_table_t;
415-
bool wsrep_append_table_key(MYSQL_THD thd, const dict_table_t &table);
416+
bool wsrep_append_table_key(MYSQL_THD thd, const dict_table_t &table,
417+
bool exclusive);
416418
#endif /* WITH_WSREP */
417419

418420
#endif /* !UNIV_INNOCHECKSUM */

storage/innobase/row/row0ins.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,7 @@ row_ins_clust_index_entry_low(
27982798
}
27992799
#ifdef WITH_WSREP
28002800
if (trx->is_wsrep() &&
2801-
wsrep_append_table_key(trx->mysql_thd, *index->table))
2801+
wsrep_append_table_key(trx->mysql_thd, *index->table, true))
28022802
{
28032803
trx->error_state = DB_ROLLBACK;
28042804
goto err_exit;

0 commit comments

Comments
 (0)