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

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

Introduction

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

Prototype

@Public
@Stable
public abstract Set<String> getApplicationTags();

Source Link

Document

Get all tags corresponding to the application

Usage

From source file:org.apache.ambari.view.slider.SliderAppsViewControllerImpl.java

License:Apache License

private SliderApp createSliderAppObject(ApplicationReport yarnApp, Set<String> properties,
        SliderClient sliderClient) {/*  ww  w  . jav a  2s  . c  o m*/
    if (yarnApp == null) {
        return null;
    }

    SliderApp app = new SliderApp();
    app.setState(yarnApp.getYarnApplicationState().name());

    // Valid Slider App?
    // We want all Slider apps except the ones which properly finished.
    if (YarnApplicationState.FINISHED.equals(yarnApp.getYarnApplicationState())) {
        try {
            if (sliderClient.actionExists(yarnApp.getName(), false) == LauncherExitCodes.EXIT_SUCCESS) {
                app.setState(SliderApp.STATE_FROZEN);
            }
        } catch (UnknownApplicationInstanceException e) {
            return null; // Application not in HDFS - means it is not frozen
        } catch (YarnException e) {
            logger.warn("Unable to determine frozen state for " + yarnApp.getName(), e);
            return null;
        } catch (IOException e) {
            logger.warn("Unable to determine frozen state for " + yarnApp.getName(), e);
            return null;
        }
    }
    if (YarnApplicationState.KILLED.equals(yarnApp.getYarnApplicationState())
            || YarnApplicationState.FAILED.equals(yarnApp.getYarnApplicationState())) {
        try {
            if (sliderClient.actionExists(yarnApp.getName(), false) != LauncherExitCodes.EXIT_SUCCESS) {
                // YARN application is killed or failed, and no HDFS content - Application has been destroyed.
                return null;
            }
        } catch (UnknownApplicationInstanceException e) {
            return null; // Application not in HDFS - means it is not frozen
        } catch (YarnException e) {
            logger.warn("Unable to determine status of killed app " + yarnApp.getName(), e);
            return null;
        } catch (IOException e) {
            logger.warn("Unable to determine status of killed app " + yarnApp.getName(), e);
            return null;
        }
    }

    app.setId(getApplicationIdString(yarnApp.getApplicationId()));
    app.setName(yarnApp.getName());
    app.setUser(yarnApp.getUser());
    app.setDiagnostics(yarnApp.getDiagnostics());
    app.setYarnId(yarnApp.getApplicationId().toString());
    app.setStartTime(yarnApp.getStartTime());
    app.setEndTime(yarnApp.getFinishTime());
    Set<String> applicationTags = yarnApp.getApplicationTags();
    if (applicationTags != null && applicationTags.size() > 0) {
        for (String tag : applicationTags) {
            int index = tag.indexOf(':');
            if (index > 0 && index < tag.length() - 1) {
                String key = tag.substring(0, index).trim();
                String value = tag.substring(index + 1).trim();
                if ("name".equals(key)) {
                    app.setType(value);
                } else if ("version".equals(key)) {
                    app.setAppVersion(value);
                } else if ("description".equals(key)) {
                    app.setDescription(value);
                }
            }
        }
    }
    if (properties != null && !properties.isEmpty()) {
        SliderAppType matchedAppType = null;
        List<SliderAppType> matchingAppTypes = getSliderAppTypes(null);
        if (matchingAppTypes != null && matchingAppTypes.size() > 0) {
            for (SliderAppType appType : matchingAppTypes) {
                if ((appType.getTypeName() != null && appType.getTypeName().equalsIgnoreCase(app.getType()))
                        && (appType.getTypeVersion() != null
                                && appType.getTypeVersion().equalsIgnoreCase(app.getAppVersion()))) {
                    matchedAppType = appType;
                    break;
                }
            }
        }

        SliderAppMasterClient sliderAppClient = yarnApp.getTrackingUrl() == null ? null
                : new SliderAppMasterClient(yarnApp.getTrackingUrl());
        SliderAppMasterData appMasterData = null;
        Map<String, String> quickLinks = new HashMap<String, String>();
        Set<String> metrics = new HashSet<String>();
        for (String property : properties) {
            if ("RUNNING".equals(app.getState())) {
                if (sliderAppClient != null) {
                    if (appMasterData == null) {
                        appMasterData = sliderAppClient.getAppMasterData();
                    }
                    if (appMasterData != null && "urls".equals(property.toLowerCase())) {
                        if (quickLinks.isEmpty()) {
                            quickLinks = sliderAppClient.getQuickLinks(appMasterData.publisherUrl);
                        }
                        app.setUrls(quickLinks);
                    } else if (appMasterData != null && "configs".equals(property.toLowerCase())) {
                        Map<String, Map<String, String>> configs = sliderAppClient
                                .getConfigs(appMasterData.publisherUrl);
                        app.setConfigs(configs);
                    } else if (appMasterData != null && "jmx".equals(property.toLowerCase())) {
                        if (quickLinks.isEmpty()) {
                            quickLinks = sliderAppClient.getQuickLinks(appMasterData.publisherUrl);
                        }
                        if (quickLinks != null && quickLinks.containsKey("JMX")) {
                            String jmxUrl = quickLinks.get("JMX");
                            if (matchedAppType != null) {
                                MetricsHolder metricsHolder = appMetrics.get(matchedAppType.uniqueName());
                                app.setJmx(sliderAppClient.getJmx(jmxUrl, viewContext, matchedAppType,
                                        metricsHolder));
                            }
                        }
                        Map<String, Map<String, String>> configs = sliderAppClient
                                .getConfigs(appMasterData.publisherUrl);
                        app.setConfigs(configs);
                    } else if ("components".equals(property.toLowerCase())) {
                        try {
                            ClusterDescription description = sliderClient
                                    .getClusterDescription(yarnApp.getName());
                            if (description != null && description.status != null
                                    && !description.status.isEmpty()) {
                                Map<String, SliderAppComponent> componentTypeMap = new HashMap<String, SliderAppComponent>();
                                for (Entry<String, Object> e : description.status.entrySet()) {
                                    @SuppressWarnings("unchecked")
                                    Map<String, Map<String, Map<String, Object>>> componentsObj = (Map<String, Map<String, Map<String, Object>>>) e
                                            .getValue();
                                    boolean isLive = "live".equals(e.getKey());
                                    for (Entry<String, Map<String, Map<String, Object>>> componentEntry : componentsObj
                                            .entrySet()) {
                                        SliderAppComponent appComponent = componentTypeMap
                                                .get(componentEntry.getKey());
                                        if (appComponent == null) {
                                            appComponent = new SliderAppComponent();
                                            appComponent.setComponentName(componentEntry.getKey());
                                            appComponent.setActiveContainers(
                                                    new HashMap<String, Map<String, String>>());
                                            appComponent.setCompletedContainers(
                                                    new HashMap<String, Map<String, String>>());
                                            componentTypeMap.put(componentEntry.getKey(), appComponent);
                                        }
                                        for (Entry<String, Map<String, Object>> containerEntry : componentEntry
                                                .getValue().entrySet()) {
                                            Map<String, String> containerDataMap = new HashMap<String, String>();
                                            String containerId = containerEntry.getKey();
                                            Map<String, Object> containerValues = containerEntry.getValue();
                                            for (String containerProperty : containerValues.keySet()) {
                                                Object containerPropertyValue = containerValues
                                                        .get(containerProperty);
                                                containerDataMap.put(containerProperty,
                                                        containerPropertyValue.toString());
                                            }
                                            if (isLive) {
                                                appComponent.getActiveContainers().put(containerId,
                                                        containerDataMap);
                                            } else {
                                                appComponent.getCompletedContainers().put(containerId,
                                                        containerDataMap);
                                            }
                                        }
                                        // Set total instances count from statistics
                                        appComponent.setInstanceCount(appComponent.getActiveContainers().size()
                                                + appComponent.getCompletedContainers().size());
                                        if (description.statistics != null && description.statistics
                                                .containsKey(componentEntry.getKey())) {
                                            Map<String, Integer> statisticsMap = description.statistics
                                                    .get(componentEntry.getKey());
                                            if (statisticsMap.containsKey("containers.desired")) {
                                                appComponent.setInstanceCount(
                                                        statisticsMap.get("containers.desired"));
                                            }
                                        }
                                    }
                                }
                                app.setAlerts(
                                        sliderAlerts.generateComponentsAlerts(componentTypeMap, app.getType()));
                                app.setComponents(componentTypeMap);
                            }
                        } catch (UnknownApplicationInstanceException e) {
                            logger.warn("Unable to determine app components for " + yarnApp.getName(), e);
                        } catch (YarnException e) {
                            logger.warn("Unable to determine app components for " + yarnApp.getName(), e);
                            throw new RuntimeException(e.getMessage(), e);
                        } catch (IOException e) {
                            logger.warn("Unable to determine app components for " + yarnApp.getName(), e);
                            throw new RuntimeException(e.getMessage(), e);
                        }
                    } else if (property.startsWith(METRICS_PREFIX)) {
                        metrics.add(property.substring(METRICS_PREFIX.length()));
                    } else if ("supportedMetrics".equals(property)) {
                        if (matchedAppType != null) {
                            app.setSupportedMetrics(matchedAppType.getSupportedMetrics());
                        }
                    }
                }
            }
        }
        if (metrics.size() > 0) {
            if (quickLinks.isEmpty()) {
                quickLinks = sliderAppClient.getQuickLinks(appMasterData.publisherUrl);
            }
            if (quickLinks != null && quickLinks.containsKey(METRICS_API_NAME)) {
                String metricsUrl = quickLinks.get(METRICS_API_NAME);
                MetricsHolder metricsHolder = appMetrics.get(matchedAppType.uniqueName());
                app.setMetrics(sliderAppClient.getMetrics(yarnApp.getName(), metricsUrl, metrics, null,
                        viewContext, matchedAppType, metricsHolder));
            }
        }
    }
    return app;
}

