openraft 0.10.2

Advanced Raft consensus
Documentation
use std::ops::Deref;
use std::time::Duration;

use openraft_macros::since;

use crate::Raft;
use crate::RaftTypeConfig;
use crate::async_runtime::watch::WatchReceiver;
use crate::errors::Fatal;
use crate::metrics::Metric;
use crate::metrics::WaitError;
use crate::raft::linearizable_read::LinearizeState;
use crate::storage::RaftStateMachine;
use crate::type_config::alias::LogIdOf;

/// Represents a linearization operation for read.
///
/// See the [read protocol documentation](crate::docs::protocol::read) for more details.
///
/// This struct is the result returned from [`Raft::get_read_linearizer()`],
/// which is the implementation of awaiting the applied log entries.
/// The application calls [`Linearizer::try_await_ready()`](Self::try_await_ready) to ensure its
/// following reads are linearized.
///
/// It retains the no-op and committed log IDs captured when the read was authorized. Their
/// maximum is the required read log ID. A relaxed linearizer, requested with
/// [`LinearizerOption::with_relaxed`], waits only for the no-op on the node that issued it; on any
/// other node it waits for the read log ID. It also tracks last known applied progress on a node.
///
/// [`LinearizerOption::with_relaxed`]: crate::raft::linearizable_read::LinearizerOption::with_relaxed
///
/// [`Raft::get_read_linearizer()`]: Raft::get_read_linearizer
#[since(version = "0.10.0")]
#[must_use = "call `try_await_ready()` to ensure linearizability"]
#[derive(Debug, Clone)]
pub struct Linearizer<C>
where C: RaftTypeConfig
{
    /// Captured read boundaries and last known applied progress.
    state: LinearizeState<C>,
}

impl<C> Deref for Linearizer<C>
where C: RaftTypeConfig
{
    type Target = LinearizeState<C>;

    fn deref(&self) -> &Self::Target {
        &self.state
    }
}

