Search in sources :

Example 1 with SamzaContainerLaunchException

use of org.apache.samza.clustermanager.SamzaContainerLaunchException in project samza by apache.

the class YarnContainerRunner method startContainer.

/**
   *    Runs a command as a process on the container. All binaries needed by the physical process are packaged in the URL
   *    specified by packagePath.
   */
private void startContainer(Path packagePath, Container container, Map<String, String> env, final String cmd) throws SamzaContainerLaunchException {
    log.info("starting container {} {} {} {}", new Object[] { packagePath, container, env, cmd });
    // TODO: SAMZA-1144 remove the customized approach for package resource and use the common one.
    // But keep it now for backward compatibility.
    // set the local package so that the containers and app master are provisioned with it
    LocalResource packageResource = Records.newRecord(LocalResource.class);
    URL packageUrl = ConverterUtils.getYarnUrlFromPath(packagePath);
    FileStatus fileStatus;
    try {
        fileStatus = packagePath.getFileSystem(yarnConfiguration).getFileStatus(packagePath);
    } catch (IOException ioe) {
        log.error("IO Exception when accessing the package status from the filesystem", ioe);
        throw new SamzaContainerLaunchException("IO Exception when accessing the package status from the filesystem");
    }
    packageResource.setResource(packageUrl);
    log.info("set package Resource in YarnContainerRunner for {}", packageUrl);
    packageResource.setSize(fileStatus.getLen());
    packageResource.setTimestamp(fileStatus.getModificationTime());
    packageResource.setType(LocalResourceType.ARCHIVE);
    packageResource.setVisibility(LocalResourceVisibility.APPLICATION);
    ByteBuffer allTokens;
    // copy tokens (copied from dist shell example)
    try {
        Credentials credentials = UserGroupInformation.getCurrentUser().getCredentials();
        DataOutputBuffer dob = new DataOutputBuffer();
        credentials.writeTokenStorageToStream(dob);
        // now remove the AM->RM token so that containers cannot access it
        Iterator iter = credentials.getAllTokens().iterator();
        while (iter.hasNext()) {
            TokenIdentifier token = ((Token) iter.next()).decodeIdentifier();
            if (token != null && token.getKind().equals(AMRMTokenIdentifier.KIND_NAME)) {
                iter.remove();
            }
        }
        allTokens = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
    } catch (IOException ioe) {
        log.error("IOException when writing credentials.", ioe);
        throw new SamzaContainerLaunchException("IO Exception when writing credentials to output buffer");
    }
    Map<String, LocalResource> localResourceMap = new HashMap<>();
    localResourceMap.put("__package", packageResource);
    // include the resources from the universal resource configurations
    LocalizerResourceMapper resourceMapper = new LocalizerResourceMapper(new LocalizerResourceConfig(config), yarnConfiguration);
    localResourceMap.putAll(resourceMapper.getResourceMap());
    ContainerLaunchContext context = Records.newRecord(ContainerLaunchContext.class);
    context.setEnvironment(env);
    context.setTokens(allTokens.duplicate());
    context.setCommands(new ArrayList<String>() {

        {
            add(cmd);
        }
    });
    context.setLocalResources(localResourceMap);
    log.debug("setting localResourceMap to {}", localResourceMap);
    log.debug("setting context to {}", context);
    StartContainerRequest startContainerRequest = Records.newRecord(StartContainerRequest.class);
    startContainerRequest.setContainerLaunchContext(context);
    try {
        nmClient.startContainer(container, context);
    } catch (YarnException ye) {
        log.error("Received YarnException when starting container: " + container.getId(), ye);
        throw new SamzaContainerLaunchException("Received YarnException when starting container: " + container.getId(), ye);
    } catch (IOException ioe) {
        log.error("Received IOException when starting container: " + container.getId(), ioe);
        throw new SamzaContainerLaunchException("Received IOException when starting container: " + container.getId(), ioe);
    }
}
Also used : AMRMTokenIdentifier(org.apache.hadoop.yarn.security.AMRMTokenIdentifier) TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) FileStatus(org.apache.hadoop.fs.FileStatus) HashMap(java.util.HashMap) Token(org.apache.hadoop.security.token.Token) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URL(org.apache.hadoop.yarn.api.records.URL) YarnException(org.apache.hadoop.yarn.exceptions.YarnException) StartContainerRequest(org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest) SamzaContainerLaunchException(org.apache.samza.clustermanager.SamzaContainerLaunchException) DataOutputBuffer(org.apache.hadoop.io.DataOutputBuffer) Iterator(java.util.Iterator) Credentials(org.apache.hadoop.security.Credentials)

Aggregations

IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 FileStatus (org.apache.hadoop.fs.FileStatus)1 DataOutputBuffer (org.apache.hadoop.io.DataOutputBuffer)1 Credentials (org.apache.hadoop.security.Credentials)1 Token (org.apache.hadoop.security.token.Token)1 TokenIdentifier (org.apache.hadoop.security.token.TokenIdentifier)1 StartContainerRequest (org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest)1 URL (org.apache.hadoop.yarn.api.records.URL)1 YarnException (org.apache.hadoop.yarn.exceptions.YarnException)1 AMRMTokenIdentifier (org.apache.hadoop.yarn.security.AMRMTokenIdentifier)1 SamzaContainerLaunchException (org.apache.samza.clustermanager.SamzaContainerLaunchException)1