feat: implement raft snapshot catchup
This commit is contained in:
@@ -5,6 +5,7 @@ public sealed class RaftNode
|
||||
private int _votesReceived;
|
||||
private readonly List<RaftNode> _cluster = [];
|
||||
private readonly RaftReplicator _replicator = new();
|
||||
private readonly RaftSnapshotStore _snapshotStore = new();
|
||||
|
||||
public string Id { get; }
|
||||
public int Term => TermState.CurrentTerm;
|
||||
@@ -77,6 +78,24 @@ public sealed class RaftNode
|
||||
Log.AppendReplicated(entry);
|
||||
}
|
||||
|
||||
public async Task<RaftSnapshot> CreateSnapshotAsync(CancellationToken ct)
|
||||
{
|
||||
var snapshot = new RaftSnapshot
|
||||
{
|
||||
LastIncludedIndex = AppliedIndex,
|
||||
LastIncludedTerm = Term,
|
||||
};
|
||||
await _snapshotStore.SaveAsync(snapshot, ct);
|
||||
return snapshot;
|
||||
}
|
||||
|
||||
public Task InstallSnapshotAsync(RaftSnapshot snapshot, CancellationToken ct)
|
||||
{
|
||||
Log.ReplaceWithSnapshot(snapshot);
|
||||
AppliedIndex = snapshot.LastIncludedIndex;
|
||||
return _snapshotStore.SaveAsync(snapshot, ct);
|
||||
}
|
||||
|
||||
public void RequestStepDown()
|
||||
{
|
||||
Role = RaftRole.Follower;
|
||||
|
||||
Reference in New Issue
Block a user