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.apache.slider.client.SliderClient.java

License:Apache License

/**
 * Build an exit code for an application from its report.
 * If the report parameter is null, its interpreted as a timeout
 * @param report report application report
 * @return the exit code/*from   ww  w  .  j av  a 2  s . c o  m*/
 * @throws IOException
 * @throws YarnException
 */
private int buildExitCode(ApplicationReport report) throws IOException, YarnException {
    if (null == report) {
        return EXIT_TIMED_OUT;
    }

    YarnApplicationState state = report.getYarnApplicationState();
    FinalApplicationStatus dsStatus = report.getFinalApplicationStatus();
    switch (state) {
    case FINISHED:
        if (FinalApplicationStatus.SUCCEEDED == dsStatus) {
            log.info("Application has completed successfully");
            return EXIT_SUCCESS;
        } else {
            log.info("Application finished unsuccessfully."
                    + "YarnState = {}, DSFinalStatus = {} Breaking monitoring loop", state, dsStatus);
            return EXIT_YARN_SERVICE_FINISHED_WITH_ERROR;
        }

    case KILLED:
        log.info("Application did not finish. YarnState={}, DSFinalStatus={}", state, dsStatus);
        return EXIT_YARN_SERVICE_KILLED;

    case FAILED:
        log.info("Application Failed. YarnState={}, DSFinalStatus={}", state, dsStatus);
        return EXIT_YARN_SERVICE_FAILED;

    default:
        //not in any of these states
        return EXIT_SUCCESS;
    }
}

From source file:org.apache.slider.client.SliderClient.java

License:Apache License

/**
 * Convert the instance details of an application to a string
 * @param name instance name//from  www  . ja v  a  2 s .  c o m
 * @param report the application report
 * @param verbose verbose output
 * @return a string
 */
String instanceDetailsToString(String name, ApplicationReport report, boolean verbose) {
    // format strings
    String staticf = "%-30s";
    String reportedf = staticf + "  %10s  %-40s";
    String livef = reportedf + " %s";
    StringBuilder builder = new StringBuilder(200);
    if (report == null) {
        builder.append(String.format(staticf, name));
    } else {
        // there's a report to look at
        String appId = report.getApplicationId().toString();
        String state = report.getYarnApplicationState().toString();
        if (report.getYarnApplicationState() == YarnApplicationState.RUNNING) {
            // running: there's a URL
            builder.append(String.format(livef, name, state, appId, report.getTrackingUrl()));
        } else {
            builder.append(String.format(reportedf, name, state, appId));
        }
        if (verbose) {
            builder.append('\n');
            builder.append(SliderUtils.appReportToString(report, "\n  "));
        }
    }

    builder.append('\n');
    return builder.toString();
}

From source file:org.apache.slider.client.SliderClient.java

License:Apache License

/**
 * Is an application active: accepted or running
 * @param report the application report//  w w w.  j a va 2s.c  o  m
 * @return true if it is running or scheduled to run.
 */
public boolean isApplicationActive(ApplicationReport report) {
    return report.getYarnApplicationState() == YarnApplicationState.RUNNING
            || report.getYarnApplicationState() == YarnApplicationState.ACCEPTED;
}

From source file:org.apache.slider.client.SliderClient.java

License:Apache License

