You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/08/18 09:29:03 UTC

[GitHub] [arrow-ballista] mingmwang opened a new pull request, #151: Stop Executor Impl, Executor Graceful Shutdown

mingmwang opened a new pull request, #151:
URL: https://github.com/apache/arrow-ballista/pull/151

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   This PR covers below parts:
   1. Implement Stop Executor grpc call
   2. Graceful shutdown executor and related internal components.
       a). Graceful shutdown arrowflight and executor server grpc services.
       b). Graceful shutdown heartbeat loop, task runner loop and task status report loop
       c). Graceful shutdown and clean up shuffle data on the Executor
   
   3. Better error handling and propagation during Executor startup
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   # What changes are included in this PR?
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] thinkharderdev commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
thinkharderdev commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r948965815


##########
ballista/rust/executor/src/execution_loop.rs:
##########
@@ -108,7 +107,7 @@ pub async fn poll_loop<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan>
                 }
             }
             Err(error) => {
-                warn!("Executor registration failed. If this continues to happen the executor might be marked as dead by the scheduler. Error: {}", error);
+                warn!("Executor poll work loop failed. If this continues to happen the Scheduler might be marked as dead. Error: {}", error);

Review Comment:
   Not sure the second edit makes sense. Why would the scheduler be marked as dead?



##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },
+                    _ = shuffle_cleaner_shutdown.recv() => {
+                        if let Err(e) = clean_all_shuffle_data(&work_dir).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        } else {
+                            info!("Shuffle data cleaned.");
+                        }
+                        return;
+                    }
+                };
             }
         });
     }
 
