summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/csuite/truncated_log/main.c
blob: 5774ff17d2345316743eba797a71ff3c4903d50e (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
/*-
 * 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"

#include <sys/wait.h>

static char home[1024]; /* Program working dir */
static const char *const uri = "table:main";
static bool use_columns = false;

#define RECORDS_FILE "records"

#define ENV_CONFIG                                                                                \
    "create,log=(enabled,file_max=100K,remove=false),"                                            \
    "transaction_sync=(enabled,method=none),statistics=(all),statistics_log=(json,on_close,wait=" \
    "1)"
#define ENV_CONFIG_ADD_REC "log=(recover=on),statistics=(all),statistics_log=(json,on_close,wait=1)"

#define LOG_FILE_1 "WiredTigerLog.0000000001"

#define K_SIZE 16
#define V_SIZE 256

static void write_and_read_new(WT_SESSION *);

/*
 * write_and_read_new --
 *     Write a new log record into the log via log print, then open up a log cursor and walk the log
 *     to make sure we can read it. The reason for this test is that if there is a partial log
 *     record at the end of the previous log file and truncate does not exist, this tests that we
 *     can still read past that record.
 */
static void
write_and_read_new(WT_SESSION *session)
{
    WT_CURSOR *logc;
    WT_ITEM logrec_key, logrec_value;
    uint64_t txnid;
    uint32_t fileid, log_file, log_offset, opcount, optype, rectype;
    bool saw_msg;

    /*
     * Write a log record and force it to disk so we can read it.
     */
    printf("Write log_printf record and verify.\n");
    testutil_check(session->log_printf(session, "Test Log Record"));
    testutil_check(session->log_flush(session, "sync=on"));
    testutil_check(session->open_cursor(session, "log:", NULL, NULL, &logc));
    saw_msg = false;
    while (logc->next(logc) == 0) {
        /*
         * We don't really need to get the key, but in case we want the LSN for some message, get
         * it.
         */
        testutil_check(logc->get_key(logc, &log_file, &log_offset, &opcount));
        testutil_check(
          logc->get_value(logc, &txnid, &rectype, &optype, &fileid, &logrec_key, &logrec_value));
        /*
         * We should never see a record from us in log file 2. We wrote a record there, but then the
         * record in log file 1 was truncated to be a partial record, ending the log there. So
         * everything after that, including everything in log file 2, is invalid until we get to log
         * file 3 which is where the post-recovery records will be written. The one exception in log
         * file two is the system record for the previous log file's LSN. Although it is written by
         * the system, we do walk it when using a cursor.
         */
        if (log_file == 2 && rectype != WT_LOGREC_SYSTEM)
            testutil_die(EINVAL, "Found LSN in Log 2");
#if 0
		printf("LSN [%" PRIu32 "][%" PRIu32 "].%" PRIu32
		    ": record type %" PRIu32 " optype %" PRIu32
		    " txnid %" PRIu64 " fileid %" PRIu32 "\n",
		    log_file, log_offset, opcount,
		    rectype, optype, txnid, fileid);
#endif
        if (rectype == WT_LOGREC_MESSAGE) {
            saw_msg = true;
            printf("Application Record: %s\n", (char *)logrec_value.data);
            break;
        }
    }
    testutil_check(logc->close(logc));
    if (!saw_msg)
        testutil_die(EINVAL, "Did not traverse log printf record");
}

static void usage(void) WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));

/*
 * usage --
 *     TODO: Add a comment describing this function.
 */
static void
usage(void)
{
    fprintf(stderr, "usage: %s [-h dir]\n", progname);
    exit(EXIT_FAILURE);
}

static void fill_db(void) WT_GCC_FUNC_DECL_ATTRIBUTE((noreturn));

/*
 * fill_db --
 *     Child process creates the database and table, and then writes data into the table until it
 *     switches into log file 2.
 */
