Example usage for org.apache.hadoop.yarn.api.records ContainerId getApplicationAttemptId

List of usage examples for org.apache.hadoop.yarn.api.records ContainerId getApplicationAttemptId

Introduction

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

Prototype

@Public
@Stable
public abstract ApplicationAttemptId getApplicationAttemptId();

Source Link

Document

Get the ApplicationAttemptId of the application to which the Container was assigned.

Usage

From source file:org.hortonworks.dovetail.am.AppMaster.java

License:Apache License

/**
 * Parse command line options/*w  ww .  j av a  2 s.  c  o  m*/
 * 
 * @param args
 *            Command line args
 * @return Whether init successful and run should be invoked
 * @throws IOException
 */
public boolean init(String[] args) throws IOException {

    Map<String, String> envs = System.getenv();

    ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));

    containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
    appAttemptID = containerId.getApplicationAttemptId();

    if (!envs.containsKey(ApplicationConstants.APP_SUBMIT_TIME_ENV)) {
        throw new RuntimeException(ApplicationConstants.APP_SUBMIT_TIME_ENV + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HOST.name())) {
        throw new RuntimeException(Environment.NM_HOST.name() + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HTTP_PORT.name())) {
        throw new RuntimeException(Environment.NM_HTTP_PORT + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_PORT.name())) {
        throw new RuntimeException(Environment.NM_PORT.name() + " not set in the environment");
    }

    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId()
            + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId="
            + appAttemptID.getAttemptId());

    numContainers = conf.getInt(DovetailConfiguration.DOVETAIL_CONTAINER_COUNT,
            DovetailConfiguration.DEFAULT_CONTAINER_COUNT);

    containerMemory = conf.getInt(DovetailConfiguration.DOVETAIL_CONTAINER_MEMORY,
            DovetailConfiguration.DEFAULT_CONTAINER_MEMORY);

    containerMemory = conf.getInt(DovetailConfiguration.DOVETAIL_CONTAINER_PRIORITY,
            DovetailConfiguration.DEFAULT_DOVETAIL_CONTAINER_PRIORITY);

    amJar = conf.get(DovetailConfiguration.DOVETAIL_AM_HDFS_DIR) + Path.SEPARATOR
            + System.getProperty(DovetailConfiguration.DOVETAIL_AM_JAR);

    return true;
}

From source file:org.springframework.yarn.support.YarnUtils.java

License:Apache License

/**
 * Gets {@link ApplicationAttemptId} from environment variables.
 *
 * @param environment Map of environment variables
 * @return the {@link ApplicationAttemptId}
 *///from  w  ww  .  ja  va  2 s . c o  m
public static ApplicationAttemptId getApplicationAttemptId(Map<String, String> environment) {
    String amContainerId = environment.get(ApplicationConstants.Environment.CONTAINER_ID.name());
    Assert.notNull(amContainerId, "CONTAINER_ID env variable has to exist to build appAttemptId");
    ContainerId containerId = ConverterUtils.toContainerId(amContainerId);
    return containerId.getApplicationAttemptId();
}

From source file:org.starschema.hadoop.yarn.applications.distributedshell.ApplicationMaster.java

License:Apache License

/**
 * Parse command line options/*from   www .ja va  2s. co  m*/
 *
 * @param args Command line args
 * @return Whether init successful and run should be invoked
 * @throws ParseException
 * @throws IOException
 */
