cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From eev...@apache.org
Subject svn commit: r915622 - in /incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli: CliMain.java CliOptions.java CliSessionState.java
Date Wed, 24 Feb 2010 00:42:18 GMT
Author: eevans
Date: Wed Feb 24 00:42:17 2010
New Revision: 915622

URL: http://svn.apache.org/viewvc?rev=915622&view=rev
Log:
less confusing handling of errors/exceptions

 * replaces some printed exceptions with human readable error messages,
   while re-enabling the printed exceptions if --debug is passed.
 * pull cluster name property on connect or startup, and output, (this
   also forces any connection errors to occur sooner).

Patch by Mark Wolfe; reviewed by eevans for CASSANDRA-807

Modified:
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
    incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java?rev=915622&r1=915621&r2=915622&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliMain.java
Wed Feb 24 00:42:17 2010
@@ -18,21 +18,22 @@
 
 package org.apache.cassandra.cli;
 
+import jline.ConsoleReader;
+import jline.History;
+import org.apache.cassandra.thrift.Cassandra;
 import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.transport.TFramedTransport;
 import org.apache.thrift.transport.TSocket;
 import org.apache.thrift.transport.TTransport;
-import org.apache.thrift.transport.TFramedTransport;
-
-import jline.*;
-import java.io.*;
 
-import org.apache.cassandra.thrift.Cassandra;
+import java.io.File;
+import java.io.IOException;
 
 import static org.apache.cassandra.db.Table.SYSTEM_TABLE;
 
