Skip to content

Commit

Permalink
Keep enough entries for the previous snapshot
Browse files Browse the repository at this point in the history
This commit fixes the logic so that the computed trailing does not
remove too many entries in the log which would make the previous
snapshot unusable.
  • Loading branch information
marco6 committed Feb 5, 2025
1 parent 56abe80 commit 508db24
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/raft/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -1678,31 +1678,35 @@ static void takeSnapshotClose(struct raft *r, struct raft_snapshot *s)

static unsigned dynamicTrailingIndex(struct raft *r, struct raft_snapshot *snapshot) {
struct raft_log *l = r->log;
unsigned threshold = (unsigned)(l->front - l->back);
size_t size = 0;
size_t snapshot_size = 0;
unsigned trailing = 0;
unsigned i;

Check warning on line 1683 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1679-L1683

Added lines #L1679 - L1683 were not covered by tests

/**
* This should never happen as it would mean that the snapshot
* contains no new entry (i.e. either the snapshot is empty
* or equal to the previous one).
*/
assert(threshold > 0);
threshold = min(r->snapshot.threshold, threshold);
assert(l->front > l->back);

Check warning on line 1689 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1689

Added line #L1689 was not covered by tests

for (i = 0; i < snapshot->n_bufs; i++) {
size += snapshot->bufs[i].len;
snapshot_size += snapshot->bufs[i].len;

Check warning on line 1692 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1692

Added line #L1692 was not covered by tests
}

for (i = 1; i <= threshold; i++) {
struct raft_entry* entry = &(l->entries[l->back - i]);
if (entry->buf.len > size) {
return i;
for (trailing = 1; l->back - trailing >= l->front; i++) {
struct raft_entry* entry = &(l->entries[l->back - trailing]);

Check warning on line 1696 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1696

Added line #L1696 was not covered by tests
if (entry->buf.len > snapshot_size) {
break;
}
size -= entry->buf.len;
snapshot_size -= entry->buf.len;

Check warning on line 1700 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1700

Added line #L1700 was not covered by tests
}

if (trailing < r->snapshot.threshold) {
trailing = r->snapshot.threshold;
} else if (trailing > r->snapshot.trailing) {
trailing = r->snapshot.trailing;

Check warning on line 1706 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1706

Added line #L1706 was not covered by tests
}

return threshold;
return trailing;

Check warning on line 1709 in src/raft/replication.c

View check run for this annotation

Codecov / codecov/patch

src/raft/replication.c#L1709

Added line #L1709 was not covered by tests
}


Expand Down

0 comments on commit 508db24

Please # to comment.