26 #include "my_config.h"
27 #include <gtest/gtest.h>
30 #include <mysqld_error.h>
32 #include "thr_malloc.h"
33 #include "thread_utils.h"
34 #include "test_mdl_context_owner.h"
44 extern "C" void thd_wait_end(MYSQL_THD thd)
51 static uint expected_error= 0;
52 extern "C" void test_error_handler_hook(uint err,
const char *str, myf MyFlags)
54 EXPECT_EQ(expected_error, err) << str;
61 void debug_sync(THD *thd,
const char *sync_point_name,
size_t name_len)
63 DBUG_PRINT(
"debug_sync_point", (
"hit: '%s'", sync_point_name));
64 FAIL() <<
"Not yet implemented.";
71 namespace mdl_unittest {
76 const char db_name[]=
"some_database";
77 const char table_name1[]=
"some_table1";
78 const char table_name2[]=
"some_table2";
79 const char table_name3[]=
"some_table3";
80 const char table_name4[]=
"some_table4";
81 const ulong zero_timeout= 0;
82 const ulong long_timeout= (ulong) 3600L*24L*365L;
89 : m_null_ticket(NULL),
94 static void SetUpTestCase()
96 error_handler_hook= test_error_handler_hook;
97 mdl_locks_hash_partitions= MDL_LOCKS_HASH_PARTITIONS_DEFAULT;
104 m_mdl_context.
init(
this);
105 EXPECT_FALSE(m_mdl_context.has_locks());
106 m_global_request.
init(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE,
117 bool needs_thr_lock_abort)
123 void test_one_simple_shared_lock(enum_mdl_type lock_type);
132 GTEST_DISALLOW_COPY_AND_ASSIGN_(
MDLTest);
145 enum_mdl_type mdl_type,
150 : m_table_name(table_name),
151 m_mdl_type(mdl_type),
152 m_lock_grabbed(lock_grabbed),
153 m_release_locks(release_locks),
154 m_lock_blocked(lock_blocked),
155 m_lock_released(lock_released),
156 m_ignore_notify(
false)
158 m_mdl_context.
init(
this);
167 void ignore_notify() { m_ignore_notify=
true; }
170 bool needs_thr_lock_abort)
177 m_release_locks->notify();
185 const char *src_function,
186 const char *src_file,
190 src_function, src_file, src_line);
197 m_lock_blocked->notify();
204 return m_mdl_context;
208 const char *m_table_name;
209 enum_mdl_type m_mdl_type;
210 Notification *m_lock_grabbed;
211 Notification *m_release_locks;
212 Notification *m_lock_blocked;
213 Notification *m_lock_released;
214 bool m_ignore_notify;
219 void MDL_thread::run()
224 global_request.
init(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE,
226 request.
init(MDL_key::TABLE, db_name, m_table_name, m_mdl_type,
229 request_list.push_front(&request);
230 if (m_mdl_type >= MDL_SHARED_UPGRADABLE)
231 request_list.push_front(&global_request);
233 EXPECT_FALSE(m_mdl_context.
acquire_locks(&request_list, long_timeout));
234 EXPECT_TRUE(m_mdl_context.
235 is_lock_owner(MDL_key::TABLE, db_name, m_table_name, m_mdl_type));
238 m_lock_grabbed->notify();
240 m_release_locks->wait_for_notification();
246 m_lock_released->notify();
250 typedef MDLTest MDLDeathTest;
256 #if GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
257 TEST_F(MDLDeathTest, DieWhenMTicketsNonempty)
259 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
260 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
263 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
264 EXPECT_DEATH(m_mdl_context.destroy(),
265 ".*Assertion.*MDL_TRANSACTION.*is_empty.*");
266 m_mdl_context.release_transactional_locks();
268 #endif // GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
275 TEST_F(MDLTest, ConstructAndDestruct)
280 void MDLTest::test_one_simple_shared_lock(enum_mdl_type lock_type)
282 m_request.
init(MDL_key::TABLE, db_name, table_name1, lock_type,
285 EXPECT_EQ(lock_type, m_request.
type);
286 EXPECT_EQ(m_null_ticket, m_request.
ticket);
289 EXPECT_NE(m_null_ticket, m_request.
ticket);
290 EXPECT_TRUE(m_mdl_context.has_locks());
291 EXPECT_TRUE(m_mdl_context.
292 is_lock_owner(MDL_key::TABLE, db_name, table_name1, lock_type));
295 request_2.
init(&m_request.
key, lock_type, MDL_TRANSACTION);
300 EXPECT_FALSE(m_mdl_context.has_locks());
307 TEST_F(MDLTest, OneShared)
309 test_one_simple_shared_lock(MDL_SHARED);
316 TEST_F(MDLTest, OneSharedHighPrio)
318 test_one_simple_shared_lock(MDL_SHARED_HIGH_PRIO);
325 TEST_F(MDLTest, OneSharedRead)
327 test_one_simple_shared_lock(MDL_SHARED_READ);
334 TEST_F(MDLTest, OneSharedWrite)
336 test_one_simple_shared_lock(MDL_SHARED_WRITE);
343 TEST_F(MDLTest, OneExclusive)
345 const enum_mdl_type lock_type= MDL_EXCLUSIVE;
346 m_request.init(MDL_key::TABLE, db_name, table_name1, lock_type,
348 EXPECT_EQ(m_null_ticket, m_request.ticket);
350 m_request_list.push_front(&m_request);
351 m_request_list.push_front(&m_global_request);
353 EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
355 EXPECT_NE(m_null_ticket, m_request.ticket);
356 EXPECT_NE(m_null_ticket, m_global_request.ticket);
357 EXPECT_TRUE(m_mdl_context.has_locks());
358 EXPECT_TRUE(m_mdl_context.
359 is_lock_owner(MDL_key::TABLE, db_name, table_name1, lock_type));
360 EXPECT_TRUE(m_mdl_context.
361 is_lock_owner(MDL_key::GLOBAL,
"",
"", MDL_INTENTION_EXCLUSIVE));
362 EXPECT_TRUE(m_request.ticket->is_upgradable_or_exclusive());
364 m_mdl_context.release_transactional_locks();
365 EXPECT_FALSE(m_mdl_context.has_locks());
373 TEST_F(MDLTest, TwoShared)
376 m_request.
init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED, MDL_EXPLICIT);
377 request_2.
init(MDL_key::TABLE, db_name, table_name2, MDL_SHARED, MDL_EXPLICIT);
379 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
380 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2));
381 EXPECT_TRUE(m_mdl_context.has_locks());
382 ASSERT_NE(m_null_ticket, m_request.ticket);
383 ASSERT_NE(m_null_ticket, request_2.
ticket);
385 EXPECT_TRUE(m_mdl_context.
386 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
387 EXPECT_TRUE(m_mdl_context.
388 is_lock_owner(MDL_key::TABLE, db_name, table_name2, MDL_SHARED));
389 EXPECT_FALSE(m_mdl_context.
390 is_lock_owner(MDL_key::TABLE, db_name, table_name3, MDL_SHARED));
392 m_mdl_context.release_lock(m_request.ticket);
393 EXPECT_FALSE(m_mdl_context.
394 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
395 EXPECT_TRUE(m_mdl_context.has_locks());
397 m_mdl_context.release_lock(request_2.
ticket);
398 EXPECT_FALSE(m_mdl_context.
399 is_lock_owner(MDL_key::TABLE, db_name, table_name2, MDL_SHARED));
400 EXPECT_FALSE(m_mdl_context.has_locks());
408 TEST_F(MDLTest, SharedLocksBetweenContexts)
411 mdl_context2.
init(
this);
413 m_request.
init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
415 request_2.
init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
418 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
421 EXPECT_TRUE(m_mdl_context.
422 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
423 EXPECT_TRUE(mdl_context2.
424 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
426 m_mdl_context.release_transactional_locks();
434 TEST_F(MDLTest, UpgradeSharedUpgradable)
436 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
439 m_request_list.push_front(&m_request);
440 m_request_list.push_front(&m_global_request);
442 EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
443 EXPECT_FALSE(m_mdl_context.
444 upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
445 EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type());
448 EXPECT_FALSE(m_mdl_context.
449 upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
450 EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type());
452 m_mdl_context.release_transactional_locks();
459 TEST_F(MDLTest, SavePoint)
464 m_request.
init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
466 request_2.
init(MDL_key::TABLE, db_name, table_name2, MDL_SHARED,
468 request_3.
init(MDL_key::TABLE, db_name, table_name3, MDL_SHARED,
470 request_4.
init(MDL_key::TABLE, db_name, table_name4, MDL_SHARED,
473 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
474 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2));
476 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_3));
477 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_4));
479 EXPECT_TRUE(m_mdl_context.
480 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
481 EXPECT_TRUE(m_mdl_context.
482 is_lock_owner(MDL_key::TABLE, db_name, table_name2, MDL_SHARED));
483 EXPECT_TRUE(m_mdl_context.
484 is_lock_owner(MDL_key::TABLE, db_name, table_name3, MDL_SHARED));
485 EXPECT_TRUE(m_mdl_context.
486 is_lock_owner(MDL_key::TABLE, db_name, table_name4, MDL_SHARED));
488 m_mdl_context.rollback_to_savepoint(savepoint);
489 EXPECT_TRUE(m_mdl_context.
490 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
491 EXPECT_TRUE(m_mdl_context.
492 is_lock_owner(MDL_key::TABLE, db_name, table_name2, MDL_SHARED));
493 EXPECT_FALSE(m_mdl_context.
494 is_lock_owner(MDL_key::TABLE, db_name, table_name3, MDL_SHARED));
495 EXPECT_FALSE(m_mdl_context.
496 is_lock_owner(MDL_key::TABLE, db_name, table_name4, MDL_SHARED));
498 m_mdl_context.release_transactional_locks();
499 EXPECT_FALSE(m_mdl_context.
500 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
501 EXPECT_FALSE(m_mdl_context.
502 is_lock_owner(MDL_key::TABLE, db_name, table_name2, MDL_SHARED));
509 TEST_F(MDLTest, ConcurrentShared)
511 Notification lock_grabbed;
512 Notification release_locks;
513 MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed,
514 &release_locks, NULL, NULL);
516 lock_grabbed.wait_for_notification();
518 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
521 EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout));
522 EXPECT_TRUE(m_mdl_context.
523 is_lock_owner(MDL_key::TABLE, db_name, table_name1, MDL_SHARED));
525 release_locks.notify();
528 m_mdl_context.release_transactional_locks();
536 TEST_F(MDLTest, ConcurrentSharedExclusive)
538 expected_error= ER_LOCK_WAIT_TIMEOUT;
540 Notification lock_grabbed;
541 Notification release_locks;
542 MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks,
544 mdl_thread.ignore_notify();
546 lock_grabbed.wait_for_notification();
548 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE,
551 m_request_list.push_front(&m_request);
552 m_request_list.push_front(&m_global_request);
555 EXPECT_TRUE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout));
556 EXPECT_FALSE(m_mdl_context.
557 is_lock_owner(MDL_key::TABLE,
558 db_name, table_name1, MDL_EXCLUSIVE));
560 release_locks.notify();
564 EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout));
565 EXPECT_NE(m_null_ticket, m_request.ticket);
567 m_mdl_context.release_transactional_locks();
575 TEST_F(MDLTest, ConcurrentExclusiveShared)
577 Notification lock_grabbed;
578 Notification release_locks;
579 MDL_thread mdl_thread(table_name1, MDL_EXCLUSIVE,
580 &lock_grabbed, &release_locks, NULL, NULL);
582 lock_grabbed.wait_for_notification();
584 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
588 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
589 EXPECT_EQ(m_null_ticket, m_request.ticket);
591 release_locks.notify();
594 EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout));
595 EXPECT_NE(m_null_ticket, m_request.ticket);
598 m_mdl_context.release_transactional_locks();
610 TEST_F(MDLTest, ConcurrentUpgrade)
612 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
614 m_request_list.push_front(&m_request);
615 m_request_list.push_front(&m_global_request);
617 EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
618 EXPECT_TRUE(m_mdl_context.
619 is_lock_owner(MDL_key::TABLE,
620 db_name, table_name1, MDL_SHARED_UPGRADABLE));
621 EXPECT_FALSE(m_mdl_context.
622 is_lock_owner(MDL_key::TABLE,
623 db_name, table_name1, MDL_EXCLUSIVE));
625 Notification lock_grabbed;
626 Notification release_locks;
627 MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks,
630 lock_grabbed.wait_for_notification();
632 EXPECT_FALSE(m_mdl_context.
633 upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
634 EXPECT_TRUE(m_mdl_context.
635 is_lock_owner(MDL_key::TABLE,
636 db_name, table_name1, MDL_EXCLUSIVE));
639 m_mdl_context.release_transactional_locks();
643 TEST_F(MDLTest, UpgradableConcurrency)
647 Notification lock_grabbed;
648 Notification release_locks;
649 MDL_thread mdl_thread(table_name1, MDL_SHARED_UPGRADABLE,
650 &lock_grabbed, &release_locks, NULL, NULL);
652 lock_grabbed.wait_for_notification();
655 m_request.init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED_WRITE,
657 EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
658 EXPECT_NE(m_null_ticket, m_request.ticket);
661 expected_error= ER_LOCK_WAIT_TIMEOUT;
662 request_2.
init(MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
664 request_list.push_front(&m_global_request);
665 request_list.push_front(&request_2);
666 EXPECT_TRUE(m_mdl_context.acquire_locks(&request_list, zero_timeout));
667 EXPECT_EQ(m_null_ticket, request_2.
ticket);
669 release_locks.notify();
672 m_mdl_context.release_transactional_locks();
692 TEST_F(MDLTest, HogLockTest1)
694 Notification thd_lock_grabbed[5];
695 Notification thd_release_locks[5];
696 Notification thd_lock_blocked[5];
697 Notification thd_lock_released[5];
700 enum {THD1_X, THD2_SR, THD3_SW, THD4_SNRW, THD5_SNRW};
707 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X],
708 &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X],
709 &thd_lock_released[THD1_X]);
711 thd_lock_grabbed[THD1_X].wait_for_notification();
718 MDL_thread mdl_thread2(table_name1, MDL_SHARED_READ,
719 &thd_lock_grabbed[THD2_SR],
720 &thd_release_locks[THD2_SR],
721 &thd_lock_blocked[THD2_SR],
722 &thd_lock_released[THD2_SR]);
724 thd_lock_blocked[THD2_SR].wait_for_notification();
731 MDL_thread mdl_thread3(table_name1, MDL_SHARED_WRITE,
732 &thd_lock_grabbed[THD3_SW],
733 &thd_release_locks[THD3_SW],
734 &thd_lock_blocked[THD3_SW],
735 &thd_lock_released[THD3_SW]);
737 thd_lock_blocked[THD3_SW].wait_for_notification();
744 MDL_thread mdl_thread4(table_name1, MDL_SHARED_NO_READ_WRITE,
745 &thd_lock_grabbed[THD4_SNRW],
746 &thd_release_locks[THD4_SNRW],
747 &thd_lock_blocked[THD4_SNRW],
748 &thd_lock_released[THD4_SNRW]);
750 thd_lock_blocked[THD4_SNRW].wait_for_notification();
753 thd_release_locks[THD1_X].notify();
754 thd_lock_released[THD1_X].wait_for_notification();
760 thd_lock_grabbed[THD4_SNRW].wait_for_notification();
767 MDL_thread mdl_thread5(table_name1, MDL_SHARED_NO_READ_WRITE,
768 &thd_lock_grabbed[THD5_SNRW],
769 &thd_release_locks[THD5_SNRW],
770 &thd_lock_blocked[THD5_SNRW],
771 &thd_lock_released[THD5_SNRW]);
773 thd_lock_blocked[THD5_SNRW].wait_for_notification();
776 thd_release_locks[THD4_SNRW].notify();
777 thd_lock_released[THD4_SNRW].wait_for_notification();
780 EXPECT_FALSE((mdl_thread2.get_mdl_context()).
781 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
784 EXPECT_FALSE((mdl_thread3.get_mdl_context()).
785 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
792 thd_lock_grabbed[THD5_SNRW].wait_for_notification();
793 thd_release_locks[THD5_SNRW].notify();
794 thd_lock_released[THD5_SNRW].wait_for_notification();
797 thd_lock_grabbed[THD2_SR].wait_for_notification();
798 thd_release_locks[THD2_SR].notify();
799 thd_lock_released[THD2_SR].wait_for_notification();
801 thd_lock_grabbed[THD3_SW].wait_for_notification();
802 thd_release_locks[THD3_SW].notify();
803 thd_lock_released[THD3_SW].wait_for_notification();
832 TEST_F(MDLTest, HogLockTest2)
834 Notification thd_lock_grabbed[5];
835 Notification thd_release_locks[5];
836 Notification thd_lock_blocked[5];
837 Notification thd_lock_released[5];
838 const ulong org_max_write_lock_count= max_write_lock_count;
841 enum {THD1_X, THD2_SR, THD3_SW, THD4_SNRW, THD5_SNRW};
843 max_write_lock_count= 1;
850 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE,
851 &thd_lock_grabbed[THD1_X],
852 &thd_release_locks[THD1_X],
853 &thd_lock_blocked[THD1_X],
854 &thd_lock_released[THD1_X]);
856 thd_lock_grabbed[THD1_X].wait_for_notification();
863 MDL_thread mdl_thread2(table_name1, MDL_SHARED_READ,
864 &thd_lock_grabbed[THD2_SR],
865 &thd_release_locks[THD2_SR],
866 &thd_lock_blocked[THD2_SR],
867 &thd_lock_released[THD2_SR]);
869 thd_lock_blocked[THD2_SR].wait_for_notification();
876 MDL_thread mdl_thread3(table_name1, MDL_SHARED_WRITE,
877 &thd_lock_grabbed[THD3_SW],
878 &thd_release_locks[THD3_SW],
879 &thd_lock_blocked[THD3_SW],
880 &thd_lock_released[THD3_SW]);
882 thd_lock_blocked[THD3_SW].wait_for_notification();
889 MDL_thread mdl_thread4(table_name1, MDL_SHARED_NO_READ_WRITE,
890 &thd_lock_grabbed[THD4_SNRW],
891 &thd_release_locks[THD4_SNRW],
892 &thd_lock_blocked[THD4_SNRW],
893 &thd_lock_released[THD4_SNRW]);
895 thd_lock_blocked[THD4_SNRW].wait_for_notification();
903 thd_release_locks[THD1_X].notify();
904 thd_lock_released[THD1_X].wait_for_notification();
907 thd_lock_grabbed[THD4_SNRW].wait_for_notification();
914 MDL_thread mdl_thread5(table_name1, MDL_SHARED_NO_READ_WRITE,
915 &thd_lock_grabbed[THD5_SNRW],
916 &thd_release_locks[THD5_SNRW],
917 &thd_lock_blocked[THD5_SNRW],
918 &thd_lock_released[THD5_SNRW]);
920 thd_lock_blocked[THD5_SNRW].wait_for_notification();
923 thd_release_locks[THD4_SNRW].notify();
924 thd_lock_released[THD4_SNRW].wait_for_notification();
932 thd_lock_grabbed[THD2_SR].wait_for_notification();
933 thd_lock_grabbed[THD3_SW].wait_for_notification();
935 thd_release_locks[THD2_SR].notify();
936 thd_lock_released[THD2_SR].wait_for_notification();
938 thd_release_locks[THD3_SW].notify();
939 thd_lock_released[THD3_SW].wait_for_notification();
942 thd_lock_grabbed[THD5_SNRW].wait_for_notification();
943 thd_release_locks[THD5_SNRW].notify();
944 thd_lock_released[THD5_SNRW].wait_for_notification();
952 max_write_lock_count= org_max_write_lock_count;
981 TEST_F(MDLTest, LockPriorityTest)
983 Notification thd_lock_grabbed[7];
984 Notification thd_release_locks[7];
985 Notification thd_lock_blocked[7];
986 Notification thd_lock_released[7];
989 enum {THD1_X, THD2_S, THD3_SR, THD4_SW, THD5_SU, THD6_SNRW, THD7_SNW};
992 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X],
993 &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X],
994 &thd_lock_released[THD1_X]);
996 thd_lock_grabbed[THD1_X].wait_for_notification();
1003 MDL_thread mdl_thread2(table_name1, MDL_SHARED, &thd_lock_grabbed[THD2_S],
1004 &thd_release_locks[THD2_S], &thd_lock_blocked[THD2_S],
1005 &thd_lock_released[THD2_S]);
1006 mdl_thread2.start();
1007 thd_lock_blocked[THD2_S].wait_for_notification();
1014 MDL_thread mdl_thread3(table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD3_SR],
1015 &thd_release_locks[THD3_SR], &thd_lock_blocked[THD3_SR],
1016 &thd_lock_released[THD3_SR]);
1017 mdl_thread3.start();
1018 thd_lock_blocked[THD3_SR].wait_for_notification();
1025 MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE,
1026 &thd_lock_grabbed[THD4_SW],
1027 &thd_release_locks[THD4_SW],
1028 &thd_lock_blocked[THD4_SW],
1029 &thd_lock_released[THD4_SW]);
1030 mdl_thread4.start();
1031 thd_lock_blocked[THD4_SW].wait_for_notification();
1038 MDL_thread mdl_thread5(table_name1, MDL_SHARED_UPGRADABLE,
1039 &thd_lock_grabbed[THD5_SU],
1040 &thd_release_locks[THD5_SU],
1041 &thd_lock_blocked[THD5_SU],
1042 &thd_lock_released[THD5_SU]);
1043 mdl_thread5.start();
1044 thd_lock_blocked[THD5_SU].wait_for_notification();
1051 MDL_thread mdl_thread6(table_name1, MDL_SHARED_NO_READ_WRITE,
1052 &thd_lock_grabbed[THD6_SNRW],
1053 &thd_release_locks[THD6_SNRW],
1054 &thd_lock_blocked[THD6_SNRW],
1055 &thd_lock_released[THD6_SNRW]);
1056 mdl_thread6.start();
1057 thd_lock_blocked[THD6_SNRW].wait_for_notification();
1063 thd_release_locks[THD1_X].notify();
1064 thd_lock_released[THD1_X].wait_for_notification();
1071 thd_lock_grabbed[THD2_S].wait_for_notification();
1072 thd_release_locks[THD2_S].notify();
1073 thd_lock_released[THD2_S].wait_for_notification();
1075 thd_lock_grabbed[THD5_SU].wait_for_notification();
1076 thd_release_locks[THD5_SU].notify();
1077 thd_lock_released[THD5_SU].wait_for_notification();
1080 thd_lock_grabbed[THD6_SNRW].wait_for_notification();
1087 MDL_thread mdl_thread7(table_name1, MDL_SHARED_NO_WRITE,
1088 &thd_lock_grabbed[THD7_SNW],
1089 &thd_release_locks[THD7_SNW],
1090 &thd_lock_blocked[THD7_SNW],
1091 &thd_lock_released[THD7_SNW]);
1092 mdl_thread7.start();
1093 thd_lock_blocked[THD7_SNW].wait_for_notification();
1096 thd_release_locks[THD6_SNRW].notify();
1097 thd_lock_released[THD6_SNRW].wait_for_notification();
1100 thd_lock_grabbed[THD7_SNW].wait_for_notification();
1101 thd_lock_grabbed[THD3_SR].wait_for_notification();
1108 thd_release_locks[THD3_SR].notify();
1109 thd_lock_released[THD3_SR].wait_for_notification();
1112 EXPECT_FALSE((mdl_thread4.get_mdl_context()).
1113 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
1117 thd_release_locks[THD7_SNW].notify();
1118 thd_lock_released[THD7_SNW].wait_for_notification();
1120 thd_lock_grabbed[THD4_SW].wait_for_notification();
1121 thd_release_locks[THD4_SW].notify();
1122 thd_lock_released[THD4_SW].wait_for_notification();
1154 TEST_F(MDLTest, HogLockTest3)
1156 Notification thd_lock_grabbed[7];
1157 Notification thd_release_locks[7];
1158 Notification thd_lock_blocked[7];
1159 Notification thd_lock_released[7];
1160 const ulong org_max_write_lock_count= max_write_lock_count;
1162 enum {THD1_X, THD2_S, THD3_SR, THD4_SW, THD5_SU, THD6_X, THD7_SNRW};
1164 max_write_lock_count= 1;
1167 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X],
1168 &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X],
1169 &thd_lock_released[THD1_X]);
1170 mdl_thread1.start();
1171 thd_lock_grabbed[THD1_X].wait_for_notification();
1178 MDL_thread mdl_thread2(table_name1, MDL_SHARED, &thd_lock_grabbed[THD2_S],
1179 &thd_release_locks[THD2_S], &thd_lock_blocked[THD2_S],
1180 &thd_lock_released[THD2_S]);
1181 mdl_thread2.start();
1182 thd_lock_blocked[THD2_S].wait_for_notification();
1189 MDL_thread mdl_thread3(table_name1, MDL_SHARED_READ, &thd_lock_grabbed[THD3_SR],
1190 &thd_release_locks[THD3_SR], &thd_lock_blocked[THD3_SR],
1191 &thd_lock_released[THD3_SR]);
1192 mdl_thread3.start();
1193 thd_lock_blocked[THD3_SR].wait_for_notification();
1200 MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE,
1201 &thd_lock_grabbed[THD4_SW],
1202 &thd_release_locks[THD4_SW],
1203 &thd_lock_blocked[THD4_SW],
1204 &thd_lock_released[THD4_SW]);
1205 mdl_thread4.start();
1206 thd_lock_blocked[THD4_SW].wait_for_notification();
1213 MDL_thread mdl_thread5(table_name1, MDL_SHARED_UPGRADABLE,
1214 &thd_lock_grabbed[THD5_SU],
1215 &thd_release_locks[THD5_SU],
1216 &thd_lock_blocked[THD5_SU],
1217 &thd_lock_released[THD5_SU]);
1218 mdl_thread5.start();
1219 thd_lock_blocked[THD5_SU].wait_for_notification();
1226 MDL_thread mdl_thread6(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD6_X],
1227 &thd_release_locks[THD6_X], &thd_lock_blocked[THD6_X],
1228 &thd_lock_released[THD6_X]);
1229 mdl_thread6.start();
1230 thd_lock_blocked[THD6_X].wait_for_notification();
1237 thd_release_locks[THD1_X].notify();
1238 thd_lock_released[THD1_X].wait_for_notification();
1241 thd_lock_grabbed[THD6_X].wait_for_notification();
1248 MDL_thread mdl_thread7(table_name1, MDL_SHARED_NO_READ_WRITE,
1249 &thd_lock_grabbed[THD7_SNRW],
1250 &thd_release_locks[THD7_SNRW],
1251 &thd_lock_blocked[THD7_SNRW],
1252 &thd_lock_released[THD7_SNRW]);
1253 mdl_thread7.start();
1254 thd_lock_blocked[THD7_SNRW].wait_for_notification();
1257 thd_release_locks[THD6_X].notify();
1258 thd_lock_released[THD6_X].wait_for_notification();
1261 thd_lock_grabbed[THD2_S].wait_for_notification();
1262 thd_lock_grabbed[THD3_SR].wait_for_notification();
1263 thd_lock_grabbed[THD4_SW].wait_for_notification();
1264 thd_lock_grabbed[THD5_SU].wait_for_notification();
1271 EXPECT_FALSE((mdl_thread7.get_mdl_context()).
1272 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
1273 MDL_SHARED_NO_READ_WRITE));
1276 thd_release_locks[THD2_S].notify();
1277 thd_lock_released[THD2_S].wait_for_notification();
1279 thd_release_locks[THD3_SR].notify();
1280 thd_lock_released[THD3_SR].wait_for_notification();
1282 thd_release_locks[THD4_SW].notify();
1283 thd_lock_released[THD4_SW].wait_for_notification();
1285 thd_release_locks[THD5_SU].notify();
1286 thd_lock_released[THD5_SU].wait_for_notification();
1288 thd_lock_grabbed[THD7_SNRW].wait_for_notification();
1289 thd_release_locks[THD7_SNRW].notify();
1290 thd_lock_released[THD7_SNRW].wait_for_notification();
1300 max_write_lock_count= org_max_write_lock_count;
1323 TEST_F(MDLTest, HogLockTest4)
1325 Notification thd_lock_grabbed[5];
1326 Notification thd_release_locks[5];
1327 Notification thd_lock_blocked[5];
1328 Notification thd_lock_released[5];
1329 const ulong org_max_write_lock_count= max_write_lock_count;
1332 enum {THD1_X, THD2_SU, THD3_X, THD4_SNRW, THD5_SR};
1334 max_write_lock_count= 1;
1337 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X],
1338 &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X],
1339 &thd_lock_released[THD1_X]);
1340 mdl_thread1.start();
1341 thd_lock_grabbed[THD1_X].wait_for_notification();
1344 MDL_thread mdl_thread2(table_name1, MDL_SHARED_UPGRADABLE,
1345 &thd_lock_grabbed[THD2_SU],
1346 &thd_release_locks[THD2_SU],
1347 &thd_lock_blocked[THD2_SU],
1348 &thd_lock_released[THD2_SU]);
1349 mdl_thread2.start();
1350 thd_lock_blocked[THD2_SU].wait_for_notification();
1353 MDL_thread mdl_thread3(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD3_X],
1354 &thd_release_locks[THD3_X], &thd_lock_blocked[THD3_X],
1355 &thd_lock_released[THD3_X]);
1356 mdl_thread3.start();
1357 thd_lock_blocked[THD3_X].wait_for_notification();
1365 thd_release_locks[THD1_X].notify();
1366 thd_lock_released[THD1_X].wait_for_notification();
1368 thd_lock_grabbed[THD3_X].wait_for_notification();
1376 thd_release_locks[THD3_X].notify();
1377 thd_lock_released[THD3_X].wait_for_notification();
1379 thd_lock_grabbed[THD2_SU].wait_for_notification();
1386 MDL_thread mdl_thread4(table_name1, MDL_SHARED_NO_READ_WRITE,
1387 &thd_lock_grabbed[THD4_SNRW],
1388 &thd_release_locks[THD4_SNRW],
1389 &thd_lock_blocked[THD4_SNRW],
1390 &thd_lock_released[THD4_SNRW]);
1391 mdl_thread4.start();
1392 thd_lock_blocked[THD4_SNRW].wait_for_notification();
1399 MDL_thread mdl_thread5(table_name1, MDL_SHARED_READ,
1400 &thd_lock_grabbed[THD5_SR],
1401 &thd_release_locks[THD5_SR],
1402 &thd_lock_blocked[THD5_SR],
1403 &thd_lock_released[THD5_SR]);
1404 mdl_thread5.start();
1405 thd_lock_blocked[THD5_SR].wait_for_notification();
1408 thd_release_locks[THD2_SU].notify();
1409 thd_lock_released[THD2_SU].wait_for_notification();
1416 thd_lock_grabbed[THD4_SNRW].wait_for_notification();
1419 EXPECT_FALSE((mdl_thread5.get_mdl_context()).
1420 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
1424 thd_release_locks[THD4_SNRW].notify();
1425 thd_lock_released[THD4_SNRW].wait_for_notification();
1427 thd_lock_grabbed[THD5_SR].wait_for_notification();
1428 thd_release_locks[THD5_SR].notify();
1429 thd_lock_released[THD5_SR].wait_for_notification();
1437 max_write_lock_count= org_max_write_lock_count;
1462 TEST_F(MDLTest, HogLockTest5)
1464 Notification thd_lock_grabbed[6];
1465 Notification thd_release_locks[6];
1466 Notification thd_lock_blocked[6];
1467 Notification thd_lock_released[6];
1468 const ulong org_max_write_lock_count= max_write_lock_count;
1471 enum {THD1_X, THD2_SNW, THD3_SR, THD4_SW, THD5_SU, THD6_SNRW};
1472 max_write_lock_count= 1;
1475 MDL_thread mdl_thread1(table_name1, MDL_EXCLUSIVE, &thd_lock_grabbed[THD1_X],
1476 &thd_release_locks[THD1_X], &thd_lock_blocked[THD1_X],
1477 &thd_lock_released[THD1_X]);
1478 mdl_thread1.start();
1479 thd_lock_grabbed[THD1_X].wait_for_notification();
1482 MDL_thread mdl_thread2(table_name1, MDL_SHARED_NO_WRITE,
1483 &thd_lock_grabbed[THD2_SNW],
1484 &thd_release_locks[THD2_SNW],
1485 &thd_lock_blocked[THD2_SNW],
1486 &thd_lock_released[THD2_SNW]);
1487 mdl_thread2.start();
1488 thd_lock_blocked[THD2_SNW].wait_for_notification();
1491 MDL_thread mdl_thread3(table_name1, MDL_SHARED_READ,
1492 &thd_lock_grabbed[THD3_SR],
1493 &thd_release_locks[THD3_SR],
1494 &thd_lock_blocked[THD3_SR],
1495 &thd_lock_released[THD3_SR]);
1496 mdl_thread3.start();
1497 thd_lock_blocked[THD3_SR].wait_for_notification();
1500 MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE,
1501 &thd_lock_grabbed[THD4_SW],
1502 &thd_release_locks[THD4_SW],
1503 &thd_lock_blocked[THD4_SW],
1504 &thd_lock_released[THD4_SW]);
1505 mdl_thread4.start();
1506 thd_lock_blocked[THD4_SW].wait_for_notification();
1509 MDL_thread mdl_thread5(table_name1, MDL_SHARED_UPGRADABLE,
1510 &thd_lock_grabbed[THD5_SU],
1511 &thd_release_locks[THD5_SU],
1512 &thd_lock_blocked[THD5_SU],
1513 &thd_lock_released[THD5_SU]);
1514 mdl_thread5.start();
1515 thd_lock_blocked[THD5_SU].wait_for_notification();
1522 thd_release_locks[THD1_X].notify();
1523 thd_lock_released[THD1_X].wait_for_notification();
1530 thd_lock_grabbed[THD2_SNW].wait_for_notification();
1531 thd_lock_grabbed[THD3_SR].wait_for_notification();
1538 MDL_thread mdl_thread6(table_name1, MDL_SHARED_NO_READ_WRITE,
1539 &thd_lock_grabbed[THD6_SNRW],
1540 &thd_release_locks[THD6_SNRW],
1541 &thd_lock_blocked[THD6_SNRW],
1542 &thd_lock_released[THD6_SNRW]);
1543 mdl_thread6.start();
1544 thd_lock_blocked[THD6_SNRW].wait_for_notification();
1548 thd_release_locks[THD2_SNW].notify();
1549 thd_lock_released[THD2_SNW].wait_for_notification();
1557 EXPECT_FALSE((mdl_thread6.get_mdl_context()).
1558 is_lock_owner(MDL_key::TABLE, db_name, table_name1,
1559 MDL_SHARED_NO_READ_WRITE));
1561 thd_lock_grabbed[THD4_SW].wait_for_notification();
1562 thd_release_locks[THD4_SW].notify();
1563 thd_lock_released[THD4_SW].wait_for_notification();
1565 thd_lock_grabbed[THD5_SU].wait_for_notification();
1566 thd_release_locks[THD5_SU].notify();
1567 thd_lock_released[THD5_SU].wait_for_notification();
1570 thd_lock_grabbed[THD6_SNRW].wait_for_notification();
1571 thd_release_locks[THD6_SNRW].notify();
1572 thd_lock_released[THD6_SNRW].wait_for_notification();
1581 max_write_lock_count= org_max_write_lock_count;
1606 #if GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
1609 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
1612 const char *too_long_name=
1613 "0123456789012345678901234567890123456789012345678901234567890123"
1614 "0123456789012345678901234567890123456789012345678901234567890123"
1615 "0123456789012345678901234567890123456789012345678901234567890123"
1618 EXPECT_DEATH(
MDL_key key0(MDL_key::TABLE, too_long_name,
""),
1619 ".*Assertion.*strlen.*");
1620 EXPECT_DEATH(
MDL_key key1(MDL_key::TABLE,
"", too_long_name),
1621 ".*Assertion.*strlen.*");
1625 EXPECT_DEATH(key2.
mdl_key_init(MDL_key::TABLE, too_long_name,
""),
1626 ".*Assertion.*strlen.*");
1627 EXPECT_DEATH(key2.
mdl_key_init(MDL_key::TABLE,
"", too_long_name),
1628 ".*Assertion.*strlen.*");
1631 #endif // GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
1640 #if defined(DBUG_OFF)
1641 TEST_F(MDLKeyTest, TruncateTooLongNames)
1644 const char *too_long_name=
1645 "0123456789012345678901234567890123456789012345678901234567890123"
1646 "0123456789012345678901234567890123456789012345678901234567890123"
1647 "0123456789012345678901234567890123456789012345678901234567890123"
1650 MDL_key key(MDL_key::TABLE, too_long_name, too_long_name);
1652 const char *db_name= key.db_name();
1653 const char *
name= key.name();
1655 EXPECT_LE(strlen(db_name), (uint)NAME_LEN);
1656 EXPECT_TRUE(strncmp(db_name, too_long_name, NAME_LEN) == 0);
1657 EXPECT_LE(strlen(name), (uint)NAME_LEN);
1658 EXPECT_TRUE(strncmp(name, too_long_name, NAME_LEN) == 0);
1660 #endif // defined(DBUG_OFF)