Search in sources :

Example 81 with ContainerLaunchContext

use of org.apache.hadoop.yarn.api.records.ContainerLaunchContext in project hadoop by apache.

the class ContainerLaunch method call.

@Override
// dispatcher not typed
@SuppressWarnings("unchecked")
public Integer call() {
    if (!validateContainerState()) {
        return 0;
    }
    final ContainerLaunchContext launchContext = container.getLaunchContext();
    ContainerId containerID = container.getContainerId();
    String containerIdStr = containerID.toString();
    final List<String> command = launchContext.getCommands();
    int ret = -1;
    Path containerLogDir;
    try {
        Map<Path, List<String>> localResources = getLocalizedResources();
        final String user = container.getUser();
        // /////////////////////////// Variable expansion
        // Before the container script gets written out.
        List<String> newCmds = new ArrayList<String>(command.size());
        String appIdStr = app.getAppId().toString();
        String relativeContainerLogDir = ContainerLaunch.getRelativeContainerLogDir(appIdStr, containerIdStr);
        containerLogDir = dirsHandler.getLogPathForWrite(relativeContainerLogDir, false);
        recordContainerLogDir(containerID, containerLogDir.toString());
        for (String str : command) {
            // TODO: Should we instead work via symlinks without this grammar?
            newCmds.add(expandEnvironment(str, containerLogDir));
        }
        launchContext.setCommands(newCmds);
        Map<String, String> environment = launchContext.getEnvironment();
        // Make a copy of env to iterate & do variable expansion
        for (Entry<String, String> entry : environment.entrySet()) {
            String value = entry.getValue();
            value = expandEnvironment(value, containerLogDir);
            entry.setValue(value);
        }
        // /////////////////////////// End of variable expansion
        FileContext lfs = FileContext.getLocalFSFileContext();
        Path nmPrivateContainerScriptPath = dirsHandler.getLocalPathForWrite(getContainerPrivateDir(appIdStr, containerIdStr) + Path.SEPARATOR + CONTAINER_SCRIPT);
        Path nmPrivateTokensPath = dirsHandler.getLocalPathForWrite(getContainerPrivateDir(appIdStr, containerIdStr) + Path.SEPARATOR + String.format(ContainerLocalizer.TOKEN_FILE_NAME_FMT, containerIdStr));
        Path nmPrivateClasspathJarDir = dirsHandler.getLocalPathForWrite(getContainerPrivateDir(appIdStr, containerIdStr));
        DataOutputStream containerScriptOutStream = null;
        DataOutputStream tokensOutStream = null;
        // Select the working directory for the container
        Path containerWorkDir = dirsHandler.getLocalPathForWrite(ContainerLocalizer.USERCACHE + Path.SEPARATOR + user + Path.SEPARATOR + ContainerLocalizer.APPCACHE + Path.SEPARATOR + appIdStr + Path.SEPARATOR + containerIdStr, LocalDirAllocator.SIZE_UNKNOWN, false);
        recordContainerWorkDir(containerID, containerWorkDir.toString());
        String pidFileSubpath = getPidFileSubpath(appIdStr, containerIdStr);
        // pid file should be in nm private dir so that it is not 
        // accessible by users
        pidFilePath = dirsHandler.getLocalPathForWrite(pidFileSubpath);
        List<String> localDirs = dirsHandler.getLocalDirs();
        List<String> logDirs = dirsHandler.getLogDirs();
        List<String> filecacheDirs = getNMFilecacheDirs(localDirs);
        List<String> userLocalDirs = getUserLocalDirs(localDirs);
        List<String> containerLocalDirs = getContainerLocalDirs(localDirs);
        List<String> containerLogDirs = getContainerLogDirs(logDirs);
        if (!dirsHandler.areDisksHealthy()) {
            ret = ContainerExitStatus.DISKS_FAILED;
            throw new IOException("Most of the disks failed. " + dirsHandler.getDisksHealthReport(false));
        }
        try {
            // /////////// Write out the container-script in the nmPrivate space.
            List<Path> appDirs = new ArrayList<Path>(localDirs.size());
            for (String localDir : localDirs) {
                Path usersdir = new Path(localDir, ContainerLocalizer.USERCACHE);
                Path userdir = new Path(usersdir, user);
                Path appsdir = new Path(userdir, ContainerLocalizer.APPCACHE);
                appDirs.add(new Path(appsdir, appIdStr));
            }
            containerScriptOutStream = lfs.create(nmPrivateContainerScriptPath, EnumSet.of(CREATE, OVERWRITE));
            // Set the token location too.
            environment.put(ApplicationConstants.CONTAINER_TOKEN_FILE_ENV_NAME, new Path(containerWorkDir, FINAL_CONTAINER_TOKENS_FILE).toUri().getPath());
            // Sanitize the container's environment
            sanitizeEnv(environment, containerWorkDir, appDirs, userLocalDirs, containerLogDirs, localResources, nmPrivateClasspathJarDir);
            exec.prepareContainer(new ContainerPrepareContext.Builder().setContainer(container).setLocalizedResources(localResources).setUser(user).setContainerLocalDirs(containerLocalDirs).setCommands(launchContext.getCommands()).build());
            // Write out the environment
            exec.writeLaunchEnv(containerScriptOutStream, environment, localResources, launchContext.getCommands(), new Path(containerLogDirs.get(0)), user);
            // /////////// End of writing out container-script
            // /////////// Write out the container-tokens in the nmPrivate space.
            tokensOutStream = lfs.create(nmPrivateTokensPath, EnumSet.of(CREATE, OVERWRITE));
            Credentials creds = container.getCredentials();
            creds.writeTokenStorageToStream(tokensOutStream);
        // /////////// End of writing out container-tokens
        } finally {
            IOUtils.cleanup(LOG, containerScriptOutStream, tokensOutStream);
        }
        ret = launchContainer(new ContainerStartContext.Builder().setContainer(container).setLocalizedResources(localResources).setNmPrivateContainerScriptPath(nmPrivateContainerScriptPath).setNmPrivateTokensPath(nmPrivateTokensPath).setUser(user).setAppId(appIdStr).setContainerWorkDir(containerWorkDir).setLocalDirs(localDirs).setLogDirs(logDirs).setFilecacheDirs(filecacheDirs).setUserLocalDirs(userLocalDirs).setContainerLocalDirs(containerLocalDirs).setContainerLogDirs(containerLogDirs).build());
    } catch (Throwable e) {
        LOG.warn("Failed to launch container.", e);
        dispatcher.getEventHandler().handle(new ContainerExitEvent(containerID, ContainerEventType.CONTAINER_EXITED_WITH_FAILURE, ret, e.getMessage()));
        return ret;
    } finally {
        setContainerCompletedStatus(ret);
    }
    handleContainerExitCode(ret, containerLogDir);
    return ret;
}
Also used : Path(org.apache.hadoop.fs.Path) ContainerPrepareContext(org.apache.hadoop.yarn.server.nodemanager.executor.ContainerPrepareContext) DataOutputStream(java.io.DataOutputStream) ContainerExitEvent(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerExitEvent) ArrayList(java.util.ArrayList) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) IOException(java.io.IOException) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) List(java.util.List) ArrayList(java.util.ArrayList) FileContext(org.apache.hadoop.fs.FileContext) Credentials(org.apache.hadoop.security.Credentials)

