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 medium-size nested transaction (the nested pieces are not too big, but the whole thing is 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 const int N = 50000;
89 const int DIV = 10;
90
91 static void
test_commit_commit(void)92 test_commit_commit (void) {
93 int i, j, k, r;
94 r=env->txn_begin(env, 0, &xparent, 0); CKERR(r);
95 k=0;
96 for (j=0; j<DIV; j++) {
97 r=env->txn_begin(env, xparent, &xchild, 0); CKERR(r);
98 for (i=0; i<N/DIV; i++) {
99 insert(k);
100 k++;
101 }
102 r=xchild->commit(xchild, 0); CKERR(r);
103 }
104 k=0;
105 for (j=0; j<DIV; j++) {
106 r=env->txn_begin(env, xparent, &xchild, 0); CKERR(r);
107 for (i=0; i<N/DIV; i++) {
108 lookup(k, 0, k);
109 k++;
110 }
111 r=xchild->commit(xchild, 0); CKERR(r);
112 }
113 r=xparent->commit(xparent, 0); CKERR(r);
114 r=env->txn_begin(env, 0, &xchild, 0); CKERR(r);
115 for (i=0; i<N; i++) {
116 lookup(i, 0, i);
117 }
118 r=xchild->commit(xchild, 0); CKERR(r);
119 }
120
121 static void
test_setup(void)122 test_setup (void) {
123 DB_TXN *txn;
124 int r;
125 toku_os_recursive_delete(TOKU_TEST_FILENAME);
126 r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
127
128 r=db_env_create(&env, 0); CKERR(r);
129 env->set_errfile(env, stderr);
130 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);
131 r=db_create(&db, env, 0); CKERR(r);
132
133 r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
134 r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
135 r=txn->commit(txn, 0); assert(r==0);
136 }
137
138 static void
test_shutdown(void)139 test_shutdown (void) {
140 int r;
141 r=db->close(db, 0); CKERR(r);
142 r=env->close(env, 0); CKERR(r);
143 }
144
145 int
test_main(int argc,char * const argv[])146 test_main (int argc, char * const argv[]) {
147 parse_args(argc, argv);
148 test_setup();
149 test_commit_commit();
150 test_shutdown();
151 return 0;
152 }
153