diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-slider/hadoop-yarn-slider-core/src/main/java/org/apache/slider/client/SliderClient.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-slider/hadoop-yarn-slider-core/src/main/java/org/apache/slider/client/SliderClient.java index 29ca471c22..a3ba8c0485 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-slider/hadoop-yarn-slider-core/src/main/java/org/apache/slider/client/SliderClient.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-applications/hadoop-yarn-slider/hadoop-yarn-slider-core/src/main/java/org/apache/slider/client/SliderClient.java @@ -230,6 +230,9 @@ public class SliderClient extends AbstractSliderLaunchedService implements RunSe @SuppressWarnings("FieldAccessedSynchronizedAndUnsynchronized") private RegistryOperations registryOperations; + private static EnumSet terminatedStates = EnumSet + .of(YarnApplicationState.FINISHED, YarnApplicationState.FAILED, + YarnApplicationState.KILLED); /** * Constructor */ @@ -699,7 +702,7 @@ private ApplicationId submitApp(Application app) //TODO set retry window submissionContext.setResource(Resource.newInstance( conf.getLong(KEY_AM_RESOURCE_MEM, DEFAULT_KEY_AM_RESOURCE_MEM), 1)); - submissionContext.setQueue(conf.get(KEY_YARN_QUEUE, DEFAULT_YARN_QUEUE)); + submissionContext.setQueue(conf.get(KEY_YARN_QUEUE, app.getQueue())); submissionContext.setApplicationName(appName); submissionContext.setApplicationType(SliderKeys.APP_TYPE); Set appTags = @@ -1725,9 +1728,8 @@ public int actionStop(String appName, ActionFreezeArgs freezeArgs) "Application " + appName + " doesn't exist in RM."); } - if (app.getYarnApplicationState().ordinal() >= YarnApplicationState.FINISHED - .ordinal()) { - log.info("Application {} is in a terminated state {}", appName, + if (terminatedStates.contains(app.getYarnApplicationState())) { + log.info("Application {} is already in a terminated state {}", appName, app.getYarnApplicationState()); return EXIT_SUCCESS; } @@ -1738,8 +1740,30 @@ public int actionStop(String appName, ActionFreezeArgs freezeArgs) Messages.StopClusterRequestProto.newBuilder() .setMessage(freezeArgs.message).build(); appMaster.stopCluster(r); - log.info("Application " + appName + " is gracefully stopped."); - } catch (IOException | YarnException e){ + log.info("Application " + appName + " is being gracefully stopped..."); + long startTime = System.currentTimeMillis(); + int pollCount = 0; + while (true) { + Thread.sleep(200); + ApplicationReport report = + yarnClient.getApplicationReport(app.getApplicationId()); + if (terminatedStates.contains(report.getYarnApplicationState())) { + log.info("Application " + appName + " is stopped."); + break; + } + // kill after 10 seconds. + if ((System.currentTimeMillis() - startTime) > 10000) { + log.info("Stop operation timeout stopping, forcefully kill the app " + + appName); + yarnClient + .killApplication(app.getApplicationId(), freezeArgs.message); + break; + } + if (++pollCount % 10 == 0) { + log.info("Waiting for application " + appName + " to be stopped."); + } + } + } catch (IOException | YarnException | InterruptedException e) { log.info("Failed to stop " + appName + " gracefully, forcefully kill the app."); yarnClient.killApplication(app.getApplicationId(), freezeArgs.message);