Skip to content

Is it safe to clear all entries on apply_snapshot #577

Description

@jorgeantonio21

Recently, I looking into how raft handles applying snapshots to its raft log stream, and upon reading the logic of apply_snapshot in:

https://github.com/tikv/raft-rs/blob/master/src/storage.rs#L238-L260

I realized that the the raft node does indeed clear all its entries: self.entries.clear().

Is this a safe approach ?

From the docs for self.entries:

// entries[i] has raft log position i+snapshot.get_metadata().index
entries: Vec<Entry>,

So I presume self.entries can indeed have entries beyond the snapshot.take_metadata().index which will be effectively lost. In my own use case, where raft log indices correspond to business logic entries, I am worried my system will indeed lose state.

Should we instead drain self.entries until the snapshot metadata index ? Similar to what compact() does ?

let offset = compact_index - entry.index;
self.entries.drain(..offset as usize);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions