Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #2227]💫Implement broker graceful shutdown🧑‍💻 #2228

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions rocketmq-broker/src/broker_bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

use log::info;
use rocketmq_common::common::broker::broker_config::BrokerConfig;
use rocketmq_common::common::server::config::ServerConfig;
use rocketmq_rust::wait_for_signal;
use rocketmq_store::config::message_store_config::MessageStoreConfig;
use tracing::error;

Expand All @@ -32,7 +33,10 @@
error!("initialize fail");
return;
}
let (_start_result, _ctrl_c) = tokio::join!(self.start(), tokio::signal::ctrl_c());
let (shutdown_tx, shutdown_rx) = tokio::sync::broadcast::channel(1);
self.broker_runtime.shutdown_rx = Some(shutdown_rx);

tokio::join!(self.start(), wait_for_signal_inner(shutdown_tx));

Check warning on line 39 in rocketmq-broker/src/broker_bootstrap.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_bootstrap.rs#L36-L39

Added lines #L36 - L39 were not covered by tests
}

async fn initialize(&mut self) -> bool {
Expand All @@ -44,6 +48,16 @@
}
}

async fn wait_for_signal_inner(shutdown_tx: tokio::sync::broadcast::Sender<()>) {
tokio::select! {
_ = wait_for_signal() => {
info!("Broker Received signal, initiating shutdown...");

Check warning on line 54 in rocketmq-broker/src/broker_bootstrap.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_bootstrap.rs#L51-L54

Added lines #L51 - L54 were not covered by tests
}
}
// Send shutdown signal to all tasks
let _ = shutdown_tx.send(());
}

Check warning on line 59 in rocketmq-broker/src/broker_bootstrap.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_bootstrap.rs#L58-L59

Added lines #L58 - L59 were not covered by tests

pub struct Builder {
broker_config: BrokerConfig,
message_store_config: MessageStoreConfig,
Expand Down
14 changes: 12 additions & 2 deletions rocketmq-broker/src/broker_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
broker_runtime: Option<RocketMQRuntime>,
shutdown: Arc<AtomicBool>,
shutdown_hook: Option<BrokerShutdownHook>,
// receiver for shutdown signal
pub(crate) shutdown_rx: Option<tokio::sync::broadcast::Receiver<()>>,
}

impl BrokerRuntime {
Expand Down Expand Up @@ -240,6 +242,7 @@
broker_runtime: Some(runtime),
shutdown: Arc::new(AtomicBool::new(false)),
shutdown_hook: None,
shutdown_rx: None,

Check warning on line 245 in rocketmq-broker/src/broker_runtime.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_runtime.rs#L245

Added line #L245 was not covered by tests
}
}

Expand All @@ -265,9 +268,9 @@
pull_request_hold_service.shutdown();
}

/* if let Some(runtime) = self.broker_runtime.take() {
if let Some(runtime) = self.broker_runtime.take() {

Check warning on line 271 in rocketmq-broker/src/broker_runtime.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_runtime.rs#L271

Added line #L271 was not covered by tests
runtime.shutdown();
}*/
}

Check warning on line 273 in rocketmq-broker/src/broker_runtime.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_runtime.rs#L273

Added line #L273 was not covered by tests
}

pub(crate) fn shutdown_basic_service(&mut self) {
Expand Down Expand Up @@ -913,6 +916,13 @@
"Rocketmq Broker({} ----Rust) start success",
self.inner.broker_config.broker_identity.broker_name
);
tokio::select! {
_ = self.shutdown_rx.as_mut().unwrap().recv() => {
info!("Broker Shutdown received, initiating graceful shutdown...");
self.shutdown();
info!("Broker Shutdown complete");

Check warning on line 923 in rocketmq-broker/src/broker_runtime.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-broker/src/broker_runtime.rs#L919-L923

Added lines #L919 - L923 were not covered by tests
}
}
}

pub(crate) fn schedule_send_heartbeat(&mut self) {}
Expand Down
Loading