Skip to content

Commit

Permalink
Stricter check on changes to the written segment of the freelist
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Mar 28, 2024
1 parent 25c9cf4 commit 52efcd7
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions dependencies/lmdb/libraries/liblmdb/mdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -4163,6 +4163,8 @@ mdb_freelist_save(MDB_txn *txn)
}
ssize_t reserved_space = -1;
ssize_t start_written = -1;
int freelist_written_start = -1;
int freelist_written_end = -1;
ssize_t end_written;
/* MDB_RESERVE cancels meminit in ovpage malloc (when no WRITEMAP) */
clean_limit = (env->me_flags & (MDB_NOMEMINIT|MDB_WRITEMAP))
Expand All @@ -4172,7 +4174,10 @@ mdb_freelist_save(MDB_txn *txn)
mop_len = (mdb_midl_is_empty(mop) ? 0 : mop[0]) + txn->mt_loose_count;
while(reserved_space < mop_len ||
(start_written > env->me_freelist_start && env->me_freelist_start > 0) ||
freecnt < txn->mt_free_pgs[0]) {
freecnt < txn->mt_free_pgs[0] ||
freelist_written_start != env->me_freelist_written_start ||
freelist_written_end != env->me_freelist_written_end
) {
/* Come back here after each Put() in case freelist changed */
/*if (start_written >= 0) {
fprintf(stderr, "Rerunning freelist save, start was %u is %u, length was %u is %u\n",start_written, env->me_freelist_start, reserved_space, mop_len);
Expand Down Expand Up @@ -4231,6 +4236,8 @@ mdb_freelist_save(MDB_txn *txn)
head_id = 1;
start_written = 1;
}
freelist_written_start = env->me_freelist_written_start;
freelist_written_end = env->me_freelist_written_end;
if (mop_len) {
// determine the size of the blocks we need
entry_size = ((mop_len / ((env->me_freelist_end - start_written) * maxfree_1pg)) + 1) * maxfree_1pg;
Expand All @@ -4247,7 +4254,7 @@ mdb_freelist_save(MDB_txn *txn)
char do_write = env->me_freelist_written_start <= end && env->me_freelist_written_end >= start;
fl_writes[i++] = do_write;
// determine if it is in the range of actively written pages
if (env->me_freelist_written_start <= end && env->me_freelist_written_end >= start) {
if (freelist_written_start <= end && freelist_written_end >= start) {
// we will reserve one entry for size and potentially one extra entry in case we are splitting an entry
data.mv_size = (len + (head_id < pglast ? 2 : 1)) * sizeof(pgno_t);
//fprintf(stderr, "id: %u size: %u", head_id, data.mv_size);
Expand Down Expand Up @@ -4326,7 +4333,10 @@ mdb_freelist_save(MDB_txn *txn)
rc = mdb_cursor_get(&mc, &key, &data, MDB_SET_KEY);
if (rc == MDB_NOTFOUND) {
if (mop_len != 0) {
fprintf(stderr, "Freelist record not found %u %u %u %u %u %u %u %u", id, mop_len, start_written, pglast, env->me_freelist_start, env->me_freelist_end, fl_writes[i], fl_writes[i + 1]);
fprintf(stderr, "Freelist record not found %u %u %u %u %u %u %i %i %i %i %i %u %u\n", id, mop_len, start_written,
pglast, env->me_freelist_start, env->me_freelist_end,
freelist_written_start, env->me_freelist_written_start, freelist_written_end, env->me_freelist_written_end,
i, fl_writes[0], fl_writes[1]);
mdb_tassert(txn, mop_len == 0);
}
rc = 0; // this is acceptable as long as there are no entries to write
Expand Down

0 comments on commit 52efcd7

Please sign in to comment.