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."
94
// Verify that if we prepare a transaction, then commit a bunch more transactions so that the logs may have been rotated, then the transaction can commit or abort properly on recovery.
96
static void clean_env (const char *envdir) {
97
const int len = strlen(envdir)+100;
99
snprintf(cmd, len, "rm -rf %s", envdir);
102
CKERR(toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO));
105
static void setup_env (DB_ENV **envp, const char *envdir) {
106
{ int chk_r = db_env_create(envp, 0); CKERR(chk_r); }
107
(*envp)->set_errfile(*envp, stderr);
109
{ int chk_r = (*envp)->set_redzone(*envp, 0); CKERR(chk_r); }
111
{ int chk_r = (*envp)->open(*envp, envdir, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE|DB_RECOVER, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
116
static void setup_env_and_prepare (DB_ENV **envp, const char *envdir) {
119
setup_env(envp, envdir);
120
CKERR(db_create(&db, *envp, 0));
121
CKERR(db->open(db, NULL, "foo.db", 0, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, S_IRWXU+S_IRWXG+S_IRWXO));
125
CKERR((*envp)->txn_begin(*envp, 0, &txn, 0));
126
for (int tnum=0; tnum<NTXNS; tnum++) {
127
for (int k=0; k<26; k++) {
130
memset(data, ' ', DSIZE);
135
dbt_init(&key, data, DSIZE);
136
CKERR(db->put(db, txn, &key, &key, 0));
139
CKERR(txn->commit(txn, 0));
142
for (int tnum=0; tnum<NTXNS; tnum++) {
144
CKERR((*envp)->txn_begin(*envp, 0, &txn, 0));
145
char data[3]={(char)('a'+tnum),'_',0};
147
dbt_init(&key, data, 3);
148
CKERR(db->put(db, txn, &key, &key, 0));
149
uint8_t gid[DB_GID_SIZE];
150
memset(gid, 0, DB_GID_SIZE);
152
CKERR(txn->prepare(txn, gid));
153
// Drop txn on the ground, since we will commit or abort it after recovery
155
//printf("commit %d\n", tnum);
156
CKERR(txn->commit(txn, 0));
157
} else if (tnum==1) {
158
//printf("abort %d\n", tnum);
159
CKERR(txn->abort(txn));
161
//printf("prepare %d\n", tnum);
164
{ int chk_r = db->close(db, 0); CKERR(chk_r); }
167
enum prepared_state {
175
static void check_prepared_list (enum prepared_state ps[NTXNS], long count, DB_PREPLIST *l) {
176
int count_prepared=0;
177
int count_maybe_prepared=0;
178
for (int j=0; j<NTXNS; j++) {
185
case MAYBE_COMMITTED:
187
count_maybe_prepared++;
194
assert(count>=count_prepared && count<=count_maybe_prepared);
197
for (int j=0; j<NTXNS; j++) {
198
found[j] = (ps[j]!=PREPARED);
201
// now found[j] is false on those transactions that I hope to find in the prepared list.
202
for (int j=0; j<count; j++) {
203
int num = l[j].gid[0]-'a';
204
assert(num>=0 && num<NTXNS);
212
for (int i=1; i<DB_GID_SIZE; i++) {
213
assert(l[j].gid[i]==0);
218
static void get_prepared (DB_ENV *env, long *count, DB_PREPLIST *l) {
219
CKERR(env->txn_recover(env, l, NTXNS, count, DB_FIRST));
220
//printf("%s:%d count=%ld\n", __FILE__, __LINE__, *count);
224
static void check_prepared_txns (DB_ENV *env, enum prepared_state ps[NTXNS]) {
225
DB_PREPLIST l[NTXNS];
227
get_prepared(env, &count, l);
228
check_prepared_list(ps, count, l);
231
static void check_state_after_full_recovery (DB_ENV *env) {
233
CKERR(db_create(&db, env, 0));
234
CKERR(db->open(db, NULL, "foo.db", 0, DB_BTREE, DB_CREATE | DB_AUTO_COMMIT, S_IRWXU+S_IRWXG+S_IRWXO));
236
for (int tnum=0; tnum<NTXNS; tnum++) {
238
CKERR(env->txn_begin(env, 0, &txn, 0));
239
char data[3]={(char)('a'+tnum),'_',0};
241
dbt_init(&key, data, 3);
243
dbt_init(&dbt_data, NULL, 0);
244
int r = db->get(db, txn, &key, &dbt_data, 0);
247
assert(dbt_data.size==3 && memcmp(dbt_data.data, data, 3)==0);
249
assert(r==DB_NOTFOUND);
251
CKERR(txn->commit(txn, 0));
253
CKERR(db->close(db, 0));
256
static void waitfor (pid_t pid) {
258
pid_t pid2 = wait(&status);
260
assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
263
static void abort_number(int num, int count, DB_PREPLIST *l) {
264
for (int j=0; j<count; j++) {
265
if (l[j].gid[0]=='a'+num) {
266
CKERR(l[j].txn->abort(l[j].txn));
272
static void commit_number(int num, int count, DB_PREPLIST *l) {
273
for (int j=0; j<count; j++) {
274
if (l[j].gid[0]=='a'+num) {
275
CKERR(l[j].txn->commit(l[j].txn, 0));
282
static void test (void) {
285
if (0==(pid=fork())) {
287
setup_env_and_prepare(&env, TOKU_TEST_FILENAME);
288
enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,PREPARED,PREPARED,PREPARED,PREPARED};
289
check_prepared_txns(env, prepared);
293
// Now run recovery and crash on purpose.
294
if (0==(pid=fork())) {
296
setup_env(&env, TOKU_TEST_FILENAME);
297
enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,PREPARED,PREPARED,PREPARED,PREPARED};
298
check_prepared_txns(env, prepared);
303
// Now see if recovery works the second time.
304
if (0==(pid=fork())) {
306
setup_env(&env, TOKU_TEST_FILENAME);
307
enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,PREPARED,PREPARED,PREPARED,PREPARED};
308
check_prepared_txns(env, prepared);
313
// Now see if recovery works the third time.
314
if (0==(pid=fork())) {
316
setup_env(&env, TOKU_TEST_FILENAME);
317
enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,PREPARED,PREPARED,PREPARED,PREPARED};
318
DB_PREPLIST l[NTXNS];
320
get_prepared(env, &count, l);
321
check_prepared_list(prepared, count, l);
322
abort_number(3, count, l);
323
commit_number(2, count, l); // do the commit second so it will make it to disk.
327
// Now see if recovery works a third time, with number 2 and 3 no longer in the prepared state.
328
if (0==(pid=fork())) {
330
setup_env(&env, TOKU_TEST_FILENAME);
331
enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,MAYBE_COMMITTED,MAYBE_ABORTED,PREPARED,PREPARED};
332
DB_PREPLIST l[NTXNS];
334
//printf("%s:%d count=%ld\n", __FILE__, __LINE__, count); // it's a little bit funky that the committed transactions in BDB (from commit_number(2,...) above) don't stay committed. But whatever...
335
get_prepared(env, &count, l);
336
check_prepared_list(prepared, count, l);
340
// Now see if recovery works a fourth time, with number 2 and 3 no longer in the prepared state.
341
// This time we'll do get_prepared with a short count.
342
if (0==(pid=fork())) {
344
setup_env(&env, TOKU_TEST_FILENAME);
345
//printf("%s:%d count=%ld\n", __FILE__, __LINE__, count); // it's a little bit funky that the committed transactions in BDB (from commit_number(2,...) above) don't stay committed. But whatever...
349
for (int recover_num=0; 1; recover_num++) {
351
DB_PREPLIST *MALLOC_N(1, l); // use malloc so that valgrind might notice a problem
352
CKERR(env->txn_recover(env, l, 1, &count, recover_num==0 ? DB_FIRST : DB_NEXT));
353
//printf("recover_num %d count=%ld\n", recover_num,count);
356
if ((l[0].gid[0]-'a')%2==0) {
357
CKERR(l[0].txn->commit(l[0].txn, 0));
359
CKERR(l[0].txn->abort(l[0].txn));
363
//printf("actual_count=%ld\n", actual_count);
365
// Now let's see what the state is.
366
check_state_after_full_recovery(env);
368
CKERR(env->close(env, 0));
372
// Now we should end up with nothing in the recovery list.
375
setup_env(&env, TOKU_TEST_FILENAME);
378
CKERR(env->txn_recover(env, l, 1, &count, DB_FIRST));
380
check_state_after_full_recovery(env);
381
CKERR(env->close(env, 0));
387
int test_main (int argc, char *const argv[]) {
388
default_parse_args(argc, argv);
389
// first test: open an environment, a db, a txn, and do a prepare. Then do txn_prepare (without even closing the environment).