use bytes::Bytes;
use d_engine_proto::common::Entry;
use d_engine_proto::common::LogId;
use d_engine_proto::server::storage::SnapshotMetadata;
#[cfg(any(test, feature = "__test_support"))]
use mockall::automock;
use tonic::async_trait;
use crate::Error;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ApplyResult {
pub index: u64,
pub succeeded: bool,
}
impl ApplyResult {
pub fn success(index: u64) -> Self {
Self {
index,
succeeded: true,
}
}
pub fn failure(index: u64) -> Self {
Self {
index,
succeeded: false,
}
}
}
#[cfg_attr(any(test, feature = "__test_support"), automock)]
#[async_trait]
pub trait StateMachine: Send + Sync + 'static {
async fn start(&self) -> Result<(), Error>;
fn stop(&self) -> Result<(), Error>;
fn is_running(&self) -> bool;
fn get(
&self,
key_buffer: &[u8],
) -> Result<Option<Bytes>, Error>;
fn entry_term(
&self,
entry_id: u64,
) -> Option<u64>;
async fn apply_chunk(
&self,
chunk: Vec<Entry>,
) -> Result<Vec<ApplyResult>, Error>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn update_last_applied(
&self,
last_applied: LogId,
);
fn last_applied(&self) -> LogId;
fn persist_last_applied(
&self,
last_applied: LogId,
) -> Result<(), Error>;
fn update_last_snapshot_metadata(
&self,
snapshot_metadata: &SnapshotMetadata,
) -> Result<(), Error>;
fn snapshot_metadata(&self) -> Option<SnapshotMetadata>;
fn persist_last_snapshot_metadata(
&self,
snapshot_metadata: &SnapshotMetadata,
) -> Result<(), Error>;
async fn apply_snapshot_from_file(
&self,
metadata: &SnapshotMetadata,
snapshot_path: std::path::PathBuf,
) -> Result<(), Error>;
async fn generate_snapshot_data(
&self,
new_snapshot_dir: std::path::PathBuf,
last_included: LogId,
) -> Result<Bytes, Error>;
fn save_hard_state(&self) -> Result<(), Error>;
fn flush(&self) -> Result<(), Error>;
async fn flush_async(&self) -> Result<(), Error>;
async fn reset(&self) -> Result<(), Error>;
async fn lease_background_cleanup(&self) -> Result<Vec<bytes::Bytes>, Error> {
Ok(vec![])
}
}