ape.ContinueNodeCommand.java Source code

Java tutorial

Introduction

Here is the source code for ape.ContinueNodeCommand.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 will start a Hadoop process that has been suspended using kill -19 datanodePID
 * 
 *
 */
public class ContinueNodeCommand extends ApeCommand {
    private Option option;

    /**
     * The constructor for this command simply creates its Option object (used by
     * the CLI parser)
     */
    public ContinueNodeCommand() {
        option = OptionBuilder.withArgName("NodeType").hasArgs(1).withValueSeparator().withDescription(
                "Continues a tasktracker or a datanode at the given hostname that has already been suspended")
                .withLongOpt("continue-node").create("e");
    }

    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 -18 \\$(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();
                Main.logger.info(e1);
                return false;
            }

            try {
                sh.waitFor();
            } catch (InterruptedException e) {
                System.out.println("The suspend node command caught an interrupt");
                e.printStackTrace();
                Main.logger.info("The suspend command caught an interrupt");
                Main.logger.info(e);
                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();
                Main.logger.info("Could not close InputStream from the suspend process.");
                Main.logger.info(e);
                return false;
            }
            return true;
        } else {
            System.err.println("Invalid node type.");
            return false;
        }
    }
}