Example usage for org.apache.hadoop.yarn.api.records AMCommand AM_SHUTDOWN

List of usage examples for org.apache.hadoop.yarn.api.records AMCommand AM_SHUTDOWN

Introduction

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

Prototype

AMCommand AM_SHUTDOWN

To view the source code for org.apache.hadoop.yarn.api.records AMCommand AM_SHUTDOWN.

Click Source Link

Usage

From source file:com.yahoo.storm.yarn.MasterServer.java

License:Open Source License

private Thread initAndStartHeartbeat(final StormAMRMClient client, final BlockingQueue<Container> launcherQueue,
        final int heartBeatIntervalMs) {
    Thread thread = new Thread() {
        @Override/*from ww w . j a va 2 s . c  om*/
        public void run() {
            try {
                while (client.getServiceState() == Service.STATE.STARTED
                        && !Thread.currentThread().isInterrupted()) {

                    Thread.sleep(heartBeatIntervalMs);

                    // We always send 50% progress.
                    AllocateResponse allocResponse = client.allocate(0.5f);

                    AMCommand am_command = allocResponse.getAMCommand();
                    if (am_command != null
                            && (am_command == AMCommand.AM_SHUTDOWN || am_command == AMCommand.AM_RESYNC)) {
                        LOG.info("Got AM_SHUTDOWN or AM_RESYNC from the RM");
                        _handler.stop();
                        System.exit(0);
                    }

                    List<Container> allocatedContainers = allocResponse.getAllocatedContainers();
                    if (allocatedContainers.size() > 0) {
                        // Add newly allocated containers to the client.
                        LOG.info("HB: Received allocated containers (" + allocatedContainers.size() + ")");
                        client.addAllocatedContainers(allocatedContainers);
                        if (client.supervisorsAreToRun()) {
                            LOG.info("HB: Supervisors are to run, so queueing (" + allocatedContainers.size()
                                    + ") containers...");
                            launcherQueue.addAll(allocatedContainers);
                        } else {
                            LOG.info("HB: Supervisors are to stop, so releasing all containers...");
                            client.stopAllSupervisors();
                        }
                    }

                    List<ContainerStatus> completedContainers = allocResponse.getCompletedContainersStatuses();

                    if (completedContainers.size() > 0 && client.supervisorsAreToRun()) {
                        LOG.debug("HB: Containers completed (" + completedContainers.size()
                                + "), so releasing them.");
                        client.startAllSupervisors();
                    }

                }
            } catch (Throwable t) {
                // Something happened we could not handle.  Make sure the AM goes
                // down so that we are not surprised later on that our heart
                // stopped..
                LOG.error("Unhandled error in AM: ", t);
                _handler.stop();
                System.exit(1);
            }
        }
    };
    thread.start();
    return thread;
}