Example 82 with ContainerLaunchContext

use of org.apache.hadoop.yarn.api.records.ContainerLaunchContext in project hadoop by apache.

the class TestLinuxContainerExecutorWithMocks method testContainerLaunchError.

@Test
public void testContainerLaunchError() throws IOException, ContainerExecutionException {
    // reinitialize executer
    Configuration conf = new Configuration();
    setupMockExecutor(MOCK_EXECUTOR_WITH_ERROR, conf);
    conf.set(YarnConfiguration.NM_LOCAL_DIRS, "file:///bin/echo");
    conf.set(YarnConfiguration.NM_LOG_DIRS, "file:///dev/null");
    LinuxContainerExecutor exec;
    LinuxContainerRuntime linuxContainerRuntime = new DefaultLinuxContainerRuntime(PrivilegedOperationExecutor.getInstance(conf));
    linuxContainerRuntime.initialize(conf);
    exec = new LinuxContainerExecutor(linuxContainerRuntime);
    mockExec = spy(exec);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            String diagnostics = (String) invocationOnMock.getArguments()[0];
            assertTrue("Invalid Diagnostics message: " + diagnostics, diagnostics.contains("badcommand"));
            return null;
        }
    }).when(mockExec).logOutput(any(String.class));
    dirsHandler = new LocalDirsHandlerService();
    dirsHandler.init(conf);
    mockExec.setConf(conf);
    String appSubmitter = "nobody";
    String cmd = String.valueOf(PrivilegedOperation.RunAsUserCommand.LAUNCH_CONTAINER.getValue());
    String appId = "APP_ID";
    String containerId = "CONTAINER_ID";
    Container container = mock(Container.class);
    ContainerId cId = mock(ContainerId.class);
    ContainerLaunchContext context = mock(ContainerLaunchContext.class);
    HashMap<String, String> env = new HashMap<String, String>();
    when(container.getContainerId()).thenReturn(cId);
    when(container.getLaunchContext()).thenReturn(context);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            ContainerDiagnosticsUpdateEvent event = (ContainerDiagnosticsUpdateEvent) invocationOnMock.getArguments()[0];
            assertTrue("Invalid Diagnostics message: " + event.getDiagnosticsUpdate(), event.getDiagnosticsUpdate().contains("badcommand"));
            return null;
        }
    }).when(container).handle(any(ContainerDiagnosticsUpdateEvent.class));
    when(cId.toString()).thenReturn(containerId);
    when(context.getEnvironment()).thenReturn(env);
    Path scriptPath = new Path("file:///bin/echo");
    Path tokensPath = new Path("file:///dev/null");
    Path workDir = new Path("/tmp");
    Path pidFile = new Path(workDir, "pid.txt");
    mockExec.activateContainer(cId, pidFile);
    int ret = mockExec.launchContainer(new ContainerStartContext.Builder().setContainer(container).setNmPrivateContainerScriptPath(scriptPath).setNmPrivateTokensPath(tokensPath).setUser(appSubmitter).setAppId(appId).setContainerWorkDir(workDir).setLocalDirs(dirsHandler.getLocalDirs()).setLogDirs(dirsHandler.getLogDirs()).setFilecacheDirs(new ArrayList<>()).setUserLocalDirs(new ArrayList<>()).setContainerLocalDirs(new ArrayList<>()).setContainerLogDirs(new ArrayList<>()).build());
    Assert.assertNotSame(0, ret);
    assertEquals(Arrays.asList(YarnConfiguration.DEFAULT_NM_NONSECURE_MODE_LOCAL_USER, appSubmitter, cmd, appId, containerId, workDir.toString(), "/bin/echo", "/dev/null", pidFile.toString(), StringUtils.join(PrivilegedOperation.LINUX_FILE_PATH_SEPARATOR, dirsHandler.getLocalDirs()), StringUtils.join(PrivilegedOperation.LINUX_FILE_PATH_SEPARATOR, dirsHandler.getLogDirs()), "cgroups=none"), readMockParams());
}
Also used : Path(org.apache.hadoop.fs.Path) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) Configuration(org.apache.hadoop.conf.Configuration) HashMap(java.util.HashMap) LinuxContainerRuntime(org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.runtime.LinuxContainerRuntime) DefaultLinuxContainerRuntime(org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.runtime.DefaultLinuxContainerRuntime) ArrayList(java.util.ArrayList) ContainerDiagnosticsUpdateEvent(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerDiagnosticsUpdateEvent) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) DefaultLinuxContainerRuntime(org.apache.hadoop.yarn.server.nodemanager.containermanager.linux.runtime.DefaultLinuxContainerRuntime) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Container(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 83 with ContainerLaunchContext

use of org.apache.hadoop.yarn.api.records.ContainerLaunchContext in project hadoop by apache.

the class TestLinuxContainerExecutor method testPostExecuteAfterReacquisition.

@Test(timeout = 10000)
public void testPostExecuteAfterReacquisition() throws Exception {
    Assume.assumeTrue(shouldRun());
    // make up some bogus container ID
    ApplicationId appId = ApplicationId.newInstance(12345, 67890);
    ApplicationAttemptId attemptId = ApplicationAttemptId.newInstance(appId, 54321);
    ContainerId cid = ContainerId.newContainerId(attemptId, 9876);
    Configuration conf = new YarnConfiguration();
    conf.setClass(YarnConfiguration.NM_LINUX_CONTAINER_RESOURCES_HANDLER, TestResourceHandler.class, LCEResourcesHandler.class);
    LinuxContainerExecutor lce = new LinuxContainerExecutor();
    lce.setConf(conf);
    try {
        lce.init();
    } catch (IOException e) {
    // expected if LCE isn't setup right, but not necessary for this test
    }
    Container container = mock(Container.class);
    ContainerLaunchContext context = mock(ContainerLaunchContext.class);
    HashMap<String, String> env = new HashMap<>();
    when(container.getLaunchContext()).thenReturn(context);
    when(context.getEnvironment()).thenReturn(env);
    lce.reacquireContainer(new ContainerReacquisitionContext.Builder().setContainer(container).setUser("foouser").setContainerId(cid).build());
    assertTrue("postExec not called after reacquisition", TestResourceHandler.postExecContainers.contains(cid));
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) HashMap(java.util.HashMap) ApplicationAttemptId(org.apache.hadoop.yarn.api.records.ApplicationAttemptId) IOException(java.io.IOException) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) ContainerReacquisitionContext(org.apache.hadoop.yarn.server.nodemanager.executor.ContainerReacquisitionContext) Container(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) YarnConfiguration(org.apache.hadoop.yarn.conf.YarnConfiguration) ApplicationId(org.apache.hadoop.yarn.api.records.ApplicationId) Test(org.junit.Test)

