Author: eevans
Date: Fri Jan 22 20:10:26 2010
New Revision: 902247
URL: http://svn.apache.org/viewvc?rev=902247&view=rev
Log:
NodeProbe refactor: move main to new class
Patch by eevans for CASSANDRA-698
Added:
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeCmd.java
Modified:
incubator/cassandra/trunk/bin/nodeprobe
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeProbe.java
Modified: incubator/cassandra/trunk/bin/nodeprobe
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/bin/nodeprobe?rev=902247&r1=902246&r2=902247&view=diff
==============================================================================
--- incubator/cassandra/trunk/bin/nodeprobe (original)
+++ incubator/cassandra/trunk/bin/nodeprobe Fri Jan 22 20:10:26 2010
@@ -51,6 +51,6 @@
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
Added:
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeCmd.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeCmd.java?rev=902247&view=auto
==============================================================================
--- incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeCmd.java
(added)
+++ incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeCmd.java
Fri Jan 22 20:10:26 2010
@@ -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 <arg> <command>%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);
+ }
+}
Modified:
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeProbe.java
URL:
http://svn.apache.org/viewvc/incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeProbe.java?rev=902247&r1=902246&r2=902247&view=diff
==============================================================================
---
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeProbe.java
(original)
+++
incubator/cassandra/trunk/src/java/org/apache/cassandra/tools/NodeProbe.java
Fri Jan 22 20:10:26 2010
@@ -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 @@
{
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 @@
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 <arg> <command>%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);
- }
-
}