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