Browse Source

ZOOKEEPER-1900

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1606841 13f79535-47bb-0310-9956-ffa450edef68
Alexander Shraer 11 years ago
parent
commit
6abd85938f

+ 2 - 0
CHANGES.txt

@@ -34,6 +34,8 @@ NEW FEATURES:
 
 BUGFIXES:
 
+  ZOOKEEPER-1900. NullPointerException in truncate (Camille Fournier)
+
   ZOOKEEPER-786. Exception in ZooKeeper.toString
   (Thomas Koch via phunt)
 

+ 5 - 0
src/java/main/org/apache/zookeeper/server/persistence/FileTxnLog.java

@@ -378,6 +378,11 @@ public class FileTxnLog implements TxnLog {
         try {
             itr = new FileTxnIterator(this.logDir, zxid);
             PositionInputStream input = itr.inputStream;
+            if(input == null) {
+                throw new IOException("No log files found to truncate! This could " +
+                        "happen if you still have snapshots from an old setup or " +
+                        "log files were deleted accidentally or dataLogDir was changed in zoo.cfg.");
+            }
             long pos = input.getPosition();
             // now, truncate at the current position
             RandomAccessFile raf=new RandomAccessFile(itr.logFile,"rw");

+ 1 - 1
src/java/main/org/apache/zookeeper/server/quorum/Observer.java

@@ -82,7 +82,7 @@ public class Observer extends Learner{
                     readPacket(qp);
                     processPacket(qp);
                 }
-            } catch (IOException e) {
+            } catch (Exception e) {
                 LOG.warn("Exception when observing the leader", e);
                 try {
                     sock.close();

+ 27 - 0
src/java/test/org/apache/zookeeper/test/TruncateTest.java

@@ -113,6 +113,33 @@ public class TruncateTest extends ZKTestCase {
         iter.close();
         ClientBase.recursiveDelete(tmpdir);
     }
+    
+    @Test
+    public void testTruncationNullLog() throws Exception {
+        File tmpdir = ClientBase.createTmpDir();
+        FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir);
+        ZKDatabase zkdb = new ZKDatabase(snaplog);
+
+        for (int i = 1; i <= 100; i++) {
+            append(zkdb, i);
+        }
+        File[] logs = snaplog.getDataDir().listFiles();
+        for(int i = 0; i < logs.length; i++) {
+            logs[i].delete();
+        }
+        try {
+            zkdb.truncateLog(1);
+            Assert.assertTrue("Should not get here", false);
+        }
+        catch(IOException e) {
+            Assert.assertTrue("Should have received an IOException", true);
+        }
+        catch(NullPointerException npe) {
+            Assert.fail("This should not throw NPE!");
+        }
+ 
+        ClientBase.recursiveDelete(tmpdir);
+    }
 
     private void append(ZKDatabase zkdb, int i) throws IOException {
         TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData);