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 multiple blocks hits the cachetable size limit
40 // and flushes eventually happen.
41 
42 
43 #include "test.h"
44 #include "cachetable/cachetable-internal.h"
45 
46 static int flush_calls = 0;
47 static int flush_evict_calls = 0;
48 static int evicted_keys = 0;
49 
50 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 f_ckpt,bool UU (is_clone))51 flush (CACHEFILE f __attribute__((__unused__)),
52        int UU(fd),
53        CACHEKEY k,
54        void *v     __attribute__((__unused__)),
55        void** UU(dd),
56        void *e     __attribute__((__unused__)),
57        PAIR_ATTR s      __attribute__((__unused__)),
58        PAIR_ATTR* new_size      __attribute__((__unused__)),
59        bool w,
60        bool keep,
61        bool f_ckpt __attribute__((__unused__)),
62         bool UU(is_clone)
63        ) {
64     assert(w == false);
65     sleep(1);
66     flush_calls++;
67     if (keep == false) {
68         flush_evict_calls++;
69         if (verbose) printf("%s:%d flush %" PRId64 "\n", __FUNCTION__, __LINE__, k.b);
70         evicted_keys |= 1 << k.b;
71     }
72 }
73 
74 static int fetch_calls = 0;
75 
76 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)77 fetch (CACHEFILE f        __attribute__((__unused__)),
78        PAIR UU(p),
79        int UU(fd),
80        CACHEKEY k,
81        uint32_t fullhash __attribute__((__unused__)),
82        void **value,
83        void** UU(dd),
84        PAIR_ATTR *sizep,
85        int  *dirtyp,
86        void *extraargs    __attribute__((__unused__))
87        ) {
88 
89     fetch_calls++;
90     if (verbose) printf("%s:%d %" PRId64 "\n", __FUNCTION__, __LINE__, k.b);
91 
92     *value = 0;
93     *sizep = make_pair_attr(1);
94     *dirtyp = 0;
95 
96     return 0;
97 }
98 
99 // Note: cachetable_size_limit must be a power of 2
cachetable_prefetch_flowcontrol_test(int cachetable_size_limit)100 static void cachetable_prefetch_flowcontrol_test (int cachetable_size_limit) {
101     int r;
102     CACHETABLE ct;
103     toku_cachetable_create(&ct, cachetable_size_limit, ZERO_LSN, nullptr);
104     evictor_test_helpers::set_hysteresis_limits(&ct->ev, cachetable_size_limit, cachetable_size_limit);
105     evictor_test_helpers::disable_ev_thread(&ct->ev);
106     const char *fname1 = TOKU_TEST_FILENAME;
107     unlink(fname1);
108     CACHEFILE f1;
109     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
110 
111     int i;
112     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
113     wc.flush_callback = flush;
114 
115     // prefetch keys 0 .. N-1.  they should all fit in the cachetable
116     for (i=0; i<cachetable_size_limit+1; i++) {
117         CACHEKEY key = make_blocknum(i);
118         uint32_t fullhash = toku_cachetable_hash(f1, key);
119         bool doing_prefetch = false;
120         r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, &doing_prefetch);
121         assert(doing_prefetch);
122         toku_cachetable_verify(ct);
123     }
124 
125     // wait for all of the blocks to be fetched
126     sleep(3);
127 
128     // prefetch keys N .. 2*N-1.  0 .. N-1 should be evicted.
129     for (i=i+1; i<2*cachetable_size_limit; i++) {
130         CACHEKEY key = make_blocknum(i);
131         uint32_t fullhash = toku_cachetable_hash(f1, key);
132         bool doing_prefetch = false;
133         r = toku_cachefile_prefetch(f1, key, fullhash, wc, fetch, def_pf_req_callback, def_pf_callback, 0, &doing_prefetch);
134         assert(!doing_prefetch);
135         toku_cachetable_verify(ct);
136     }
137 
138 
139     toku_cachefile_close(&f1, false, ZERO_LSN);
140     if (verbose) printf("%s:%d 0x%x 0x%x\n", __FUNCTION__, __LINE__,
141         evicted_keys, (1 << (2*cachetable_size_limit))-1);
142     toku_cachetable_close(&ct);
143 }
144 
145 int
test_main(int argc,const char * argv[])146 test_main(int argc, const char *argv[]) {
147     default_parse_args(argc, argv);
148     cachetable_prefetch_flowcontrol_test(8);
149     return 0;
150 }
151