summaryrefslogtreecommitdiff
path: root/mysql-test/suite/perfschema/t/mdl_func.test
blob: 7aad37ea4d4a3582c772db38903cbc3560dc526e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
--source include/not_embedded.inc
--source include/have_perfschema.inc
# This test fails very frequently on a Windows builder.
--source include/not_windows.inc

UPDATE performance_schema.setup_instruments SET enabled = 'NO', timed = 'YES';

UPDATE performance_schema.setup_instruments SET enabled = 'YES'
WHERE name in ('wait/io/table/sql/handler',
               'wait/lock/table/sql/handler',
               'wait/lock/metadata/sql/mdl');

set @orig_sql_mode= @@sql_mode;
set sql_mode= (select replace(@@sql_mode,'NO_AUTO_CREATE_USER',''));
grant ALL on *.* to user1@localhost;
grant ALL on *.* to user2@localhost;
set sql_mode= @orig_sql_mode;

insert into performance_schema.setup_objects (object_type, object_schema, object_name, enabled, timed)
  values ('TABLE', 'mtr', '%', 'NO', 'NO');

--echo # Switch to (con1, localhost, user1, , )
connect (con1, localhost, user1, , );

let $user1_tid=`select THREAD_ID from performance_schema.threads where PROCESSLIST_ID=connection_id()`;

--echo # Switch to (con2, localhost, user2, , )
connect (con2, localhost, user2, , );

let $user2_tid=`select THREAD_ID from performance_schema.threads where PROCESSLIST_ID=connection_id()`;

--connection default

--disable_query_log
--eval set @user1_tid= $user1_tid;
--eval set @user2_tid= $user2_tid;
--enable_query_log

--disable_warnings
drop function if exists thread_id_name;
--enable_warnings

--delimiter //

create function thread_id_name(THREAD_ID int)
returns varchar(16)
begin
  if (THREAD_ID IS NULL)
  then
    return NULL;
  end if;
  if (THREAD_ID = @user1_tid)
  then
    return "USER1";
  end if;
  if (THREAD_ID = @user2_tid)
  then
    return "USER2";
  end if;
  return "OTHER";
end;
//

delimiter ;//

select thread_id_name(NULL);
select thread_id_name(@user1_tid);
select thread_id_name(@user2_tid);

prepare dump_metadata_locks from
  "select OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME,
    LOCK_TYPE, LOCK_DURATION, LOCK_STATUS,
    thread_id_name(OWNER_THREAD_ID) as OWNER_THREAD_ID
    from performance_schema.metadata_locks
    order by OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME,
    LOCK_TYPE, LOCK_DURATION, LOCK_STATUS, OWNER_THREAD_ID;";

prepare dump_table_handles from
  "select OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME,
    INTERNAL_LOCK, EXTERNAL_LOCK,
    thread_id_name(OWNER_THREAD_ID) as OWNER_THREAD_ID
    from performance_schema.table_handles
    order by OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME,
    INTERNAL_LOCK, EXTERNAL_LOCK, OWNER_THREAD_ID;";

prepare dump_waits_current from
  "select thread_id_name(THREAD_ID) as THREAD_ID,
    EVENT_NAME,
    TIMER_START is not NULL as TIMER_START_SET,
    TIMER_END is not NULL as TIMER_END_SET,
    OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME,
    OPERATION
    from performance_schema.events_waits_current
    where event_name = \'wait/lock/metadata/sql/mdl\';";

prepare dump_waits_history_long from
  "select thread_id_name(THREAD_ID) as THREAD_ID,
    EVENT_NAME,
    TIMER_START is not NULL as TIMER_START_SET,
    TIMER_END is not NULL as TIMER_END_SET,
    OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, INDEX_NAME,
    OPERATION
    from performance_schema.events_waits_history_long
    where event_name = \'wait/lock/metadata/sql/mdl\';";

# reset lost counters
truncate table performance_schema.events_statements_summary_by_digest;
flush status;

flush tables;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (id INT PRIMARY KEY, b CHAR(100) DEFAULT 'initial value');

INSERT INTO t1 (id) VALUES (1), (2), (3), (4), (5), (6), (7), (8);

TRUNCATE TABLE performance_schema.events_waits_history_long;
TRUNCATE TABLE performance_schema.events_waits_history;
TRUNCATE TABLE performance_schema.events_waits_current;

--connection con1

BEGIN;

# MDL lock granted immediately, no wait.
SELECT * from t1 where id=1;

--connection default

echo "---- Marker 1 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

# The MDL lock is now released
COMMIT;

