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 the table lock log entry is handled
40 
41 #include <sys/stat.h>
42 #include "test.h"
43 
44 
45 const int envflags = DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE;
46 const char *namea="a.db";
47 
48 DB_ENV *env;
49 DB_TXN *tid;
50 DB     *db;
51 DBT key,data;
52 
53 static void
do_x1_shutdown(void)54 do_x1_shutdown (void) {
55     int r;
56     toku_os_recursive_delete(TOKU_TEST_FILENAME);
57     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
58     CKERR(r);
59 
60     r=db_env_create(&env, 0);                                                  assert(r==0);
61     env->set_errfile(env, stderr);
62     r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE|DB_THREAD, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
63     {
64         DB_TXN *oldest;
65         r=env->txn_begin(env, 0, &oldest, 0);
66         CKERR(r);
67     }
68 
69     r=db_create(&db, env, 0);                                                  CKERR(r);
70     r=env->txn_begin(env, 0, &tid, 0);                                         assert(r==0);
71     r=db->open(db, tid, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);               CKERR(r);
72     r=tid->commit(tid, 0);                                                     assert(r==0);
73 
74     r=env->txn_begin(env, 0, &tid, 0);                                         assert(r==0);
75     r=db->put(db, tid, dbt_init(&key, "a", 2), dbt_init(&data, "b", 2), 0);    assert(r==0);
76     r=tid->commit(tid, 0);                                                     assert(r==0);
77 
78     r=db->close(db, 0);                                                        assert(r==0);
79 
80     //printf("shutdown\n");
81     toku_hard_crash_on_purpose();
82 }
83 
84 static void
do_x1_recover(bool UU (did_commit))85 do_x1_recover (bool UU(did_commit)) {
86     int r;
87     char glob[TOKU_PATH_MAX+1];
88     toku_os_recursive_delete(toku_path_join(glob, 2, TOKU_TEST_FILENAME, "*.tokudb"));
89 
90     r = db_env_create(&env, 0);                                                             CKERR(r);
91     r = env->open(env, TOKU_TEST_FILENAME, envflags|DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO);               CKERR(r);
92 
93     r=env->txn_begin(env, 0, &tid, 0);                                         assert(r==0);
94     r=db_create(&db, env, 0);                                                  CKERR(r);
95     r=db->open(db, tid, "foo.db", 0, DB_BTREE, 0, S_IRWXU+S_IRWXG+S_IRWXO);                       CKERR(r);
96     r=db->get(db, tid, dbt_init(&key, "a", 2), dbt_init_malloc(&data), 0);     assert(r==0);
97     r=tid->commit(tid, 0);                                                     assert(r==0);
98     toku_free(data.data);
99     r=db->close(db, 0);                                                        CKERR(r);
100     r=env->close(env, 0);                                                      CKERR(r);
101 }
102 
103 bool do_commit=false, do_recover_committed=false;
104 
105 static void
x1_parse_args(int argc,char * const argv[])106 x1_parse_args (int argc, char * const argv[]) {
107     int resultcode;
108     char *cmd = argv[0];
109     argc--; argv++;
110     while (argc>0) {
111 	if (strcmp(argv[0], "-v") == 0) {
112 	    verbose++;
113 	} else if (strcmp(argv[0],"-q")==0) {
114 	    verbose--;
115 	    if (verbose<0) verbose=0;
116 	} else if (strcmp(argv[0], "--test") == 0) {
117 	    do_commit=true;
118 	} else if (strcmp(argv[0], "--recover") == 0) {
119 	    do_recover_committed=true;
120 	} else if (strcmp(argv[0], "-h")==0) {
121 	    resultcode=0;
122 	do_usage:
123 	    fprintf(stderr, "Usage:\n%s [-v|-q]* [-h] {--commit | --abort | --explicit-abort | --recover-committed | --recover-aborted } \n", cmd);
124 	    exit(resultcode);
125 	} else {
126 	    fprintf(stderr, "Unknown arg: %s\n", argv[0]);
127 	    resultcode=1;
128 	    goto do_usage;
129 	}
130 	argc--;
131 	argv++;
132     }
133     {
134 	int n_specified=0;
135 	if (do_commit)            n_specified++;
136 	if (do_recover_committed) n_specified++;
137 	if (n_specified>1) {
138 	    printf("Specify only one of --commit or --abort or --recover-committed or --recover-aborted\n");
139 	    resultcode=1;
140 	    goto do_usage;
141 	}
142     }
143 }
144 
145 int
test_main(int argc,char * const argv[])146 test_main (int argc, char * const argv[])
147 {
148     x1_parse_args(argc, argv);
149     if (do_commit) {
150 	do_x1_shutdown();
151     } else if (do_recover_committed) {
152 	do_x1_recover(true);
153     }
154 #if 0
155     else {
156 	do_test();
157     }
158 #endif
159     return 0;
160 }
161