File: gcache_top_test.cpp

package info (click to toggle)
galera-4 26.4.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,968 kB
  • sloc: cpp: 133,534; ansic: 12,171; sh: 1,446; tcl: 51; makefile: 19
file content (293 lines) | stat: -rw-r--r-- 9,645 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2025 Codership Oy <info@codership.com>
 *
 * $Id$
 */

#define GCACHE_UNIT_TEST
#define GCACHE_PAGE_STORE_UNIT_TEST

#include "GCache.hpp"
#include "gcache_limits.hpp"
#include "gcache_top_test.hpp"

#include <vector>
#include <gu_inttypes.hpp>

using namespace gcache;

static int const DEBUG = 4;

static void
test_caching_fill_page(gcache::GCache&     gc,
                       std::vector<void*>& buf,
                       size_t const        expected_page_count)
{
    const PageStore& ps(gc.page_store());
    for (int i(0); i < 3; i++)
    {
        buf.push_back(gc.malloc(1));
        void* const ptr(buf[buf.size() - 1]);
        ck_assert(nullptr != ptr);
        ck_assert(SEQNO_NONE == ptr2BH(ptr)->seqno_g);
        ck_assert_msg(ps.count() == expected_page_count,
                      "%d. buf = %zu, ps.count() = %zu (expected %zu)",
                      i, buf.size()-1, ps.count(), expected_page_count);
    }
}

START_TEST(top_level_page_caching) // test that caching in pages work
{
    log_info << "\n#\n# top_level_page_caching\n#";
    const char* const dir_name = "";
    size_t const bh_size = sizeof(gcache::BufferHeader);
    size_t const page_size = (8 + bh_size)*3; // fits 3 buffers <= 8 bytes
    size_t const keep_size = 2*page_size;     // keep at least 2 pages

    gu::Config cfg;
    GCache::register_params(cfg);
    cfg.set("gcache.dir", dir_name);
    cfg.set("gcache.size", 0); // turn off ring buffer
    cfg.set("gcache.page_size", page_size);
    cfg.set("gcache.keep_pages_size", keep_size);
#ifndef NDEBUG
    cfg.set("gcache.debug", DEBUG);
#endif

    GCache gc(nullptr, cfg, dir_name);
    const seqno2ptr_t& sm(gc.seqno_map());
    const PageStore&   ps(gc.page_store());
    ck_assert_msg(ps.page_size() == page_size,
                  "ps.page_size: %zu (expected %zu)",
                  ps.page_size(), page_size);

    std::vector<void*> buf;

    mark_point();

    /*
     * 1. Populate 6 pages
     */
    for (size_t page_count(1); page_count <= 6; page_count++)
        test_caching_fill_page(gc, buf, page_count);
    ck_assert_msg(ps.total_pages() == 6,
                  "total_pages %zu (expected 6)", ps.total_pages());

    /*
     * 2. Free some "unused" buffers and assign seqnos out of order to others
     */
    gc.free(buf[0]);
    gc.free(buf[1]);
    gc.free(buf[2]);
    ps.wait_page_discard(); // 1st page should go
    ck_assert_msg(ps.total_pages() == 5,
                  "total_pages %zu (expected 5)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == -1,
                  "seqno_min: %" PRId64 " (expected -1)", gc.seqno_min());

    gc.free(buf[3]);
    gc.seqno_assign(buf[4], 1, 0, false); gc.seqno_release(1);
    ck_assert(gc.seqno_min() == 1);
    gc.free(buf[5]);
    ps.wait_page_discard(); // 2nd page should go
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 4)", ps.total_pages());
    if (!sm.empty())
    {
        ck_assert_msg(sm.empty() == true,
                      "SM size: %zu, begin: %" PRId64 ", end: %" PRId64
                      ", front: %p, back: %p"
                      ,sm.size(), sm.index_begin(), sm.index_end()
                      ,sm.front(), sm.back()
            );
    }
    ck_assert_msg(gc.seqno_min() == -1,
                  "seqno_min: %" PRId64 " (expected -1)", gc.seqno_min());

    gc.free(buf[6]);
    gc.seqno_assign(buf[7], 4, 0, false); // this should pin the page
    gc.free(buf[8]);
    ck_assert_msg(gc.seqno_min() == 4,
                  "seqno_min: %" PRId64 " (expected 4)", gc.seqno_min());
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 4)", ps.total_pages());

    gc.free(buf[9]);
    gc.seqno_assign(buf[11], 2, 0, false); gc.seqno_release(2);
    ck_assert(gc.seqno_min() == 2);
    gc.seqno_assign(buf[10], 3, 0, false); gc.seqno_release(3);
    // page 3 should stay since seqno 4 is not released, and therefore page 4
    ps.wait_page_discard();
    ck_assert(gc.seqno_min() == 2);
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 4)", ps.total_pages());

    gc.seqno_release(4);
    // only after releasing seqno 4 all other buffers and pages 3 and 4 may go
    ps.wait_page_discard();
    ck_assert_msg(ps.total_pages() == 2,
                  "total_pages %zu (expected 2)", ps.total_pages());
    ck_assert(gc.seqno_min() == -1);

    /*
     * 3. Test that the last 2 pages will remain even after freeing
     */
    gc.free(buf[12]);
    gc.seqno_assign(buf[13], 5, 0, false);
    gc.seqno_assign(buf[14], 6, 0, false);
    ck_assert(gc.seqno_min() == 5);

    gc.seqno_assign(buf[15], 7, 0, false);
    gc.seqno_assign(buf[16], 8, 0, false);
    gc.seqno_release(8); // releases all previous seqnos as well
    gc.free(buf[17]);
    ps.wait_page_discard();
    ck_assert(gc.seqno_min() == 5);
    ck_assert_msg(ps.total_pages() == 2,
                  "total_pages %zu (expected 2)", ps.total_pages());

    buf.push_back(gc.malloc(1));
    // this shall allocate one more page and page 5 should go,
    // only 6 and 7 should remain
    ck_assert(ps.count() == 7);
    ps.wait_page_discard();
    ck_assert_msg(ps.total_pages() == 2,
                  "total_pages %zu (expected 2)", ps.total_pages());
    ck_assert(gc.seqno_min() == 7);

    for (size_t i(18); i < buf.size(); i++)
    {
        gc.free(buf[i]);
    }

    mark_point();
}
END_TEST

