Example usage for org.apache.hadoop.yarn.api.records ApplicationSubmissionContext getAMContainerSpec

List of usage examples for org.apache.hadoop.yarn.api.records ApplicationSubmissionContext getAMContainerSpec

Introduction

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

Prototype

@Public
@Stable
public abstract ContainerLaunchContext getAMContainerSpec();

Source Link

Document

Get the ContainerLaunchContext to describe the Container with which the ApplicationMaster is launched.

Usage

From source file:org.apache.tez.client.LocalClient.java

License:Apache License

protected Thread createDAGAppMaster(final ApplicationSubmissionContext appContext) {
    Thread thread = new Thread(new Runnable() {
        @Override//from  www.ja  v  a 2s.com
        public void run() {
            try {
                ApplicationId appId = appContext.getApplicationId();

                // Set up working directory for DAGAppMaster
                Path staging = TezCommonUtils.getTezSystemStagingPath(conf, appId.toString());
                Path userDir = TezCommonUtils.getTezSystemStagingPath(conf, appId.toString() + "_wd");
                LOG.info("Using working directory: " + userDir.toUri().getPath());

                FileSystem fs = FileSystem.get(conf);
                // copy data from staging directory to working directory to simulate the resource localizing
                FileUtil.copy(fs, staging, fs, userDir, false, conf);
                // Prepare Environment
                Path logDir = new Path(userDir, "localmode-log-dir");
                Path localDir = new Path(userDir, "localmode-local-dir");
                fs.mkdirs(logDir);
                fs.mkdirs(localDir);

                UserGroupInformation.setConfiguration(conf);
                // Add session specific credentials to the AM credentials.
                ByteBuffer tokens = appContext.getAMContainerSpec().getTokens();

                Credentials amCredentials;
                if (tokens != null) {
                    amCredentials = TezCommonUtils.parseCredentialsBytes(tokens.array());
                } else {
                    amCredentials = new Credentials();
                }

                // Construct, initialize, and start the DAGAppMaster
                ApplicationAttemptId applicationAttemptId = ApplicationAttemptId.newInstance(appId, 0);
                ContainerId cId = ContainerId.newInstance(applicationAttemptId, 1);
                String currentHost = InetAddress.getLocalHost().getHostName();
                int nmPort = YarnConfiguration.DEFAULT_NM_PORT;
                int nmHttpPort = YarnConfiguration.DEFAULT_NM_WEBAPP_PORT;
                long appSubmitTime = System.currentTimeMillis();

                dagAppMaster = createDAGAppMaster(applicationAttemptId, cId, currentHost, nmPort, nmHttpPort,
                        new SystemClock(), appSubmitTime, isSession, userDir.toUri().getPath(),
                        new String[] { localDir.toUri().getPath() }, new String[] { logDir.toUri().getPath() },
                        amCredentials, UserGroupInformation.getCurrentUser().getShortUserName());
                clientHandler = new DAGClientHandler(dagAppMaster);
                DAGAppMaster.initAndStartAppMaster(dagAppMaster, conf);

            } catch (Throwable t) {
                LOG.fatal("Error starting DAGAppMaster", t);
                if (dagAppMaster != null) {
                    dagAppMaster.stop();
                }
                amFailException = t;
            }
        }
    });

    thread.setName("DAGAppMaster Thread");
    LOG.info("DAGAppMaster thread has been created");

    return thread;
}

From source file:org.apache.tez.client.TestTezClient.java

License:Apache License

