Example usage for org.apache.hadoop.yarn.api.records ApplicationReport getYarnApplicationState

List of usage examples for org.apache.hadoop.yarn.api.records ApplicationReport getYarnApplicationState

Introduction

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

Prototype

@Public
@Stable
public abstract YarnApplicationState getYarnApplicationState();

Source Link

Document

Get the YarnApplicationState of the application.

Usage

From source file:org.dknight.app.UnmanagedAMLauncher.java

License:Apache License

/**
 * Monitor the submitted application for completion. Kill application if time
 * expires./*from w  w  w .j a va  2  s  .  c om*/
 *
 * @param appId
 *          Application Id of application to be monitored
 * @return true if application completed successfully
 * @throws YarnException
 * @throws IOException
 */
private ApplicationReport monitorApplication(ApplicationId appId, Set<YarnApplicationState> finalState)
        throws YarnException, IOException {

    long foundAMCompletedTime = 0;
    final int timeToWaitMS = 10000;
    StringBuilder expectedFinalState = new StringBuilder();
    boolean first = true;
    for (YarnApplicationState state : finalState) {
        if (first) {
            first = false;
            expectedFinalState.append(state.name());
        } else {
            expectedFinalState.append("," + state.name());
        }
    }

    while (true) {

        // Check app status every 1 second.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            LOG.debug("Thread sleep in monitoring loop interrupted");
        }

        // Get application report for the appId we are interested in
        ApplicationReport report = rmClient.getApplicationReport(appId);

        LOG.info("Got application report from ASM for" + ", appId=" + appId.getId() + ", appAttemptId="
                + report.getCurrentApplicationAttemptId() + ", clientToAMToken=" + report.getClientToAMToken()
                + ", appDiagnostics=" + report.getDiagnostics() + ", appMasterHost=" + report.getHost()
                + ", appQueue=" + report.getQueue() + ", appMasterRpcPort=" + report.getRpcPort()
                + ", appStartTime=" + report.getStartTime() + ", yarnAppState="
                + report.getYarnApplicationState().toString() + ", distributedFinalState="
                + report.getFinalApplicationStatus().toString() + ", appTrackingUrl=" + report.getTrackingUrl()
                + ", appUser=" + report.getUser());

        YarnApplicationState state = report.getYarnApplicationState();
        if (finalState.contains(state)) {
            return report;
        }

        // wait for 10 seconds after process has completed for app report to
        // come back
        if (amCompleted) {
            if (foundAMCompletedTime == 0) {
                foundAMCompletedTime = System.currentTimeMillis();
            } else if ((System.currentTimeMillis() - foundAMCompletedTime) > timeToWaitMS) {
                LOG.warn("Waited " + timeToWaitMS / 1000 + " seconds after process completed for AppReport"
                        + " to reach desired final state. Not waiting anymore." + "CurrentState = " + state
                        + ", ExpectedStates = " + expectedFinalState.toString());
                throw new RuntimeException("Failed to receive final expected state" + " in ApplicationReport"
                        + ", CurrentState=" + state + ", ExpectedStates=" + expectedFinalState.toString());
            }
        }
    }
}

From source file:org.elasticsearch.hadoop.yarn.cli.YarnBootstrap.java

License:Apache License

private String buildStatusReport(List<ApplicationReport> esApps) {
    if (esApps.isEmpty()) {
        return String.format("No Elasticsearch YARN clusters found at %s, webapp at %s",
                getConf().get(YarnConfiguration.RM_ADDRESS),
                WebAppUtils.getRMWebAppURLWithoutScheme(getConf()));
    }//from  w  w w.  j av a2 s .c  om

    String columnSeparator = "  ";
    StringBuilder sb = new StringBuilder();
    // header
    sb.append("Id                            ");
    sb.append(columnSeparator);
    sb.append("State     ");
    sb.append(columnSeparator);
    sb.append("Status   ");
    sb.append(columnSeparator);
    sb.append("Start Time       ");
    sb.append(columnSeparator);
    sb.append("Finish Time      ");
    sb.append(columnSeparator);
    sb.append("Tracking URL");
    sb.append("\n");

    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);

    for (ApplicationReport appReport : esApps) {
        sb.append(appReport.getApplicationId());
        sb.append(columnSeparator);
        sb.append(box(appReport.getYarnApplicationState().toString(), 10));
        sb.append(columnSeparator);
        sb.append(box(appReport.getFinalApplicationStatus().toString(), 9));
        sb.append(columnSeparator);
        long date = appReport.getStartTime();
        sb.append(date == 0 ? "N/A              " : box(dateFormat.format(new Date(date)), 17));
        sb.append(columnSeparator);
        date = appReport.getFinishTime();
        sb.append(date == 0 ? "N/A              " : box(dateFormat.format(new Date(date)), 17));
        sb.append(columnSeparator);
        sb.append(appReport.getTrackingUrl());
        sb.append(columnSeparator);
        sb.append("\n");
    }

    return sb.toString();
}

