File: LockTest.java

package info (click to toggle)
libdb-je-java 3.3.98-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,052 kB
  • sloc: java: 153,077; xml: 2,034; makefile: 3
file content (1035 lines) | stat: -rw-r--r-- 42,124 bytes parent folder | download | duplicates (3)
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
1028
1029
1030
1031
1032
1033
1034
1035
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2002,2010 Oracle.  All rights reserved.
 *
 * $Id: LockTest.java,v 1.61.2.2 2010/01/04 15:30:48 cwl Exp $
 */

package com.sleepycat.je.txn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import junit.framework.TestCase;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.TransactionConfig;
import com.sleepycat.je.config.EnvironmentParams;
import com.sleepycat.je.dbi.EnvironmentImpl;
import com.sleepycat.je.dbi.MemoryBudget;
import com.sleepycat.je.log.ReplicationContext;
import com.sleepycat.je.util.TestUtils;

public class LockTest extends TestCase {
    private EnvironmentImpl envImpl;
    private File envHome;

    public LockTest() {
        envHome =  new File(System.getProperty(TestUtils.DEST_DIR));
    }

    public void setUp()
        throws DatabaseException {

        EnvironmentConfig envConfig = TestUtils.initEnvConfig();
        envConfig.setConfigParam(EnvironmentParams.NODE_MAX.getName(), "6");
        envConfig.setAllowCreate(true);
        envImpl = new EnvironmentImpl(envHome,
                                      envConfig,
                                      null /*sharedCacheEnv*/,
                                      false /*replicationIntended*/);
    }

    public void tearDown()
        throws DatabaseException {

        envImpl.close();
    }

    public void testLockConflicts()
        throws Exception {

        Locker txn1 = BasicLocker.createBasicLocker(envImpl);
        Locker txn2 = BasicLocker.createBasicLocker(envImpl);
        Locker txn3 = BasicLocker.createBasicLocker(envImpl);

        MemoryBudget mb = envImpl.getMemoryBudget();
        try {

            /*
             * Start fresh. Ask for a read lock from txn1 twice,
             * should only be one owner. Then add multiple
             * would-be-writers as waiters.
             */
            Lock lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.EXISTING,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);

            /* txn1 has a READ lock. */
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());

