Class RaftCluster<TMember>
Represents transport-independent implementation of Raft protocol.
Inherited Members
Namespace: DotNext.Net.Cluster.Consensus.Raft
Assembly: DotNext.Net.Cluster.dll
Syntax
public abstract class RaftCluster<TMember> : Disposable, IDisposable, IRaftCluster, IReplicationCluster<IRaftLogEntry>, IReplicationCluster, ICluster, IRaftStateMachine where TMember : class, IRaftClusterMember, IDisposable
Type Parameters
Name | Description |
---|---|
TMember | The type implementing communication details with remote nodes. |
Constructors
| Improve this Doc View SourceRaftCluster(IClusterMemberConfiguration, out RaftCluster<TMember>.MemberCollectionBuilder)
Initializes a new cluster manager for the local node.
Declaration
protected RaftCluster(IClusterMemberConfiguration config, out RaftCluster<TMember>.MemberCollectionBuilder members)
Parameters
Type | Name | Description |
---|---|---|
IClusterMemberConfiguration | config | The configuration of the local node. |
RaftCluster.MemberCollectionBuilder<> | members | The collection of members that can be modified at construction stage. |
Properties
| Improve this Doc View SourceAuditTrail
Associates audit trail with the current instance.
Declaration
public IPersistentState AuditTrail { get; set; }
Property Value
Type | Description |
---|---|
IPersistentState |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
IsLeaderLocal
Indicates that local member is a leader.
Declaration
protected bool IsLeaderLocal { get; }
Property Value
Type | Description |
---|---|
Boolean |
Leader
Gets leader of the cluster.
Declaration
public TMember Leader { get; }
Property Value
Type | Description |
---|---|
TMember |
Logger
Gets logger used by this object.
Declaration
[CLSCompliant(false)]
protected virtual ILogger Logger { get; }
Property Value
Type | Description |
---|---|
ILogger |
Members
Gets members of Raft-based cluster.
Declaration
public IReadOnlyCollection<TMember> Members { get; }
Property Value
Type | Description |
---|---|
IReadOnlyCollection<TMember> | A collection of cluster member. |
Metrics
Establishes metrics collector.
Declaration
public MetricsCollector Metrics { protected get; set; }
Property Value
Type | Description |
---|---|
MetricsCollector |
Term
Gets Term value maintained by local member.
Declaration
public long Term { get; }
Property Value
Type | Description |
---|---|
Int64 |
Token
Gets token that can be used for all internal asynchronous operations.
Declaration
protected CancellationToken Token { get; }
Property Value
Type | Description |
---|---|
CancellationToken |
Methods
| Improve this Doc View SourceChangeMembersAsync(RaftCluster<TMember>.MemberCollectionMutator)
Modifies collection of cluster members.
Declaration
protected Task ChangeMembersAsync(RaftCluster<TMember>.MemberCollectionMutator mutator)
Parameters
Type | Name | Description |
---|---|---|
RaftCluster.MemberCollectionMutator<> | mutator | The action that can be used to change set of cluster members. |
Returns
Type | Description |
---|---|
Task | The task representing asynchronous execution of this method. |
ChangeMembersAsync(RaftCluster<TMember>.MemberCollectionMutator, CancellationToken)
Modifies collection of cluster members.
Declaration
protected Task ChangeMembersAsync(RaftCluster<TMember>.MemberCollectionMutator mutator, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
RaftCluster.MemberCollectionMutator<> | mutator | The action that can be used to change set of cluster members. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task | The task representing asynchronous execution of this method. |
Dispose(Boolean)
Releases managed and unmanaged resources associated with this object.
Declaration
protected override void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
Boolean | disposing | true if called from Dispose(); false if called from finalizer Finalize(). |
Overrides
| Improve this Doc View SourceFindMember(Predicate<TMember>)
Finds cluster member using predicate.
Declaration
protected TMember FindMember(Predicate<TMember> criteria)
Parameters
Type | Name | Description |
---|---|---|
Predicate<TMember> | criteria | The predicate used to find appropriate member. |
Returns
Type | Description |
---|---|
TMember | The cluster member; or null if there is not member matching to the specified criteria. |
FindMember<TArg>(Func<TMember, TArg, Boolean>, TArg)
Finds cluster member using predicate.
Declaration
protected TMember FindMember<TArg>(Func<TMember, TArg, bool> criteria, TArg arg)
Parameters
Type | Name | Description |
---|---|---|
Func<TMember, TArg, Boolean> | criteria | The predicate used to find appropriate member. |
TArg | arg | The argument to be passed to the matching function. |
Returns
Type | Description |
---|---|
TMember | The cluster member; or null if member doesn't exist. |
Type Parameters
Name | Description |
---|---|
TArg | The type of the predicate parameter. |
ForceReplicationAsync(TimeSpan, CancellationToken)
Forces replication.
Declaration
public Task<bool> ForceReplicationAsync(TimeSpan timeout, CancellationToken token = default(CancellationToken))
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | The time to wait until replication ends. |
CancellationToken | token | The token that can be used to cancel waiting. |
Returns
Type | Description |
---|---|
Task<Boolean> | true if replication is completed; false. |
Exceptions
Type | Condition |
---|---|
InvalidOperationException | The local cluster member is not a leader. |
OperationCanceledException | The operation has been canceled. |
ReceiveEntries<TEntry>(TMember, Int64, ILogEntryProducer<TEntry>, Int64, Int64, Int64)
Handles AppendEntries message received from remote cluster member.
Declaration
[Obsolete("Use ReceiveEntriesAsync method instead")]
protected Task<Result<bool>> ReceiveEntries<TEntry>(TMember sender, long senderTerm, ILogEntryProducer<TEntry> entries, long prevLogIndex, long prevLogTerm, long commitIndex)
where TEntry : IRaftLogEntry
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The sender of the replica message. |
Int64 | senderTerm | Term value provided by Heartbeat message sender. |
ILogEntryProducer<TEntry> | entries | The stateful function that provides entries to be committed locally. |
Int64 | prevLogIndex | Index of log entry immediately preceding new ones. |
Int64 | prevLogTerm | Term of |
Int64 | commitIndex | The last entry known to be committed on the sender side. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if log entry is committed successfully; false if preceding is not present in local audit trail. |
Type Parameters
Name | Description |
---|---|
TEntry | The actual type of the log entry returned by the supplier. |
ReceiveEntriesAsync<TEntry>(TMember, Int64, ILogEntryProducer<TEntry>, Int64, Int64, Int64, CancellationToken)
Handles AppendEntries message received from remote cluster member.
Declaration
protected Task<Result<bool>> ReceiveEntriesAsync<TEntry>(TMember sender, long senderTerm, ILogEntryProducer<TEntry> entries, long prevLogIndex, long prevLogTerm, long commitIndex, CancellationToken token)
where TEntry : IRaftLogEntry
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The sender of the replica message. |
Int64 | senderTerm | Term value provided by Heartbeat message sender. |
ILogEntryProducer<TEntry> | entries | The stateful function that provides entries to be committed locally. |
Int64 | prevLogIndex | Index of log entry immediately preceding new ones. |
Int64 | prevLogTerm | Term of |
Int64 | commitIndex | The last entry known to be committed on the sender side. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if log entry is committed successfully; false if preceding is not present in local audit trail. |
Type Parameters
Name | Description |
---|---|
TEntry | The actual type of the log entry returned by the supplier. |
ReceiveResign()
Revokes leadership of the local node.
Declaration
[Obsolete("Use ReceiveResignAsync method instead")]
protected Task<bool> ReceiveResign()
Returns
Type | Description |
---|---|
Task<Boolean> | true, if leadership is revoked successfully; otherwise, false. |
ReceiveResignAsync(CancellationToken)
Revokes leadership of the local node.
Declaration
protected Task<bool> ReceiveResignAsync(CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task<Boolean> | true, if leadership is revoked successfully; otherwise, false. |
ReceiveSnapshot(TMember, Int64, IRaftLogEntry, Int64)
Handles InstallSnapshot message received from remote cluster member.
Declaration
[Obsolete("Use ReceiveSnapshotAsync method instead")]
protected Task<Result<bool>> ReceiveSnapshot(TMember sender, long senderTerm, IRaftLogEntry snapshot, long snapshotIndex)
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The sender of the snapshot message. |
Int64 | senderTerm | Term value provided by InstallSnapshot message sender. |
IRaftLogEntry | snapshot | The snapshot to be installed into local audit trail. |
Int64 | snapshotIndex | The index of the last log entry included in the snapshot. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if snapshot is installed successfully; false if snapshot is outdated. |
ReceiveSnapshotAsync<TSnapshot>(TMember, Int64, TSnapshot, Int64, CancellationToken)
Handles InstallSnapshot message received from remote cluster member.
Declaration
protected Task<Result<bool>> ReceiveSnapshotAsync<TSnapshot>(TMember sender, long senderTerm, TSnapshot snapshot, long snapshotIndex, CancellationToken token)
where TSnapshot : IRaftLogEntry
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The sender of the snapshot message. |
Int64 | senderTerm | Term value provided by InstallSnapshot message sender. |
TSnapshot | snapshot | The snapshot to be installed into local audit trail. |
Int64 | snapshotIndex | The index of the last log entry included in the snapshot. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if snapshot is installed successfully; false if snapshot is outdated. |
Type Parameters
Name | Description |
---|---|
TSnapshot | The type of snapshot record. |
ReceiveVote(TMember, Int64, Int64, Int64)
Votes for the new candidate.
Declaration
[Obsolete("Use ReceiveVoteAsync method instead")]
protected Task<Result<bool>> ReceiveVote(TMember sender, long senderTerm, long lastLogIndex, long lastLogTerm)
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The vote sender. |
Int64 | senderTerm | Term value provided by sender of the request. |
Int64 | lastLogIndex | Index of candidate's last log entry. |
Int64 | lastLogTerm | Term of candidate's last log entry. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if local node accepts new leader in the cluster; otherwise, false. |
ReceiveVoteAsync(TMember, Int64, Int64, Int64, CancellationToken)
Votes for the new candidate.
Declaration
protected Task<Result<bool>> ReceiveVoteAsync(TMember sender, long senderTerm, long lastLogIndex, long lastLogTerm, CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
TMember | sender | The vote sender. |
Int64 | senderTerm | Term value provided by sender of the request. |
Int64 | lastLogIndex | Index of candidate's last log entry. |
Int64 | lastLogTerm | Term of candidate's last log entry. |
CancellationToken | token | The token that can be used to cancel the operation. |
Returns
Type | Description |
---|---|
Task<Result<Boolean>> | true if local node accepts new leader in the cluster; otherwise, false. |
StartAsync(CancellationToken)
Starts serving local member.
Declaration
public virtual Task StartAsync(CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | token | The token that can be used to cancel initialization process. |
Returns
Type | Description |
---|---|
Task | The task representing asynchronous execution of the method. |
StopAsync(CancellationToken)
Stops serving local member.
Declaration
public virtual Task StopAsync(CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | token | The token that can be used to cancel shutdown process. |
Returns
Type | Description |
---|---|
Task | The task representing asynchronous execution of the method. |
Events
| Improve this Doc View SourceLeaderChanged
An event raised when leader has been changed.
Declaration
public event ClusterLeaderChangedEventHandler LeaderChanged
Event Type
Type | Description |
---|---|
ClusterLeaderChangedEventHandler |
Explicit Interface Implementations
| Improve this Doc View SourceIRaftCluster.ElectionTimeout
Declaration
TimeSpan IRaftCluster.ElectionTimeout { get; }
Returns
Type | Description |
---|---|
TimeSpan |
ICluster.Leader
Declaration
IClusterMember ICluster.Leader { get; }
Returns
Type | Description |
---|---|
IClusterMember |
ICluster.Members
Declaration
IReadOnlyCollection<IClusterMember> ICluster.Members { get; }
Returns
Type | Description |
---|---|
IReadOnlyCollection<IClusterMember> |
ICluster.ResignAsync(CancellationToken)
Declaration
Task<bool> ICluster.ResignAsync(CancellationToken token)
Parameters
Type | Name | Description |
---|---|---|
CancellationToken | token |
Returns
Type | Description |
---|---|
Task<Boolean> |