From 2f752830ba74c90ccce818d687572db9afded25b Mon Sep 17 00:00:00 2001 From: Yufei Gu Date: Mon, 1 Apr 2019 20:05:15 -0700 Subject: [PATCH] YARN-9214. Add AbstractYarnScheduler#getValidQueues method to remove duplication. Contributed by Wanqiang Ji. --- .../scheduler/AbstractYarnScheduler.java | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java index 92dde949647..5168b34d6c1 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/AbstractYarnScheduler.java @@ -771,16 +771,9 @@ public void moveAllApps(String sourceQueue, String destQueue) LOG.warn(e.toString()); throw new YarnException(e); } - // check if source queue is a valid - List apps = getAppsInQueue(sourceQueue); - if (apps == null) { - String errMsg = - "The specified Queue: " + sourceQueue + " doesn't exist"; - LOG.warn(errMsg); - throw new YarnException(errMsg); - } + // generate move events for each pending/running app - for (ApplicationAttemptId appAttemptId : apps) { + for (ApplicationAttemptId appAttemptId : getAppsFromQueue(sourceQueue)) { this.rmContext.getDispatcher().getEventHandler() .handle(new RMAppManagerEvent(appAttemptId.getApplicationId(), destQueue, RMAppManagerEventType.APP_MOVE)); @@ -795,15 +788,8 @@ public void killAllAppsInQueue(String queueName) throws YarnException { writeLock.lock(); try { - // check if queue is a valid - List apps = getAppsInQueue(queueName); - if (apps == null) { - String errMsg = "The specified Queue: " + queueName + " doesn't exist"; - LOG.warn(errMsg); - throw new YarnException(errMsg); - } // generate kill events for each pending/running app - for (ApplicationAttemptId app : apps) { + for (ApplicationAttemptId app : getAppsFromQueue(queueName)) { this.rmContext.getDispatcher().getEventHandler().handle( new RMAppEvent(app.getApplicationId(), RMAppEventType.KILL, "Application killed due to expiry of reservation queue " @@ -1529,4 +1515,28 @@ public boolean attemptAllocationOnNode(SchedulerApplicationAttempt appAttempt, public void resetSchedulerMetrics() { // reset scheduler metrics } + + /** + * Gets the apps from a given queue. + * + * Mechanics: + * 1. Get all {@link ApplicationAttemptId}s in the given queue by + * {@link #getAppsInQueue(String)} method. + * 2. Always need to check validity for the given queue by the returned + * values. + * + * @param queueName queue name + * @return a collection of app attempt ids in the given queue, it maybe empty. + * @throws YarnException if {@link #getAppsInQueue(String)} return null, will + * throw this exception. + */ + private List getAppsFromQueue(String queueName) + throws YarnException { + List apps = getAppsInQueue(queueName); + if (apps == null) { + throw new YarnException("The specified queue: " + queueName + + " doesn't exist"); + } + return apps; + } }