Skip to content

Commit 36153db

Browse files
committed
addressing review
1 parent 0b3dd36 commit 36153db

File tree

4 files changed

+26
-74
lines changed

4 files changed

+26
-74
lines changed

.github/scripts/generate-snapshot.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ USAGE
1717
exit 0
1818
}
1919

20-
# Parse arguments
20+
# Parse arguments.
2121
while [ $# -gt 0 ]; do
2222
case "$1" in
2323
--main-only)
@@ -59,23 +59,23 @@ if [ -z "$OUTPUT_DIR" ]; then
5959
usage
6060
fi
6161

62-
# Cleanup
62+
# Cleanup.
6363
trap 'rm -f temp temp-wal temp-shm' EXIT
6464

6565
SQLITE3_CHECKPOINT_ON_CLOSE=""
6666
if [ "$MAIN_ONLY" -eq 0 ]; then
6767
SQLITE3_CHECKPOINT_ON_CLOSE=".dbconfig no_ckpt_on_close on"
6868
fi
6969

70-
# First generate a sqlite3 database
70+
# First generate a sqlite3 database.
7171
echo -n "Generating database with $ROW_COUNT rows..."
7272
cat <<EOF | sqlite3 temp > /dev/null 2>&1
7373
PRAGMA journal_mode=WAL;
7474
PRAGMA wal_autocheckpoint=$WAL_AUTOCHECKPOINT;
7575
$SQLITE3_CHECKPOINT_ON_CLOSE
7676
7777
CREATE TABLE test(id INTEGER PRIMARY KEY, value TEXT NOT NULL);
78-
WITH sequence AS (
78+
WITH RECURSIVE sequence AS (
7979
SELECT 1 AS id
8080
8181
UNION ALL
@@ -87,12 +87,10 @@ WITH sequence AS (
8787
INSERT OR REPLACE INTO test
8888
SELECT id, hex(randomblob(16))
8989
FROM sequence;
90-
9190
EOF
9291
echo "Done"
9392

94-
95-
# Now generate a dqlite snapshot from the sqlite3 database
93+
# Now generate a dqlite snapshot from the sqlite3 database.
9694
echo -n "Generating snapshot in '$OUTPUT_DIR'..."
9795
cat <<EOF | dqlite-utils > /dev/null 2>&1
9896
.snapshot

src/fsm.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,12 @@ static int decodeDatabase(const struct registry *r,
202202
const size_t page_size = r->config->vfs.page_size;
203203
dqlite_assert((header.main_size % page_size) == 0);
204204

205-
size_t page_count = (size_t)header.main_size / page_size;
206-
void **pages = raft_malloc(sizeof(void *) * page_count);
205+
size_t main_page_count = (size_t)header.main_size / page_size;
206+
void **pages = raft_malloc(sizeof(void *) * main_page_count);
207207
if (pages == NULL) {
208208
return RAFT_NOMEM;
209209
}
210-
for (size_t i = 0; i < page_count; i++) {
210+
for (size_t i = 0; i < main_page_count; i++) {
211211
pages[i] = (void *)(cursor->p + i * page_size);
212212
}
213213
cursor->p += header.main_size;
@@ -225,15 +225,18 @@ static int decodeDatabase(const struct registry *r,
225225
const unsigned n_frames =
226226
(unsigned)((header.wal_size - (size_t)wal_header_size) /
227227
wal_frame_size);
228-
const char *wal = cursor->p;
229-
const void *last_frame =
230-
(const uint8_t *)wal + wal_header_size +
228+
const uint8_t *wal = (uint8_t*)cursor->p;
229+
// In dqlite, WAL is always well-formed (id doesn't contain
230+
// partial transactions). As such, the last frame is always a
231+
// commit frame and must contain the size of the database after
232+
// the transaction.
233+
const uint8_t *last_frame =
234+
wal + wal_header_size +
231235
n_frames * wal_frame_size - wal_frame_size;
232-
233236
const size_t wal_page_count = ByteGetBe32(last_frame + 4);
234237
dqlite_assert(wal_page_count != 0);
235238

236-
if (wal_page_count > page_count) {
239+
if (wal_page_count > main_page_count) {
237240
void *wal_pages = raft_realloc(
238241
pages, sizeof(void *) * wal_page_count);
239242
if (wal_pages == NULL) {
@@ -244,10 +247,10 @@ static int decodeDatabase(const struct registry *r,
244247
}
245248

246249
/* Read pages in the WAL order */
247-
for (const char *frame = wal + wal_header_size;
250+
for (const uint8_t *frame = wal + wal_header_size;
248251
frame < wal + header.wal_size; frame += wal_frame_size) {
249252
uint32_t page_number =
250-
ByteGetBe32((const uint8_t *)frame);
253+
ByteGetBe32(frame);
251254
if (page_number > wal_page_count) {
252255
continue;
253256
}
@@ -258,7 +261,7 @@ static int decodeDatabase(const struct registry *r,
258261

259262
/* Verify that if the WAL resized the database, then no page is
260263
* missing. */
261-
for (size_t page_number = page_count;
264+
for (size_t page_number = main_page_count;
262265
page_number <= wal_page_count; page_number++) {
263266
if (pages[page_number - 1] == NULL) {
264267
tracef("missing page %" PRIu64 " in wal",
@@ -267,12 +270,12 @@ static int decodeDatabase(const struct registry *r,
267270
return RAFT_INVALID;
268271
}
269272
}
270-
page_count = wal_page_count;
273+
main_page_count = wal_page_count;
271274
}
272275
cursor->p += header.wal_size;
273276

274277
*snapshot = (struct vfsSnapshot){
275-
.page_count = page_count,
278+
.page_count = main_page_count,
276279
.page_size = page_size,
277280
.pages = pages,
278281
};

test/integration/test_node.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -268,24 +268,13 @@ __attribute__((constructor)) static void snapshot_dirs_env_init(void)
268268
}
269269
}
270270

271-
// Make the sanitizer happy
271+
// Make the sanitizer happy.
272272
__attribute__((destructor)) static void snapshot_dirs_env_uninit(void)
273273
{
274274
free(snapshot_dir_env);
275275
snapshot_dir_env = NULL;
276276
}
277277

278-
279-
RUNNER_ARGUMENT(snapshot, "directory where to find a test snapshot") {
280-
if (NEXT_ARG == NULL) {
281-
printf("Error: --snapshot-dirs requires an argument\n");
282-
return false;
283-
}
284-
SHIFT;
285-
snapshot_dirs[snapshot_dirs_n++] = CURRENT_ARG;
286-
return true;
287-
}
288-
289278
static void *setUpExistingSnapshot(const MunitParameter params[], void *user_data)
290279
{
291280
return setUp(params, user_data, munit_parameters_get(params, SNAPSHOT_DIR_PARAM), "127.0.0.1:9001");
@@ -303,7 +292,7 @@ TEST(node, existing_snapshot, setUpExistingSnapshot, tearDownKeepDir, 0, test_sn
303292
rv = dqlite_node_start(f->node);
304293
munit_assert_int(rv, ==, 0);
305294

306-
/* Open a bunch of clients and do a query on them */
295+
/* Open a client and do a query on them */
307296
struct client_proto client;
308297
struct rows rows;
309298

test/lib/runner.h

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
extern MunitSuite _main_suites[];
2020
extern int _main_suites_n;
2121

22-
extern MunitArgument _main_args[];
23-
extern int _main_args_n;
24-
2522
/* Maximum number of test cases for each suite */
2623
#define SUITE__CAP 128
2724
#define TEST__CAP SUITE__CAP
@@ -93,14 +90,12 @@ extern int _main_args_n;
9390
} while (0)
9491

9592
#else
96-
#error "backtrace enabled, but no library to support it"
93+
# error "backtrace enabled, but no library to support it"
9794
#endif /* HAVE_EXECINFO_H */
9895

9996
#else
10097

101-
#define PRINT_BACKTRACE(SKIP) \
102-
do { \
103-
} while (0)
98+
#define PRINT_BACKTRACE(SKIP) do {} while (0)
10499

105100
#endif /* DQLITE_ASSERT_WITH_BACKTRACE */
106101

@@ -133,48 +128,15 @@ extern int _main_args_n;
133128
MunitSuite _main_suites[SUITE__CAP]; \
134129
int _main_suites_n = 0; \
135130
\
136-
MunitArgument _main_args[SUITE__CAP] = {}; \
137-
int _main_args_n = 0; \
138-
\
139131
int main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc)]) \
140132
{ \
141133
signal(SIGPIPE, SIG_IGN); \
142134
signal(SIGABRT, print_backtrace); \
143135
dqliteTracingMaybeEnable(true); \
144136
MunitSuite suite = { (char *)"", NULL, _main_suites, 1, 0 }; \
145-
return munit_suite_main_custom(&suite, (void *)NAME, argc, \
146-
argv, _main_args); \
137+
return munit_suite_main(&suite, (void *)NAME, argc, argv); \
147138
}
148139

149-
/* Declare and register a new argument. */
150-
#define RUNNER_ARGUMENT(NAME, HELP_STR) \
151-
static bool argument_##NAME##_parse( \
152-
const MunitSuite *suite, void *user_data, int *arg, int argc, \
153-
char *const argv[MUNIT_ARRAY_PARAM(argc)]); \
154-
static void argument_##NAME##_help(const MunitArgument *argument, \
155-
void *user_data) \
156-
{ \
157-
(void)user_data; \
158-
printf("--%s: %s\n", argument->name, HELP_STR); \
159-
} \
160-
__attribute__((constructor)) static void argument_##NAME##_init(void) \
161-
{ \
162-
int n = _main_args_n; \
163-
_main_args[n].name = #NAME; \
164-
_main_args[n].parse_argument = argument_##NAME##_parse; \
165-
_main_args[n].write_help = argument_##NAME##_help; \
166-
_main_args_n = n + 1; \
167-
} \
168-
static bool argument_##NAME##_parse( \
169-
MUNIT_UNUSED const MunitSuite *suite, \
170-
MUNIT_UNUSED void *user_data, MUNIT_UNUSED int *arg, \
171-
MUNIT_UNUSED int argc, \
172-
MUNIT_UNUSED char *const argv[MUNIT_ARRAY_PARAM(argc)])
173-
174-
#define CURRENT_ARG (argv[*arg])
175-
#define NEXT_ARG (argc > *arg + 1 ? argv[*arg + 1] : NULL)
176-
#define SHIFT (*arg)++
177-
178140
/* Declare and register a new test suite #S belonging to the file's test module.
179141
*
180142
* A test suite is a pair of static variables:

0 commit comments

Comments
 (0)