public int actionExists(String name, ActionExistsArgs args) throws YarnException, IOException {
    verifyBindingsDefined();/*from  w  w w.j av a2 s . c  o  m*/
    SliderUtils.validateClusterName(name);
    boolean checkLive = args.live;
    log.debug("actionExists({}, {}, {})", name, checkLive, args.state);

    //initial probe for a cluster in the filesystem
    Path clusterDirectory = sliderFileSystem.buildClusterDirPath(name);
    if (!sliderFileSystem.getFileSystem().exists(clusterDirectory)) {
        throw unknownClusterException(name);
    }
    String state = args.state;
    if (!checkLive && SliderUtils.isUnset(state)) {
        log.info("Application {} exists", name);
        return EXIT_SUCCESS;
    }

    //test for liveness/state
    boolean inDesiredState = false;
    ApplicationReport instance;
    instance = findInstance(name);
    if (instance == null) {
        log.info("Application {} not running", name);
        return EXIT_FALSE;
    }
    if (checkLive) {
        // the app exists, check that it is not in any terminated state
        YarnApplicationState appstate = instance.getYarnApplicationState();
        log.debug(" current app state = {}", appstate);
        inDesiredState = appstate.ordinal() < YarnApplicationState.FINISHED.ordinal();
    } else {
        // scan for instance in single --state state
        List<ApplicationReport> userInstances = yarnClient.listInstances("");
        state = state.toUpperCase(Locale.ENGLISH);
        YarnApplicationState desiredState = extractYarnApplicationState(state);
        ApplicationReport foundInstance = yarnClient.findAppInInstanceList(userInstances, name, desiredState);
        if (foundInstance != null) {
            // found in selected state: success
            inDesiredState = true;
            // mark this as the instance to report
            instance = foundInstance;
        }
    }

    SliderUtils.OnDemandReportStringifier report = new SliderUtils.OnDemandReportStringifier(instance);
    if (!inDesiredState) {
        //cluster in the list of apps but not running
        log.info("Application {} found but is in wrong state {}", name, instance.getYarnApplicationState());
        log.debug("State {}", report);
        return EXIT_FALSE;
    } else {
        log.debug("Application instance is in desired state");
        log.info("Application {} is {}\n{}", name, instance.getYarnApplicationState(), report);
        return EXIT_SUCCESS;
    }
}

From source file:org.apache.slider.client.SliderClient.java

License:Apache License

@Override
public int actionFreeze(String clustername, ActionFreezeArgs freezeArgs) throws YarnException, IOException {
    verifyBindingsDefined();//w ww. j  a v a2 s  .  com
    SliderUtils.validateClusterName(clustername);
    int waittime = freezeArgs.getWaittime();
    String text = freezeArgs.message;
    boolean forcekill = freezeArgs.force;
    log.debug("actionFreeze({}, reason={}, wait={}, force={})", clustername, text, waittime, forcekill);

    //is this actually a known cluster?
    sliderFileSystem.locateInstanceDefinition(clustername);
    ApplicationReport app = findInstance(clustername);
    if (app == null) {
        // exit early
        log.info("Cluster {} not running", clustername);
        // not an error to stop a stopped cluster
        return EXIT_SUCCESS;
    }
    log.debug("App to stop was found: {}:\n{}", clustername, new SliderUtils.OnDemandReportStringifier(app));
    if (app.getYarnApplicationState().ordinal() >= YarnApplicationState.FINISHED.ordinal()) {
        log.info("Cluster {} is a terminated state {}", clustername, app.getYarnApplicationState());
        return EXIT_SUCCESS;
    }

    // IPC request for a managed shutdown is only possible if the app is running.
    // so we need to force kill if the app is accepted or submitted
    if (!forcekill && app.getYarnApplicationState().ordinal() < YarnApplicationState.RUNNING.ordinal()) {
        log.info("Cluster {} is in a pre-running state {}. Force killing it", clustername,
                app.getYarnApplicationState());
        forcekill = true;
    }

    LaunchedApplication application = new LaunchedApplication(yarnClient, app);
    applicationId = application.getApplicationId();

    if (forcekill) {
        // escalating to forced kill
        application.kill("Forced stop of " + clustername + ": " + text);
    } else {
        try {
            SliderClusterProtocol appMaster = connect(app);
            Messages.StopClusterRequestProto r = Messages.StopClusterRequestProto.newBuilder().setMessage(text)
                    .build();
            appMaster.stopCluster(r);

            log.debug("Cluster stop command issued");

        } catch (YarnException e) {
            log.warn("Exception while trying to terminate {}: {}", clustername, e);
            return EXIT_FALSE;
        } catch (IOException e) {
            log.warn("Exception while trying to terminate {}: {}", clustername, e);
            return EXIT_FALSE;
        }
    }

    //wait for completion. We don't currently return an exception during this process
    //as the stop operation has been issued, this is just YARN.
    try {
        if (waittime > 0) {
            ApplicationReport applicationReport = application.monitorAppToState(YarnApplicationState.FINISHED,
                    new Duration(waittime * 1000));
            if (applicationReport == null) {
                log.info("application did not shut down in time");
                return EXIT_FALSE;
            }
        }

        // JDK7    } catch (YarnException | IOException e) {
    } catch (YarnException e) {
        log.warn("Exception while waiting for the application {} to shut down: {}", clustername, e);
    } catch (IOException e) {
        log.warn("Exception while waiting for the application {} to shut down: {}", clustername, e);
    }

    return EXIT_SUCCESS;
}

