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 cachetable prefetch of the same block multiple times only
40 // fetches the block once.
41
42 #include "test.h"
43
44 static int fetch_calls = 0;
45
46 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)47 fetch (CACHEFILE f __attribute__((__unused__)),
48 PAIR UU(p),
49 int UU(fd),
50 CACHEKEY k __attribute__((__unused__)),
51 uint32_t fullhash __attribute__((__unused__)),
52 void **value __attribute__((__unused__)),
53 void** UU(dd),
54 PAIR_ATTR *sizep __attribute__((__unused__)),
55 int *dirtyp __attribute__((__unused__)),
56 void *extraargs __attribute__((__unused__))
57 ) {
58
59 fetch_calls++;
60 sleep(10);
61
62 *value = 0;
63 *sizep = make_pair_attr(1);
64 *dirtyp = 1;
65
66 return 0;
67 }
68
cachetable_prefetch_maybegetandpin_test(void)69 static void cachetable_prefetch_maybegetandpin_test (void) {
70 const int test_limit = 1;
71 int r;
72 CACHETABLE ct;
73 toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
74 const char *fname1 = TOKU_TEST_FILENAME;
75 unlink(fname1);
76 CACHEFILE f1;
77 r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
78
79 // prefetch block 0. this will take 10 seconds.
80 CACHEKEY key = make_blocknum(0);
81 uint32_t fullhash = toku_cachetable_hash(f1, make_blocknum(0));
82 CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
83 r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, NULL);
84 toku_cachetable_verify(ct);
85
86 // prefetch again. this should do nothing.
87 r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, NULL);
88 toku_cachetable_verify(ct);
89
90 // verify that maybe_get_and_pin returns an error while the prefetch is in progress
91 int i;
92 for (i=1; i>=0; i++) {
93 void *v;
94 r = toku_cachetable_maybe_get_and_pin(f1, key, fullhash, PL_WRITE_EXPENSIVE, &v);
95 if (r == 0) break;
96 toku_pthread_yield();
97 }
98 if (verbose) printf("%s:%d %d\n", __FUNCTION__, __LINE__, i);
99 assert(i>1);
100 toku_cachetable_verify(ct);
101
102 // there should only be 1 fetch callback
103 assert(fetch_calls == 1);
104
105 r = toku_test_cachetable_unpin(f1, key, fullhash, CACHETABLE_CLEAN, make_pair_attr(1));
106 assert(r == 0);
107 toku_cachetable_verify(ct);
108
109 toku_cachefile_close(&f1, false, ZERO_LSN);
110 toku_cachetable_close(&ct);
111 }
112
113 int
test_main(int argc,const char * argv[])114 test_main(int argc, const char *argv[]) {
115 default_parse_args(argc, argv);
116 cachetable_prefetch_maybegetandpin_test();
117 return 0;
118 }
119