You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by ma...@apache.org on 2013/10/04 09:44:23 UTC

[1/2] git commit: Use Java7 file APIs to get saner exceptions etc.

Updated Branches:
  refs/heads/trunk 6dda6f2d8 -> bbf3cc516


Use Java7 file APIs to get saner exceptions etc.

And since windows does not support moving a file on top of another
existing file, remove the old file before moving.

Patch by marcuse, reviewed by jbellis for CASSANDRA-5383.


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

Branch: refs/heads/trunk
Commit: 8dfd75de160a29516af315166649d5c66cec3113
Parents: 213f6bb
Author: Marcus Eriksson <ma...@spotify.com>
Authored: Fri Oct 4 09:37:22 2013 +0200
Committer: Marcus Eriksson <ma...@spotify.com>
Committed: Fri Oct 4 09:37:22 2013 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../db/compaction/LeveledManifest.java          |  4 ++
 .../org/apache/cassandra/io/util/FileUtils.java | 57 ++++++++++++++++----
 .../utils/BackgroundActivityMonitor.java        | 10 +---
 .../org/apache/cassandra/utils/CLibrary.java    |  2 -
 .../org/apache/cassandra/utils/FBUtilities.java |  7 +++
 6 files changed, 62 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index e08e5b6..acd4d52 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -16,6 +16,7 @@
  * Delete can potentially be skipped in batch (CASSANDRA-6115)
  * Allow alter keyspace on system_traces (CASSANDRA-6016)
  * Disallow empty column names in cql (CASSANDRA-6136)
+ * Use Java7 file-handling APIs and fix file moving on Windows (CASSANDRA-5383)
 Merged from 1.2:
  * Never return WriteTimeout for CL.ANY (CASSANDRA-6032)
  * Tracing should log write failure rather than raw exceptions (CASSANDRA-6133)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java b/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java
index 82aa2d6..7348c29 100644
--- a/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java
+++ b/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java
@@ -39,6 +39,7 @@ import org.apache.cassandra.dht.Bounds;
 import org.apache.cassandra.dht.Token;
 import org.apache.cassandra.io.sstable.*;
 import org.apache.cassandra.io.util.FileUtils;
+import org.apache.cassandra.utils.FBUtilities;
 import org.apache.cassandra.utils.Pair;
 
 public class LeveledManifest
@@ -595,6 +596,9 @@ public class LeveledManifest
         SSTableMetadata.serializer.legacySerialize(metadata, oldMetadata.right, descriptor, out);
         out.flush();
         out.close();
+        // we cant move a file on top of another file in windows:
+        if (!FBUtilities.isUnix())
+            FileUtils.delete(filename);
         FileUtils.renameWithConfirm(filename + "-tmp", filename);
     }
 }

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/src/java/org/apache/cassandra/io/util/FileUtils.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/io/util/FileUtils.java b/src/java/org/apache/cassandra/io/util/FileUtils.java
index a0be773..6b91bd3 100644
--- a/src/java/org/apache/cassandra/io/util/FileUtils.java
+++ b/src/java/org/apache/cassandra/io/util/FileUtils.java
@@ -22,7 +22,9 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.MappedByteBuffer;
 import java.nio.file.Files;
-import java.nio.file.Paths;
+import java.nio.file.AtomicMoveNotSupportedException;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
 import java.text.DecimalFormat;
 import java.util.Arrays;
 
@@ -74,9 +76,7 @@ public class FileUtils
 
         try
         {
-            // Avoiding getAbsolutePath() in case there is ever a difference between that and the the
-            // behavior of nio2.
-            Files.createLink(Paths.get(to.getPath()), Paths.get(from.getPath()));
+            Files.createLink(to.toPath(), from.toPath());
         }
         catch (IOException e)
         {
@@ -111,13 +111,27 @@ public class FileUtils
         assert file.exists() : "attempted to delete non-existing file " + file.getName();
         if (logger.isDebugEnabled())
             logger.debug("Deleting " + file.getName());
-        if (!file.delete())
-            throw new FSWriteError(new IOException("Failed to delete " + file.getAbsolutePath()), file);
+        try
+        {
+            Files.delete(file.toPath());
+        }
+        catch (IOException e)
+        {
+            throw new FSWriteError(e, file);
+        }
     }
 
     public static void renameWithOutConfirm(String from, String to)
     {
-        new File(from).renameTo(new File(to));
+        try
+        {
+            atomicMoveWithFallback(new File(from).toPath(), new File(to).toPath());
+        }
+        catch (IOException e)
+        {
+            if (logger.isTraceEnabled())
+                logger.trace("Could not move file "+from+" to "+to, e);
+        }
     }
 
     public static void renameWithConfirm(String from, String to)
@@ -132,10 +146,35 @@ public class FileUtils
             logger.debug((String.format("Renaming %s to %s", from.getPath(), to.getPath())));
         // this is not FSWE because usually when we see it it's because we didn't close the file before renaming it,
         // and Windows is picky about that.
-        if (!from.renameTo(to))
-            throw new RuntimeException(String.format("Failed to rename %s to %s", from.getPath(), to.getPath()));
+        try
+        {
+            atomicMoveWithFallback(from.toPath(), to.toPath());
+        }
+        catch (IOException e)
+        {
+            throw new RuntimeException(String.format("Failed to rename %s to %s", from.getPath(), to.getPath()), e);
+        }
     }
 
