1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #include "locktree_unit_test.h"
40
41 namespace toku {
42
43 // test that the same txn can relock ranges it already owns
44 // ensure that existing read locks can be upgrading to
45 // write locks if overlapping and ensure that existing read
46 // or write locks are consolidated by overlapping relocks.
test_single_txnid_optimization(void)47 void locktree_unit_test::test_single_txnid_optimization(void) {
48 locktree lt;
49
50 DICTIONARY_ID dict_id = { 1 };
51 lt.create(nullptr, dict_id, dbt_comparator);
52
53 const DBT *zero = get_dbt(0);
54 const DBT *one = get_dbt(1);
55 const DBT *two = get_dbt(2);
56 const DBT *three = get_dbt(3);
57
58 int r;
59 TXNID txnid_a = 1001;
60 TXNID txnid_b = 2001;
61
62 // the single txnid optimization takes advantage of the fact that
63 // a locktree with only locks for a single txnid can be unlocked
64 // by just destroy every node. if this is implemented incorrectly,
65 // then some other txnid's lock might get lost. so test that no
66 // matter where txnid b takes its write lock in the middle of a bunch
67 // of txnid a locks, the txnid b lock does not get lost.
68 for (int where = 0; where < 4; where++) {
69 range_buffer buffer;
70 buffer.create();
71
72 #define lock_and_append_point_for_txnid_a(key) \
73 r = lt.acquire_write_lock(txnid_a, key, key, nullptr, false); \
74 invariant_zero(r); \
75 buffer.append(key, key);
76
77 #define maybe_point_locks_for_txnid_b(i) \
78 if (where == i) { \
79 r = lt.acquire_write_lock(txnid_b, one, one, nullptr, false); \
80 invariant_zero(r); \
81 }
82
83 lock_and_append_point_for_txnid_a(two);
84 maybe_point_locks_for_txnid_b(0);
85
86 lock_and_append_point_for_txnid_a(three);
87 maybe_point_locks_for_txnid_b(1);
88
89 lock_and_append_point_for_txnid_a(zero);
90 maybe_point_locks_for_txnid_b(2);
91
92 lt.release_locks(txnid_a, &buffer);
93
94 // txnid b does not take a lock on iteration 3
95 if (where != 3) {
96 struct verify_fn_obj {
97 TXNID expected_txnid;
98 keyrange *expected_range;
99 const comparator *cmp;
100 bool fn(const keyrange &range, TXNID txnid) {
101 invariant(txnid == expected_txnid);
102 keyrange::comparison c = range.compare(*cmp, *expected_range);
103 invariant(c == keyrange::comparison::EQUALS);
104 return true;
105 }
106 } verify_fn;
107 verify_fn.cmp = <.m_cmp;
108
109 keyrange range;
110 range.create(one, one);
111 verify_fn.expected_txnid = txnid_b;
112 verify_fn.expected_range = ⦥
113 locktree_iterate<verify_fn_obj>(<, &verify_fn);
114 lt.remove_overlapping_locks_for_txnid(txnid_b, one, one);
115 }
116
117 buffer.destroy();
118 }
119
120 lt.release_reference();
121 lt.destroy();
122 }
123
124 } /* namespace toku */
125
main(void)126 int main(void) {
127 toku::locktree_unit_test test;
128 test.test_single_txnid_optimization();
129 return 0;
130 }
131