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 
58 const unsigned int myformatid = 0x74736554;
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     TOKU_XA_XID x = {.formatID = myformatid,
73 		     .gtrid_length = 8,
74 		     .bqual_length = 9};
75     for (int i=0; i<8+9; i++) x.data[i] = 42+i;
76     CKERR(txn->xa_prepare(txn, &x, 0));
77     if (commit)
78 	CKERR(txn->commit(txn, 0));
79 }
80 
test1(void)81 static void test1 (void) {
82     pid_t pid;
83     bool do_fork = true;
84     if (!do_fork || 0==(pid=fork())) {
85 	DB_ENV *env;
86 	setup_env_and_prepare(&env, TOKU_TEST_FILENAME, false);
87 	{
88 	    TOKU_XA_XID l[1];
89 	    long count=-1;
90 	    CKERR(env->txn_xa_recover(env, l, 1, &count, DB_FIRST));
91 	    printf("%s:%d count=%ld\n", __FILE__, __LINE__, count);
92 	    assert(count==1);
93 	    assert(myformatid==l[0].formatID);
94 	    assert( 8==l[0].gtrid_length);
95 	    assert( 9==l[0].bqual_length);
96 	    for (int i=0; i<8+9; i++) {
97 		assert(l[0].data[i]==42+i);
98 	    }
99 	}
100 	exit(0);
101     }
102     int status;
103     if (do_fork) {
104 	pid_t pid2 = wait(&status);
105 	assert(pid2==pid);
106     }
107 
108     DB_ENV *env2;
109     char envdir2[TOKU_PATH_MAX+1];
110     setup_env_and_prepare(&env2, toku_path_join(envdir2, 2, TOKU_TEST_FILENAME, "envdir2"), true);
111 
112     // Now we can look at env2 in the debugger to see if we managed to make it the same
113 
114     DB_ENV *env;
115     setup_env(&env, TOKU_TEST_FILENAME);
116 
117     {
118 	TOKU_XA_XID l[1];
119 	long count=-1;
120 	{
121 	    int r = env->txn_xa_recover(env, l, 1, &count, DB_FIRST);
122 	    printf("r=%d count=%ld\n", r, count);
123 	}
124 	assert(count==1);
125 	assert(l[0].data[0]==42);
126 	assert(myformatid==l[0].formatID);
127 	assert( 8        ==l[0].gtrid_length);
128 	assert( 9        ==l[0].bqual_length);
129 	for (int i=0; i<8+9; i++) {
130 	    assert(l[0].data[i]==42+i);
131 	}
132 	{
133 	    DB_TXN *txn;
134 	    int r = env->get_txn_from_xid(env, &l[0], &txn);
135 	    assert(r==0);
136 	    { int chk_r = txn->commit(txn, 0); CKERR(chk_r); }
137 	}
138     }
139     { int chk_r = env2->close(env2, 0); CKERR(chk_r); }
140     { int chk_r = env ->close(env,  0); CKERR(chk_r); }
141 }
142 
test_main(int argc,char * const argv[])143 int test_main (int argc, char *const argv[]) {
144     default_parse_args(argc, argv);
145     // first test: open an environment, a db, a txn, and do a prepare.   Then do txn_prepare (without even closing the environment).
146     test1();
147 
148 
149     // second test: poen environment, a db, a txn, prepare, close the environment.  Then reopen and do txn_prepare.
150 
151     // third test: make sure there is an fsync on txn_prepare, but not on the following commit.
152 
153 
154     // Then close the environment Find out what BDB does when ask for the txn prepares.
155     // Other tests: read prepared txns, 1 at a time. Then close it and read them again.
156     return 0;
157 }
158