public void testTezClient(boolean isSession) throws Exception {
    Map<String, LocalResource> lrs = Maps.newHashMap();
    String lrName1 = "LR1";
    lrs.put(lrName1, LocalResource.newInstance(URL.newInstance("file", "localhost", 0, "/test"),
            LocalResourceType.FILE, LocalResourceVisibility.PUBLIC, 1, 1));

    TezClientForTest client = configure(lrs, isSession);

    ArgumentCaptor<ApplicationSubmissionContext> captor = ArgumentCaptor
            .forClass(ApplicationSubmissionContext.class);
    when(client.mockYarnClient.getApplicationReport(client.mockAppId).getYarnApplicationState())
            .thenReturn(YarnApplicationState.RUNNING);
    client.start();// w w  w .ja va2s.  c o  m
    verify(client.mockYarnClient, times(1)).init((Configuration) any());
    verify(client.mockYarnClient, times(1)).start();
    if (isSession) {
        verify(client.mockYarnClient, times(1)).submitApplication(captor.capture());
        ApplicationSubmissionContext context = captor.getValue();
        Assert.assertEquals(3, context.getAMContainerSpec().getLocalResources().size());
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_AM_LOCAL_RESOURCES_PB_FILE_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_PB_BINARY_CONF_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources().containsKey(lrName1));
    } else {
        verify(client.mockYarnClient, times(0)).submitApplication(captor.capture());
    }

    String mockLR1Name = "LR1";
    Map<String, LocalResource> lrDAG = Collections.singletonMap(mockLR1Name,
            LocalResource.newInstance(URL.newInstance("file", "localhost", 0, "/test1"), LocalResourceType.FILE,
                    LocalResourceVisibility.PUBLIC, 1, 1));
    Vertex vertex = Vertex.create("Vertex", ProcessorDescriptor.create("P"), 1, Resource.newInstance(1, 1));
    DAG dag = DAG.create("DAG").addVertex(vertex).addTaskLocalFiles(lrDAG);
    DAGClient dagClient = client.submitDAG(dag);

    Assert.assertTrue(dagClient.getExecutionContext().contains(client.mockAppId.toString()));

    if (isSession) {
        verify(client.mockYarnClient, times(1)).submitApplication(captor.capture());
        verify(client.sessionAmProxy, times(1)).submitDAG((RpcController) any(), (SubmitDAGRequestProto) any());
    } else {
        verify(client.mockYarnClient, times(1)).submitApplication(captor.capture());
        ApplicationSubmissionContext context = captor.getValue();
        Assert.assertEquals(4, context.getAMContainerSpec().getLocalResources().size());
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_AM_LOCAL_RESOURCES_PB_FILE_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_PB_BINARY_CONF_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_PB_PLAN_BINARY_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources().containsKey(lrName1));
    }

    // add resources
    String lrName2 = "LR2";
    lrs.clear();
    lrs.put(lrName2, LocalResource.newInstance(URL.newInstance("file", "localhost", 0, "/test2"),
            LocalResourceType.FILE, LocalResourceVisibility.PUBLIC, 1, 1));
    client.addAppMasterLocalFiles(lrs);

    ApplicationId appId2 = ApplicationId.newInstance(0, 2);
    when(client.mockYarnClient.createApplication().getNewApplicationResponse().getApplicationId())
            .thenReturn(appId2);

    when(client.mockYarnClient.getApplicationReport(appId2).getYarnApplicationState())
            .thenReturn(YarnApplicationState.RUNNING);
    dag = DAG.create("DAG")
            .addVertex(Vertex.create("Vertex", ProcessorDescriptor.create("P"), 1, Resource.newInstance(1, 1)));
    dagClient = client.submitDAG(dag);

    if (isSession) {
        // same app master
        verify(client.mockYarnClient, times(1)).submitApplication(captor.capture());
        Assert.assertTrue(dagClient.getExecutionContext().contains(client.mockAppId.toString()));
        // additional resource is sent
        ArgumentCaptor<SubmitDAGRequestProto> captor1 = ArgumentCaptor.forClass(SubmitDAGRequestProto.class);
        verify(client.sessionAmProxy, times(2)).submitDAG((RpcController) any(), captor1.capture());
        SubmitDAGRequestProto proto = captor1.getValue();
        Assert.assertEquals(1, proto.getAdditionalAmResources().getLocalResourcesCount());
        Assert.assertEquals(lrName2, proto.getAdditionalAmResources().getLocalResources(0).getName());
    } else {
        // new app master
        Assert.assertTrue(dagClient.getExecutionContext().contains(appId2.toString()));
        verify(client.mockYarnClient, times(2)).submitApplication(captor.capture());
        // additional resource is added
        ApplicationSubmissionContext context = captor.getValue();
        Assert.assertEquals(5, context.getAMContainerSpec().getLocalResources().size());
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_AM_LOCAL_RESOURCES_PB_FILE_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_PB_BINARY_CONF_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources()
                .containsKey(TezConstants.TEZ_PB_PLAN_BINARY_NAME));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources().containsKey(lrName1));
        Assert.assertTrue(context.getAMContainerSpec().getLocalResources().containsKey(lrName2));
    }

    client.stop();
    if (isSession) {
        verify(client.sessionAmProxy, times(1)).shutdownSession((RpcController) any(),
                (ShutdownSessionRequestProto) any());
    }
    verify(client.mockYarnClient, times(1)).stop();
}

From source file:org.apache.tez.client.TestTezClientUtils.java

License:Apache License

