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 int num_entries;
42 bool flush_may_occur;
43 int expected_flushed_key;
44 bool check_flush;
45 
46 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))47 flush (CACHEFILE f __attribute__((__unused__)),
48        int UU(fd),
49        CACHEKEY k  __attribute__((__unused__)),
50        void *v     __attribute__((__unused__)),
51        void** UU(dd),
52        void *e     __attribute__((__unused__)),
53        PAIR_ATTR s      __attribute__((__unused__)),
54        PAIR_ATTR* new_size      __attribute__((__unused__)),
55        bool w      __attribute__((__unused__)),
56        bool keep   __attribute__((__unused__)),
57        bool c      __attribute__((__unused__)),
58         bool UU(is_clone)
59        ) {
60     /* Do nothing */
61     if (check_flush && !keep) {
62         if (verbose) { printf("FLUSH: %d write_me %d expected %d\n", (int)k.b, w, expected_flushed_key); }
63         assert(flush_may_occur);
64         assert(!w);
65         assert(expected_flushed_key == (int)k.b);
66         expected_flushed_key--;
67     }
68 }
69 
70 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)71 fetch (CACHEFILE f        __attribute__((__unused__)),
72        PAIR UU(p),
73        int UU(fd),
74        CACHEKEY k         __attribute__((__unused__)),
75        uint32_t fullhash __attribute__((__unused__)),
76        void **value       __attribute__((__unused__)),
77        void** UU(dd),
78        PAIR_ATTR *sizep        __attribute__((__unused__)),
79        int  *dirtyp,
80        void *extraargs    __attribute__((__unused__))
81        ) {
82     *dirtyp = 0;
83     *value = NULL;
84     *sizep = make_pair_attr(1);
85     return 0;
86 }
87 
88 static void
cachetable_test(void)89 cachetable_test (void) {
90     const int test_limit = 4;
91     num_entries = 0;
92     int r;
93     CACHETABLE ct;
94     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
95     const char *fname1 = TOKU_TEST_FILENAME;
96     unlink(fname1);
97     CACHEFILE f1;
98     r = toku_cachetable_openf(&f1, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
99 
100     void* v1;
101     void* v2;
102     flush_may_occur = false;
103     check_flush = true;
104     CACHETABLE_WRITE_CALLBACK wc = def_write_callback(NULL);
105     wc.flush_callback = flush;
106     for (int i = 0; i < 100000; i++) {
107       r = toku_cachetable_get_and_pin(f1, make_blocknum(1), 1, &v1, wc, fetch, def_pf_req_callback, def_pf_callback, true, NULL);
108         r = toku_test_cachetable_unpin(f1, make_blocknum(1), 1, CACHETABLE_CLEAN, make_pair_attr(1));
109     }
110     for (int i = 0; i < 8; i++) {
111       r = toku_cachetable_get_and_pin(f1, make_blocknum(2), 2, &v2, wc, fetch, def_pf_req_callback, def_pf_callback, true, NULL);
112         r = toku_test_cachetable_unpin(f1, make_blocknum(2), 2, CACHETABLE_CLEAN, make_pair_attr(1));
113     }
114     for (int i = 0; i < 4; i++) {
115       r = toku_cachetable_get_and_pin(f1, make_blocknum(3), 3, &v2, wc, fetch, def_pf_req_callback, def_pf_callback, true, NULL);
116         r = toku_test_cachetable_unpin(f1, make_blocknum(3), 3, CACHETABLE_CLEAN, make_pair_attr(1));
117     }
118     for (int i = 0; i < 2; i++) {
119       r = toku_cachetable_get_and_pin(f1, make_blocknum(4), 4, &v2, wc, fetch, def_pf_req_callback, def_pf_callback, true, NULL);
120         r = toku_test_cachetable_unpin(f1, make_blocknum(4), 4, CACHETABLE_CLEAN, make_pair_attr(1));
121     }
122     flush_may_occur = true;
123     expected_flushed_key = 4;
124     toku_cachetable_put(f1, make_blocknum(5), 5, NULL, make_pair_attr(1), wc, put_callback_nop);
125     ct->ev.signal_eviction_thread();
126     usleep(1*1024*1024);
127 
128     flush_may_occur = true;
129     r = toku_test_cachetable_unpin(f1, make_blocknum(5), 5, CACHETABLE_CLEAN, make_pair_attr(2));
130     ct->ev.signal_eviction_thread();
131     usleep(1*1024*1024);
132 
133     check_flush = false;
134     toku_cachefile_close(&f1, false, ZERO_LSN);
135     toku_cachetable_close(&ct);
136 }
137 
138 int
test_main(int argc,const char * argv[])139 test_main(int argc, const char *argv[]) {
140     default_parse_args(argc, argv);
141     cachetable_test();
142     return 0;
143 }
144