Example usage for org.apache.hadoop.yarn.client.api NMClient getContainerStatus

List of usage examples for org.apache.hadoop.yarn.client.api NMClient getContainerStatus

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.client.api NMClient getContainerStatus.

Prototype

public abstract ContainerStatus getContainerStatus(ContainerId containerId, NodeId nodeId)
        throws YarnException, IOException;

Source Link

Document

Query the status of a container.

Usage

From source file:yrun.YarnRunnerApplicationMaster.java

License:Apache License

public void run(String[] args) throws Exception {
    JsonParser parser = new JsonParser();
    JsonElement element = parser.parse(new FileReader(YarnRunner.MASTER_JSON));

    LOG.info("Json [" + element + "]");

    JsonObject jsonObject = (JsonObject) element;
    int priority = jsonObject.get("priority").getAsInt();
    int numberOfContainers = jsonObject.get("numberOfContainers").getAsInt();
    int memory = jsonObject.get("memory").getAsInt();
    int vCores = jsonObject.get("vCores").getAsInt();
    String command = jsonObject.get("command").getAsString();

    // startHttpServer();
    // InetSocketAddress address = _server.getAddress();
    // LOG.info("Http server started at [" + address + "]");

    // String appHostName = "app-host-name";
    // int appHostPort = address.getPort();
    // String appTrackingUrl = "http://" + address.getHostName() + ":" +
    // appHostPort + "/";
    // LOG.info("App Tracking Url [" + appTrackingUrl + "]");

    // Initialize clients to ResourceManager and NodeManagers
    Configuration conf = new YarnConfiguration();

    AMRMClient<ContainerRequest> rmClient = AMRMClient.createAMRMClient();
    rmClient.init(conf);/*from   w  ww .j av  a  2  s . c om*/
    rmClient.start();

    NMClient nmClient = NMClient.createNMClient();
    nmClient.init(conf);
    nmClient.start();

    // Register with ResourceManager
    LOG.info("Register Application Master 0");
    String appHostName = "";
    int appHostPort = 0;
    String appTrackingUrl = "";
    rmClient.registerApplicationMaster(appHostName, appHostPort, appTrackingUrl);
    LOG.info("Register Application Master 1");

    // Priority for worker containers - priorities are intra-application
    Priority priorityRecord = Records.newRecord(Priority.class);
    priorityRecord.setPriority(priority);

    // Resource requirements for worker containers
    Resource capability = Records.newRecord(Resource.class);
    capability.setMemory(memory);
    capability.setVirtualCores(vCores);

    // Make container requests to ResourceManager
    for (int i = 0; i < numberOfContainers; ++i) {
        ContainerRequest containerAsk = new ContainerRequest(capability, null, null, priorityRecord);
        LOG.info("Making resource request for [" + i + "]");
        rmClient.addContainerRequest(containerAsk);
    }

    // Obtain allocated containers, launch and check for responses
    int responseId = 0;
    int completedContainers = 0;
    long lastReport = 0;

    List<Container> containers = new ArrayList<Container>();

    while (completedContainers < numberOfContainers) {
        if (lastReport + TimeUnit.SECONDS.toNanos(5) < System.nanoTime()) {
            for (Container container : containers) {
                ContainerId containerId = container.getId();
                NodeId nodeId = container.getNodeId();
                ContainerStatus containerStatus = nmClient.getContainerStatus(containerId, nodeId);
                LOG.info("NodeId [" + nodeId + "] Container Status [" + containerStatus + "]");

                // Figure out

            }
            lastReport = System.nanoTime();
        }

        AllocateResponse response = rmClient.allocate(responseId++);
        for (Container container : response.getAllocatedContainers()) {
            containers.add(container);
            // Launch container by create ContainerLaunchContext
            ContainerLaunchContext ctx = Records.newRecord(ContainerLaunchContext.class);
            ctx.setCommands(
                    Collections.singletonList(command + " 1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR
                            + "/stdout2" + " 2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr"));
            LOG.info("Launching container " + container.getId());
            nmClient.startContainer(container, ctx);
        }

        for (ContainerStatus status : response.getCompletedContainersStatuses()) {
            completedContainers++;
            LOG.info("Completed container " + status.getContainerId());
        }
        Thread.sleep(100);
    }
    // _server.stop(0);
    // Un-register with ResourceManager
    rmClient.unregisterApplicationMaster(FinalApplicationStatus.SUCCEEDED, "", "");
}