Example usage for org.apache.hadoop.yarn.api.records ApplicationAttemptReport getAMContainerId

List of usage examples for org.apache.hadoop.yarn.api.records ApplicationAttemptReport getAMContainerId

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.api.records ApplicationAttemptReport getAMContainerId.

Prototype

@Public
@Unstable
public abstract ContainerId getAMContainerId();

Source Link

Document

Get the ContainerId of AMContainer for this attempt

Usage

From source file:io.hops.hopsworks.common.admin.llap.LlapClusterFacade.java

License:Open Source License

public List<String> getLlapHosts() {
    ArrayList<String> hosts = new ArrayList<>();

    if (!isClusterUp() || isClusterStarting()) {
        return hosts;
    }/*from   w w  w .j av a  2  s  .  c  om*/

    // The cluster is app, so the appId exists
    String llapAppID = variablesFacade.getVariableValue(Settings.VARIABLE_LLAP_APP_ID);

    ApplicationId appId = ApplicationId.fromString(llapAppID);
    YarnClient yarnClient = yarnClientService.getYarnClientSuper(settings.getConfiguration()).getYarnClient();
    try {
        List<ApplicationAttemptReport> attempts = yarnClient.getApplicationAttempts(appId);
        ApplicationAttemptReport current = null;
        for (ApplicationAttemptReport attempt : attempts) {
            // Only if the app is running the metrics are available
            if (attempt.getYarnApplicationAttemptState() == YarnApplicationAttemptState.RUNNING) {
                current = attempt;
                break;
            }
        }

        if (current == null) {
            return hosts;
        }

        List<ContainerReport> containerReports = yarnClient.getContainers(current.getApplicationAttemptId());

        // For all the new/running containers, which are not the application master, get the host
        for (ContainerReport containerReport : containerReports) {
            // Only if the container is running the metrics are available
            if (containerReport.getContainerState() == ContainerState.RUNNING
                    && !containerReport.getContainerId().equals(current.getAMContainerId())) {
                hosts.add(containerReport.getAssignedNode().getHost());
            }
        }

    } catch (IOException | YarnException ex) {
        logger.log(Level.SEVERE, "Couldn't retrieve the containers for LLAP cluster", ex);
    } finally {
        try {
            yarnClient.close();
        } catch (IOException ex) {
        }
    }

    return hosts;
}