Example 84 with ContainerLaunchContext

use of org.apache.hadoop.yarn.api.records.ContainerLaunchContext in project hadoop by apache.

the class TestContainerManager method testLocalingResourceWhileContainerRunning.

// Start the container
// While the container is running, localize new resources.
// Verify the symlink is created properly
@Test
public void testLocalingResourceWhileContainerRunning() throws Exception {
    // Real del service
    delSrvc = new DeletionService(exec);
    delSrvc.init(conf);
    ((NodeManager.NMContext) context).setContainerExecutor(exec);
    containerManager = createContainerManager(delSrvc);
    containerManager.init(conf);
    containerManager.start();
    // set up local resources
    Map<String, LocalResource> localResource = setupLocalResources("file", "symLink1");
    ContainerLaunchContext context = recordFactory.newRecordInstance(ContainerLaunchContext.class);
    context.setLocalResources(localResource);
    // a long running container - sleep
    context.setCommands(Arrays.asList("sleep 6"));
    ContainerId cId = createContainerId(0);
    // start the container
    StartContainerRequest scRequest = StartContainerRequest.newInstance(context, createContainerToken(cId, DUMMY_RM_IDENTIFIER, this.context.getNodeId(), user, this.context.getContainerTokenSecretManager()));
    StartContainersRequest allRequests = StartContainersRequest.newInstance(Arrays.asList(scRequest));
    containerManager.startContainers(allRequests);
    BaseContainerManagerTest.waitForContainerState(containerManager, cId, ContainerState.RUNNING);
    BaseContainerManagerTest.waitForApplicationState(containerManager, cId.getApplicationAttemptId().getApplicationId(), ApplicationState.RUNNING);
    checkResourceLocalized(cId, "symLink1");
    // Localize new local resources while container is running
    Map<String, LocalResource> localResource2 = setupLocalResources("file2", "symLink2");
    ResourceLocalizationRequest request = ResourceLocalizationRequest.newInstance(cId, localResource2);
    containerManager.localize(request);
    // Verify resource is localized and symlink is created.
    GenericTestUtils.waitFor(new Supplier<Boolean>() {

        public Boolean get() {
            try {
                checkResourceLocalized(cId, "symLink2");
                return true;
            } catch (Throwable e) {
                return false;
            }
        }
    }, 500, 20000);
    BaseContainerManagerTest.waitForContainerState(containerManager, cId, ContainerState.COMPLETE);
    // Verify container cannot localize resources while at non-running state.
    try {
        containerManager.localize(request);
        Assert.fail();
    } catch (YarnException e) {
        Assert.assertTrue(e.getMessage().contains("Cannot perform LOCALIZE"));
    }
}
Also used : StartContainersRequest(org.apache.hadoop.yarn.api.protocolrecords.StartContainersRequest) DeletionService(org.apache.hadoop.yarn.server.nodemanager.DeletionService) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) LocalResource(org.apache.hadoop.yarn.api.records.LocalResource) StartContainerRequest(org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) ResourceLocalizationRequest(org.apache.hadoop.yarn.api.protocolrecords.ResourceLocalizationRequest) Test(org.junit.Test)

