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 bool check_me;
44 bool flush_called;
45 
46 //
47 // This test verifies that get_and_pin takes a write lock on a PAIR.
48 //
49 
50 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))51 flush (CACHEFILE f __attribute__((__unused__)),
52        int UU(fd),
53        CACHEKEY k  __attribute__((__unused__)),
54        void *v     __attribute__((__unused__)),
55        void** UU(dd),
56        void *e     __attribute__((__unused__)),
57        PAIR_ATTR s      __attribute__((__unused__)),
58        PAIR_ATTR* new_size      __attribute__((__unused__)),
59        bool w      __attribute__((__unused__)),
60        bool keep   __attribute__((__unused__)),
61        bool c      __attribute__((__unused__)),
62         bool UU(is_clone)
63        ) {
64   /* Do nothing */
65   if (verbose) { printf("FLUSH: %d\n", (int)k.b); }
66   //usleep (5*1024*1024);
67   if (check_me) {
68     flush_called = true;
69     assert(c);
70     assert(keep);
71     assert(w);
72   }
73 }
74 
kibbutz_work(void * fe_v)75 static void kibbutz_work(void *fe_v)
76 {
77     CACHEFILE CAST_FROM_VOIDP(f1, fe_v);
78     sleep(2);
79     foo = true;
80     int r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
81     assert(r==0);
82     remove_background_job_from_cf(f1);
83 }
84 
85 static void
run_test(void)86 run_test (void) {
87     const int test_limit = 12;
88     int r;
89     CACHETABLE ct;
90     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
91     const char *fname1 = TOKU_TEST_FILENAME;
92     unlink(fname1);
93     CACHEFILE f1;
94     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
95     create_dummy_functions(f1);
96 
97     void* v1;
98     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
99     wc.flush_callback = flush;
100     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
101     foo = false;
102     cachefile_kibbutz_enq(f1, kibbutz_work, f1);
103     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
104     assert(foo);
105     r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
106 
107     //now let's do a simple checkpoint test
108     // first dirty the PAIR
109     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
110     r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_DIRTY, make_pair_attr(8));
111 
112     // now this should mark the pair for checkpoint
113     CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
114     toku_cachetable_begin_checkpoint(cp, NULL);
115 
116     //
117     // now we pin the pair again, and verify in flush callback that the pair is being checkpointed
118     //
119     check_me = true;
120     flush_called = false;
121     r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, def_fetch, def_pf_req_callback, def_pf_callback, true, NULL);
122     assert(flush_called);
123     r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
124 
125 
126     check_me = false;
127     toku_cachetable_end_checkpoint(
128         cp,
129         NULL,
130         NULL,
131         NULL
132         );
133 
134     toku_cachetable_verify(ct);
135     toku_cachefile_close(&f1, false, ZERO_LSN);
136     toku_cachetable_close(&ct);
137 }
138 
139 int
test_main(int argc,const char * argv[])140 test_main(int argc, const char *argv[]) {
141   default_parse_args(argc, argv);
142   run_test();
143   return 0;
144 }
145