Show / Hide Table of Contents

Class MemoryBasedStateMachine

Represents memory-based state machine with snapshotting support.

Inheritance
object
Disposable
PersistentState
MemoryBasedStateMachine
Implements
IDisposable
IPersistentState
IAuditTrail<IRaftLogEntry>
IAuditTrail
Inherited Members
PersistentState.CreateBackupAsync(Stream, CancellationToken)
PersistentState.RestoreFromBackupAsync(Stream, DirectoryInfo, CancellationToken)
PersistentState.Location
PersistentState.ReadAsync<TResult>(ILogEntryConsumer<IRaftLogEntry, TResult>, long, long, CancellationToken)
PersistentState.ReadAsync<TResult>(ILogEntryConsumer<IRaftLogEntry, TResult>, long, CancellationToken)
PersistentState.AppendAsync<TEntry>(TEntry, long, CancellationToken)
PersistentState.AppendAsync<TEntry>(TEntry, CancellationToken)
PersistentState.AppendAsync<TEntry>(TEntry, bool, CancellationToken)
PersistentState.AppendAsync<TEntry>(ILogEntryProducer<TEntry>, CancellationToken)
PersistentState.DropAsync(long, bool, CancellationToken)
PersistentState.WaitForApplyAsync(CancellationToken)
PersistentState.WaitForApplyAsync(long, CancellationToken)
PersistentState.CommitAsync(long, CancellationToken)
PersistentState.CommitAsync(CancellationToken)
PersistentState.Term
PersistentState.CreateJsonLogEntry<T>(T)
PersistentState.CreateBinaryLogEntry(ReadOnlyMemory<byte>)
PersistentState.CreateBinaryLogEntry<T>(T)
PersistentState.LastCommittedEntryIndex
PersistentState.LastEntryIndex
Disposable.IsDisposed
Disposable.IsDisposing
Disposable.IsDisposingOrDisposed
Disposable.CreateException()
Disposable.DisposedTask
Disposable.GetDisposedTask<T>()
Disposable.TrySetDisposedException<T>(TaskCompletionSource<T>)
Disposable.TrySetDisposedException(TaskCompletionSource)
Disposable.DisposeAsyncCore()
Disposable.DisposeAsync()
Disposable.TryBeginDispose()
Disposable.Dispose()
Disposable.Dispose(IEnumerable<IDisposable>)
Disposable.DisposeAsync(IEnumerable<IAsyncDisposable>)
Disposable.Dispose<T>(ReadOnlySpan<T>)
Disposable.DisposeAsync(params IAsyncDisposable[])
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
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.statefile containing internal state of Raft node
<partition>file containing log partition with log records
snapshotfile containing snapshot
The audit trail supports log compaction. However, it doesn't know how to interpret and reduce log records during compaction. To do that, you can override CreateSnapshotBuilder(in SnapshotBuilderContext) method and implement state machine logic.

Constructors

| Edit this page View Source

MemoryBasedStateMachine(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

recordsPerPartition is less than 2.

| Edit this page View Source

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

recordsPerPartition is less than 2.

Properties

| Edit this page View Source

CompactionCount

Gets approximate number of partitions that can be compacted.

Declaration
public long CompactionCount { get; }
Property Value
Type Description
long
| Edit this page View Source

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 Source

ApplyAsync(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
CommandInterpreter
| Edit this page View Source

ClearAsync(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
PersistentState.ClearAsync(CancellationToken)
Exceptions
Type Condition
OperationCanceledException

The operation has been cancelled.

| Edit this page View Source

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.

| Edit this page View Source

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
PersistentState.Dispose(bool)
| Edit this page View Source

ForceCompactionAsync(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.

| Edit this page View Source

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
PersistentState.InitializeAsync(CancellationToken)
Exceptions
Type Condition
OperationCanceledException

The operation has been cancelled.

| Edit this page View Source

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.

Implements

IDisposable
IPersistentState
IAuditTrail<TEntry>
IAuditTrail

Extension Methods

Command.AppendAsync<TCommand>(IPersistentState, TCommand, object?, CancellationToken)
PersistentStateExtensions.AppendAsync(IPersistentState, ReadOnlyMemory<byte>, object?, CancellationToken)
PersistentStateExtensions.AppendAsync<T>(IPersistentState, T, object?, CancellationToken)
PersistentStateExtensions.AppendJsonAsync<T>(IPersistentState, T, object?, CancellationToken)
BasicExtensions.As<T>(T)
BasicExtensions.GetUserData<T>(T)
BasicExtensions.IsBetween<T, TLowerBound, TUpperBound>(T, TLowerBound, TUpperBound)
BasicExtensions.IsOneOf<T>(T, ReadOnlySpan<T>)
ExpressionBuilder.Const<T>(T)
AsyncLockAcquisition.AcquireLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireReadLockAsync<T>(T, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, bool, TimeSpan, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, CancellationToken)
AsyncLockAcquisition.AcquireWriteLockAsync<T>(T, TimeSpan, CancellationToken)
LockAcquisition.AcquireReadLock<T>(T)
LockAcquisition.AcquireReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireUpgradeableReadLock<T>(T)
LockAcquisition.AcquireUpgradeableReadLock<T>(T, TimeSpan)
LockAcquisition.AcquireWriteLock<T>(T)
LockAcquisition.AcquireWriteLock<T>(T, TimeSpan)
  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