mdl_sync.test 106 KB
Newer Older
Konstantin Osipov's avatar
Konstantin Osipov committed
1 2 3 4 5
#
# We need the Debug Sync Facility.
#
--source include/have_debug_sync.inc

6 7 8
# We need InnoDB tables for some of the tests.
--source include/have_innodb.inc

Konstantin Osipov's avatar
Konstantin Osipov committed
9 10 11 12
# Save the initial number of concurrent sessions.
--source include/count_sessions.inc


Konstantin Osipov's avatar
Konstantin Osipov committed
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
# Clean up resources used in this test case.
--disable_warnings
SET DEBUG_SYNC= 'RESET';
--enable_warnings

#
# Test the case of when a exclusive lock request waits for a
# shared lock being upgraded to a exclusive lock.
#

connect (con1,localhost,root,,test,,);
connect (con2,localhost,root,,test,,);
connect (con3,localhost,root,,test,,);

connection default;

--disable_warnings
drop table if exists t1,t2,t3;
--enable_warnings

create table t1 (i int);
create table t2 (i int);

--echo connection: default
lock tables t2 read;

connection con1;
--echo connection: con1
set debug_sync='mdl_upgrade_shared_lock_to_exclusive SIGNAL parked WAIT_FOR go';
--send alter table t1 rename t3

connection default;
--echo connection: default
set debug_sync= 'now WAIT_FOR parked';

connection con2;
--echo connection: con2
50
set debug_sync='mdl_acquire_lock_wait SIGNAL go';
Konstantin Osipov's avatar
Konstantin Osipov committed
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
--send drop table t1,t2

connection con1;
--echo connection: con1
--reap

connection default;
--echo connection: default
unlock tables;

connection con2;
--echo connection: con2
--error ER_BAD_TABLE_ERROR
--reap

connection default;
drop table t3;

disconnect con1;
disconnect con2;
disconnect con3;

# Clean up resources used in this test case.
--disable_warnings
SET DEBUG_SYNC= 'RESET';
--enable_warnings
Konstantin Osipov's avatar
Konstantin Osipov committed
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
--echo #
--echo # Basic test coverage for type-of-operation aware metadata locks.
--echo #
--disable_warnings
drop table if exists t1, t2, t3;
--enable_warnings
connect(mdl_con1,localhost,root,,);
connect(mdl_con2,localhost,root,,);
connect(mdl_con3,localhost,root,,);
connection default;
set debug_sync= 'RESET';
create table t1 (c1 int);

--echo # 
--echo # A) First let us check compatibility rules between differend kinds of
--echo #    type-of-operation aware metadata locks.
--echo #    Of course, these rules are already covered by the tests scattered
--echo #    across the test suite. But it still makes sense to have one place
--echo #    which covers all of them.
--echo #

--echo # 1) Acquire S (simple shared) lock on the table (by using HANDLER):
--echo #
handler t1 open;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open t;
handler t close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1), (1);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail after opening the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that SNRW lock is compatible with S lock.
lock table t1 write;
insert into t1 values (1);
unlock tables;
--echo # Check that X lock is incompatible with S lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock RENAME TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
handler t1 open;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that upgrade from SNW to X is blocked by presence of S lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock ALTER TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
handler t1 open;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that upgrade from SNRW to X is blocked by presence of S lock.
lock table t1 write;
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above upgrade of SNRW to X in ALTER TABLE is blocked
--echo # because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock ALTER TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # 2) Acquire SH (shared high-priority) lock on the table.
--echo #    We have to involve DEBUG_SYNC facility for this as usually
--echo #    such kind of locks are short-lived.
--echo #
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail after opening the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that SNRW lock is compatible with SH lock.
lock table t1 write;
delete from t1 limit 1;
unlock tables;
--echo # Check that X lock is incompatible with SH lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SH lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that upgrade from SNW to X is blocked by presence of SH lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of SH lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t1';
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that upgrade from SNRW to X is blocked by presence of S lock.
lock table t1 write;
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above upgrade of SNRW to X in ALTER TABLE is blocked
--echo # because of S lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo #
--echo # 3) Acquire SR lock on the table.
--echo #
--echo #
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
insert into t1 values (1);
--echo # Check that SNW lock is compatible with it. To do this use ALTER TABLE
--echo # which will fail after opening the table and thus obtaining SNW metadata
--echo # lock.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # Check that SNRW lock is not compatible with SR lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLES.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
delete from t1 limit 1;
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that X lock is incompatible with SR lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that upgrade from SNW to X is blocked by presence of SR lock.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of SR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--reap
--echo # Restore the original state of the things.
alter table t1 drop column c2;
--echo #
--echo # There is no need to check that upgrade from SNRW to X is blocked
--echo # by presence of SR lock because SNRW is incompatible with SR anyway.
--echo # 
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo #
--echo # 4) Acquire SW lock on the table.
--echo #
--echo #
begin;
insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that S, SH, SR and SW locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
465 466 467 468 469
--echo # Disable result log to make test robust against
--echo # effects of concurrent insert.
--disable_result_log
select * from t1;
--enable_result_log
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
insert into t1 values (1);
--echo # Check that SNW lock is not compatible with SW lock.
--echo # Again we use ALTER TABLE which fails after opening
--echo # the table to avoid upgrade of SNW -> X.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above ALTER TABLE is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
begin;
insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that SNRW lock is not compatible with SW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLES.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
delete from t1 limit 2;
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
begin;
insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that X lock is incompatible with SW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE.
--reap
--echo # Restore the original state of the things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of SW lock because SNW/SNRW is incompatible
--echo # with SW anyway.
--echo # 
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo #
--echo # 5) Acquire SNW lock on the table. We have to use DEBUG_SYNC for
--echo #    this, to prevent SNW from being immediately upgraded to X.
--echo #
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that S, SH and SR locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
--echo # Check that SW lock is incompatible with SNW lock.
--echo # Sending:
--send delete from t1 limit 2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "delete from t1 limit 2";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus DELETE.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that SNW lock is incompatible with SNW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTERs.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping first ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping another ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that SNRW lock is incompatible with SNW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above LOCK TABLES is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus LOCK TABLES.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLES
--reap
insert into t1 values (1);
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
set debug_sync= 'now WAIT_FOR locked';
--echo # Check that X lock is incompatible with SNW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME is blocked because of SNW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock ALTER and thus RENAME TABLE.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE
--reap
--echo # Revert back to original state of things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of another SNW lock because SNW/SNRW is
--echo # incompatible with SNW anyway.
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo #
--echo # 6) Acquire SNRW lock on the table. 
--echo #
--echo #
lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that S and SH locks are compatible with it.
handler t1 open;
handler t1 close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that SR lock is incompatible with SNRW lock.
--echo # Sending:
--send select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above SELECT is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock SELECT.
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that SW lock is incompatible with SNRW lock.
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above DELETE is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock DELETE.
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that SNW lock is incompatible with SNRW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above ALTER is blocked because of UNWR lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock ALTER.
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that SNRW lock is incompatible with SNRW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above LOCK TABLES is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock waiting LOCK TABLES.
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLES
--reap
insert into t1 values (1);
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that X lock is incompatible with SNRW lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Check that the above RENAME is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME TABLE
--reap
--echo # Revert back to original state of things.
rename table t2 to t1;
--echo #
--echo # There is no need to check that upgrade from SNW/SNRW to X is
--echo # blocked by presence of another SNRW lock because SNW/SNRW is
--echo # incompatible with SNRW anyway.
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo #
--echo # 7) Now do the same round of tests for X lock. We use additional
--echo #    table to get long-lived lock of this type.
--echo #
create table t2 (c1 int);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Take a lock on t2, so RENAME TABLE t1 TO t2 will get blocked
--echo # after acquiring X lock on t1.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that S lock in incompatible with X lock.
--echo # Sending:
--send handler t1 open;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above HANDLER statement is blocked because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "handler t1 open";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping HANDLER.
--reap
handler t1 close;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SH lock in incompatible with X lock.
--echo # Sending:
--send select column_name from information_schema.columns where table_schema='test' and table_name='t1';
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT ... FROM I_S ... statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info like "select column_name from information_schema.columns%";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping SELECT ... FROM I_S.
--reap
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SR lock in incompatible with X lock.
--echo # Sending:
--send select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SW lock in incompatible with X lock.
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above DELETE statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNW lock is incompatible with X lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNRW lock is incompatible with X lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above LOCK TABLE statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Prepare for blocking RENAME TABLE.
lock tables t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME has acquired X lock on t1 and is waiting for t2.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that X lock is incompatible with X lock.
--echo # Sending:
--send rename table t1 to t3;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above RENAME statement is blocked
--echo # because of X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t3";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping RENAME.
--reap
rename table t3 to t1;

