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