summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/test/cppsuite/test_harness/workload_generator.h
blob: 78b53475e58b2f52214714c3e31e5a98efd7cddf (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
#ifndef WORKLOAD_GENERATOR_H
#define WORKLOAD_GENERATOR_H

#include "api_const.h"
#include "configuration_settings.h"
#include "random_generator.h"
#include "debug_utils.h"
#include "thread_manager.h"

namespace test_harness {
class workload_generator {
    public:
    workload_generator(configuration *configuration)
    {
        _configuration = configuration;
    }

    ~workload_generator()
    {
        if (_session != nullptr) {
            if (_session->close(_session, NULL) != 0)
                /* Failing to close session is not blocking. */
                debug_info(
                  "Failed to close session, shutting down uncleanly", _trace_level, DEBUG_ERROR);
            _session = nullptr;
        }

        if (_conn != nullptr) {
            if (_conn->close(_conn, NULL) != 0)
                /* Failing to close connection is not blocking. */
                debug_info(
                  "Failed to close connection, shutting down uncleanly", _trace_level, DEBUG_ERROR);
            _conn = nullptr;
        }
    }

    /*
     * Function that performs the following steps using the configuration that is defined by the
     * test:
     *  - Create the working dir.
     *  - Open a connection.
     *  - Open a session.
     *  - Create n collections as per the configuration.
     *      - Open a cursor on each collection.
     *      - Insert m key/value pairs in each collection. Values are random strings which size is
     * defined by the configuration.
     */
    int
    load(const char *home = DEFAULT_DIR)
    {
        WT_CURSOR *cursor;
        int64_t collection_count, key_count, value_size;
        std::string collection_name;

        cursor = nullptr;
        collection_count = key_count = value_size = 0;
        collection_name = "";

        /* Create the working dir. */
        testutil_make_work_dir(home);

        /* Open connection. */
        testutil_check(wiredtiger_open(home, NULL, CONNECTION_CREATE, &_conn));

        /* Open session. */
        testutil_check(_conn->open_session(_conn, NULL, NULL, &_session));

        /* Create n collections as per the configuration and store each collection name. */
        testutil_check(_configuration->get_int(COLLECTION_COUNT, collection_count));
        for (int i = 0; i < collection_count; ++i) {
            collection_name = "table:collection" + std::to_string(i);
            testutil_check(
              _session->create(_session, collection_name.c_str(), DEFAULT_TABLE_SCHEMA));
            _collection_names.push_back(collection_name);
        }
        debug_info(
          std::to_string(collection_count) + " collections created", _trace_level, DEBUG_INFO);

        /* Open a cursor on each collection and use the configuration to insert key/value pairs. */
        testutil_check(_configuration->get_int(KEY_COUNT, key_count));
        testutil_check(_configuration->get_int(VALUE_SIZE, value_size));
        for (const auto &collection_name : _collection_names) {
            /* WiredTiger lets you open a cursor on a collection using the same pointer. When a
             * session is closed, WiredTiger APIs close the cursors too. */
            testutil_check(
              _session->open_cursor(_session, collection_name.c_str(), NULL, NULL, &cursor));
            for (size_t j = 0; j < key_count; ++j) {
                cursor->set_key(cursor, j);
                /* Generation of a random string value using the size defined in the test
                 * configuration. */
                std::string generated_value =
                  random_generator::random_generator::get_instance().generate_string(value_size);
                cursor->set_value(cursor, generated_value.c_str());
                testutil_check(cursor->insert(cursor));
            }
        }
        debug_info(
          std::to_string(collection_count) + " key/value inserted", _trace_level, DEBUG_INFO);
        debug_info("Load stage done", _trace_level, DEBUG_INFO);
        return (0);
    }

    /* Do the work of the main part of the workload. */
    int
    run()
    {

        WT_SESSION *session;
        int64_t duration_seconds, read_threads;

        session = nullptr;
        duration_seconds = read_threads = 0;

        testutil_check(_configuration->get_int(DURATION_SECONDS, duration_seconds));
        testutil_check(_configuration->get_int(READ_THREADS, read_threads));

        /* Generate threads to execute read operations on the collections. */
        for (int i = 0; i < read_threads; ++i) {
            testutil_check(_conn->open_session(_conn, NULL, NULL, &session));
            thread_context *tc =
              new thread_context(session, _collection_names, thread_operation::READ);
            _thread_manager.add_thread(tc, &execute_operation);
        }

        /*
         * Spin until duration seconds has expired. If the call to run() returns we destroy the test
         * and the workload generator.
         */
        std::this_thread::sleep_for(std::chrono::seconds(duration_seconds));
        _thread_manager.finish();
        debug_info("Run stage done", _trace_level, DEBUG_INFO);
        return 0;
    }

    /* Workload threaded operations. */
    static void
    execute_operation(thread_context &context)
    {
        thread_operation operation;

        operation = context.get_thread_operation();

        if (context.get_session() == nullptr) {
            testutil_die(DEBUG_ABORT, "system: execute_operation : Session is NULL");
        }

        switch (operation) {
        case thread_operation::INSERT:
            /* Sleep until it is implemented. */
            std::this_thread::sleep_for(std::chrono::seconds(1));
            break;
        case thread_operation::READ:
            read_operation(context);
            break;
        case thread_operation::REMOVE:
            /* Sleep until it is implemented. */
            std::this_thread::sleep_for(std::chrono::seconds(1));
            break;
        case thread_operation::UPDATE:
            /* Sleep until it is implemented. */
            std::this_thread::sleep_for(std::chrono::seconds(1));
            break;
        default:
            testutil_die(DEBUG_ABORT, "system: thread_operation is unknown : %d",
              static_cast<int>(thread_operation::UPDATE));
            break;
        }
    }

    /* Basic read operation that walks a cursors across all collections. */
    static void
    read_operation(thread_context &context)
    {
        WT_CURSOR *cursor;
        std::vector<WT_CURSOR *> cursors;

        /* Get a cursor for each collection in collection_names. */
        for (const auto &it : context.get_collection_names()) {
            testutil_check(context.get_session()->open_cursor(
              context.get_session(), it.c_str(), NULL, NULL, &cursor));
            cursors.push_back(cursor);
        }

        while (context.is_running()) {
            /* Walk each cursor. */
            for (const auto &it : cursors)
                it->next(it);
        }
    }

    /* WiredTiger APIs wrappers for single operations. */
    static int
    insert(WT_CURSOR *cursor)
    {
        if (cursor == nullptr)
            throw std::invalid_argument("Failed to call insert, invalid cursor");
        return (cursor->insert(cursor));
    }

    static int
    search(WT_CURSOR *cursor)
    {
        if (cursor == nullptr)
            throw std::invalid_argument("Failed to call search, invalid cursor");
        return (cursor->search(cursor));
    }

    static int
    search_near(WT_CURSOR *cursor, int *exact)
    {
        if (cursor == nullptr)
            throw std::invalid_argument("Failed to call search_near, invalid cursor");
        return (cursor->search_near(cursor, exact));
    }

    static int
    update(WT_CURSOR *cursor)
    {
        if (cursor == nullptr)
            throw std::invalid_argument("Failed to call update, invalid cursor");
        return (cursor->update(cursor));
    }

    private:
    std::vector<std::string> _collection_names;
    configuration *_configuration = nullptr;
    WT_CONNECTION *_conn = nullptr;
    WT_SESSION *_session = nullptr;
    thread_manager _thread_manager;
};
} // namespace test_harness

#endif