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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to