Skip to content

Commit c47477d

Browse files
committed
Refactor: Remove inappropraite error log on quit
`RaftCore` should not output ERROR level log on a normal quit. Reduce ERROR level to INFO. - Fix: #1096
1 parent c508a35 commit c47477d

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

openraft/src/core/raft_core.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use crate::entry::RaftEntry;
4747
use crate::error::ClientWriteError;
4848
use crate::error::Fatal;
4949
use crate::error::ForwardToLeader;
50+
use crate::error::Infallible;
5051
use crate::error::InitializeError;
5152
use crate::error::QuorumNotEnough;
5253
use crate::error::RPCError;
@@ -220,34 +221,41 @@ where
220221
pub(crate) async fn main(
221222
mut self,
222223
rx_shutdown: <C::AsyncRuntime as AsyncRuntime>::OneshotReceiver<()>,
223-
) -> Result<(), Fatal<C::NodeId>> {
224+
) -> Result<Infallible, Fatal<C::NodeId>> {
224225
let span = tracing::span!(parent: &self.span, Level::DEBUG, "main");
225226
let res = self.do_main(rx_shutdown).instrument(span).await;
226227

227228
// Flush buffered metrics
228229
self.report_metrics(None);
229230

230-
tracing::info!("update the metrics for shutdown");
231+
// Safe unwrap: res is Result<Infallible, _>
232+
let err = res.unwrap_err();
233+
match err {
234+
Fatal::Stopped => { /* Normal quit */ }
235+
_ => {
236+
tracing::error!(error = display(&err), "quit RaftCore::main on error");
237+
}
238+
}
239+
240+
tracing::debug!("update the metrics for shutdown");
231241
{
232242
let mut curr = self.tx_metrics.borrow().clone();
233243
curr.state = ServerState::Shutdown;
234-
235-
if let Err(err) = &res {
236-
tracing::error!(?err, "quit RaftCore::main on error");
237-
curr.running_state = Err(err.clone());
238-
}
244+
curr.running_state = Err(err.clone());
239245

240246
let _ = self.tx_metrics.send(curr);
241247
}
242248

243-
res
249+
tracing::info!("RaftCore shutdown complete");
250+
251+
Err(err)
244252
}
245253

246254
#[tracing::instrument(level="trace", skip_all, fields(id=display(self.id), cluster=%self.config.cluster_name))]
247255
async fn do_main(
248256
&mut self,
249257
rx_shutdown: <C::AsyncRuntime as AsyncRuntime>::OneshotReceiver<()>,
250-
) -> Result<(), Fatal<C::NodeId>> {
258+
) -> Result<Infallible, Fatal<C::NodeId>> {
251259
tracing::debug!("raft node is initializing");
252260

253261
self.engine.startup();
@@ -900,11 +908,13 @@ where
900908
}
901909

902910
/// Run an event handling loop
911+
///
912+
/// It always returns a [`Fatal`] error upon returning.
903913
#[tracing::instrument(level="debug", skip_all, fields(id=display(self.id)))]
904914
async fn runtime_loop(
905915
&mut self,
906916
mut rx_shutdown: <C::AsyncRuntime as AsyncRuntime>::OneshotReceiver<()>,
907-
) -> Result<(), Fatal<C::NodeId>> {
917+
) -> Result<Infallible, Fatal<C::NodeId>> {
908918
// Ratio control the ratio of number of RaftMsg to process to number of Notify to process.
909919
let mut balancer = Balancer::new(10_000);
910920

openraft/src/raft/core_state.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::error::Fatal;
2+
use crate::error::Infallible;
23
use crate::AsyncRuntime;
34
use crate::NodeId;
45

@@ -9,8 +10,8 @@ where
910
A: AsyncRuntime,
1011
{
1112
/// The RaftCore task is still running.
12-
Running(A::JoinHandle<Result<(), Fatal<NID>>>),
13+
Running(A::JoinHandle<Result<Infallible, Fatal<NID>>>),
1314

1415
/// The RaftCore task has finished. The return value of the task is stored.
15-
Done(Result<(), Fatal<NID>>),
16+
Done(Result<Infallible, Fatal<NID>>),
1617
}

0 commit comments

Comments
 (0)