summaryrefslogtreecommitdiff
path: root/storage/tokudb/PerconaFT/src/tests/test.h
blob: c5214961afd5736c8802847b15d38ecee4c35a9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of PerconaFT.


Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License, version 2,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.

----------------------------------------

    PerconaFT is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License, version 3,
    as published by the Free Software Foundation.

    PerconaFT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
======= */

#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."

#pragma once

#include <toku_portability.h>

#include <string.h>
#include <stdlib.h>
#include <toku_stdint.h>
#include <stdio.h>
#include <db.h>
#include <limits.h>
#include <errno.h>
#include <toku_htonl.h>
#include <portability/toku_path.h>
#include <portability/toku_crash.h>
#include "toku_assert.h"
#include <signal.h>
#include <time.h>

#include "ydb.h"
//TDB uses DB_NOTFOUND for c_del and DB_CURRENT errors.
#ifdef DB_KEYEMPTY
#error
#endif
#define DB_KEYEMPTY DB_NOTFOUND

// Certain tests fail when row locks taken for read are not shared.
// This switch prevents them from failing so long as read locks are not shared.
#define BLOCKING_ROW_LOCKS_READS_NOT_SHARED

int verbose=0;

#define UU(x) x __attribute__((__unused__))

#define CKERR(r) ({ int __r = r; if (__r!=0) fprintf(stderr, "%s:%d error %d %s\n", __FILE__, __LINE__, __r, db_strerror(r)); assert(__r==0); })
#define CKERR2(r,r2) do { if (r!=r2) fprintf(stderr, "%s:%d error %d %s, expected %d\n", __FILE__, __LINE__, r, db_strerror(r), r2); assert(r==r2); } while (0)
#define CKERR2s(r,r2,r3) do { if (r!=r2 && r!=r3) fprintf(stderr, "%s:%d error %d %s, expected %d or %d\n", __FILE__, __LINE__, r, db_strerror(r), r2,r3); assert(r==r2||r==r3); } while (0)

/*
 * Helpers for defining pseudo-hygienic macros using a (gensym)-like
 * technique.
 */
#define _CONCAT(x, y) x ## y
#define CONCAT(x, y) _CONCAT(x, y)
#define GS(symbol) CONCAT(CONCAT(__gensym_, __LINE__), CONCAT(_, symbol))

#define DEBUG_LINE do { \
    fprintf(stderr, "%s() %s:%d\n", __FUNCTION__, __FILE__, __LINE__); \
    fflush(stderr); \
} while (0)

static __attribute__((__unused__)) void
parse_args (int argc, char * const argv[]) {
    const char *argv0=argv[0];
    while (argc>1) {
	int resultcode=0;
	if (strcmp(argv[1], "-v")==0) {
	    verbose++;
	} else if (strcmp(argv[1],"-q")==0) {
	    verbose--;
	    if (verbose<0) verbose=0;
	} else if (strcmp(argv[1], "-h")==0) {
	do_usage:
	    fprintf(stderr, "Usage:\n%s [-v|-q] [-h]\n", argv0);
	    exit(resultcode);
	} else {
	    resultcode=1;
	    goto do_usage;
	}
	argc--;
	argv++;
    }
}

static __attribute__((__unused__)) void 
print_engine_status(DB_ENV * UU(env)) {
    if (verbose) {  // verbose declared statically in this file
        uint64_t nrows;
        env->get_engine_status_num_rows(env, &nrows);
        int bufsiz = nrows * 128;   // assume 128 characters per row
        char buff[bufsiz];  
        env->get_engine_status_text(env, buff, bufsiz);
        printf("Engine status:\n");
        printf("%s", buff);
    }
}

static __attribute__((__unused__)) uint64_t
get_engine_status_val(DB_ENV * UU(env), const char * keyname) {
    uint64_t rval = 0;
    uint64_t nrows;
    uint64_t max_rows;
    env->get_engine_status_num_rows(env, &max_rows);
    TOKU_ENGINE_STATUS_ROW_S mystat[max_rows];
    fs_redzone_state redzone_state;
    uint64_t panic;
    uint32_t panic_string_len = 1024;
    char panic_string[panic_string_len];
    int r = env->get_engine_status (env, mystat, max_rows, &nrows, &redzone_state, &panic, panic_string, panic_string_len, TOKU_ENGINE_STATUS);
    CKERR(r);
    int found = 0;
    for (uint64_t i = 0; i < nrows && !found; i++) {
        if (strcmp(keyname, mystat[i].keyname) == 0) {
            found++;
            rval = mystat[i].value.num;
        }
    }
    CKERR2(found, 1);
    return rval;
}

static __attribute__((__unused__)) DBT *
dbt_init(DBT *dbt, const void *data, uint32_t size) {
    memset(dbt, 0, sizeof *dbt);
    dbt->data = (void*)data;
    dbt->size = size;
    return dbt;
}

static __attribute__((__unused__)) DBT *
dbt_init_malloc (DBT *dbt) {
    memset(dbt, 0, sizeof *dbt);
    dbt->flags = DB_DBT_MALLOC;
    return dbt;
}