static void
fill_db(void)
{
    FILE *fp;
    WT_CONNECTION *conn;
    WT_CURSOR *cursor, *logc;
    WT_LSN lsn, save_lsn;
    WT_SESSION *session;
    uint32_t i, max_key, min_key, units, unused;
    char k[K_SIZE], v[V_SIZE];
    const char *table_config;
    bool first;

    if (use_columns)
        table_config = "key_format=r,value_format=S";
    else
        table_config = "key_format=S,value_format=S";

    /*
     * Run in the home directory so that the records file is in there too.
     */
    if (chdir(home) != 0)
        testutil_die(errno, "chdir: %s", home);
    testutil_check(wiredtiger_open(NULL, NULL, ENV_CONFIG, &conn));
    testutil_check(conn->open_session(conn, NULL, NULL, &session));
    testutil_check(session->create(session, uri, table_config));
    testutil_check(session->open_cursor(session, uri, NULL, NULL, &cursor));

    /*
     * Keep a separate file with the records we wrote for checking.
     */
    (void)unlink(RECORDS_FILE);
    if ((fp = fopen(RECORDS_FILE, "w")) == NULL)
        testutil_die(errno, "fopen");
    /*
     * Set to no buffering.
     */
    __wt_stream_set_no_buffer(fp);
    save_lsn.l.file = 0;

    /*
     * Write data into the table until we move to log file 2. We do the calculation below so that we
     * don't have to walk the log for every record.
     *
     * Calculate about how many records should fit in the log file. Subtract a bunch for metadata
     * and file creation records. Then subtract out a few more records to be conservative.
     */
    units = (K_SIZE + V_SIZE) / 128 + 1;
    min_key = 90000 / (units * 128) - 15;
    max_key = min_key * 2;
    first = true;
    for (i = 0; i < max_key; ++i) {
        if (use_columns)
            cursor->set_key(cursor, i + 1);
        else {
            testutil_check(__wt_snprintf(k, sizeof(k), "key%03" PRIu32, i));
            cursor->set_key(cursor, k);
        }
        testutil_check(
          __wt_snprintf(v, sizeof(v), "value%0*" PRIu32, (int)(V_SIZE - (strlen("value") + 1)), i));
        cursor->set_value(cursor, v);
        testutil_check(cursor->insert(cursor));

        /*
         * Walking the ever growing log can be slow, so only start looking for the cross into log
         * file 2 after a minimum.
         */
        if (i > min_key) {
            testutil_check(session->open_cursor(session, "log:", NULL, NULL, &logc));
            if (save_lsn.l.file != 0) {
                logc->set_key(logc, save_lsn.l.file, save_lsn.l.offset, 0);
                testutil_check(logc->search(logc));
            }
            while (logc->next(logc) == 0) {
                testutil_check(logc->get_key(logc, &lsn.l.file, &lsn.l.offset, &unused));
                /*
                 * Save the LSN so that we know the offset of the last LSN in log file 1 later.
                 */
                if (lsn.l.file < 2)
                    save_lsn = lsn;
                else {
                    /*
                     * If this is the first time through that the key is larger than the minimum key
                     * and we're already in log file 2 then we did not calculate correctly and the
                     * test should fail.
                     */
                    if (first)
                        testutil_die(EINVAL, "min_key too high");
                    if (fprintf(fp, "%" PRIu32 " %" PRIu32 "\n", save_lsn.l.offset, i - 1) == -1)
                        testutil_die(errno, "fprintf");
                    break;
                }
            }
            first = false;
            testutil_check(logc->close(logc));
        }
    }
    if (fclose(fp) != 0)
        testutil_die(errno, "fclose");
    _exit(EXIT_SUCCESS);
}

extern int __wt_optind;
extern char *__wt_optarg;

/*
 * main --
 *     TODO: Add a comment describing this function.
 */
