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 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #pragma once
40
41 #include "test.h"
42
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include <toku_pthread.h>
47 #include <unistd.h>
48 #include <memory.h>
49 #include <sys/stat.h>
50 #include <db.h>
51
52 #include "threaded_stress_test_helpers.h"
53
54 toku_pthread_t checkpoint_tid;
55 static int cnt = 0;
56 static bool starting_a_chkpt = false;
57
58 int state_to_crash = 0;
59
do_checkpoint_and_crash(void * arg)60 static void *do_checkpoint_and_crash(void *arg) {
61 // first verify that checkpointed_data is correct;
62 DB_ENV* CAST_FROM_VOIDP(env, arg);
63 if (verbose) printf("starting a checkpoint\n");
64 int r = env->txn_checkpoint(env, 0, 0, 0); assert(r==0);
65 if (verbose) printf("completed a checkpoint, about to crash\n");
66 toku_hard_crash_on_purpose();
67 return arg;
68 }
69
flt_callback(int flt_state,void * extra)70 static void flt_callback(int flt_state, void* extra) {
71 cnt++;
72 if (verbose) printf("flt_state!! %d\n", flt_state);
73 if (cnt > 0 && !starting_a_chkpt && flt_state == state_to_crash) {
74 starting_a_chkpt = true;
75 if (verbose)
76 printf("flt_state %d\n", flt_state);
77 int r = toku_pthread_create(toku_uninstrumented,
78 &checkpoint_tid,
79 nullptr,
80 do_checkpoint_and_crash,
81 extra);
82 assert(r == 0);
83 usleep(2 * 1000 * 1000);
84 }
85 }
86
87
88 static void
stress_table(DB_ENV * env,DB ** dbp,struct cli_args * cli_args)89 stress_table(DB_ENV *env, DB **dbp, struct cli_args *cli_args) {
90 //
91 // the threads that we want:
92 // - one thread constantly updating random values
93 // - one thread doing table scan with bulk fetch
94 // - one thread doing table scan without bulk fetch
95 // - one thread doing random point queries
96 //
97
98 if (verbose) printf("starting creation of pthreads\n");
99 const int num_threads = 1;
100 struct arg myargs[num_threads];
101 for (int i = 0; i < num_threads; i++) {
102 arg_init(&myargs[i], dbp, env, cli_args);
103 }
104
105 // make the guy that updates the db
106 struct update_op_args uoe = get_update_op_args(cli_args, NULL);
107 myargs[0].operation_extra = &uoe;
108 myargs[0].operation = update_op;
109 //myargs[0].update_pad_frequency = 0;
110
111 db_env_set_flusher_thread_callback(flt_callback, env);
112 run_workers(myargs, num_threads, cli_args->num_seconds, true, cli_args);
113 }
114
115 static int
run_recover_flt_test(int argc,char * const argv[])116 run_recover_flt_test(int argc, char *const argv[]) {
117 struct cli_args args = get_default_args();
118 // make test time arbitrarily high because we expect a crash
119 args.num_seconds = 1000000000;
120 if (state_to_crash == 1) {
121 // Getting flt_state 1 (inbox flush) requires a larger tree with more messages floating in it
122 args.num_elements = 100000;
123 args.disperse_keys = true;
124 args.key_size = 8;
125 args.val_size = 192;
126 } else {
127 args.num_elements = 2000;
128 }
129 // we want to induce a checkpoint
130 args.env_args.checkpointing_period = 0;
131 args.env_args.cachetable_size = 20 * 1024 * 1024;
132 parse_stress_test_args(argc, argv, &args);
133 if (args.do_test_and_crash) {
134 stress_test_main(&args);
135 }
136 if (args.do_recover) {
137 stress_recover(&args);
138 }
139 return 0;
140 }
141