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
- TTL support for automatic key expiration
Implementations§
Source§impl FileStateMachine
impl FileStateMachine
Sourcepub fn set_lease(&mut self, lease: Arc<DefaultLease>)
pub fn set_lease(&mut self, lease: Arc<DefaultLease>)
Sets the lease manager for this state machine.
This is an internal method called by NodeBuilder during initialization.
The lease will also be restored from snapshot during apply_snapshot_from_file().
Also available for testing and benchmarks.
Sourcepub async fn load_lease_data(&self) -> Result<(), Error>
pub async fn load_lease_data(&self) -> Result<(), Error>
Loads TTL data into the configured lease
Called after NodeBuilder injects the lease. Also available for testing and benchmarks.
Trait Implementations§
Source§impl Debug for FileStateMachine
impl Debug for FileStateMachine
Source§impl Drop for FileStateMachine
impl Drop for FileStateMachine
Source§impl StateMachine for FileStateMachine
impl StateMachine for FileStateMachine
Source§fn apply_chunk<'life0, 'async_trait>(
&'life0 self,
chunk: Vec<Entry>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
fn apply_chunk<'life0, 'async_trait>(
&'life0 self,
chunk: Vec<Entry>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
Thread-safe: called serially by single-task CommitHandler
Source§fn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
fn start<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
Starts the state machine service. Read more
Source§fn stop(&self) -> Result<(), Error>
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
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<Bytes>, Error>
fn get(&self, key_buffer: &[u8]) -> Result<Option<Bytes>, 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>
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 len(&self) -> usize
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)
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
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>
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>
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>
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>
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
'life0: 'async_trait,
'life1: 'async_trait,
FileStateMachine: 'async_trait,
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
'life0: 'async_trait,
'life1: 'async_trait,
FileStateMachine: '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<Bytes, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
fn generate_snapshot_data<'life0, 'async_trait>(
&'life0 self,
new_snapshot_dir: PathBuf,
last_included: LogId,
) -> Pin<Box<dyn Future<Output = Result<Bytes, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
Generates a snapshot of the state machine’s current key-value entries
up to the specified
last_included_index. Read moreSource§fn save_hard_state(&self) -> Result<(), Error>
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>
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
'life0: 'async_trait,
FileStateMachine: 'async_trait,
fn flush_async<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: '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
'life0: 'async_trait,
FileStateMachine: 'async_trait,
fn reset<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
FileStateMachine: 'async_trait,
Resets the state machine to its initial state.
Async operation as it may involve cleaning up files and data.
Auto Trait Implementations§
impl !Freeze for FileStateMachine
impl !RefUnwindSafe for FileStateMachine
impl Send for FileStateMachine
impl Sync for FileStateMachine
impl Unpin for FileStateMachine
impl !UnwindSafe for FileStateMachine
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request