From source file:org.elasticsearch.hadoop.yarn.client.ClientRpc.java

License:Apache License

public void waitForApp(ApplicationId appId, long timeout) {
    boolean repeat = false;
    long start = System.currentTimeMillis();
    do {//  w ww  .  j  a v  a 2 s .c  o  m
        try {
            ApplicationReport appReport = client.getApplicationReport(appId);
            YarnApplicationState appState = appReport.getYarnApplicationState();
            repeat = (appState != YarnApplicationState.FINISHED && appState != YarnApplicationState.KILLED
                    && appState != YarnApplicationState.FAILED);
            if (repeat) {
                Thread.sleep(500);
            }
        } catch (Exception ex) {
            throw new EsYarnException(ex);
        }
    } while (repeat && (System.currentTimeMillis() - start) < timeout);
}

From source file:org.hdl.caffe.yarn.app.Client.java

License:Apache License

/**
 * Monitor the submitted application for completion.
 *
 * @param appId Application Id of application to be monitored
 * @return true if application completed successfully
 * @throws YarnException//from  www.  ja v a  2 s.c  o  m
 * @throws IOException
 */
private boolean monitorApplication(ApplicationId appId) throws YarnException, IOException {

    while (true) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            LOG.debug("Thread sleep in monitoring loop interrupted");
        }

        ApplicationReport report = yarnClient.getApplicationReport(appId);

        LOG.info("Got application report from ASM for" + ", appId=" + appId.getId() + ", clientToAMToken="
                + report.getClientToAMToken() + ", appDiagnostics=" + report.getDiagnostics()
                + ", appMasterHost=" + report.getHost() + ", appQueue=" + report.getQueue()
                + ", appMasterRpcPort=" + report.getRpcPort() + ", appStartTime=" + report.getStartTime()
                + ", yarnAppState=" + report.getYarnApplicationState().toString() + ", caffeAppFinalState="
                + report.getFinalApplicationStatus().toString() + ", appTrackingUrl=" + report.getTrackingUrl()
                + ", appUser=" + report.getUser());

        YarnApplicationState state = report.getYarnApplicationState();
        FinalApplicationStatus caffeStatus = report.getFinalApplicationStatus();

        if (YarnApplicationState.RUNNING == state) {
            if (appRpc == null) {
                String hostname = report.getHost();
                int port = report.getRpcPort();
                LOG.info("Application master rpc host: " + hostname + "; port: " + port);
                appRpc = new CaffeApplicationRpcClient(hostname, port).getRpc();
            }
        }

        if (YarnApplicationState.FINISHED == state) {
            if (FinalApplicationStatus.SUCCEEDED == caffeStatus) {
                LOG.info("Application has completed successfully. Breaking monitoring loop");
                return true;
            } else {
                LOG.info("Application did finished unsuccessfully." + " YarnState=" + state.toString()
                        + ", appFinalState=" + caffeStatus.toString() + ". Breaking monitoring loop");
                return false;
            }
        } else if (YarnApplicationState.KILLED == state || YarnApplicationState.FAILED == state) {
            LOG.info("Application did not finish." + " YarnState=" + state.toString() + ", appFinalState="
                    + caffeStatus.toString() + ". Breaking monitoring loop");
            return false;
        }
    }

}

From source file:org.hdl.tensorflow.yarn.client.Client.java

License:Apache License

