FileStateMachine

Struct FileStateMachine 

Source
pub struct FileStateMachine { /* private fields */ }
Expand description

File-based state machine implementation with persistence

Design principles:

  • All data is persisted to disk for durability
  • In-memory cache for fast read operations
  • Write-ahead logging for crash consistency
  • Efficient snapshot handling with file-based storage
  • Thread-safe with minimal lock contention

Implementations§

Source§

impl FileStateMachine

Source

pub async fn new(data_dir: PathBuf, node_id: u32) -> Result<Self, Error>

Creates a new file-based state machine with persistence

§Arguments
  • data_dir - Directory where data files will be stored
  • node_id - Unique identifier for this node
§Returns

Result containing the initialized FileStateMachine

Source

pub async fn reset(&self) -> Result<(), Error>

Resets the state machine to its initial empty state

This method:

  1. Clears all in-memory data
  2. Resets Raft state to initial values
  3. Clears all persisted files
  4. Maintains operational state (running status, node ID)

Trait Implementations§

Source§

impl Debug for FileStateMachine

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FileStateMachine

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl StateMachine for FileStateMachine

Source§

fn start(&self) -> Result<(), Error>

Starts the state machine service. This is typically a sync operation as it just flips internal state flags.
Source§

fn stop(&self) -> Result<(), Error>

Stops the state machine service gracefully. This is typically a sync operation for state management.
Source§

fn is_running(&self) -> bool

Checks if the state machine is currently running. Sync operation as it just checks an atomic boolean.
Source§

fn get(&self, key_buffer: &[u8]) -> Result<Option<Vec<u8>>, Error>

Retrieves a value by key from the state machine. Sync operation as it accesses in-memory data structures.
Source§

fn entry_term(&self, entry_id: u64) -> Option<u64>

Returns the term of a specific log entry by its ID. Sync operation as it queries in-memory data.
Source§

fn apply_chunk<'life0, 'async_trait>( &'life0 self, chunk: Vec<Entry>, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Applies a chunk of log entries to the state machine. Async operation as it may involve disk I/O for persistence.
Source§

fn len(&self) -> usize

Returns the number of entries in the state machine. NOTE: This may be expensive for some implementations. Sync operation but should be used cautiously.
Source§

fn update_last_applied(&self, last_applied: LogId)

Updates the last applied index in memory. Sync operation as it just updates atomic variables.
Source§

fn last_applied(&self) -> LogId

Gets the last applied log index and term. Sync operation as it reads from atomic variables.
Source§

fn persist_last_applied(&self, last_applied: LogId) -> Result<(), Error>

Persists the last applied index to durable storage. Should be async as it involves disk I/O.
Source§

fn update_last_snapshot_metadata( &self, snapshot_metadata: &SnapshotMetadata, ) -> Result<(), Error>

Updates snapshot metadata in memory. Sync operation as it updates in-memory structures.
Source§

fn snapshot_metadata(&self) -> Option<SnapshotMetadata>

Retrieves the current snapshot metadata. Sync operation as it reads from in-memory structures.
Source§

fn persist_last_snapshot_metadata( &self, snapshot_metadata: &SnapshotMetadata, ) -> Result<(), Error>

Persists snapshot metadata to durable storage. Should be async as it involves disk I/O.
Source§

fn apply_snapshot_from_file<'life0, 'life1, 'async_trait>( &'life0 self, metadata: &'life1 SnapshotMetadata, snapshot_dir: PathBuf, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Applies a snapshot received from the Raft leader to the local state machine Read more
Source§

fn generate_snapshot_data<'life0, 'async_trait>( &'life0 self, new_snapshot_dir: PathBuf, last_included: LogId, ) -> Pin<Box<dyn Future<Output = Result<[u8; 32], Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Generates a snapshot of the state machine’s current key-value entries up to the specified last_included_index. Read more
Source§

fn save_hard_state(&self) -> Result<(), Error>

Saves the hard state of the state machine. Sync operation as it typically just delegates to other persistence methods.
Source§

fn flush(&self) -> Result<(), Error>

Flushes any pending writes to durable storage. Sync operation that may block but provides a synchronous interface.
Source§

fn flush_async<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Flushes any pending writes to durable storage. Async operation as it involves disk I/O.
Source§

fn reset<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Resets the state machine to its initial state. Async operation as it may involve cleaning up files and data.
Source§

fn is_empty(&self) -> bool

Checks if the state machine is empty. Sync operation that typically delegates to len().

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more