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 #include "cachetable-test.h"
41 
42 bool foo;
43 
44 //
45 // This test verifies that get_and_pin_nonblocking works and returns DB_TRYAGAIN when the PAIR is being used.
46 //
47 
48 static void
flush(CACHEFILE f,int UU (fd),CACHEKEY k,void * v,void ** UU (dd),void * e,PAIR_ATTR s,PAIR_ATTR * new_size,bool w,bool keep,bool c,bool UU (is_clone))49 flush (CACHEFILE f __attribute__((__unused__)),
50        int UU(fd),
51        CACHEKEY k  __attribute__((__unused__)),
52        void *v     __attribute__((__unused__)),
53        void** UU(dd),
54        void *e     __attribute__((__unused__)),
55        PAIR_ATTR s      __attribute__((__unused__)),
56        PAIR_ATTR* new_size      __attribute__((__unused__)),
57        bool w      __attribute__((__unused__)),
58        bool keep   __attribute__((__unused__)),
59        bool c      __attribute__((__unused__)),
60         bool UU(is_clone)
61        ) {
62   /* Do nothing */
63   if (verbose) { printf("FLUSH: %d\n", (int)k.b); }
64   // this should not be flushed until the bottom of the test, which
65   // verifies that this is called if we have it pending a checkpoint
66   if (w) {
67     assert(c);
68     assert(keep);
69   }
70   //usleep (5*1024*1024);
71 }
72 
true_def_pf_req_callback(void * UU (ftnode_pv),void * UU (read_extraargs))73 static bool true_def_pf_req_callback(void* UU(ftnode_pv), void* UU(read_extraargs)) {
74   return true;
75 }
true_def_pf_callback(void * UU (ftnode_pv),void * UU (dd),void * UU (read_extraargs),int UU (fd),PAIR_ATTR * sizep)76 static int true_def_pf_callback(void* UU(ftnode_pv), void* UU(dd), void* UU(read_extraargs), int UU(fd), PAIR_ATTR* sizep) {
77     *sizep = make_pair_attr(8);
78     return 0;
79 }
80 
81 
kibbutz_work(void * fe_v)82 static void kibbutz_work(void *fe_v)
83 {
84     CACHEFILE CAST_FROM_VOIDP(f1, fe_v);
85     sleep(2);
86     foo = true;
87     int r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
88     assert(r==0);
89     remove_background_job_from_cf(f1);
90 }
91 
92 
93 static void
run_test(void)94 run_test (void) {
95     const int test_limit = 12;
96     int r;
97     CACHETABLE ct;
98     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
99     const char *fname1 = TOKU_TEST_FILENAME;
100     unlink(fname1);
101     CACHEFILE f1;
102     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
103     create_dummy_functions(f1);
104 
105     void* v1;
106     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
107     wc.flush_callback = flush;
108     //
109     // test that if we are getting a PAIR for the first time that TOKUDB_TRY_AGAIN is returned
110     // because the PAIR was not in the cachetable.
111     //
112     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, NULL);
113     assert(r==TOKUDB_TRY_AGAIN);
114     // now it should succeed
115     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, NULL);
116     assert(r==0);
117     foo = false;
118     cachefile_kibbutz_enq(f1, kibbutz_work, f1);
119     // because node is in use, should return TOKUDB_TRY_AGAIN
120     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, NULL);
121     assert(r==TOKUDB_TRY_AGAIN);
122     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
123     assert(foo);
124     r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8)); assert(r==0);
125 
126     // now make sure we get TOKUDB_TRY_AGAIN when a partial fetch is involved
127     // first make sure value is there
128     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, NULL);
129     assert(r==0);
130     r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8)); assert(r==0);
131     // now make sure that we get TOKUDB_TRY_AGAIN for the partial fetch
132     r = toku_cachetable_get_and_pin_nonblocking(f1, make_blocknum(1), 1, &v1, wc, def_fetch, true_def_pf_req_callback, true_def_pf_callback, PL_WRITE_EXPENSIVE, NULL, NULL);
133     assert(r==TOKUDB_TRY_AGAIN);
134 
135     toku_cachetable_verify(ct);
136     toku_cachefile_close(&f1, false, ZERO_LSN);
137     toku_cachetable_close(&ct);
138 }
139 
140 int
test_main(int argc,const char * argv[])141 test_main(int argc, const char *argv[]) {
142   default_parse_args(argc, argv);
143   run_test();
144   return 0;
145 }
146