From source file:org.apache.slider.client.SliderYarnClientImpl.java

License:Apache License

/**
 * Monitor the submitted application for reaching the requested state.
 * Will also report if the app reaches a later state (failed, killed, etc)
 * Kill application if duration!= null & time expires. 
 * @param appId Application Id of application to be monitored
 * @param duration how long to wait -must be more than 0
 * @param desiredState desired state./*from   ww w.j ava 2s .  c o  m*/
 * @return the application report -null on a timeout
 * @throws YarnException
 * @throws IOException
 */
public ApplicationReport monitorAppToState(ApplicationId appId, YarnApplicationState desiredState,
        Duration duration) throws YarnException, IOException {

    if (appId == null) {
        throw new BadCommandArgumentsException("null application ID");
    }
    if (duration.limit <= 0) {
        throw new BadCommandArgumentsException("Invalid monitoring duration");
    }
    log.debug("Waiting {} millis for app to reach state {} ", duration.limit, desiredState);
    duration.start();
    while (true) {

        // Get application report for the appId we are interested in

        ApplicationReport r = getApplicationReport(appId);

        log.debug("queried status is\n{}", new SliderUtils.OnDemandReportStringifier(r));

        YarnApplicationState state = r.getYarnApplicationState();
        if (state.ordinal() >= desiredState.ordinal()) {
            log.debug("App in desired state (or higher) :{}", state);
            return r;
        }
        if (duration.getLimitExceeded()) {
            log.debug("Wait limit of {} millis to get to state {}, exceeded; app status\n {}", duration.limit,
                    desiredState, new SliderUtils.OnDemandReportStringifier(r));
            return null;
        }

        // sleep 1s.
        try {
            Thread.sleep(1000);
        } catch (InterruptedException ignored) {
            log.debug("Thread sleep in monitoring loop interrupted");
        }
    }
}

From source file:org.apache.slider.client.SliderYarnClientImpl.java

License:Apache License

/**
 * Find an app in the instance list in the desired state 
 * @param instances instance list/*from   w  w  w  .  jav a  2  s.  c o m*/
 * @param appname application name
 * @param desiredState yarn state desired
 * @return the match or null for none
 */
public ApplicationReport findAppInInstanceList(List<ApplicationReport> instances, String appname,
        YarnApplicationState desiredState) {
    ApplicationReport found = null;
    ApplicationReport foundAndLive = null;
    log.debug("Searching {} records for instance name {} in state '{}'", instances.size(), appname,
            desiredState);
    for (ApplicationReport app : instances) {
        if (app.getName().equals(appname)) {

            YarnApplicationState appstate = app.getYarnApplicationState();
            log.debug("app ID {} is in state {}", app.getApplicationId(), appstate);
            if (appstate.equals(desiredState)) {
                log.debug("match");
                return app;
            }
        }
    }
    // nothing found in desired state
    log.debug("No match");
    return null;
}

From source file:org.apache.slider.common.tools.SliderUtils.java

License:Apache License

