forked from MahletNigusse/write-after-read-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdl-t.cc
4643 lines (3856 loc) · 148 KB
/
mdl-t.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
/* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
/**
This is a unit test for the 'meta data locking' classes.
It is written to illustrate how we can use Google Test for unit testing
of MySQL code.
For documentation on Google Test, see http://code.google.com/p/googletest/
and the contained wiki pages GoogleTestPrimer and GoogleTestAdvancedGuide.
The code below should hopefully be (mostly) self-explanatory.
*/
// First include (the generated) my_config.h, to get correct platform defines.
#include "my_config.h"
#include <gtest/gtest.h>
#include "mdl.h"
#include <mysqld_error.h>
#include "thr_malloc.h"
#include "thread_utils.h"
#include "test_mdl_context_owner.h"
/*
Mock thd_wait_begin/end functions
*/
extern "C" void thd_wait_begin(THD *thd, int wait_type)
{
}
extern "C" void thd_wait_end(THD *thd)
{
}
/*
A mock error handler.
*/
static uint expected_error= 0;
extern "C" void test_error_handler_hook(uint err, const char *str, myf MyFlags)
{
EXPECT_EQ(expected_error, err) << str;
}
/*
Mock away this global function.
We don't need DEBUG_SYNC functionality in a unit test.
*/
void debug_sync(THD *thd, const char *sync_point_name, size_t name_len)
{
DBUG_PRINT("debug_sync_point", ("hit: '%s'", sync_point_name));
FAIL() << "Not yet implemented.";
}
/*
Putting everything in a namespace prevents any (unintentional)
name clashes with the code under test.
*/
namespace mdl_unittest {
using thread::Notification;
using thread::Thread;
const char db_name[]= "some_database";
const char table_name1[]= "some_table1";
const char table_name2[]= "some_table2";
const char table_name3[]= "some_table3";
const char table_name4[]= "some_table4";
const ulong zero_timeout= 0;
const ulong long_timeout= (ulong) 3600L*24L*365L;
class MDLTest : public ::testing::Test, public Test_MDL_context_owner
{
protected:
MDLTest()
: m_null_ticket(NULL),
m_null_request(NULL)
{
}
static void SetUpTestCase()
{
/* Save original and install our custom error hook. */
m_old_error_handler_hook= error_handler_hook;
error_handler_hook= test_error_handler_hook;
}
static void TearDownTestCase()
{
error_handler_hook= m_old_error_handler_hook;
}
void SetUp()
{
expected_error= 0;
mdl_locks_unused_locks_low_water= MDL_LOCKS_UNUSED_LOCKS_LOW_WATER_DEFAULT;
max_write_lock_count= ULONG_MAX;
mdl_init();
m_mdl_context.init(this);
EXPECT_FALSE(m_mdl_context.has_locks());
MDL_REQUEST_INIT(&m_global_request,
MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE,
MDL_TRANSACTION);
}
void TearDown()
{
m_mdl_context.destroy();
mdl_destroy();
}
virtual void notify_shared_lock(MDL_context_owner *in_use,
bool needs_thr_lock_abort)
{
in_use->notify_shared_lock(NULL, needs_thr_lock_abort);
}
// A utility member for testing single lock requests.
void test_one_simple_shared_lock(enum_mdl_type lock_type);
const MDL_ticket *m_null_ticket;
const MDL_request *m_null_request;
MDL_context m_mdl_context;
MDL_request m_request;
MDL_request m_global_request;
MDL_request_list m_request_list;
private:
GTEST_DISALLOW_COPY_AND_ASSIGN_(MDLTest);
static void (*m_old_error_handler_hook)(uint, const char *, myf);
};
void (*MDLTest::m_old_error_handler_hook)(uint, const char *, myf);
/*
Will grab a lock on table_name of given type in the run() function.
The two notifications are for synchronizing with the main thread.
Does *not* take ownership of the notifications.
*/
class MDL_thread : public Thread, public Test_MDL_context_owner
{
public:
MDL_thread(const char *table_name,
enum_mdl_type mdl_type,
Notification *lock_grabbed,
Notification *release_locks,
Notification *lock_blocked,
Notification *lock_released)
: m_table_name(table_name),
m_mdl_type(mdl_type),
m_lock_grabbed(lock_grabbed),
m_release_locks(release_locks),
m_lock_blocked(lock_blocked),
m_lock_released(lock_released),
m_enable_release_on_notify(false)
{
m_mdl_context.init(this);
}
~MDL_thread()
{
m_mdl_context.destroy();
}
virtual void run();
void enable_release_on_notify() { m_enable_release_on_notify= true; }
virtual void notify_shared_lock(MDL_context_owner *in_use,
bool needs_thr_lock_abort)
{
if (in_use)
in_use->notify_shared_lock(NULL, needs_thr_lock_abort);
else if (m_enable_release_on_notify && m_release_locks)
m_release_locks->notify();
}
virtual void enter_cond(mysql_cond_t *cond,
mysql_mutex_t* mutex,
const PSI_stage_info *stage,
PSI_stage_info *old_stage,
const char *src_function,
const char *src_file,
int src_line)
{
Test_MDL_context_owner::enter_cond(cond, mutex, stage, old_stage,
src_function, src_file, src_line);
/*
No extra checks needed here since MDL uses enter_con only when thread
is blocked.
*/
if (m_lock_blocked)
m_lock_blocked->notify();
return;
}
MDL_context& get_mdl_context()
{
return m_mdl_context;
}
private:
const char *m_table_name;
enum_mdl_type m_mdl_type;
Notification *m_lock_grabbed;
Notification *m_release_locks;
Notification *m_lock_blocked;
Notification *m_lock_released;
bool m_enable_release_on_notify;
MDL_context m_mdl_context;
};
void MDL_thread::run()
{
MDL_request request;
MDL_REQUEST_INIT(&request,
MDL_key::TABLE, db_name, m_table_name, m_mdl_type,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.acquire_lock(&request, long_timeout));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, m_table_name,
m_mdl_type));
// Tell the main thread that we have grabbed our locks.
if (m_lock_grabbed)
m_lock_grabbed->notify();
// Hold on to locks until we are told to release them
if (m_release_locks)
m_release_locks->wait_for_notification();
m_mdl_context.release_transactional_locks();
// Tell the main thread that grabbed lock is released.
if (m_lock_released)
m_lock_released->notify();
}
// Google Test recommends DeathTest suffix for classes use in death tests.
typedef MDLTest MDLDeathTest;
/*
Verifies that we die with a DBUG_ASSERT if we destry a non-empty MDL_context.
*/
#if GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
TEST_F(MDLDeathTest, DieWhenMTicketsNonempty)
{
::testing::FLAGS_gtest_death_test_style = "threadsafe";
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_DEATH(m_mdl_context.destroy(),
".*Assertion.*MDL_TRANSACTION.*is_empty.*");
m_mdl_context.release_transactional_locks();
}
#endif // GTEST_HAS_DEATH_TEST && !defined(DBUG_OFF)
/*
The most basic test: just construct and destruct our test fixture.
*/
TEST_F(MDLTest, ConstructAndDestruct)
{
}
void MDLTest::test_one_simple_shared_lock(enum_mdl_type lock_type)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, lock_type,
MDL_TRANSACTION);
EXPECT_EQ(lock_type, m_request.type);
EXPECT_EQ(m_null_ticket, m_request.ticket);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
EXPECT_TRUE(m_mdl_context.has_locks());
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
lock_type));
MDL_request request_2;
MDL_REQUEST_INIT_BY_KEY(&request_2, &m_request.key, lock_type, MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2));
EXPECT_EQ(m_request.ticket, request_2.ticket);
m_mdl_context.release_transactional_locks();
EXPECT_FALSE(m_mdl_context.has_locks());
}
/*
Acquires one lock of type MDL_SHARED.
*/
TEST_F(MDLTest, OneShared)
{
test_one_simple_shared_lock(MDL_SHARED);
}
/*
Acquires one lock of type MDL_SHARED_HIGH_PRIO.
*/
TEST_F(MDLTest, OneSharedHighPrio)
{
test_one_simple_shared_lock(MDL_SHARED_HIGH_PRIO);
}
/*
Acquires one lock of type MDL_SHARED_READ.
*/
TEST_F(MDLTest, OneSharedRead)
{
test_one_simple_shared_lock(MDL_SHARED_READ);
}
/*
Acquires one lock of type MDL_SHARED_WRITE.
*/
TEST_F(MDLTest, OneSharedWrite)
{
test_one_simple_shared_lock(MDL_SHARED_WRITE);
}
/*
Acquires one lock of type MDL_EXCLUSIVE.
*/
TEST_F(MDLTest, OneExclusive)
{
const enum_mdl_type lock_type= MDL_EXCLUSIVE;
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, lock_type,
MDL_TRANSACTION);
EXPECT_EQ(m_null_ticket, m_request.ticket);
m_request_list.push_front(&m_request);
m_request_list.push_front(&m_global_request);
EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
EXPECT_NE(m_null_ticket, m_request.ticket);
EXPECT_NE(m_null_ticket, m_global_request.ticket);
EXPECT_TRUE(m_mdl_context.has_locks());
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
lock_type));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::GLOBAL, "", "",
MDL_INTENTION_EXCLUSIVE));
EXPECT_TRUE(m_request.ticket->is_upgradable_or_exclusive());
m_mdl_context.release_transactional_locks();
EXPECT_FALSE(m_mdl_context.has_locks());
}
/*
Acquires two locks, on different tables, of type MDL_SHARED.
Verifies that they are independent.
*/
TEST_F(MDLTest, TwoShared)
{
MDL_request request_2;
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED, MDL_EXPLICIT);
MDL_REQUEST_INIT(&request_2,
MDL_key::TABLE, db_name, table_name2, MDL_SHARED, MDL_EXPLICIT);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2));
EXPECT_TRUE(m_mdl_context.has_locks());
ASSERT_NE(m_null_ticket, m_request.ticket);
ASSERT_NE(m_null_ticket, request_2.ticket);
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name2,
MDL_SHARED));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name3,
MDL_SHARED));
m_mdl_context.release_lock(m_request.ticket);
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.has_locks());
m_mdl_context.release_lock(request_2.ticket);
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name2,
MDL_SHARED));
EXPECT_FALSE(m_mdl_context.has_locks());
}
/*
Verifies that two different contexts can acquire a shared lock
on the same table.
*/
TEST_F(MDLTest, SharedLocksBetweenContexts)
{
MDL_context mdl_context2;
mdl_context2.init(this);
MDL_request request_2;
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
MDL_REQUEST_INIT(&request_2,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_FALSE(mdl_context2.try_acquire_lock(&request_2));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_TRUE(mdl_context2.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
m_mdl_context.release_transactional_locks();
mdl_context2.release_transactional_locks();
}
/*
Verifies that we can upgrade a shared lock to exclusive.
*/
TEST_F(MDLTest, UpgradeSharedUpgradable)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
MDL_TRANSACTION);
m_request_list.push_front(&m_request);
m_request_list.push_front(&m_global_request);
EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
EXPECT_FALSE(m_mdl_context.
upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type());
// Another upgrade should be a no-op.
EXPECT_FALSE(m_mdl_context.
upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
EXPECT_EQ(MDL_EXCLUSIVE, m_request.ticket->get_type());
m_mdl_context.release_transactional_locks();
}
/*
Verfies that locks are released when we roll back to a savepoint.
*/
TEST_F(MDLTest, SavePoint)
{
MDL_request request_2;
MDL_request request_3;
MDL_request request_4;
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
MDL_REQUEST_INIT(&request_2,
MDL_key::TABLE, db_name, table_name2, MDL_SHARED,
MDL_TRANSACTION);
MDL_REQUEST_INIT(&request_3,
MDL_key::TABLE, db_name, table_name3, MDL_SHARED,
MDL_TRANSACTION);
MDL_REQUEST_INIT(&request_4,
MDL_key::TABLE, db_name, table_name4, MDL_SHARED,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_2));
MDL_savepoint savepoint= m_mdl_context.mdl_savepoint();
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_3));
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&request_4));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name2,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name3,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name4,
MDL_SHARED));
m_mdl_context.rollback_to_savepoint(savepoint);
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name2,
MDL_SHARED));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name3,
MDL_SHARED));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name4,
MDL_SHARED));
m_mdl_context.release_transactional_locks();
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name2,
MDL_SHARED));
}
/*
Verifies that we can grab shared locks concurrently, in different threads.
*/
TEST_F(MDLTest, ConcurrentShared)
{
Notification lock_grabbed;
Notification release_locks;
MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed,
&release_locks, NULL, NULL);
mdl_thread.start();
lock_grabbed.wait_for_notification();
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED));
release_locks.notify();
mdl_thread.join();
m_mdl_context.release_transactional_locks();
}
/*
Verifies that we cannot grab an exclusive lock on something which
is locked with a shared lock in a different thread.
*/
TEST_F(MDLTest, ConcurrentSharedExclusive)
{
expected_error= ER_LOCK_WAIT_TIMEOUT;
Notification lock_grabbed;
Notification release_locks;
MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks,
NULL, NULL);
mdl_thread.start();
lock_grabbed.wait_for_notification();
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_EXCLUSIVE,
MDL_TRANSACTION);
m_request_list.push_front(&m_request);
m_request_list.push_front(&m_global_request);
// We should *not* be able to grab the lock here.
EXPECT_TRUE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_EXCLUSIVE));
release_locks.notify();
mdl_thread.join();
// Now we should be able to grab the lock.
EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, zero_timeout));
EXPECT_NE(m_null_ticket, m_request.ticket);
m_mdl_context.release_transactional_locks();
}
/*
Verifies that we cannot we cannot grab a shared lock on something which
is locked exlusively in a different thread.
*/
TEST_F(MDLTest, ConcurrentExclusiveShared)
{
Notification lock_grabbed;
Notification release_locks;
MDL_thread mdl_thread(table_name1, MDL_EXCLUSIVE,
&lock_grabbed, &release_locks, NULL, NULL);
mdl_thread.start();
lock_grabbed.wait_for_notification();
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED,
MDL_TRANSACTION);
// We should *not* be able to grab the lock here.
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);
release_locks.notify();
// The other thread should eventually release its locks.
EXPECT_FALSE(m_mdl_context.acquire_lock(&m_request, long_timeout));
EXPECT_NE(m_null_ticket, m_request.ticket);
mdl_thread.join();
m_mdl_context.release_transactional_locks();
}
/*
Verifies the following scenario:
Thread 1: grabs a shared upgradable lock.
Thread 2: grabs a shared lock.
Thread 1: asks for an upgrade to exclusive (needs to wait for thread 2)
Thread 2: gets notified, and releases lock.
Thread 1: gets the exclusive lock.
*/
TEST_F(MDLTest, ConcurrentUpgrade)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
MDL_TRANSACTION);
m_request_list.push_front(&m_request);
m_request_list.push_front(&m_global_request);
EXPECT_FALSE(m_mdl_context.acquire_locks(&m_request_list, long_timeout));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_SHARED_UPGRADABLE));
EXPECT_FALSE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_EXCLUSIVE));
Notification lock_grabbed;
Notification release_locks;
MDL_thread mdl_thread(table_name1, MDL_SHARED, &lock_grabbed, &release_locks,
NULL, NULL);
mdl_thread.enable_release_on_notify();
mdl_thread.start();
lock_grabbed.wait_for_notification();
EXPECT_FALSE(m_mdl_context.
upgrade_shared_lock(m_request.ticket, MDL_EXCLUSIVE, long_timeout));
EXPECT_TRUE(m_mdl_context.
owns_equal_or_stronger_lock(MDL_key::TABLE, db_name, table_name1,
MDL_EXCLUSIVE));
mdl_thread.join();
m_mdl_context.release_transactional_locks();
}
TEST_F(MDLTest, UpgradableConcurrency)
{
MDL_request request_2;
MDL_request_list request_list;
Notification lock_grabbed;
Notification release_locks;
MDL_thread mdl_thread(table_name1, MDL_SHARED_UPGRADABLE,
&lock_grabbed, &release_locks, NULL, NULL);
mdl_thread.start();
lock_grabbed.wait_for_notification();
// We should be able to take a SW lock.
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_WRITE,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
// But SHARED_UPGRADABLE is not compatible with itself
expected_error= ER_LOCK_WAIT_TIMEOUT;
MDL_REQUEST_INIT(&request_2,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_UPGRADABLE,
MDL_TRANSACTION);
request_list.push_front(&m_global_request);
request_list.push_front(&request_2);
EXPECT_TRUE(m_mdl_context.acquire_locks(&request_list, zero_timeout));
EXPECT_EQ(m_null_ticket, request_2.ticket);
release_locks.notify();
mdl_thread.join();
m_mdl_context.release_transactional_locks();
}
/*
Test compatibility matrice for MDL_SHARED_WRITE_LOW_PRIO lock.
*/
TEST_F(MDLTest, SharedWriteLowPrioCompatibility)
{
enum_mdl_type compatible[]= { MDL_SHARED, MDL_SHARED_HIGH_PRIO,
MDL_SHARED_READ, MDL_SHARED_WRITE,
MDL_SHARED_WRITE_LOW_PRIO,
MDL_SHARED_UPGRADABLE };
enum_mdl_type incompatible[]= { MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE,
MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE };
enum_mdl_type higher_prio[]= { MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE,
MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE };
Notification lock_grabbed;
Notification release_lock;
MDL_thread mdl_thread(table_name1, MDL_SHARED_WRITE_LOW_PRIO,
&lock_grabbed, &release_lock, NULL, NULL);
uint i;
// Start thread which will acquire SWLP lock and pause.
mdl_thread.start();
lock_grabbed.wait_for_notification();
// We should be able to take all locks from compatible list
for (i= 0; i < sizeof(compatible)/sizeof(enum_mdl_type); ++i)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, compatible[i],
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
m_mdl_context.release_transactional_locks();
}
// But none of the locks from incompatible list
for (i= 0; i < sizeof(incompatible)/sizeof(enum_mdl_type); ++i)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, incompatible[i],
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);
}
release_lock.notify();
mdl_thread.join();
// Check that SWLP lock can be acquired when any of compatible locks is active
for (i= 0; i < sizeof(compatible)/sizeof(enum_mdl_type); ++i)
{
Notification second_grabbed;
Notification second_release;
MDL_thread mdl_thread2(table_name1, compatible[i],
&second_grabbed, &second_release,
NULL, NULL);
// Start thread that will acquire one of locks from compatible list
mdl_thread2.start();
second_grabbed.wait_for_notification();
// Acquisition of SWLP should succeed
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1,
MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
m_mdl_context.release_transactional_locks();
second_release.notify();
mdl_thread2.join();
}
/*
Check that SWLP lock can't be acquired when any of incompatible locks
is active.
*/
for (i= 0; i < sizeof(incompatible)/sizeof(enum_mdl_type); ++i)
{
Notification third_grabbed;
Notification third_release;
MDL_thread mdl_thread3(table_name1, incompatible[i],
&third_grabbed, &third_release,
NULL, NULL);
// Start thread that will acquire one of locks from incompatible list
mdl_thread3.start();
third_grabbed.wait_for_notification();
// Acquisition of SWLP should fail.
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1,
MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);
third_release.notify();
mdl_thread3.join();
}
/*
Check that SWLP lock can't be acquired if one of higher-prio locks is
pending.
*/
for (i= 0; i < sizeof(higher_prio)/sizeof(enum_mdl_type); ++i)
{
Notification fourth_grabbed;
Notification fourth_release;
Notification fifth_blocked;
MDL_thread mdl_thread4(table_name1, MDL_SHARED_WRITE,
&fourth_grabbed, &fourth_release,
NULL, NULL);
MDL_thread mdl_thread5(table_name1, higher_prio[i],
NULL, NULL, &fifth_blocked, NULL);
// Acquire SW lock on the table.
mdl_thread4.start();
fourth_grabbed.wait_for_notification();
// Ensure that there is pending high-prio lock.
mdl_thread5.start();
fifth_blocked.wait_for_notification();
// Acquisition of SWLP should fail because there is pending high-prio lock.
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1,
MDL_SHARED_WRITE_LOW_PRIO, MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);
fourth_release.notify();
mdl_thread4.join();
mdl_thread5.join();
}
/*
Check that higher-prio locks can be acquired even if there
is pending SWLP lock.
*/
for (i= 0; i < sizeof(higher_prio)/sizeof(enum_mdl_type); ++i)
{
Notification sixth_grabbed;
Notification sixth_release;
Notification seventh_blocked;
Notification seventh_grabbed;
Notification eighth_blocked;
Notification eighth_release;
MDL_thread mdl_thread6(table_name1, MDL_EXCLUSIVE,
&sixth_grabbed, &sixth_release,
NULL, NULL);
MDL_thread mdl_thread7(table_name1, higher_prio[i],
&seventh_grabbed, NULL, &seventh_blocked, NULL);
MDL_thread mdl_thread8(table_name1, MDL_SHARED_WRITE_LOW_PRIO,
NULL, &eighth_release, &eighth_blocked, NULL);
// Acquire X lock on the table.
mdl_thread6.start();
sixth_grabbed.wait_for_notification();
// Ensure that there is pending high-prio lock.
mdl_thread7.start();
seventh_blocked.wait_for_notification();
// Ensure that there is pending SWLP after it.
mdl_thread8.start();
eighth_blocked.wait_for_notification();
// Release X lock.
sixth_release.notify();
mdl_thread6.join();
/*
This should unblock high-prio lock and not SWLP (otherwise we will
wait for SWLP release.
*/
seventh_grabbed.wait_for_notification();
mdl_thread7.join();
// After this SWLP lock will be granted and can be released
eighth_release.notify();
mdl_thread8.join();
}
}
/*
Test compatibility matrice for MDL_SHARED_READ_ONLY lock.
*/
TEST_F(MDLTest, SharedReadOnlyCompatibility)
{
enum_mdl_type compatible[]= { MDL_SHARED, MDL_SHARED_HIGH_PRIO,
MDL_SHARED_READ, MDL_SHARED_UPGRADABLE,
MDL_SHARED_READ_ONLY, MDL_SHARED_NO_WRITE};
enum_mdl_type incompatible[]= { MDL_SHARED_WRITE, MDL_SHARED_WRITE_LOW_PRIO,
MDL_SHARED_NO_READ_WRITE, MDL_EXCLUSIVE };
enum_mdl_type higher_prio[]= { MDL_SHARED_WRITE, MDL_SHARED_NO_READ_WRITE,
MDL_EXCLUSIVE };
Notification lock_grabbed;
Notification release_lock;
MDL_thread mdl_thread(table_name1, MDL_SHARED_READ_ONLY,
&lock_grabbed, &release_lock, NULL, NULL);
uint i;
// Start thread which will acquire SRO lock and pause.
mdl_thread.start();
lock_grabbed.wait_for_notification();
// We should be able to take all locks from compatible list
for (i= 0; i < sizeof(compatible)/sizeof(enum_mdl_type); ++i)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, compatible[i],
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
m_mdl_context.release_transactional_locks();
}
// But none of the locks from incompatible list
for (i= 0; i < sizeof(incompatible)/sizeof(enum_mdl_type); ++i)
{
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, incompatible[i],
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);
}
release_lock.notify();
mdl_thread.join();
// Check that SRO lock can be acquired when any of compatible locks is active
for (i= 0; i < sizeof(compatible)/sizeof(enum_mdl_type); ++i)
{
Notification second_grabbed;
Notification second_release;
MDL_thread mdl_thread2(table_name1, compatible[i],
&second_grabbed, &second_release,
NULL, NULL);
// Start thread that will acquire one of locks from compatible list
mdl_thread2.start();
second_grabbed.wait_for_notification();
// Acquisition of SRO should succeed
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_READ_ONLY,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_NE(m_null_ticket, m_request.ticket);
m_mdl_context.release_transactional_locks();
second_release.notify();
mdl_thread2.join();
}
/*
Check that SRO lock can't be acquired when any of incompatible locks
is active.
*/
for (i= 0; i < sizeof(incompatible)/sizeof(enum_mdl_type); ++i)
{
Notification third_grabbed;
Notification third_release;
MDL_thread mdl_thread3(table_name1, incompatible[i],
&third_grabbed, &third_release,
NULL, NULL);
// Start thread that will acquire one of locks from incompatible list
mdl_thread3.start();
third_grabbed.wait_for_notification();
// Acquisition of SRO should fail.
MDL_REQUEST_INIT(&m_request,
MDL_key::TABLE, db_name, table_name1, MDL_SHARED_READ_ONLY,
MDL_TRANSACTION);
EXPECT_FALSE(m_mdl_context.try_acquire_lock(&m_request));
EXPECT_EQ(m_null_ticket, m_request.ticket);