-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
- Loading branch information
Showing
52 changed files
with
414 additions
and
393 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using SlimCluster; | ||
|
||
public abstract class AbstractStatus : IStatus | ||
{ | ||
public Guid Id { get; } | ||
|
||
public string Name { get; } | ||
|
||
protected AbstractStatus(Guid id, string name) | ||
{ | ||
Id = id; | ||
Name = name; | ||
} | ||
|
||
#region equals | ||
|
||
public override bool Equals(object? obj) | ||
=> obj is AbstractStatus state && Id.Equals(state.Id); | ||
|
||
public override int GetHashCode() | ||
=> HashCode.Combine(Id); | ||
|
||
#endregion | ||
|
||
public override string ToString() | ||
=> Name; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
public class LogEntry | ||
{ | ||
public int Index { get; set; } | ||
public int Term { get; set; } | ||
/// <summary> | ||
/// Command for the <see cref="IStateMachine"/>. | ||
/// </summary> | ||
public object Command { get; set; } | ||
|
||
public LogEntry(int index, int term, object command) | ||
{ | ||
Index = index; | ||
Term = term; | ||
Command = command; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
public class RaftCandidateState | ||
{ | ||
public int Term { get; set; } | ||
public ISet<string> RecivedVotesFrom { get; set; } | ||
|
||
public RaftCandidateState(int term) | ||
{ | ||
Term = term; | ||
RecivedVotesFrom = new HashSet<string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
public class RaftCluster : ICluster | ||
{ | ||
public string ClusterId { get; protected set; } | ||
|
||
public IReadOnlyCollection<INode> Members { get; protected set; } | ||
|
||
public INode? LeaderNode { get; protected set; } | ||
|
||
public IClusterStatus Status { get; protected set; } | ||
|
||
public RaftCluster(string clusterId) | ||
{ | ||
ClusterId = clusterId; | ||
Members = new List<INode>(); | ||
LeaderNode = null; | ||
Status = RaftClusterStatus.Initializing; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using SlimCluster; | ||
|
||
public class RaftClusterStatus : AbstractStatus, IClusterStatus | ||
{ | ||
protected RaftClusterStatus(Guid id, string name) : base(id, name) | ||
{ | ||
} | ||
|
||
public static readonly RaftClusterStatus Initializing = new (new Guid("{8939D9A3-7B4F-4B6D-9248-F8086346C1F7}"), "Initializing"); | ||
public static readonly RaftClusterStatus Working = new (new Guid("{87A7A4EE-1B22-4BAE-9021-FB56698BA6C8}"), "Working"); | ||
public static readonly RaftClusterStatus Waiting = new (new Guid("{A5DED565-8580-4ED8-BB73-E1F102127322}"), "Waiting"); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
public class RaftLeaderState | ||
{ | ||
public IDictionary<string, int> NextIndex { get; protected set; } | ||
public IDictionary<string, int> MatchIndex { get; protected set; } | ||
|
||
public RaftLeaderState() | ||
{ | ||
NextIndex = new Dictionary<string, int>(); | ||
MatchIndex = new Dictionary<string, int>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using SlimCluster.Membership; | ||
|
||
public class RaftNode | ||
{ | ||
private readonly IClusterMembership clusterMembership; | ||
|
||
#region persistent state | ||
|
||
public int CurrentTerm { get; protected set; } | ||
public string? VotedFor { get; protected set; } | ||
public IList<LogEntry> Log { get; protected set; } | ||
public Snapshot? Snapshot { get; protected set; } | ||
|
||
#endregion | ||
|
||
#region volatile state | ||
|
||
public int CommitIndex { get; protected set; } | ||
public int LastApplied { get; protected set; } | ||
|
||
#endregion | ||
|
||
public RaftLeaderState? LeaderState { get; protected set; } | ||
public RaftCandidateState? CandidateState { get; protected set; } | ||
|
||
public RaftNode(IClusterMembership clusterMembership) | ||
{ | ||
this.clusterMembership = clusterMembership; | ||
|
||
CurrentTerm = 0; | ||
VotedFor = null; | ||
Log = new List<LogEntry>(); | ||
Snapshot = null; | ||
|
||
CommitIndex = 0; | ||
LastApplied = 0; | ||
|
||
LeaderState = null; | ||
CandidateState = null; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace SlimCluster.Consensus.Raft; | ||
|
||
using SlimCluster; | ||
|
||
public class RaftNodeStatus : AbstractStatus, INodeStatus | ||
{ | ||
protected RaftNodeStatus(Guid id, string name) : base(id, name) | ||
{ | ||
} | ||
|
||
public static readonly RaftNodeStatus Leader = new (new Guid("{FF564377-5F12-473E-8FC0-E44BAFE74BA8}"), "Leader"); | ||
public static readonly RaftNodeStatus Follower = new (new Guid("{0F5DB060-5ADB-4973-9EEE-A6B8FD5CEEC8}"), "Follower"); | ||
public static readonly RaftNodeStatus Candidate = new (new Guid("{BC46D901-FDE0-4D54-8E7F-78129627138C}"), "Candidate"); | ||
} |
23 changes: 23 additions & 0 deletions
23
src/SlimCluster.Consensus.Raft/SlimCluster.Consensus.Raft.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="../Common.Properties.xml" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Description>Raft consensus algorithm implementation for a distributed .NET service</Description> | ||
<PackageTags>SlimCluster Cluster Consensus Raft</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\SlimCluster.Membership\SlimCluster.Membership.csproj" /> | ||
<ProjectReference Include="..\SlimCluster\SlimCluster.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\icon.png"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.