static ClusterSpec getClusterSpec(YarnClient client, ApplicationId appId) throws Exception {
    ClusterSpec clusterSpec = ClusterSpec.empty();
    ApplicationReport report = client.getApplicationReport(appId);
    YarnApplicationState state = report.getYarnApplicationState();
    if (state.equals(YarnApplicationState.RUNNING)) {
        String hostname = report.getHost();
        int port = report.getRpcPort();
        TFApplicationRpc rpc = TFApplicationRpcClient.getInstance(hostname, port);
        String spec = rpc.getClusterSpec();
        if (spec != null) {
            clusterSpec = ClusterSpec.fromJsonString(spec);
        }/*from   w  w  w . ja v a 2s.  c  o m*/
    }
    return clusterSpec;
}

From source file:org.hdl.tensorflow.yarn.client.LaunchCluster.java

License:Apache License

boolean awaitApplication(ApplicationId appId) throws Exception {
    Set<YarnApplicationState> terminated = Sets.newHashSet(YarnApplicationState.FAILED,
            YarnApplicationState.FINISHED, YarnApplicationState.KILLED);
    while (true) {
        ApplicationReport report = yarnClient.getApplicationReport(appId);
        YarnApplicationState state = report.getYarnApplicationState();
        if (state.equals(YarnApplicationState.RUNNING)) {
            ClusterSpec clusterSpec = Client.getClusterSpec(yarnClient, appId);
            if (isClusterSpecSatisfied(clusterSpec)) {
                System.out.println("ClusterSpec: " + Utils.toJsonString(clusterSpec.getCluster()));
                return true;
            }/*w  w w .  jav  a  2  s.c om*/
        } else if (terminated.contains(state)) {
            return false;
        } else {
            Thread.sleep(1000);
        }
    }
}

From source file:org.hortonworks.dovetail.client.Client.java

License:Apache License

/**
 * Monitor the submitted application for completion. Kill application if
 * time expires.//from w w  w . java2s  .  c o m
 * 
 * @param appId
 *            Application Id of application to be monitored
 * @return true if application completed successfully
 * @throws YarnException
 * @throws IOException
 */
private boolean monitorApplication(ApplicationId appId) throws YarnException, IOException {

    while (true) {

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            LOG.finest("Thread sleep in monitoring loop interrupted");
        }

        ApplicationReport report = yarnClient.getApplicationReport(appId);

        LOG.info("Got application report from ASM for" + ", appId=" + appId.getId() + ", clientToAMToken="
                + report.getClientToAMToken() + ", appDiagnostics=" + report.getDiagnostics()
                + ", appMasterHost=" + report.getHost() + ", appQueue=" + report.getQueue()
                + ", appMasterRpcPort=" + report.getRpcPort() + ", appStartTime=" + report.getStartTime()
                + ", yarnAppState=" + report.getYarnApplicationState().toString() + ", distributedFinalState="
                + report.getFinalApplicationStatus().toString() + ", appTrackingUrl=" + report.getTrackingUrl()
                + ", appUser=" + report.getUser());

        YarnApplicationState state = report.getYarnApplicationState();
        FinalApplicationStatus dovetailStatus = report.getFinalApplicationStatus();
        if (YarnApplicationState.FINISHED == state) {
            if (FinalApplicationStatus.SUCCEEDED == dovetailStatus) {
                LOG.info("Application has completed successfully. Breaking monitoring loop");
                return true;
            } else {
                LOG.info("Application did finished unsuccessfully." + " YarnState=" + state.toString()
                        + ", DovetailFinalStatus=" + dovetailStatus.toString() + ". Breaking monitoring loop");
                return false;
            }
        } else if (YarnApplicationState.KILLED == state || YarnApplicationState.FAILED == state) {
            LOG.info("Application did not finish." + " YarnState=" + state.toString() + ", DovetailFinalStatus="
                    + dovetailStatus.toString() + ". Breaking monitoring loop");
            return false;
        }
    }
}

From source file:org.huahinframework.manager.rest.service.ApplicationService.java

License:Apache License