impl<C> Linearizer<C>
where C: RaftTypeConfig
{
    /// Create a linearizer from the captured no-op and committed log IDs and applied progress.
    ///
    /// When transferring a linearizer to a follower, preserve both log IDs from the leader.
    /// `applied` belongs to `node_id` and is not reused as another node's progress.
    #[since(version = "0.10.1", change = "accept separate no-op and committed log IDs")]
    #[since(version = "0.10.0", change = "accept ReadLogId instead of LogId")]
    #[since(version = "0.10.0")]
    pub fn new(
        node_id: C::NodeId,
        noop_log_id: LogIdOf<C>,
        committed: Option<LogIdOf<C>>,
        applied: Option<LogIdOf<C>>,
    ) -> Self {
        Self {
            state: LinearizeState::new(node_id, noop_log_id, committed, applied),
        }
    }

    /// Mark whether this read requires only the no-op log ID of the authorizing leadership.
    ///
    /// The captured committed log ID is retained either way, and [`Self::try_await_ready`] on
    /// another node still waits for it.
    pub(crate) fn with_relaxed(self, relaxed: bool) -> Self {
        let state = self.state.with_relaxed(relaxed);
        Self { state }
    }

    /// Replace the last applied log id this linearizer reports.
    ///
    /// A queued read observes the applied log id when it is created, which may lag by a heartbeat
    /// round when the read is finally answered. Refreshing it lets the caller skip waiting for
    /// apply progress the leader has already made.
    pub(crate) fn with_applied(self, applied: Option<LogIdOf<C>>) -> Self {
        let node_id = self.state.node_id().clone();
        let state = self.state.with_applied(node_id, applied);
        Self { state }
    }

    /// Waits indefinitely for the state machine to apply all required log entries for linearizable
    /// reads.
    ///
    /// This is a convenience method that calls [`try_await_ready(_, None)`](Self::try_await_ready)
    /// with no timeout and unwraps the result.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use openraft::Raft;
    /// # use openraft::RaftTypeConfig;
    /// # use openraft::storage::RaftStateMachine;
    /// use openraft::ReadPolicy;
    ///
    /// # trait Get {
    /// #     fn get(&self, key: &str) -> Option<String>;
    /// # }
    /// #
    /// # async fn example<C, SM>(raft: &Raft<C, SM>) -> Result<(), Box<dyn std::error::Error>>
    /// # where
    /// #     C: RaftTypeConfig,
    /// #     SM: RaftStateMachine<C> + Get,
    /// # {
    /// let linearizer = raft.get_read_linearizer(ReadPolicy::ReadIndex).await?;
    ///
    /// // `?` returns the `Fatal` error that stopped this node.
    /// linearizer.await_ready(raft).await?;
    ///
    /// // This node has applied the read log ID: reads of its state machine are now linearizable.
    /// let value = raft.with_state_machine(|sm| Box::pin(async move { sm.get("foo") })).await?;
    /// println!("foo: {value:?}");
    /// # Ok(())
    /// # }
    /// ```
    #[since(version = "0.10.2", change = "compares complete log IDs, not only indexes")]
    #[since(version = "0.10.0")]
    pub async fn await_ready<SM>(self, raft: &Raft<C, SM>) -> Result<LinearizeState<C>, Fatal<C>>
    where SM: RaftStateMachine<C> {
        let state_res = self.try_await_ready(raft, None).await?;
        // Safe unwrap: No timeout error.
        Ok(state_res.unwrap())
    }

    /// Waits for the state machine to apply all required log entries for linearizable reads.
    ///
    /// This method ensures linearizability by waiting for the state machine to apply all log
    /// entries up to [`read_log_id_on(this node)`](LinearizeState::read_log_id_on): the
    /// `read_log_id`, or only the no-op log ID for a relaxed linearizer on the node that issued it.
    ///
    /// The wait compares complete log IDs, which order by leader ID before index, so an applied
    /// entry of a newer leadership ends it even at a smaller index. The read log ID may name an
    /// uncommitted entry that a newer leader replaced.
    ///
    /// # Returns
    ///
    /// Returns `Ok(Ok(LinearizeState))` once `applied >= read_log_id_on(this node)`, indicating
    /// it's safe to perform linearizable reads.
    ///
    /// If `timeout` is provided and expires, returns `Ok(Err(LinearizeState))` where
    /// `applied < read_log_id_on(this node)`, indicating the read cannot be performed yet.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use std::time::Duration;
    /// # use openraft::Raft;
    /// # use openraft::RaftTypeConfig;
    /// # use openraft::storage::RaftStateMachine;
    /// use openraft::ReadPolicy;
    ///
    /// # trait Get {
    /// #     fn get(&self, key: &str) -> Option<String>;
    /// # }
    /// #
    /// # async fn example<C, SM>(raft: &Raft<C, SM>) -> Result<(), Box<dyn std::error::Error>>
    /// # where
    /// #     C: RaftTypeConfig,
    /// #     SM: RaftStateMachine<C> + Get,
    /// # {
    /// let linearizer = raft.get_read_linearizer(ReadPolicy::ReadIndex).await?;
    ///
    /// // `?` returns the `Fatal` error that stopped this node.
    /// match linearizer.try_await_ready(raft, Some(Duration::from_secs(1))).await? {
    ///     Ok(_) => {
    ///         // This node has applied the read log ID: reads of its state machine are now
    ///         // linearizable.
    ///         let value = raft
    ///             .with_state_machine(|sm| Box::pin(async move { sm.get("foo") }))
    ///             .await?;
    ///         println!("foo: {value:?}");
    ///     }
    ///     Err(state) => {
    ///         // The timeout expired before this node applied the read log ID, so its state
    ///         // machine may miss a write that completed before this read. Retry the read
    ///         // later, or report a timeout.
    ///         println!("not ready to read: {state}");
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    #[since(version = "0.10.2", change = "compares complete log IDs, not only indexes")]
    #[since(version = "0.10.0")]
    pub async fn try_await_ready<SM>(
        self,
        raft: &Raft<C, SM>,
        timeout: Option<Duration>,
    ) -> Result<Result<LinearizeState<C>, LinearizeState<C>>, Fatal<C>>
    where
        SM: RaftStateMachine<C>,
    {
        let this_id = raft.inner.id();

        if self.state.is_ready_on_node(this_id) {
            return Ok(Ok(self.state));
        }

        let expected = Some(self.state.read_log_id_on(this_id).log_id().clone());

        let res = raft.inner.wait(timeout).ge(Metric::Applied(expected), "Linearizer::try_await_ready").await;

        match res {
            Ok(metrics) => Ok(Ok(self.state.with_applied(this_id.clone(), metrics.last_applied))),
            Err(e) => match e {
                WaitError::Timeout(_, _) => {
                    let metrics_rx = raft.metrics();
                    let ref_metrics = metrics_rx.borrow_watched();
                    let applied = ref_metrics.last_applied.clone();

                    let state = self.state.with_applied(this_id.clone(), applied);
                    if state.is_ready_on_node(this_id) {
                        Ok(Ok(state))
                    } else {
                        Ok(Err(state))
                    }
                }
                WaitError::ShuttingDown => {
                    let err = raft.inner.get_core_stop_error().await;
                    Err(err)
                }
            },
        }
    }
}