1
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
5
COPYING CONDITIONS NOTICE:
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of version 2 of the GNU General Public License as
9
published by the Free Software Foundation, and provided that the
10
following conditions are met:
12
* Redistributions of source code must retain this COPYING
13
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
14
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
15
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
18
* Redistributions in binary form must reproduce this COPYING
19
CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
20
DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
21
PATENT MARKING NOTICE (below), and the PATENT RIGHTS
22
GRANT (below) in the documentation and/or other materials
23
provided with the distribution.
25
You should have received a copy of the GNU General Public License
26
along with this program; if not, write to the Free Software
27
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
32
TokuDB, Tokutek Fractal Tree Indexing Library.
33
Copyright (C) 2007-2013 Tokutek, Inc.
37
This program is distributed in the hope that it will be useful, but
38
WITHOUT ANY WARRANTY; without even the implied warranty of
39
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40
General Public License for more details.
42
UNIVERSITY PATENT NOTICE:
44
The technology is licensed by the Massachusetts Institute of
45
Technology, Rutgers State University of New Jersey, and the Research
46
Foundation of State University of New York at Stony Brook under
47
United States of America Serial No. 11/760379 and to the patents
48
and/or patent applications resulting from it.
50
PATENT MARKING NOTICE:
52
This software is covered by US Patent No. 8,185,551.
53
This software is covered by US Patent No. 8,489,638.
57
"THIS IMPLEMENTATION" means the copyrightable works distributed by
58
Tokutek as part of the Fractal Tree project.
60
"PATENT CLAIMS" means the claims of patents that are owned or
61
licensable by Tokutek, both currently or in the future; and that in
62
the absence of this license would be infringed by THIS
63
IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
65
"PATENT CHALLENGE" shall mean a challenge to the validity,
66
patentability, enforceability and/or non-infringement of any of the
67
PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
69
Tokutek hereby grants to you, for the term and geographical scope of
70
the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
71
irrevocable (except as stated in this section) patent license to
72
make, have made, use, offer to sell, sell, import, transfer, and
73
otherwise run, modify, and propagate the contents of THIS
74
IMPLEMENTATION, where such license applies only to the PATENT
75
CLAIMS. This grant does not include claims that would be infringed
76
only as a consequence of further modifications of THIS
77
IMPLEMENTATION. If you or your agent or licensee institute or order
78
or agree to the institution of patent litigation against any entity
79
(including a cross-claim or counterclaim in a lawsuit) alleging that
80
THIS IMPLEMENTATION constitutes direct or contributory patent
81
infringement, or inducement of patent infringement, then any rights
82
granted to you under this License shall terminate as of the date
83
such litigation is filed. If you or your agent or exclusive
84
licensee institute or order or agree to the institution of a PATENT
85
CHALLENGE, then Tokutek may terminate any rights granted to you
89
#ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
90
#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
91
// verify that blocking lock waits eventually time out if the lock owner never releases the lock.
97
// B tries to write lock 0, blocks
98
// B's write lock times out, B aborts its txn
99
// A wakes up and commits its txn
102
#include "toku_pthread.h"
110
static void test_seq_init(struct test_seq *seq) {
112
toku_mutex_init(&seq->lock, NULL);
113
toku_cond_init(&seq->cv, NULL);
116
static void test_seq_destroy(struct test_seq *seq) {
117
toku_mutex_destroy(&seq->lock);
118
toku_cond_destroy(&seq->cv);
121
static void test_seq_sleep(struct test_seq *seq, int new_state) {
122
toku_mutex_lock(&seq->lock);
123
while (seq->state != new_state) {
124
toku_cond_wait(&seq->cv, &seq->lock);
126
toku_mutex_unlock(&seq->lock);
129
static void test_seq_next_state(struct test_seq *seq) {
130
toku_mutex_lock(&seq->lock);
132
toku_cond_broadcast(&seq->cv);
133
toku_mutex_unlock(&seq->lock);
136
static void t_a(DB_ENV *db_env, DB *db, struct test_seq *seq) {
138
test_seq_sleep(seq, 0);
140
DB_TXN *txn_a = NULL;
141
r = db_env->txn_begin(db_env, NULL, &txn_a, 0); assert(r == 0);
142
DBT key = { .data = &k, .size = sizeof k };
143
DBT val = { .data = &k, .size = sizeof k };
144
r = db->put(db, txn_a, &key, &val, 0); assert(r == 0);
145
test_seq_next_state(seq);
147
r = txn_a->commit(txn_a, 0); assert(r == 0);
150
static void t_b(DB_ENV *db_env, DB *db, struct test_seq *seq) {
152
test_seq_sleep(seq, 1);
154
DB_TXN *txn_b = NULL;
155
r = db_env->txn_begin(db_env, NULL, &txn_b, 0); assert(r == 0);
156
DBT key = { .data = &k, .size = sizeof k };
157
DBT val = { .data = &k, .size = sizeof k };
158
r = db->put(db, txn_b, &key, &val, 0);
160
assert(r == DB_LOCK_DEADLOCK);
162
assert(r == DB_LOCK_NOTGRANTED);
164
r = txn_b->abort(txn_b); assert(r == 0);
170
struct test_seq *seq;
173
static void *t_a_thread(void *arg) {
174
struct t_a_args *a = (struct t_a_args *) arg;
175
t_a(a->env, a->db, a->seq);
179
int test_main(int argc, char * const argv[]) {
180
uint64_t cachesize = 0;
181
uint32_t pagesize = 0;
182
const char *db_env_dir = TOKU_TEST_FILENAME;
183
const char *db_filename = "test.db";
184
int db_env_open_flags = DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG | DB_THREAD;
186
// parse_args(argc, argv);
187
for (int i = 1; i < argc; i++) {
188
if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbose") == 0) {
192
if (strcmp(argv[i], "-q") == 0 || strcmp(argv[i], "--quiet") == 0) {
202
char rm_cmd[strlen(db_env_dir) + strlen("rm -rf ") + 1];
203
snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", db_env_dir);
204
r = system(rm_cmd); assert(r == 0);
206
r = toku_os_mkdir(db_env_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); assert(r == 0);
208
DB_ENV *db_env = NULL;
209
r = db_env_create(&db_env, 0); assert(r == 0);
211
const uint64_t gig = 1 << 30;
212
r = db_env->set_cachesize(db_env, cachesize / gig, cachesize % gig, 1); assert(r == 0);
214
r = db_env->open(db_env, db_env_dir, db_env_open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert(r == 0);
217
r = db_env->get_timeout(db_env, &t, DB_SET_LOCK_TIMEOUT); assert(r == 0);
218
if (verbose) printf("lock %d\n", t);
219
r = db_env->get_timeout(db_env, &t, DB_SET_TXN_TIMEOUT); assert(r == 0);
220
if (verbose) printf("txn %d\n", t);
222
r = db_env->set_timeout(db_env, 5000000, DB_SET_LOCK_TIMEOUT); assert(r == 0);
223
r = db_env->set_timeout(db_env, 5000000, DB_SET_TXN_TIMEOUT); assert(r == 0);
225
r = db_env->get_timeout(db_env, &t, DB_SET_LOCK_TIMEOUT); assert(r == 0);
226
if (verbose) printf("lock %d\n", t);
227
r = db_env->get_timeout(db_env, &t, DB_SET_TXN_TIMEOUT); assert(r == 0);
228
if (verbose) printf("txn %d\n", t);
230
r = db_env->set_lk_detect(db_env, DB_LOCK_EXPIRE); assert(r == 0);
233
uint64_t lock_timeout_msec;
234
r = db_env->get_lock_timeout(db_env, &lock_timeout_msec); assert(r == 0);
235
if (verbose) printf("lock timeout: %" PRIu64 "\n", lock_timeout_msec);
236
r = db_env->set_lock_timeout(db_env, 5000); assert(r == 0);
237
r = db_env->get_lock_timeout(db_env, &lock_timeout_msec); assert(r == 0);
238
if (verbose) printf("lock timeout: %" PRIu64 "\n", lock_timeout_msec);
243
r = db_create(&db, db_env, 0); assert(r == 0);
245
r = db->set_pagesize(db, pagesize); assert(r == 0);
247
r = db->open(db, NULL, db_filename, NULL, DB_BTREE, DB_CREATE|DB_AUTO_COMMIT|DB_THREAD, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert(r == 0);
250
struct test_seq seq; ZERO_STRUCT(seq); test_seq_init(&seq);
251
toku_pthread_t t_a_id;
252
struct t_a_args t_a_args = { db_env, db, &seq };
253
r = toku_pthread_create(&t_a_id, NULL, t_a_thread, &t_a_args); assert(r == 0);
254
t_b(db_env, db, &seq);
256
r = toku_pthread_join(t_a_id, &ret); assert(r == 0);
257
test_seq_destroy(&seq);
260
r = db->close(db, 0); assert(r == 0); db = NULL;
261
r = db_env->close(db_env, 0); assert(r == 0); db_env = NULL;