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 // This file contains some basic tests for partial fetch, ensuring that
43 // it works correctly
44 //
45
46 uint32_t fetch_val = 0;
47 bool pf_req_called;
48
49 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)50 fetch (CACHEFILE f __attribute__((__unused__)),
51 PAIR UU(p),
52 int UU(fd),
53 CACHEKEY k __attribute__((__unused__)),
54 uint32_t fullhash __attribute__((__unused__)),
55 void **value __attribute__((__unused__)),
56 void** UU(dd),
57 PAIR_ATTR *sizep __attribute__((__unused__)),
58 int *dirtyp,
59 void *extraargs __attribute__((__unused__))
60 ) {
61 *dirtyp = 0;
62 *value = &fetch_val;
63 *sizep = make_pair_attr(sizeof(fetch_val));
64 return 0;
65 }
66
67 static int
err_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)68 err_fetch (CACHEFILE f __attribute__((__unused__)),
69 PAIR UU(p),
70 int UU(fd),
71 CACHEKEY k __attribute__((__unused__)),
72 uint32_t fullhash __attribute__((__unused__)),
73 void **value __attribute__((__unused__)),
74 void** UU(dd),
75 PAIR_ATTR *sizep __attribute__((__unused__)),
76 int *dirtyp,
77 void *extraargs __attribute__((__unused__))
78 ) {
79 assert(false);
80 *dirtyp = 0;
81 return 0;
82 }
83
pf_req_callback(void * UU (ftnode_pv),void * UU (read_extraargs))84 static bool pf_req_callback(void* UU(ftnode_pv), void* UU(read_extraargs)) {
85 return false;
86 }
87
true_pf_req_callback(void * UU (ftnode_pv),void * UU (read_extraargs))88 static bool true_pf_req_callback(void* UU(ftnode_pv), void* UU(read_extraargs)) {
89 if (pf_req_called) return false;
90 return true;
91 }
92
err_pf_callback(void * UU (ftnode_pv),void * UU (dd),void * UU (read_extraargs),int UU (fd),PAIR_ATTR * UU (sizep))93 static int err_pf_callback(void* UU(ftnode_pv), void* UU(dd), void* UU(read_extraargs), int UU(fd), PAIR_ATTR* UU(sizep)) {
94 assert(false);
95 return 0; // gcov
96 }
97
pf_callback(void * UU (ftnode_pv),void * UU (dd),void * UU (read_extraargs),int UU (fd),PAIR_ATTR * UU (sizep))98 static int pf_callback(void* UU(ftnode_pv), void* UU(dd), void* UU(read_extraargs), int UU(fd), PAIR_ATTR* UU(sizep)) {
99 assert(false);
100 return 0; // gcov
101 }
102
true_pf_callback(void * UU (ftnode_pv),void * UU (dd),void * read_extraargs,int UU (fd),PAIR_ATTR * sizep)103 static int true_pf_callback(void* UU(ftnode_pv), void* UU(dd), void* read_extraargs, int UU(fd), PAIR_ATTR* sizep) {
104 pf_req_called = true;
105 *sizep = make_pair_attr(sizeof(fetch_val)+1);
106 assert(read_extraargs == &fetch_val);
107 return 0;
108 }
109
110
111 static void
cachetable_test(void)112 cachetable_test (void) {
113 const int test_limit = 12;
114 int r;
115 CACHETABLE ct;
116 bool doing_prefetch = false;
117 toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
118 const char *fname1 = TOKU_TEST_FILENAME;
119 unlink(fname1);
120 CACHEFILE f1;
121 r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
122
123 void* v1;
124 CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
125 r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, fetch, pf_req_callback, pf_callback, true, NULL);
126 assert(&fetch_val == v1);
127 //
128 // verify that a prefetch of this node will fail
129 //
130 r = toku_cachefile_prefetch(
131 f1,
132 make_blocknum(1),
133 1,
134 wc,
135 fetch,
136 pf_req_callback,
137 pf_callback,
138 NULL,
139 &doing_prefetch
140 );
141 assert(r == 0);
142 // make sure that prefetch should not happen, because we have already pinned node
143 assert(!doing_prefetch);
144 r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
145 //
146 // now get and pin node again, and make sure that partial fetch and fetch are not called
147 //
148 r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, err_fetch, pf_req_callback, err_pf_callback, true, NULL);
149 r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
150 //
151 // now make sure that if we say a partial fetch is required, that we get a partial fetch
152 // and that read_extraargs properly passed down
153 //
154 pf_req_called = false;
155 r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, err_fetch, true_pf_req_callback, true_pf_callback, true, &fetch_val);
156 assert(pf_req_called);
157 PAIR_ATTR attr;
158 r = toku_cachetable_get_attr(f1, make_blocknum(1), 1, &attr);
159 assert(r == 0);
160 assert(attr.size == sizeof(fetch_val)+1);
161 r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
162
163 // close and reopen cachefile so we can do some simple prefetch tests
164 toku_cachefile_close(&f1, false, ZERO_LSN);
165 toku_cachetable_close(&ct);
166 toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
167 r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
168 //
169 // verify that a prefetch of the node will succeed
170 //
171 r = toku_cachefile_prefetch(
172 f1,
173 make_blocknum(1),
174 1,
175 wc,
176 fetch,
177 pf_req_callback,
178 pf_callback,
179 NULL,
180 &doing_prefetch
181 );
182 assert(r == 0);
183 // make sure that prefetch should happen, because we have already pinned node
184 assert(doing_prefetch);
185 //
186 // now verify we can pin it, and NO fetch callback should get called
187 //
188 r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, err_fetch, pf_req_callback, err_pf_callback, true, NULL);
189 assert(&fetch_val == v1);
190 r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
191
192 //
193 // now verify a prefetch that requires a partial fetch works, and that we can then pin the node
194 //
195 pf_req_called = false;
196 r = toku_cachefile_prefetch(
197 f1,
198 make_blocknum(1),
199 1,
200 wc,
201 fetch,
202 true_pf_req_callback,
203 true_pf_callback,
204 &fetch_val,
205 &doing_prefetch
206 );
207 assert(doing_prefetch);
208 r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, err_fetch, pf_req_callback, err_pf_callback, true, NULL);
209 assert(&fetch_val == v1);
210 r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(8));
211
212
213 toku_cachetable_verify(ct);
214 toku_cachefile_close(&f1, false, ZERO_LSN);
215 toku_cachetable_close(&ct);
216
217
218 }
219
220 int
test_main(int argc,const char * argv[])221 test_main(int argc, const char *argv[]) {
222 default_parse_args(argc, argv);
223 cachetable_test();
224 return 0;
225 }
226