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 
41 /* Is it feasible to run 4 billion transactions in one test in the regression tests? */
42 #include <db.h>
43 #include <sys/stat.h>
44 #include <ft/logger/log.h>
45 #include <src/ydb_txn.h>
46 
47 static void
four_billion_subtransactions(int do_something_in_children,int use_big_increment)48 four_billion_subtransactions (int do_something_in_children, int use_big_increment) {
49     DB_ENV *env;
50     DB *db;
51     DB_TXN *xparent;
52 
53     uint64_t extra_increment;
54     if (use_big_increment) {
55 	extra_increment = (1<<28); // 1/4 of a billion, so 16 transactions should push us over the edge.
56     } else {
57 	extra_increment = 0; // xid is already incrementing once per txn.
58     }
59 
60     int r;
61     toku_os_recursive_delete(TOKU_TEST_FILENAME);
62     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
63 
64     r = db_env_create(&env, 0);                                           CKERR(r);
65 
66     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);
67     r = db_create(&db, env, 0);                                           CKERR(r);
68 
69     {
70 	DB_TXN *txn;
71 	r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
72 	r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
73 	r=txn->commit(txn, 0);    assert(r==0);
74     }
75 
76     r=env->txn_begin(env, 0, &xparent, 0);  CKERR(r);
77     long long i;
78     long long const fourbillion = use_big_increment ? 32 : 500000; // if using the big increment we should run into trouble in only 32 transactions or less.
79     for (i=0; i < fourbillion + 100; i++) {
80 	DB_TXN *xchild;
81         toku_increase_last_xid(env, extra_increment);
82 	r=env->txn_begin(env, xparent, &xchild, 0); CKERR(r);
83 	if (do_something_in_children) {
84 	    char hello[30], there[30];
85 	    snprintf(hello, sizeof(hello), "hello%lld", i);
86 	    snprintf(there, sizeof(there), "there%lld", i);
87 	    DBT key, val;
88 	    r=db->put(db, xchild,
89 		      dbt_init(&key, hello, strlen(hello)+1),
90 		      dbt_init(&val, there, strlen(there)+1),
91 		      0);
92 	    CKERR(r);
93 	}
94 	r=xchild->commit(xchild, 0); CKERR(r);
95     }
96     r=xparent->commit(xparent, 0); CKERR(r);
97 
98     r=db->close(db, 0); CKERR(r);
99     r=env->close(env, 0); CKERR(r);
100 }
101 
102 int
test_main(int argc,char * const argv[])103 test_main (int argc, char *const argv[])
104 {
105     parse_args(argc, argv);
106     four_billion_subtransactions(0, 0);
107     four_billion_subtransactions(1, 0);
108     four_billion_subtransactions(0, 1);
109     four_billion_subtransactions(1, 1);
110     return 0;
111 }
112 
113