--connection default

echo "---- Marker 2 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

BEGIN;

# MDL lock is granted immediately, no wait
UPDATE t1 set b="new value" where id=2;

--connection default

echo "---- Marker 3 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con2

BEGIN;

# MDL lock is PENDING : waiting for the update transaction
--send DROP TABLE t1;

--connection default

# Wait for the DROP TABLE t1 to block
let $wait_condition=
  select count(*) = 1 from performance_schema.events_waits_current
  where event_name = 'wait/lock/metadata/sql/mdl'
  and object_name = 't1';
--source include/wait_condition.inc

echo "---- Marker 4 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

# MDL lock on the update is released,
# MDL pending lock is granted for the drop table, then released.
COMMIT;

--connection con2

# complete DROP TABLE t1
--reap

--connection default

echo "---- Marker 5 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection default

TRUNCATE TABLE performance_schema.events_waits_history_long;
TRUNCATE TABLE performance_schema.events_waits_history;
TRUNCATE TABLE performance_schema.events_waits_current;
create table t1 (a int);

--connection con1

# Get MDL read lock
LOCK TABLE t1 READ;

--connection default

echo "---- Marker 6 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con2

# User1 has a READ LOCK
# User2 waiting for WRITE LOCK
--send LOCK TABLE t1 write;

--connection default

# Wait for the LOCK TABLE t1 write to block
let $wait_condition=
  select count(*) = 1 from performance_schema.events_waits_current
  where event_name = 'wait/lock/metadata/sql/mdl'
  and object_name = 't1';
--source include/wait_condition.inc

echo "---- Marker 7 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

# User1 releases a READ LOCK
# User2 granted a WRITE LOCK
UNLOCK TABLES;

--connection con2

# Complete LOCK TABLE t1 write
--reap

--connection default

echo "---- Marker 8 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

# User2 granted a WRITE LOCK
# User1 waits for a READ LOCK
--send LOCK TABLES t1 read;

--connection default

# Wait for the LOCK TABLES t1 READ to block
let $wait_condition=
  select count(*) = 1 from performance_schema.events_waits_current
  where event_name = 'wait/lock/metadata/sql/mdl'
  and object_name = 't1';
--source include/wait_condition.inc

echo "---- Marker 9 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con2

UNLOCK TABLES;

--connection con1

# Complete LOCK TABLE t1 read
--reap

--connection default

echo "---- Marker 10 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

UNLOCK TABLES;

--connection default

echo "---- Marker 11 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results


TRUNCATE TABLE performance_schema.events_waits_history_long;
TRUNCATE TABLE performance_schema.events_waits_history;
TRUNCATE TABLE performance_schema.events_waits_current;
--connection con1

BEGIN;

# MDL lock is granted immediately, no wait
UPDATE LOW_PRIORITY t1 SET a=8;

--connection default

echo "---- Marker 12 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1
COMMIT;

--connection default

TRUNCATE TABLE performance_schema.events_waits_history_long;
TRUNCATE TABLE performance_schema.events_waits_history;
TRUNCATE TABLE performance_schema.events_waits_current;

--connection con1

SELECT GET_LOCK('test', 0);

--connection default

echo "---- Marker 13 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con2

--send SELECT GET_LOCK('test', 120);

--connection default

let $wait_condition=
  select count(*) = 1 from performance_schema.events_waits_current
  where event_name = 'wait/lock/metadata/sql/mdl'
  and object_name = 'test';
--source include/wait_condition.inc

echo "---- Marker 14 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con1

SELECT RELEASE_LOCK('test');

--connection con2

--reap

--connection default

echo "---- Marker 15 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

--connection con2

SELECT RELEASE_LOCK('test');

--connection default

echo "---- Marker 16 ----";

--vertical_results
execute dump_metadata_locks;
execute dump_table_handles;
execute dump_waits_current;
execute dump_waits_history_long;
--horizontal_results

# Cleanup

--disconnect con1
--disconnect con2

--connection default

drop table t1;

UPDATE performance_schema.setup_instruments SET enabled = 'YES', timed = 'YES';

revoke all privileges, grant option from user1@localhost;
revoke all privileges, grant option from user2@localhost;

drop user user1@localhost;
drop user user2@localhost;

drop function thread_id_name;

drop prepare dump_metadata_locks;
drop prepare dump_table_handles;
drop prepare dump_waits_current;
drop prepare dump_waits_history_long;

delete from performance_schema.setup_objects
  where object_schema='mtr';

# In case of failure, will indicate the root cause
show global status like "performance_schema%";