From source file:org.apache.hive.service.server.KillQueryImpl.java

License:Apache License

public static Set<ApplicationId> getChildYarnJobs(Configuration conf, String tag)
        throws IOException, YarnException {
    Set<ApplicationId> childYarnJobs = new HashSet<ApplicationId>();
    GetApplicationsRequest gar = GetApplicationsRequest.newInstance();
    gar.setScope(ApplicationsRequestScope.OWN);
    gar.setApplicationTags(Collections.singleton(tag));

    ApplicationClientProtocol proxy = ClientRMProxy.createRMProxy(conf, ApplicationClientProtocol.class);
    GetApplicationsResponse apps = proxy.getApplications(gar);
    List<ApplicationReport> appsList = apps.getApplicationList();
    for (ApplicationReport appReport : appsList) {
        if (isAdmin() || appReport.getApplicationTags()
                .contains(QueryState.USERID_TAG + "=" + SessionState.get().getUserName())) {
            childYarnJobs.add(appReport.getApplicationId());
        }//from   ww w  .j  a v a2s  . co  m
    }

    if (childYarnJobs.isEmpty()) {
        LOG.info("No child applications found");
    } else {
        LOG.info("Found child YARN applications: " + StringUtils.join(childYarnJobs, ","));
    }

    return childYarnJobs;
}

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  w  w  . j  a  v a 2  s  .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();
}