/* -*- 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 . ---------------------------------------- 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 . ======= */ #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." // test the loader write dbfile function #define DONT_DEPRECATE_WRITES #define DONT_DEPRECATE_MALLOC #include "test.h" #include "loader/loader-internal.h" #include "ftloader-error-injector.h" #include static int qsort_compare_ints (const void *a, const void *b) { int avalue = *(int*)a; int bvalue = *(int*)b; if (avaluebvalue) return +1; return 0; } static int compare_ints (DB *UU(desc), const DBT *akey, const DBT *bkey) { assert(akey->size==sizeof(int)); assert(bkey->size==sizeof(int)); return qsort_compare_ints(akey->data, bkey->data); } static void err_cb(DB *db UU(), int dbn UU(), int err UU(), DBT *key UU(), DBT *val UU(), void *extra UU()) { fprintf(stderr, "error in test"); abort(); } static int write_dbfile (char *tf_template, int n, char *output_name, bool expect_error, int testno) { if (verbose) printf("test start %d %d testno=%d\n", n, expect_error, testno); int result = 0; DB *dest_db = NULL; struct ft_loader_s bl; ZERO_STRUCT(bl); bl.temp_file_template = tf_template; bl.reserved_memory = 512*1024*1024; int r = ft_loader_init_file_infos(&bl.file_infos); CKERR(r); ft_loader_lock_init(&bl); ft_loader_set_fractal_workers_count_from_c(&bl); struct merge_fileset fs; init_merge_fileset(&fs); // put rows in the row set struct rowset aset; uint64_t size_est = 0; init_rowset(&aset, toku_ft_loader_get_rowset_budget_for_testing()); for (int i=0; in_rows; i++) { struct row *row = &rs->rows[i]; assert(row->klen==sizeof(int)); assert(row->vlen==sizeof(int)); assert((int)(num_found+i)==*(int*)(rs->data+row->off)); found_size_est += ft_loader_leafentry_size(row->klen, row->vlen, TXNID_NONE); } num_found += rs->n_rows; r = toku_queue_enq(q2, v, 0, NULL); assert(r==0); } assert((int)num_found == n); if (!expect_error) assert(found_size_est == size_est); r = toku_queue_eof(q2); assert(r==0); r = toku_queue_destroy(q); assert(r==0); DESCRIPTOR_S desc; toku_fill_dbt(&desc.dbt, "abcd", 4); int fd = open(output_name, O_RDWR | O_CREAT | O_BINARY, S_IRWXU|S_IRWXG|S_IRWXO); assert(fd>=0); toku_set_func_malloc_only(my_malloc); toku_set_func_realloc_only(my_realloc); ft_loader_set_os_fwrite(bad_fwrite); toku_set_func_write(bad_write); toku_set_func_pwrite(bad_pwrite); ft_loader_set_error_function(&bl.error_callback, NULL, NULL); ft_loader_set_poll_function(&bl.poll_callback, loader_poll_callback, NULL); result = toku_loader_write_ft_from_q_in_C(&bl, &desc, fd, 1000, q2, size_est, 0, 0, 0, TOKU_DEFAULT_COMPRESSION_METHOD, 16); toku_set_func_malloc_only(NULL); toku_set_func_realloc_only(NULL); ft_loader_set_os_fwrite(NULL); toku_set_func_write(NULL); toku_set_func_pwrite(NULL); ft_loader_destroy_error_callback(&bl.error_callback); ft_loader_destroy_poll_callback(&bl.poll_callback); ft_loader_lock_destroy(&bl); r = toku_queue_destroy(q2); assert(r==0); destroy_merge_fileset(&fs); ft_loader_fi_destroy(&bl.file_infos, expect_error); return result; } static int usage(const char *progname, int n) { fprintf(stderr, "Usage: %s [options] directory\n", progname); fprintf(stderr, "[-v] turn on verbose\n"); fprintf(stderr, "[-q] turn off verbose\n"); fprintf(stderr, "[-r %d] set the number of rows\n", n); fprintf(stderr, "[-s] set the small loader size factor\n"); fprintf(stderr, "[-m] inject big malloc and realloc errors\n"); fprintf(stderr, "[--malloc_limit %u] set the threshold for failing malloc and realloc\n", (unsigned) my_big_malloc_limit); fprintf(stderr, "[--realloc_errors] inject realloc errors\n"); fprintf(stderr, "[-w] inject write errors\n"); fprintf(stderr, "[-u] inject user errors\n"); return 1; } int test_main (int argc, const char *argv[]) { const char *progname=argv[0]; int n = 1; argc--; argv++; while (argc>0) { if (strcmp(argv[0],"-h")==0) { return usage(progname, n); } else if (strcmp(argv[0],"-v")==0) { verbose=1; } else if (strcmp(argv[0],"-q")==0) { verbose=0; } else if (strcmp(argv[0],"-r") == 0) { argc--; argv++; n = atoi(argv[0]); } else if (strcmp(argv[0],"-s") == 0) { toku_ft_loader_set_size_factor(1); } else if (strcmp(argv[0],"-w") == 0) { do_write_errors = 1; } else if (strcmp(argv[0],"-m") == 0) { do_malloc_errors = 1; do_realloc_errors = 1; } else if (strcmp(argv[0],"-u") == 0) { do_user_errors = 1; } else if (strcmp(argv[0],"--realloc_errors") == 0) { do_realloc_errors = 1; } else if (strcmp(argv[0],"--malloc_limit") == 0 && argc > 1) { argc--; argv++; my_big_malloc_limit = atoi(argv[0]); } else if (argc!=1) { return usage(progname, n); } else { break; } argc--; argv++; } const char* directory = TOKU_TEST_FILENAME; char unlink_all[strlen(directory)+20]; snprintf(unlink_all, strlen(directory)+20, "rm -rf %s", directory); int templen = strlen(directory)+15; char tf_template[templen]; int tlen = snprintf(tf_template, templen, "%s/tempXXXXXX", directory); assert (tlen>0 && tlen0 && olen