Example usage for org.apache.hadoop.yarn.api.records ContainerLaunchContext getTokens

List of usage examples for org.apache.hadoop.yarn.api.records ContainerLaunchContext getTokens

Introduction

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

Prototype

@Public
@Stable
public abstract ByteBuffer getTokens();

Source Link

Document

Get all the tokens needed by this container.

Usage

From source file:com.continuuity.weave.internal.yarn.Hadoop21YarnAppClient.java

License:Apache License

private void addRMToken(ContainerLaunchContext context) {
    if (!UserGroupInformation.isSecurityEnabled()) {
        return;/* www  .j  a va2  s.c o m*/
    }

    try {
        Credentials credentials = YarnUtils.decodeCredentials(context.getTokens());

        Configuration config = yarnClient.getConfig();
        Token<TokenIdentifier> token = ConverterUtils.convertFromYarn(
                yarnClient.getRMDelegationToken(new Text(YarnUtils.getYarnTokenRenewer(config))),
                YarnUtils.getRMAddress(config));

        LOG.info("Added RM delegation token {}", token);
        credentials.addToken(token.getService(), token);

        context.setTokens(YarnUtils.encodeCredentials(credentials));

    } catch (Exception e) {
        LOG.error("Fails to create credentials.", e);
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.tez.dag.app.rm.container.AMContainerHelpers.java

License:Apache License

@VisibleForTesting
public static ContainerLaunchContext createContainerLaunchContext(TezDAGID tezDAGID,
        Map<String, LocalResource> commonDAGLRs, Map<ApplicationAccessType, String> acls,
        ContainerId containerId, Map<String, LocalResource> localResources, Map<String, String> vertexEnv,
        String javaOpts, InetSocketAddress taskAttemptListenerAddress, Credentials credentials,
        AppContext appContext, Resource containerResource, Configuration conf) {

    ContainerLaunchContext commonContainerSpec = null;
    synchronized (commonContainerSpecLock) {
        if (!commonContainerSpecs.containsKey(tezDAGID)) {
            commonContainerSpec = createCommonContainerLaunchContext(acls, credentials, commonDAGLRs);
            commonContainerSpecs.put(tezDAGID, commonContainerSpec);
        } else {/* w w w  .  j a va  2 s.co m*/
            commonContainerSpec = commonContainerSpecs.get(tezDAGID);
        }

        // Ensure that we remove container specs for previous AMs to reduce
        // memory footprint
        if (lastDAGID == null) {
            lastDAGID = tezDAGID;
        } else if (!lastDAGID.equals(tezDAGID)) {
            commonContainerSpecs.remove(lastDAGID);
            lastDAGID = tezDAGID;
        }
    }

    // Fill in the fields needed per-container that are missing in the common
    // spec.
    Map<String, LocalResource> lResources = new TreeMap<String, LocalResource>();
    lResources.putAll(commonContainerSpec.getLocalResources());
    lResources.putAll(localResources);

    // Setup environment by cloning from common env.
    Map<String, String> env = commonContainerSpec.getEnvironment();
    Map<String, String> myEnv = new HashMap<String, String>(env.size());
    myEnv.putAll(env);
    myEnv.putAll(vertexEnv);

    String modifiedJavaOpts = TezClientUtils.maybeAddDefaultMemoryJavaOpts(javaOpts, containerResource,
            conf.getDouble(TezConfiguration.TEZ_CONTAINER_MAX_JAVA_HEAP_FRACTION,
                    TezConfiguration.TEZ_CONTAINER_MAX_JAVA_HEAP_FRACTION_DEFAULT));
    if (LOG.isDebugEnabled()) {
        if (!modifiedJavaOpts.equals(javaOpts)) {
            LOG.debug("Modified java opts for container" + ", containerId=" + containerId
                    + ", originalJavaOpts=" + javaOpts + ", modifiedJavaOpts=" + modifiedJavaOpts);
        }
    }

    List<String> commands = TezRuntimeChildJVM.getVMCommand(taskAttemptListenerAddress, containerId.toString(),
            appContext.getApplicationID().toString(), appContext.getApplicationAttemptId().getAttemptId(),
            modifiedJavaOpts);

    // Duplicate the ByteBuffers for access by multiple containers.
    Map<String, ByteBuffer> myServiceData = new HashMap<String, ByteBuffer>();
    for (Entry<String, ByteBuffer> entry : commonContainerSpec.getServiceData().entrySet()) {
        myServiceData.put(entry.getKey(), entry.getValue().duplicate());
    }

    // Construct the actual Container
    ContainerLaunchContext container = ContainerLaunchContext.newInstance(lResources, myEnv, commands,
            myServiceData, commonContainerSpec.getTokens().duplicate(), acls);

    return container;
}

From source file:org.apache.twill.internal.yarn.Hadoop23YarnAppClient.java

License:Apache License

/**
 * Overrides parent method to adds RM delegation token to the given context. If YARN is running with HA RM,
 * delegation tokens for each RM service will be added.
 *//*from  www  . j  a va 2  s.co m*/
protected void addRMToken(ContainerLaunchContext context, YarnClient yarnClient, ApplicationId appId) {
    if (!UserGroupInformation.isSecurityEnabled()) {
        return;
    }

    try {
        Text renewer = new Text(UserGroupInformation.getCurrentUser().getShortUserName());
        org.apache.hadoop.yarn.api.records.Token rmDelegationToken = yarnClient.getRMDelegationToken(renewer);

        // The following logic is copied from ClientRMProxy.getRMDelegationTokenService, which is not available in
        // YARN older than 2.4
        List<String> services = new ArrayList<>();
        if (HAUtil.isHAEnabled(configuration)) {
            // If HA is enabled, we need to enumerate all RM hosts
            // and add the corresponding service name to the token service
            // Copy the yarn conf since we need to modify it to get the RM addresses
            YarnConfiguration yarnConf = new YarnConfiguration(configuration);
            for (String rmId : HAUtil.getRMHAIds(configuration)) {
                yarnConf.set(YarnConfiguration.RM_HA_ID, rmId);
                InetSocketAddress address = yarnConf.getSocketAddr(YarnConfiguration.RM_ADDRESS,
                        YarnConfiguration.DEFAULT_RM_ADDRESS, YarnConfiguration.DEFAULT_RM_PORT);
                services.add(SecurityUtil.buildTokenService(address).toString());
            }
        } else {
            services.add(SecurityUtil.buildTokenService(YarnUtils.getRMAddress(configuration)).toString());
        }

        Credentials credentials = YarnUtils.decodeCredentials(context.getTokens());

        // casting needed for later Hadoop version
        @SuppressWarnings("RedundantCast")
        Token<TokenIdentifier> token = ConverterUtils.convertFromYarn(rmDelegationToken,
                (InetSocketAddress) null);

        token.setService(new Text(Joiner.on(',').join(services)));
        credentials.addToken(new Text(token.getService()), token);

        LOG.debug("Added RM delegation token {} for application {}", token, appId);
        credentials.addToken(token.getService(), token);

        context.setTokens(YarnUtils.encodeCredentials(credentials));

    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:yarnkit.container.ContainerLaunchContextFactory.java

License:Apache License

public ContainerLaunchContext duplicate(@CheckForNull ContainerLaunchContext original) {
    Preconditions.checkNotNull(original, "ContainerLaunchContext should not be null");

    ContainerLaunchContext copy = Records.newRecord(ContainerLaunchContext.class);
    copy.setCommands(original.getCommands());
    copy.setEnvironment(original.getEnvironment());
    copy.setLocalResources(original.getLocalResources());
    ByteBuffer token = original.getTokens();
    if (token != null) {
        copy.setTokens(token.duplicate());
    }// w w w. ja v a  2s  .  c o m
    return copy;
}