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                                                                         \
38     "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
39 
40 /* The goal of this test.  Make sure that inserts stay behind deletes. */
41 
42 #include "test.h"
43 
44 #include "cachetable/checkpoint.h"
45 #include "ft-flusher-internal.h"
46 #include "ft-flusher.h"
47 #include <ft-cachetable-wrappers.h>
48 
49 static TOKUTXN const null_txn = 0;
50 
51 enum { NODESIZE = 1024, KSIZE = NODESIZE - 100, TOKU_PSIZE = 20 };
52 
53 CACHETABLE ct;
54 FT_HANDLE ft;
55 const char *fname = TOKU_TEST_FILENAME;
56 
update_func(DB * UU (db),const DBT * key,const DBT * old_val,const DBT * UU (extra),void (* set_val)(const DBT * new_val,void * set_extra),void * set_extra)57 static int update_func(DB *UU(db), const DBT *key, const DBT *old_val,
58                        const DBT *UU(extra),
59                        void (*set_val)(const DBT *new_val, void *set_extra),
60                        void *set_extra) {
61   DBT new_val;
62   assert(old_val->size > 0);
63   if (verbose) {
64     printf("applying update to %s\n", (char *)key->data);
65   }
66   toku_init_dbt(&new_val);
67   set_val(&new_val, set_extra);
68   return 0;
69 }
70 
doit()71 static void doit() {
72   BLOCKNUM node_leaf;
73   BLOCKNUM node_root;
74   BLOCKNUM node_internal;
75   int r;
76 
77   toku_cachetable_create(&ct, 500 * 1024 * 1024, ZERO_LSN, nullptr);
78   unlink(fname);
79   r = toku_open_ft_handle(fname, 1, &ft, NODESIZE, NODESIZE / 2,
80                           TOKU_DEFAULT_COMPRESSION_METHOD, ct, null_txn,
81                           toku_builtin_compare_fun);
82   assert(r == 0);
83 
84   ft->options.update_fun = update_func;
85   ft->ft->update_fun = update_func;
86 
87   toku_testsetup_initialize(); // must precede any other toku_testsetup calls
88   char *pivots[1];
89   pivots[0] = toku_strdup("kkkkk");
90   int pivot_len = 6;
91   r = toku_testsetup_leaf(ft, &node_leaf, 2, pivots, &pivot_len);
92   assert(r == 0);
93 
94   toku_free(pivots[0]);
95 
96   r = toku_testsetup_nonleaf(ft, 1, &node_internal, 1, &node_leaf, 0, 0);
97   assert(r == 0);
98 
99   r = toku_testsetup_nonleaf(ft, 2, &node_root, 1, &node_internal, 0, 0);
100   assert(r == 0);
101 
102   r = toku_testsetup_root(ft, node_root);
103   assert(r == 0);
104 
105   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
106                                     "a", // key
107                                     2,   // keylen
108                                     "aa", 3);
109   assert(r == 0);
110 
111   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
112                                     "z", // key
113                                     2,   // keylen
114                                     "zz", 3);
115   assert(r == 0);
116   char filler[400];
117   memset(filler, 0, sizeof(filler));
118   // now we insert filler data so that the rebalance
119   // keeps it at two nodes
120   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
121                                     "b", // key
122                                     2,   // keylen
123                                     filler, sizeof(filler));
124   assert(r == 0);
125   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
126                                     "y", // key
127                                     2,   // keylen
128                                     filler, sizeof(filler));
129   assert(r == 0);
130 
131   r = toku_testsetup_insert_to_nonleaf(ft, node_internal, FT_INSERT,
132                                        "a", // key
133                                        2,   // keylen
134                                        "yy", 3);
135   assert(r == 0);
136 
137   r = toku_testsetup_insert_to_nonleaf(ft, node_root, FT_INSERT,
138                                        "a", // key
139                                        2,   // keylen
140                                        "zz", 3);
141   assert(r == 0);
142 
143   // at this point of time, the logical row count will be 6. This has to be
144   // manually set up as the tests work under the interface of the ft_send_msg
145   ft->ft->in_memory_logical_rows = 6;
146   // now run a checkpoint to get everything clean
147   CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
148   r = toku_checkpoint(cp, NULL, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);
149   assert_zero(r);
150   // now do a lookup on one of the keys, this should bring a leaf node up to
151   // date
152   DBT k;
153   struct check_pair pair = {2, "a", 3, "zz", 0};
154   r = toku_ft_lookup(ft, toku_fill_dbt(&k, "a", 2), lookup_checkf, &pair);
155   assert(r == 0);
156   assert(ft->ft->in_memory_logical_rows == 4);
157   FTNODE node;
158   // now lock and release the leaf node to make sure it is what we expect it to
159   // be.
160   toku_pin_node_with_min_bfe(&node, node_leaf, ft);
161   for (int i = 0; i < 20; i++) {
162     toku_ftnode_pe_callback(node, make_pair_attr(0xffffffff), ft->ft,
163                             def_pe_finalize_impl, nullptr);
164   }
165   toku_unpin_ftnode(ft->ft, node);
166   assert(ft->ft->in_memory_logical_rows == 6);
167 
168   r = toku_close_ft_handle_nolsn(ft, 0);
169   assert(r == 0);
170   toku_cachetable_close(&ct);
171 }
172 
test_main(int argc,const char * argv[])173 int test_main(int argc __attribute__((__unused__)),
174               const char *argv[] __attribute__((__unused__))) {
175   default_parse_args(argc, argv);
176   doit();
177   return 0;
178 }
179