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 /* Test to see if a big nested transaction (so big that it's rollbacks spill into a file)
41  * can commit properly.
42  *  Four Tests:
43  *     big child aborts, parent aborts
44  *     big child aborts, parent commits
45  *     big child commits, parent aborts
46  *     big child commits, parent commits (This test)
47  */
48 
49 #include <db.h>
50 #include <sys/stat.h>
51 
52 static DB_ENV *env;
53 static DB *db;
54 static DB_TXN *xchild, *xparent;
55 
insert(int i)56 static void insert (int i) {
57     char hello[30], there[30];
58     DBT key,data;
59     if (verbose) printf("Insert %d\n", i);
60     snprintf(hello, sizeof(hello), "hello%d", i);
61     snprintf(there, sizeof(there), "there%d", i);
62     int r = db->put(db, xchild,
63 		    dbt_init(&key,  hello, strlen(hello)+1),
64 		    dbt_init(&data, there, strlen(there)+1),
65 		    0);
66     CKERR(r);
67 }
68 
lookup(int i,int expect,int expectj)69 static void lookup (int i, int expect, int expectj) {
70     char hello[30], there[30];
71     DBT key,data;
72     snprintf(hello, sizeof(hello), "hello%d", i);
73     memset(&data, 0, sizeof(data));
74     if (verbose) printf("Looking up %d (expecting %s)\n", i, expect==0 ? "to find" : "not to find");
75     int r = db->get(db, xchild,
76 		    dbt_init(&key,  hello, strlen(hello)+1),
77 		    &data,
78 		    0);
79     assert(expect==r);
80     if (expect==0) {
81 	CKERR(r);
82 	snprintf(there, sizeof(there), "there%d", expectj);
83 	assert(data.size==strlen(there)+1);
84 	assert(strcmp((char*)data.data, there)==0);
85     }
86 }
87 
88 int N = 50000;
89 
90 static void
test_commit_commit(void)91 test_commit_commit (void) {
92     int i, r;
93     r=env->txn_begin(env, 0, &xparent, 0);  CKERR(r);
94     r=env->txn_begin(env, xparent, &xchild, 0); CKERR(r);
95     for (i=0; i<N; i++) {
96 	insert(i);
97     }
98     r=xchild->commit(xchild, 0); CKERR(r);
99     r=env->txn_begin(env, xparent, &xchild, 0); CKERR(r);
100     for (i=0; i<N; i++) {
101 	lookup(i, 0, i);
102     }
103     r=xchild->commit(xchild, 0); CKERR(r);
104     r=xparent->commit(xparent, 0); CKERR(r);
105     r=env->txn_begin(env, 0, &xchild, 0); CKERR(r);
106     for (i=0; i<N; i++) {
107 	lookup(i, 0, i);
108     }
109     r=xchild->commit(xchild, 0); CKERR(r);
110 }
111 
112 static void
setup(void)113 setup (void) {
114     DB_TXN *txn;
115     int r;
116     toku_os_recursive_delete(TOKU_TEST_FILENAME);
117     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);       CKERR(r);
118 
119     r=db_env_create(&env, 0); CKERR(r);
120     r=env->set_redzone(env, 0); CKERR(r);
121 
122     env->set_errfile(env, stderr);
123     r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
124     r=db_create(&db, env, 0); CKERR(r);
125 
126     r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
127     r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
128     r=txn->commit(txn, 0);    assert(r==0);
129 }
130 
131 static void
test_shutdown(void)132 test_shutdown (void) {
133     int r;
134     r=db->close(db, 0); CKERR(r);
135     r=env->close(env, 0); CKERR(r);
136 }
137 
138 int
test_main(int argc,char * const argv[])139 test_main (int argc, char * const argv[]) {
140     parse_args(argc, argv);
141     setup();
142     test_commit_commit();
143     test_shutdown();
144     return 0;
145 }
146