public boolean init(String[] args) throws ParseException, IOException {
    Options opts = new Options();
    opts.addOption("app_attempt_id", true, "App Attempt ID. Not to be used unless for testing purposes");
    opts.addOption("shell_env", true, "Environment for shell script. Specified as env_key=env_val pairs");
    opts.addOption("container_memory", true, "Amount of memory in MB to be requested to run the shell command");
    opts.addOption("container_vcores", true,
            "Amount of virtual cores to be requested to run the shell command");
    opts.addOption("num_containers", true, "No. of containers on which the shell command needs to be executed");
    opts.addOption("priority", true, "Application Priority. Default 0");
    opts.addOption("debug", false, "Dump out debug information");

    opts.addOption("help", false, "Print usage");
    CommandLine cliParser = new GnuParser().parse(opts, args);

    if (args.length == 0) {
        printUsage(opts);
        throw new IllegalArgumentException("No args specified for application master to initialize");
    }

    //Check whether customer log4j.properties file exists
    if (fileExist(log4jPath)) {
        try {
            Log4jPropertyHelper.updateLog4jConfiguration(ApplicationMaster.class, log4jPath);
        } catch (Exception e) {
            LOG.warn("Can not set up custom log4j properties. " + e);
        }
    }

    if (cliParser.hasOption("help")) {
        printUsage(opts);
        return false;
    }

    if (cliParser.hasOption("debug")) {
        dumpOutDebugInfo();
    }

    Map<String, String> envs = System.getenv();

    if (!envs.containsKey(Environment.CONTAINER_ID.name())) {
        if (cliParser.hasOption("app_attempt_id")) {
            String appIdStr = cliParser.getOptionValue("app_attempt_id", "");
            appAttemptID = ConverterUtils.toApplicationAttemptId(appIdStr);
        } else {
            throw new IllegalArgumentException("Application Attempt Id not set in the environment");
        }
    } else {
        ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
        appAttemptID = containerId.getApplicationAttemptId();
    }

    if (!envs.containsKey(ApplicationConstants.APP_SUBMIT_TIME_ENV)) {
        throw new RuntimeException(ApplicationConstants.APP_SUBMIT_TIME_ENV + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HOST.name())) {
        throw new RuntimeException(Environment.NM_HOST.name() + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_HTTP_PORT.name())) {
        throw new RuntimeException(Environment.NM_HTTP_PORT + " not set in the environment");
    }
    if (!envs.containsKey(Environment.NM_PORT.name())) {
        throw new RuntimeException(Environment.NM_PORT.name() + " not set in the environment");
    }

    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId()
            + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId="
            + appAttemptID.getAttemptId());

    if (!fileExist(shellCommandPath) && envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION).isEmpty()) {
        throw new IllegalArgumentException(
                "No shell command or shell script specified to be executed by application master");
    }

    if (fileExist(shellCommandPath)) {
        shellCommand = readContent(shellCommandPath);
    }

    if (fileExist(shellArgsPath)) {
        shellArgs = readContent(shellArgsPath);
    }

    if (cliParser.hasOption("shell_env")) {
        String shellEnvs[] = cliParser.getOptionValues("shell_env");
        for (String env : shellEnvs) {
            env = env.trim();
            int index = env.indexOf('=');
            if (index == -1) {
                shellEnv.put(env, "");
                continue;
            }
            String key = env.substring(0, index);
            String val = "";
            if (index < (env.length() - 1)) {
                val = env.substring(index + 1);
            }
            shellEnv.put(key, val);
        }
    }

    if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION)) {
        scriptPath = envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLOCATION);

        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP)) {
            shellScriptPathTimestamp = Long.parseLong(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTTIMESTAMP));
        }
        if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN)) {
            shellScriptPathLen = Long.parseLong(envs.get(DSConstants.DISTRIBUTEDSHELLSCRIPTLEN));
        }
        if (!scriptPath.isEmpty() && (shellScriptPathTimestamp <= 0 || shellScriptPathLen <= 0)) {
            LOG.error("Illegal values in env for shell script path" + ", path=" + scriptPath + ", len="
                    + shellScriptPathLen + ", timestamp=" + shellScriptPathTimestamp);
            throw new IllegalArgumentException("Illegal values in env for shell script path");
        }
    }

    if (envs.containsKey(DSConstants.HAZELLOCATION)) {
        hazelPath = envs.get(DSConstants.HAZELLOCATION);

        if (envs.containsKey(DSConstants.HAZELTIMESTAMP)) {
            hazelPathTimestamp = Long.parseLong(envs.get(DSConstants.HAZELTIMESTAMP));
        }
        if (envs.containsKey(DSConstants.HAZELLEN)) {
            hazelPathLen = Long.parseLong(envs.get(DSConstants.HAZELLEN));
        }
        if (!hazelPath.isEmpty() && (hazelPathTimestamp <= 0 || hazelPathLen <= 0)) {
            LOG.error("Illegal values in env for Hazelcast" + ", path=" + hazelPath + ", len=" + hazelPathLen
                    + ", timestamp=" + hazelPathTimestamp);
            throw new IllegalArgumentException("Illegal values in env for Hazelcast path");
        }
    }

    if (envs.containsKey(DSConstants.DISTRIBUTEDSHELLTIMELINEDOMAIN)) {
        domainId = envs.get(DSConstants.DISTRIBUTEDSHELLTIMELINEDOMAIN);
    }

    containerMemory = Integer.parseInt(cliParser.getOptionValue("container_memory", "10"));
    containerVirtualCores = Integer.parseInt(cliParser.getOptionValue("container_vcores", "1"));
    numTotalContainers = Integer.parseInt(cliParser.getOptionValue("num_containers", "1"));
    if (numTotalContainers == 0) {
        throw new IllegalArgumentException("Cannot run distributed shell with no containers");
    }
    requestPriority = Integer.parseInt(cliParser.getOptionValue("priority", "0"));
    return true;
}

From source file:oz.hadoop.yarn.test.cluster.InJvmContainerExecutor.java

License:Apache License

/**
 * Most of this code is copied from the super class's launchContainer method (unfortunately), since directory
 * and other preparation logic is tightly coupled with the actual container launch.
 * Would be nice if it was broken apart where launch method would be invoked when
 * everything is prepared/*from  w ww .  j  a v  a  2 s.c o  m*/
 */
