asfgit closed pull request #7414: [FLINK-11253] Add shutdown hook for yarn session client in attached mode URL: https://github.com/apache/flink/pull/7414
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/flink-yarn/src/main/java/org/apache/flink/yarn/cli/FlinkYarnSessionCli.java b/flink-yarn/src/main/java/org/apache/flink/yarn/cli/FlinkYarnSessionCli.java index 931b22c0373..8c8992a2c87 100644 --- a/flink-yarn/src/main/java/org/apache/flink/yarn/cli/FlinkYarnSessionCli.java +++ b/flink-yarn/src/main/java/org/apache/flink/yarn/cli/FlinkYarnSessionCli.java @@ -40,6 +40,7 @@ import org.apache.flink.util.ExecutorUtils; import org.apache.flink.util.FlinkException; import org.apache.flink.util.Preconditions; +import org.apache.flink.util.ShutdownHookUtil; import org.apache.flink.yarn.AbstractYarnClusterDescriptor; import org.apache.flink.yarn.YarnClusterDescriptor; import org.apache.flink.yarn.configuration.YarnConfigOptions; @@ -648,48 +649,22 @@ public int run(String[] args) throws CliArgsException, FlinkException { yarnClusterDescriptor.getYarnClient(), yarnApplicationId, new ScheduledExecutorServiceAdapter(scheduledExecutorService)); - + Thread shutdownHook = ShutdownHookUtil.addShutdownHook( + () -> shutdownCluster(yarnClusterDescriptor, clusterClient, scheduledExecutorService, + yarnApplicationStatusMonitor, yarnApplicationId), + getClass().getSimpleName(), + LOG); try { runInteractiveCli( clusterClient, yarnApplicationStatusMonitor, acceptInteractiveInput); } finally { - try { - yarnApplicationStatusMonitor.close(); - } catch (Exception e) { - LOG.info("Could not properly close the Yarn application status monitor.", e); - } - - clusterClient.shutDownCluster(); - - try { - clusterClient.shutdown(); - } catch (Exception e) { - LOG.info("Could not properly shutdown cluster client.", e); - } - - // shut down the scheduled executor service - ExecutorUtils.gracefulShutdown( - 1000L, - TimeUnit.MILLISECONDS, - scheduledExecutorService); - - deleteYarnPropertiesFile(); - - ApplicationReport applicationReport; - - try { - applicationReport = yarnClusterDescriptor - .getYarnClient() - .getApplicationReport(yarnApplicationId); - } catch (YarnException | IOException e) { - LOG.info("Could not log the final application report.", e); - applicationReport = null; - } - - if (applicationReport != null) { - logFinalApplicationReport(applicationReport); + shutdownCluster(yarnClusterDescriptor, clusterClient, scheduledExecutorService, + yarnApplicationStatusMonitor, yarnApplicationId); + if (shutdownHook != null) { + // we do not need the hook anymore as we have just tried to shutdown the cluster. + ShutdownHookUtil.removeShutdownHook(shutdownHook, getClass().getSimpleName(), LOG); } } } @@ -705,6 +680,47 @@ public int run(String[] args) throws CliArgsException, FlinkException { return 0; } + private void shutdownCluster(AbstractYarnClusterDescriptor yarnClusterDescriptor, + ClusterClient clusterClient, ScheduledExecutorService scheduledExecutorService, + YarnApplicationStatusMonitor yarnApplicationStatusMonitor, ApplicationId yarnApplicationId) { + try { + yarnApplicationStatusMonitor.close(); + } catch (Exception e) { + LOG.info("Could not properly close the Yarn application status monitor.", e); + } + + clusterClient.shutDownCluster(); + + try { + clusterClient.shutdown(); + } catch (Exception e) { + LOG.info("Could not properly shutdown cluster client.", e); + } + + // shut down the scheduled executor service + ExecutorUtils.gracefulShutdown( + 1000L, + TimeUnit.MILLISECONDS, + scheduledExecutorService); + + deleteYarnPropertiesFile(); + + ApplicationReport applicationReport; + + try { + applicationReport = yarnClusterDescriptor + .getYarnClient() + .getApplicationReport(yarnApplicationId); + } catch (YarnException | IOException e) { + LOG.info("Could not log the final application report.", e); + applicationReport = null; + } + + if (applicationReport != null) { + logFinalApplicationReport(applicationReport); + } + } + private void logFinalApplicationReport(ApplicationReport appReport) { LOG.info("Application " + appReport.getApplicationId() + " finished with state " + appReport .getYarnApplicationState() + " and final state " + appReport ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services