Skip to content

Commit f505d7e

Browse files
committed
Fix: add_learner() should block forever
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
1 parent 39b57c4 commit f505d7e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

openraft/src/raft.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ where
513513
///
514514
/// If the node to add is already a voter or learner, it will still re-add it.
515515
///
516-
/// A `node` stores the network address of a node. Thus an application does not
516+
/// A `node` is able to store the network address of a node. Thus an application does not
517517
/// need another store for mapping node-id to ip-addr when implementing the RaftNetwork.
518518
#[tracing::instrument(level = "debug", skip(self, id), fields(target=display(id)))]
519519
pub async fn add_learner(
@@ -816,6 +816,9 @@ where
816816

817817
/// Get a handle to wait for the metrics to satisfy some condition.
818818
///
819+
/// If `timeout` is `None`, then it will wait forever(10 years).
820+
/// If `timeout` is `Some`, then it will wait for the specified duration.
821+
///
819822
/// ```ignore
820823
/// # use std::time::Duration;
821824
/// # use openraft::{State, Raft};
@@ -834,7 +837,7 @@ where
834837
pub fn wait(&self, timeout: Option<Duration>) -> Wait<C::NodeId, C::Node> {
835838
let timeout = match timeout {
836839
Some(t) => t,
837-
None => Duration::from_millis(500),
840+
None => Duration::from_secs(86400 * 365 * 100),
838841
};
839842
Wait {
840843
timeout,

tests/tests/membership/t30_commit_joint_config.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::sync::Arc;
2+
use std::time::Duration;
23

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

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

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

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

5657
tracing::info!(
5758
log_index,
@@ -76,7 +77,7 @@ async fn commit_joint_config_during_0_to_012() -> Result<()> {
7677
.wait_for_metrics(
7778
&0,
7879
|x| x.last_applied.index() > Some(log_index),
79-
None,
80+
timeout(),
8081
"the next joint log should not commit",
8182
)
8283
.await;
@@ -134,10 +135,14 @@ async fn commit_joint_config_during_012_to_234() -> Result<()> {
134135
}
135136
log_index += 2;
136137

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

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

142143
Ok(())
143144
}
145+
146+
fn timeout() -> Option<Duration> {
147+
Some(Duration::from_millis(1_000))
148+
}

0 commit comments

Comments
 (0)