Skip to content
This repository has been archived by the owner on Mar 4, 2024. It is now read-only.

Commit

Permalink
replication: Try to reinstate entries that have been truncated away
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Miller <[email protected]>
  • Loading branch information
cole-miller committed Sep 26, 2023
1 parent b68076f commit 66a2a3e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,41 @@ static int ensureCapacity(struct raft_log *l)
return 0;
}

int logReinstate(struct raft_log *l, raft_term term, unsigned short type)
{
if (l->refs_size == 0) {
return 0;
}

raft_index index = logLastIndex(l) + 1;
size_t key = refsKey(index, l->refs_size);
struct raft_entry_ref *bucket = &l->refs[key];
struct raft_entry_ref *slot;
struct raft_entry *entry;

if (bucket->count == 0) {
return 0;
}

if (bucket->index != index) {
return 0;
}

for (slot = bucket; slot != NULL; slot = slot->next) {
if (slot->term == term) {
slot->count += (unsigned short)1;
l->back += 1;
l->back %= l->size;
entry = &l->entries[l->back];
assert(entry->term == term);
assert(entry->type == type);
return 1;
}
}

return 0;
}

int logAppend(struct raft_log *l,
const raft_term term,
const unsigned short type,
Expand Down
2 changes: 2 additions & 0 deletions src/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ raft_index logSnapshotIndex(struct raft_log *l);
* invoked. Return #NULL if there is no such entry. */
const struct raft_entry *logGet(struct raft_log *l, const raft_index index);

int logReinstate(struct raft_log *l, raft_term term, unsigned short type);

/* Append a new entry to the log. */
int logAppend(struct raft_log *l,
raft_term term,
Expand Down
5 changes: 5 additions & 0 deletions src/replication.c
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,11 @@ int replicationAppend(struct raft *r,
goto err_after_request_alloc;
}

rv = logReinstate(r->log, copy.term, copy.type);
if (rv > 0) {
continue;
}

rv = logAppend(r->log, copy.term, copy.type, &copy.buf, NULL);
if (rv != 0) {
goto err_after_request_alloc;
Expand Down

0 comments on commit 66a2a3e

Please sign in to comment.