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 
71 // callback functions for toku_ft_flush_some_child
destroy_bn(void * UU (extra))72 static bool destroy_bn(void *UU(extra)) { return true; }
73 
recursively_flush_should_not_happen(FTNODE UU (child),void * UU (extra))74 static bool recursively_flush_should_not_happen(FTNODE UU(child),
75                                                 void *UU(extra)) {
76   assert(false);
77 }
78 
child_to_flush(FT UU (h),FTNODE parent,void * UU (extra))79 static int child_to_flush(FT UU(h), FTNODE parent, void *UU(extra)) {
80   assert(parent->height == 1);
81   assert(parent->n_children == 1);
82   return 0;
83 }
84 
dummy_update_status(FTNODE UU (child),int UU (dirtied),void * UU (extra))85 static void dummy_update_status(FTNODE UU(child), int UU(dirtied),
86                                 void *UU(extra)) {}
87 
doit()88 static void doit() {
89   BLOCKNUM node_leaf;
90   BLOCKNUM node_root;
91   BLOCKNUM node_internal;
92   int r;
93 
94   toku_cachetable_create(&ct, 500 * 1024 * 1024, ZERO_LSN, nullptr);
95   unlink(fname);
96   r = toku_open_ft_handle(fname, 1, &ft, NODESIZE, NODESIZE / 2,
97                           TOKU_DEFAULT_COMPRESSION_METHOD, ct, null_txn,
98                           toku_builtin_compare_fun);
99   assert(r == 0);
100 
101   ft->options.update_fun = update_func;
102   ft->ft->update_fun = update_func;
103 
104   toku_testsetup_initialize(); // must precede any other toku_testsetup calls
105   char *pivots[1];
106   pivots[0] = toku_strdup("kkkkk");
107   int pivot_len = 6;
108   r = toku_testsetup_leaf(ft, &node_leaf, 2, pivots, &pivot_len);
109   assert(r == 0);
110 
111   toku_free(pivots[0]);
112 
113   r = toku_testsetup_nonleaf(ft, 1, &node_internal, 1, &node_leaf, 0, 0);
114   assert(r == 0);
115 
116   r = toku_testsetup_nonleaf(ft, 2, &node_root, 1, &node_internal, 0, 0);
117   assert(r == 0);
118 
119   r = toku_testsetup_root(ft, node_root);
120   assert(r == 0);
121 
122   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
123                                     "a", // key
124                                     2,   // keylen
125                                     "aa", 3);
126   assert(r == 0);
127 
128   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
129                                     "z", // key
130                                     2,   // keylen
131                                     "zz", 3);
132   assert(r == 0);
133   char filler[400];
134   memset(filler, 0, sizeof(filler));
135   // now we insert filler data so that the rebalance
136   // keeps it at two nodes
137   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
138                                     "b", // key
139                                     2,   // keylen
140                                     filler, sizeof(filler));
141   assert(r == 0);
142   r = toku_testsetup_insert_to_leaf(ft, node_leaf,
143                                     "y", // key
144                                     2,   // keylen
145                                     filler, sizeof(filler));
146   assert(r == 0);
147 
148   r = toku_testsetup_insert_to_nonleaf(ft, node_internal, FT_INSERT,
149                                        "a", // key
150                                        2,   // keylen
151                                        "yy", 3);
152   assert(r == 0);
153 
154   r = toku_testsetup_insert_to_nonleaf(ft, node_root, FT_INSERT,
155                                        "a", // key
156                                        2,   // keylen
157                                        "zz", 3);
158   assert(r == 0);
159 
160   // at this point of time, the logical row count will be 6. This has to be
161   // manually set up as the tests work under the interface of the ft_send_msg
162   ft->ft->in_memory_logical_rows = 6;
163   // now run a checkpoint to get everything clean
164   CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
165   r = toku_checkpoint(cp, NULL, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);
166   assert_zero(r);
167   // now do a lookup on one of the keys, this should bring a leaf node up to
168   // date
169   DBT k;
170   struct check_pair pair = {2, "a", 3, "zz", 0};
171   r = toku_ft_lookup(ft, toku_fill_dbt(&k, "a", 2), lookup_checkf, &pair);
172   assert(r == 0);
173   assert(ft->ft->in_memory_logical_rows == 4);
174 
175   // now lock and release the leaf node to make sure it is what we expect it to
176   // be.
177   FTNODE node = NULL;
178   ftnode_fetch_extra bfe;
179   bfe.create_for_min_read(ft->ft);
180   toku_pin_ftnode_with_dep_nodes(
181       ft->ft, node_internal, toku_cachetable_hash(ft->ft->cf, node_internal),
182       &bfe, PL_WRITE_EXPENSIVE, 0, NULL, &node, true);
183   assert(node->height == 1);
184   assert(node->n_children == 1);
185 
186   struct flusher_advice fa;
187   flusher_advice_init(&fa, child_to_flush, destroy_bn,
188                       recursively_flush_should_not_happen, default_merge_child,
189                       dummy_update_status, default_pick_child_after_split,
190                       NULL);
191 
192   // do the flush which forces an evict of the leaf. logical row count back to
193   // 6 before the flush
194   toku_ft_flush_some_child(ft->ft, node, &fa);
195 
196   assert(ft->ft->in_memory_logical_rows == 5);
197 
198   r = toku_close_ft_handle_nolsn(ft, 0);
199   assert(r == 0);
200   toku_cachetable_close(&ct);
201 }
202 
test_main(int argc,const char * argv[])203 int test_main(int argc __attribute__((__unused__)),
204               const char *argv[] __attribute__((__unused__))) {
205   default_parse_args(argc, argv);
206   doit();
207   return 0;
208 }
209