diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ClientRMService.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ClientRMService.java index 6c37b7e9c0..74ecec33d0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ClientRMService.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/ClientRMService.java @@ -899,6 +899,9 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request) String name = request.getName(); final Map apps = rmContext.getRMApps(); + final Set runningAppsFilteredByQueues = + getRunningAppsFilteredByQueues(apps, queues); + Iterator appsIter = apps.values().iterator(); List reports = new ArrayList(); @@ -912,17 +915,8 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request) } if (queues != null && !queues.isEmpty()) { - Map> foundApps = queryApplicationsByQueues(apps, queues); - List runningAppsByQueues = foundApps.entrySet().stream() - .filter(e -> queues.contains(e.getKey())) - .map(Map.Entry::getValue) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - List runningAppsById = runningAppsByQueues.stream() - .filter(app -> app.getApplicationId().equals(application.getApplicationId())) - .collect(Collectors.toList()); - - if (runningAppsById.isEmpty() && !queues.contains(application.getQueue())) { + if (!runningAppsFilteredByQueues.contains(application.getApplicationId()) && + !queues.contains(application.getQueue())) { continue; } } @@ -1001,20 +995,21 @@ public GetApplicationsResponse getApplications(GetApplicationsRequest request) return response; } - private Map> queryApplicationsByQueues( + private Set getRunningAppsFilteredByQueues( Map apps, Set queues) { - final Map> appsToQueues = new HashMap<>(); + final Set runningApps = new HashSet<>(); for (String queue : queues) { List appsInQueue = scheduler.getAppsInQueue(queue); - if (appsInQueue != null && !appsInQueue.isEmpty()) { + if (appsInQueue != null) { for (ApplicationAttemptId appAttemptId : appsInQueue) { RMApp rmApp = apps.get(appAttemptId.getApplicationId()); - appsToQueues.putIfAbsent(queue, new ArrayList<>()); - appsToQueues.get(queue).add(rmApp); + if (rmApp != null) { + runningApps.add(rmApp.getApplicationId()); + } } } } - return appsToQueues; + return runningApps; } private Set getLowerCasedAppTypes(GetApplicationsRequest request) {