+    let mut service_handlers: FuturesUnordered<JoinHandle<Result<(), BallistaError>>> =
+        FuturesUnordered::new();
+
+    // Channels used to receive stop requests from Executor grpc service.
+    let (stop_send, mut stop_recv) = mpsc::channel::<bool>(10);
+
     match scheduler_policy {
         TaskSchedulingPolicy::PushStaged => {
-            tokio::spawn(executor_server::startup(
-                scheduler,
-                executor.clone(),
-                default_codec,
-            ));
+            service_handlers.push(
+                //If there is executor registration error during startup, return the error and stop early.
+                executor_server::startup(
+                    scheduler,
+                    executor.clone(),
+                    default_codec,
+                    stop_send,
+                    &graceful,
+                )
+                .await?,
+            );
         }
         _ => {
-            tokio::spawn(execution_loop::poll_loop(
+            service_handlers.push(tokio::spawn(execution_loop::poll_loop(

Review Comment:
   Why does the poll loop not listen for shutdown signals?



##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -368,16 +425,31 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> TaskRunnerPool<T,
 
         //2. loop for task fetching and running
         let executor_server = self.executor_server.clone();
+        let mut task_runner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let task_runner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting the task runner pool");
+
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   ```suggestion
               // Capture `task_runner_complete`. This will be dropped when this background task returns, indicating that shutdown is complete
   ```



##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -310,25 +354,38 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> TaskRunnerPool<T,
         Self { executor_server }
     }
 
-    // There are two loop(future) running separately in tokio runtime.
-    // First is for sending back task status to scheduler
-    // Second is for receiving task from scheduler and run
-    async fn start(
+    fn start(
         &self,
         mut rx_task: mpsc::Receiver<TaskDefinition>,
         mut rx_task_status: mpsc::Receiver<TaskStatus>,
+        graceful: &Graceful,
     ) {
         //1. loop for task status reporting
         let executor_server = self.executor_server.clone();
+        let mut tasks_status_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let tasks_status_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting the task status reporter");
-            loop {
+
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   ```suggestion
               // Capture `task_status_complete`. This will be dropped when this background task returns, indicating that shutdown is complete
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] avantgardnerio commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949335643


##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },
+                    _ = shuffle_cleaner_shutdown.recv() => {
+                        if let Err(e) = clean_all_shuffle_data(&work_dir).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        } else {
+                            info!("Shuffle data cleaned.");
+                        }
+                        return;
+                    }
+                };
             }
         });
     }
 
+    let mut service_handlers: FuturesUnordered<JoinHandle<Result<(), BallistaError>>> =
+        FuturesUnordered::new();
+
+    // Channels used to receive stop requests from Executor grpc service.
+    let (stop_send, mut stop_recv) = mpsc::channel::<bool>(10);
+
     match scheduler_policy {
         TaskSchedulingPolicy::PushStaged => {
-            tokio::spawn(executor_server::startup(
-                scheduler,
-                executor.clone(),
-                default_codec,
-            ));
+            service_handlers.push(
+                //If there is executor registration error during startup, return the error and stop early.
+                executor_server::startup(
+                    scheduler,
+                    executor.clone(),
+                    default_codec,
+                    stop_send,
+                    &graceful,
+                )
+                .await?,
+            );
         }
         _ => {
-            tokio::spawn(execution_loop::poll_loop(
+            service_handlers.push(tokio::spawn(execution_loop::poll_loop(
                 scheduler,
                 executor.clone(),
                 default_codec,
-            ));
+            )));
         }
+    };
+    service_handlers.push(tokio::spawn(flight_server_run(
+        addr,
+        graceful.subscribe_for_shutdown(),
+    )));
+
+    // Concurrently run the service checking and listen for the `shutdown` signal and wait for the stop request coming.
+    // The check_services runs until an error is encountered, so under normal circumstances, this `select!` statement runs
+    // until the `shutdown` signal is received or a stop request is coming.
+    tokio::select! {
+        service_val = check_services(&mut service_handlers) => {
+             info!("services stopped with reason {:?}", service_val);
+        },
+        _ = signal::ctrl_c() => {

Review Comment:
   My main concern is that `crtl_c` gets called when the process receives a SIGINT on linux. This is how kubernetes/docker will ask a container politely to shutdown. From the docs it does appear SIGINT will trigger `ctrl_c` but I just thought I'd mention something here to see if you tried that @mingmwang .



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949776805


##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -289,18 +317,34 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> Heartbeater<T, U>
         Self { executor_server }
     }
 
-    async fn start(&self) {
+    fn start(&self, graceful: &Graceful) {
         let executor_server = self.executor_server.clone();
+        let mut heartbeat_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let heartbeat_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting heartbeater to send heartbeat the scheduler periodically");
-            loop {
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   Calling the drop explicitly is better in most case, but if the internal loop has complex branches, we will have to call the drop in all the possible branches which is error prone.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] andygrove commented on pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#issuecomment-1221803279

   Thanks @mingmwang for the work and thank you, @thinkharderdev @yahoNanJing and @avantgardnerio for reviewing. I will merge this now.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#issuecomment-1219289030

   @thinkharderdev @yahoNanJing @andygrove  Please help to take a look.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] avantgardnerio commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949339461


##########
ballista/rust/executor/src/graceful.rs:
##########
@@ -0,0 +1,58 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::shutdown::Shutdown;
+use tokio::sync::{broadcast, mpsc};
+
+pub struct Graceful {
+    /// Broadcasts a shutdown signal to all related components.
+    pub notify_shutdown: broadcast::Sender<()>,
+
+    /// Used as part of the graceful shutdown process to wait for
+    /// related components to complete processing.
+    ///
+    /// Tokio channels are closed once all `Sender` handles go out of scope.
+    /// When a channel is closed, the receiver receives `None`. This is
+    /// leveraged to detect all shutdown processing completing.
+    pub shutdown_complete_rx: mpsc::Receiver<()>,
+
+    pub shutdown_complete_tx: mpsc::Sender<()>,
+}
+
+impl Graceful {

Review Comment:
   `Graceful` is an adjective, and therefor not a great name. I would suggest `ShutdownNotifier` or similar.



##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -289,18 +317,34 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> Heartbeater<T, U>
         Self { executor_server }
     }
 
-    async fn start(&self) {
+    fn start(&self, graceful: &Graceful) {
         let executor_server = self.executor_server.clone();
+        let mut heartbeat_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let heartbeat_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting heartbeater to send heartbeat the scheduler periodically");
-            loop {
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   Maybe an explicit call to `drop` would be better in this case than a comment explaining that it will be dropped...



##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {

Review Comment:
   Superfluous scope?



##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },

Review Comment:
   There is some very funky formatting going on here. Could you run formatting on the sections that have been changed please? @andygrove any opposition to adding format checks to CI for Ballista?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] andygrove commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r951004236


##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },

Review Comment:
   I didn't realize that we were missing CI checks here. +1 for adding them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949744647


##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },
+                    _ = shuffle_cleaner_shutdown.recv() => {
+                        if let Err(e) = clean_all_shuffle_data(&work_dir).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        } else {
+                            info!("Shuffle data cleaned.");
+                        }
+                        return;
+                    }
+                };
             }
         });
     }
 
+    let mut service_handlers: FuturesUnordered<JoinHandle<Result<(), BallistaError>>> =
+        FuturesUnordered::new();
+
+    // Channels used to receive stop requests from Executor grpc service.
+    let (stop_send, mut stop_recv) = mpsc::channel::<bool>(10);
+
     match scheduler_policy {
         TaskSchedulingPolicy::PushStaged => {
-            tokio::spawn(executor_server::startup(
-                scheduler,
-                executor.clone(),
-                default_codec,
-            ));
+            service_handlers.push(
+                //If there is executor registration error during startup, return the error and stop early.
+                executor_server::startup(
+                    scheduler,
+                    executor.clone(),
+                    default_codec,
+                    stop_send,
+                    &graceful,
+                )
+                .await?,
+            );
         }
         _ => {
-            tokio::spawn(execution_loop::poll_loop(
+            service_handlers.push(tokio::spawn(execution_loop::poll_loop(

Review Comment:
   Good catch. The reason I didn't add the shutdown logic here is just because this poll loop is also used by standalone Executor, I'm not sure whether a standalone Executor need graceful shutdown or not.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] avantgardnerio commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
avantgardnerio commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r950287963


##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -289,18 +317,34 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> Heartbeater<T, U>
         Self { executor_server }
     }
 
-    async fn start(&self) {
+    fn start(&self, graceful: &Graceful) {
         let executor_server = self.executor_server.clone();
+        let mut heartbeat_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let heartbeat_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting heartbeater to send heartbeat the scheduler periodically");
-            loop {
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   That's a well-reasoned explanation, thank you :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949743190


##########
ballista/rust/executor/src/execution_loop.rs:
##########
@@ -108,7 +107,7 @@ pub async fn poll_loop<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan>
                 }
             }
             Err(error) => {
-                warn!("Executor registration failed. If this continues to happen the executor might be marked as dead by the scheduler. Error: {}", error);
+                warn!("Executor poll work loop failed. If this continues to happen the Scheduler might be marked as dead. Error: {}", error);

Review Comment:
   If I understand correctly, currently in the Scheduler side, there is no logic to mark an Executor dead or blacklist an Executor. And I think in a distributed system, if a remote RPC call failed,  callers usually suspect that the other part might has a problem.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949759864


##########
ballista/rust/executor/src/executor_server.rs:
##########
@@ -368,16 +425,31 @@ impl<T: 'static + AsLogicalPlan, U: 'static + AsExecutionPlan> TaskRunnerPool<T,
 
         //2. loop for task fetching and running
         let executor_server = self.executor_server.clone();
+        let mut task_runner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let task_runner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
             info!("Starting the task runner pool");
+
+            // Drop and notifies the receiver half that the shutdown is complete

Review Comment:
   I will take @avantgardnerio suggestion and call the drop explicitly.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#issuecomment-1221727706

   @alamb @andygrove 
   ]Please help to approve and merge the PR.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] andygrove merged pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
andygrove merged PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949756356


##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },
+                    _ = shuffle_cleaner_shutdown.recv() => {
+                        if let Err(e) = clean_all_shuffle_data(&work_dir).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        } else {
+                            info!("Shuffle data cleaned.");
+                        }
+                        return;
+                    }
+                };
             }
         });
     }
 
+    let mut service_handlers: FuturesUnordered<JoinHandle<Result<(), BallistaError>>> =
+        FuturesUnordered::new();
+
+    // Channels used to receive stop requests from Executor grpc service.
+    let (stop_send, mut stop_recv) = mpsc::channel::<bool>(10);
+
     match scheduler_policy {
         TaskSchedulingPolicy::PushStaged => {
-            tokio::spawn(executor_server::startup(
-                scheduler,
-                executor.clone(),
-                default_codec,
-            ));
+            service_handlers.push(
+                //If there is executor registration error during startup, return the error and stop early.
+                executor_server::startup(
+                    scheduler,
+                    executor.clone(),
+                    default_codec,
+                    stop_send,
+                    &graceful,
+                )
+                .await?,
+            );
         }
         _ => {
-            tokio::spawn(execution_loop::poll_loop(
+            service_handlers.push(tokio::spawn(execution_loop::poll_loop(
                 scheduler,
                 executor.clone(),
                 default_codec,
-            ));
+            )));
         }
+    };
+    service_handlers.push(tokio::spawn(flight_server_run(
+        addr,
+        graceful.subscribe_for_shutdown(),
+    )));
+
+    // Concurrently run the service checking and listen for the `shutdown` signal and wait for the stop request coming.
+    // The check_services runs until an error is encountered, so under normal circumstances, this `select!` statement runs
+    // until the `shutdown` signal is received or a stop request is coming.
+    tokio::select! {
+        service_val = check_services(&mut service_handlers) => {
+             info!("services stopped with reason {:?}", service_val);
+        },
+        _ = signal::ctrl_c() => {

Review Comment:
   https://hyper.rs/guides/server/graceful-shutdown/



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-ballista] mingmwang commented on a diff in pull request #151: Stop Executor Impl, Executor Graceful Shutdown

Posted by GitBox <gi...@apache.org>.
mingmwang commented on code in PR #151:
URL: https://github.com/apache/arrow-ballista/pull/151#discussion_r949746027


##########
ballista/rust/executor/src/main.rs:
##########
@@ -154,57 +162,156 @@ async fn main() -> Result<()> {
     let scheduler_policy = opt.task_scheduling_policy;
     let cleanup_ttl = opt.executor_cleanup_ttl;
 
+    // Graceful shutdown notification
+    let graceful = Graceful::new();
+
     if opt.executor_cleanup_enable {
         let mut interval_time =
             time::interval(Core_Duration::from_secs(opt.executor_cleanup_interval));
+        let mut shuffle_cleaner_shutdown = graceful.subscribe_for_shutdown();
+        // Not used directly.
+        let shuffle_cleaner_complete = graceful.shutdown_complete_tx.clone();
         tokio::spawn(async move {
-            loop {
-                interval_time.tick().await;
-                if let Err(e) =
-                    clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
-                {
-                    error!("Ballista executor fail to clean_shuffle_data {:?}", e)
-                }
+            // Drop and notifies the receiver half that the shutdown is complete
+            let _shuffle_cleaner_complete = shuffle_cleaner_complete;
+            // As long as the shutdown notification has not been received
+            while !shuffle_cleaner_shutdown.is_shutdown() {
+                tokio::select! {
+                    _ = interval_time.tick() => {
+                            if let Err(e) = clean_shuffle_data_loop(&work_dir, cleanup_ttl as i64).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        }
+                        },
+                    _ = shuffle_cleaner_shutdown.recv() => {
+                        if let Err(e) = clean_all_shuffle_data(&work_dir).await
+                        {
+                            error!("Ballista executor fail to clean_shuffle_data {:?}", e)
+                        } else {
+                            info!("Shuffle data cleaned.");
+                        }
+                        return;
+                    }
+                };
             }
         });
     }
 
+    let mut service_handlers: FuturesUnordered<JoinHandle<Result<(), BallistaError>>> =
+        FuturesUnordered::new();
+
+    // Channels used to receive stop requests from Executor grpc service.
+    let (stop_send, mut stop_recv) = mpsc::channel::<bool>(10);
+
     match scheduler_policy {
         TaskSchedulingPolicy::PushStaged => {
-            tokio::spawn(executor_server::startup(
-                scheduler,
-                executor.clone(),
-                default_codec,
-            ));
+            service_handlers.push(
+                //If there is executor registration error during startup, return the error and stop early.
+                executor_server::startup(
+                    scheduler,
+                    executor.clone(),
+                    default_codec,
+                    stop_send,
+                    &graceful,
+                )
+                .await?,
+            );
         }
         _ => {
-            tokio::spawn(execution_loop::poll_loop(
+            service_handlers.push(tokio::spawn(execution_loop::poll_loop(
                 scheduler,
                 executor.clone(),
                 default_codec,
-            ));
+            )));
         }
+    };
+    service_handlers.push(tokio::spawn(flight_server_run(
+        addr,
+        graceful.subscribe_for_shutdown(),
+    )));
+
+    // Concurrently run the service checking and listen for the `shutdown` signal and wait for the stop request coming.
+    // The check_services runs until an error is encountered, so under normal circumstances, this `select!` statement runs
+    // until the `shutdown` signal is received or a stop request is coming.
+    tokio::select! {
+        service_val = check_services(&mut service_handlers) => {
+             info!("services stopped with reason {:?}", service_val);
+        },
+        _ = signal::ctrl_c() => {

Review Comment:
   I did not get chance to test it on kubernetes/docker containers yet. I see that some other systems have the similar logic. Let me do some test and will get back on this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org