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 "test.h"
40 
41 
42 static void
cachetable_unpin_test(int n)43 cachetable_unpin_test (int n) {
44     const int test_limit = 2*n;
45     int r;
46     CACHETABLE ct;
47     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
48     const char *fname1 = TOKU_TEST_FILENAME;
49     unlink(fname1);
50     CACHEFILE f1;
51     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
52 
53     int i;
54     for (i=1; i<=n; i++) {
55         uint32_t hi;
56         CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
57         hi = toku_cachetable_hash(f1, make_blocknum(i));
58         toku_cachetable_put(f1, make_blocknum(i), hi, (void *)(long)i, make_pair_attr(1), wc, put_callback_nop);
59         assert(toku_cachefile_count_pinned(f1, 0) == i);
60 
61         void *v;
62         r = toku_cachetable_maybe_get_and_pin(f1, make_blocknum(i), hi, PL_WRITE_EXPENSIVE, &v);
63         assert(r == -1);
64         assert(toku_cachefile_count_pinned(f1, 0) == i);
65 
66         //r = toku_test_cachetable_unpin(f1, make_blocknum(i), hi, CACHETABLE_CLEAN, 1);
67         //assert(r == 0);
68         assert(toku_cachefile_count_pinned(f1, 0) == i);
69     }
70     for (i=n; i>0; i--) {
71         uint32_t hi;
72         hi = toku_cachetable_hash(f1, make_blocknum(i));
73         r = toku_test_cachetable_unpin(f1, make_blocknum(i), hi, CACHETABLE_CLEAN, make_pair_attr(1));
74         assert(r == 0);
75         assert(toku_cachefile_count_pinned(f1, 0) == i-1);
76     }
77     assert(toku_cachefile_count_pinned(f1, 1) == 0);
78     toku_cachetable_verify(ct);
79 
80     toku_cachefile_close(&f1, false, ZERO_LSN);
81     toku_cachetable_close(&ct);
82 }
83 
84 enum unpin_evictor_test_type {
85     unpin_increase,
86     unpin_decrease,
87     unpin_invalid_attr
88 };
89 
90 static void
unpin_and_evictor_test(enum unpin_evictor_test_type test_type)91 unpin_and_evictor_test(enum unpin_evictor_test_type test_type) {
92     int r;
93     CACHETABLE ct;
94     int test_limit = 4;
95     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
96     const char *fname1 = TOKU_TEST_FILENAME;
97     unlink(fname1);
98     CACHEFILE f1;
99 
100     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
101     evictor_test_helpers::set_hysteresis_limits(&ct->ev, test_limit, test_limit);
102     evictor_test_helpers::disable_ev_thread(&ct->ev);
103 
104     void* value2;
105     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
106     // this should put in the cachetable a pair of size 8
107     r = toku_cachetable_get_and_pin(
108         f1,
109         make_blocknum(1),
110         1,
111         &value2,
112         wc,
113         def_fetch,
114         def_pf_req_callback,
115         def_pf_callback,
116         true,
117         0
118         );
119     assert(r==0);
120     //
121     // now we unpin,
122     // if we increase the size, we should catch a sleep
123     // if we don't increase the size, there should be no sleep
124     // if we pass in an invalid pair_attr, there should be no sleep.
125     //
126     uint64_t old_num_ev_runs = 0;
127     uint64_t new_num_ev_runs = 0;
128     if (test_type == unpin_increase) {
129         old_num_ev_runs = evictor_test_helpers::get_num_eviction_runs(&ct->ev);
130         r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_DIRTY, make_pair_attr(9));
131         new_num_ev_runs = evictor_test_helpers::get_num_eviction_runs(&ct->ev);
132         assert(new_num_ev_runs > old_num_ev_runs);
133     }
134     else if (test_type == unpin_decrease || test_type == unpin_invalid_attr) {
135         old_num_ev_runs = evictor_test_helpers::get_num_eviction_runs(&ct->ev);
136         r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_DIRTY, make_pair_attr(8));
137         new_num_ev_runs = evictor_test_helpers::get_num_eviction_runs(&ct->ev);
138         assert(new_num_ev_runs == old_num_ev_runs);
139     }
140     else {
141         assert(false);
142     }
143 
144     toku_cachetable_verify(ct);
145     toku_cachefile_close(&f1, false, ZERO_LSN);
146     toku_cachetable_close(&ct);
147 }
148 
149 int
test_main(int argc,const char * argv[])150 test_main(int argc, const char *argv[]) {
151     default_parse_args(argc, argv);
152     cachetable_unpin_test(8);
153     unpin_and_evictor_test(unpin_increase);
154     unpin_and_evictor_test(unpin_decrease);
155     unpin_and_evictor_test(unpin_invalid_attr);
156     return 0;
157 }
158