~ubuntu-branches/ubuntu/trusty/mariadb-5.5/trusty-proposed

« back to all changes in this revision

Viewing changes to storage/tokudb/ft-index/src/tests/test-prepare3.cc

  • Committer: Package Import Robot
  • Author(s): James Page, Otto Kekäläinen
  • Date: 2014-02-17 16:51:52 UTC
  • mfrom: (2.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20140217165152-k315d3175g865kkx
Tags: 5.5.35-1
[ Otto Kekäläinen ]
* New upstream release, fixing the following security issues:
  - Buffer overflow in client/mysql.cc (Closes: #737597).
    - CVE-2014-0001
  - http://www.oracle.com/technetwork/topics/security/cpujan2014-1972949.html
    - CVE-2013-5891
    - CVE-2013-5908
    - CVE-2014-0386
    - CVE-2014-0393
    - CVE-2014-0401
    - CVE-2014-0402
    - CVE-2014-0412
    - CVE-2014-0420
    - CVE-2014-0437
* Upstream https://mariadb.atlassian.net/browse/MDEV-4902
  fixes compatibility with Bison 3.0 (Closes: #733002)
* Updated Russian debconf translation (Closes: #734426)
* Updated Japanese debconf translation (Closes: #735284)
* Updated French debconf translation (Closes: #736480)
* Renamed SONAME properly (Closes: #732967)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
COPYING CONDITIONS NOTICE:
 
6
 
 
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:
 
11
 
 
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
 
16
        GRANT (below).
 
17
 
 
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.
 
24
 
 
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
 
28
  02110-1301, USA.
 
29
 
 
30
COPYRIGHT NOTICE:
 
31
 
 
32
  TokuDB, Tokutek Fractal Tree Indexing Library.
 
33
  Copyright (C) 2007-2013 Tokutek, Inc.
 
34
 
 
35
DISCLAIMER:
 
36
 
 
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.
 
41
 
 
42
UNIVERSITY PATENT NOTICE:
 
43
 
 
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.
 
49
 
 
50
PATENT MARKING NOTICE:
 
51
 
 
52
  This software is covered by US Patent No. 8,185,551.
 
53
  This software is covered by US Patent No. 8,489,638.
 
54
 
 
55
PATENT RIGHTS GRANT:
 
56
 
 
57
  "THIS IMPLEMENTATION" means the copyrightable works distributed by
 
58
  Tokutek as part of the Fractal Tree project.
 
59
 
 
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.
 
64
 
 
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.
 
68
 
 
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
 
86
  under this License.
 
87
*/
 
88
 
 
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
#include "test.h"
 
92
#include <sys/wait.h>
 
93
 
 
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.
 
95
 
 
96
static void clean_env (const char *envdir) {
 
97
    const int len = strlen(envdir)+100;
 
98
    char cmd[len];
 
99
    snprintf(cmd, len, "rm -rf %s", envdir);
 
100
    int r = system(cmd);
 
101
    CKERR(r);
 
102
    CKERR(toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO));
 
103
}
 
104
 
 
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);
 
108
#ifdef TOKUDB
 
109
    { int chk_r = (*envp)->set_redzone(*envp, 0); CKERR(chk_r); }
 
110
#endif
 
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); }
 
112
}
 
113
 
 
114
#define NTXNS 6
 
115
 
 
116
static void setup_env_and_prepare (DB_ENV **envp, const char *envdir) {
 
117
    DB *db;
 
118
    clean_env(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));
 
122
 
 
123
    {
 
124
        DB_TXN *txn;
 
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++) {
 
128
                #define DSIZE 200
 
129
                char data[DSIZE];
 
130
                memset(data, ' ', DSIZE);
 
131
                data[0]='a'+tnum;
 
132
                data[1]='a'+k;
 
133
                data[DSIZE-1]=0;
 
134
                DBT key;
 
135
                dbt_init(&key, data, DSIZE);
 
136
                CKERR(db->put(db, txn, &key, &key, 0));
 
137
            }
 
138
        }
 
139
        CKERR(txn->commit(txn, 0));
 
140
    }
 
141
 
 
142
    for (int tnum=0; tnum<NTXNS; tnum++) {
 
143
        DB_TXN *txn;
 
144
        CKERR((*envp)->txn_begin(*envp, 0, &txn, 0));
 
145
        char data[3]={(char)('a'+tnum),'_',0};
 
146
        DBT key;
 
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);
 
151
        gid[0]='a'+tnum;
 
152
        CKERR(txn->prepare(txn, gid));
 
153
        // Drop txn on the ground, since we will commit or abort it after recovery
 
154
        if (tnum==0) {
 
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));
 
160
        } else {
 
161
            //printf("prepare %d\n", tnum);
 
162
        }
 
163
    }
 
164
    { int chk_r = db->close(db, 0); CKERR(chk_r); }
 
165
}
 
166
 
 
167
enum prepared_state {
 
168
    COMMITTED,
 
169
    ABORTED,
 
170
    MAYBE_COMMITTED,
 
171
    MAYBE_ABORTED,
 
172
    PREPARED};
 
173
    
 
174
 
 
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++) {
 
179
        switch (ps[j]) {
 
180
        case COMMITTED:
 
181
        case ABORTED:
 
182
            goto next;
 
183
        case PREPARED:
 
184
            count_prepared++;
 
185
        case MAYBE_COMMITTED:
 
186
        case MAYBE_ABORTED:
 
187
            count_maybe_prepared++;
 
188
            goto next;
 
189
        }
 
190
        assert(0);
 
191
    next:;
 
192
    }
 
193
            
 
194
    assert(count>=count_prepared && count<=count_maybe_prepared);
 
