cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jasobr...@apache.org
Subject git commit: nodetool history logging patch by jasobrown; reviewed by Dave Brosius for CASSANDRA-5823
Date Wed, 31 Jul 2013 18:14:53 GMT
Updated Branches:
  refs/heads/cassandra-1.2 1a4942583 -> ba274adb7


nodetool history logging
patch by jasobrown; reviewed by Dave Brosius for CASSANDRA-5823


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/ba274adb
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/ba274adb
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/ba274adb

Branch: refs/heads/cassandra-1.2
Commit: ba274adb7a0ee5f85df93eb5f3e40423772e1c07
Parents: 1a49425
Author: Jason Brown <jasedbrown@gmail.com>
Authored: Mon Jul 29 11:17:29 2013 -0700
Committer: Jason Brown <jasedbrown@gmail.com>
Committed: Wed Jul 31 11:14:36 2013 -0700

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 bin/cqlsh                                       | 15 +++++++-
 .../org/apache/cassandra/cli/CliClient.java     | 17 +++++++--
 src/java/org/apache/cassandra/cli/CliMain.java  | 23 +++++++++---
 .../org/apache/cassandra/tools/NodeCmd.java     | 39 ++++++++++++++++++--
 .../org/apache/cassandra/utils/FBUtilities.java |  8 ++++
 6 files changed, 88 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index a809bc6..da1ec20 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -5,6 +5,7 @@
  * update default LCS sstable size to 160MB (CASSANDRA-5727)
  * Allow compacting 2Is via nodetool (CASSANDRA-5670)
  * Hex-encode non-String keys in OPP (CASSANDRA-5793)
+ * nodetool history logging (CASSANDRA-5823)
 
 
 1.2.8

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/bin/cqlsh
----------------------------------------------------------------------
diff --git a/bin/cqlsh b/bin/cqlsh
index 59aac0d..f0db1b3 100755
--- a/bin/cqlsh
+++ b/bin/cqlsh
@@ -118,8 +118,19 @@ from cqlshlib.formatting import format_by_type
 from cqlshlib.util import trim_if_present
 from cqlshlib.tracing import print_trace_session
 