Example 85 with ContainerLaunchContext

use of org.apache.hadoop.yarn.api.records.ContainerLaunchContext in project hadoop by apache.

the class TestContainerManager method testUnauthorizedRequests.

@Test
public void testUnauthorizedRequests() throws IOException, YarnException {
    containerManager.start();
    // Create a containerId that belongs to an unauthorized appId
    ContainerId cId = createContainerId(0, 1);
    // startContainers()
    ContainerLaunchContext containerLaunchContext = recordFactory.newRecordInstance(ContainerLaunchContext.class);
    StartContainerRequest scRequest = StartContainerRequest.newInstance(containerLaunchContext, createContainerToken(cId, DUMMY_RM_IDENTIFIER, context.getNodeId(), user, context.getContainerTokenSecretManager()));
    List<StartContainerRequest> list = new ArrayList<>();
    list.add(scRequest);
    StartContainersRequest allRequests = StartContainersRequest.newInstance(list);
    StartContainersResponse startResponse = containerManager.startContainers(allRequests);
    Assert.assertFalse("Should not be authorized to start container", startResponse.getSuccessfullyStartedContainers().contains(cId));
    Assert.assertTrue("Start container request should fail", startResponse.getFailedRequests().containsKey(cId));
    // Insert the containerId into context, make it as if it is running
    ContainerTokenIdentifier containerTokenIdentifier = BuilderUtils.newContainerTokenIdentifier(scRequest.getContainerToken());
    Container container = new ContainerImpl(conf, null, containerLaunchContext, null, metrics, containerTokenIdentifier, context);
    context.getContainers().put(cId, container);
    // stopContainers()
    List<ContainerId> containerIds = new ArrayList<>();
    containerIds.add(cId);
    StopContainersRequest stopRequest = StopContainersRequest.newInstance(containerIds);
    StopContainersResponse stopResponse = containerManager.stopContainers(stopRequest);
    Assert.assertFalse("Should not be authorized to stop container", stopResponse.getSuccessfullyStoppedContainers().contains(cId));
    Assert.assertTrue("Stop container request should fail", stopResponse.getFailedRequests().containsKey(cId));
    // getContainerStatuses()
    containerIds = new ArrayList<>();
    containerIds.add(cId);
    GetContainerStatusesRequest request = GetContainerStatusesRequest.newInstance(containerIds);
    GetContainerStatusesResponse response = containerManager.getContainerStatuses(request);
    Assert.assertEquals("Should not be authorized to get container status", response.getContainerStatuses().size(), 0);
    Assert.assertTrue("Get status request should fail", response.getFailedRequests().containsKey(cId));
}
Also used : StartContainersRequest(org.apache.hadoop.yarn.api.protocolrecords.StartContainersRequest) StartContainersResponse(org.apache.hadoop.yarn.api.protocolrecords.StartContainersResponse) GetContainerStatusesRequest(org.apache.hadoop.yarn.api.protocolrecords.GetContainerStatusesRequest) ArrayList(java.util.ArrayList) ContainerLaunchContext(org.apache.hadoop.yarn.api.records.ContainerLaunchContext) StartContainerRequest(org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest) ContainerTokenIdentifier(org.apache.hadoop.yarn.security.ContainerTokenIdentifier) GetContainerStatusesResponse(org.apache.hadoop.yarn.api.protocolrecords.GetContainerStatusesResponse) Container(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.Container) ContainerId(org.apache.hadoop.yarn.api.records.ContainerId) ContainerImpl(org.apache.hadoop.yarn.server.nodemanager.containermanager.container.ContainerImpl) StopContainersRequest(org.apache.hadoop.yarn.api.protocolrecords.StopContainersRequest) StopContainersResponse(org.apache.hadoop.yarn.api.protocolrecords.StopContainersResponse) Test(org.junit.Test)

