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 // verify that closing the cachetable with prefetches in progress works
40 
41 #include "test.h"
42 
43 bool expect_pf;
44 
45 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))46 flush (CACHEFILE f __attribute__((__unused__)),
47        int UU(fd),
48        CACHEKEY k  __attribute__((__unused__)),
49        void *v     __attribute__((__unused__)),
50        void** UU(dd),
51        void *e     __attribute__((__unused__)),
52        PAIR_ATTR s      __attribute__((__unused__)),
53        PAIR_ATTR* new_size      __attribute__((__unused__)),
54        bool w      __attribute__((__unused__)),
55        bool keep   __attribute__((__unused__)),
56        bool c      __attribute__((__unused__)),
57         bool UU(is_clone)
58        ) {
59     assert(w == false);
60 }
61 
62 static int fetch_calls = 0;
63 
64 static int
fetch(CACHEFILE f,PAIR UU (p),int UU (fd),CACHEKEY k,uint32_t fullhash,void ** value,void ** UU (dd),PAIR_ATTR * sizep,int * dirtyp,void * extraargs)65 fetch (CACHEFILE f        __attribute__((__unused__)),
66        PAIR UU(p),
67        int UU(fd),
68        CACHEKEY k         __attribute__((__unused__)),
69        uint32_t fullhash __attribute__((__unused__)),
70        void **value       __attribute__((__unused__)),
71        void** UU(dd),
72        PAIR_ATTR *sizep        __attribute__((__unused__)),
73        int  *dirtyp       __attribute__((__unused__)),
74        void *extraargs    __attribute__((__unused__))
75        ) {
76 
77     fetch_calls++;
78     sleep(2);
79 
80     *value = 0;
81     *sizep = make_pair_attr(1);
82     *dirtyp = 0;
83 
84     return 0;
85 }
86 
cachetable_prefetch_full_test(bool partial_fetch)87 static void cachetable_prefetch_full_test (bool partial_fetch) {
88     const int test_limit = 2;
89     expect_pf = false;
90     int r;
91     CACHETABLE ct;
92     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
93     const char *fname1 = TOKU_TEST_FILENAME;
94     unlink(fname1);
95     CACHEFILE f1;
96     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
97 
98     // prefetch block 0. this will take 2 seconds.
99     CACHEKEY key = make_blocknum(0);
100     uint32_t fullhash = toku_cachetable_hash(f1, make_blocknum(0));
101 
102     // if we want to do a test of partial fetch,
103     // we first put the key into the cachefile so that
104     // the subsequent prefetch does a partial fetch
105     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
106     wc.flush_callback = flush;
107     if (partial_fetch) {
108         expect_pf = true;
109         void* value;
110         r = toku_cachetable_get_and_pin(
111             f1,
112             key,
113             fullhash,
114             &value,
115             wc,
116             fetch,
117             def_pf_req_callback,
118             def_pf_callback,
119             true,
120             0
121             );
122         assert(r==0);
123         r = toku_test_cachetable_unpin(f1, key, fullhash, CACHETABLE_CLEAN, make_pair_attr(1));
124     }
125 
126     r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, NULL);
127     toku_cachetable_verify(ct);
128 
129     // close with the prefetch in progress. the close should block until
130     // all of the reads and writes are complete.
131     toku_cachefile_close(&f1, false, ZERO_LSN);
132     toku_cachetable_close(&ct);
133 }
134 
135 int
test_main(int argc,const char * argv[])136 test_main(int argc, const char *argv[]) {
137     default_parse_args(argc, argv);
138     cachetable_prefetch_full_test(true);
139     cachetable_prefetch_full_test(false);
140     return 0;
141 }
142