StephanEwen commented on a change in pull request #13574:
URL: https://github.com/apache/flink/pull/13574#discussion_r518153103



##########
File path: 
flink-core/src/main/java/org/apache/flink/util/ComponentClosingUtils.java
##########
@@ -0,0 +1,117 @@
+/*
+ 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.
+ */
+
+package org.apache.flink.util;
+
+import org.apache.flink.util.function.ThrowingRunnable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A util class to help with a clean component shutdown.
+ */
+public class ComponentClosingUtils {
+       private static final Logger LOG = 
LoggerFactory.getLogger(ComponentClosingUtils.class);
+       // A shared watchdog executor to handle the timeout closing.
+       private static final ScheduledExecutorService watchDog =
+                       
Executors.newSingleThreadScheduledExecutor((ThreadFactory) r -> {

Review comment:
       There is a utility in `flink-runtime` that has a similar purpose: 
`FutureUtils.Delayer`.
   Eventually consolidating these would make sense, I think.
   
   Can do this now, or file a follow-up ticket to consolidate these (should be 
feasible to move the FutureUtils and related classes to `flink-core`).

##########
File path: 
flink-core/src/main/java/org/apache/flink/util/ComponentClosingUtils.java
##########
@@ -0,0 +1,117 @@
+/*
+ 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.
+ */
+
+package org.apache.flink.util;
+
+import org.apache.flink.util.function.ThrowingRunnable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * A util class to help with a clean component shutdown.
+ */
+public class ComponentClosingUtils {
+       private static final Logger LOG = 
LoggerFactory.getLogger(ComponentClosingUtils.class);
+       // A shared watchdog executor to handle the timeout closing.
+       private static final ScheduledExecutorService watchDog =
+                       
Executors.newSingleThreadScheduledExecutor((ThreadFactory) r -> {
+                               Thread t = new Thread(r, 
"ComponentClosingUtil");
+                               t.setUncaughtExceptionHandler((thread, 
exception) -> {
+                                       LOG.error("FATAL: The component closing 
util thread caught exception ", exception);
+                                       System.exit(-17);
+                               });
+                               return t;
+                       });
+
+       private ComponentClosingUtils() {}
+
+       /**
+        * Close a component with a timeout.
+        *
+        * @param componentName the name of the component.
+        * @param closingSequence the closing logic which is a callable that 
can throw exceptions.
+        * @param closeTimeoutMs the timeout in milliseconds to waif for the 
component to close.
+        * @return An optional throwable which is non-empty if an error 
occurred when closing the component.
+        */
+       public static CompletableFuture<Void> closeAsyncWithTimeout(
+                       String componentName,
+                       ThrowingRunnable<Exception> closingSequence,
+                       long closeTimeoutMs) {
+               return closeAsyncWithTimeout(
+                               componentName,
+                               (Runnable) () -> {
+                                       try {
+                                               closingSequence.run();
+                                       } catch (Exception e) {
+                                               throw new 
ClosingException(componentName, e);
+                                       }
+                               }, closeTimeoutMs);
+       }
+
+       /**
+        * Close a component with a timeout.
+        *
+        * @param componentName the name of the component.
+        * @param closingSequence the closing logic.
+        * @param closeTimeoutMs the timeout in milliseconds to waif for the 
component to close.
+        * @return An optional throwable which is non-empty if an error 
occurred when closing the component.
+        */
+       public static CompletableFuture<Void> closeAsyncWithTimeout(

Review comment:
       Minor comment:
   
   I think it is slightly nicer to have a `ThrowingRunnable` here and handle 
the exception via try/catch in the thread, rather than with the 
UncaughtExceptionHandler. That saves some unchecked exception wrapping.
   
   But this is really a matter of taste, so feel free to keep it as it is if 
you prefer that variant.




----------------------------------------------------------------
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.

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


Reply via email to