public static String appReportToString(ApplicationReport r, String separator) {
    StringBuilder builder = new StringBuilder(512);
    builder.append("application ").append(r.getName()).append("/").append(r.getApplicationType())
            .append(separator);/*  w  ww . ja v  a  2s.  co  m*/
    Set<String> tags = r.getApplicationTags();
    if (!tags.isEmpty()) {
        for (String tag : tags) {
            builder.append(tag).append(separator);
        }
    }
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm");
    dateFormat.setTimeZone(TimeZone.getDefault());
    builder.append("state: ").append(r.getYarnApplicationState());
    String trackingUrl = r.getTrackingUrl();
    if (isSet(trackingUrl)) {
        builder.append(separator).append("URL: ").append(trackingUrl);
    }
    builder.append(separator).append("Started: ").append(dateFormat.format(new Date(r.getStartTime())));
    long finishTime = r.getFinishTime();
    if (finishTime > 0) {
        builder.append(separator).append("Finished: ").append(dateFormat.format(new Date(finishTime)));
    }
    String rpcHost = r.getHost();
    if (!isSet(rpcHost)) {
        builder.append(separator).append("RPC :").append(rpcHost).append(':').append(r.getRpcPort());
    }
    String diagnostics = r.getDiagnostics();
    if (!isSet(diagnostics)) {
        builder.append(separator).append("Diagnostics :").append(diagnostics);
    }
    return builder.toString();
}

From source file:org.apache.slider.common.tools.SliderUtils.java

License:Apache License

/**
 * Sorts the given list of application reports
 * Finished instances are ordered by finished time and running/accepted instances are
 * ordered by start time//from   ww w  . j  a  v a  2s  .  c o  m
 * Finally Instance are order by finished instances coming after running instances
 *
 * @param instances list of instances
 */
public static void sortApplicationReport(List<ApplicationReport> instances) {
    if (instances.size() <= 1) {
        return;
    }
    List<ApplicationReport> nonLiveInstance = new ArrayList<ApplicationReport>(instances.size());
    List<ApplicationReport> liveInstance = new ArrayList<ApplicationReport>(instances.size());

    for (ApplicationReport report : instances) {
        if (report.getYarnApplicationState() == YarnApplicationState.RUNNING
                || report.getYarnApplicationState() == YarnApplicationState.ACCEPTED) {
            liveInstance.add(report);
        } else {
            nonLiveInstance.add(report);
        }
    }

    if (liveInstance.size() > 1) {
        Collections.sort(liveInstance, new MostRecentlyStartedAppFirst());
    }
    if (nonLiveInstance.size() > 1) {
        Collections.sort(nonLiveInstance, new MostRecentAppFinishFirst());
    }
    instances.clear();
    instances.addAll(liveInstance);
    instances.addAll(nonLiveInstance);
}

From source file:org.apache.slider.common.tools.SliderUtils.java

License:Apache License

/**
 * Built a (sorted) map of application reports, mapped to the instance name
 * The list is sorted, and the addition process does not add a report
 * if there is already one that exists. If the list handed in is sorted,
 * those that are listed first form the entries returned
 * @param instances list of intances/*from  ww w  .  j  av  a2s  . c om*/
 * @param minState minimum YARN state to be included
 * @param maxState maximum YARN state to be included
 * @return all reports in the list whose state &gt;= minimum and &lt;= maximum
 */
public static Map<String, ApplicationReport> buildApplicationReportMap(List<ApplicationReport> instances,
        YarnApplicationState minState, YarnApplicationState maxState) {
    TreeMap<String, ApplicationReport> map = new TreeMap<String, ApplicationReport>();
    for (ApplicationReport report : instances) {
        YarnApplicationState state = report.getYarnApplicationState();
        if (state.ordinal() >= minState.ordinal() && state.ordinal() <= maxState.ordinal()
                && map.get(report.getName()) == null) {
            map.put(report.getName(), report);
        }
    }
    return map;
}