Skip to content

Commit

Permalink
Fix: add_learner() should block forever
Browse files Browse the repository at this point in the history
The `Raft::add_learner()` method, when invoked with the `blocking`
parameter set to `true`, should block forever until the learner
synchronizes its logs with the leader.

In its current implementation, `add_learner()` calls the `Raft::wait()`
method, which has a default timeout of `500ms`. To achieve the desired
blocking behavior, the default timeout should be increased
significantly.

- Fix: #846
  • Loading branch information
drmingdrmer committed May 20, 2023
1 parent 39b57c4 commit f505d7e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
7 changes: 5 additions & 2 deletions openraft/src/raft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ where
///
/// If the node to add is already a voter or learner, it will still re-add it.
///
/// A `node` stores the network address of a node. Thus an application does not
/// A `node` is able to store the network address of a node. Thus an application does not
/// need another store for mapping node-id to ip-addr when implementing the RaftNetwork.
#[tracing::instrument(level = "debug", skip(self, id), fields(target=display(id)))]
pub async fn add_learner(
Expand Down Expand Up @@ -816,6 +816,9 @@ where

/// Get a handle to wait for the metrics to satisfy some condition.
///
/// If `timeout` is `None`, then it will wait forever(10 years).
/// If `timeout` is `Some`, then it will wait for the specified duration.
///
/// ```ignore
/// # use std::time::Duration;
/// # use openraft::{State, Raft};
Expand All @@ -834,7 +837,7 @@ where
pub fn wait(&self, timeout: Option<Duration>) -> Wait<C::NodeId, C::Node> {
let timeout = match timeout {
Some(t) => t,
None => Duration::from_millis(500),
None => Duration::from_secs(86400 * 365 * 100),
};
Wait {
timeout,
Expand Down
13 changes: 9 additions & 4 deletions tests/tests/membership/t30_commit_joint_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::sync::Arc;
use std::time::Duration;

use anyhow::Result;
use futures::stream::StreamExt;
Expand Down Expand Up @@ -36,7 +37,7 @@ async fn commit_joint_config_during_0_to_012() -> Result<()> {
// Assert all nodes are in learner state & have no entries.
let mut log_index = 1;

router.wait_for_log(&btreeset![0], Some(log_index), None, "init node 0").await?;
router.wait_for_log(&btreeset![0], Some(log_index), timeout(), "init node 0").await?;

// Sync some new nodes.
router.new_raft_node(1).await;
Expand All @@ -51,7 +52,7 @@ async fn commit_joint_config_during_0_to_012() -> Result<()> {
}
log_index += 2;

router.wait_for_log(&btreeset![0], Some(log_index), None, "init node 0").await?;
router.wait_for_log(&btreeset![0], Some(log_index), timeout(), "init node 0").await?;

tracing::info!(
log_index,
Expand All @@ -76,7 +77,7 @@ async fn commit_joint_config_during_0_to_012() -> Result<()> {
.wait_for_metrics(
&0,
|x| x.last_applied.index() > Some(log_index),
None,
timeout(),
"the next joint log should not commit",
)
.await;
Expand Down Expand Up @@ -134,10 +135,14 @@ async fn commit_joint_config_during_012_to_234() -> Result<()> {
}
log_index += 2;

let wait_rst = router.wait_for_log(&btreeset![0], Some(log_index), None, "cluster of joint").await;
let wait_rst = router.wait_for_log(&btreeset![0], Some(log_index), timeout(), "cluster of joint").await;

// the first step of joint should not pass because the new config can not constitute a quorum
assert!(wait_rst.is_err());

Ok(())
}

fn timeout() -> Option<Duration> {
Some(Duration::from_millis(1_000))
}

0 comments on commit f505d7e

Please sign in to comment.