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
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
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* 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
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/db/concurrency/lock_manager_defs.h"
#include "mongo/db/concurrency/lock_manager_test_help.h"
#include "mongo/db/service_context_test_fixture.h"
#include "mongo/unittest/death_test.h"
#include "mongo/unittest/unittest.h"
namespace mongo {
class LockManagerTest : public ServiceContextTest {};
TEST(ResourceId, Semantics) {
ResourceId resIdDb(RESOURCE_DATABASE, 324334234);
ASSERT(resIdDb.getType() == RESOURCE_DATABASE);
ASSERT(resIdDb.getHashId() == 324334234);
ResourceId resIdColl(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
ASSERT(resIdColl.getType() == RESOURCE_COLLECTION);
// Comparison functions
// Make sure the operator < is defined.
ASSERT(resIdDb < resIdColl || resIdColl < resIdDb);
ResourceId resId(RESOURCE_DATABASE, 324334234);
ASSERT_EQUALS(resIdDb, resId);
// Assignment functions
resId = resIdColl;
ASSERT_EQUALS(resId, resIdColl);
}
TEST(ResourceId, Masking) {
const uint64_t maxHash = (1ULL << 61) - 1; // Only 61 bits usable for hash
ResourceType resources[3] = {RESOURCE_GLOBAL, RESOURCE_COLLECTION, RESOURCE_METADATA};
uint64_t hashes[3] = {maxHash, maxHash / 3, maxHash / 3 * 2};
// The test below verifies that types/hashes are stored/retrieved unchanged
for (int h = 0; h < 3; h++) {
for (int r = 0; r < 3; r++) {
ResourceId id(resources[r], hashes[h]);
ASSERT_EQUALS(id.getHashId(), hashes[h]);
ASSERT_EQUALS(id.getType(), resources[r]);
}
}
}
class ResourceIdTest : public unittest::Test {};
DEATH_TEST_F(ResourceIdTest, StringConstructorMustNotBeCollection, "invariant") {
ResourceId(RESOURCE_COLLECTION, "TestDB.collection");
}
DEATH_TEST_F(ResourceIdTest, StringConstructorMustNotBeDatabase, "invariant") {
ResourceId(RESOURCE_DATABASE, "TestDB");
}
DEATH_TEST_F(ResourceIdTest, CantCreateResourceMutexDirectly, "invariant") {
ResourceId(RESOURCE_MUTEX, "TestDB");
}
//
// LockManager
//
TEST_F(LockManagerTest, Grant) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
TrackingLockGrantNotification notify;
LockRequest request;
request.initNew(&locker, ¬ify);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request, MODE_S));
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 1);
ASSERT(notify.numNotifies == 0);
lockMgr.unlock(&request);
ASSERT(request.recursiveCount == 0);
}
TEST_F(LockManagerTest, GrantMultipleNoConflict) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
TrackingLockGrantNotification notify;
LockRequest request[6];
for (int i = 0; i < 6; i++) {
request[i].initNew(&locker, ¬ify);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request[i], MODE_S));
ASSERT(request[i].mode == MODE_S);
ASSERT(request[i].recursiveCount == 1);
}
ASSERT(notify.numNotifies == 0);
// Free the first
lockMgr.unlock(&request[0]);
// Free the last
lockMgr.unlock(&request[5]);
// Free one in the middle
lockMgr.unlock(&request[3]);
// Free the remaining so the LockMgr does not compain about leaked locks
lockMgr.unlock(&request[1]);
lockMgr.unlock(&request[2]);
lockMgr.unlock(&request[4]);
}
TEST_F(LockManagerTest, GrantMultipleFIFOOrder) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
std::unique_ptr<LockerImpl> locker[6];
for (int i = 0; i < 6; i++) {
locker[i].reset(new LockerImpl(getServiceContext()));
}
TrackingLockGrantNotification notify[6];
LockRequest request[6];
for (int i = 0; i < 6; i++) {
request[i].initNew(locker[i].get(), ¬ify[i]);
lockMgr.lock(resId, &request[i], MODE_X);
ASSERT(request[i].mode == MODE_X);
ASSERT(request[i].recursiveCount == 1);
}
// Release the last held lock and ensure the next one, based on time is granted
for (int i = 0; i < 5; i++) {
lockMgr.unlock(&request[i]);
ASSERT(notify[i + 1].numNotifies == 1);
ASSERT(notify[i + 1].lastResId == resId);
ASSERT(notify[i + 1].lastResult == LOCK_OK);
}
// Release the last one
lockMgr.unlock(&request[5]);
}
TEST_F(LockManagerTest, GrantRecursive) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
LockRequestCombo request(&locker);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request, MODE_S));
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 1);
ASSERT(request.numNotifies == 0);
// Acquire again, in the same mode
ASSERT(LOCK_OK == lockMgr.convert(resId, &request, MODE_S));
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 2);
ASSERT(request.numNotifies == 0);
// Release first acquire
lockMgr.unlock(&request);
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 1);
// Release second acquire
lockMgr.unlock(&request);
ASSERT(request.recursiveCount == 0);
}
TEST_F(LockManagerTest, GrantRecursiveCompatibleConvertUp) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
LockRequestCombo request(&locker);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request, MODE_IS));
ASSERT(request.mode == MODE_IS);
ASSERT(request.recursiveCount == 1);
ASSERT(request.numNotifies == 0);
// Acquire again, in *compatible*, but stricter mode
ASSERT(LOCK_OK == lockMgr.convert(resId, &request, MODE_S));
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 2);
ASSERT(request.numNotifies == 0);
// Release the first acquire
lockMgr.unlock(&request);
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 1);
// Release the second acquire
lockMgr.unlock(&request);
ASSERT(request.recursiveCount == 0);
}
TEST_F(LockManagerTest, GrantRecursiveNonCompatibleConvertUp) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
LockRequestCombo request(&locker);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request, MODE_S));
ASSERT(request.mode == MODE_S);
ASSERT(request.recursiveCount == 1);
ASSERT(request.numNotifies == 0);
// Acquire again, in *non-compatible*, but stricter mode
ASSERT(LOCK_OK == lockMgr.convert(resId, &request, MODE_X));
ASSERT(request.mode == MODE_X);
ASSERT(request.recursiveCount == 2);
ASSERT(request.numNotifies == 0);
// Release first acquire
lockMgr.unlock(&request);
ASSERT(request.mode == MODE_X);
ASSERT(request.recursiveCount == 1);
// Release second acquire
lockMgr.unlock(&request);
ASSERT(request.recursiveCount == 0);
}
TEST_F(LockManagerTest, GrantRecursiveNonCompatibleConvertDown) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
LockRequestCombo request(&locker);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request, MODE_X));
ASSERT(request.mode == MODE_X);
ASSERT(request.recursiveCount == 1);
ASSERT(request.numNotifies == 0);
// Acquire again, in *non-compatible*, but less strict mode
ASSERT(LOCK_OK == lockMgr.convert(resId, &request, MODE_S));
ASSERT(request.mode == MODE_X);
ASSERT(request.recursiveCount == 2);
ASSERT(request.numNotifies == 0);
// Release first acquire
lockMgr.unlock(&request);
ASSERT(request.mode == MODE_X);
ASSERT(request.recursiveCount == 1);
// Release second acquire
lockMgr.unlock(&request);
ASSERT(request.recursiveCount == 0);
}
TEST_F(LockManagerTest, Conflict) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockerImpl locker2(getServiceContext());
LockRequestCombo request1(&locker1);
LockRequestCombo request2(&locker2);
// First request granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_S));
ASSERT(request1.recursiveCount == 1);
ASSERT(request1.numNotifies == 0);
// Second request must block
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request2, MODE_X));
ASSERT(request2.mode == MODE_X);
ASSERT(request2.recursiveCount == 1);
ASSERT(request2.numNotifies == 0);
// Release first request
lockMgr.unlock(&request1);
ASSERT(request1.recursiveCount == 0);
ASSERT(request1.numNotifies == 0);
ASSERT(request2.mode == MODE_X);
ASSERT(request2.recursiveCount == 1);
ASSERT(request2.numNotifies == 1);
ASSERT(request2.lastResult == LOCK_OK);
// Release second acquire
lockMgr.unlock(&request2);
ASSERT(request2.recursiveCount == 0);
ASSERT(request1.numNotifies == 0);
ASSERT(request2.numNotifies == 1);
}
TEST_F(LockManagerTest, MultipleConflict) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
TrackingLockGrantNotification notify;
LockRequest request[6];
for (int i = 0; i < 6; i++) {
request[i].initNew(&locker, ¬ify);
if (i == 0) {
ASSERT(LOCK_OK == lockMgr.lock(resId, &request[i], MODE_X));
} else {
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request[i], MODE_X));
}
ASSERT(request[i].mode == MODE_X);
ASSERT(request[i].recursiveCount == 1);
}
ASSERT(notify.numNotifies == 0);
// Free them one by one and make sure they get granted in the correct order
for (int i = 0; i < 6; i++) {
lockMgr.unlock(&request[i]);
if (i < 5) {
ASSERT(notify.numNotifies == i + 1);
}
}
}
TEST_F(LockManagerTest, ConflictCancelWaiting) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
TrackingLockGrantNotification notify1;
LockerImpl locker2(getServiceContext());
TrackingLockGrantNotification notify2;
LockRequest request1;
request1.initNew(&locker1, ¬ify1);
LockRequest request2;
request2.initNew(&locker2, ¬ify2);
// First request granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_S));
ASSERT(notify1.numNotifies == 0);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request2, MODE_X));
// Release second request (which is still in the WAITING mode)
lockMgr.unlock(&request2);
ASSERT(notify2.numNotifies == 0);
ASSERT(request1.mode == MODE_S);
ASSERT(request1.recursiveCount == 1);
// Release second acquire
lockMgr.unlock(&request1);
}
TEST_F(LockManagerTest, ConflictCancelMultipleWaiting) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
TrackingLockGrantNotification notify;
LockRequest request[6];
for (int i = 0; i < 6; i++) {
request[i].initNew(&locker, ¬ify);
lockMgr.lock(resId, &request[i], MODE_X);
ASSERT(request[i].mode == MODE_X);
ASSERT(request[i].recursiveCount == 1);
}
ASSERT(notify.numNotifies == 0);
// Free the second (waiting)
lockMgr.unlock(&request[1]);
// Free the last
lockMgr.unlock(&request[5]);
// Free one in the middle
lockMgr.unlock(&request[3]);
// Free the remaining so the LockMgr does not compain about leaked locks
lockMgr.unlock(&request[2]);
lockMgr.unlock(&request[4]);
lockMgr.unlock(&request[0]);
}
TEST_F(LockManagerTest, CancelWaitingConversionWeakModes) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockerImpl locker2(getServiceContext());
LockRequestCombo request1(&locker1);
LockRequestCombo request2(&locker2);
// First request granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_IS));
ASSERT(request1.numNotifies == 0);
// Second request is granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request2, MODE_IX));
ASSERT(request2.numNotifies == 0);
// Convert first request to conflicting
ASSERT(LOCK_WAITING == lockMgr.convert(resId, &request1, MODE_S));
ASSERT(request1.mode == MODE_IS);
ASSERT(request1.convertMode == MODE_S);
ASSERT(request1.numNotifies == 0);
// Cancel the conflicting conversion
lockMgr.unlock(&request1);
ASSERT(request1.mode == MODE_IS);
ASSERT(request1.convertMode == MODE_NONE);
ASSERT(request1.numNotifies == 0);
// Free the remaining locks so the LockManager destructor does not complain
lockMgr.unlock(&request1);
lockMgr.unlock(&request2);
}
TEST_F(LockManagerTest, CancelWaitingConversionStrongModes) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockerImpl locker2(getServiceContext());
LockRequestCombo request1(&locker1);
LockRequestCombo request2(&locker2);
// First request granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_S));
ASSERT(request1.numNotifies == 0);
// Second request is granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request2, MODE_S));
ASSERT(request2.numNotifies == 0);
// Convert second request to conflicting
ASSERT(LOCK_WAITING == lockMgr.convert(resId, &request2, MODE_X));
ASSERT(request2.mode == MODE_S);
ASSERT(request2.convertMode == MODE_X);
ASSERT(request2.numNotifies == 0);
// Cancel the conflicting upgrade
lockMgr.unlock(&request2);
ASSERT(request2.mode == MODE_S);
ASSERT(request2.convertMode == MODE_NONE);
ASSERT(request2.numNotifies == 0);
// Free the remaining locks so the LockManager destructor does not complain
lockMgr.unlock(&request1);
lockMgr.unlock(&request2);
}
TEST_F(LockManagerTest, ConflictingConversion) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockerImpl locker2(getServiceContext());
LockRequestCombo request1(&locker1);
LockRequestCombo request2(&locker2);
// The S requests are granted right away
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_S));
ASSERT(request1.numNotifies == 0);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request2, MODE_S));
ASSERT(request2.numNotifies == 0);
// Convert first request to conflicting
ASSERT(LOCK_WAITING == lockMgr.convert(resId, &request1, MODE_X));
ASSERT(request1.numNotifies == 0);
// Free the second lock and make sure the first is granted
lockMgr.unlock(&request2);
ASSERT(request1.mode == MODE_X);
ASSERT(request1.numNotifies == 1);
ASSERT(request2.numNotifies == 0);
// Frees the first reference, mode remains X
lockMgr.unlock(&request1);
ASSERT(request1.mode == MODE_X);
ASSERT(request1.recursiveCount == 1);
lockMgr.unlock(&request1);
}
TEST_F(LockManagerTest, ConflictingConversionInTheMiddle) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker(getServiceContext());
TrackingLockGrantNotification notify;
LockRequest request[3];
for (int i = 0; i < 3; i++) {
request[i].initNew(&locker, ¬ify);
lockMgr.lock(resId, &request[i], MODE_S);
}
// Upgrade the one in the middle (not the first one)
ASSERT(LOCK_WAITING == lockMgr.convert(resId, &request[1], MODE_X));
ASSERT(notify.numNotifies == 0);
// Release the two shared modes
lockMgr.unlock(&request[0]);
ASSERT(notify.numNotifies == 0);
lockMgr.unlock(&request[2]);
ASSERT(notify.numNotifies == 1);
ASSERT(request[1].mode == MODE_X);
// Request 1 should be unlocked twice
lockMgr.unlock(&request[1]);
lockMgr.unlock(&request[1]);
}
TEST_F(LockManagerTest, ConvertUpgrade) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockRequestCombo request1(&locker1);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_S));
LockerImpl locker2(getServiceContext());
LockRequestCombo request2(&locker2);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request2, MODE_S));
// Upgrade the S lock to X
ASSERT(LOCK_WAITING == lockMgr.convert(resId, &request1, MODE_X));
ASSERT(!lockMgr.unlock(&request1));
ASSERT(lockMgr.unlock(&request1));
ASSERT(lockMgr.unlock(&request2));
}
TEST_F(LockManagerTest, Downgrade) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl locker1(getServiceContext());
LockRequestCombo request1(&locker1);
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_X));
LockerImpl locker2(getServiceContext());
LockRequestCombo request2(&locker2);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request2, MODE_S));
// Downgrade the X request to S
lockMgr.downgrade(&request1, MODE_S);
ASSERT(request2.numNotifies == 1);
ASSERT(request2.lastResult == LOCK_OK);
ASSERT(request2.recursiveCount == 1);
ASSERT(lockMgr.unlock(&request1));
ASSERT(lockMgr.unlock(&request2));
}
// Lock conflict matrix tests
static void checkConflict(ServiceContext* serviceContext,
LockMode existingMode,
LockMode newMode,
bool hasConflict) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl lockerExisting(serviceContext);
TrackingLockGrantNotification notifyExisting;
LockRequest requestExisting;
requestExisting.initNew(&lockerExisting, ¬ifyExisting);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestExisting, existingMode));
LockerImpl lockerNew(serviceContext);
TrackingLockGrantNotification notifyNew;
LockRequest requestNew;
requestNew.initNew(&lockerNew, ¬ifyNew);
LockResult result = lockMgr.lock(resId, &requestNew, newMode);
if (hasConflict) {
ASSERT_EQUALS(LOCK_WAITING, result);
} else {
ASSERT_EQUALS(LOCK_OK, result);
}
lockMgr.unlock(&requestNew);
lockMgr.unlock(&requestExisting);
}
TEST_F(LockManagerTest, ValidateConflictMatrix) {
checkConflict(getServiceContext(), MODE_IS, MODE_IS, false);
checkConflict(getServiceContext(), MODE_IS, MODE_IX, false);
checkConflict(getServiceContext(), MODE_IS, MODE_S, false);
checkConflict(getServiceContext(), MODE_IS, MODE_X, true);
checkConflict(getServiceContext(), MODE_IX, MODE_IS, false);
checkConflict(getServiceContext(), MODE_IX, MODE_IX, false);
checkConflict(getServiceContext(), MODE_IX, MODE_S, true);
checkConflict(getServiceContext(), MODE_IX, MODE_X, true);
checkConflict(getServiceContext(), MODE_S, MODE_IS, false);
checkConflict(getServiceContext(), MODE_S, MODE_IX, true);
checkConflict(getServiceContext(), MODE_S, MODE_S, false);
checkConflict(getServiceContext(), MODE_S, MODE_X, true);
checkConflict(getServiceContext(), MODE_X, MODE_IS, true);
checkConflict(getServiceContext(), MODE_X, MODE_IX, true);
checkConflict(getServiceContext(), MODE_X, MODE_S, true);
checkConflict(getServiceContext(), MODE_X, MODE_X, true);
}
TEST_F(LockManagerTest, EnqueueAtFront) {
LockManager lockMgr;
const ResourceId resId(
RESOURCE_COLLECTION,
NamespaceString::createNamespaceString_forTest(boost::none, "TestDB.collection"));
LockerImpl lockerX(getServiceContext());
LockRequestCombo requestX(&lockerX);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestX, MODE_X));
// The subsequent request will block
LockerImpl lockerLow(getServiceContext());
LockRequestCombo requestLow(&lockerLow);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestLow, MODE_X));
// This is a "queue jumping request", which will go before locker 2 above
LockerImpl lockerHi(getServiceContext());
LockRequestCombo requestHi(&lockerHi);
requestHi.enqueueAtFront = true;
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestHi, MODE_X));
// Once the X request is gone, lockerHi should be granted, because it's queue jumping
ASSERT(lockMgr.unlock(&requestX));
ASSERT(requestHi.lastResId == resId);
ASSERT(requestHi.lastResult == LOCK_OK);
// Finally lockerLow should be granted
ASSERT(lockMgr.unlock(&requestHi));
ASSERT(requestLow.lastResId == resId);
ASSERT(requestLow.lastResult == LOCK_OK);
// This avoids the lock manager asserting on leaked locks
ASSERT(lockMgr.unlock(&requestLow));
}
TEST_F(LockManagerTest, CompatibleFirstImmediateGrant) {
LockManager lockMgr;
const ResourceId resId(RESOURCE_GLOBAL, 0);
LockerImpl locker1(getServiceContext());
LockRequestCombo request1(&locker1);
LockerImpl locker2(getServiceContext());
LockRequestCombo request2(&locker2);
request2.compatibleFirst = true;
LockerImpl locker3(getServiceContext());
LockRequestCombo request3(&locker3);
// Lock all in IS mode
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_IS));
ASSERT(LOCK_OK == lockMgr.lock(resId, &request2, MODE_IS));
ASSERT(LOCK_OK == lockMgr.lock(resId, &request3, MODE_IS));
// Now an exclusive mode comes, which would block
LockerImpl lockerX(getServiceContext());
LockRequestCombo requestX(&lockerX);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestX, MODE_X));
// If an S comes, it should be granted, because of request2
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// If request1 goes away, the policy should still be compatible-first, because of request2
ASSERT(lockMgr.unlock(&request1));
// If S comes again, it should be granted, because of request2 still there
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// With request2 gone the policy should go back to FIFO, even though request3 is active
ASSERT(lockMgr.unlock(&request2));
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// Unlock request3 to keep the lock mgr not assert for leaked locks
ASSERT(lockMgr.unlock(&request3));
ASSERT(lockMgr.unlock(&requestX));
}
TEST_F(LockManagerTest, CompatibleFirstGrantAlreadyQueued) {
LockManager lockMgr;
const ResourceId resId(RESOURCE_GLOBAL, 0);
// This tests the following behaviors (alternatives indicated with '|'):
// Lock held in X, queue: S X|IX IS, where S is compatibleFirst.
// Once X unlocks|downgrades both the S and IS requests should proceed.
enum UnblockMethod { kDowngrading, kUnlocking };
LockMode conflictingModes[2] = {MODE_IX, MODE_X};
UnblockMethod unblockMethods[2] = {kDowngrading, kUnlocking};
for (LockMode writerMode : conflictingModes) {
for (UnblockMethod unblockMethod : unblockMethods) {
LockerImpl locker1(getServiceContext());
LockRequestCombo request1(&locker1);
LockerImpl locker2(getServiceContext());
LockRequestCombo request2(&locker2);
request2.compatibleFirst = true;
LockerImpl locker3(getServiceContext());
LockRequestCombo request3(&locker3);
LockerImpl locker4(getServiceContext());
LockRequestCombo request4(&locker4);
// Hold the lock in X and establish the S IX|X IS queue.
ASSERT(LOCK_OK == lockMgr.lock(resId, &request1, MODE_X));
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request2, MODE_S));
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request3, writerMode));
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request4, MODE_IS));
// Now unlock the initial X, so all readers should be able to proceed, while the writer
// remains queued.
if (unblockMethod == kUnlocking) {
ASSERT(lockMgr.unlock(&request1));
} else {
invariant(unblockMethod == kDowngrading);
lockMgr.downgrade(&request1, MODE_S);
}
ASSERT(request2.lastResult == LOCK_OK);
ASSERT(request3.lastResult == LOCK_INVALID);
ASSERT(request4.lastResult == LOCK_OK);
// Now unlock the readers, and the writer succeeds as well.
ASSERT(lockMgr.unlock(&request2));
ASSERT(lockMgr.unlock(&request4));
if (unblockMethod == kDowngrading) {
ASSERT(lockMgr.unlock(&request1));
}
ASSERT(request3.lastResult == LOCK_OK);
// Unlock the writer
ASSERT(lockMgr.unlock(&request3));
}
}
}
TEST_F(LockManagerTest, CompatibleFirstDelayedGrant) {
LockManager lockMgr;
const ResourceId resId(RESOURCE_GLOBAL, 0);
LockerImpl lockerXInitial(getServiceContext());
LockRequestCombo requestXInitial(&lockerXInitial);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestXInitial, MODE_X));
LockerImpl locker1(getServiceContext());
LockRequestCombo request1(&locker1);
LockerImpl locker2(getServiceContext());
LockRequestCombo request2(&locker2);
request2.compatibleFirst = true;
LockerImpl locker3(getServiceContext());
LockRequestCombo request3(&locker3);
// Lock all in IS mode (should block behind the global lock)
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request1, MODE_IS));
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request2, MODE_IS));
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &request3, MODE_IS));
// Now an exclusive mode comes, which would block behind the IS modes
LockerImpl lockerX(getServiceContext());
LockRequestCombo requestX(&lockerX);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestX, MODE_X));
// Free the first X lock so all IS modes are granted
ASSERT(lockMgr.unlock(&requestXInitial));
ASSERT(request1.lastResult == LOCK_OK);
ASSERT(request2.lastResult == LOCK_OK);
ASSERT(request3.lastResult == LOCK_OK);
// If an S comes, it should be granted, because of request2
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// If request1 goes away, the policy should still be compatible-first, because of request2
ASSERT(lockMgr.unlock(&request1));
// If S comes again, it should be granted, because of request2 still there
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// With request2 gone the policy should go back to FIFO, even though request3 is active
ASSERT(lockMgr.unlock(&request2));
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// Unlock request3 to keep the lock mgr not assert for leaked locks
ASSERT(lockMgr.unlock(&request3));
ASSERT(lockMgr.unlock(&requestX));
}
TEST_F(LockManagerTest, CompatibleFirstCancelWaiting) {
LockManager lockMgr;
const ResourceId resId(RESOURCE_GLOBAL, 0);
LockerImpl lockerSInitial(getServiceContext());
LockRequestCombo requestSInitial(&lockerSInitial);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestSInitial, MODE_S));
LockerImpl lockerX(getServiceContext());
LockRequestCombo requestX(&lockerX);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestX, MODE_X));
LockerImpl lockerPending(getServiceContext());
LockRequestCombo requestPending(&lockerPending);
requestPending.compatibleFirst = true;
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestPending, MODE_S));
// S1 is not granted yet, so the policy should still be FIFO
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// Unlock S1, the policy should still be FIFO
ASSERT(lockMgr.unlock(&requestPending));
{
LockerImpl lockerS(getServiceContext());
LockRequestCombo requestS(&lockerS);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestS, MODE_S));
ASSERT(lockMgr.unlock(&requestS));
}
// Unlock remaining locks to keep the leak detection logic happy
ASSERT(lockMgr.unlock(&requestSInitial));
ASSERT(lockMgr.unlock(&requestX));
}
TEST_F(LockManagerTest, Fairness) {
LockManager lockMgr;
const ResourceId resId(RESOURCE_GLOBAL, 0);
// Start with some 'regular' intent locks
LockerImpl lockerIS(getServiceContext());
LockRequestCombo requestIS(&lockerIS);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestIS, MODE_IS));
LockerImpl lockerIX(getServiceContext());
LockRequestCombo requestIX(&lockerIX);
ASSERT(LOCK_OK == lockMgr.lock(resId, &requestIX, MODE_IX));
// Now a conflicting lock comes
LockerImpl lockerX(getServiceContext());
LockRequestCombo requestX(&lockerX);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestX, MODE_X));
// Now, whoever comes next should be blocked
LockerImpl lockerIX1(getServiceContext());
LockRequestCombo requestIX1(&lockerIX1);
ASSERT(LOCK_WAITING == lockMgr.lock(resId, &requestIX1, MODE_IX));
// Freeing the first two locks should grant the X lock
ASSERT(lockMgr.unlock(&requestIS));
ASSERT(lockMgr.unlock(&requestIX));
ASSERT_EQ(LOCK_OK, requestX.lastResult);
ASSERT_EQ(1, requestX.numNotifies);
ASSERT_EQ(LOCK_INVALID, requestIX1.lastResult);
ASSERT_EQ(0, requestIX1.numNotifies);
ASSERT(lockMgr.unlock(&requestX));
ASSERT_EQ(LOCK_OK, requestIX1.lastResult);
ASSERT_EQ(1, requestIX1.numNotifies);
// Unlock all locks so we don't assert for leaked locks
ASSERT(lockMgr.unlock(&requestIX1));
}
TEST_F(LockManagerTest, HasConflictingRequests) {
LockManager lockMgr;
ResourceId resId{RESOURCE_GLOBAL, 0};
LockerImpl lockerIX{getServiceContext()};
LockRequestCombo requestIX{&lockerIX};
ASSERT_EQ(lockMgr.lock(resId, &requestIX, LockMode::MODE_IX), LockResult::LOCK_OK);
ASSERT_FALSE(lockMgr.hasConflictingRequests(resId, &requestIX));
LockerImpl lockerX{getServiceContext()};
LockRequestCombo requestX{&lockerX};
ASSERT_EQ(lockMgr.lock(resId, &requestX, LockMode::MODE_X), LockResult::LOCK_WAITING);
ASSERT_TRUE(lockMgr.hasConflictingRequests(resId, &requestIX));
ASSERT_TRUE(lockMgr.hasConflictingRequests(resId, &requestX));
ASSERT(lockMgr.unlock(&requestIX));
ASSERT(lockMgr.unlock(&requestX));
}
} // namespace mongo
|