static __attribute__((__unused__)) DBT *
dbt_init_realloc (DBT *dbt) {
    memset(dbt, 0, sizeof *dbt);
    dbt->flags = DB_DBT_REALLOC;
    return dbt;
}

// Simple LCG random number generator.  Not high quality, but good enough.
static uint32_t rstate=1;
static inline void mysrandom (int s) {
    rstate=s;
}
static inline uint32_t myrandom (void) {
    rstate = (279470275ull*(uint64_t)rstate)%4294967291ull;
    return rstate;
}

static __attribute__((__unused__)) int
int64_dbt_cmp (DB *db UU(), const DBT *a, const DBT *b) {
//    assert(db && a && b);
    assert(a);
    assert(b);
//    assert(db);

    assert(a->size == sizeof(int64_t));
    assert(b->size == sizeof(int64_t));

    int64_t x = *(int64_t *) a->data;
    int64_t y = *(int64_t *) b->data;

    if (x<y) return -1;
    if (x>y) return 1;
    return 0;
}

static __attribute__((__unused__)) int
int_dbt_cmp (DB *db, const DBT *a, const DBT *b) {
  assert(db && a && b);
  assert(a->size == sizeof(int));
  assert(b->size == sizeof(int));

  int x = *(int *) a->data;
  int y = *(int *) b->data;

    if (x<y) return -1;
    if (x>y) return 1;
    return 0;
}

static __attribute__((__unused__)) int
uint_dbt_cmp (DB *db, const DBT *a, const DBT *b) {
  assert(db && a && b);
  assert(a->size == sizeof(unsigned int));
  assert(b->size == sizeof(unsigned int));

  unsigned int x = *(unsigned int *) a->data;
  unsigned int y = *(unsigned int *) b->data;

    if (x<y) return -1;
    if (x>y) return 1;
    return 0;
}

#define SET_TRACE_FILE(x) toku_set_trace_file(x)
#define CLOSE_TRACE_FILE(x) toku_close_trace_file()

#include <memory.h>

static uint64_t __attribute__((__unused__))
random64(void) {
    const unsigned int seed = 0xFEEDFACE;
    static int seeded = 0;
    if (!seeded) {
        seeded = 1;
        srandom(seed);
    }
    //random() generates 31 bits of randomness (low order)
    uint64_t low     = random();
    uint64_t high    = random();
    uint64_t twobits = random();
    uint64_t ret     = low | (high<<31) | (twobits<<62); 
    return ret;
}

static __attribute__((__unused__))
double get_tdiff(void) {
    static struct timeval prev={0,0};
    if (prev.tv_sec==0) {
	gettimeofday(&prev, 0);
	return 0.0;
    } else {
	struct timeval now;
	gettimeofday(&now, 0);
	double diff = now.tv_sec - prev.tv_sec + 1e-6*(now.tv_usec - prev.tv_usec);
	prev = now;
	return diff;
    }
}

static __attribute__((__unused__))
void format_time(const time_t *timer, char *buf) {
    ctime_r(timer, buf);
    size_t len = strlen(buf);
    assert(len < 26);
    char end;

    assert(len>=1);
    end = buf[len-1];
    while (end == '\n' || end == '\r') {
        buf[len-1] = '\0';
        len--;
        assert(len>=1);
        end = buf[len-1];
    }
}

static __attribute__((__unused__))
void print_time_now(void) {
    char timestr[80];
    time_t now = time(NULL);
    format_time(&now, timestr);
    printf("%s", timestr);
}

static void UU()
multiply_locks_for_n_dbs(DB_ENV *env, int num_dbs) {
    uint64_t current_max_lock_memory;
    int r = env->get_lk_max_memory(env, &current_max_lock_memory);
    CKERR(r);
    r = env->set_lk_max_memory(env, current_max_lock_memory * num_dbs);
    CKERR(r);
}

static inline void
default_parse_args (int argc, char * const argv[]) {
    const char *progname=argv[0];
    argc--; argv++;
    while (argc>0) {
	if (strcmp(argv[0],"-v")==0) {
	    ++verbose;
	} else if (strcmp(argv[0],"-q")==0) {
	    verbose=0;
	} else {
	    fprintf(stderr, "Usage:\n %s [-v] [-q]\n", progname);
	    exit(1);
	}
	argc--; argv++;
    }
}

UU()
static void copy_dbt(DBT *dest, const DBT *src) {
    assert(dest->flags & DB_DBT_REALLOC);
    dest->data = toku_xrealloc(dest->data, src->size);
    dest->size = src->size;
    memcpy(dest->data, src->data, src->size);
}

