From 76a6da8d266395ea63a3aaf8e592a18c19a20633 Mon Sep 17 00:00:00 2001 From: Eric Evans Date: Fri, 22 Jan 2010 20:10:26 +0000 Subject: [PATCH] NodeProbe refactor: move main to new class Patch by eevans for CASSANDRA-698 git-svn-id: https://svn.apache.org/repos/asf/incubator/cassandra/trunk@902247 13f79535-47bb-0310-9956-ffa450edef68 --- bin/nodeprobe | 2 +- .../org/apache/cassandra/tools/NodeCmd.java | 209 +++++++++++++++ .../org/apache/cassandra/tools/NodeProbe.java | 248 +----------------- 3 files changed, 211 insertions(+), 248 deletions(-) create mode 100644 src/java/org/apache/cassandra/tools/NodeCmd.java diff --git a/bin/nodeprobe b/bin/nodeprobe index 638f272f00ff..d9e82f1104e2 100755 --- a/bin/nodeprobe +++ b/bin/nodeprobe @@ -51,6 +51,6 @@ case "`uname`" in esac $JAVA -cp $CLASSPATH -Dstorage-config=$CASSANDRA_CONF \ - org.apache.cassandra.tools.NodeProbe $@ + org.apache.cassandra.tools.NodeCmd $@ # vi:ai sw=4 ts=4 tw=0 et diff --git a/src/java/org/apache/cassandra/tools/NodeCmd.java b/src/java/org/apache/cassandra/tools/NodeCmd.java new file mode 100644 index 000000000000..c14494e5b75e --- /dev/null +++ b/src/java/org/apache/cassandra/tools/NodeCmd.java @@ -0,0 +1,209 @@ +package org.apache.cassandra.tools; + +import java.io.IOException; + +import org.apache.cassandra.db.CompactionManager; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.CommandLineParser; +import org.apache.commons.cli.HelpFormatter; +import org.apache.commons.cli.Option; +import org.apache.commons.cli.Options; +import org.apache.commons.cli.ParseException; +import org.apache.commons.cli.PosixParser; + +public class NodeCmd { + private static final String HOST_OPTION = "host"; + private static final String PORT_OPTION = "port"; + private static final int defaultPort = 8080; + private static Options options = null; + + static + { + options = new Options(); + Option optHost = new Option(HOST_OPTION, true, "node hostname or ip address"); + optHost.setRequired(true); + options.addOption(optHost); + options.addOption(PORT_OPTION, true, "remote jmx agent port number"); + } + + /** + * Prints usage information to stdout. + */ + private static void printUsage() + { + HelpFormatter hf = new HelpFormatter(); + String header = String.format( + "%nAvailable commands: ring, info, cleanup, compact, cfstats, snapshot [name], clearsnapshot, " + + "tpstats, flush, repair, decommission, move, loadbalance, removetoken, " + + " getcompactionthreshold, setcompactionthreshold [minthreshold] ([maxthreshold])"); + String usage = String.format("java %s -host %n", NodeProbe.class.getName()); + hf.printHelp(usage, "", options, header); + } + + public static void main(String[] args) throws IOException, InterruptedException, ParseException + { + CommandLineParser parser = new PosixParser(); + CommandLine cmd = parser.parse(options, args); + + String host = cmd.getOptionValue(HOST_OPTION); + int port = defaultPort; + + String portNum = cmd.getOptionValue(PORT_OPTION); + if (portNum != null) + { + try + { + port = Integer.parseInt(portNum); + } + catch (NumberFormatException e) + { + throw new ParseException("Port must be a number"); + } + } + + NodeProbe probe = null; + try + { + probe = new NodeProbe(host, port); + } + catch (IOException ioe) + { + System.err.println("Error connecting to remote JMX agent!"); + ioe.printStackTrace(); + System.exit(3); + } + + if (cmd.getArgs().length < 1) + { + System.err.println("Missing argument for command."); + printUsage(); + System.exit(1); + } + + // Execute the requested command. + String[] arguments = cmd.getArgs(); + String cmdName = arguments[0]; + if (cmdName.equals("ring")) + { + probe.printRing(System.out); + } + else if (cmdName.equals("info")) + { + probe.printInfo(System.out); + } + else if (cmdName.equals("cleanup")) + { + probe.forceTableCleanup(); + } + else if (cmdName.equals("compact")) + { + probe.forceTableCompaction(); + } + else if (cmdName.equals("cfstats")) + { + probe.printColumnFamilyStats(System.out); + } + else if (cmdName.equals("decommission")) + { + probe.decommission(); + } + else if (cmdName.equals("loadbalance")) + { + probe.loadBalance(); + } + else if (cmdName.equals("move")) + { + if (arguments.length <= 1) + { + System.err.println("missing token argument"); + } + probe.move(arguments[1]); + } + else if (cmdName.equals("removetoken")) + { + if (arguments.length <= 1) + { + System.err.println("missing token argument"); + } + probe.removeToken(arguments[1]); + } + else if (cmdName.equals("snapshot")) + { + String snapshotName = ""; + if (arguments.length > 1) + { + snapshotName = arguments[1]; + } + probe.takeSnapshot(snapshotName); + } + else if (cmdName.equals("clearsnapshot")) + { + probe.clearSnapshot(); + } + else if (cmdName.equals("tpstats")) + { + probe.printThreadPoolStats(System.out); + } + else if (cmdName.equals("flush") || cmdName.equals("repair")) + { + if (cmd.getArgs().length < 2) + { + System.err.println("Missing keyspace argument."); + printUsage(); + System.exit(1); + } + + String[] columnFamilies = new String[cmd.getArgs().length - 2]; + for (int i = 0; i < columnFamilies.length; i++) + { + columnFamilies[i] = cmd.getArgs()[i + 2]; + } + if (cmdName.equals("flush")) + probe.forceTableFlush(cmd.getArgs()[1], columnFamilies); + else // cmdName.equals("repair") + probe.forceTableRepair(cmd.getArgs()[1], columnFamilies); + } + else if (cmdName.equals("getcompactionthreshold")) + { + probe.getCompactionThreshold(System.out); + } + else if (cmdName.equals("setcompactionthreshold")) + { + if (arguments.length < 2) + { + System.err.println("Missing threshold value(s)"); + printUsage(); + System.exit(1); + } + int minthreshold = Integer.parseInt(arguments[1]); + int maxthreshold = CompactionManager.instance.getMaximumCompactionThreshold(); + if (arguments.length > 2) + { + maxthreshold = Integer.parseInt(arguments[2]); + } + + if (minthreshold > maxthreshold) + { + System.err.println("Min threshold can't be greater than Max threshold"); + printUsage(); + System.exit(1); + } + + if (minthreshold < 2 && maxthreshold != 0) + { + System.err.println("Min threshold must be at least 2"); + printUsage(); + System.exit(1); + } + probe.setCompactionThreshold(minthreshold, maxthreshold); + } + else + { + System.err.println("Unrecognized command: " + cmdName + "."); + printUsage(); + System.exit(1); + } + + System.exit(0); + } +} diff --git a/src/java/org/apache/cassandra/tools/NodeProbe.java b/src/java/org/apache/cassandra/tools/NodeProbe.java index 6c1b7968b7d5..f170ccc4c29c 100644 --- a/src/java/org/apache/cassandra/tools/NodeProbe.java +++ b/src/java/org/apache/cassandra/tools/NodeProbe.java @@ -45,13 +45,6 @@ import org.apache.cassandra.db.CompactionManagerMBean; import org.apache.cassandra.dht.Range; import org.apache.cassandra.service.StorageServiceMBean; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.CommandLineParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; -import org.apache.commons.cli.PosixParser; /** * JMX client operations for Cassandra. @@ -60,60 +53,15 @@ public class NodeProbe { private static final String fmtUrl = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi"; private static final String ssObjName = "org.apache.cassandra.service:type=StorageService"; - private static final String HOST_OPTION = "host"; - private static final String PORT_OPTION = "port"; private static final int defaultPort = 8080; - private static Options options = null; - private CommandLine cmd = null; private String host; private int port; - + private MBeanServerConnection mbeanServerConn; private StorageServiceMBean ssProxy; private MemoryMXBean memProxy; private RuntimeMXBean runtimeProxy; private CompactionManagerMBean mcmProxy; - - static - { - options = new Options(); - Option optHost = new Option(HOST_OPTION, true, "node hostname or ip address"); - optHost.setRequired(true); - options.addOption(optHost); - options.addOption(PORT_OPTION, true, "remote jmx agent port number"); - } - - /** - * Creates a NodeProbe using command-line arguments. - * - * @param cmdArgs list of arguments passed on the command line - * @throws ParseException for missing required, or unrecognized options - * @throws IOException on connection failures - */ - private NodeProbe(String[] cmdArgs) throws ParseException, IOException, InterruptedException - { - parseArgs(cmdArgs); - this.host = cmd.getOptionValue(HOST_OPTION); - - String portNum = cmd.getOptionValue(PORT_OPTION); - if (portNum != null) - { - try - { - this.port = Integer.parseInt(portNum); - } - catch (NumberFormatException e) - { - throw new ParseException("Port must be a number"); - } - } - else - { - this.port = defaultPort; - } - - connect(); - } /** * Creates a NodeProbe using the specified JMX host and port. @@ -478,198 +426,4 @@ public void setCompactionThreshold(int minimumCompactionThreshold, int maximumCo mcmProxy.setMaximumCompactionThreshold(maximumCompactionThreshold); } } - - /** - * Parse the supplied command line arguments. - * - * @param args arguments passed on the command line - * @throws ParseException for missing required, or unrecognized options - */ - private void parseArgs(String[] args) throws ParseException - { - CommandLineParser parser = new PosixParser(); - cmd = parser.parse(options, args); - } - - /** - * Retrieve any non-option arguments passed on the command line. - * - * @return non-option command args - */ - private String[] getArgs() - { - return cmd.getArgs(); - } - - /** - * Prints usage information to stdout. - */ - private static void printUsage() - { - HelpFormatter hf = new HelpFormatter(); - String header = String.format( - "%nAvailable commands: ring, info, cleanup, compact, cfstats, snapshot [name], clearsnapshot, " + - "tpstats, flush, repair, decommission, move, loadbalance, removetoken, " + - " getcompactionthreshold, setcompactionthreshold [minthreshold] ([maxthreshold])"); - String usage = String.format("java %s -host %n", NodeProbe.class.getName()); - hf.printHelp(usage, "", options, header); - } - - /** - * @param args - */ - public static void main(String[] args) throws IOException, InterruptedException - { - NodeProbe probe = null; - try - { - probe = new NodeProbe(args); - } - catch (ParseException pe) - { - System.err.println(pe.getMessage()); - NodeProbe.printUsage(); - System.exit(1); - } - catch (IOException ioe) - { - System.err.println("Error connecting to remote JMX agent!"); - ioe.printStackTrace(); - System.exit(3); - } - - if (probe.getArgs().length < 1) - { - System.err.println("Missing argument for command."); - NodeProbe.printUsage(); - System.exit(1); - } - - // Execute the requested command. - String[] arguments = probe.getArgs(); - String cmdName = arguments[0]; - if (cmdName.equals("ring")) - { - probe.printRing(System.out); - } - else if (cmdName.equals("info")) - { - probe.printInfo(System.out); - } - else if (cmdName.equals("cleanup")) - { - probe.forceTableCleanup(); - } - else if (cmdName.equals("compact")) - { - probe.forceTableCompaction(); - } - else if (cmdName.equals("cfstats")) - { - probe.printColumnFamilyStats(System.out); - } - else if (cmdName.equals("decommission")) - { - probe.decommission(); - } - else if (cmdName.equals("loadbalance")) - { - probe.loadBalance(); - } - else if (cmdName.equals("move")) - { - if (arguments.length <= 1) - { - System.err.println("missing token argument"); - } - probe.move(arguments[1]); - } - else if (cmdName.equals("removetoken")) - { - if (arguments.length <= 1) - { - System.err.println("missing token argument"); - } - probe.removeToken(arguments[1]); - } - else if (cmdName.equals("snapshot")) - { - String snapshotName = ""; - if (arguments.length > 1) - { - snapshotName = arguments[1]; - } - probe.takeSnapshot(snapshotName); - } - else if (cmdName.equals("clearsnapshot")) - { - probe.clearSnapshot(); - } - else if (cmdName.equals("tpstats")) - { - probe.printThreadPoolStats(System.out); - } - else if (cmdName.equals("flush") || cmdName.equals("repair")) - { - if (probe.getArgs().length < 2) - { - System.err.println("Missing keyspace argument."); - NodeProbe.printUsage(); - System.exit(1); - } - - String[] columnFamilies = new String[probe.getArgs().length - 2]; - for (int i = 0; i < columnFamilies.length; i++) - { - columnFamilies[i] = probe.getArgs()[i + 2]; - } - if (cmdName.equals("flush")) - probe.forceTableFlush(probe.getArgs()[1], columnFamilies); - else // cmdName.equals("repair") - probe.forceTableRepair(probe.getArgs()[1], columnFamilies); - } - else if (cmdName.equals("getcompactionthreshold")) - { - probe.getCompactionThreshold(System.out); - } - else if (cmdName.equals("setcompactionthreshold")) - { - if (arguments.length < 2) - { - System.err.println("Missing threshold value(s)"); - NodeProbe.printUsage(); - System.exit(1); - } - int minthreshold = Integer.parseInt(arguments[1]); - int maxthreshold = CompactionManager.instance.getMaximumCompactionThreshold(); - if (arguments.length > 2) - { - maxthreshold = Integer.parseInt(arguments[2]); - } - - if (minthreshold > maxthreshold) - { - System.err.println("Min threshold can't be greater than Max threshold"); - NodeProbe.printUsage(); - System.exit(1); - } - - if (minthreshold < 2 && maxthreshold != 0) - { - System.err.println("Min threshold must be at least 2"); - NodeProbe.printUsage(); - System.exit(1); - } - probe.setCompactionThreshold(minthreshold, maxthreshold); - } - else - { - System.err.println("Unrecognized command: " + cmdName + "."); - NodeProbe.printUsage(); - System.exit(1); - } - - System.exit(0); - } - }