@Path("/list")
@GET//from   w  ww  . j  av  a2  s. c  o  m
@Produces(MediaType.APPLICATION_JSON)
public JSONObject list() {
    JSONObject jsonObject = new JSONObject();

    try {
        GetAllApplicationsRequest request = recordFactory.newRecordInstance(GetAllApplicationsRequest.class);
        GetAllApplicationsResponse response = applicationsManager.getAllApplications(request);

        JSONObject appObject = new JSONObject();
        List<JSONObject> apps = new ArrayList<JSONObject>();
        for (ApplicationReport ar : response.getApplicationList()) {
            JSONObject app = new JSONObject();
            app.put(Response.ID, ar.getApplicationId().toString());
            app.put(Response.USER, ar.getUser());
            app.put(Response.NAME, ar.getName());
            app.put(Response.QUEUE, ar.getQueue());
            YarnApplicationState state = ar.getYarnApplicationState();
            app.put(Response.STATE, state);
            app.put(Response.FINAL_STATUS, ar.getFinalApplicationStatus().name());
            String trackingUrl = ar.getTrackingUrl();
            boolean trackingUrlIsNotReady = trackingUrl == null || trackingUrl.isEmpty()
                    || YarnApplicationState.NEW == state || YarnApplicationState.SUBMITTED == state
                    || YarnApplicationState.ACCEPTED == state;
            String trackingUI = trackingUrlIsNotReady ? "UNASSIGNED"
                    : (ar.getFinishTime() == 0 ? "ApplicationMaster" : "History");
            app.put(Response.TRACKING_UI, trackingUI);
            app.put(Response.TRACKING_URL, trackingUrl);
            app.put(Response.DIAGNOSTICS, ar.getDiagnostics());
            app.put(Response.START_TIME, new Date(ar.getStartTime()));
            app.put(Response.FINISHED_TIME, ar.getFinishTime() == 0 ? "" : new Date(ar.getFinishTime()));
            app.put(Response.ELAPSED_TIME,
                    (Times.elapsed(ar.getStartTime(), ar.getFinishTime()) / 1000) + "sec");
            apps.add(app);
        }

        appObject.put(Response.APP, new JSONArray(apps));
        jsonObject.put(Response.APPS, appObject);
    } catch (Exception e) {
        e.printStackTrace();
        log.error(e);
        Map<String, String> status = new HashMap<String, String>();
        status.put(Response.STATUS, e.getMessage());
        jsonObject = new JSONObject(status);
    }

    return jsonObject;
}

From source file:org.springframework.cloud.dataflow.module.deployer.yarn.YarnCloudAppServiceApplication.java

License:Apache License

public Collection<CloudAppInstanceInfo> getSubmittedApplications() {
    List<CloudAppInstanceInfo> appIds = new ArrayList<CloudAppInstanceInfo>();
    for (ApplicationReport report : yarnClient.listApplications("DATAFLOW")) {
        appIds.add(new CloudAppInstanceInfo(report.getApplicationId().toString(), report.getName(),
                report.getYarnApplicationState().toString(), report.getOriginalTrackingUrl()));
    }/*from   w  w  w  . java 2s.  c  om*/
    return appIds;
}

From source file:org.springframework.cloud.dataflow.yarn.buildtests.AbstractCliBootYarnClusterTests.java

License:Apache License

protected ApplicationInfo submitApplicationAndWaitState(long timeout, TimeUnit unit,
        YarnApplicationState... applicationStates) throws Exception {
    Assert.notEmpty(applicationStates, "Need to have atleast one state");
    Assert.notNull(getYarnClient(), "Yarn client must be set");

    YarnApplicationState state = null;/*from   ww  w.ja v a 2s .  c  o  m*/
    ApplicationReport report = null;

    ApplicationId applicationId = submitApplication();
    Assert.notNull(applicationId, "Failed to get application id from submit");

    long end = System.currentTimeMillis() + unit.toMillis(timeout);

    // break label for inner loop
    done: do {
        report = findApplicationReport(getYarnClient(), applicationId);
        if (report == null) {
            break;
        }
        state = report.getYarnApplicationState();
        for (YarnApplicationState stateCheck : applicationStates) {
            if (state.equals(stateCheck)) {
                break done;
            }
        }
        Thread.sleep(1000);
    } while (System.currentTimeMillis() < end);
    return new ApplicationInfo(applicationId, report);
}