Skip to content

Commit

Permalink
refactor: graceful shutdown on meta node & unify election path (#17608)
Browse files Browse the repository at this point in the history
Signed-off-by: Bugen Zhao <[email protected]>
  • Loading branch information
BugenZhao authored Jul 10, 2024
1 parent 4d34bd3 commit 0987e27
Show file tree
Hide file tree
Showing 13 changed files with 244 additions and 278 deletions.
3 changes: 1 addition & 2 deletions src/cmd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ pub fn compute(opts: ComputeNodeOpts) -> ! {

pub fn meta(opts: MetaNodeOpts) -> ! {
init_risingwave_logger(LoggerSettings::from_opts(&opts));
// TODO(shutdown): pass the shutdown token
main_okk(|_| risingwave_meta_node::start(opts));
main_okk(|shutdown| risingwave_meta_node::start(opts, shutdown));
}

pub fn frontend(opts: FrontendOpts) -> ! {
Expand Down
3 changes: 2 additions & 1 deletion src/cmd_all/src/standalone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ pub async fn standalone(
is_in_memory = matches!(opts.backend, Some(MetaBackend::Mem));
tracing::info!("starting meta-node thread with cli args: {:?}", opts);

let shutdown = shutdown.clone();
let _meta_handle = tokio::spawn(async move {
let dangerous_max_idle_secs = opts.dangerous_max_idle_secs;
risingwave_meta_node::start(opts).await;
risingwave_meta_node::start(opts, shutdown).await;
tracing::warn!("meta is stopped, shutdown all nodes");
if let Some(idle_exit_secs) = dangerous_max_idle_secs {
eprintln!("{}",
Expand Down
42 changes: 7 additions & 35 deletions src/meta/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use redact::Secret;
use risingwave_common::config::OverrideConfig;
use risingwave_common::util::meta_addr::MetaAddressStrategy;
use risingwave_common::util::resource_util;
use risingwave_common::util::tokio_util::sync::CancellationToken;
use risingwave_common::{GIT_SHA, RW_VERSION};
use risingwave_common_heap_profiling::HeapProfiler;
use risingwave_meta::*;
Expand Down Expand Up @@ -204,7 +205,10 @@ use risingwave_common::config::{load_config, MetaBackend, RwConfig};
use tracing::info;

/// Start meta node
pub fn start(opts: MetaNodeOpts) -> Pin<Box<dyn Future<Output = ()> + Send>> {
pub fn start(
opts: MetaNodeOpts,
shutdown: CancellationToken,
) -> Pin<Box<dyn Future<Output = ()> + Send>> {
// WARNING: don't change the function signature. Making it `async fn` will cause
// slow compile in release mode.
Box::pin(async move {
Expand Down Expand Up @@ -324,7 +328,7 @@ pub fn start(opts: MetaNodeOpts) -> Pin<Box<dyn Future<Output = ()> + Send>> {
max_timeout_ms / 1000
} + MIN_TIMEOUT_INTERVAL_SEC;

let (mut join_handle, leader_lost_handle, shutdown_send) = rpc_serve(
rpc_serve(
add_info,
backend,
max_heartbeat_interval,
Expand Down Expand Up @@ -428,42 +432,10 @@ pub fn start(opts: MetaNodeOpts) -> Pin<Box<dyn Future<Output = ()> + Send>> {
},
config.system.into_init_system_params(),
Default::default(),
shutdown,
)
.await
.unwrap();

tracing::info!("Meta server listening at {}", listen_addr);

match leader_lost_handle {
None => {
tokio::select! {
_ = tokio::signal::ctrl_c() => {
tracing::info!("receive ctrl+c");
shutdown_send.send(()).unwrap();
join_handle.await.unwrap()
}
res = &mut join_handle => res.unwrap(),
};
}
Some(mut handle) => {
tokio::select! {
_ = &mut handle => {
tracing::info!("receive leader lost signal");
// When we lose leadership, we will exit as soon as possible.
}
_ = tokio::signal::ctrl_c() => {
tracing::info!("receive ctrl+c");
shutdown_send.send(()).unwrap();
join_handle.await.unwrap();
handle.abort();
}
res = &mut join_handle => {
res.unwrap();
handle.abort();
},
};
}
};
})
}

Expand Down
Loading

0 comments on commit 0987e27

Please sign in to comment.