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

static const char name[] = "lsm:test";
#define NUM_DOCS 100000
#define NUM_QUERIES (NUM_DOCS / 100)

static void
rand_str(uint64_t i, char *str)
{
    uint64_t x, y;

    y = strlen(str);
    for (x = y; x > y - 8; x--) {
        str[x - 1] = (char)(i % 10) + 48;
        i = i / 10;
    }
}

static void
check_str(uint64_t i, char *str, bool mod)
{
    char str2[] = "0000000000000000";

    rand_str(i, str2);
    if (mod)
        str2[0] = 'A';
    testutil_checkfmt(strcmp(str, str2), "strcmp failed, got %s, expected %s", str, str2);
}

static void
query_docs(WT_CURSOR *cursor, bool mod)
{
    WT_ITEM key, value;
    int i;

    for (i = 0; i < NUM_QUERIES; i++) {
        testutil_check(cursor->next(cursor));
        testutil_check(cursor->get_key(cursor, &key));
        testutil_check(cursor->get_value(cursor, &value));
        check_str((uint64_t)key.data, (char *)value.data, mod);
    }
    printf("%d documents read\n", NUM_QUERIES);
}

static void *
compact_thread(void *args)
{
    WT_SESSION *session;

    session = (WT_SESSION *)args;
    testutil_check(session->compact(session, name, NULL));
    return (NULL);
}

int
main(int argc, char *argv[])
{
    TEST_OPTS *opts, _opts;
    WT_CURSOR *rcursor, *wcursor;
    WT_ITEM key, value;
    WT_SESSION *session, *session2;
    pthread_t thread;
    uint64_t i;

    char str[] = "0000000000000000";

    /*
     * Create a clean test directory for this run of the test program if the environment variable
     * isn't already set (as is done by make check).
     */
    opts = &_opts;
    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, NULL, "create,cache_size=200M", &opts->conn));

    testutil_check(opts->conn->open_session(opts->conn, NULL, NULL, &session));
    testutil_check(opts->conn->open_session(opts->conn, NULL, NULL, &session2));

    testutil_check(session->create(session, name, "key_format=Q,value_format=S"));

    /* Populate the table with some data. */
    testutil_check(session->open_cursor(session, name, NULL, "overwrite", &wcursor));
    for (i = 0; i < NUM_DOCS; i++) {
        wcursor->set_key(wcursor, i);
        rand_str(i, str);
        wcursor->set_value(wcursor, str);
        testutil_check(wcursor->insert(wcursor));
    }
    testutil_check(wcursor->close(wcursor));
    printf("%d documents inserted\n", NUM_DOCS);

    /* Perform some random reads */
    testutil_check(session->open_cursor(session, name, NULL, "next_random=true", &rcursor));
    query_docs(rcursor, false);
    testutil_check(rcursor->close(rcursor));

    /* Setup Transaction to pin the current values */
    testutil_check(session2->begin_transaction(session2, "isolation=snapshot"));
    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));

    /* Perform updates in a txn to confirm that we see only the original. */
    testutil_check(session->open_cursor(session, name, NULL, "overwrite", &wcursor));
    for (i = 0; i < NUM_DOCS; i++) {
        rand_str(i, str);
        str[0] = 'A';
        wcursor->set_key(wcursor, i);
        wcursor->set_value(wcursor, str);
        testutil_check(wcursor->update(wcursor));
    }
    testutil_check(wcursor->close(wcursor));
    printf("%d documents set to update\n", NUM_DOCS);

    /* Random reads, which should see the original values */
    query_docs(rcursor, false);
    testutil_check(rcursor->close(rcursor));

    /* Finish the txn */
    testutil_check(session2->rollback_transaction(session2, NULL));

    /* Random reads, which should see the updated values */
    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));
    query_docs(rcursor, true);
    testutil_check(rcursor->close(rcursor));

    /* Setup a pre-delete txn */
    testutil_check(session2->begin_transaction(session2, "isolation=snapshot"));
    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));

    /* Delete all but one document */
    testutil_check(session->open_cursor(session, name, NULL, "overwrite", &wcursor));
    for (i = 0; i < NUM_DOCS - 1; i++) {
        wcursor->set_key(wcursor, i);
        testutil_check(wcursor->remove(wcursor));
    }
    testutil_check(wcursor->close(wcursor));
    printf("%d documents deleted\n", NUM_DOCS - 1);

    /* Random reads, which should not see the deletes */
    query_docs(rcursor, true);
    testutil_check(rcursor->close(rcursor));

    /* Rollback the txn so we can see the deletes */
    testutil_check(session2->rollback_transaction(session2, NULL));

    /* Find the one remaining document 3 times */
    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));
    for (i = 0; i < 3; i++) {
        testutil_check(rcursor->next(rcursor));
        testutil_check(rcursor->get_key(rcursor, &key));
        testutil_check(rcursor->get_value(rcursor, &value));
        /* There should only be one value available to us */
        testutil_assertfmt((uint64_t)key.data == NUM_DOCS - 1, "expected %d and got %" PRIu64,
          NUM_DOCS - 1, (uint64_t)key.data);
        check_str((uint64_t)key.data, (char *)value.data, true);
    }
    printf("Found the deleted doc 3 times\n");
    testutil_check(rcursor->close(rcursor));

    /* Repopulate the table for compact. */
    testutil_check(session->open_cursor(session, name, NULL, "overwrite", &wcursor));
    for (i = 0; i < NUM_DOCS - 1; i++) {
        wcursor->set_key(wcursor, i);
        rand_str(i, str);
        str[0] = 'A';
        wcursor->set_value(wcursor, str);
        testutil_check(wcursor->insert(wcursor));
    }
    testutil_check(wcursor->close(wcursor));

    /* Run random cursor queries while compact is running */
    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));
    testutil_check(pthread_create(&thread, NULL, compact_thread, session));
    query_docs(rcursor, true);
    testutil_check(rcursor->close(rcursor));
    testutil_check(pthread_join(thread, NULL));

    /* Delete everything. Check for infinite loops */
    testutil_check(session->open_cursor(session, name, NULL, "overwrite", &wcursor));
    for (i = 0; i < NUM_DOCS; i++) {
        wcursor->set_key(wcursor, i);
        testutil_check(wcursor->remove(wcursor));
    }
    testutil_check(wcursor->close(wcursor));

    testutil_check(session2->open_cursor(session2, name, NULL, "next_random=true", &rcursor));
    for (i = 0; i < 3; i++)
        testutil_assert(rcursor->next(rcursor) == WT_NOTFOUND);
    printf("Successfully got WT_NOTFOUND\n");

    testutil_cleanup(opts);
    return (EXIT_SUCCESS);
}