Aggregations

ContainerLaunchContext (org.apache.hadoop.yarn.api.records.ContainerLaunchContext)130 Test (org.junit.Test)57 ArrayList (java.util.ArrayList)54 ContainerId (org.apache.hadoop.yarn.api.records.ContainerId)52 HashMap (java.util.HashMap)50 ApplicationId (org.apache.hadoop.yarn.api.records.ApplicationId)50 ApplicationSubmissionContext (org.apache.hadoop.yarn.api.records.ApplicationSubmissionContext)42 LocalResource (org.apache.hadoop.yarn.api.records.LocalResource)41 StartContainerRequest (org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest)40 Path (org.apache.hadoop.fs.Path)37 StartContainersRequest (org.apache.hadoop.yarn.api.protocolrecords.StartContainersRequest)37 ByteBuffer (java.nio.ByteBuffer)29 Resource (org.apache.hadoop.yarn.api.records.Resource)25 YarnConfiguration (org.apache.hadoop.yarn.conf.YarnConfiguration)25 IOException (java.io.IOException)24 Credentials (org.apache.hadoop.security.Credentials)23 File (java.io.File)22 ApplicationAttemptId (org.apache.hadoop.yarn.api.records.ApplicationAttemptId)22 GetContainerStatusesRequest (org.apache.hadoop.yarn.api.protocolrecords.GetContainerStatusesRequest)20 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)20