Skip to content

Commit

Permalink
Refactor: Remove inappropraite error log on quit
Browse files Browse the repository at this point in the history
`RaftCore` should not output ERROR level log on a normal quit.
Reduce ERROR level to INFO.

- Fix: #1096
  • Loading branch information
drmingdrmer committed Apr 9, 2024
1 parent c508a35 commit c47477d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
30 changes: 20 additions & 10 deletions openraft/src/core/raft_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use crate::entry::RaftEntry;
use crate::error::ClientWriteError;
use crate::error::Fatal;
use crate::error::ForwardToLeader;
use crate::error::Infallible;
use crate::error::InitializeError;
use crate::error::QuorumNotEnough;
use crate::error::RPCError;
Expand Down Expand Up @@ -220,34 +221,41 @@ where
pub(crate) async fn main(
mut self,
rx_shutdown: <C::AsyncRuntime as AsyncRuntime>::OneshotReceiver<()>,
) -> Result<(), Fatal<C::NodeId>> {
) -> Result<Infallible, Fatal<C::NodeId>> {
let span = tracing::span!(parent: &self.span, Level::DEBUG, "main");
let res = self.do_main(rx_shutdown).instrument(span).await;

// Flush buffered metrics
self.report_metrics(None);

tracing::info!("update the metrics for shutdown");
// Safe unwrap: res is Result<Infallible, _>
let err = res.unwrap_err();
match err {
Fatal::Stopped => { /* Normal quit */ }
_ => {
tracing::error!(error = display(&err), "quit RaftCore::main on error");
}
}

tracing::debug!("update the metrics for shutdown");
{
let mut curr = self.tx_metrics.borrow().clone();
curr.state = ServerState::Shutdown;

if let Err(err) = &res {
tracing::error!(?err, "quit RaftCore::main on error");
curr.running_state = Err(err.clone());
}
curr.running_state = Err(err.clone());

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

res
tracing::info!("RaftCore shutdown complete");

Err(err)
}

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

self.engine.startup();
Expand Down Expand Up @@ -900,11 +908,13 @@ where
}

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

Expand Down
5 changes: 3 additions & 2 deletions openraft/src/raft/core_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::error::Fatal;
use crate::error::Infallible;
use crate::AsyncRuntime;
use crate::NodeId;

Expand All @@ -9,8 +10,8 @@ where
A: AsyncRuntime,
{
/// The RaftCore task is still running.
Running(A::JoinHandle<Result<(), Fatal<NID>>>),
Running(A::JoinHandle<Result<Infallible, Fatal<NID>>>),

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

0 comments on commit c47477d

Please sign in to comment.