Debian Codegraph @

test/unit/test_replication.c

raw
1 #include "../lib/cluster.h"
2 #include "../lib/runner.h"
3
4 #include "../../src/format.h"
5 #include "../../src/leader.h"
6
7 TEST_MODULE(replication_v1);
8
9 /******************************************************************************
10 *
11 * Fixture
12 *
13 ******************************************************************************/
14
15 #define FIXTURE \
16 FIXTURE_CLUSTER; \
17 struct leader leaders[N_SERVERS]; \
18 sqlite3_stmt *stmt;
19
20 #define SETUP \
21 unsigned i; \
22 SETUP_CLUSTER(V2) \
23 for (i = 0; i < N_SERVERS; i++) { \
24 SETUP_LEADER(i); \
25 }
26
27 #define SETUP_LEADER(I) \
28 do { \
29 struct leader *leader = &f->leaders[I]; \
30 struct registry *registry = CLUSTER_REGISTRY(I); \
31 struct db *db; \
32 int rc2; \
33 rc2 = registry__db_get(registry, "test.db", &db); \
34 munit_assert_int(rc2, ==, 0); \
35 rc2 = leader__init(leader, db, CLUSTER_RAFT(I)); \
36 munit_assert_int(rc2, ==, 0); \
37 } while (0)
38
39 #define TEAR_DOWN \
40 unsigned i; \
41 for (i = 0; i < N_SERVERS; i++) { \
42 TEAR_DOWN_LEADER(i); \
43 } \
44 TEAR_DOWN_CLUSTER
45
46 #define TEAR_DOWN_LEADER(I) \
47 do { \
48 struct leader *leader = &f->leaders[I]; \
49 leader__close(leader); \
50 } while (0)
51
52 /******************************************************************************
53 *
54 * Helper macros.
55 *
56 ******************************************************************************/
57
58 /* Return the i'th leader object. */
59 #define LEADER(I) &f->leaders[I]
60
61 /* Return the SQLite connection of the i'th leader object */
62 #define CONN(I) (LEADER(I))->conn
63
64 /* Prepare the fixture's statement using the connection of the I'th leader */
65 #define PREPARE(I, SQL) \
66 { \
67 int rc2; \
68 rc2 = sqlite3_prepare_v2(CONN(I), SQL, -1, &f->stmt, NULL); \
69 munit_assert_int(rc2, ==, 0); \
70 }
71
72 /* Reset the fixture's statement, expecting the given return code. */
73 #define RESET(RC) \
74 { \
75 int rc2; \
76 rc2 = sqlite3_reset(f->stmt); \
77 munit_assert_int(rc2, ==, RC); \
78 }
79
80 /* Finalize the fixture's statement */
81 #define FINALIZE \
82 { \
83 int rc2; \
84 rc2 = sqlite3_finalize(f->stmt); \
85 munit_assert_int(rc2, ==, 0); \
86 }
87
88 /* Submit an exec request using the I'th leader. */
89 #define EXEC(I) \
90 { \
91 int rc2; \
92 rc2 = leader__exec(LEADER(I), &f->req, f->stmt, 0, \
93 fixture_exec_cb); \
94 munit_assert_int(rc2, ==, 0); \
95 }
96
97 /* Convenience to prepare, execute and finalize a statement. */
98 #define EXEC_SQL(I, SQL) \
99 PREPARE(I, SQL); \
100 EXEC(I); \
101 CLUSTER_APPLIED(CLUSTER_LAST_INDEX(I)); \
102 FINALIZE
103
104 /******************************************************************************
105 *
106 * Helper macros.
107 *
108 ******************************************************************************/
109
110 /* Assert the number of pages in the WAL file on the I'th node. */
111 #define ASSERT_WAL_PAGES(I, N) \
112 { \
113 struct leader *leader_ = &f->leaders[I]; \
114 sqlite3_file *file_; \
115 sqlite_int64 size_; \
116 int pages_; \
117 int rv_; \
118 rv_ = sqlite3_file_control(leader_->conn, "main", \
119 SQLITE_FCNTL_JOURNAL_POINTER, \
120 &file_); \
121 munit_assert_int(rv_, ==, 0); \
122 rv_ = file_->pMethods->xFileSize(file_, &size_); \
123 munit_assert_int(rv_, ==, 0); \
124 pages_ = formatWalCalcFramesNumber( \
125 leader_->db->config->page_size, size_); \
126 munit_assert_int(pages_, ==, N); \
127 }
128
129 /******************************************************************************
130 *
131 * leader__init
132 *
133 ******************************************************************************/
134
135 struct init_fixture
136 {
137 FIXTURE;
138 };
139
140 TEST_SUITE(init);
141 TEST_SETUP(init)
142 {
143 struct init_fixture *f = munit_malloc(sizeof *f);
144 SETUP;
145 return f;
146 }
147 TEST_TEAR_DOWN(init)
148 {
149 struct init_fixture *f = data;
150 TEAR_DOWN;
151 free(f);
152 }
153
154 /* The connection is open and can be used. */
155 TEST_CASE(init, conn, NULL)
156 {
157 struct init_fixture *f = data;
158 sqlite3_stmt *stmt;
159 int rc;
160 (void)params;
161 rc = sqlite3_prepare_v2(CONN(0), "SELECT 1", -1, &stmt, NULL);
162 munit_assert_int(rc, ==, 0);
163 sqlite3_finalize(stmt);
164 return MUNIT_OK;
165 }
166
167 /******************************************************************************
168 *
169 * leader__exec
170 *
171 ******************************************************************************/
172
173 struct exec_fixture
174 {
175 FIXTURE;
176 struct exec req;
177 bool invoked;
178 int status;
179 };
180
181 static void fixture_exec_cb(struct exec *req, int status)
182 {
183 struct exec_fixture *f = req->data;
184 f->invoked = true;
185 f->status = status;
186 }
187
188 TEST_SUITE(exec);
189 TEST_SETUP(exec)
190 {
191 struct exec_fixture *f = munit_malloc(sizeof *f);
192 SETUP;
193 f->req.data = f;
194 return f;
195 }
196 TEST_TEAR_DOWN(exec)
197 {
198 struct exec_fixture *f = data;
199 TEAR_DOWN;
200 free(f);
201 }
202
203 TEST_CASE(exec, success, NULL)
204 {
205 struct exec_fixture *f = data;
206 (void)params;
207 CLUSTER_ELECT(0);
208 PREPARE(0, "CREATE TABLE test (a INT)");
209 EXEC(0);
210 CLUSTER_APPLIED(4);
211 munit_assert_true(f->invoked);
212 munit_assert_int(f->status, ==, SQLITE_DONE);
213 FINALIZE;
214 return MUNIT_OK;
215 }
216
217 /* A snapshot is taken after applying an entry. */
218 TEST_CASE(exec, snapshot, NULL)
219 {
220 struct exec_fixture *f = data;
221 (void)params;
222 CLUSTER_SNAPSHOT_THRESHOLD(0, 4);
223 CLUSTER_ELECT(0);
224 PREPARE(0, "CREATE TABLE test (n INT)");
225 EXEC(0);
226 CLUSTER_APPLIED(4);
227 FINALIZE;
228 PREPARE(0, "INSERT INTO test(n) VALUES(1)");
229 EXEC(0);
230 CLUSTER_APPLIED(5);
231 munit_assert_true(f->invoked);
232 munit_assert_int(f->status, ==, SQLITE_DONE);
233 FINALIZE;
234 return MUNIT_OK;
235 }
236
237 /* If a transaction is in progress, no snapshot is taken. */
238 TEST_CASE(exec, snapshot_busy, NULL)
239 {
240 struct exec_fixture *f = data;
241 (void)params;
242 unsigned i;
243 CLUSTER_SNAPSHOT_THRESHOLD(0, 4);
244 CLUSTER_ELECT(0);
245 EXEC_SQL(0, "PRAGMA cache_size = 1");
246 EXEC_SQL(0, "CREATE TABLE test (n INT)");
247 EXEC_SQL(0, "BEGIN");
248 /* Accumulate enough dirty data to fill the page cache */
249 for (i = 0; i < 163; i++) {
250 EXEC_SQL(0, "INSERT INTO test(n) VALUES(1)");
251 }
252 return MUNIT_OK;
253 }
254
255 /* If the WAL size grows beyond the configured threshold, checkpoint it. */
256 TEST_CASE(exec, checkpoint, NULL)
257 {
258 struct exec_fixture *f = data;
259 struct config *config = CLUSTER_CONFIG(0);
260 (void)params;
261 config->checkpoint_threshold = 3;
262 CLUSTER_ELECT(0);
263 EXEC_SQL(0, "CREATE TABLE test (n INT)");
264 EXEC_SQL(0, "INSERT INTO test(n) VALUES(1)");
265 /* The WAL was truncated. */
266 ASSERT_WAL_PAGES(0, 0);
267 return MUNIT_OK;
268 }
269
270 /* If a read transaction is in progress, no checkpoint is taken. */
271 TEST_CASE(exec, checkpoint_read_lock, NULL)
272 {
273 struct exec_fixture *f = data;
274 struct config *config = CLUSTER_CONFIG(0);
275 struct registry *registry = CLUSTER_REGISTRY(0);
276 struct db *db;
277 struct leader leader2;
278 char *errmsg;
279 int rv;
280 (void)params;
281 config->checkpoint_threshold = 3;
282
283 CLUSTER_ELECT(0);
284 EXEC_SQL(0, "CREATE TABLE test (n INT)");
285
286 /* Initialize another leader. */
287 rv = registry__db_get(registry, "test.db", &db);
288 munit_assert_int(rv, ==, 0);
289 leader__init(&leader2, db, CLUSTER_RAFT(0));
290
291 /* Start a read transaction in the other leader. */
292 rv = sqlite3_exec(leader2.conn, "BEGIN", NULL, NULL, &errmsg);
293 munit_assert_int(rv, ==, 0);
294
295 rv = sqlite3_exec(leader2.conn, "SELECT * FROM test", NULL, NULL,
296 &errmsg);
297 munit_assert_int(rv, ==, 0);
298
299 EXEC_SQL(0, "INSERT INTO test(n) VALUES(1)");
300
301 /* The WAL was not truncated. */
302 ASSERT_WAL_PAGES(0, 3);
303
304 leader__close(&leader2);
305
306 return MUNIT_OK;
307 }
308
309 /******************************************************************************
310 *
311 * Fixture
312 *
313 ******************************************************************************/
314
315 struct fixture
316 {
317 FIXTURE_CLUSTER;
318 struct leader leaders[N_SERVERS];
319 sqlite3_stmt *stmt;
320 struct exec req;
321 bool invoked;
322 int status;
323 };
324
325 static void *setUp(const MunitParameter params[], void *user_data)
326 {
327 struct fixture *f = munit_malloc(sizeof *f);
328 SETUP_CLUSTER(V2);
329 SETUP_LEADER(0);
330 f->req.data = f;
331 return f;
332 }
333
334 static void tearDown(void *data)
335 {
336 struct fixture *f = data;
337 TEAR_DOWN_LEADER(0);
338 TEAR_DOWN_CLUSTER;
339 free(f);
340 }
341
342 SUITE(replication)
343
344 static void execCb(struct exec *req, int status)
345 {
346 struct fixture *f = req->data;
347 f->invoked = true;
348 f->status = status;
349 }
350
351 TEST(replication, exec, setUp, tearDown, 0, NULL)
352 {
353 struct fixture *f = data;
354 int rv;
355
356 CLUSTER_ELECT(0);
357
358 PREPARE(0, "BEGIN");
359 rv = leader__exec(LEADER(0), &f->req, f->stmt, 0, execCb);
360 CLUSTER_APPLIED(3);
361 munit_assert_int(rv, ==, 0);
362 munit_assert_true(f->invoked);
363 munit_assert_int(f->status, ==, SQLITE_DONE);
364 f->invoked = false;
365 FINALIZE;
366
367 PREPARE(0, "CREATE TABLE test (a INT)");
368 rv = leader__exec(LEADER(0), &f->req, f->stmt, 0, execCb);
369 munit_assert_int(rv, ==, 0);
370 munit_assert_true(f->invoked);
371 munit_assert_int(f->status, ==, SQLITE_DONE);
372 f->invoked = false;
373 FINALIZE;
374
375 PREPARE(0, "COMMIT");
376 rv = leader__exec(LEADER(0), &f->req, f->stmt, 0, execCb);
377 munit_assert_int(rv, ==, 0);
378 munit_assert_false(f->invoked);
379 FINALIZE;
380
381 CLUSTER_APPLIED(4);
382
383 munit_assert_true(f->invoked);
384 munit_assert_int(f->status, ==, SQLITE_DONE);
385
386 PREPARE(0, "SELECT * FROM test");
387 FINALIZE;
388
389 SETUP_LEADER(1);
390 PREPARE(1, "SELECT * FROM test");
391 FINALIZE;
392 TEAR_DOWN_LEADER(1);
393
394 return MUNIT_OK;
395 }
396
397 /* If the WAL size grows beyond the configured threshold, checkpoint it. */
398 TEST(replication, checkpoint, setUp, tearDown, 0, NULL)
399 {
400 struct fixture *f = data;
401 struct config *config = CLUSTER_CONFIG(0);
402 int rv;
403
404 config->checkpoint_threshold = 3;
405
406 CLUSTER_ELECT(0);
407
408 PREPARE(0, "CREATE TABLE test (n INT)");
409 rv = leader__exec(LEADER(0), &f->req, f->stmt, 0, execCb);
410 munit_assert_int(rv, ==, 0);
411 CLUSTER_APPLIED(4);
412 FINALIZE;
413
414 PREPARE(0, "INSERT INTO test(n) VALUES(1)");
415 rv = leader__exec(LEADER(0), &f->req, f->stmt, 0, execCb);
416 munit_assert_int(rv, ==, 0);
417 CLUSTER_APPLIED(6);
418 FINALIZE;
419
420 /* The WAL was truncated. */
421 ASSERT_WAL_PAGES(0, 0);
422
423 return MUNIT_OK;
424 }