// DBT_ARRAY is a toku-specific type
UU()
static int
env_update_multiple_test_no_array(
    DB_ENV *env, 
    DB *src_db, 
    DB_TXN *txn, 
    DBT *old_src_key, DBT *old_src_data,
    DBT *new_src_key, DBT *new_src_data,
    uint32_t num_dbs, DB **db_array, uint32_t* flags_array,
    uint32_t num_keys, DBT keys[],
    uint32_t num_vals, DBT vals[]) {
    int r;
    DBT_ARRAY key_arrays[num_keys];
    DBT_ARRAY val_arrays[num_vals];
    for (uint32_t i = 0; i < num_keys; i++) {
        toku_dbt_array_init(&key_arrays[i], 1);
        key_arrays[i].dbts[0] = keys[i];
    }
    for (uint32_t i = 0; i < num_vals; i++) {
        toku_dbt_array_init(&val_arrays[i], 1);
        val_arrays[i].dbts[0] = vals[i];
    }
    r = env->update_multiple(env, src_db, txn, old_src_key, old_src_data, new_src_key, new_src_data,
                          num_dbs, db_array, flags_array,
                          num_keys, &key_arrays[0],
                          num_vals, &val_arrays[0]);
    for (uint32_t i = 0; i < num_keys; i++) {
        invariant(key_arrays[i].size == 1);
        invariant(key_arrays[i].capacity == 1);
        keys[i] = key_arrays[i].dbts[0];
        toku_dbt_array_destroy_shallow(&key_arrays[i]);
    }
    for (uint32_t i = 0; i < num_vals; i++) {
        invariant(val_arrays[i].size == 1);
        invariant(val_arrays[i].capacity == 1);
        vals[i] = val_arrays[i].dbts[0];
        toku_dbt_array_destroy_shallow(&val_arrays[i]);
    }
    return r;
}

UU()
static int env_put_multiple_test_no_array(
    DB_ENV *env, 
    DB *src_db, 
    DB_TXN *txn, 
    const DBT *src_key, 
    const DBT *src_val, 
    uint32_t num_dbs, 
    DB **db_array, 
    DBT *keys,
    DBT *vals,
    uint32_t *flags_array) 
{
    int r;
    DBT_ARRAY key_arrays[num_dbs];
    DBT_ARRAY val_arrays[num_dbs];
    for (uint32_t i = 0; i < num_dbs; i++) {
        toku_dbt_array_init(&key_arrays[i], 1);
        toku_dbt_array_init(&val_arrays[i], 1);
        key_arrays[i].dbts[0] = keys[i];
        val_arrays[i].dbts[0] = vals[i];
    }
    r = env->put_multiple(env, src_db, txn, src_key, src_val, num_dbs, db_array, &key_arrays[0], &val_arrays[0], flags_array);
    for (uint32_t i = 0; i < num_dbs; i++) {
        invariant(key_arrays[i].size == 1);
        invariant(key_arrays[i].capacity == 1);
        invariant(val_arrays[i].size == 1);
        invariant(val_arrays[i].capacity == 1);
        keys[i] = key_arrays[i].dbts[0];
        vals[i] = val_arrays[i].dbts[0];
        toku_dbt_array_destroy_shallow(&key_arrays[i]);
        toku_dbt_array_destroy_shallow(&val_arrays[i]);
    }
    return r;
}

UU()
static int env_del_multiple_test_no_array(
    DB_ENV *env, 
    DB *src_db, 
    DB_TXN *txn, 
    const DBT *src_key, 
    const DBT *src_val, 
    uint32_t num_dbs, 
    DB **db_array, 
    DBT *keys,
    uint32_t *flags_array) 
{
    int r;
    DBT_ARRAY key_arrays[num_dbs];
    for (uint32_t i = 0; i < num_dbs; i++) {
        toku_dbt_array_init(&key_arrays[i], 1);
        key_arrays[i].dbts[0] = keys[i];
    }
    r = env->del_multiple(env, src_db, txn, src_key, src_val, num_dbs, db_array, &key_arrays[0], flags_array);
    for (uint32_t i = 0; i < num_dbs; i++) {
        invariant(key_arrays[i].size == 1);
        invariant(key_arrays[i].capacity == 1);
        keys[i] = key_arrays[i].dbts[0];
        toku_dbt_array_destroy_shallow(&key_arrays[i]);
    }
    return r;
}

/* Some macros for evaluating blocks or functions within the scope of a
 * transaction. */
#define IN_TXN_COMMIT(env, parent, txn, flags, expr) ({                 \
            DB_TXN *txn;                                                \
            { int chk_r = (env)->txn_begin((env), (parent), &(txn), (flags)); CKERR(chk_r); } \
            (expr);                                                     \
            { int chk_r = (txn)->commit((txn), 0); CKERR(chk_r); }      \
        })

#define IN_TXN_ABORT(env, parent, txn, flags, expr) ({                  \
            DB_TXN *txn;                                                \
            { int chk_r = (env)->txn_begin((env), (parent), &(txn), (flags)); CKERR(chk_r); } \
            (expr);                                                     \
            { int chk_r = (txn)->abort(txn); CKERR(chk_r); }            \
        })

int test_main(int argc, char *const argv[]);
int main(int argc, char *const argv[]) {
    int r;
    toku_os_initialize_settings(1);
    r = test_main(argc, argv);
    return r;
}

#ifndef DB_GID_SIZE
#define	DB_GID_SIZE	DB_XIDDATASIZE
#endif