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 // verify that prelocking read ranges on multiple transactions do not conflict
42 
prelock_range(DBC * cursor,int left,int right)43 static int prelock_range(DBC *cursor, int left, int right) {
44     DBT key_left; dbt_init(&key_left, &left, sizeof left);
45     DBT key_right; dbt_init(&key_right, &right, sizeof right);
46     int r = cursor->c_set_bounds(cursor, &key_left, &key_right, true, 0);
47     return r;
48 }
49 
test_read_read(DB_ENV * env,DB * db,uint32_t iso_flags,int expect_r)50 static void test_read_read(DB_ENV *env, DB *db, uint32_t iso_flags, int expect_r) {
51     int r;
52 
53     DB_TXN *txn_a = NULL;
54     r = env->txn_begin(env, NULL, &txn_a, iso_flags); assert_zero(r);
55     DB_TXN *txn_b = NULL;
56     r = env->txn_begin(env, NULL, &txn_b, iso_flags); assert_zero(r);
57 
58     DBC *cursor_a = NULL;
59     r = db->cursor(db, txn_a, &cursor_a, 0); assert_zero(r);
60     DBC *cursor_b = NULL;
61     r = db->cursor(db, txn_b, &cursor_b, 0); assert_zero(r);
62 
63     r = prelock_range(cursor_a, htonl(10), htonl(100)); assert_zero(r);
64     r = prelock_range(cursor_b, htonl(50), htonl(200)); assert(r == expect_r);
65 
66     r = cursor_a->c_close(cursor_a); assert_zero(r);
67     r = cursor_b->c_close(cursor_b); assert_zero(r);
68 
69     r = txn_a->commit(txn_a, 0); assert_zero(r);
70     r = txn_b->commit(txn_b, 0); assert_zero(r);
71 }
72 
test_main(int argc,char * const argv[])73 int test_main(int argc, char * const argv[]) {
74     int r;
75 
76     const char *env_dir = TOKU_TEST_FILENAME;
77     const char *db_filename = "prelocktest";
78 
79     parse_args(argc, argv);
80 
81     char rm_cmd[strlen(env_dir) + strlen("rm -rf ") + 1];
82     snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", env_dir);
83     r = system(rm_cmd); assert_zero(r);
84 
85     r = toku_os_mkdir(env_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); assert_zero(r);
86 
87     DB_ENV *env = NULL;
88     r = db_env_create(&env, 0); assert_zero(r);
89     int env_open_flags = DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG;
90     r = env->open(env, env_dir, env_open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert_zero(r);
91 
92     // create the db
93     DB *db = NULL;
94     r = db_create(&db, env, 0); assert_zero(r);
95     DB_TXN *create_txn = NULL;
96     r = env->txn_begin(env, NULL, &create_txn, 0); assert_zero(r);
97     r = db->open(db, create_txn, db_filename, NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert_zero(r);
98     r = create_txn->commit(create_txn, 0); assert_zero(r);
99 
100     test_read_read(env, db, DB_READ_COMMITTED, 0);
101     test_read_read(env, db, DB_READ_UNCOMMITTED, 0);
102 #ifdef BLOCKING_ROW_LOCKS_READS_NOT_SHARED
103     test_read_read(env, db, DB_SERIALIZABLE, DB_LOCK_NOTGRANTED);
104 #else
105     test_read_read(env, db, DB_SERIALIZABLE, 0);
106 #endif
107 
108     r = db->close(db, 0); assert_zero(r);
109 
110     r = env->close(env, 0); assert_zero(r);
111     return 0;
112 }
113