            /* txn2 asks for a read lock, gets it. */
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);

            /* txn1 asks for WRITE, must wait */
            assertEquals(LockGrantType.WAIT_PROMOTION,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);

            /* txn2 write request must wait */
            assertEquals(LockGrantType.WAIT_PROMOTION,
                         lock.lock(LockType.WRITE, txn2, false, mb, 0).
                         lockGrant);

            /* Two read locks, two write waiters */
            assertEquals(2, lock.nOwners());
            assertEquals(2, lock.nWaiters());

            /*
             * Release txn1 read lock, which causes txn2's read lock to be
             * promoted to a write lock.
             */
            lock.release(txn1, mb, 0 /* lockTableIndex */);
            assertEquals(1, lock.nOwners());
            assertEquals(1, lock.nWaiters());

            /* Release txn2 write lock, now txn1 will get its write lock. */
            lock.release(txn2, mb, 0 /* lockTableIndex */);
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());

            /* Release txn1's write lock. */
            lock.release(txn1, mb, 0 /* lockTableIndex */);
            assertEquals(0, lock.nOwners());
            assertEquals(0, lock.nWaiters());

            /* Start fresh. Get a write lock, then get a read lock. */
            lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.EXISTING,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);

            /* Start fresh. Get a read lock, upgrade to a write lock. */
            lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.PROMOTION,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);

            /*
             * Start fresh. Get a read lock, then ask for a non-blocking
             * write lock. The latter should be denied.
             */
            lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.DENIED,
                         lock.lock(LockType.WRITE, txn2, true, mb, 0).
                         lockGrant);
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);

            /* Two write requests, should be one owner. */
            lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.EXISTING,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(1, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);

            /*
             * Ensure that a read request behind a write request that waits
             * also waits.  blocking requests.
             */
            lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);

            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.WRITE, txn2, false, mb, 0).
                         lockGrant);

            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn3, false, mb, 0).
                         lockGrant);

            assertEquals(1, lock.nOwners());
            assertEquals(2, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);
            lock.release(txn2, mb, 0 /* lockTableIndex */);
            lock.release(txn3, mb, 0 /* lockTableIndex */);

            /* Check non blocking requests */
            lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);

            /* Since non-blocking request, this fails and doesn't go
               on the wait queue. */
            assertEquals(LockGrantType.DENIED,
                         lock.lock(LockType.WRITE, txn2, true, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn3, true, mb, 0).
                         lockGrant);
            assertEquals(2, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);
            lock.release(txn3, mb, 0 /* lockTableIndex */);

            lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn3, false, mb, 0).
                         lockGrant);
            assertEquals(3, lock.nOwners());
            assertEquals(0, lock.nWaiters());
            lock.release(txn1, mb, 0 /* lockTableIndex */);
            lock.release(txn2, mb, 0 /* lockTableIndex */);
            lock.release(txn3, mb, 0 /* lockTableIndex */);
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
        }
    }

    public void testOwners()
        throws Exception {

        Locker txn1 = BasicLocker.createBasicLocker(envImpl);
        Locker txn2 = BasicLocker.createBasicLocker(envImpl);
        Locker txn3 = BasicLocker.createBasicLocker(envImpl);
        Locker txn4 = BasicLocker.createBasicLocker(envImpl);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            /*
             * Build up 3 owners and waiters for a lock, to test the
             * lazy initialization and optimization for single owner/waiter.
             */
            Lock lock = new LockImpl();
            /* should be no writer. */
            assertTrue(lock.getWriteOwnerLocker() == null);

            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn3, false, mb, 0).
                         lockGrant);

            /* should be no writer. */
            assertTrue(lock.getWriteOwnerLocker() == null);

            /* expect 3 owners, 0 waiters. */
            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.READ));
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            expectedOwners.add(new LockInfo(txn3, LockType.READ));
            checkOwners(expectedOwners, lock, 0);

            /* release the first locker. */
            lock.release(txn1, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            expectedOwners.add(new LockInfo(txn3, LockType.READ));
            checkOwners(expectedOwners, lock, 0);

            /* Add more. */
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn4, false, mb, 0).
                         lockGrant);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            expectedOwners.add(new LockInfo(txn3, LockType.READ));
            expectedOwners.add(new LockInfo(txn4, LockType.READ));
            checkOwners(expectedOwners, lock, 0);

            /* release */
            lock.release(txn2, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn3, LockType.READ));
            expectedOwners.add(new LockInfo(txn4, LockType.READ));
            checkOwners(expectedOwners, lock, 0);

            /* release */
            lock.release(txn3, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn4, LockType.READ));
            /* only 1 lock, in the owner set, but not a write owner. */
            assertTrue(lock.getWriteOwnerLocker() == null);

            /* release */
            lock.release(txn4, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            checkOwners(expectedOwners, lock, 0);

            /* Add owners again. */
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.READ));
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            checkOwners(expectedOwners, lock, 0);

            /* Release for the sake of the memory leak checking */
            lock.release(txn1, mb, 0);
            lock.release(txn2, mb, 0);

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
            txn4.operationEnd();
        }
    }

    public void testWaiters()
        throws Exception {

        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn2 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn3 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn4 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn5 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            /*
             * Build up 1 owners and 3waiters for a lock, to test the
             * lazy initialization and optimization for single owner/waiter.
             */
            Lock lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.WRITE, txn3, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.WRITE, txn4, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_PROMOTION,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);

            /* should be no writer. */
            assertTrue(lock.getWriteOwnerLocker() == null);

            /* expect 2 owners, 3 waiters. */
            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.READ));
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            checkOwners(expectedOwners, lock, 3);

            List<LockInfo> waiters = new ArrayList<LockInfo>();
            waiters.add(new LockInfo(txn1, LockType.WRITE));
            waiters.add(new LockInfo(txn3, LockType.WRITE));
            waiters.add(new LockInfo(txn4, LockType.WRITE));
            checkWaiters(waiters, lock);

            /* release a waiter, shouldn't change anything. */
            lock.release(txn4, mb, 0);
            checkWaiters(waiters, lock);

            /*
             * Release the other read lock, expect txn1 to be promoted to a
             * write lock.
             */
            lock.release(txn2, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.WRITE));
            checkOwners(expectedOwners, lock, 2);

            waiters.remove(0);
            checkWaiters(waiters, lock);

            /* release */
            lock.release(txn1, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn3, LockType.WRITE));
            checkOwners(expectedOwners, lock, 1);

            waiters.remove(0);
            checkWaiters(waiters, lock);

            /*
             * Add multiple read lock waiters so that we can promoting multiple
             * waiters.
             */
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn5, false, mb, 0).
                         lockGrant);

            checkOwners(expectedOwners, lock, 4);
            waiters.add(new LockInfo(txn1, LockType.READ));
            waiters.add(new LockInfo(txn2, LockType.READ));
            waiters.add(new LockInfo(txn5, LockType.READ));
            checkWaiters(waiters, lock);

            /* flush one of the waiters. */
            lock.flushWaiter(txn5, mb, 0);
            waiters.remove(3);
            checkWaiters(waiters, lock);

            /* re-add. */
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn5, false, mb, 0).
                         lockGrant);
            waiters.add(new LockInfo(txn5, LockType.READ));

            /* release txn3 */
            lock.release(txn3, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn4, LockType.WRITE));
            checkOwners(expectedOwners, lock, 3);
            waiters.remove(0);
            checkWaiters(waiters, lock);

            /* release txn4, expect all read locks to promote. */
            lock.release(txn4, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.READ));
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            expectedOwners.add(new LockInfo(txn5, LockType.READ));
            checkOwners(expectedOwners, lock, 0);
            waiters.clear();
            checkWaiters(waiters, lock);

            /* Release for the sake of the memory leak checking */
            lock.release(txn1, mb, 0);
            lock.release(txn2, mb, 0);
            lock.release(txn5, mb, 0);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
            txn4.operationEnd();
            txn5.operationEnd();
        }
    }

    public void testPromotion()
        throws Exception {

        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn2 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn3 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn4 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn5 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            /*
             * Build up 1 owners and 3 read waiters for a lock. Then
             * check that all the waiters promote properly.
             */
            Lock lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn3, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn4, false, mb, 0).
                         lockGrant);

            /* Check that 1 owner, 3 waiters exist. */
            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.WRITE));
            checkOwners(expectedOwners, lock, 3);

            List<LockInfo> waiters = new ArrayList<LockInfo>();
            waiters.add(new LockInfo(txn2, LockType.READ));
            waiters.add(new LockInfo(txn3, LockType.READ));
            waiters.add(new LockInfo(txn4, LockType.READ));
            checkWaiters(waiters, lock);

            /* Release the writer, expect all 3 waiters to promote. */
            lock.release(txn1, mb, 0);
            expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn2, LockType.READ));
            expectedOwners.add(new LockInfo(txn3, LockType.READ));
            expectedOwners.add(new LockInfo(txn4, LockType.READ));
            checkOwners(expectedOwners, lock, 0);
            waiters.clear();
            checkWaiters(waiters, lock);

            /* Release for the sake of the memory leak checking */
            lock.release(txn2, mb, 0);
            lock.release(txn3, mb, 0);
            lock.release(txn4, mb, 0);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
            txn4.operationEnd();
            txn5.operationEnd();
        }
    }

    /**
     * Tests conflicts between range locks and all other lock types.
     */
    public void testRangeConflicts()
        throws Exception {

        /* No owner */
        checkConflict(null,
                      LockType.RANGE_READ,
                      LockGrantType.NEW);
        checkConflict(null,
                      LockType.RANGE_WRITE,
                      LockGrantType.NEW);
        checkConflict(null,
                      LockType.RANGE_INSERT,
                      LockGrantType.NEW);

        /* Owner has READ */
        checkConflict(LockType.READ,
                      LockType.RANGE_READ,
                      LockGrantType.NEW);
        checkConflict(LockType.READ,
                      LockType.RANGE_WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.READ,
                      LockType.RANGE_INSERT,
                      LockGrantType.NEW);

        /* Owner has WRITE */
        checkConflict(LockType.WRITE,
                      LockType.RANGE_READ,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.WRITE,
                      LockType.RANGE_WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.WRITE,
                      LockType.RANGE_INSERT,
                      LockGrantType.NEW);

        /* Owner has RANGE_READ */
        checkConflict(LockType.RANGE_READ,
                      LockType.READ,
                      LockGrantType.NEW);
        checkConflict(LockType.RANGE_READ,
                      LockType.WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_READ,
                      LockType.RANGE_READ,
                      LockGrantType.NEW);
        checkConflict(LockType.RANGE_READ,
                      LockType.RANGE_WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_READ,
                      LockType.RANGE_INSERT,
                      LockGrantType.WAIT_NEW);

        /* Owner has RANGE_WRITE */
        checkConflict(LockType.RANGE_WRITE,
                      LockType.READ,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_WRITE,
                      LockType.WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_WRITE,
                      LockType.RANGE_READ,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_WRITE,
                      LockType.RANGE_WRITE,
                      LockGrantType.WAIT_NEW);
        checkConflict(LockType.RANGE_WRITE,
                      LockType.RANGE_INSERT,
                      LockGrantType.WAIT_NEW);

        /* Owner has RANGE_INSERT */
        checkConflict(LockType.RANGE_INSERT,
                      LockType.READ,
                      LockGrantType.NEW);
        checkConflict(LockType.RANGE_INSERT,
                      LockType.WRITE,
                      LockGrantType.NEW);
        checkConflict(LockType.RANGE_INSERT,
                      LockType.RANGE_READ,
                      LockGrantType.WAIT_RESTART);
        checkConflict(LockType.RANGE_INSERT,
                      LockType.RANGE_WRITE,
                      LockGrantType.WAIT_RESTART);
        checkConflict(LockType.RANGE_INSERT,
                      LockType.RANGE_INSERT,
                      LockGrantType.NEW);
    }

    /**
     * Tests that when the first request is held and the second request is
     * requested, the second grant type is returned.
     */
    private void checkConflict(LockType firstRequest, LockType secondRequest,
                               LockGrantType secondGrantType)
        throws Exception {

        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn2 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            Lock lock = new LockImpl();

            if (firstRequest != null) {
                assertEquals(LockGrantType.NEW,
                             lock.lock(firstRequest, txn1, false, mb, 0).
                             lockGrant);
            }
            LockGrantType typeGranted =
                lock.lock(secondRequest, txn2, false, mb, 0).
                lockGrant;
            assertEquals(secondGrantType, typeGranted);

            boolean wait = (typeGranted == LockGrantType.WAIT_NEW ||
                            typeGranted == LockGrantType.WAIT_PROMOTION ||
                            typeGranted == LockGrantType.WAIT_RESTART);
            boolean given = (typeGranted == LockGrantType.NEW);
            boolean restart = (typeGranted == LockGrantType.WAIT_RESTART);

            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            List<LockInfo> expectedWaiters = new ArrayList<LockInfo>();

            if (firstRequest != null) {
                expectedOwners.add(new LockInfo(txn1, firstRequest));
            }
            if (given) {
                expectedOwners.add(new LockInfo(txn2, secondRequest));
            } else if (wait) {
                if (restart) {
                    expectedWaiters.add(new LockInfo(txn2, LockType.RESTART));
                } else {
                    expectedWaiters.add(new LockInfo(txn2, secondRequest));
                }
            }

            checkOwners(expectedOwners, lock, expectedWaiters.size());
            checkWaiters(expectedWaiters, lock);

            lock.release(txn1, mb, 0);
            if (wait) {
                if (restart) {
                    checkOwners(new HashSet<LockInfo>(), lock, 0);
                } else {
                    checkOwners(new HashSet<LockInfo>(expectedWaiters), lock, 0);
                }
            }
            lock.release(txn2, mb, 0);
            assertEquals(0, lock.nOwners());
            assertEquals(0, lock.nWaiters());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
        }
    }

    /**
     * Tests upgrades between range locks and all other lock types.
     */
    public void testRangeUpgrades()
        throws Exception {

        /* Owner has READ */
        checkUpgrade(LockType.READ,
                     LockType.RANGE_READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_READ);
        checkUpgrade(LockType.READ,
                     LockType.RANGE_WRITE,
                     LockGrantType.PROMOTION,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.READ,
                     LockType.RANGE_INSERT,
                     null,
                     LockType.READ);

        /* Owner has WRITE */
        checkUpgrade(LockType.WRITE,
                     LockType.RANGE_READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.WRITE,
                     LockType.RANGE_WRITE,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.WRITE,
                     LockType.RANGE_INSERT,
                     null,
                     LockType.WRITE);

        /* Owner has RANGE_READ */
        checkUpgrade(LockType.RANGE_READ,
                     LockType.READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_READ);
        checkUpgrade(LockType.RANGE_READ,
                     LockType.WRITE,
                     LockGrantType.PROMOTION,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_READ,
                     LockType.RANGE_READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_READ);
        checkUpgrade(LockType.RANGE_READ,
                     LockType.RANGE_WRITE,
                     LockGrantType.PROMOTION,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_READ,
                     LockType.RANGE_INSERT,
                     null,
                     LockType.RANGE_READ);

        /* Owner has RANGE_WRITE */
        checkUpgrade(LockType.RANGE_WRITE,
                     LockType.READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_WRITE,
                     LockType.WRITE,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_WRITE,
                     LockType.RANGE_READ,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_WRITE,
                     LockType.RANGE_WRITE,
                     LockGrantType.EXISTING,
                     LockType.RANGE_WRITE);
        checkUpgrade(LockType.RANGE_WRITE,
                     LockType.RANGE_INSERT,
                     null,
                     LockType.RANGE_WRITE);

        /* Owner has RANGE_INSERT */
        checkUpgrade(LockType.RANGE_INSERT,
                     LockType.READ,
                     null,
                     LockType.RANGE_INSERT);
        checkUpgrade(LockType.RANGE_INSERT,
                     LockType.WRITE,
                     null,
                     LockType.RANGE_INSERT);
        checkUpgrade(LockType.RANGE_INSERT,
                     LockType.RANGE_READ,
                     null,
                     LockType.RANGE_INSERT);
        checkUpgrade(LockType.RANGE_INSERT,
                     LockType.RANGE_WRITE,
                     null,
                     LockType.RANGE_INSERT);
        checkUpgrade(LockType.RANGE_INSERT,
                     LockType.RANGE_INSERT,
                     LockGrantType.EXISTING,
                     LockType.RANGE_INSERT);
    }

    /**
     * Tests that when the first request is held and the second request is
     * requested, the second grant type is returned and the final type is then
     * held.  A null secondGrantType arg means that an assertion is expected.
     */
    private void checkUpgrade(LockType firstRequest, LockType secondRequest,
                              LockGrantType secondGrantType,
                              LockType finalType)
        throws Exception {

        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            Lock lock = new LockImpl();

            assertEquals(LockGrantType.NEW,
                         lock.lock(firstRequest, txn1, false, mb, 0).
                         lockGrant);
            LockGrantType typeGranted = null;
            try {
                typeGranted = lock.lock(secondRequest, txn1, false, mb, 0).
                    lockGrant;
                if (secondGrantType == null) {
                    fail("expected AssertionError");
                }
            } catch (AssertionError e) {
                if (secondGrantType != null) {
                    fail(e.toString());
                }
            }
            assertEquals(secondGrantType, typeGranted);

            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, finalType));
            checkOwners(expectedOwners, lock, 0);
            lock.release(txn1, mb, 0);
            assertEquals(0, lock.nOwners());
            assertEquals(0, lock.nWaiters());
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
        }
    }

    /**
     * Tests that when a range read/write is requested, and a range insert is
     * waiting but not held, a WAIT_RESTART occurs.  This requires that the
     * waiter list is examined by Lock.lock().
     */
    public void testRangeInsertWaiterConflict()
        throws Exception {
        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn2 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn3 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            Lock lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.RANGE_READ, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.RANGE_INSERT, txn2, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_RESTART,
                         lock.lock(LockType.RANGE_READ, txn3, false, mb, 0).
                         lockGrant);

            /* Check that 1 owner, 1 waiter exist. */

            Set<LockInfo> expectedOwners = new HashSet<LockInfo>();
            expectedOwners.add(new LockInfo(txn1, LockType.RANGE_READ));
            checkOwners(expectedOwners, lock, 2);

            List<LockInfo> waiters = new ArrayList<LockInfo>();
            waiters.add(new LockInfo(txn2, LockType.RANGE_INSERT));
            waiters.add(new LockInfo(txn3, LockType.RESTART));
            checkWaiters(waiters, lock);

            /* Release for the sake of the memory leak checking */
            lock.release(txn1, mb, 0);
            lock.release(txn2, mb, 0);
            lock.release(txn3, mb, 0);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
        }
    }

    private void checkOwners(Set<LockInfo> expectedOwners,
                             Lock lock,
                             int numExpectedWaiters) {

        /* check number of owners. */
        Set owners = lock.getOwnersClone();
        assertEquals(expectedOwners.size(), owners.size());

        /* check number of waiters. */
        assertEquals(numExpectedWaiters, lock.nWaiters());

        /* Make sure that isOwner returns the right thing. */
        Iterator<LockInfo> iter = expectedOwners.iterator();
        while (iter.hasNext()) {
            LockInfo info = iter.next();

            /* Make sure it's an owner, of the right type of lock. */
            assertEquals(info.getLockType().isWriteLock(),
                         lock.isOwnedWriteLock(info.getLocker()));
            assertTrue(lock.isOwner(info.getLocker(), info.getLockType()));
        }
    }

    private void checkWaiters(List<LockInfo> expectedWaiters,
                              Lock lock) {
        List waiters = lock.getWaitersListClone();
        assertEquals(expectedWaiters.size(), waiters.size());

        /* check order of the list. */
        for (int i = 0; i < expectedWaiters.size(); i++) {
            LockInfo info = expectedWaiters.get(i);
            LockInfo waiterInfo = (LockInfo) waiters.get(i);
            assertEquals("i=" + i, info.getLocker(), waiterInfo.getLocker());
            assertEquals("i=" + i,
                         info.getLockType(), waiterInfo.getLockType());
            assertFalse(lock.isOwner(info.getLocker(), info.getLockType()));
            assertTrue(lock.isWaiter(info.getLocker()));
        }
    }

    public void testTransfer()
        throws Exception {

        Locker txn1 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn2 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn3 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn4 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        Locker txn5 = Txn.createAutoTxn(envImpl, new TransactionConfig(),
                                        false, /*noAPIReadLock*/
                                        ReplicationContext.NO_REPLICATE);
        MemoryBudget mb = envImpl.getMemoryBudget();

        try {
            /* Transfer from one locker to another locker. */
            Long nid = new Long(1);
            Lock lock = new LockImpl();
            assertEquals(LockGrantType.NEW,
                         lock.lock(LockType.WRITE, txn1, false, mb, 0).
                         lockGrant);
            assertEquals(LockGrantType.WAIT_NEW,
                         lock.lock(LockType.READ, txn2, false, mb, 0).
                         lockGrant);
            assertTrue(lock.isOwner(txn1, LockType.WRITE));
            assertFalse(lock.isOwner(txn2, LockType.READ));

            lock.transfer(nid, txn1, txn2, mb, 0);
            assertFalse(lock.isOwner(txn1, LockType.WRITE));
            assertFalse(lock.isOwner(txn1, LockType.READ));
            assertTrue(lock.isOwnedWriteLock(txn2));

            /* Transfer to multiple lockers. */
            Locker[] destLockers = new Locker[3];
            destLockers[0] = txn3;
            destLockers[1] = txn4;
            destLockers[2] = txn5;
            lock.demote(txn2);
            lock.transferMultiple(nid, txn2, destLockers, mb, 0);
            assertFalse(lock.isOwner(txn2, LockType.WRITE));
            assertFalse(lock.isOwner(txn2, LockType.READ));

            for (int i = 0; i < destLockers.length; i++) {
                assertTrue(lock.isOwner(destLockers[i], LockType.READ));
                assertFalse(lock.isOwner(destLockers[i], LockType.WRITE));
                lock.release(destLockers[i], mb, 0);
            }

        } finally {
            txn1.operationEnd();
            txn2.operationEnd();
            txn3.operationEnd();
            txn4.operationEnd();
            txn5.operationEnd();
        }
    }
}