Class RaftCluster<TMember>
Represents transport-independent implementation of Raft protocol.
Namespace: DotNext.Net.Cluster.Consensus.Raft
Assembly: DotNext.Net.Cluster.dll
Syntax
public abstract class RaftCluster<TMember> : Disposable, IRaftCluster, IReplicationCluster<IRaftLogEntry>, IReplicationCluster, ICluster, IRaftStateMachine where TMember : class, IRaftClusterMember, IDisposable
Type Parameters
| Name | Description |
|---|---|
| TMember |
Constructors
| Improve this Doc View SourceRaftCluster(IClusterMemberConfiguration, out RaftCluster<TMember>.MutableMemberCollection)
Initializes a new cluster manager for the local node.
Declaration
protected RaftCluster(IClusterMemberConfiguration config, out RaftCluster<TMember>.MutableMemberCollection members)
Parameters
| Type | Name | Description |
|---|---|---|
| IClusterMemberConfiguration | config | The configuration of the local node. |
| RaftCluster.MutableMemberCollection<> | 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 |
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
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. |
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 |
FindMember(Predicate<TMember>)
Finds cluster member using predicate.
Declaration
protected TMember FindMember(Predicate<TMember> matcher)
Parameters
| Type | Name | Description |
|---|---|---|
| Predicate<TMember> | matcher | The predicate used to find appropriate member. |
Returns
| Type | Description |
|---|---|
| TMember | The cluster member; |
ReceiveEntries<TEntry>(TMember, Int64, ILogEntryProducer<TEntry>, Int64, Int64, Int64)
Handles AppendEntries message received from remote cluster member.
Declaration
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. |
ReceiveResign()
Revokes leadership of the local node.
Declaration
protected Task<bool> ReceiveResign()
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
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. |
ReceiveVote(TMember, Int64, Int64, Int64)
Votes for the new candidate.
Declaration
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. |
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. |
WriteAsync<TEntry>(ILogEntryProducer<TEntry>, WriteConcern, TimeSpan)
Writes message into the cluster according with the specified concern.
Declaration
public Task WriteAsync<TEntry>(ILogEntryProducer<TEntry> entries, WriteConcern concern, TimeSpan timeout)
where TEntry : IRaftLogEntry
Parameters
| Type | Name | Description |
|---|---|---|
| ILogEntryProducer<TEntry> | entries | The number of commands to be committed into the audit trail. |
| WriteConcern | concern | The value describing level of acknowledgment from cluster. |
| TimeSpan | timeout | The timeout of the asynchronous operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous state of this operation. |
Type Parameters
| Name | Description |
|---|---|
| TEntry | The actual type of the log entry returned by the supplier. |
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> |
IReplicationCluster.AuditTrail
Declaration
IAuditTrail IReplicationCluster.AuditTrail { get; }
Returns
| Type | Description |
|---|---|
| IAuditTrail |
IReplicationCluster<IRaftLogEntry>.AuditTrail
Declaration
IAuditTrail<IRaftLogEntry> IReplicationCluster<IRaftLogEntry>.AuditTrail { get; }
Returns
| Type | Description |
|---|---|
| IAuditTrail<IRaftLogEntry> |