summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/csuite/wt3363_checkpoint_op_races/main.c
blob: 02a9b26546467b20f81196993490174e730b5b60 (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
/*-
 * Public Domain 2014-present MongoDB, Inc.
 * Public Domain 2008-2014 WiredTiger, Inc.
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or
 * distribute this software, either in source code form or as a compiled
 * binary, for any purpose, commercial or non-commercial, and by any
 * means.
 *
 * In jurisdictions that recognize copyright laws, the author or authors
 * of this software dedicate any and all copyright interest in the
 * software to the public domain. We make this dedication for the benefit
 * of the public at large and to the detriment of our heirs and
 * successors. We intend this dedication to be an overt act of
 * relinquishment in perpetuity of all present and future rights to this
 * software under copyright law.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
#include "test_util.h"

/*
 * JIRA ticket reference: WT-3363
 *
 * Test case description: There are a number of operations that we run that we
 * expect not to conflict with or block against a running checkpoint. This test
 * aims to run repeated checkpoints in a thread, while running an assortment
 * of operations that we expect to execute quickly on further threads. To
 * ensure that we catch any blockages we introduce a very large delay into the
 * checkpoint and measure that no operation takes 1/2 the length of this delay.
 *
 * Failure mode: We monitor the execution time of all operations and if we find
 * any operation taking longer than 1/2 the delay time, we abort dumping a core
 * file which can be used to determine what operation was blocked.
 */
static WT_THREAD_RET do_checkpoints(void *);
static WT_THREAD_RET do_ops(void *);
static WT_THREAD_RET monitor(void *);

/*
 * Time delay to introduce into checkpoints in seconds. Should be at-least double the maximum time
 * that any one of the operations should take. Currently this is set to 10 seconds and we expect no
 * single operation to take longer than 5 seconds.
 */
#define MAX_EXECUTION_TIME 10
#define N_THREADS 10

/*
 * Number of seconds to execute for. Initially set to 15 minutes, as we need to run long enough to
 * be certain we have captured any blockages. In initial testing 5 minutes was enough to reproduce
 * the issue, so we run for 3x that here to ensure we reproduce before declaring success.
 */
#define RUNTIME 900.0

static WT_EVENT_HANDLER event_handler = {handle_op_error, handle_op_message, NULL, NULL};

int
main(int argc, char *argv[])
{
    TEST_OPTS *opts, _opts;
    TEST_PER_THREAD_OPTS thread_args[N_THREADS];
    pthread_t ckpt_thread, mon_thread, threads[N_THREADS];
    int i;

    /*
     * This test should not run unless long tests flag is set. The test runs for 15 minutes.
     */
    if (!testutil_is_flag_set("TESTUTIL_ENABLE_TIMING_TESTS"))
        return (EXIT_SUCCESS);

    opts = &_opts;
    opts->unique_id = 0;
    memset(opts, 0, sizeof(*opts));

    testutil_check(testutil_parse_opts(argc, argv, opts));
    testutil_make_work_dir(opts->home);

    testutil_check(wiredtiger_open(opts->home, &event_handler,
      "create,cache_size=1G,timing_stress_for_test=[checkpoint_slow]", &opts->conn));

    testutil_check(pthread_create(&ckpt_thread, NULL, do_checkpoints, opts));

    for (i = 0; i < N_THREADS; ++i) {
        thread_args[i].testopts = opts;
        thread_args[i].thread_counter = 0;
        thread_args[i].threadnum = i;
        testutil_check(pthread_create(&threads[i], NULL, do_ops, &thread_args[i]));
    }

    /*
     * Pass the whole array of thread arguments to the monitoring thread. This thread will need to
     * monitor each threads counter to track if it is stuck.
     */
    testutil_check(pthread_create(&mon_thread, NULL, monitor, thread_args));

    for (i = 0; i < N_THREADS; ++i)
        testutil_check(pthread_join(threads[i], NULL));

    testutil_check(pthread_join(mon_thread, NULL));

    testutil_check(pthread_join(ckpt_thread, NULL));

    printf("Success\n");

    testutil_cleanup(opts);
    return (EXIT_SUCCESS);
}

/*
 * Function for repeatedly running checkpoint operations.
 */
static WT_THREAD_RET
do_checkpoints(void *_opts)
{
    TEST_OPTS *opts;
    WT_DECL_RET;
    WT_SESSION *session;
    time_t now, start;

    opts = (TEST_OPTS *)_opts;
    (void)time(&start);
    (void)time(&now);

    while (difftime(now, start) < RUNTIME) {
        testutil_check(opts->conn->open_session(opts->conn, NULL, NULL, &session));

        if ((ret = session->checkpoint(session, "force")) != 0)
            if (ret != EBUSY && ret != ENOENT)
                testutil_die(ret, "session.checkpoint");

        testutil_check(session->close(session, NULL));

        /*
         * A short sleep to let operations process and avoid back to back checkpoints locking up
         * resources.
         */
        sleep(1);
        (void)time(&now);
    }

    return (WT_THREAD_RET_VALUE);
}

/*
 * Function to monitor running operations and abort to dump core in the event that we catch an
 * operation running long.
 */
static WT_THREAD_RET
monitor(void *args)
{
    TEST_PER_THREAD_OPTS *thread_args;
    time_t now, start;
    int ctr, i, last_ops[N_THREADS];

    thread_args = (TEST_PER_THREAD_OPTS *)args;

    (void)time(&start);
    (void)time(&now);

    memset(last_ops, 0, sizeof(int) + N_THREADS);

    while (difftime(now, start) < RUNTIME) {
        /*
         * Checkpoints will run for slightly over MAX_EXECUTION_TIME. MAX_EXECUTION_TIME should
         * always be long enough that we can complete any single operation in 1/2 that time.
         */
        sleep(MAX_EXECUTION_TIME / 2);

        for (i = 0; i < N_THREADS; i++) {
            ctr = thread_args[i].thread_counter;

            /* Ignore any threads which may not have started yet. */
            if (ctr == 0)
                continue;

            /*
             * We track how many operations each thread has done. If we have slept and the counter
             * remains the same for a thread it is stuck and should drop a core so the cause of the
             * hang can be investigated.
             */
            if (ctr != last_ops[i])
                last_ops[i] = ctr;
            else {
                printf(
                  "Thread %d had a task running"
                  " for more than %d seconds\n",
                  i, MAX_EXECUTION_TIME / 2);
                abort();
            }
        }
        (void)time(&now);
    }

    return (WT_THREAD_RET_VALUE);
}

/*
 * Worker thread. Executes random operations from the set of 6.
 */
static WT_THREAD_RET
do_ops(void *args)
{
    WT_RAND_STATE rnd;
    time_t now, start;

    __wt_random_init_seed(NULL, &rnd);
    (void)time(&start);
    (void)time(&now);

    while (difftime(now, start) < RUNTIME) {
        switch (__wt_random(&rnd) % 6) {
        case 0:
            op_bulk(args);
            break;
        case 1:
            op_create(args);
            break;
        case 2:
            op_cursor(args);
            break;
        case 3:
            op_drop(args);
            break;
        case 4:
            op_bulk_unique(args);
            break;
        case 5:
            op_create_unique(args);
            break;
        }
        (void)time(&now);
    }

    return (WT_THREAD_RET_VALUE);
}