Skip to content

Commit 6dbc48c

Browse files
committed
MDEV-7324 - Lock-free hash for table definition cache
1 parent 8883c54 commit 6dbc48c

22 files changed

+889
-751
lines changed

include/lf.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@ void lf_hash_init(LF_HASH *hash, uint element_size, uint flags,
231231
void lf_hash_destroy(LF_HASH *hash);
232232
int lf_hash_insert(LF_HASH *hash, LF_PINS *pins, const void *data);
233233
void *lf_hash_search(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen);
234+
void *lf_hash_search_using_hash_value(LF_HASH *hash, LF_PINS *pins,
235+
my_hash_value_type hash_value,
236+
const void *key, uint keylen);
234237
int lf_hash_delete(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen);
235238
int lf_hash_iterate(LF_HASH *hash, LF_PINS *pins,
236239
my_hash_walk_action action, void *argument);

mysql-test/r/ps.result

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,9 +2119,9 @@ select Host, Db from mysql.host limit 0;
21192119
Host Db
21202120
show open tables from mysql;
21212121
Database Table In_use Name_locked
2122-
mysql user 0 0
21232122
mysql general_log 0 0
21242123
mysql host 0 0
2124+
mysql user 0 0
21252125
call proc_1();
21262126
show open tables from mysql;
21272127
Database Table In_use Name_locked
@@ -2132,9 +2132,9 @@ select Host, Db from mysql.host limit 0;
21322132
Host Db
21332133
show open tables from mysql;
21342134
Database Table In_use Name_locked
2135-
mysql user 0 0
21362135
mysql general_log 0 0
21372136
mysql host 0 0
2137+
mysql user 0 0
21382138
call proc_1();
21392139
show open tables from mysql;
21402140
Database Table In_use Name_locked
@@ -2145,9 +2145,9 @@ select Host, Db from mysql.host limit 0;
21452145
Host Db
21462146
show open tables from mysql;
21472147
Database Table In_use Name_locked
2148-
mysql user 0 0
21492148
mysql general_log 0 0
21502149
mysql host 0 0
2150+
mysql user 0 0
21512151
call proc_1();
21522152
show open tables from mysql;
21532153
Database Table In_use Name_locked
@@ -2158,9 +2158,9 @@ select Host, Db from mysql.host limit 0;
21582158
Host Db
21592159
show open tables from mysql;
21602160
Database Table In_use Name_locked
2161-
mysql user 0 0
21622161
mysql general_log 0 0
21632162
mysql host 0 0
2163+
mysql user 0 0
21642164
flush tables;
21652165
create function func_1() returns int begin flush tables; return 1; end|
21662166
ERROR 0A000: FLUSH is not allowed in stored function or trigger
@@ -2176,9 +2176,9 @@ select Host, Db from mysql.host limit 0;
21762176
Host Db
21772177
show open tables from mysql;
21782178
Database Table In_use Name_locked
2179-
mysql user 0 0
21802179
mysql general_log 0 0
21812180
mysql host 0 0
2181+
mysql user 0 0
21822182
prepare abc from "flush tables";
21832183
execute abc;
21842184
show open tables from mysql;
@@ -2190,9 +2190,9 @@ select Host, Db from mysql.host limit 0;
21902190
Host Db
21912191
show open tables from mysql;
21922192
Database Table In_use Name_locked
2193-
mysql user 0 0
21942193
mysql general_log 0 0
21952194
mysql host 0 0
2195+
mysql user 0 0
21962196
execute abc;
21972197
show open tables from mysql;
21982198
Database Table In_use Name_locked
@@ -2203,9 +2203,9 @@ select Host, Db from mysql.host limit 0;
22032203
Host Db
22042204
show open tables from mysql;
22052205
Database Table In_use Name_locked
2206-
mysql user 0 0
22072206
mysql general_log 0 0
22082207
mysql host 0 0
2208+
mysql user 0 0
22092209
execute abc;
22102210
show open tables from mysql;
22112211
Database Table In_use Name_locked
@@ -2216,9 +2216,9 @@ select Host, Db from mysql.host limit 0;
22162216
Host Db
22172217
show open tables from mysql;
22182218
Database Table In_use Name_locked
2219-
mysql user 0 0
22202219
mysql general_log 0 0
22212220
mysql host 0 0
2221+
mysql user 0 0
22222222
flush tables;
22232223
deallocate prepare abc;
22242224
create procedure proc_1() flush logs;

mysql-test/r/show_check.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ create table t1(n int);
259259
insert into t1 values (1);
260260
show open tables;
261261
Database Table In_use Name_locked
262-
test t1 0 0
263262
mysql general_log 0 0
263+
test t1 0 0
264264
drop table t1;
265265
create table t1 (a int not null, b VARCHAR(10), INDEX (b) ) AVG_ROW_LENGTH=10 CHECKSUM=1 COMMENT="test" ENGINE=MYISAM MIN_ROWS=10 MAX_ROWS=100 PACK_KEYS=1 DELAY_KEY_WRITE=1 ROW_FORMAT=fixed;
266266
show create table t1;

mysql-test/suite/perfschema/r/dml_setup_instruments.result

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ wait/synch/rwlock/sql/LOCK_grant YES YES
2424
wait/synch/rwlock/sql/LOCK_system_variables_hash YES YES
2525
wait/synch/rwlock/sql/LOCK_sys_init_connect YES YES
2626
wait/synch/rwlock/sql/LOCK_sys_init_slave YES YES
27-
wait/synch/rwlock/sql/LOCK_tdc YES YES
2827
wait/synch/rwlock/sql/LOGGER::LOCK_logger YES YES
2928
wait/synch/rwlock/sql/MDL_context::LOCK_waiting_for YES YES
3029
wait/synch/rwlock/sql/MDL_lock::rwlock YES YES
3130
wait/synch/rwlock/sql/Query_cache_query::lock YES YES
31+
wait/synch/rwlock/sql/THR_LOCK_servers YES YES
3232
select * from performance_schema.setup_instruments
3333
where name like 'Wait/Synch/Cond/sql/%'
3434
and name not in (

mysql-test/suite/perfschema/r/func_mutex.result

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ WHERE name LIKE 'wait/synch/mutex/%'
55
truncate table performance_schema.events_statements_summary_by_digest;
66
flush status;
77
select NAME from performance_schema.mutex_instances
8-
where NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share' GROUP BY NAME;
8+
where NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex' GROUP BY NAME;
99
NAME
10-
wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share
10+
wait/synch/mutex/mysys/THR_LOCK::mutex
1111
select NAME from performance_schema.rwlock_instances
1212
where NAME = 'wait/synch/rwlock/sql/LOCK_grant';
1313
NAME
@@ -24,7 +24,7 @@ id b
2424
1 initial value
2525
SET @before_count = (SELECT SUM(TIMER_WAIT)
2626
FROM performance_schema.events_waits_history_long
27-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
27+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
2828
SELECT * FROM t1;
2929
id b
3030
1 initial value
@@ -37,12 +37,12 @@ id b
3737
8 initial value
3838
SET @after_count = (SELECT SUM(TIMER_WAIT)
3939
FROM performance_schema.events_waits_history_long
40-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
40+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
4141
SELECT IF((@after_count - @before_count) > 0, 'Success', 'Failure') test_fm1_timed;
4242
test_fm1_timed
4343
Success
4444
UPDATE performance_schema.setup_instruments SET enabled = 'NO'
45-
WHERE NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share';
45+
WHERE NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex';
4646
TRUNCATE TABLE performance_schema.events_waits_history_long;
4747
TRUNCATE TABLE performance_schema.events_waits_history;
4848
TRUNCATE TABLE performance_schema.events_waits_current;
@@ -51,7 +51,7 @@ id b
5151
1 initial value
5252
SET @before_count = (SELECT SUM(TIMER_WAIT)
5353
FROM performance_schema.events_waits_history_long
54-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
54+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
5555
SELECT * FROM t1;
5656
id b
5757
1 initial value
@@ -64,7 +64,7 @@ id b
6464
8 initial value
6565
SET @after_count = (SELECT SUM(TIMER_WAIT)
6666
FROM performance_schema.events_waits_history_long
67-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
67+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
6868
SELECT IF((COALESCE(@after_count, 0) - COALESCE(@before_count, 0)) = 0, 'Success', 'Failure') test_fm2_timed;
6969
test_fm2_timed
7070
Success

mysql-test/suite/perfschema/t/func_mutex.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ flush status;
1919

2020
# Make sure objects are instrumented
2121
select NAME from performance_schema.mutex_instances
22-
where NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share' GROUP BY NAME;
22+
where NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex' GROUP BY NAME;
2323
select NAME from performance_schema.rwlock_instances
2424
where NAME = 'wait/synch/rwlock/sql/LOCK_grant';
2525

@@ -49,18 +49,18 @@ SELECT * FROM t1 WHERE id = 1;
4949

5050
SET @before_count = (SELECT SUM(TIMER_WAIT)
5151
FROM performance_schema.events_waits_history_long
52-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
52+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
5353

5454
SELECT * FROM t1;
5555

5656
SET @after_count = (SELECT SUM(TIMER_WAIT)
5757
FROM performance_schema.events_waits_history_long
58-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
58+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
5959

6060
SELECT IF((@after_count - @before_count) > 0, 'Success', 'Failure') test_fm1_timed;
6161

6262
UPDATE performance_schema.setup_instruments SET enabled = 'NO'
63-
WHERE NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share';
63+
WHERE NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex';
6464

6565
TRUNCATE TABLE performance_schema.events_waits_history_long;
6666
TRUNCATE TABLE performance_schema.events_waits_history;
@@ -70,13 +70,13 @@ SELECT * FROM t1 WHERE id = 1;
7070

7171
SET @before_count = (SELECT SUM(TIMER_WAIT)
7272
FROM performance_schema.events_waits_history_long
73-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
73+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
7474

7575
SELECT * FROM t1;
7676

7777
SET @after_count = (SELECT SUM(TIMER_WAIT)
7878
FROM performance_schema.events_waits_history_long
79-
WHERE (EVENT_NAME = 'wait/synch/mutex/sql/TABLE_SHARE::tdc.LOCK_table_share'));
79+
WHERE (EVENT_NAME = 'wait/synch/mutex/mysys/THR_LOCK::mutex'));
8080

8181
SELECT IF((COALESCE(@after_count, 0) - COALESCE(@before_count, 0)) = 0, 'Success', 'Failure') test_fm2_timed;
8282

mysql-test/t/ps.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,24 +2224,32 @@ deallocate prepare abc;
22242224

22252225
create procedure proc_1() flush tables;
22262226
flush tables;
2227+
--sorted_result
22272228
show open tables from mysql;
22282229
select Host, User from mysql.user limit 0;
22292230
select Host, Db from mysql.host limit 0;
2231+
--sorted_result
22302232
show open tables from mysql;
22312233
call proc_1();
2234+
--sorted_result
22322235
show open tables from mysql;
22332236
select Host, User from mysql.user limit 0;
22342237
select Host, Db from mysql.host limit 0;
2238+
--sorted_result
22352239
show open tables from mysql;
22362240
call proc_1();
2241+
--sorted_result
22372242
show open tables from mysql;
22382243
select Host, User from mysql.user limit 0;
22392244
select Host, Db from mysql.host limit 0;
2245+
--sorted_result
22402246
show open tables from mysql;
22412247
call proc_1();
2248+
--sorted_result
22422249
show open tables from mysql;
22432250
select Host, User from mysql.user limit 0;
22442251
select Host, Db from mysql.host limit 0;
2252+
--sorted_result
22452253
show open tables from mysql;
22462254
flush tables;
22472255
delimiter |;
@@ -2261,24 +2269,31 @@ drop procedure proc_1;
22612269
flush tables;
22622270
select Host, User from mysql.user limit 0;
22632271
select Host, Db from mysql.host limit 0;
2272+
--sorted_result
22642273
show open tables from mysql;
22652274
--enable_ps_protocol
22662275

22672276
prepare abc from "flush tables";
22682277
execute abc;
2278+
--sorted_result
22692279
show open tables from mysql;
22702280
select Host, User from mysql.user limit 0;
22712281
select Host, Db from mysql.host limit 0;
2282+
--sorted_result
22722283
show open tables from mysql;
22732284
execute abc;
2285+
--sorted_result
22742286
show open tables from mysql;
22752287
select Host, User from mysql.user limit 0;
22762288
select Host, Db from mysql.host limit 0;
2289+
--sorted_result
22772290
show open tables from mysql;
22782291
execute abc;
2292+
--sorted_result
22792293
show open tables from mysql;
22802294
select Host, User from mysql.user limit 0;
22812295
select Host, Db from mysql.host limit 0;
2296+
--sorted_result
22822297
show open tables from mysql;
22832298
flush tables;
22842299
deallocate prepare abc;

mysql-test/t/show_check.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ show create table t1;
135135
drop table t1;
136136

137137
flush tables;
138+
--sorted_result
138139
show open tables;
139140
create table t1(n int);
140141
insert into t1 values (1);
142+
--sorted_result
141143
show open tables;
142144
drop table t1;
143145

@@ -617,6 +619,7 @@ show databases;
617619
show tables;
618620
show events;
619621
show table status;
622+
--sorted_result
620623
show open tables;
621624
show plugins;
622625
show columns in t1;

mysys/lf_hash.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static int lfind(LF_SLIST * volatile *head, CHARSET_INFO *cs, uint32 hashnr,
122122
{
123123
if (unlikely(callback))
124124
{
125-
if (callback(cursor->curr + 1, (void*)key))
125+
if (cur_hashnr & 1 && callback(cursor->curr + 1, (void*)key))
126126
return 1;
127127
}
128128
else if (cur_hashnr >= hashnr)
@@ -467,12 +467,13 @@ int lf_hash_delete(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen)
467467
NOTE
468468
see lsearch() for pin usage notes
469469
*/
470-
void *lf_hash_search(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen)
470+
void *lf_hash_search_using_hash_value(LF_HASH *hash, LF_PINS *pins,
471+
my_hash_value_type hashnr,
472+
const void *key, uint keylen)
471473
{
472474
LF_SLIST * volatile *el, *found;
473-
uint bucket, hashnr= calc_hash(hash, (uchar *)key, keylen);
475+
uint bucket= hashnr % hash->size;
474476

475-
bucket= hashnr % hash->size;
476477
lf_rwlock_by_pins(pins);
477478
el= _lf_dynarray_lvalue(&hash->array, bucket);
478479
if (unlikely(!el))
@@ -521,6 +522,13 @@ int lf_hash_iterate(LF_HASH *hash, LF_PINS *pins,
521522
return res;
522523
}
523524

525+
void *lf_hash_search(LF_HASH *hash, LF_PINS *pins, const void *key, uint keylen)
526+
{
527+
return lf_hash_search_using_hash_value(hash, pins,
528+
calc_hash(hash, (uchar*) key, keylen),
529+
key, keylen);
530+
}
531+
524532
static const uchar *dummy_key= (uchar*)"";
525533

526534
/*

sql/handler.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5001,12 +5001,12 @@ bool ha_table_exists(THD *thd, const char *db, const char *table_name,
50015001
else if (engines_with_discover)
50025002
hton= &dummy;
50035003

5004-
TABLE_SHARE *share= tdc_lock_share(db, table_name);
5005-
if (share)
5004+
TDC_element *element= tdc_lock_share(thd, db, table_name);
5005+
if (element && element != MY_ERRPTR)
50065006
{
50075007
if (hton)
5008-
*hton= share->db_type();
5009-
tdc_unlock_share(share);
5008+
*hton= element->share->db_type();
5009+
tdc_unlock_share(element);
50105010
DBUG_RETURN(TRUE);
50115011
}
50125012

sql/mysqld.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4789,7 +4789,8 @@ static int init_server_components()
47894789
all things are initialized so that unireg_abort() doesn't fail
47904790
*/
47914791
mdl_init();
4792-
if (tdc_init() | hostname_cache_init())
4792+
tdc_init();
4793+
if (hostname_cache_init())
47934794
unireg_abort(1);
47944795

47954796
query_cache_set_min_res_unit(query_cache_min_res_unit);

0 commit comments

Comments
 (0)