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 an in progress prefetch works
40
41 #include "test.h"
42
43 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))44 flush (CACHEFILE f __attribute__((__unused__)),
45 int UU(fd),
46 CACHEKEY k __attribute__((__unused__)),
47 void *v __attribute__((__unused__)),
48 void** UU(dd),
49 void *e __attribute__((__unused__)),
50 PAIR_ATTR s __attribute__((__unused__)),
51 PAIR_ATTR* new_size __attribute__((__unused__)),
52 bool w __attribute__((__unused__)),
53 bool keep __attribute__((__unused__)),
54 bool c __attribute__((__unused__)),
55 bool UU(is_clone)
56 ) {
57 assert(w == false && v != NULL);
58 toku_free(v);
59 }
60
61 static int fetch_calls = 0;
62
63 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)64 fetch (CACHEFILE f __attribute__((__unused__)),
65 PAIR UU(p),
66 int UU(fd),
67 CACHEKEY k __attribute__((__unused__)),
68 uint32_t fullhash __attribute__((__unused__)),
69 void **value __attribute__((__unused__)),
70 void** UU(dd),
71 PAIR_ATTR *sizep __attribute__((__unused__)),
72 int *dirtyp __attribute__((__unused__)),
73 void *extraargs __attribute__((__unused__))
74 ) {
75
76 fetch_calls++;
77 sleep(10);
78
79 *value = toku_malloc(1);
80 *sizep = make_pair_attr(1);
81 *dirtyp = 0;
82
83 return 0;
84 }
85
cachetable_prefetch_close_leak_test(void)86 static void cachetable_prefetch_close_leak_test (void) {
87 const int test_limit = 1;
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
96 // prefetch block 0. this will take 10 seconds.
97 CACHEKEY key = make_blocknum(0);
98 uint32_t fullhash = toku_cachetable_hash(f1, make_blocknum(0));
99 CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
100 wc.flush_callback = flush;
101 r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, NULL);
102 toku_cachetable_verify(ct);
103
104 // close with the prefetch in progress. the close should block until
105 // all of the reads and writes are complete.
106 toku_cachefile_close(&f1, false, ZERO_LSN);
107 toku_cachetable_close(&ct);
108 }
109
110 int
test_main(int argc,const char * argv[])111 test_main(int argc, const char *argv[]) {
112 default_parse_args(argc, argv);
113 cachetable_prefetch_close_leak_test();
114 return 0;
115 }
116