feat: implement raft log replication and apply
This commit is contained in:
25
src/NATS.Server/Raft/RaftLog.cs
Normal file
25
src/NATS.Server/Raft/RaftLog.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace NATS.Server.Raft;
|
||||
|
||||
public sealed class RaftLog
|
||||
{
|
||||
private readonly List<RaftLogEntry> _entries = [];
|
||||
|
||||
public IReadOnlyList<RaftLogEntry> Entries => _entries;
|
||||
|
||||
public RaftLogEntry Append(int term, string command)
|
||||
{
|
||||
var entry = new RaftLogEntry(_entries.Count + 1, term, command);
|
||||
_entries.Add(entry);
|
||||
return entry;
|
||||
}
|
||||
|
||||
public void AppendReplicated(RaftLogEntry entry)
|
||||
{
|
||||
if (_entries.Any(e => e.Index == entry.Index))
|
||||
return;
|
||||
|
||||
_entries.Add(entry);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record RaftLogEntry(long Index, int Term, string Command);
|
||||
Reference in New Issue
Block a user