-//
-// Cassandra Command Line Interface (CLI) Main
-//
+/**
+ * Cassandra Command Line Interface (CLI) Main
+ */
 public class CliMain
 {
     public final static String PROMPT = "cassandra";
@@ -44,17 +45,26 @@
     private static CliClient cliClient_;
     private static CliCompleter completer_ = new CliCompleter();
 
-    // Establish a thrift connection to cassandra instance
+    /**
+     * Establish a thrift connection to cassandra instance
+     *
+     * @param server - hostname or IP of the server
+     * @param port   - Thrift port number
+     */
     public static void connect(String server, int port)
     {
+
         TSocket socket = new TSocket(server, port);
 
         if (transport_ != null)
             transport_.close();
 
-        if (css_.framed) {
+        if (css_.framed)
+        {
             transport_ = new TFramedTransport(socket);
-        } else {
+        }
+        else 
+        {
             transport_ = socket;
         }
 
@@ -65,16 +75,38 @@
         {
             transport_.open();
         }
-        catch(Exception e)
+        catch (Exception e)
         {
             // Should move this to Log4J as well probably...
-            System.err.println("Exception " + e.getMessage());            
-            e.printStackTrace();
+            css_.err.format("Exception connecting to %s/%d - %s\n", server, port, e.getMessage());
+
+            if (css_.debug)
+                e.printStackTrace();
+
+            return;
         }
 
         thriftClient_ = cassandraClient;
         cliClient_ = new CliClient(css_, thriftClient_);
-        
+
+        // Lookup the cluster name, this is to make it clear which cluster the user is connected
to
+        String clusterName;
+
+        try
+        {
+            clusterName = thriftClient_.get_string_property("cluster name");
+        }
+        catch (Exception e)
+        {
+
+            css_.err.println("Exception retrieving information about the cassandra node,
check you have connected to the thrift port.");
+
+            if (css_.debug)
+                e.printStackTrace();
+
+            return;
+        }
+
         // Extend the completer with keyspace and column family data.
         try
         {
@@ -83,7 +115,7 @@
                 // Ignore system column family
                 if (keyspace.equals(SYSTEM_TABLE))
                     continue;
-                
+
                 for (String cf : cliClient_.getCFMetaData(keyspace).keySet())
                 {
                     for (String cmd : completer_.getKeyspaceCommands())
@@ -94,12 +126,18 @@
         catch (Exception e)
         {
             // Yes, we really do want to ignore any exceptions encountered here.
+            if (css_.debug)
+                e.printStackTrace();
+
+            return;
         }
 
-        css_.out.printf("Connected to %s/%d\n", server, port);
+        css_.out.printf("Connected to: \"%s\" on %s/%d%n", clusterName, server, port);
     }
 
-    // Disconnect thrift connection to cassandra instance
+    /**
+     * Disconnect thrift connection to cassandra instance
+     */
     public static void disconnect()
     {
         if (transport_ != null)
@@ -115,6 +153,9 @@
         css_.out.println("Type 'help' or '?' for help. Type 'quit' or 'exit' to quit.");
     }
 
+    /**
+     * Checks whether the thrift client is connected.
+     */
     public static boolean isConnected()
     {
         if (thriftClient_ == null)
@@ -131,16 +172,16 @@
         {
             cliClient_.executeCLIStmt(query);
         }
-        catch(Exception e)
+        catch (Exception e)
         {
             System.err.println("Exception " + e.getMessage());
-            e.printStackTrace(System.err);
+            if (css_.debug)
+                e.printStackTrace();
+
         }
-        return;
     }
 
-
-    public static void main(String args[]) throws IOException  
+    public static void main(String args[]) throws IOException
     {
         // process command line args
         CliOptions cliOptions = new CliOptions();
@@ -151,7 +192,7 @@
         {
             connect(css_.hostName, css_.thriftPort);
         }
-        else
+        else 
         {
             // If not, client must connect explicitly using the "connect" CLI statement.
             cliClient_ = new CliClient(css_, null);
@@ -161,14 +202,14 @@
         reader.addCompletor(completer_);
         reader.setBellEnabled(false);
 
-        String historyFile = System.getProperty("user.home") + File.separator  + HISTORYFILE;
+        String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
 
         reader.setHistory(new History(new File(historyFile)));
 
         printBanner();
 
         String line;
-        while ((line = reader.readLine(PROMPT+"> ")) != null)
+        while ((line = reader.readLine(PROMPT + "> ")) != null)
         {
             processCLIStmt(line);
         }

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java?rev=915622&r1=915621&r2=915622&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliOptions.java
Wed Feb 24 00:42:17 2010
@@ -19,6 +19,11 @@
 
 import org.apache.commons.cli.*;
 
+/**
+ *
+ * Used to process, and act upon the arguments passed to the CLI.
+ *
+ */
 public class CliOptions {
 
     private static Options options = null; // Info about command line options
@@ -28,6 +33,7 @@
     private static final String HOST_OPTION = "host";
     private static final String PORT_OPTION = "port";
     private static final String FRAME_OPTION = "framed";
+    private static final String DEBUG_OPTION = "debug";
 
     // Default values for optional command line arguments
     private static final int    DEFAULT_THRIFT_PORT = 9160;
@@ -40,12 +46,13 @@
         options.addOption(HOST_OPTION, true, "cassandra server's host name");
         options.addOption(PORT_OPTION, true, "cassandra server's thrift port");  
         options.addOption(FRAME_OPTION, false, "cassandra server's framed transport");  
+        options.addOption(DEBUG_OPTION, false, "display stack traces");  
     }
 
     private static void printUsage()
     {
         System.err.println("");
-        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--framed]");
+        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--framed] [--debug]");
         System.err.println("");
     }
 
@@ -81,10 +88,17 @@
         }
 
         // Look to see if frame has been specified
-        if (cmd.hasOption(FRAME_OPTION)) {
+        if (cmd.hasOption(FRAME_OPTION))
+        {
             css.framed = true;
         }
 
+        // Look to see if frame has been specified
+        if (cmd.hasOption(DEBUG_OPTION))
+        {
+            css.debug = true;
+        }
+
         // Look for optional args.
         if (cmd.hasOption(PORT_OPTION))
         {

Modified: incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
URL: http://svn.apache.org/viewvc/incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java?rev=915622&r1=915621&r2=915622&view=diff
==============================================================================
--- incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
(original)
+++ incubator/cassandra/branches/cassandra-0.6/src/java/org/apache/cassandra/cli/CliSessionState.java
Wed Feb 24 00:42:17 2010
@@ -21,12 +21,16 @@
 import java.io.InputStream;
 import java.io.PrintStream;
 
-public class CliSessionState {
+/**
+ * Used to hold the state for the CLI.
+ */
+public class CliSessionState
+{
 
-    public boolean timingOn = false;
     public String  hostName;       // cassandra server name
     public int     thriftPort;     // cassandra server's thrift port
     public boolean framed = false; // cassandra server's framed transport 
+    public boolean debug = false;  // print stack traces when errors occur in the CLI
 
     /*
      * Streams to read/write from
@@ -35,7 +39,8 @@
     public PrintStream out;
     public PrintStream err;
 
-    public CliSessionState() {
+    public CliSessionState()
+    {
         in = System.in;
         out = System.out;
         err = System.err;



Mime
View raw message