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 <sys/wait.h>
41 
42 // Verify that if tokudb crashes during recovery, then the prepared transactions are still prepared.
43 
clean_env(const char * envdir)44 static void clean_env (const char *envdir) {
45     const int len = strlen(envdir)+100;
46     char cmd[len];
47     snprintf(cmd, len, "rm -rf %s", envdir);
48     int r = system(cmd);
49     CKERR(r);
50     CKERR(toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO));
51 }
52 
setup_env(DB_ENV ** envp,const char * envdir)53 static void setup_env (DB_ENV **envp, const char *envdir) {
54     { int chk_r = db_env_create(envp, 0); CKERR(chk_r); }
55     (*envp)->set_errfile(*envp, stderr);
56     { int chk_r = (*envp)->set_redzone(*envp, 0); CKERR(chk_r); }
57     { int chk_r = (*envp)->open(*envp, envdir, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE|DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
58 }
59 
setup_env_and_prepare(DB_ENV ** envp,const char * envdir,bool commit)60 static void setup_env_and_prepare (DB_ENV **envp, const char *envdir, bool commit) {
61     DB *db;
62     DB_TXN *txn;
63     clean_env(envdir);
64     setup_env(envp, envdir);
65     CKERR(db_create(&db, *envp, 0));
66     CKERR(db->open(db, NULL, "foo.db", 0, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, S_IRWXU+S_IRWXG+S_IRWXO));
67     CKERR((*envp)->txn_begin(*envp, 0, &txn, 0));
68     DBT key;
69     dbt_init(&key, "foo", 4);
70     CKERR(db->put(db, txn, &key, &key, 0));
71     { int chk_r = db->close(db, 0); CKERR(chk_r); }
72     uint8_t gid[DB_GID_SIZE];
73     memset(gid, 0, DB_GID_SIZE);
74     gid[0]=42;
75     CKERR(txn->prepare(txn, gid, 0));
76     if (commit)
77 	CKERR(txn->commit(txn, 0));
78 }
79 
test(void)80 static void test (void) {
81     pid_t pid;
82 
83     if (0==(pid=fork())) {
84 	DB_ENV *env;
85 	setup_env_and_prepare(&env, TOKU_TEST_FILENAME, false);
86 	{
87 	    DB_PREPLIST l[1];
88 	    long count=-1;
89 	    CKERR(env->txn_recover(env, l, 1, &count, DB_FIRST));
90 	    printf("%s:%d count=%ld\n", __FILE__, __LINE__, count);
91 	    assert(count==1);
92 	    assert(l[0].gid[0]==42);
93 	}
94 	exit(0);
95     }
96     {
97 	int status;
98 	pid_t pid2 = wait(&status);
99 	assert(pid2==pid);
100 	assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
101     }
102     // Now run recovery and crash on purpose.
103     if (0==(pid=fork())) {
104 	DB_ENV *env;
105 	setup_env(&env, TOKU_TEST_FILENAME);
106 
107 	// make sure there is 1 prepared txn.
108 	{
109 	    DB_PREPLIST l[1];
110 	    long count=-1;
111 	    int r = env->txn_recover(env, l, 1, &count, DB_FIRST);
112 	    printf("r=%d count=%ld\n", r, count);
113 	    assert(count==1);
114 	    assert(l[0].gid[0]==42);
115 	    for (int i=1; i<DB_GID_SIZE; i++) {
116 		assert(l[0].gid[i]==0);
117 	    }
118 	}
119 
120 	exit(0);
121     }
122     {
123 	int status;
124 	pid_t pid2 = wait(&status);
125 	assert(pid2==pid);
126 	assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
127     }
128 
129     // Now see if recovery works the second time.
130     DB_ENV *env;
131     setup_env(&env, TOKU_TEST_FILENAME);
132     {
133 	DB_PREPLIST l[1];
134 	long count=-1;
135 	int r = env->txn_recover(env, l, 1, &count, DB_FIRST);
136 	printf("r=%d count=%ld\n", r, count);
137 	assert(count==1);
138 	assert(l[0].gid[0]==42);
139 	for (int i=1; i<DB_GID_SIZE; i++) {
140 	    assert(l[0].gid[i]==0);
141 	}
142 	{ int chk_r = l->txn->commit(l->txn, 0); CKERR(chk_r); }
143     }
144     { int chk_r = env ->close(env,  0); CKERR(chk_r); }
145 }
146 
test_main(int argc,char * const argv[])147 int test_main (int argc, char *const argv[]) {
148     default_parse_args(argc, argv);
149     // first test: open an environment, a db, a txn, and do a prepare.   Then do txn_prepare (without even closing the environment).
150     test();
151 
152 
153     // second test: poen environment, a db, a txn, prepare, close the environment.  Then reopen and do txn_prepare.
154 
155     // third test: make sure there is an fsync on txn_prepare, but not on the following commit.
156 
157 
158     // Then close the environment Find out what BDB does when ask for the txn prepares.
159     // Other tests: read prepared txns, 1 at a time. Then close it and read them again.
160     return 0;
161 }
162