Debian Codegraph @

test/integration/test_fsm.c

raw
1 #include "../../src/client/protocol.h"
2 #include "../../src/command.h"
3 #include "../../src/server.h"
4 #include "../lib/client.h"
5 #include "../lib/heap.h"
6 #include "../lib/runner.h"
7 #include "../lib/server.h"
8 #include "../lib/sqlite.h"
9
10 #ifndef FLAKY
11
12 /******************************************************************************
13 *
14 * Fixture
15 *
16 ******************************************************************************/
17
18 #define N_SERVERS 1
19 #define FIXTURE \
20 struct test_server servers[N_SERVERS]; \
21 struct client_proto *client
22
23 #define SETUP \
24 unsigned i_; \
25 test_heap_setup(params, user_data); \
26 test_sqlite_setup(params); \
27 for (i_ = 0; i_ < N_SERVERS; i_++) { \
28 struct test_server *server = &f->servers[i_]; \
29 test_server_setup(server, i_ + 1, params); \
30 } \
31 test_server_network(f->servers, N_SERVERS); \
32 for (i_ = 0; i_ < N_SERVERS; i_++) { \
33 struct test_server *server = &f->servers[i_]; \
34 test_server_start(server, params); \
35 } \
36 SELECT(1)
37
38 #define TEAR_DOWN \
39 unsigned i_; \
40 for (i_ = 0; i_ < N_SERVERS; i_++) { \
41 test_server_tear_down(&f->servers[i_]); \
42 } \
43 test_sqlite_tear_down(); \
44 test_heap_tear_down(data)
45
46 /******************************************************************************
47 *
48 * Helper macros.
49 *
50 ******************************************************************************/
51
52 /* Use the client connected to the server with the given ID. */
53 #define SELECT(ID) f->client = test_server_client(&f->servers[ID - 1])
54
55 /* Make sure the snapshots scheduled by raft don't interfere with the snapshots
56 * scheduled by the tests. */
57 static char *snapshot_threshold[] = {"8192", NULL};
58 static MunitParameterEnum snapshot_params[] = {
59 {SNAPSHOT_THRESHOLD_PARAM, snapshot_threshold},
60 {NULL, NULL},
61 };
62
63 /******************************************************************************
64 *
65 * snapshot
66 *
67 ******************************************************************************/
68
69 SUITE(fsm)
70
71 struct fixture
72 {
73 FIXTURE;
74 };
75
76 static void *setUp(const MunitParameter params[], void *user_data)
77 {
78 struct fixture *f = munit_malloc(sizeof *f);
79 SETUP;
80 return f;
81 }
82
83 static void tearDown(void *data)
84 {
85 struct fixture *f = data;
86 TEAR_DOWN;
87 free(f);
88 }
89
90 TEST(fsm, snapshotFreshDb, setUp, tearDown, 0, snapshot_params)
91 {
92 struct fixture *f = data;
93 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
94 struct raft_buffer *bufs;
95 unsigned n_bufs = 0;
96 int rv;
97
98 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
99 munit_assert_int(rv, ==, 0);
100 munit_assert_uint(n_bufs, ==, 1); /* Snapshot header */
101
102 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
103 munit_assert_int(rv, ==, 0);
104 munit_assert_ptr_null(bufs);
105 munit_assert_uint(n_bufs, ==, 0);
106 return MUNIT_OK;
107 }
108
109 TEST(fsm, snapshotWrittenDb, setUp, tearDown, 0, snapshot_params)
110 {
111 struct fixture *f = data;
112 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
113 struct raft_buffer *bufs;
114 unsigned n_bufs = 0;
115 int rv;
116
117 uint32_t stmt_id;
118 uint64_t last_insert_id;
119 uint64_t rows_affected;
120
121 /* Add some data to database */
122 HANDSHAKE;
123 OPEN;
124 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
125 EXEC(stmt_id, &last_insert_id, &rows_affected);
126 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
127 EXEC(stmt_id, &last_insert_id, &rows_affected);
128
129 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
130 munit_assert_int(rv, ==, 0);
131 munit_assert_uint(n_bufs, >, 1);
132
133 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
134 munit_assert_int(rv, ==, 0);
135 munit_assert_ptr_null(bufs);
136 munit_assert_uint(n_bufs, ==, 0);
137 return MUNIT_OK;
138 }
139
140 TEST(fsm, snapshotHeapFaultSingleDB, setUp, tearDown, 0, snapshot_params)
141 {
142 struct fixture *f = data;
143 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
144 struct raft_buffer *bufs;
145 unsigned n_bufs = 0;
146 int rv;
147
148 uint32_t stmt_id;
149 uint64_t last_insert_id;
150 uint64_t rows_affected;
151
152 /* Add some data to database */
153 HANDSHAKE;
154 OPEN;
155 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
156 EXEC(stmt_id, &last_insert_id, &rows_affected);
157 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
158 EXEC(stmt_id, &last_insert_id, &rows_affected);
159
160 /* Inject heap faults at different stages of fsm__snapshot */
161 test_heap_fault_config(0, 1);
162 test_heap_fault_enable();
163 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
164 munit_assert_int(rv, !=, 0);
165
166 test_heap_fault_config(1, 1);
167 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
168 munit_assert_int(rv, !=, 0);
169
170 test_heap_fault_config(2, 1);
171 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
172 munit_assert_int(rv, !=, 0);
173
174 return MUNIT_OK;
175 }
176
177 TEST(fsm, snapshotHeapFaultTwoDB, setUp, tearDown, 0, snapshot_params)
178 {
179 struct fixture *f = data;
180 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
181 struct raft_buffer *bufs;
182 unsigned n_bufs = 0;
183 int rv;
184
185 uint32_t stmt_id;
186 uint64_t last_insert_id;
187 uint64_t rows_affected;
188
189 /* Open 2 databases and add data to them */
190 HANDSHAKE;
191 OPEN_NAME("test");
192 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
193 EXEC(stmt_id, &last_insert_id, &rows_affected);
194 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
195 EXEC(stmt_id, &last_insert_id, &rows_affected);
196
197 /* Close and reopen the client and open a second database */
198 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
199
200 HANDSHAKE;
201 OPEN_NAME("test2");
202 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
203 EXEC(stmt_id, &last_insert_id, &rows_affected);
204 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
205 EXEC(stmt_id, &last_insert_id, &rows_affected);
206
207 /* Inject heap faults at different stages of fsm__snapshot */
208 test_heap_fault_config(0, 1);
209 test_heap_fault_enable();
210 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
211 munit_assert_int(rv, !=, 0);
212
213 test_heap_fault_config(1, 1);
214 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
215 munit_assert_int(rv, !=, 0);
216
217 test_heap_fault_config(2, 1);
218 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
219 munit_assert_int(rv, !=, 0);
220
221 test_heap_fault_config(3, 1);
222 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
223 munit_assert_int(rv, !=, 0);
224
225 return MUNIT_OK;
226 }
227
228 TEST(fsm, snapshotNewDbAddedBeforeFinalize, setUp, tearDown, 0, snapshot_params)
229 {
230 struct fixture *f = data;
231 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
232 struct raft_buffer *bufs;
233 unsigned n_bufs = 0;
234 int rv;
235
236 uint32_t stmt_id;
237 uint64_t last_insert_id;
238 uint64_t rows_affected;
239
240 /* Add some data to database */
241 HANDSHAKE;
242 OPEN_NAME("test");
243 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
244 EXEC(stmt_id, &last_insert_id, &rows_affected);
245 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
246 EXEC(stmt_id, &last_insert_id, &rows_affected);
247
248 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
249 munit_assert_int(rv, ==, 0);
250 munit_assert_uint(n_bufs, >, 1);
251
252 /* Close and reopen the client and open a second database,
253 * and ensure finalize succeeds. */
254 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
255
256 HANDSHAKE;
257 OPEN_NAME("test2");
258 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
259 EXEC(stmt_id, &last_insert_id, &rows_affected);
260
261 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
262 EXEC(stmt_id, &last_insert_id, &rows_affected);
263
264 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
265 munit_assert_int(rv, ==, 0);
266 munit_assert_ptr_null(bufs);
267 munit_assert_uint(n_bufs, ==, 0);
268 return MUNIT_OK;
269 }
270
271 TEST(fsm, snapshotWritesBeforeFinalize, setUp, tearDown, 0, snapshot_params)
272 {
273 struct fixture *f = data;
274 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
275 struct raft_buffer *bufs;
276 unsigned n_bufs = 0;
277 uint32_t stmt_id;
278 uint64_t last_insert_id;
279 uint64_t rows_affected;
280 char sql[128];
281 int rv;
282
283 /* Add some data to database */
284 HANDSHAKE;
285 OPEN;
286 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
287 EXEC(stmt_id, &last_insert_id, &rows_affected);
288 PREPARE("INSERT INTO test(n) VALUES(0)", &stmt_id);
289 EXEC(stmt_id, &last_insert_id, &rows_affected);
290
291 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
292 munit_assert_int(rv, ==, 0);
293 munit_assert_uint(n_bufs, >, 1);
294
295 /* Add (a lot) more data to the database */
296 for (unsigned i = 0; i < 1000; ++i) {
297 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1);
298 PREPARE(sql, &stmt_id);
299 EXEC(stmt_id, &last_insert_id, &rows_affected);
300 }
301
302 /* Finalize succeeds */
303 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
304 munit_assert_int(rv, ==, 0);
305 munit_assert_ptr_null(bufs);
306 munit_assert_uint(n_bufs, ==, 0);
307
308 /* Triggers a checkpoint */
309 PREPARE("INSERT INTO test(n) VALUES(1001)", &stmt_id);
310 EXEC(stmt_id, &last_insert_id, &rows_affected);
311
312 return MUNIT_OK;
313 }
314
315 TEST(fsm, concurrentSnapshots, setUp, tearDown, 0, snapshot_params)
316 {
317 struct fixture *f = data;
318 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
319 struct raft_buffer *bufs;
320 struct raft_buffer *bufs2;
321 unsigned n_bufs = 0;
322 unsigned n_bufs2 = 0;
323 uint32_t stmt_id;
324 uint64_t last_insert_id;
325 uint64_t rows_affected;
326 int rv;
327
328 /* Add some data to database */
329 HANDSHAKE;
330 OPEN;
331 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
332 EXEC(stmt_id, &last_insert_id, &rows_affected);
333
334 /* Second snapshot fails when first isn't finalized */
335 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
336 munit_assert_int(rv, ==, 0);
337 rv = fsm->snapshot(fsm, &bufs2, &n_bufs2);
338 munit_assert_int(rv, ==, RAFT_BUSY);
339
340 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
341 munit_assert_int(rv, ==, 0);
342
343 /* Second snapshot succeeds after first is finalized */
344 rv = fsm->snapshot(fsm, &bufs2, &n_bufs2);
345 munit_assert_int(rv, ==, 0);
346
347 rv = fsm->snapshot_finalize(fsm, &bufs2, &n_bufs2);
348 munit_assert_int(rv, ==, 0);
349
350 return MUNIT_OK;
351 }
352
353 /* Copies n raft buffers to a single raft buffer */
354 static struct raft_buffer n_bufs_to_buf(struct raft_buffer bufs[], unsigned n)
355 {
356 uint8_t *cursor;
357 struct raft_buffer buf = {0};
358
359 /* Allocate a suitable buffer */
360 for (unsigned i = 0; i < n; ++i) {
361 buf.len += bufs[i].len;
362 }
363 buf.base = raft_malloc(buf.len);
364 munit_assert_ptr_not_null(buf.base);
365
366 /* Copy all data */
367 cursor = buf.base;
368 for (unsigned i = 0; i < n; ++i) {
369 memcpy(cursor, bufs[i].base, bufs[i].len);
370 cursor += bufs[i].len;
371 }
372 munit_assert_ullong((uintptr_t)(cursor - (uint8_t *)buf.base), ==,
373 buf.len);
374
375 return buf;
376 }
377
378 static char *num_records[] = {
379 "0", "1", "256",
380 /* WAL will just have been checkpointed after 993 writes. */
381 "993",
382 /* Non-empty WAL, checkpointed twice */
383 "2200", NULL};
384
385 static MunitParameterEnum restore_params[] = {
386 {"num_records", num_records},
387 {SNAPSHOT_THRESHOLD_PARAM, snapshot_threshold},
388 {NULL, NULL},
389 };
390
391 TEST(fsm, snapshotRestore, setUp, tearDown, 0, restore_params)
392 {
393 struct fixture *f = data;
394 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
395 struct raft_buffer *bufs;
396 struct raft_buffer snapshot;
397 long n_records =
398 strtol(munit_parameters_get(params, "num_records"), NULL, 0);
399 unsigned n_bufs = 0;
400 uint32_t stmt_id;
401 uint64_t last_insert_id;
402 uint64_t rows_affected;
403 struct rows rows;
404 int rv;
405 char sql[128];
406
407 /* Add some data to database */
408 HANDSHAKE;
409 OPEN;
410 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
411 EXEC(stmt_id, &last_insert_id, &rows_affected);
412 for (int i = 0; i < n_records; ++i) {
413 sprintf(sql, "INSERT INTO test(n) VALUES(%d)", i + 1);
414 PREPARE(sql, &stmt_id);
415 EXEC(stmt_id, &last_insert_id, &rows_affected);
416 }
417
418 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
419 munit_assert_int(rv, ==, 0);
420
421 /* Deep copy snapshot */
422 snapshot = n_bufs_to_buf(bufs, n_bufs);
423
424 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
425 munit_assert_int(rv, ==, 0);
426
427 /* Additionally frees snapshot.base */
428 rv = fsm->restore(fsm, &snapshot);
429 munit_assert_int(rv, ==, 0);
430
431 /* Table is there on fresh connection. */
432 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
433 HANDSHAKE;
434 OPEN;
435 PREPARE("SELECT COUNT(*) from test", &stmt_id);
436 QUERY(stmt_id, &rows);
437 munit_assert_long(rows.next->values->integer, ==, n_records);
438 clientCloseRows(&rows);
439
440 /* Still possible to insert entries */
441 for (int i = 0; i < n_records; ++i) {
442 sprintf(sql, "INSERT INTO test(n) VALUES(%ld)",
443 n_records + i + 1);
444 PREPARE(sql, &stmt_id);
445 EXEC(stmt_id, &last_insert_id, &rows_affected);
446 }
447
448 return MUNIT_OK;
449 }
450
451 TEST(fsm, snapshotRestoreMultipleDBs, setUp, tearDown, 0, snapshot_params)
452 {
453 struct fixture *f = data;
454 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
455 struct raft_buffer *bufs;
456 struct raft_buffer snapshot;
457 unsigned n_bufs = 0;
458 uint32_t stmt_id;
459 uint64_t last_insert_id;
460 uint64_t rows_affected;
461 struct rows rows;
462 uint64_t code;
463 char *msg;
464 int rv;
465
466 /* Create 2 databases and add data to them. */
467 HANDSHAKE;
468 OPEN_NAME("test");
469 PREPARE("CREATE TABLE test (n INT)", &stmt_id);
470 EXEC(stmt_id, &last_insert_id, &rows_affected);
471 PREPARE("INSERT INTO test(n) VALUES(1)", &stmt_id);
472 EXEC(stmt_id, &last_insert_id, &rows_affected);
473
474 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
475 HANDSHAKE;
476 OPEN_NAME("test2");
477 PREPARE("CREATE TABLE test2a (n INT)", &stmt_id);
478 EXEC(stmt_id, &last_insert_id, &rows_affected);
479 PREPARE("INSERT INTO test2a(n) VALUES(1)", &stmt_id);
480 EXEC(stmt_id, &last_insert_id, &rows_affected);
481
482 /* Snapshot both databases and restore the data. */
483 rv = fsm->snapshot(fsm, &bufs, &n_bufs);
484 munit_assert_int(rv, ==, 0);
485
486 /* Copy the snapshot to restore it */
487 snapshot = n_bufs_to_buf(bufs, n_bufs);
488 rv = fsm->snapshot_finalize(fsm, &bufs, &n_bufs);
489 munit_assert_int(rv, ==, 0);
490
491 /* Create a new table in test2 that shouldn't be visible after
492 * restoring the snapshot. */
493 PREPARE("CREATE TABLE test2b (n INT)", &stmt_id);
494 EXEC(stmt_id, &last_insert_id, &rows_affected);
495 PREPARE("INSERT INTO test2b(n) VALUES(1)", &stmt_id);
496 EXEC(stmt_id, &last_insert_id, &rows_affected);
497
498 /* Restore snapshot */
499 rv = fsm->restore(fsm, &snapshot);
500 munit_assert_int(rv, ==, 0);
501
502 /* Reopen connection */
503 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
504 HANDSHAKE;
505 OPEN_NAME("test2");
506
507 /* Table before snapshot is there on second DB */
508 PREPARE("SELECT * from test2a", &stmt_id);
509 QUERY(stmt_id, &rows);
510 clientCloseRows(&rows);
511
512 /* Table after snapshot is not there on second DB */
513 PREPARE_FAIL("SELECT * from test2b", &stmt_id, &code, &msg);
514 munit_assert_uint64(code, ==, COWSQL_ERROR);
515 munit_assert_string_equal(msg, "no such table: test2b");
516 free(msg);
517
518 /* Table is there on first DB */
519 test_server_client_reconnect(&f->servers[0], &f->servers[0].client);
520 HANDSHAKE;
521 OPEN_NAME("test");
522 PREPARE("SELECT * from test", &stmt_id);
523 QUERY(stmt_id, &rows);
524 clientCloseRows(&rows);
525
526 return MUNIT_OK;
527 }
528
529 /******************************************************************************
530 *
531 * apply
532 *
533 ******************************************************************************/
534
535 TEST(fsm, applyFail, setUp, tearDown, 0, NULL)
536 {
537 int rv;
538 struct command_frames c;
539 struct raft_buffer buf;
540 struct fixture *f = data;
541 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
542 void *result = (void *)(uintptr_t)0xDEADBEEF;
543
544 /* Create a frames command without data. */
545 c.filename = "test";
546 c.tx_id = 0;
547 c.truncate = 0;
548 c.is_commit = 0;
549 c.frames.n_pages = 0;
550 c.frames.page_size = 4096;
551 c.frames.data = NULL;
552 rv = command__encode(COMMAND_FRAMES, &c, &buf);
553
554 /* Apply the command and expect it to fail. */
555 rv = fsm->apply(fsm, &buf, &result);
556 munit_assert_int(rv, !=, 0);
557 munit_assert_ptr_null(result);
558
559 raft_free(buf.base);
560 return MUNIT_OK;
561 }
562
563 TEST(fsm, applyUnknownTypeFail, setUp, tearDown, 0, NULL)
564 {
565 int rv;
566 struct command_frames c;
567 struct raft_buffer buf;
568 struct fixture *f = data;
569 struct raft_fsm *fsm = &f->servers[0].cowsql->raft_fsm;
570 void *result = (void *)(uintptr_t)0xDEADBEEF;
571
572 /* Create a frames command without data. */
573 c.filename = "test";
574 c.tx_id = 0;
575 c.truncate = 0;
576 c.is_commit = 0;
577 c.frames.n_pages = 0;
578 c.frames.page_size = 4096;
579 c.frames.data = NULL;
580 rv = command__encode(COMMAND_FRAMES, &c, &buf);
581
582 /* Command type does not exist. */
583 ((uint8_t *)(buf.base))[1] = COMMAND_CHECKPOINT + 8;
584
585 /* Apply the command and expect it to fail. */
586 rv = fsm->apply(fsm, &buf, &result);
587 munit_assert_int(rv, ==, COWSQL_PROTO);
588 munit_assert_ptr_null(result);
589
590 raft_free(buf.base);
591 return MUNIT_OK;
592 }
593
594 #endif /* not FLAKY */