@Override public List<ContainerReport> getContainers(ApplicationAttemptId applicationAttemptId) throws YarnException, IOException { List<ContainerReport> containersForAttempt = new ArrayList<ContainerReport>(); boolean appNotFoundInRM = false; try { GetContainersRequest request = Records.newRecord(GetContainersRequest.class); request.setApplicationAttemptId(applicationAttemptId); GetContainersResponse response = rmClient.getContainers(request); containersForAttempt.addAll(response.getContainerList()); } catch (YarnException e) { if (e.getClass() != ApplicationNotFoundException.class || !historyServiceEnabled) { // If Application is not in RM and history service is enabled then we // need to check with history service else throw exception. throw e; } appNotFoundInRM = true; } if (historyServiceEnabled) { // Check with AHS even if found in RM because to capture info of finished // containers also List<ContainerReport> containersListFromAHS = null; try { containersListFromAHS = historyClient.getContainers(applicationAttemptId); } catch (IOException e) { // History service access might be enabled but system metrics publisher // is disabled hence app not found exception is possible if (appNotFoundInRM) { // app not found in bothM and RM then propagate the exception. throw e; } } if (null != containersListFromAHS && containersListFromAHS.size() > 0) { // remove duplicates Set<ContainerId> containerIdsToBeKeptFromAHS = new HashSet<ContainerId>(); Iterator<ContainerReport> tmpItr = containersListFromAHS.iterator(); while (tmpItr.hasNext()) { containerIdsToBeKeptFromAHS.add(tmpItr.next().getContainerId()); } Iterator<ContainerReport> rmContainers = containersForAttempt.iterator(); while (rmContainers.hasNext()) { ContainerReport tmp = rmContainers.next(); containerIdsToBeKeptFromAHS.remove(tmp.getContainerId()); // Remove containers from AHS as container from RM will have latest // information } if (containerIdsToBeKeptFromAHS.size() > 0 && containersListFromAHS.size() != containerIdsToBeKeptFromAHS.size()) { Iterator<ContainerReport> containersFromHS = containersListFromAHS.iterator(); while (containersFromHS.hasNext()) { ContainerReport containerReport = containersFromHS.next(); if (containerIdsToBeKeptFromAHS.contains(containerReport.getContainerId())) { containersForAttempt.add(containerReport); } } } else if (containersListFromAHS.size() == containerIdsToBeKeptFromAHS.size()) { containersForAttempt.addAll(containersListFromAHS); } } } return containersForAttempt; }
@Override public List<ContainerReport> getContainers(ApplicationAttemptId appAttemptId) throws YarnException, IOException { when(mockContainersResponse.getContainerList()).thenReturn(getContainersReport(appAttemptId)); return super.getContainers(appAttemptId); }