private void prepareContainerDirectories(Container container, Path nmPrivateContainerScriptPath,
        Path nmPrivateTokensPath, String userName, String appId, Path containerWorkDir, List<String> localDirs,
        List<String> logDirs) {

    FsPermission dirPerm = new FsPermission(APPDIR_PERM);
    ContainerId containerId = container.getContainerId();
    String containerIdStr = ConverterUtils.toString(containerId);
    String appIdStr = ConverterUtils.toString(containerId.getApplicationAttemptId().getApplicationId());

    try {
        for (String sLocalDir : localDirs) {
            Path usersdir = new Path(sLocalDir, ContainerLocalizer.USERCACHE);
            Path userdir = new Path(usersdir, userName);
            Path appCacheDir = new Path(userdir, ContainerLocalizer.APPCACHE);
            Path appDir = new Path(appCacheDir, appIdStr);
            Path containerDir = new Path(appDir, containerIdStr);
            createDir(containerDir, dirPerm, true);
        }

        // Create the container log-dirs on all disks
        this.createLogDirs(appIdStr, containerIdStr, logDirs);

        Path tmpDir = new Path(containerWorkDir, YarnConfiguration.DEFAULT_CONTAINER_TEMP_DIR);
        createDir(tmpDir, dirPerm, false);

        // copy launch script to work dir
        Path launchDst = new Path(containerWorkDir, ContainerLaunch.CONTAINER_SCRIPT);
        fc.util().copy(nmPrivateContainerScriptPath, launchDst);

        // copy container tokens to work dir
        Path tokenDst = new Path(containerWorkDir, ContainerLaunch.FINAL_CONTAINER_TOKENS_FILE);
        fc.util().copy(nmPrivateTokensPath, tokenDst);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to prepare container directories for container " + container,
                e);
    }
}

From source file:x10.x10rt.yarn.ApplicationMaster.java

License:Open Source License

public ApplicationMaster(String[] args) throws Exception {
    this.conf = new YarnConfiguration();
    Map<String, String> envs = System.getenv();

    ContainerId containerId = ConverterUtils.toContainerId(envs.get(Environment.CONTAINER_ID.name()));
    appAttemptID = containerId.getApplicationAttemptId();

    LOG.info("Application master for app" + ", appId=" + appAttemptID.getApplicationId().getId()
            + ", clustertimestamp=" + appAttemptID.getApplicationId().getClusterTimestamp() + ", attemptId="
            + appAttemptID.getAttemptId());

    initialNumPlaces = Integer.parseInt(envs.get(X10_NPLACES));
    coresPerPlace = Integer.parseInt(envs.get(X10_NTHREADS));
    nthreads = coresPerPlace;/*www. jav a2 s. c  o m*/
    links = new HashMap<Integer, ApplicationMaster.CommunicationLink>(initialNumPlaces);
    places = new HashMap<ContainerId, Integer>(initialNumPlaces);
    pendingReads = new HashMap<SocketChannel, ByteBuffer>();
    selector = Selector.open();
    this.args = args;
    this.appName = System.getProperty(X10_YARN_MAIN);

    // look for max memory argument
    this.memoryPerPlaceInMb = 4024; // default of 1Gb per place
    for (int i = 0; i < args.length; i++) {
        try {
            if (args[i].startsWith("-Xmx")) {
                if (args[i].toLowerCase().endsWith("g"))
                    this.memoryPerPlaceInMb = Integer.parseInt(args[i].substring(4, args[i].length() - 1))
                            * 1024;
                else if (args[i].toLowerCase().endsWith("m"))
                    this.memoryPerPlaceInMb = Integer.parseInt(args[i].substring(4, args[i].length() - 1));
                else if (args[i].toLowerCase().endsWith("k"))
                    this.memoryPerPlaceInMb = Integer.parseInt(args[i].substring(4, args[i].length() - 1))
                            / 1024;
                else
                    this.memoryPerPlaceInMb = Integer.parseInt(args[i].substring(4)) / 1024 / 1024;
                break;
            }
        } catch (NumberFormatException e) {
            // ignore, use default value
            e.printStackTrace();
        }
    }

    if (envs.containsKey(X10YARNENV_ + X10_YARN_KILL)) {
        placeKiller = new ScheduledThreadPoolExecutor(1);
        // things to kill takes the form place:delayInSeconds,place:delayInSeconds,etc.  e.g. "2:2,3:3" will kill place 2 after 2 seconds, 3 after 3 seconds
        String thingsToKill = System.getenv(X10YARNENV_ + X10_YARN_KILL);
        // TODO: format error checking
        String[] sets = thingsToKill.split(",");
        pendingKills = new HashMap<Integer, Integer>(sets.length);
        for (String set : sets) {
            String[] place_delay = set.split(":");
            int place = Integer.parseInt(place_delay[0]);
            int delay = Integer.parseInt(place_delay[1]);
            pendingKills.put(place, delay);
        }
    } else {
        placeKiller = null;
        pendingKills = null;
    }
}