Example usage for org.apache.hadoop.yarn.api.records Container getNodeHttpAddress

List of usage examples for org.apache.hadoop.yarn.api.records Container getNodeHttpAddress

Introduction

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

Prototype

@Public
@Stable
public abstract String getNodeHttpAddress();

Source Link

Document

Get the http uri of the node on which the container is allocated.

Usage

From source file:tachyon.yarn.ApplicationMasterTest.java

License:Apache License

/**
 * Mocks mRMClient to randomly allocated one of the requested hosts.
 *
 * This involves/*from   ww w  .j ava2 s  .  c  om*/
 * 1) Creating NUM_WORKERS mock containers, each with a different mock host
 * 2) Mocking mYarnClient to return the mock hosts of the mock containers
 * 3) Mocking mRMClient.addContainerRequest to asynchronously call mMaster.onContainersAllocated
 * with a random container on a requested host
 *
 * @param numContainers the number of mock container hosts
 */
private void mockResourceManager(int numContainers) throws Exception {
    final Random random = new Random();
    final List<Container> mockContainers = Lists.newArrayList();
    List<NodeReport> nodeReports = Lists.newArrayList();
    List<String> hosts = Lists.newArrayList(MASTER_ADDRESS);
    for (int i = 0; i < numContainers - 1; i++) {
        String host = "host" + i;
        hosts.add(host);
    }
    for (String host : hosts) {
        Container mockContainer = Mockito.mock(Container.class);
        Mockito.when(mockContainer.getNodeHttpAddress()).thenReturn(host + ":8042");
        Mockito.when(mockContainer.getNodeId()).thenReturn(NodeId.newInstance(host, 0));
        mockContainers.add(mockContainer);
        NodeReport report = Mockito.mock(NodeReport.class);
        Mockito.when(report.getNodeId()).thenReturn(NodeId.newInstance(host, 0));
        nodeReports.add(report);
    }
    Mockito.when(YarnUtils.getNodeHosts(mYarnClient)).thenReturn(Sets.newHashSet(hosts));

    // Pretend to be the Resource Manager, allocating containers when they are requested.
    Mockito.doAnswer(new Answer<Void>() {
        @Override
        public Void answer(final InvocationOnMock invocation) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    // Allow the requests to interleave randomly
                    CommonUtils.sleepMs(50 + random.nextInt(200));
                    // Allocate a randomly chosen container from among the requested hosts
                    ContainerRequest request = invocation.getArgumentAt(0, ContainerRequest.class);
                    Set<String> requestedHosts = Sets.newHashSet(request.getNodes());
                    List<Container> requestedContainers = Lists.newArrayList();
                    for (Container container : mockContainers) {
                        if (requestedHosts.contains(container.getNodeId().getHost())) {
                            requestedContainers.add(container);
                        }
                    }
                    mMaster.onContainersAllocated(Lists
                            .newArrayList(requestedContainers.get(random.nextInt(requestedContainers.size()))));
                }
            }).start();
            return null;
        }
    }).when(mRMClient).addContainerRequest(Mockito.<ContainerRequest>any());
}

From source file:tachyon.yarn.ApplicationMasterTest.java

License:Apache License

/**
 * Tests that the Tachyon master container is launched properly.
 *//*from www . ja v  a2  s  . co  m*/
@Test
public void launchTachyonMasterContainersTest() throws Exception {
    Container mockContainer = Mockito.mock(Container.class);
    Mockito.when(mockContainer.getNodeHttpAddress()).thenReturn("1.2.3.4:8042");

    mMaster.onContainersAllocated(Lists.newArrayList(mockContainer));

    Mockito.verify(mNMClient).startContainer(Mockito.same(mockContainer),
            Mockito.argThat(getContextMatcher(EXPECTED_MASTER_CONTEXT)));
    Assert.assertEquals("1.2.3.4", mPrivateAccess.getMasterContainerNetAddress());
    Assert.assertTrue(mPrivateAccess.getMasterAllocated().getCount() == 0);
}