Class MemoryBasedStateMachine
Represents memory-based state machine with snapshotting support.
Inherited Members
Namespace: DotNext.Net.Cluster.Consensus.Raft
Assembly: DotNext.Net.Cluster.dll
Syntax
public abstract class MemoryBasedStateMachine : PersistentState, IDisposable, IPersistentState, IAuditTrail<IRaftLogEntry>, IAuditTrail
Remarks
The layout of the audit trail file system:
| node.state | file containing internal state of Raft node |
| <partition> | file containing log partition with log records |
| snapshot | file containing snapshot |
Constructors
| Edit this page View SourceMemoryBasedStateMachine(DirectoryInfo, int, Options?)
Initializes a new memory-based state machine.
Declaration
protected MemoryBasedStateMachine(DirectoryInfo path, int recordsPerPartition, MemoryBasedStateMachine.Options? configuration = null)
Parameters
| Type | Name | Description |
|---|---|---|
| DirectoryInfo | path | The path to the folder to be used by audit trail. |
| int | recordsPerPartition | The maximum number of log entries that can be stored in the single file called partition. |
| MemoryBasedStateMachine.Options | configuration | The configuration of the persistent audit trail. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
MemoryBasedStateMachine(string, int, Options?)
Initializes a new memory-based state machine.
Declaration
protected MemoryBasedStateMachine(string path, int recordsPerPartition, MemoryBasedStateMachine.Options? configuration = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | path | The path to the folder to be used by audit trail. |
| int | recordsPerPartition | The maximum number of log entries that can be stored in the single file called partition. |
| MemoryBasedStateMachine.Options | configuration | The configuration of the persistent audit trail. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
Properties
| Edit this page View SourceCompactionCount
Gets approximate number of partitions that can be compacted.
Declaration
public long CompactionCount { get; }
Property Value
| Type | Description |
|---|---|
| long |
IsBackgroundCompaction
Gets a value indicating that log compaction should be called manually using ForceCompactionAsync(long, CancellationToken) in the background.
Declaration
public bool IsBackgroundCompaction { get; }
Property Value
| Type | Description |
|---|---|
| bool |
Methods
| Edit this page View SourceApplyAsync(LogEntry)
Applies the command represented by the log entry to the underlying database engine.
Declaration
protected abstract ValueTask ApplyAsync(PersistentState.LogEntry entry)
Parameters
| Type | Name | Description |
|---|---|---|
| PersistentState.LogEntry | entry | The entry to be applied to the state machine. |
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this method. |
Remarks
Context can be used to pass custom data through WAL processing pipeline. The value of this property is not persistent. The data can be passed as a part of log entry which is used as the argument of AppendAsync<TEntry>(TEntry, CancellationToken) method. The passed log entry type must implement IInputLogEntry interface.
See Also
| Edit this page View SourceClearAsync(CancellationToken)
Removes all log entries from the log.
Declaration
protected override sealed Task ClearAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous result of the method. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been cancelled. |
CreateSnapshotBuilder(in SnapshotBuilderContext)
Creates a new snapshot builder.
Declaration
protected abstract MemoryBasedStateMachine.SnapshotBuilder CreateSnapshotBuilder(in MemoryBasedStateMachine.SnapshotBuilderContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| MemoryBasedStateMachine.SnapshotBuilderContext | context | The context of the snapshot builder. |
Returns
| Type | Description |
|---|---|
| MemoryBasedStateMachine.SnapshotBuilder | The snapshot builder. |
Dispose(bool)
Releases all resources associated with this audit trail.
Declaration
protected override void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | true if called from Dispose(); false if called from finalizer. |
Overrides
| Edit this page View SourceForceCompactionAsync(long, CancellationToken)
Forces log compaction.
Declaration
public ValueTask ForceCompactionAsync(long count, CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| long | count | The number of partitions to be compacted. |
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this operation. |
Remarks
Full compaction may be time-expensive operation. In this case,
all readers will be blocked until the end of the compaction.
Therefore, count can be used to reduce
lock contention between compaction and readers. If it is 1
then compaction range is limited to the log entries contained in the single partition.
This may be helpful if manual compaction is triggered by the background job.
The job can wait for the commit using WaitForCommitAsync(CancellationToken)
and then call this method with appropriate number of partitions to be collected
according to CompactionCount property.
Exceptions
| Type | Condition |
|---|---|
| ObjectDisposedException | This log is disposed. |
| OperationCanceledException | The operation has been canceled. |
InitializeAsync(CancellationToken)
Initializes this state asynchronously.
Declaration
public override Task InitializeAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous result of the method. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been cancelled. |
ReplayAsync(CancellationToken)
Reconstructs dataset by calling ApplyAsync(LogEntry) for each committed entry.
Declaration
public Task ReplayAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous state of the method. |