Example usage for org.apache.hadoop.yarn.server.nodemanager.containermanager.container Container getContainerId

List of usage examples for org.apache.hadoop.yarn.server.nodemanager.containermanager.container Container getContainerId

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.server.nodemanager.containermanager.container Container getContainerId.

Prototype

ContainerId getContainerId();

Source Link

Usage

From source file:com.github.sakserv.minicluster.yarn.InJvmContainerExecutor.java

License:Apache License

/**
 * Will launch containers within the same JVM as this Container Executor. It
 * will do so by: - extracting Container's class name and program arguments
 * from the launch script (e.g., launch_container.sh) - Creating an isolated
 * ClassLoader for each container - Calling doLaunchContainer(..) method to
 * launch Container/*from   ww w. j  ava 2 s.c o m*/
 */
private int doLaunch(Container container, Path containerWorkDir) throws Exception {
    Map<String, String> environment = container.getLaunchContext().getEnvironment();
    EnvironmentUtils.putAll(environment);

    Set<URL> additionalClassPathUrls = this.filterAndBuildUserClasspath(container);

    ExecJavaCliParser javaCliParser = this.createExecCommandParser(containerWorkDir.toString());

    UserGroupInformation.setLoginUser(null);
    try {
        // create Isolated Class Loader for each container and set it as context
        // class loader
        URLClassLoader containerCl = new URLClassLoader(
                additionalClassPathUrls.toArray(additionalClassPathUrls.toArray(new URL[] {})), null);
        Thread.currentThread().setContextClassLoader(containerCl);
        String containerLauncher = javaCliParser.getMain();

        Class<?> containerClass = Class.forName(containerLauncher, true, containerCl);
        Method mainMethod = containerClass.getMethod("main", new Class[] { String[].class });
        mainMethod.setAccessible(true);
        String[] arguments = javaCliParser.getMainArguments();

        this.doLaunchContainer(containerClass, mainMethod, arguments);

    } catch (Exception e) {
        logger.error("Failed to launch container " + container, e);
        container.handle(new ContainerDiagnosticsUpdateEvent(container.getContainerId(), e.getMessage()));
        return -1;
    } finally {
        logger.info("Removing symlinks");
        this.cleanUp();
    }
    return 0;
}

From source file:com.hortonworks.minicluster.InJvmContainerExecutor.java

License:Apache License

/**
 * Will launch containers within the same JVM as this Container Executor. It
 * will do so by: - extracting Container's class name and program arguments
 * from the launch script (e.g., launch_container.sh) - Creating an isolated
 * ClassLoader for each container - Calling doLaunchContainer(..) method to
 * launch Container/*from w  w w.  j a  v a  2s  .c o  m*/
 */
private int doLaunch(Container container, Path containerWorkDir) {
    Map<String, String> environment = container.getLaunchContext().getEnvironment();
    EnvironmentUtils.putAll(environment);

    Set<URL> additionalClassPathUrls = this.filterAndBuildUserClasspath(container);

    ExecJavaCliParser javaCliParser = this.createExecCommandParser(containerWorkDir.toString());

    UserGroupInformation.setLoginUser(null);
    try {
        // create Isolated Class Loader for each container and set it as context
        // class loader
        URLClassLoader containerCl = new URLClassLoader(
                additionalClassPathUrls.toArray(additionalClassPathUrls.toArray(new URL[] {})), null);
        Thread.currentThread().setContextClassLoader(containerCl);
        String containerLauncher = javaCliParser.getMain();
        Class<?> containerClass = Class.forName(containerLauncher, true, containerCl);
        Method mainMethod = containerClass.getMethod("main", new Class[] { String[].class });
        mainMethod.setAccessible(true);
        String[] arguments = javaCliParser.getMainArguments();

        this.doLaunchContainer(containerClass, mainMethod, arguments);
    } catch (Exception e) {
        logger.error("Failed to launch container " + container, e);
        container.handle(new ContainerDiagnosticsUpdateEvent(container.getContainerId(), e.getMessage()));
        return -1;
    } finally {
        logger.info("Removing symlinks");
        this.cleanUp();
    }
    return 0;
}

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

License:Apache License

/**
 *
 */// www .j a v  a 2s. c  o m
private int doLaunch(Container container, Path containerWorkDir) {
    Set<Path> paths = this.getIncomingClassPathEntries(container);
    String currentClassPath = System.getProperty("java.class.path");
    final Set<URL> additionalClassPathUrls = new HashSet<>();
    if (logger.isDebugEnabled()) {
        logger.debug("Building additional classpath for the container: " + container);
    }
    List<String> ae = Arrays.asList(additionalClassPathExclusions); // for logging purposes
    for (Path path : paths) {
        String resourceName = path.getName();
        if (currentClassPath.contains(resourceName)) {
            if (logger.isDebugEnabled()) {
                logger.debug("\t skipping " + resourceName + ". Already in the classpath.");
            }
        } else {
            if (!this.shouldExclude(path.getName())) {
                if (logger.isDebugEnabled()) {
                    logger.debug("\t adding " + resourceName + " to the classpath");
                }
                try {
                    additionalClassPathUrls.add(path.toUri().toURL());
                } catch (Exception e) {
                    throw new IllegalArgumentException(e);
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                            "Excluding " + path.getName() + " based on 'additionalClassPathExclusions': " + ae);
                }
            }
        }
    }

    Map<String, String> environment = container.getLaunchContext().getEnvironment();
    try {
        URLClassLoader cl = new URLClassLoader(additionalClassPathUrls.toArray(new URL[] {}));
        String containerLauncher = environment.get("CONTAINER_LAUNCHER");
        Class<?> amClass = Class.forName(containerLauncher, true, cl);
        Method mainMethod = amClass.getMethod("main", new Class[] { String[].class });
        mainMethod.setAccessible(true);
        String mainArgs = environment.get("CONTAINER_ARG");
        String[] arguments = new String[] { mainArgs, containerLauncher };
        mainMethod.invoke(null, (Object) arguments);
    } catch (Exception e) {
        logger.error("Failed to launch container " + container, e);
        container.handle(new ContainerDiagnosticsUpdateEvent(container.getContainerId(), e.getMessage()));
        return -1968;
    }
    return 0;
}

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//w  w w.j av 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);
    }
}