@Test(timeout = 5000)
public void testAMLoggingOptsSimple() throws IOException, YarnException {

    TezConfiguration tezConf = new TezConfiguration();
    tezConf.set(TezConfiguration.TEZ_AM_LOG_LEVEL, "WARN");

    ApplicationId appId = ApplicationId.newInstance(1000, 1);
    DAG dag = DAG.create("testdag");
    dag.addVertex(Vertex.create("testVertex", ProcessorDescriptor.create("processorClassname"), 1)
            .setTaskLaunchCmdOpts("initialLaunchOpts"));
    AMConfiguration amConf = new AMConfiguration(tezConf, new HashMap<String, LocalResource>(),
            new Credentials());
    ApplicationSubmissionContext appSubmissionContext = TezClientUtils.createApplicationSubmissionContext(appId,
            dag, "amName", amConf, new HashMap<String, LocalResource>(), new Credentials(), false,
            new TezApiVersionInfo(), mock(HistoryACLPolicyManager.class));

    List<String> expectedCommands = new LinkedList<String>();
    expectedCommands.add("-Dlog4j.configuratorClass=org.apache.tez.common.TezLog4jConfigurator");
    expectedCommands.add("-Dlog4j.configuration=" + TezConstants.TEZ_CONTAINER_LOG4J_PROPERTIES_FILE);
    expectedCommands.add("-D" + YarnConfiguration.YARN_APP_CONTAINER_LOG_DIR + "="
            + ApplicationConstants.LOG_DIR_EXPANSION_VAR);
    expectedCommands.add("-D" + TezConstants.TEZ_ROOT_LOGGER_NAME + "=" + "WARN" + ","
            + TezConstants.TEZ_CONTAINER_LOGGER_NAME);

    List<String> commands = appSubmissionContext.getAMContainerSpec().getCommands();
    assertEquals(1, commands.size());// w  ww.  ja va  2  s . co  m
    for (String expectedCmd : expectedCommands) {
        assertTrue(commands.get(0).contains(expectedCmd));
    }

    Map<String, String> environment = appSubmissionContext.getAMContainerSpec().getEnvironment();
    String logEnv = environment.get(TezConstants.TEZ_CONTAINER_LOG_PARAMS);
    assertNull(logEnv);
}

From source file:org.apache.tez.client.TestTezClientUtils.java

License:Apache License

@Test(timeout = 5000)
public void testAMLoggingOptsPerLogger() throws IOException, YarnException {

    TezConfiguration tezConf = new TezConfiguration();
    tezConf.set(TezConfiguration.TEZ_AM_LOG_LEVEL,
            "WARN;org.apache.hadoop.ipc=DEBUG;org.apache.hadoop.security=DEBUG");

    ApplicationId appId = ApplicationId.newInstance(1000, 1);
    DAG dag = DAG.create("testdag");
    dag.addVertex(Vertex.create("testVertex", ProcessorDescriptor.create("processorClassname"), 1)
            .setTaskLaunchCmdOpts("initialLaunchOpts"));
    AMConfiguration amConf = new AMConfiguration(tezConf, new HashMap<String, LocalResource>(),
            new Credentials());
    ApplicationSubmissionContext appSubmissionContext = TezClientUtils.createApplicationSubmissionContext(appId,
            dag, "amName", amConf, new HashMap<String, LocalResource>(), new Credentials(), false,
            new TezApiVersionInfo(), mock(HistoryACLPolicyManager.class));

    List<String> expectedCommands = new LinkedList<String>();
    expectedCommands.add("-Dlog4j.configuratorClass=org.apache.tez.common.TezLog4jConfigurator");
    expectedCommands.add("-Dlog4j.configuration=" + TezConstants.TEZ_CONTAINER_LOG4J_PROPERTIES_FILE);
    expectedCommands.add("-D" + YarnConfiguration.YARN_APP_CONTAINER_LOG_DIR + "="
            + ApplicationConstants.LOG_DIR_EXPANSION_VAR);
    expectedCommands.add("-D" + TezConstants.TEZ_ROOT_LOGGER_NAME + "=" + "WARN" + ","
            + TezConstants.TEZ_CONTAINER_LOGGER_NAME);

    List<String> commands = appSubmissionContext.getAMContainerSpec().getCommands();
    assertEquals(1, commands.size());//from   w  w  w . jav  a 2s .  c om
    for (String expectedCmd : expectedCommands) {
        assertTrue(commands.get(0).contains(expectedCmd));
    }

    Map<String, String> environment = appSubmissionContext.getAMContainerSpec().getEnvironment();
    String logEnv = environment.get(TezConstants.TEZ_CONTAINER_LOG_PARAMS);
    assertEquals("org.apache.hadoop.ipc=DEBUG;org.apache.hadoop.security=DEBUG", logEnv);
}