+    /**
+     * Move a file atomically, if it fails, it falls back to a non-atomic operation
+     * @param from
+     * @param to
+     * @throws IOException
+     */
+    private static void atomicMoveWithFallback(Path from, Path to) throws IOException
+    {
+        try
+        {
+            Files.move(from, to, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
+        }
+        catch (AtomicMoveNotSupportedException e)
+        {
+            logger.debug("Could not do an atomic move", e);
+            Files.move(from, to, StandardCopyOption.REPLACE_EXISTING);
+        }
+
+    }
     public static void truncate(String path, long size)
     {
         RandomAccessFile file;

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/src/java/org/apache/cassandra/utils/BackgroundActivityMonitor.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/BackgroundActivityMonitor.java b/src/java/org/apache/cassandra/utils/BackgroundActivityMonitor.java
index 7a8431e..bad9a17 100644
--- a/src/java/org/apache/cassandra/utils/BackgroundActivityMonitor.java
+++ b/src/java/org/apache/cassandra/utils/BackgroundActivityMonitor.java
@@ -52,7 +52,6 @@ public class BackgroundActivityMonitor
     public static final int IRQ_INDEX = 5;
     public static final int SOFTIRQ_INDEX = 6;
 
-    private static final String OPERATING_SYSTEM = System.getProperty("os.name").toLowerCase();
     private static final int NUM_CPUS = Runtime.getRuntime().availableProcessors();
     private static final String PROC_STAT_PATH = "/proc/stat";
 
@@ -71,18 +70,13 @@ public class BackgroundActivityMonitor
         }
         catch (IOException ex)
         {
-            if (isUnix())
+            if (FBUtilities.isUnix())
                 logger.warn("Couldn't open /proc/stats");
             statsFile = null;
         }
         reportThread.scheduleAtFixedRate(new BackgroundActivityReporter(), 1, 1, TimeUnit.SECONDS);
     }
 
-    public static boolean isUnix()
-    {
-        return OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix");
-    }
-
     private long[] readAndCompute() throws IOException
     {
         statsFile.seek(0);
@@ -155,7 +149,7 @@ public class BackgroundActivityMonitor
             catch (IOException e)
             {
                 // ignore;
-                if (isUnix())
+                if (FBUtilities.isUnix())
                     logger.warn("Couldn't read /proc/stats");
             }
             if (report == -1d)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/src/java/org/apache/cassandra/utils/CLibrary.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/CLibrary.java b/src/java/org/apache/cassandra/utils/CLibrary.java
index e6efbbd..6711098 100644
--- a/src/java/org/apache/cassandra/utils/CLibrary.java
+++ b/src/java/org/apache/cassandra/utils/CLibrary.java
@@ -76,8 +76,6 @@ public final class CLibrary
     private static native int mlockall(int flags) throws LastErrorException;
     private static native int munlockall() throws LastErrorException;
 
-    private static native int link(String from, String to) throws LastErrorException;
-
     // fcntl - manipulate file descriptor, `man 2 fcntl`
     public static native int fcntl(int fd, int command, long flags) throws LastErrorException;
 

http://git-wip-us.apache.org/repos/asf/cassandra/blob/8dfd75de/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 113cde0..8f26bf0 100644
--- a/src/java/org/apache/cassandra/utils/FBUtilities.java
+++ b/src/java/org/apache/cassandra/utils/FBUtilities.java
@@ -70,6 +70,8 @@ public class FBUtilities
     public static final BigInteger TWO = new BigInteger("2");
     private static final String DEFAULT_TRIGGER_DIR = "triggers";
 
+    private static final String OPERATING_SYSTEM = System.getProperty("os.name").toLowerCase();
+
     private static volatile InetAddress localInetAddress;
     private static volatile InetAddress broadcastInetAddress;
 
@@ -680,4 +682,9 @@ public class FBUtilities
         FileUtils.createDirectory(historyDir);
         return historyDir;
     }
+
+    public static boolean isUnix()
+    {
+        return OPERATING_SYSTEM.contains("nix") || OPERATING_SYSTEM.contains("nux") || OPERATING_SYSTEM.contains("aix");
+    }
 }


[2/2] git commit: Merge branch 'cassandra-2.0' into trunk

Posted by ma...@apache.org.
Merge branch 'cassandra-2.0' into trunk


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

Branch: refs/heads/trunk
Commit: bbf3cc5166a8279db851090e64997ffc00cb8f15
Parents: 6dda6f2 8dfd75d
Author: Marcus Eriksson <ma...@spotify.com>
Authored: Fri Oct 4 09:42:13 2013 +0200
Committer: Marcus Eriksson <ma...@spotify.com>
Committed: Fri Oct 4 09:42:13 2013 +0200

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../db/compaction/LeveledManifest.java          |  4 ++
 .../org/apache/cassandra/io/util/FileUtils.java | 57 ++++++++++++++++----
 .../utils/BackgroundActivityMonitor.java        | 10 +---
 .../org/apache/cassandra/utils/CLibrary.java    |  2 -
 .../org/apache/cassandra/utils/FBUtilities.java |  7 +++
 6 files changed, 62 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/bbf3cc51/CHANGES.txt
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/cassandra/blob/bbf3cc51/src/java/org/apache/cassandra/db/compaction/LeveledManifest.java
----------------------------------------------------------------------