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 bool pf_called;
true_pf_req_callback(void * UU (ftnode_pv),void * UU (read_extraargs))42 static bool true_pf_req_callback(void* UU(ftnode_pv), void* UU(read_extraargs)) {
43   if (pf_called) return false;
44   return true;
45 }
46 
true_pf_callback(void * UU (ftnode_pv),void * UU (dd),void * UU (read_extraargs),int UU (fd),PAIR_ATTR * sizep)47 static int true_pf_callback(void* UU(ftnode_pv), void* UU(dd), void* UU(read_extraargs), int UU(fd), PAIR_ATTR* sizep) {
48     *sizep = make_pair_attr(9);
49     pf_called = true;
50     return 0;
51 }
52 
kibbutz_work(void * fe_v)53 static void kibbutz_work(void *fe_v)
54 {
55     CACHEFILE CAST_FROM_VOIDP(f1, fe_v);
56     sleep(2);
57     int r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
58     assert(r==0);
59     remove_background_job_from_cf(f1);
60 }
61 
62 static void
unlock_dummy(void * UU (v))63 unlock_dummy (void* UU(v)) {
64 }
65 
reset_unlockers(UNLOCKERS unlockers)66 static void reset_unlockers(UNLOCKERS unlockers) {
67     unlockers->locked = true;
68 }
69 
70 static void
run_test(pair_lock_type lock_type)71 run_test (pair_lock_type lock_type) {
72     const int test_limit = 12;
73     struct unlockers unlockers = {true, unlock_dummy, NULL, NULL};
74     int r;
75     CACHETABLE ct;
76     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
77     const char *fname1 = TOKU_TEST_FILENAME;
78     unlink(fname1);
79     CACHEFILE f1;
80     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
81 
82     void* v1;
83     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
84     r = toku_cachetable_get_and_pin_with_dep_pairs(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, lock_type, NULL, 0, NULL, NULL);
85     cachefile_kibbutz_enq(f1, kibbutz_work, f1);
86     reset_unlockers(&unlockers);
87     r = toku_cachetable_get_and_pin_nonblocking(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, PL_WRITE_EXPENSIVE, NULL, &unlockers);
88     // to fix #5393, we changed behavior on full fetch where if we
89     // requested a PL_WRITE_CHEAP, and had to grab a PL_WRITE_EXPENSIVE for
90     // a full fetch, we keep it as a PL_WRITE_EXPENSIVE because downgrading back
91     // was too big a pain.
92     if (lock_type == PL_WRITE_EXPENSIVE || lock_type == PL_WRITE_CHEAP) {
93         assert(r == TOKUDB_TRY_AGAIN); assert(!unlockers.locked);
94     }
95     else {
96         assert(r == 0); assert(unlockers.locked);
97         r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8)); assert(r==0);
98     }
99 
100     // now do the same test with a partial fetch required
101     pf_called = false;
102     r = toku_cachetable_get_and_pin_with_dep_pairs(f1, make_blocknum(1), 1, &v1, wc, def_fetch, true_pf_req_callback, true_pf_callback, lock_type, NULL, 0, NULL, NULL);
103     assert(pf_called);
104     cachefile_kibbutz_enq(f1, kibbutz_work, f1);
105     reset_unlockers(&unlockers);
106     r = toku_cachetable_get_and_pin_nonblocking(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, PL_WRITE_EXPENSIVE, NULL, &unlockers);
107     if (lock_type == PL_WRITE_EXPENSIVE) {
108         assert(r == TOKUDB_TRY_AGAIN); assert(!unlockers.locked);
109     }
110     else {
111         assert(r == 0); assert(unlockers.locked);
112         r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8)); assert(r==0);
113     }
114 
115     toku_cachetable_verify(ct);
116     toku_cachefile_close(&f1, false, ZERO_LSN);
117     toku_cachetable_close(&ct);
118 }
119 
120 int
test_main(int argc,const char * argv[])121 test_main(int argc, const char *argv[]) {
122   default_parse_args(argc, argv);
123   run_test(PL_READ);
124   run_test(PL_WRITE_CHEAP);
125   run_test(PL_WRITE_EXPENSIVE);
126   return 0;
127 }
128