195
 
 
196
    bool found[NTXNS];
 
197
    for (int j=0; j<NTXNS; j++) {
 
198
        found[j] = (ps[j]!=PREPARED);
 
199
    }
 
200
 
 
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);
 
205
        switch (ps[num]) {
 
206
        case PREPARED:
 
207
            assert(!found[num]);
 
208
            found[num]=true;
 
209
            break;
 
210
        default:;
 
211
        }
 
212
        for (int i=1; i<DB_GID_SIZE; i++) {
 
213
                assert(l[j].gid[i]==0);
 
214
        }
 
215
    }
 
216
}
 
217
 
 
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);
 
221
    assert(*count>=0);
 
222
}
 
223
 
 
224
static void check_prepared_txns (DB_ENV *env, enum prepared_state ps[NTXNS]) {
 
225
    DB_PREPLIST l[NTXNS];
 
226
    long count=-1;
 
227
    get_prepared(env, &count, l);
 
228
    check_prepared_list(ps, count, l);
 
229
}
 
230
 
 
231
static void check_state_after_full_recovery (DB_ENV *env) {
 
232
    DB *db;
 
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));
 
235
 
 
236
    for (int tnum=0; tnum<NTXNS; tnum++) {
 
237
        DB_TXN *txn;
 
238
        CKERR(env->txn_begin(env, 0, &txn, 0));
 
239
        char data[3]={(char)('a'+tnum),'_',0};
 
240
        DBT key;
 
241
        dbt_init(&key, data, 3);
 
242
        DBT dbt_data;
 
243
        dbt_init(&dbt_data, NULL, 0);
 
244
        int r = db->get(db, txn, &key, &dbt_data, 0);
 
245
        if (tnum%2==0) {
 
246
            assert(r==0);
 
247
            assert(dbt_data.size==3 && memcmp(dbt_data.data, data, 3)==0);
 
248
        } else {
 
249
            assert(r==DB_NOTFOUND);
 
250
        }
 
251
        CKERR(txn->commit(txn, 0));
 
252
    }
 
253
    CKERR(db->close(db, 0));
 
254
}
 
255
 
 
256
static void waitfor (pid_t pid) {
 
257
    int status;
 
258
    pid_t pid2 = wait(&status);
 
259
    assert(pid2==pid);
 
260
    assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
 
261
}
 
262
 
 
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));
 
267
            return;
 
268
        }
 
269
    }
 
270
    assert(0);
 
271
}
 
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));
 
276
            return;
 
277
        }
 
278
    }
 
279
    assert(0);
 
280
}
 
281
 
 
282
static void test (void) {
 
283
    pid_t pid;
 
284
 
 
285
    if (0==(pid=fork())) {
 
286
        DB_ENV *env;
 
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);
 
290
        exit(0);
 
291
    }
 
292
    waitfor(pid);
 
293
    // Now run recovery and crash on purpose.
 
294
    if (0==(pid=fork())) {
 
295
        DB_ENV *env;
 
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);
 
299
        exit(0);
 
300
    }
 
301
    waitfor(pid);
 
302
 
 
303
    // Now see if recovery works the second time.
 
304
    if (0==(pid=fork())) {
 
305
        DB_ENV *env;
 
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);
 
309
        exit(0);
 
310
    }
 
311
    waitfor(pid);
 
312
 
 
313
    // Now see if recovery works the third time.
 
314
    if (0==(pid=fork())) {
 
315
        DB_ENV *env;
 
316
        setup_env(&env, TOKU_TEST_FILENAME);
 
317
        enum prepared_state prepared[NTXNS]={COMMITTED,ABORTED,PREPARED,PREPARED,PREPARED,PREPARED};
 
318
        DB_PREPLIST l[NTXNS];
 
319
        long count=-1;
 
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.
 
324
        exit(0);
 
325
    }
 
326
    waitfor(pid);
 
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())) {
 
329
        DB_ENV *env;
 
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];
 
333
        long count=-1;
 
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);
 
337
        exit(0);
 
338
    }
 
339
    waitfor(pid);
 
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())) {
 
343
        DB_ENV *env;
 
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...
 
346
 
 
347
        long actual_count=0;
 
348
 
 
349
        for (int recover_num=0; 1; recover_num++) {
 
350
            long count=-1;
 
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);
 
354
            if (count==0) break;
 
355
            actual_count++;
 
356
            if ((l[0].gid[0]-'a')%2==0) {
 
357
                CKERR(l[0].txn->commit(l[0].txn, 0));
 
358
            } else {
 
359
                CKERR(l[0].txn->abort(l[0].txn));
 
360
            }
 
361
            toku_free(l);
 
362
        }
 
363
        //printf("actual_count=%ld\n", actual_count);
 
364
 
 
365
        // Now let's see what the state is.
 
366
        check_state_after_full_recovery(env);
 
367
 
 
368
        CKERR(env->close(env, 0));
 
369
        exit(0);
 
370
    }
 
371
    waitfor(pid);
 
372
    // Now we should end up with nothing in the recovery list.
 
373
    {
 
374
        DB_ENV *env;
 
375
        setup_env(&env, TOKU_TEST_FILENAME);
 
376
        long count=-1;
 
377
        DB_PREPLIST l[1];
 
378
        CKERR(env->txn_recover(env, l, 1, &count, DB_FIRST));
 
379
        assert(count==0);
 
380
        check_state_after_full_recovery(env);
 
381
        CKERR(env->close(env, 0));
 
382
    }   
 
383
        
 
384
 
 
385
}
 
386
 
 
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).
 
390
    test();
 
391
    
 
392
    return 0;
 
393
}