-CONFIG_FILE = os.path.expanduser(os.path.join('~', '.cqlshrc'))
-HISTORY = os.path.expanduser(os.path.join('~', '.cqlsh_history'))
+HISTORY_DIR = os.path.expanduser(os.path.join('~', '.cassandra'))
+CONFIG_FILE = os.path.join(HISTORY_DIR, 'cqlshrc')
+HISTORY = os.path.join(HISTORY_DIR, 'cqlsh_history')
+if not os.path.exists(HISTORY_DIR):
+    os.mkdir(HISTORY_DIR)
+
+OLD_CONFIG_FILE = os.path.expanduser(os.path.join('~', '.cqlshrc'))
+if os.path.exists(OLD_CONFIG_FILE):
+    os.rename(OLD_CONFIG_FILE, CONFIG_FILE)
+OLD_HISTORY = os.path.expanduser(os.path.join('~', '.cqlsh_history'))
+if os.path.exists(OLD_HISTORY):
+    os.rename(OLD_HISTORY, HISTORY)
+
 DEFAULT_HOST = 'localhost'
 DEFAULT_PORT = 9160
 DEFAULT_CQLVER = '3'

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/src/java/org/apache/cassandra/cli/CliClient.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cli/CliClient.java b/src/java/org/apache/cassandra/cli/CliClient.java
index 6857aea..2229207 100644
--- a/src/java/org/apache/cassandra/cli/CliClient.java
+++ b/src/java/org/apache/cassandra/cli/CliClient.java
@@ -3032,6 +3032,7 @@ public class CliClient
 
     class CfAssumptions
     {
+        private static final String ASSUMPTIONS_FILENAME = "assumptions.json";
         //Map<KeySpace, Map<ColumnFamily, Map<Property, Value>>>
         private Map<String, Map<String, Map<String, String>>> assumptions;
         private boolean assumptionsChanged;
@@ -3041,8 +3042,16 @@ public class CliClient
         {
             assumptions = new HashMap<String, Map<String, Map<String, String>>>();
             assumptionsChanged = false;
-            assumptionDirectory = new File(System.getProperty("user.home"), ".cassandra-cli");
-            assumptionDirectory.mkdirs();
+            assumptionDirectory = FBUtilities.getToolsOutputDirectory();
+
+            File oldAssumptionDir = new File(System.getProperty("user.home") + File.separator
+ ".cassandra-cli");
+            if (oldAssumptionDir.exists())
+            {
+                File oldAssumptionFile = new File(oldAssumptionDir, ASSUMPTIONS_FILENAME);
+                if (oldAssumptionFile.exists())
+                    FileUtils.renameWithConfirm(oldAssumptionFile, new File(assumptionDirectory,
ASSUMPTIONS_FILENAME));
+                FileUtils.deleteRecursive(oldAssumptionDir);
+            }
         }
 
         public void addAssumption(String keyspace, String columnFamily, String property,
String value)
@@ -3088,7 +3097,7 @@ public class CliClient
 
         private void readAssumptions()
         {
-            File assumptionFile = new File(assumptionDirectory, "assumptions.json");
+            File assumptionFile = new File(assumptionDirectory, ASSUMPTIONS_FILENAME);
             if (assumptionFile.isFile())
             {
                 try
@@ -3152,7 +3161,7 @@ public class CliClient
         {
             if (assumptionsChanged)
             {
-                File assumptionFile = new File(assumptionDirectory, "assumptions.json");
+                File assumptionFile = new File(assumptionDirectory, ASSUMPTIONS_FILENAME);
                 try
                 {
                     JsonFactory f = new JsonFactory();

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/src/java/org/apache/cassandra/cli/CliMain.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cli/CliMain.java b/src/java/org/apache/cassandra/cli/CliMain.java
index 547f642..8c110c2 100644
--- a/src/java/org/apache/cassandra/cli/CliMain.java
+++ b/src/java/org/apache/cassandra/cli/CliMain.java
@@ -27,7 +27,9 @@ import java.util.*;
 import jline.ConsoleReader;
 import jline.History;
 import org.apache.cassandra.auth.IAuthenticator;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.cassandra.thrift.*;
+import org.apache.cassandra.utils.FBUtilities;
 import org.apache.thrift.TException;
 import org.apache.thrift.protocol.TBinaryProtocol;
 import org.apache.thrift.transport.TSocket;
@@ -38,7 +40,8 @@ import org.apache.thrift.transport.TTransport;
  */
 public class CliMain
 {
-    public final static String HISTORYFILE = ".cassandra.history";
+    public final static String OLD_HISTORYFILE = ".cassandra.history";
+    public final static String HISTORYFILE = "cli.history";
 
     private static TTransport transport = null;
     private static Cassandra.Client thriftClient = null;
@@ -283,17 +286,16 @@ public class CliMain
         {
             reader.addCompletor(completer);
             reader.setBellEnabled(false);
-
-            String historyFile = System.getProperty("user.home") + File.separator + HISTORYFILE;
+            File historyFile = handleHistoryFiles();
 
             try
             {
-                History history = new History(new File(historyFile));
+                History history = new History(historyFile);
                 reader.setHistory(history);
             }
             catch (IOException exp)
             {
-                sessionState.err.printf("Unable to open %s for writing %n", historyFile);
+                sessionState.err.printf("Unable to open %s for writing", historyFile.getAbsolutePath());
             }
         }
         else if (!sessionState.verbose) // if in batch mode but no verbose flag
@@ -346,6 +348,17 @@ public class CliMain
         }
     }
 
+    private static File handleHistoryFiles()
+    {
+        File outputDir = FBUtilities.getToolsOutputDirectory();
+        File historyFile = new File(outputDir, HISTORYFILE);
+        File oldHistoryFile = new File(System.getProperty("user.home"), OLD_HISTORYFILE);
+        if(oldHistoryFile.exists())
+            FileUtils.renameWithConfirm(oldHistoryFile, historyFile);
+
+        return historyFile;
+    }
+
     private static void evaluateFileStatements(BufferedReader reader) throws IOException
     {
         String line = "";

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/src/java/org/apache/cassandra/tools/NodeCmd.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/tools/NodeCmd.java b/src/java/org/apache/cassandra/tools/NodeCmd.java
index 0bedd4e..f6d4310 100644
--- a/src/java/org/apache/cassandra/tools/NodeCmd.java
+++ b/src/java/org/apache/cassandra/tools/NodeCmd.java
@@ -17,20 +17,21 @@
  */
 package org.apache.cassandra.tools;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintStream;
+import java.io.*;
 import java.lang.management.MemoryUsage;
 import java.net.ConnectException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.text.DecimalFormat;
+import java.text.SimpleDateFormat;
 import java.util.*;
 import java.util.Map.Entry;
 import java.util.concurrent.ExecutionException;
 
+import com.google.common.base.Joiner;
 import com.google.common.collect.LinkedHashMultimap;
 import com.google.common.collect.Maps;
+import org.apache.cassandra.utils.FBUtilities;
 import org.apache.commons.cli.*;
 import org.yaml.snakeyaml.Loader;
 import org.yaml.snakeyaml.TypeDescription;
@@ -55,6 +56,7 @@ import org.apache.cassandra.utils.Pair;
 
 public class NodeCmd
 {
+    private static final String HISTORYFILE = "nodetool.history";
     private static final Pair<String, String> SNAPSHOT_COLUMNFAMILY_OPT = Pair.create("cf",
"column-family");
     private static final Pair<String, String> HOST_OPT = Pair.create("h", "host");
     private static final Pair<String, String> PORT_OPT = Pair.create("p", "port");
@@ -1072,6 +1074,8 @@ public class NodeCmd
         }
         try
         {
+            //print history here after we've already determined we can reasonably call cassandra
+            printHistory(args, cmd);
             NodeCommand command = null;
 
             try
@@ -1083,7 +1087,6 @@ public class NodeCmd
                 badUse(e.getMessage());
             }
 
-
             NodeCmd nodeCmd = new NodeCmd(probe);
 
             // Execute the requested command.
@@ -1330,6 +1333,34 @@ public class NodeCmd
         System.exit(probe.isFailed() ? 1 : 0);
     }
 
+    private static void printHistory(String[] args, ToolCommandLine cmd)
+    {
+        //don't bother to print if no args passed (meaning, nodetool is just printing out
the sub-commands list)
+        if (args.length == 0)
+            return;
+        String cmdLine = Joiner.on(" ").skipNulls().join(args);
+        final String password = cmd.getOptionValue(PASSWORD_OPT.left);
+        if (password != null)
+            cmdLine = cmdLine.replace(password, "<hidden>");
+
+        FileWriter writer = null;
+        try
+        {
+            final String outputDir = FBUtilities.getToolsOutputDirectory().getCanonicalPath();
+            writer = new FileWriter(new File(outputDir, HISTORYFILE), true);
+            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss,SSS");
+            writer.append(sdf.format(new Date()) + ": " + cmdLine + "\n");
+        }
+        catch (IOException ioe)
+        {
+            //quietly ignore any errors about not being able to write out history
+        }
+        finally
+        {
+            FileUtils.closeQuietly(writer);
+        }
+    }
+
     private static Throwable findInnermostThrowable(Throwable ex)
     {
         Throwable inner = ex.getCause();

http://git-wip-us.apache.org/repos/asf/cassandra/blob/ba274adb/src/java/org/apache/cassandra/utils/FBUtilities.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/FBUtilities.java b/src/java/org/apache/cassandra/utils/FBUtilities.java
index 679e3df..2c9a505 100644
--- a/src/java/org/apache/cassandra/utils/FBUtilities.java
+++ b/src/java/org/apache/cassandra/utils/FBUtilities.java
@@ -37,6 +37,7 @@ import java.util.concurrent.TimeoutException;
 
 import com.google.common.base.Joiner;
 import com.google.common.collect.AbstractIterator;
+import org.apache.cassandra.io.util.FileUtils;
 import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -619,4 +620,11 @@ public class FBUtilities
                                buffer.getData().length, buffer.getLength(), size, object);
         return buffer.getData();
     }
+
+    public static File getToolsOutputDirectory()
+    {
+        File historyDir = new File(System.getProperty("user.home"), ".cassandra");
+        FileUtils.createDirectory(historyDir);
+        return historyDir;
+    }
 }


Mime
View raw message