summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/format/import.c
blob: ff5454376162f691956a5a6b2407d97bf3323838 (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
/*-
 * 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 "format.h"

static void copy_file_into_directory(WT_SESSION *, const char *);
static void get_file_metadata(WT_SESSION *, const char **, const char **);
static void populate_table(WT_SESSION *);
static void verify_import(WT_SESSION *);

/*
 * Import directory initialize command, remove and create import directory, to place new database
 * connection.
 */
#define HOME_IMPORT_INIT_CMD "rm -rf %s/" IMPORT_DIR "&& mkdir %s/" IMPORT_DIR
#define IMPORT_DIR "IMPORT"
/*
 * The number of entries in the import table, primary use for validating contents after import.
 * There is no benefit to varying the number of entries in the import table.
 */
#define IMPORT_ENTRIES 1000
#define IMPORT_TABLE_CONFIG "key_format=i,value_format=i"
#define IMPORT_URI "table:import"
#define IMPORT_URI_FILE "file:import.wt"

/*
 * import --
 *     Periodically import table.
 */
WT_THREAD_RET
import(void *arg)
{
    WT_CONNECTION *conn, *import_conn;
    WT_SESSION *import_session, *session;
    size_t cmd_len;
    uint32_t import_value;
    u_int period;
    char buf[2048], *cmd;
    const char *file_config, *table_config;

    WT_UNUSED(arg);
    conn = g.wts_conn;
    file_config = table_config = NULL;
    import_value = 0;

    /*
     * Create a new database, primarily used for testing import.
     */
    cmd_len = strlen(g.home) * 2 + strlen(HOME_IMPORT_INIT_CMD) + 1;
    cmd = dmalloc(cmd_len);
    testutil_check(__wt_snprintf(cmd, cmd_len, HOME_IMPORT_INIT_CMD, g.home, g.home));
    testutil_checkfmt(system(cmd), "%s", "import directory creation failed");
    free(cmd);

    cmd_len = strlen(g.home) + strlen(IMPORT_DIR) + 10;
    cmd = dmalloc(cmd_len);
    testutil_check(__wt_snprintf(cmd, cmd_len, "%s/%s", g.home, IMPORT_DIR));
    /* Open a connection to the database, creating it if necessary. */
    create_database(cmd, &import_conn);
    free(cmd);

    /*
     * Open two sessions, one for test/format database and one for the import database.
     */
    testutil_check(import_conn->open_session(import_conn, NULL, NULL, &import_session));
    testutil_check(conn->open_session(conn, NULL, NULL, &session));

    /* Create new table and populate with data in import database. */
    testutil_checkfmt(
      import_session->create(import_session, IMPORT_URI, IMPORT_TABLE_CONFIG), "%s", IMPORT_URI);
    populate_table(import_session);

    /* Grab metadata information for table from import database connection. */
    get_file_metadata(import_session, &file_config, &table_config);

    while (!g.workers_finished) {
        /* Copy table into test/format database directory. */
        copy_file_into_directory(import_session, "import.wt");

        /* Perform import with either repair or file metadata. */
        import_value = mmrand(NULL, 0, 1);
        if (import_value == 0)
            testutil_check(__wt_snprintf(buf, sizeof(buf), "import=(enabled,repair=true)"));
        else
            testutil_check(__wt_snprintf(buf, sizeof(buf),
              "%s,import=(enabled,repair=false,file_metadata=(%s))", table_config, file_config));
        testutil_check(session->create(session, IMPORT_URI, buf));

        verify_import(session);

        /* Drop import table, so we can import the table again */
        testutil_drop(session, IMPORT_URI, NULL);

        period = mmrand(NULL, 1, 10);
        while (period > 0 && !g.workers_finished) {
            --period;
            __wt_sleep(1, 0);
        }
    }
    wts_close(&import_conn);
    testutil_check(session->close(session, NULL));
    return (WT_THREAD_RET_VALUE);
}

/*
 * verify_import --
 *     Verify all the values inside the imported table.
 */
static void
verify_import(WT_SESSION *session)
{
    WT_CURSOR *cursor;
    WT_DECL_RET;
    int iteration, key, value;

    iteration = 0;
    testutil_check(session->open_cursor(session, IMPORT_URI, NULL, NULL, &cursor));

    while ((ret = cursor->next(cursor)) == 0) {
        testutil_check(cursor->get_key(cursor, &key));
        testutil_assert(key == iteration);
        testutil_check(cursor->get_value(cursor, &value));
        testutil_assert(value == iteration);
        iteration++;
    }
    testutil_assert(iteration == IMPORT_ENTRIES);
    scan_end_check(ret == WT_NOTFOUND);
    testutil_check(cursor->close(cursor));
}

/*
 * populate_table --
 *     Populate the import table with simple data.
 */
static void
populate_table(WT_SESSION *session)
{
    WT_CURSOR *cursor;
    int i;

    testutil_check(session->open_cursor(session, IMPORT_URI, NULL, NULL, &cursor));

    for (i = 0; i < IMPORT_ENTRIES; ++i) {
        cursor->set_key(cursor, i);
        cursor->set_value(cursor, i);
        testutil_check(cursor->insert(cursor));
    }
    testutil_check(cursor->close(cursor));
    testutil_check(session->checkpoint(session, NULL));
}

/*
 * get_file_metadata --
 *     Get import file and table metadata information from import database connection.
 */
static void
get_file_metadata(WT_SESSION *session, const char **file_config, const char **table_config)
{
    WT_CURSOR *metadata_cursor;

    testutil_check(session->open_cursor(session, "metadata:", NULL, NULL, &metadata_cursor));
    metadata_cursor->set_key(metadata_cursor, IMPORT_URI);
    testutil_check(metadata_cursor->search(metadata_cursor));
    metadata_cursor->get_value(metadata_cursor, table_config);

    metadata_cursor->set_key(metadata_cursor, IMPORT_URI_FILE);
    testutil_check(metadata_cursor->search(metadata_cursor));
    metadata_cursor->get_value(metadata_cursor, file_config);

    testutil_check(metadata_cursor->close(metadata_cursor));
}

/*
 * copy_file_into_directory --
 *     Copy a single file into the test/format directory.
 */
static void
copy_file_into_directory(WT_SESSION *session, const char *name)
{
    size_t buf_len;
    char to[64];

    buf_len = strlen(name) + 10;
    testutil_check(__wt_snprintf(to, buf_len, "../%s", name));
    testutil_check(__wt_copy_and_sync(session, name, to));
}