START_TEST(top_level_page_caching_locking) // test that caching in pages work
{
    log_info << "\n#\n# top_level_page_caching_locking\n#";
    const char* const dir_name = "";
    size_t const bh_size = sizeof(gcache::BufferHeader);
    size_t const page_size = (8 + bh_size)*3; // fits 3 buffers <= 8 bytes
    size_t const keep_size = 2*page_size;     // keep at least 2 pages

    gu::Config cfg;
    GCache::register_params(cfg);
    cfg.set("gcache.dir", dir_name);
    cfg.set("gcache.size", 0); // turn off ring buffer
    cfg.set("gcache.page_size", page_size);
    cfg.set("gcache.keep_pages_size", keep_size);
#ifndef NDEBUG
    cfg.set("gcache.debug", DEBUG);
#endif

    GCache gc(nullptr, cfg, dir_name);
    const PageStore&   ps(gc.page_store());
    ck_assert_msg(ps.page_size() == page_size,
                  "ps.page_size: %zu (expected %zu)",
                  ps.page_size(), page_size);

    std::vector<void*> buf;

    mark_point();

    /*
     * 1. Populate 5 pages
     */
    for (size_t page_count(1); page_count <= 5; page_count++)
        test_caching_fill_page(gc, buf, page_count);
    ck_assert_msg(ps.total_pages() == 5,
                  "total_pages %zu (expected 5)", ps.total_pages());

    /*
     * 2. Assign seqnos
     */
    for (size_t seqno(1); seqno <= buf.size(); seqno++)
        gc.seqno_assign(buf[seqno - 1], seqno, 0, false);
    ck_assert(gc.seqno_min() == 1);

    /*
     * 3. Lock seqno 5, it should hold page 2
     */
    gc.seqno_lock(5);

    gc.seqno_release(1);
    gc.seqno_release(2);
    ps.wait_page_discard();
    ck_assert_msg(ps.total_pages() == 5,
                  "total_pages %zu (expected 5)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 1,
                  "seqno_min: %" PRId64 " (expected 1)", gc.seqno_min());
    gc.seqno_release(3);
    ps.wait_page_discard(); // page 1 should go
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 4)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 4,
                  "seqno_min: %" PRId64 " (expected 4)", gc.seqno_min());
    try {
        gc.seqno_lock(3);
        ck_abort_msg("Should fail.");
    } catch (gu::NotFound&) {}

    gc.seqno_release(6);    // batch-releases all seqnos <= 6
    ps.wait_page_discard(); // page 2 should be held by a lock
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 5)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 4,
                  "seqno_min: %" PRId64 " (expected 4)", gc.seqno_min());

    /*
     * 4. Lock seqno 4 to test that nothing changed
     */
    gc.seqno_lock(4);

    gc.seqno_release(9);
    ps.wait_page_discard(); // page 2 should be held by a lock
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 5)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 4,
                  "seqno_min: %" PRId64 " (expected 4)", gc.seqno_min());

    /* and release page 4 just to see that it doesn't go below because it is
     * within the keep_pages_size */
    gc.seqno_release(12);

    /*
     * 5. Unlock seqnos. Only after the second unlock pages should be discarded
     */
    gc.seqno_unlock();
    ps.wait_page_discard();
    ck_assert_msg(ps.total_pages() == 4,
                  "total_pages %zu (expected 4)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 4,
                  "seqno_min: %" PRId64 " (expected 4)", gc.seqno_min());

    gc.seqno_unlock();
    ps.wait_page_discard(); // pages 2 and 3 should be discarded
    ck_assert_msg(ps.total_pages() == 2,
                  "total_pages %zu (expected 2)", ps.total_pages());
    ck_assert_msg(gc.seqno_min() == 10,
                  "seqno_min: %" PRId64 " (expected 10)", gc.seqno_min());

    for (size_t seqno(10); seqno <= buf.size(); seqno++)
    {
        gc.seqno_release(seqno);
    }

    mark_point();
}
END_TEST

Suite* gcache_top_suite()
{
    Suite* s = suite_create("gcache::top-level");
    TCase* tc;

    tc = tcase_create("test");
    tcase_add_test(tc, top_level_page_caching);
    tcase_add_test(tc, top_level_page_caching_locking);
    suite_add_tcase(s, tc);

    return s;
}