int
main(int argc, char *argv[])
{
    FILE *fp;
    WT_CONNECTION *conn;
    WT_CURSOR *cursor;
    WT_SESSION *session;
    pid_t pid;
    uint64_t new_offset, offset;
    uint32_t count, max_key;
    int ch, ret, status;
    const char *working_dir;
    bool preserve;

    preserve = false;
    (void)testutil_set_progname(argv);

    working_dir = "WT_TEST.truncated-log";
    while ((ch = __wt_getopt(progname, argc, argv, "ch:")) != EOF)
        switch (ch) {
        case 'c':
            /* Variable-length columns only (for now) */
            use_columns = true;
            break;
        case 'h':
            working_dir = __wt_optarg;
            break;
        case 'p':
            preserve = true;
            break;
        default:
            usage();
        }
    argc -= __wt_optind;
    if (argc != 0)
        usage();

    testutil_work_dir_from_path(home, sizeof(home), working_dir);
    testutil_make_work_dir(home);

    /*
     * Fork a child to do its work. Wait for it to exit.
     */
    testutil_assert_errno((pid = fork()) >= 0);

    if (pid == 0) { /* child */
        fill_db();
        /* NOTREACHED */
    }

    /* parent */
    /* Wait for child to kill itself. */
    testutil_assert_errno(waitpid(pid, &status, 0) != -1);

    /*
     * !!! If we wanted to take a copy of the directory before recovery,
     * this is the place to do it.
     */
    if (chdir(home) != 0)
        testutil_die(errno, "chdir: %s", home);

    printf("Open database, run recovery and verify content\n");
    if ((fp = fopen(RECORDS_FILE, "r")) == NULL)
        testutil_die(errno, "fopen");
    ret = fscanf(fp, "%" SCNu64 " %" SCNu32 "\n", &offset, &max_key);
    if (ret != 2)
        testutil_die(errno, "fscanf");
    if (fclose(fp) != 0)
        testutil_die(errno, "fclose");
    /*
     * The offset is the beginning of the last record. Truncate to the middle of that last record
     * (i.e. ahead of that offset).
     */
    if (offset > UINT64_MAX - V_SIZE)
        testutil_die(ERANGE, "offset");
    new_offset = offset + V_SIZE;
    printf("Parent: Log file 1: Key %" PRIu32 " at %" PRIu64 "\n", max_key, offset);
    printf("Parent: Truncate mid-record to %" PRIu64 "\n", new_offset);
    if (truncate(LOG_FILE_1, (wt_off_t)new_offset) != 0)
        testutil_die(errno, "truncate");

    testutil_check(wiredtiger_open(NULL, NULL, ENV_CONFIG_ADD_REC, &conn));
    testutil_check(conn->open_session(conn, NULL, NULL, &session));
    testutil_check(session->open_cursor(session, uri, NULL, NULL, &cursor));

    /*
     * For every key in the saved file, verify that the key exists in the table after recovery.
     * Since we did write-no-sync, we expect every key to have been recovered.
     */
    count = 0;
    while (cursor->next(cursor) == 0)
        ++count;
    /*
     * The max key in the saved file is the key we truncated, but the key space starts at 0 and
     * we're counting the records here, so we expect the max key number of records. Add one for the
     * system record for the previous LSN that the cursor will see too.
     */
    if (count > (max_key + 1)) {
        printf("expected %" PRIu32 " records found %" PRIu32 "\n", max_key, count);
        return (EXIT_FAILURE);
    }
    printf("%" PRIu32 " records verified\n", count);

    /*
     * Write a log record and then walk the log to make sure we can read that log record that is
     * beyond the truncated record.
     */
    write_and_read_new(session);
    testutil_check(conn->close(conn, NULL));
    if (!preserve) {
        /* At this point $PATH is inside `home`, which we intend to delete. cd to the parent dir. */
        if (chdir("../") != 0)
            testutil_die(errno, "root chdir: %s", home);
        testutil_clean_work_dir(home);
    }

    return (EXIT_SUCCESS);
}