--echo #
--echo # B) Now let us test compatibility in cases when both locks
--echo #    are pending. I.e. let us test rules for priorities between
--echo #    different types of metadata locks.
--echo #

--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo #
--echo # 1) Check compatibility for pending SNW lock.
--echo #
--echo # Acquire SW lock in order to create pending SNW lock later.
begin;
insert into t1 values (1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending SNW lock.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that ALTER TABLE is waiting with pending SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Check that S, SH and SR locks are compatible with pending SNW
handler t1 open t;
handler t close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
select count(*) from t1;
--echo # Check that SW is incompatible with pending SNW
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of pending SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping ALTER.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo #
--echo # We can't do similar check for SNW, SNRW and X locks because
--echo # they will also be blocked by active SW lock.
--echo #
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo #
--echo # 2) Check compatibility for pending SNRW lock.
--echo #
--echo # Acquire SR lock in order to create pending SNRW lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending SNRW lock.
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that S and SH locks are compatible with pending SNRW
handler t1 open t;
handler t close;
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that SR is incompatible with pending SNRW
--echo # Sending:
--send select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT is blocked because of pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # Restore pending SNRW lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that SW is incompatible with pending SNRW
--echo # Sending:
--send insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above INSERT is blocked because of pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock LOCK TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping INSERT.
--reap
--echo # Restore pending SNRW lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that LOCK TABLE is waiting with pending SNRW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that SNW is compatible with pending SNRW
--echo # So ALTER TABLE statements are not starved by LOCK TABLEs.
--error ER_DUP_ENTRY
alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Unblock LOCK TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping LOCK TABLE.
--reap
unlock tables;
--echo #
--echo # We can't do similar check for SNRW and X locks because
--echo # they will also be blocked by active SR lock.
--echo #
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo #
--echo # 3) Check compatibility for pending X lock.
--echo #
--echo # Acquire SR lock in order to create pending X lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SH locks are compatible with pending X
select column_name from information_schema.columns where
  table_schema='test' and table_name='t1';
--echo # Check that S is incompatible with pending X
--echo # Sending:
--send handler t1 open;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above HANDLER OPEN is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "handler t1 open";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping HANDLER t1 OPEN.
--reap
handler t1 close;
--echo # Restore pending X lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SR is incompatible with pending X
--echo # Sending:
--send select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping SELECT.
--reap
--echo # Restore pending X lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SW is incompatible with pending X
--echo # Sending:
--send delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above DELETE is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "delete from t1 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping DELETE.
--reap
--echo # Restore pending X lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNW is incompatible with pending X
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above ALTER TABLE is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # Restore pending X lock.
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
handler t1 open;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Add pending X lock.
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that RENAME TABLE is waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that SNRW is incompatible with pending X
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'mdl_con3'.
connection mdl_con3;
--echo # Check that the above LOCK TABLES is blocked because of pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Unblock RENAME TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reaping RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reaping LOCK TABLES.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;

--echo #
--echo #
--echo # C) Now let us test how type-of-operation locks are handled in
--echo #    transactional context. Obviously we are mostly interested
--echo #    in conflicting types of locks.
--echo #

--echo #
--echo # 1) Let us check how various locks used within transactional
--echo #    context interact with active/pending SNW lock.
--echo #
--echo #    We start with case when we are acquiring lock on the table
--echo #    which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create an active SNW lock on t2.
--echo # We have to use DEBUG_SYNC facility as otherwise SNW lock
--echo # will be immediately released (or upgraded to X lock).
insert into t2 values (1), (1);
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t2 add primary key (c1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'now WAIT_FOR locked';
--echo # SR lock should be acquired without any waiting.
select count(*) from t2;
commit;
--echo # Now let us check that we will wait in case of SW lock.
begin;
select count(*) from t1;
--echo # Sending:
--send insert into t2 values (1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "insert into t2 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE and thus INSERT.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # Now let us see what happens when we are acquiring lock on the table
--echo # which is already used in transaction.
--echo #
--echo # *) First, case when transaction which has SR lock on the table also
--echo #    locked in SNW mode acquires yet another SR lock and then tries
--echo #    to acquire SW lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create an active SNW lock on t1.
set debug_sync= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
set debug_sync= 'now WAIT_FOR locked';
--echo # We should still be able to get SR lock without waiting.
select count(*) from t1;
--echo # Since the above ALTER TABLE is not upgrading SNW lock to X by waiting
--echo # for SW lock we won't create deadlock.
--echo # So the below INSERT should not end-up with ER_LOCK_DEADLOCK error.
--echo # Sending:
--send insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE and thus INSERT.
set debug_sync= 'now SIGNAL finish';
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # **) Now test in which transaction that has SW lock on the table
--echo #     against which there is pending SNW lock acquires SR and SW
--echo #     locks on this table.
--echo #
begin;
insert into t1 values (1);
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create pending SNW lock on t1.
--echo # Sending:
--send alter table t1 add primary key (c1);
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE starts waiting for SNW lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add primary key (c1)";
--source include/wait_condition.inc
--echo # We should still be able to get both SW and SR locks without waiting.
select count(*) from t1;
delete from t1 limit 1;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--error ER_DUP_ENTRY
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # 2) Now similar tests for active SNW lock which is being upgraded
--echo #    to X lock.
--echo #
--echo #    Again we start with case when we are acquiring lock on the
--echo #    table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Start transaction which will prevent SNW -> X upgrade from
--echo # completing immediately.
begin;
select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X on t2.
--echo # Sending:
--send alter table t2 add column c2 int;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t2 add column c2 int";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SR lock on t2 causes waiting.
--echo # Sending:
--send select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap SELECT.
--reap
commit;
--echo # Do similar check for SW lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Start transaction which will prevent SNW -> X upgrade from
--echo # completing immediately.
begin;
select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X on t2.
--echo # Sending:
--send alter table t2 drop column c2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t2 drop column c2";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SW lock on t2 causes waiting.
--echo # Sending:
--send insert into t2 values (1);
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above INSERT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "insert into t2 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap INSERT.
--reap
commit;
--echo #
--echo # Test for the case in which we are acquiring lock on the table
--echo # which is already used in transaction.
--echo #
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create SNW lock pending upgrade to X.
--echo # Sending:
--send alter table t1 add column c2 int;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE starts waiting X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "alter table t1 add column c2 int";
--source include/wait_condition.inc
--echo # Check that transaction is still able to acquire SR lock.
select count(*) from t1;
--echo # Waiting trying to acquire SW lock will cause deadlock and
--echo # therefore should cause an error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock ALTER TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap ALTER TABLE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # 3) Check how various locks used within transactional context
--echo #    interact with active/pending SNRW lock.
--echo # 
--echo #    Once again we start with case when we are acquiring lock on
--echo #    the table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
lock table t2 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Attempt to acquire SR should be blocked. It should
--echo # not cause errors as it does not creates deadlock.
--echo # Sending:
--send select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that the above SELECT is blocked 
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock SELECT.
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap SELECT.
--reap
commit;
--echo # Repeat the same test for SW lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
lock table t2 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Again attempt to acquire SW should be blocked and should
--echo # not cause any errors.
--echo # Sending:
--send delete from t2 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Check that the above DELETE is blocked 
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "delete from t2 limit 1";
--source include/wait_condition.inc
--echo # Unblock DELETE.
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap DELETE.
--reap
commit;
--echo #
--echo # Now coverage for the case in which we are acquiring lock on
--echo # the table which is already used in transaction and against
--echo # which there is a pending SNRW lock request.
--echo #
--echo # *) Let us start with case when transaction has only a SR lock.
--echo #
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until LOCK TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that another instance of SR lock is granted without waiting.
select count(*) from t1;
--echo # Attempt to wait for SW lock will lead to deadlock, thus
--echo # the below statement should end with ER_LOCK_DEADLOCK error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock LOCK TABLES.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap LOCK TABLES.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # **) Now case when transaction has a SW lock.
--echo #
begin;
delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Sending:
--send lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until LOCK TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "lock table t1 write";
--source include/wait_condition.inc
--echo # Check that both SR and SW locks are granted without waiting
--echo # and errors.
select count(*) from t1;
insert into t1 values (1, 1);
--echo # Unblock LOCK TABLES.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap LOCK TABLES.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # 4) Check how various locks used within transactional context
--echo #    interact with active/pending X lock.
--echo # 
--echo #    As usual we start with case when we are acquiring lock on
--echo #    the table which was not used in the transaction before.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Start transaction which will prevent X lock from going away
--echo # immediately.
begin;
select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create pending X lock on t2.
--echo # Sending:
--send rename table t2 to t3;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until RENAME TABLE starts waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t2 to t3";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SR lock on t2 causes waiting.
--echo # Sending:
--send select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above SELECT is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "select count(*) from t2";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap RENAME TABLE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap SELECT.
--error ER_NO_SUCH_TABLE
--reap
commit;
rename table t3 to t2;
--echo # The same test for SW lock.
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Start transaction which will prevent X lock from going away
--echo # immediately.
begin;
select count(*) from t2;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Create pending X lock on t2.
--echo # Sending:
--send rename table t2 to t3;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until RENAME TABLE starts waiting with pending X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t2 to t3";
--source include/wait_condition.inc
--echo # Check that attempt to acquire SW lock on t2 causes waiting.
--echo # Sending:
--send delete from t2 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con2'.
connection mdl_con2;
--echo # Check that the above DELETE is blocked.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "delete from t2 limit 1";
--source include/wait_condition.inc
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap RENAME TABLE.
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap DELETE.
--error ER_NO_SUCH_TABLE
--reap
commit;
rename table t3 to t2;
--echo #
--echo # Coverage for the case in which we are acquiring lock on
--echo # the table which is already used in transaction and against
--echo # which there is a pending X lock request.
--echo #
--echo # *) The first case is when transaction has only a SR lock.
--echo #
begin;
select count(*) from t1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until RENAME TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that another instance of SR lock is granted without waiting.
select count(*) from t1;
--echo # Attempt to wait for SW lock will lead to deadlock, thus
--echo # the below statement should end with ER_LOCK_DEADLOCK error.
--error ER_LOCK_DEADLOCK
delete from t1 limit 1;
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # **) The second case is when transaction has a SW lock.
--echo #
begin;
delete from t1 limit 1;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Sending:
--send rename table t1 to t2;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until RENAME TABLE is blocked creating pending request for X lock.
let $wait_condition=
select count(*) = 1 from information_schema.processlist
where state = "Waiting for table" and info = "rename table t1 to t2";
--source include/wait_condition.inc
--echo # Check that both SR and SW locks are granted without waiting
--echo # and errors.
select count(*) from t1;
insert into t1 values (1, 1);
--echo # Unblock RENAME TABLE.
commit;
--echo # 
--echo # Switching to connection 'mdl_con1'.
connection mdl_con1;
--echo # Reap RENAME TABLE.
--error ER_TABLE_EXISTS_ERROR
--reap
--echo # 
--echo # Switching to connection 'default'.
connection default;

--echo # Clean-up.
disconnect mdl_con1;
disconnect mdl_con2;
disconnect mdl_con3;
set debug_sync= 'RESET';
drop table t1, t2;


--echo #
--echo # Additional coverage for some scenarios in which not quite
--echo # correct use of S metadata locks by HANDLER statement might
--echo # have caused deadlocks.
--echo #
--disable_warnings
drop table if exists t1, t2;
--enable_warnings
connect(handler_con1,localhost,root,,);
connect(handler_con2,localhost,root,,);
connection default;
create table t1 (i int);
create table t2 (j int);
insert into t1 values (1);

--echo #
--echo # First, check scenario in which we upgrade SNRW lock to X lock
--echo # on a table while having HANDLER READ trying to acquire TL_READ
--echo # on the same table.
--echo #
handler t1 open;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
lock table t1 write;
--echo # Upgrade SNRW to X lock.
--echo # Sending:
--send alter table t1 add column j int;
--echo # 
--echo # Switching to connection 'handler_con2'.
connection handler_con2;
--echo # Wait until ALTER is blocked during upgrade.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column j int";
--source include/wait_condition.inc
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # The below statement should not cause deadlock.
--send handler t1 read first;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Reap ALTER TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap HANDLER READ.
--reap
handler t1 close;

--echo #
--echo # Now, check scenario in which upgrade of SNRW lock to X lock
--echo # can be blocked by HANDLER which is open in connection currently
--echo # waiting to get table-lock owned by connection doing upgrade.
--echo #
handler t1 open;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
lock table t1 write, t2 read;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Execute statement which will be blocked on table-level lock
--echo # owned by connection 'handler_con1'.
--echo # Sending:
--send insert into t2 values (1);
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Wait until INSERT is blocked on table-level lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Table lock" and info = "insert into t2 values (1)";
--source include/wait_condition.inc
2284 2285 2286 2287 2288 2289 2290 2291 2292 2293
--echo # Sending 'alter table t1 drop column j'. It should not cause
--echo # deadlock.
send alter table t1 drop column j;
--echo # Switching to connection 'handler_con2'.
connection handler_con2;
--echo # Wait until ALTER is blocked during upgrade.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 drop column j";
--source include/wait_condition.inc
2294 2295 2296 2297
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # Reap INSERT.
2298
--error ER_LOCK_ABORTED
2299 2300 2301
--reap
handler t1 close;
--echo #
2302 2303 2304 2305 2306 2307 2308 2309
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Reaping 'alter table t1 drop column j'
--reap
unlock tables;
--echo # Switching to connection 'default'.
connection default;

2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331
--echo # Then, check the scenario in which upgrade of SNRW lock to X
--echo # lock is blocked by HANDLER which is open in connection currently
--echo # waiting to get SW lock on the same table.
--echo #
handler t1 open;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
lock table t1 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # The below insert should be blocked because active SNRW lock on 't1'.
--echo # Sending:
--send insert into t1 values (1);
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Wait until INSERT is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "insert into t1 values (1)";
2332
--source include/wait_condition.inc
2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
--echo # The below ALTER TABLE will be blocked because of presence of HANDLER.
--echo # Sending:
--send alter table t1 add column j int;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # INSERT should be chosen as victim for resolving deadlock.
--echo # Reaping INSERT.
--error ER_LOCK_DEADLOCK
--reap
--echo # Close HANDLER to unblock ALTER TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Reaping ALTER TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;

--echo #
--echo # Finally, test in which upgrade of SNRW lock to X lock is blocked
--echo # by HANDLER which is open in connection currently waiting to get
--echo # SR lock on the table on which lock is upgraded.
--echo #
handler t1 open;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
lock table t1 write, t2 write;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # The below insert should be blocked because active SNRW lock on 't1'.
--echo # Sending:
--send insert into t2 values (1);
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Wait until INSERT is blocked because of SNRW lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "insert into t2 values (1)";
2378
--source include/wait_condition.inc
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406
--echo # The below ALTER TABLE will be blocked because of presence of HANDLER.
--echo # Sending:
--send alter table t1 drop column j;
--echo # 
--echo # Switching to connection 'default'.
connection default;
--echo # INSERT should be chosen as victim for resolving deadlock.
--echo # Reaping INSERT.
--error ER_LOCK_DEADLOCK
--reap
--echo # Close HANDLER to unblock ALTER TABLE.
handler t1 close;
--echo # 
--echo # Switching to connection 'handler_con1'.
connection handler_con1;
--echo # Reaping ALTER TABLE.
--reap
unlock tables;
--echo # 
--echo # Switching to connection 'default'.
connection default;

--echo # Clean-up.
disconnect handler_con1;
disconnect handler_con2;
drop tables t1, t2;


2407 2408 2409 2410 2411
--echo #
--echo # Test coverage for basic deadlock detection in metadata
--echo # locking subsystem.
--echo #
--disable_warnings
2412
drop tables if exists t0, t1, t2, t3, t4, t5;
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522
--enable_warnings

connect(deadlock_con1,localhost,root,,);
connect(deadlock_con2,localhost,root,,);
connect(deadlock_con3,localhost,root,,);
connection default;
create table t1 (i int);
create table t2 (j int);
create table t3 (k int);
create table t4 (k int);

--echo #
--echo # Test for the case in which no deadlock occurs.
--echo #

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
begin;
insert into t1 values (1);

--echo #
--echo # Switching to connection 'deadlock_con2'.
connection deadlock_con2;
begin;
insert into t2 values (1);

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Send:
--send rename table t2 to t0, t3 to t2, t0 to t3;

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
--echo # for 'deadlock_con2' which holds shared metadata lock on 't2'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t2 to t0, t3 to t2, t0 to t3";
--source include/wait_condition.inc
--echo # The below statement should wait for exclusive metadata lock
--echo # on 't2' to go away and should not produce ER_LOCK_DEADLOCK
--echo # as no deadlock is possible in this situation.
--echo # Send:
--send select * from t2;

--echo #
--echo # Switching to connection 'deadlock_con2'.
connection deadlock_con2;
--echo # Wait until the above SELECT * FROM t2 is starts waiting
--echo # for an exclusive metadata lock to go away.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "select * from t2";
--source include/wait_condition.inc
--echo #
--echo # Unblock RENAME TABLE by releasing shared metadata lock on t2.
commit;

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Reap SELECT.
--reap

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo #
--echo # Let us check that in the process of waiting for conflicting lock
--echo # on table 't2' to go away transaction in connection 'deadlock_con1'
--echo # has not released metadata lock on table 't1'.
--echo # Send:
--send rename table t1 to t0, t3 to t1, t0 to t3;

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
--echo # for 'deadlock_con1' which should still hold shared metadata lock on
--echo # table 't1'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t0, t3 to t1, t0 to t3";
--source include/wait_condition.inc
--echo # Commit transaction to unblock RENAME TABLE.
commit;

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
--echo # Test for case when deadlock occurs and should be detected immediately.
--echo #

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
begin;
2523
insert into t2 values (2);
2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Send:
--send rename table t2 to t0, t1 to t2, t0 to t1;

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Wait until the above RENAME TABLE is blocked because it has to wait
2535
--echo # for 'deadlock_con1' which holds shared metadata lock on 't2'.
2536 2537 2538 2539 2540 2541 2542 2543
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t2 to t0, t1 to t2, t0 to t1";
--source include/wait_condition.inc
--echo # 
--echo # The below statement should not wait as doing so will cause deadlock.
--echo # Instead it should fail and emit ER_LOCK_DEADLOCK statement.
--error ER_LOCK_DEADLOCK
2544
select * from t1;
2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569

--echo #
--echo # Let us check that failure of the above statement has not released
--echo # metadata lock on table 't1', i.e. that RENAME TABLE is still blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t2 to t0, t1 to t2, t0 to t1";
--source include/wait_condition.inc
--echo # Commit transaction to unblock RENAME TABLE.
commit;

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Reap RENAME TABLE.
--reap

--echo #
--echo # Test for the case in which deadlock also occurs but not immediately.
--echo #

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
begin;
2570
insert into t2 values (1);
2571 2572 2573 2574

--echo #
--echo # Switching to connection 'default'.
connection default;
2575
lock table t1 write;
2576 2577 2578 2579 2580

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # The below SELECT statement should wait for metadata lock
2581
--echo # on table 't1' and should not produce ER_LOCK_DEADLOCK
2582
--echo # immediately as no deadlock is possible at the moment.
2583
--send select * from t1;
2584 2585

--echo #
2586 2587 2588 2589
--echo # Switching to connection 'deadlock_con2'.
connection deadlock_con2;
--echo # Wait until the above SELECT * FROM t1 is starts waiting
--echo # for an UNRW metadata lock to go away.
2590 2591
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
2592
  where state = "Waiting for table" and info = "select * from t1";
2593 2594 2595 2596
--source include/wait_condition.inc

--echo # Send RENAME TABLE statement that will deadlock with the
--echo # SELECT statement and thus should abort the latter.
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610
--send rename table t1 to t0, t2 to t1, t0 to t2;

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Wait till above RENAME TABLE is blocked while holding
--echo # pending X lock on t1.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "rename table t1 to t0, t2 to t1, t0 to t2";
--source include/wait_condition.inc
--echo # Allow the above RENAME TABLE to acquire lock on t1 and
--echo # create pending lock on t2 thus creating deadlock.
unlock tables;
2611 2612 2613 2614 2615 2616 2617

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Since the latest RENAME TABLE entered in deadlock with SELECT
--echo # statement the latter should be aborted and emit ER_LOCK_DEADLOCK
--echo # error.
2618
--echo # Reap SELECT * FROM t1.
2619 2620 2621 2622 2623
--error ER_LOCK_DEADLOCK
--reap

--echo #
--echo # Again let us check that failure of the SELECT statement has not
2624
--echo # released metadata lock on table 't2', i.e. that the latest RENAME
2625 2626 2627
--echo # is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
2628
  where state = "Waiting for table" and info = "rename table t1 to t0, t2 to t1, t0 to t2";
2629 2630 2631 2632 2633 2634 2635
--source include/wait_condition.inc
--echo # Commit transaction to unblock this RENAME TABLE.
commit;

--echo #
--echo # Switching to connection 'deadlock_con2'.
connection deadlock_con2;
2636
--echo # Reap RENAME TABLE ... .
2637 2638 2639 2640 2641 2642
--reap;

--echo #
--echo # Switching to connection 'default'.
connection default;

2643 2644 2645 2646 2647 2648 2649
drop tables t1, t2, t3, t4;

--echo #
--echo # Now, test case which shows that deadlock detection empiric
--echo # also takes into account requests for metadata lock upgrade.
--echo #
create table t1 (i int);
2650 2651 2652 2653 2654
insert into t1 values (1);
--echo # Avoid race which occurs when SELECT in 'deadlock_con1' connection
--echo # accesses table before the above INSERT unlocks the table and thus
--echo # its result becomes visible to other connections.
select * from t1;
2655 2656 2657 2658 2659

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
begin;
2660
select * from t1;
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Send:
--send alter table t1 add column j int, rename to t2;

--echo #
--echo # Switching to connection 'deadlock_con1'.
connection deadlock_con1;
--echo # Wait until the above ALTER TABLE ... RENAME acquires exclusive
--echo # metadata lock on 't2' and starts waiting for connection
--echo # 'deadlock_con1' which holds shared lock on 't1'.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column j int, rename to t2";
--source include/wait_condition.inc

--echo # The below statement should not wait as it will cause deadlock.
--echo # An appropriate error should be reported instead.
--error ER_LOCK_DEADLOCK
select * from t2;

--echo # Again let us check that failure of the above statement has not
--echo # released all metadata locks in connection 'deadlock_con1' and
--echo # so ALTER TABLE ... RENAME is still blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column j int, rename to t2";
--source include/wait_condition.inc

--echo # Commit transaction to unblock ALTER TABLE ... RENAME.
commit;

--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Reap ALTER TABLE ... RENAME.
--reap

drop table t2;

disconnect deadlock_con1;
disconnect deadlock_con2;
disconnect deadlock_con3;


Konstantin Osipov's avatar
Konstantin Osipov committed
2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
--echo #
--echo # Test for bug #46748 "Assertion in MDL_context::wait_for_locks()
--echo # on INSERT + CREATE TRIGGER".
--echo #
--disable_warnings
drop tables if exists t1, t2, t3, t4, t5;
--enable_warnings
--echo # Let us simulate scenario in which we open some tables from extended
--echo # part of prelocking set but then encounter conflicting metadata lock,
--echo # so have to back-off and wait for it to go away.
connect (con1root,localhost,root,,test,,);
connect (con2root,localhost,root,,test,,);
connection default;
create table t1 (i int);
create table t2 (j int);
create table t3 (k int);
create table t4 (l int);
create trigger t1_bi before insert on t1 for each row
  insert into t2 values (new.i);
create trigger t2_bi before insert on t2 for each row
  insert into t3 values (new.j);
--echo #
--echo # Switching to connection 'con1root'.
connection con1root;
lock tables t4 read;
--echo #
--echo # Switching to connection 'con2root'.
connection con2root;
--echo # Send :
--send rename table t3 to t5, t4 to t3;
--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Wait until the above RENAME TABLE adds pending requests for exclusive
--echo # metadata lock on its tables and blocks due to 't4' being used by LOCK
--echo # TABLES.
let $wait_condition= select count(*)= 1 from information_schema.processlist
                       where state= 'Waiting for table' and
                             info='rename table t3 to t5, t4 to t3';
--source include/wait_condition.inc
--echo # Send :
--send insert into t1 values (1);
--echo #
--echo # Switching to connection 'con1root'.
connection con1root;
--echo # Wait until INSERT statement waits due to encountering pending
--echo # exclusive metadata lock on 't3'.
let $wait_condition= select count(*)= 1 from information_schema.processlist
                       where state= 'Waiting for table' and
                             info='insert into t1 values (1)';
--source include/wait_condition.inc
unlock tables;
--echo #
--echo # Switching to connection 'con2root'.
connection con2root;
--echo # Reap RENAME TABLE.
--reap
--echo #
--echo # Switching to connection 'default'.
connection default;
--echo # Reap INSERT.
--reap
--echo # Clean-up.
disconnect con1root;
disconnect con2root;
drop tables t1, t2, t3, t5;


2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805
--echo #
--echo # Bug#42546 - Backup: RESTORE fails, thinking it finds an existing table
--echo #

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings
set @save_log_output=@@global.log_output;
set global log_output=file;

connect(con2, localhost, root,,);

--echo #
--echo # Test 1: CREATE TABLE
--echo #

--echo # Connection 2 
connection con2;
--echo # Start insert on the not-yet existing table
--echo # Wait after taking the MDL lock
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send INSERT INTO t1 VALUES(1,"def")

--echo # Connection 1
connection default;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Now INSERT has a MDL on the non-existent table t1.

--echo #
--echo # Continue the INSERT once CREATE waits for exclusive lock
2806
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL finish';
2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845
--echo # Try to create that table.
--send CREATE TABLE t1 (c1 INT, c2 VARCHAR(100), KEY(c1))

--echo # Connection 2
--echo # Insert fails
connection con2;
--error ER_NO_SUCH_TABLE
--reap

--echo # Connection 1
connection default;
--reap;
SET DEBUG_SYNC= 'RESET';
SHOW TABLES;

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

--echo #
--echo # Test 2: CREATE TABLE LIKE
--echo #

CREATE TABLE t2 (c1 INT, c2 VARCHAR(100), KEY(c1));

--echo # Connection 2 
connection con2;
--echo # Start insert on the not-yet existing table
--echo # Wait after taking the MDL
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL locked WAIT_FOR finish';
--send INSERT INTO t1 VALUES(1,"def")

--echo # Connection 1
connection default;
SET DEBUG_SYNC= 'now WAIT_FOR locked';
--echo # Now INSERT has a MDL on the non-existent table t1.

--echo #
--echo # Continue the INSERT once CREATE waits for exclusive lock
2846
SET DEBUG_SYNC= 'mdl_acquire_lock_wait SIGNAL finish';
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870
--echo # Try to create that table.
--send CREATE TABLE t1 LIKE t2

--echo # Connection 2
--echo # Insert fails
connection con2;
--error ER_NO_SUCH_TABLE
--reap

--echo # Connection 1
connection default;
--reap
SET DEBUG_SYNC= 'RESET';
SHOW TABLES;

DROP TABLE t2;
disconnect con2;
--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

set global log_output=@save_log_output;


Konstantin Osipov's avatar
Konstantin Osipov committed
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885
--echo #
--echo # Bug #46044 "MDL deadlock on LOCK TABLE + CREATE TABLE HIGH_PRIORITY
--echo #             FOR UPDATE"
--echo #
--disable_warnings
drop tables if exists t1, t2;
--enable_warnings
connect (con46044, localhost, root,,);
connect (con46044_2, localhost, root,,);
connection default;
create table t1 (i int);

--echo # Let us check that we won't deadlock if during filling
--echo # of I_S table we encounter conflicting metadata lock
--echo # which owner is in its turn waiting for our connection.
2886
lock tables t1 read;
Konstantin Osipov's avatar
Konstantin Osipov committed
2887 2888 2889 2890

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Sending:
2891
--send create table t2 select * from t1 for update;
Konstantin Osipov's avatar
Konstantin Osipov committed
2892 2893 2894 2895 2896 2897

--echo # Switching to connection 'default'.
connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
2898
  where state = "Table lock" and info = "create table t2 select * from t1 for update";
Konstantin Osipov's avatar
Konstantin Osipov committed
2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938
--source include/wait_condition.inc

--echo # First let us check that SHOW FIELDS/DESCRIBE doesn't
--echo # gets blocked and emits and error.
--error ER_WARN_I_S_SKIPPED_TABLE
show fields from t2;

--echo # Now test for I_S query which reads only .FRMs.
--echo #
--echo # Query below should only emit a warning.
select column_name from information_schema.columns
  where table_schema='test' and table_name='t2';

--echo # Finally, test for I_S query which does full-blown table open.
--echo #
--echo # Query below should not be blocked. Warning message should be
--echo # stored in the 'table_comment' column.
select table_name, table_type, auto_increment, table_comment
  from information_schema.tables where table_schema='test' and table_name='t2';

--echo # Switching to connection 'default'.
connection default;
unlock tables;

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap
drop table t2;

--echo #
--echo # Let us also check that queries to I_S wait for conflicting metadata
--echo # locks to go away instead of skipping table with a warning in cases
--echo # when deadlock is not possible. This is a nice thing from compatibility
--echo # and ease of use points of view.
--echo #
--echo # We check same three queries to I_S in this new situation.

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
2939
lock tables t1 read;
Konstantin Osipov's avatar
Konstantin Osipov committed
2940 2941 2942 2943

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Sending:
2944
--send create table t2 select * from t1 for update;
Konstantin Osipov's avatar
Konstantin Osipov committed
2945 2946 2947 2948 2949 2950

--echo # Switching to connection 'default'.
connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
2951
  where state = "Table lock" and info = "create table t2 select * from t1 for update";
Konstantin Osipov's avatar
Konstantin Osipov committed
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980
--source include/wait_condition.inc

--echo # Let us check that SHOW FIELDS/DESCRIBE gets blocked.
--echo # Sending:
--send show fields from t2;

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
--echo # Wait until SHOW FIELDS gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "show fields from t2";
--source include/wait_condition.inc

unlock tables;

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SHOW FIELDS ...
--reap
drop table t2;

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
2981
lock tables t1 read;
Konstantin Osipov's avatar
Konstantin Osipov committed
2982 2983 2984 2985

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Sending:
2986
--send create table t2 select * from t1 for update;
Konstantin Osipov's avatar
Konstantin Osipov committed
2987 2988 2989 2990 2991 2992

--echo # Switching to connection 'default'.
connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
2993
  where state = "Table lock" and info = "create table t2 select * from t1 for update";
Konstantin Osipov's avatar
Konstantin Osipov committed
2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
--source include/wait_condition.inc

--echo # Check that I_S query which reads only .FRMs gets blocked.
--echo # Sending:
--send select column_name from information_schema.columns where table_schema='test' and table_name='t2';

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
--echo # Wait until SELECT COLUMN_NAME FROM I_S.COLUMNS  gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and
        info like "select column_name from information_schema.columns%";
--source include/wait_condition.inc

unlock tables;

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SELECT COLUMN_NAME FROM I_S.COLUMNS
--reap
drop table t2;

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
3024
lock tables t1 read;
Konstantin Osipov's avatar
Konstantin Osipov committed
3025 3026 3027 3028

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Sending:
3029
--send create table t2 select * from t1 for update;
Konstantin Osipov's avatar
Konstantin Osipov committed
3030 3031 3032 3033 3034 3035

--echo # Switching to connection 'default'.
connection default;
--echo # Waiting until CREATE TABLE ... SELECT ... is blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
3036
  where state = "Table lock" and info = "create table t2 select * from t1 for update";
Konstantin Osipov's avatar
Konstantin Osipov committed
3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072
--source include/wait_condition.inc

--echo # Finally, check that I_S query which does full-blown table open
--echo # also gets blocked.
--echo # Sending:
--send select table_name, table_type, auto_increment, table_comment from information_schema.tables where table_schema='test' and table_name='t2';

--echo # Switching to connection 'con46044_2'.
connection con46044_2;
--echo # Wait until SELECT ... FROM I_S.TABLES gets blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and
        info like "select table_name, table_type, auto_increment, table_comment from information_schema.tables%";
--source include/wait_condition.inc

unlock tables;

--echo # Switching to connection 'con46044'.
connection con46044;
--echo # Reaping CREATE TABLE ... SELECT ... .
--reap

--echo # Switching to connection 'default'.
connection default;
--echo # Reaping SELECT ... FROM I_S.TABLES
--reap
drop table t2;

--echo # Switching to connection 'default'.
connection default;
--echo # Clean-up.
disconnect con46044;
disconnect con46044_2;
drop table t1;

Konstantin Osipov's avatar
Konstantin Osipov committed
3073

3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087
--echo #
--echo # Test for bug #46273 "MySQL 5.4.4 new MDL: Bug#989 is not fully fixed
--echo #                      in case of ALTER".
--echo #
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con46273,localhost,root,,test,,);
connection default;
create table t1 (c1 int primary key, c2 int, c3 int);
insert into t1 values (1,1,0),(2,2,0),(3,3,0),(4,4,0),(5,5,0);

begin;
3088
select * from t1 where c2 = 3;
3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099

--echo #
--echo # Switching to connection 'con46273'.
connection con46273;
set debug_sync='after_lock_tables_takes_lock SIGNAL alter_table_locked WAIT_FOR alter_go';
--send alter table t1 add column e int, rename to t2;

--echo #
--echo # Switching to connection 'default'.
connection default;
set debug_sync='now WAIT_FOR alter_table_locked';
3100
set debug_sync='before_open_table_wait_refresh SIGNAL alter_go';
3101 3102 3103 3104
--echo # The below statement should get ER_LOCK_DEADLOCK error
--echo # (i.e. it should not allow ALTER to proceed, and then
--echo # fail due to 't1' changing its name to 't2').
--error ER_LOCK_DEADLOCK
3105
update t1 set c3=c3+1 where c2 = 3;
3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133

--echo #
--echo # Let us check that failure of the above statement has not released
--echo # metadata lock on table 't1', i.e. that ALTER TABLE is still blocked.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "alter table t1 add column e int, rename to t2";
--source include/wait_condition.inc

--echo # Unblock ALTER TABLE by commiting transaction and thus releasing
--echo # metadata lock on 't1'.
commit;

--echo #
--echo # Switching to connection 'con46273'.
connection con46273;
--echo # Reap ALTER TABLE.
--reap

--echo #
--echo # Switching to connection 'default'.
connection default;
disconnect con46273;
--echo # Clean-up.
set debug_sync= 'RESET';
drop table t2;


Konstantin Osipov's avatar
Konstantin Osipov committed
3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169
--echo #
--echo # Test for bug #46673 "Deadlock between FLUSH TABLES WITH READ LOCK
--echo #                      and DML".
--echo #
--disable_warnings
drop tables if exists t1;
--enable_warnings
connect (con46673, localhost, root,,);
connection default;
create table t1 (i int);

--echo # Switching to connection 'con46673'.
connection con46673;
begin;
insert into t1 values (1);

--echo # Switching to connection 'default'.
connection default;
--echo # Statement below should not get blocked. And if after some
--echo # changes to code it is there should not be a deadlock between
--echo # it and transaction from connection 'con46673'.
flush tables with read lock;
unlock tables;

--echo # Switching to connection 'con46673'.
connection con46673;
delete from t1 where i = 1;
commit;

--echo # Switching to connection 'default'.
connection default;
--echo # Clean-up
disconnect con46673;
drop table t1;


Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205
--echo #
--echo # Bug#48210 FLUSH TABLES WITH READ LOCK deadlocks 
--echo #           against concurrent CREATE PROCEDURE
--echo #

connect (con2, localhost, root);

--echo # Test 1: CREATE PROCEDURE

--echo # Connection 1
connection default;
--echo # Start CREATE PROCEDURE and open mysql.proc
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL table_opened WAIT_FOR grlwait';
--send CREATE PROCEDURE p1() SELECT 1

--echo # Connection 2
connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR table_opened';
--echo # Check that FLUSH must wait to get the GRL
--echo # and let CREATE PROCEDURE continue
SET DEBUG_SYNC= 'wait_lock_global_read_lock SIGNAL grlwait';
--send FLUSH TABLES WITH READ LOCK

--echo # Connection 1
connection default;
--reap

--echo # Connection 2
connection con2;
--reap
UNLOCK TABLES;

--echo # Connection 1
connection default;
SET DEBUG_SYNC= 'RESET';

Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3206
--echo # Test 2: DROP PROCEDURE
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3207 3208

connection default;
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3209
--echo # Start DROP PROCEDURE and open tables
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3210
SET DEBUG_SYNC= 'after_open_table_mdl_shared SIGNAL table_opened WAIT_FOR grlwait';
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3211
--send DROP PROCEDURE p1
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3212 3213 3214 3215 3216

--echo # Connection 2
connection con2;
SET DEBUG_SYNC= 'now WAIT_FOR table_opened';
--echo # Check that FLUSH must wait to get the GRL
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3217
--echo # and let DROP PROCEDURE continue
Jon Olav Hauglid's avatar
Jon Olav Hauglid committed
3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236
SET DEBUG_SYNC= 'wait_lock_global_read_lock SIGNAL grlwait';
--send FLUSH TABLES WITH READ LOCK

--echo # Connection 1
connection default;
--reap

--echo # Connection 2
connection con2;
--reap
UNLOCK TABLES;

--echo # Connection 1
connection default;
SET DEBUG_SYNC= 'RESET';

disconnect con2;


3237 3238 3239 3240 3241
--echo #
--echo # Bug#50786 Assertion `thd->mdl_context.trans_sentinel() == __null' 
--echo #           failed in open_ltable()
--echo #

3242 3243
--echo # Supress warnings written to the log file
call mtr.add_suppression("Wait on a lock was aborted due to a pending exclusive lock");
3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302
--disable_warnings
DROP TABLE IF EXISTS t1, t2;
--enable_warnings

connect (con1,localhost,root);
connect (con2,localhost,root);
connect (con3,localhost,root);
connection default;

CREATE TABLE t1 (i INT);
CREATE TABLE t2 (i INT);

SET @old_general_log= @@global.general_log;
SET @@global.general_log= 1;

SET @old_log_output= @@global.log_output;
SET @@global.log_output= 'TABLE';

SET @old_sql_log_off= @@session.sql_log_off;
SET @@session.sql_log_off= 1;

--echo # connection: con1
connection con1;
HANDLER t1 OPEN;

--echo # connection: con3
connection con3;
SET @@session.sql_log_off= 1;

--echo # connection: con2
connection con2;
SET DEBUG_SYNC= 'thr_multi_lock_after_thr_lock SIGNAL parked WAIT_FOR go';

# The below statement will block on the debug sync point
# after it gets write lock on mysql.general_log table.
--echo # Sending:
--send SELECT 1

--echo # connection: con3
connection con3;
SET DEBUG_SYNC= 'now WAIT_FOR parked';

--echo # connection: con1
connection con1;
# This statement will block in open_ltable() when
# trying to write into mysql.general_log.
--echo # Sending:
--send SELECT 1

--echo # connection: con3
connection con3;
let $wait_condition=
  SELECT COUNT(*) = 1 FROM information_schema.processlist
  WHERE state = "Table lock" and info = "SELECT 1";
--source include/wait_condition.inc
# The ALTER below will try to abort the statement in connection con1,
# since the latter waits on a table-level lock while having a HANDLER
# open. This will cause mysql_lock_tables() in con1 fail which before
# triggered the assert.
3303
ALTER TABLE t1 ADD COLUMN j INT;
3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330

--echo # connection: default
connection default;
SET DEBUG_SYNC= 'now SIGNAL go';

--echo # connection: con1
connection con1;
--echo # Reaping SELECT 1
--reap
HANDLER t1 CLOSE;

--echo # connection: con2
connection con2;
--echo # Reaping SELECT 1
--reap

--echo # connection: default
connection default;
DROP TABLE t1, t2;
SET DEBUG_SYNC= 'RESET';
disconnect con1;
disconnect con2;
disconnect con3;
SET @@global.general_log= @old_general_log;
SET @@global.log_output= @old_log_output;
SET @@session.sql_log_off= @old_sql_log_off;

3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386

--echo #
--echo # Additional coverage for bug #50913 "Deadlock between
--echo # open_and_lock_tables_derived and MDL". The main test
--echo # case is in lock_multi.test
--echo #
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con50913_1,localhost,root);
connect (con50913_2,localhost,root);
connection default;
create table t1 (i int) engine=InnoDB;

--echo # Switching to connection 'con50913_1'.
connection con50913_1;
set debug_sync= 'thr_multi_lock_after_thr_lock SIGNAL parked WAIT_FOR go';
--echo # Sending:
--send alter table t1 add column j int

--echo # Switching to connection 'default'.
connection default;
--echo # Wait until ALTER TABLE gets blocked on a sync point after
--echo # acquiring thr_lock.c lock.
set debug_sync= 'now WAIT_FOR parked';
--echo # The below statement should wait on MDL lock and not deadlock on
--echo # thr_lock.c lock.
--echo # Sending:
--send truncate table t1

--echo # Switching to connection 'con50913_2'.
connection con50913_2;
--echo # Wait until TRUNCATE TABLE is blocked on MDL lock.
let $wait_condition=
  select count(*) = 1 from information_schema.processlist
  where state = "Waiting for table" and info = "truncate table t1";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE.
set debug_sync= 'now SIGNAL go';

--echo # Switching to connection 'con50913_1'.
connection con50913_1;
--echo # Reaping ALTER TABLE.
--reap

--echo # Switching to connection 'default'.
connection default;
--echo # Reaping TRUNCATE TABLE.
--reap
disconnect con50913_1;
disconnect con50913_2;
set debug_sync= 'RESET';
drop table t1;


3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474
--echo #
--echo # Test for bug #50998 "Deadlock in MDL code during test
--echo #                      rqg_mdl_stability".
--echo # Also provides coverage for the case when addition of
--echo # waiting statement adds several loops in the waiters
--echo # graph and therefore several searches for deadlock
--echo # should be performed.
--disable_warnings
drop table if exists t1;
--enable_warnings
set debug_sync= 'RESET';
connect (con1,localhost,root);
connect (con2,localhost,root);
connect (con3,localhost,root);
connection default;
create table t1 (i int);

--echo # Switching to connection 'con1'.
connection con1;
begin;
select * from t1;

--echo # Switching to connection 'con2'.
connection con2;
begin;
select * from t1;

--echo # Switching to connection 'default'.
connection default;
--echo # Start ALTER TABLE which will acquire SNW lock and
--echo # table lock and get blocked on sync point.
set debug_sync= 'thr_multi_lock_after_thr_lock SIGNAL parked WAIT_FOR go';
--echo # Sending:
--send alter table t1 add column j int

--echo # Switching to connection 'con1'.
connection con1;
--echo # Wait until ALTER TABLE gets blocked on a sync point.
set debug_sync= 'now WAIT_FOR parked';
--echo # Sending:
--send insert into t1 values (1)

--echo # Switching to connection 'con2'.
connection con2;
--echo # Sending:
--send insert into t1 values (1)

--echo # Switching to connection 'con3'.
connection con3;
--echo # Wait until both 'con1' and 'con2' are blocked trying to acquire
--echo # SW lock on the table.
let $wait_condition=
  select count(*) = 2 from information_schema.processlist
  where state = "Waiting for table" and info = "insert into t1 values (1)";
--source include/wait_condition.inc
--echo # Unblock ALTER TABLE. Since it will try to upgrade SNW to X lock
--echo # deadlock with two loops in waiting graph will occur. Both loops
--echo # should be found and DML statements in both 'con1' and 'con2'
--echo # should be aborted with ER_LOCK_DEADLOCK errors.
set debug_sync= 'now SIGNAL go';

--echo # Switching to connection 'con1'.
connection con1;
--echo # Reaping INSERT. It should end with ER_LOCK_DEADLOCK error and
--echo # not wait indefinitely (as it happened before the bugfix).
--error ER_LOCK_DEADLOCK
--reap
commit;

--echo # Switching to connection 'con2'.
connection con2;
--echo # Reaping INSERT.
--error ER_LOCK_DEADLOCK
--reap
commit;

--echo # Switching to connection 'default'.
connection default;
--echo # Reap ALTER TABLE.
--reap

disconnect con1;
disconnect con2;
disconnect con3;
connection default;
set debug_sync= 'RESET';
drop table t1;

3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552
--echo #
--echo # Bug#42643: InnoDB does not support replication of TRUNCATE TABLE
--echo #
--echo # Ensure that a acquired lock is not given up due to a conflict.
--echo #

connect (con1,localhost,root,,test,,);
connect (con2,localhost,root,,test,,);
connect (con3,localhost,root,,test,,);

connection default;

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

CREATE TABLE t1 (a INT) ENGINE=InnoDB;
INSERT INTO t1 VALUES (1),(2),(3);

--echo # Connection: con1
connection con1;
SET debug_sync='lock_table_for_truncate SIGNAL parked_truncate WAIT_FOR go_truncate';
send TRUNCATE TABLE t1;

connection default;
--echo # Connection: default
SET debug_sync='now WAIT_FOR parked_truncate';

connection con2;
--echo # Connection: con2
SET debug_sync='after_open_table_ignore_flush SIGNAL parked_show WAIT_FOR go_show';
send SHOW FIELDS FROM t1;

connection default;
--echo # Connection: default
SET debug_sync='now WAIT_FOR parked_show';

connection con3;
--echo # Connection: con3
SET debug_sync='after_flush_unlock SIGNAL parked_flush WAIT_FOR go_flush';
send FLUSH TABLES t1;

connection default;
--echo # Connection: default
SET debug_sync='now WAIT_FOR parked_flush';
SET debug_sync='now SIGNAL go_truncate';

connection con1;
--echo # Connection: con1
--echo # Reaping...
reap;

connection default;
--echo # Connection: default
SET debug_sync= 'now SIGNAL go_show';

connection con2;
--echo # Connection: con2 (SHOW FIELDS FROM t1)
--echo # Reaping...
reap;

connection default;
--echo # Connection: default
SET debug_sync= 'now SIGNAL go_flush';

connection con3;
--echo # Connection: con3 (FLUSH TABLES t1)
--echo # Reaping...
reap;

disconnect con1;
disconnect con2;
disconnect con3;

connection default;
--echo # Connection: default
SET debug_sync= 'RESET';
DROP TABLE t1;
3553

Konstantin Osipov's avatar
Konstantin Osipov committed
3554 3555 3556
# Check that all connections opened by test cases in this file are really
# gone so execution of other tests won't be affected by their presence.
--source include/wait_until_count_sessions.inc