ape.SuspendNodeCommand.java Source code

Java tutorial

Introduction

Here is the source code for ape.SuspendNodeCommand.java

Source

/**
  * Copyright (c) 2012 Yahoo! Inc. All rights reserved.
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *   http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License. See accompanying LICENSE file.
*/
package ape;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;

/**
 * This command suspends a running Hadoop process which must be either a tasktracker or a datanode.
 * 
 * This is the equivalent of running "kill -19 PID" on the command line.
 * 
 *
 */
public class SuspendNodeCommand extends ApeCommand {
    private Option option;

    /**
     * The constructor for this command simply creates its Option object (used by
     * the CLI parser)
     */
    public SuspendNodeCommand() {
        option = OptionBuilder.withArgName("NodeType").hasArgs(1).withValueSeparator()
                .withDescription("Suspends a tasktracker or a datanode at the given hostname")
                .withLongOpt("suspend-node").create("s");
    }

    public String getName() {
        return option.getLongOpt();
    }

    public Option getOption() {
        return option;
    }

    public boolean runImpl(String[] args) throws IOException {
        String nodeType = null;
        nodeType = args[0];

        if (Main.VERBOSE)
            System.out.println("Nodetype" + args[0]);

        Process p;

        if (nodeType.toLowerCase().equals("datanode") || nodeType.toLowerCase().equals("tasktracker")
                || nodeType.toLowerCase().equals("jobtracker") || nodeType.toLowerCase().equals("namenode")) {
            System.out.println("Suspend is about to execute the following command:");
            String cmd = "echo \"kill -19 \\$(ps aux | grep -i " + nodeType
                    + " | grep -v grep | grep -v ape | awk -F ' '  '{print \\$2}')\" > kill-node.sh && chmod +x kill-node.sh && ./kill-node.sh";
            System.out.println(cmd);
            /**
             * The above code block checks to see if a valid node type is passed.
             * If it is, then it sends a very ugly bash command to kill the corresponding process.
             * It gets a list of running processes, then greps it for the node type, then 
             * removes the result generated by running grep, then it gets the process ID of that line 
             * 
             */

            ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
            pb.redirectErrorStream(true);
            Process sh;
            try {
                sh = pb.start();
            } catch (IOException e1) {
                e1.printStackTrace();
                return false;
            }

            try {
                sh.waitFor();
            } catch (InterruptedException e) {
                System.out.println("The suspend node command caught an Interrupt...");
                e.printStackTrace();
                return false;
            }
            InputStream shIn = sh.getInputStream();
            int c;
            while ((c = shIn.read()) != -1) {
                System.out.write(c);
            }
            try {
                shIn.close();
            } catch (IOException e) {
                System.out.println("Could not close InputStream from the suspend process.");
                e.printStackTrace();
                return false;
            }
            return true;
        } else {
            System.err.println("Invalid node type.");
            return false;
        }
    }
}