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 #include "toku_pthread.h"
41 #include <db.h>
42 #include <sys/stat.h>
43 #include "ydb-internal.h"
44 
45 #include "test_kv_gen.h"
46 
47 /*
48  */
49 
50 DB_ENV *env;
51 enum {MAX_NAME=128};
52 int NUM_DBS=5;
53 int NUM_ROWS=100000;
54 int CHECK_RESULTS=0;
55 int SRC_VERSION = 4;
56 int littlenode = 0;
57 
58 #define OLDDATADIR "../../../../tokudb.data/"
59 
60 char *env_dir = TOKU_TEST_FILENAME; // the default env_dir.
61 char *db_v5_dir = "dir.preload-db.c.tdb";
62 char *db_v4_dir        = OLDDATADIR "env_preload.4.2.0.cleanshutdown";
63 char *db_v4_dir_node4k = OLDDATADIR "env_preload.4.2.0.node4k.cleanshutdown";
64 
65 
upgrade_test_2(DB ** dbs)66 static void upgrade_test_2(DB **dbs) {
67     int r = 0;
68     // open the DBS
69     {
70         DBT desc;
71         dbt_init(&desc, "foo", sizeof("foo"));
72         char name[MAX_NAME*2];
73 
74         int idx[MAX_DBS];
75         for(int i=0;i<NUM_DBS;i++) {
76             idx[i] = i;
77             r = db_create(&dbs[i], env, 0);                                                                       CKERR(r);
78             dbs[i]->app_private = &idx[i];
79             snprintf(name, sizeof(name), "db_%04x", i);
80             r = dbs[i]->open(dbs[i], NULL, name, NULL, DB_BTREE, DB_CREATE, 0666);                                CKERR(r);
81             IN_TXN_COMMIT(env, NULL, txn_desc, 0, {
82                     { int chk_r = dbs[i]->change_descriptor(dbs[i], txn_desc, &desc, 0); CKERR(chk_r); }
83             });
84         }
85     }
86     // close
87     {
88         for(int i=0;i<NUM_DBS;i++) {
89             dbs[i]->close(dbs[i], 0);                                                                             CKERR(r);
90             dbs[i] = NULL;
91         }
92     }
93     // open
94     {
95         DBT desc;
96         dbt_init(&desc, "foo", sizeof("foo"));
97         char name[MAX_NAME*2];
98 
99         int idx[MAX_DBS];
100         for(int i=0;i<NUM_DBS;i++) {
101             idx[i] = i;
102             r = db_create(&dbs[i], env, 0);                                                                       CKERR(r);
103             dbs[i]->app_private = &idx[i];
104             snprintf(name, sizeof(name), "db_%04x", i);
105             r = dbs[i]->open(dbs[i], NULL, name, NULL, DB_BTREE, DB_CREATE, 0666);                                CKERR(r);
106             IN_TXN_COMMIT(env, NULL, txn_desc, 0, {
107                     { int chk_r = dbs[i]->change_descriptor(dbs[i], txn_desc, &desc, 0); CKERR(chk_r); }
108             });
109         }
110     }
111 
112     // read and verify all rows
113     {
114         if ( verbose ) {printf("checking");fflush(stdout);}
115         check_results(env, dbs, NUM_DBS, NUM_ROWS);
116         if ( verbose) {printf("\ndone\n");fflush(stdout);}
117     }
118     // close
119     {
120         for(int i=0;i<NUM_DBS;i++) {
121             dbs[i]->close(dbs[i], 0);                                                                             CKERR(r);
122             dbs[i] = NULL;
123         }
124     }
125 }
126 
127 
setup(void)128 static void setup(void) {
129     int r;
130     int len = 256;
131     char syscmd[len];
132     char * src_db_dir;
133 
134     if ( SRC_VERSION == 4 ) {
135 	if (littlenode)
136 	    src_db_dir = db_v4_dir_node4k;
137 	else
138 	    src_db_dir = db_v4_dir;
139     }
140     else if ( SRC_VERSION == 5 ) {
141         src_db_dir = db_v5_dir;
142     }
143     else {
144         fprintf(stderr, "unsupported PerconaFT version %d to upgrade\n", SRC_VERSION);
145         assert(0);
146     }
147 
148     r = snprintf(syscmd, len, "rm -rf %s", env_dir);
149     assert(r<len);
150     r = system(syscmd);
151     CKERR(r);
152 
153     r = snprintf(syscmd, len, "cp -r %s %s", src_db_dir, env_dir);
154     assert(r<len);
155     r = system(syscmd);
156     CKERR(r);
157     generate_permute_tables();
158 
159 }
160 
run_test(int checkpoint_period)161 static void run_test(int checkpoint_period)
162 {
163     int r;
164 
165     r = db_env_create(&env, 0);                                                                               CKERR(r);
166     if (littlenode) {
167 	r = env->set_cachesize(env, 0, 512*1024, 1);                                                              CKERR(r);
168     }
169     r = env->set_redzone(env, 0);                                                                             CKERR(r);
170     int envflags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE;
171     r = env->open(env, env_dir, envflags, S_IRWXU+S_IRWXG+S_IRWXO);                                            CKERR(r);
172     env->set_errfile(env, stderr);
173     r = env->checkpointing_set_period(env, checkpoint_period);                                                  CKERR(r);
174 
175     DB **dbs = (DB**)toku_malloc(sizeof(DB*) * NUM_DBS);
176     assert(dbs != NULL);
177 
178     // --------------------------
179     upgrade_test_2(dbs);
180     // --------------------------
181 
182     if (verbose >= 2)
183 	print_engine_status(env);
184     r = env->close(env, 0);                                                                                   CKERR(r);
185     toku_free(dbs);
186 
187 }
188 
189 // ------------ infrastructure ----------
190 static void do_args(int argc, char * const argv[]);
191 
192 
193 
test_main(int argc,char * const * argv)194 int test_main(int argc, char * const *argv) {
195     do_args(argc, argv);
196     if (SRC_VERSION == 4) {
197 	littlenode = 1;  // 4k nodes, small cache
198     }
199     setup();
200     run_test(1);
201     return 0;
202 }
203 
do_args(int argc,char * const argv[])204 static void do_args(int argc, char * const argv[]) {
205     int resultcode;
206     char *cmd = argv[0];
207     argc--; argv++;
208 
209     while (argc>0) {
210 	if (strcmp(argv[0], "-v")==0) {
211 	    verbose++;
212 	} else if (strcmp(argv[0],"-q")==0) {
213 	    verbose--;
214 	    if (verbose<0) verbose=0;
215         } else if (strcmp(argv[0], "-h")==0) {
216 	    resultcode=0;
217 	do_usage:
218 	    fprintf(stderr, "Usage: -h -c -d <num_dbs> -r <num_rows> %s\n", cmd);
219 	    exit(resultcode);
220         } else if (strcmp(argv[0], "-d")==0) {
221             argc--; argv++;
222             NUM_DBS = atoi(argv[0]);
223             if ( NUM_DBS > MAX_DBS ) {
224                 fprintf(stderr, "max value for -d field is %d\n", MAX_DBS);
225                 resultcode=1;
226                 goto do_usage;
227             }
228         } else if (strcmp(argv[0], "-r")==0) {
229             argc--; argv++;
230             NUM_ROWS = atoi(argv[0]);
231         } else if (strcmp(argv[0], "-c")==0) {
232             CHECK_RESULTS = 1;
233         } else if (strcmp(argv[0], "-V")==0) {
234             argc--; argv++;
235             SRC_VERSION = atoi(argv[0]);
236 	} else {
237 	    fprintf(stderr, "Unknown arg: %s\n", argv[0]);
238 	    resultcode=1;
239 	    goto do_usage;
240 	}
241 	argc--;
242 	argv++;
243     }
244 }
245