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 <stdio.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <memory.h>
45 #include <sys/stat.h>
46 #include <db.h>
47 
48 // Recreate a mysqld crash by closing and opening a db within a transaction.
49 // The crash occurs when writing a dirty cachetable pair, so we insert one
50 // row.
51 static void
test_txn_close_before_prepare_commit(void)52 test_txn_close_before_prepare_commit (void) {
53 
54     toku_os_recursive_delete(TOKU_TEST_FILENAME);
55     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
56 
57     int r;
58     DB_ENV *env;
59     r = db_env_create(&env, 0); assert(r == 0);
60     env->set_errfile(env, stdout);
61     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL + DB_INIT_LOG + DB_INIT_LOCK + DB_INIT_TXN + DB_PRIVATE + DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
62     if (r != 0) printf("%s:%d:%d:%s\n", __FILE__, __LINE__, r, db_strerror(r));
63     assert(r == 0);
64 
65     DB *db;
66     r = db_create(&db, env, 0); assert(r == 0);
67     r = db->open(db, NULL, "test.db", 0, DB_BTREE, DB_CREATE|DB_AUTO_COMMIT, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
68 
69     DB_TXN *txn = 0;
70     r = env->txn_begin(env, 0, &txn, 0); assert(r == 0);
71 
72     DBT key, val;
73     int k = 1, v = 1;
74     r = db->put(db, txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
75     assert(r == 0);
76 
77     // Close before commit
78     r = db->close(db, 0); assert(r == 0);
79 
80     uint8_t gid[DB_GID_SIZE];
81     memset(gid, 1, DB_GID_SIZE);
82     r = txn->prepare(txn, gid, 0);   assert(r == 0);
83     r = txn->commit(txn, 0); assert(r == 0);
84 
85     r = env->close(env, 0); assert(r == 0);
86 }
87 
88 int
test_main(int UU (argc),char UU (* const argv[]))89 test_main(int UU(argc), char UU(*const argv[])) {
90     test_txn_close_before_prepare_commit();
91     return 0;
92 }
93