Просмотр исходного кода

ZOOKEEPER-1087. ForceSync VM arguement not working when set to "no"

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1137864 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 14 лет назад
Родитель
Сommit
d5036dad60

+ 2 - 0
CHANGES.txt

@@ -231,6 +231,8 @@ BUGFIXES:
 
   ZOOKEEPER-1060. QuorumPeer takes a long time to shutdown (Vishal via fpj)
 
+  ZOOKEEPER-1087. ForceSync VM arguement not working when set to "no" (Nate Putnam via breed)
+
 IMPROVEMENTS:
   ZOOKEEPER-724. Improve junit test integration - log harness information 
   (phunt via mahadev)

+ 9 - 4
src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java

@@ -100,9 +100,6 @@ public class FileTxnLog implements TxnLog {
     static {
         LOG = LoggerFactory.getLogger(FileTxnLog.class);
 
-        forceSync =
-            !System.getProperty("zookeeper.forceSync", "yes").equals("no");
-
         String size = System.getProperty("zookeeper.preAllocSize");
         if (size != null) {
             try {
@@ -119,7 +116,7 @@ public class FileTxnLog implements TxnLog {
     volatile FileOutputStream fos = null;
 
     File logDir;
-    private static boolean forceSync = true;
+    private final boolean forceSync = !System.getProperty("zookeeper.forceSync", "yes").equals("no");;
     long dbId;
     private LinkedList<FileOutputStream> streamsToFlush =
         new LinkedList<FileOutputStream>();
@@ -385,6 +382,14 @@ public class FileTxnLog implements TxnLog {
         return fh.getDbid();
     }
 
+    /**
+     * the forceSync value. true if forceSync is enabled, false otherwise.
+     * @return the forceSync value
+     */
+    public boolean isForceSync() {
+        return forceSync;
+    }
+
     /**
      * a class that keeps track of the position 
      * in the input stream. The position points to offset

+ 22 - 0
src/java/test/org/apache/zookeeper/server/ZooKeeperServerTest.java

@@ -26,6 +26,7 @@ import org.apache.zookeeper.server.persistence.FileTxnLog;
 import org.apache.zookeeper.server.persistence.Util;
 import org.junit.Assert;
 import org.junit.Test;
+import org.junit.Assert;
 
 public class ZooKeeperServerTest extends ZKTestCase {
     @Test
@@ -92,4 +93,25 @@ public class ZooKeeperServerTest extends ZKTestCase {
         Assert.assertEquals(orig[4], filelist[2]);
     }
 
+    @Test
+    public void testForceSyncDefaultEnabled() {
+        File file = new File("foo.10027c6de");
+        FileTxnLog log = new FileTxnLog(file);
+        Assert.assertTrue(log.isForceSync());
+    }
+
+    @Test
+    public void testForceSyncDefaultDisabled() {
+        try {
+            File file = new File("foo.10027c6de");
+            System.setProperty("zookeeper.forceSync","no");
+            FileTxnLog log = new FileTxnLog(file);
+            Assert.assertFalse(log.isForceSync());
+        }
+        finally {
+            //Reset back to default.
+            System.setProperty("zookeeper.forceSync","yes");
+        }
+    }
+
 }