Procházet zdrojové kódy

ZOOKEEPER-882. Startup loads last transaction from snapshot (jared via fpj)


git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1052244 13f79535-47bb-0310-9956-ffa450edef68
Flavio Paiva Junqueira před 14 roky
rodič
revize
2a6f177a50

+ 2 - 0
CHANGES.txt

@@ -166,6 +166,8 @@ BUGFIXES:
   ZOOKEEPER-958. Flag to turn off autoconsume in hedwig c++ client (Ivan Kelly
   via mahadev)
 
+  ZOOKEEPER-882. Startup loads last transaction from snapshot (j:ared via fpj)
+
 IMPROVEMENTS:
   ZOOKEEPER-724. Improve junit test integration - log harness information 
   (phunt via mahadev)

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

@@ -570,11 +570,14 @@ public class FileTxnLog implements TxnLog {
                 inputStream.close();
                 inputStream = null;
                 ia = null;
-                // thsi means that the file has ended
+                hdr = null;
+                // this means that the file has ended
                 // we shoud go to the next file
                 if (!goToNextLog()) {
                     return false;
                 }
+                // if we went to the next log file, we should call next() again
+                return next();
             }
             return true;
         }

+ 1 - 1
src/java/main/org/apache/zookeeper/server/persistence/FileTxnSnapLog.java

@@ -123,7 +123,7 @@ public class FileTxnSnapLog {
             PlayBackListener listener) throws IOException {
         snapLog.deserialize(dt, sessions);
         FileTxnLog txnLog = new FileTxnLog(dataDir);
-        TxnIterator itr = txnLog.read(dt.lastProcessedZxid);
+        TxnIterator itr = txnLog.read(dt.lastProcessedZxid+1);
         long highestZxid = dt.lastProcessedZxid;
         TxnHeader hdr;
         while (true) {

+ 107 - 0
src/java/test/org/apache/zookeeper/test/LoadFromLogTest.java

@@ -0,0 +1,107 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.zookeeper.test;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.PortAssignment;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.ZKTestCase;
+import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.ZooDefs.Ids;
+import org.apache.zookeeper.server.ServerCnxnFactory;
+import org.apache.zookeeper.server.SyncRequestProcessor;
+import org.apache.zookeeper.server.ZooKeeperServer;
+import org.apache.zookeeper.server.persistence.FileTxnSnapLog;
+import org.apache.zookeeper.server.persistence.FileTxnLog;
+import org.apache.zookeeper.server.persistence.TxnLog.TxnIterator;
+import org.apache.zookeeper.txn.TxnHeader;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class LoadFromLogTest extends ZKTestCase implements  Watcher {
+    private static String HOSTPORT = "127.0.0.1:" + PortAssignment.unique();
+    private static final int CONNECTION_TIMEOUT = 3000;
+    private static final int NUM_MESSAGES = 300;
+
+    // setting up the quorum has a transaction overhead for creating and closing the session
+    private static final int TRANSACTION_OVERHEAD = 2;	
+    private static final int TOTAL_TRANSACTIONS = NUM_MESSAGES + TRANSACTION_OVERHEAD;
+
+    /**
+     * test that all transactions from the Log are loaded, and only once
+     * @throws Exception an exception might be thrown here
+     */
+    @Test
+    public void testLoad() throws Exception {
+        // setup a single server cluster
+        File tmpDir = ClientBase.createTmpDir();
+        ClientBase.setupTestEnv();
+        ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
+        SyncRequestProcessor.setSnapCount(100);
+        final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
+        ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
+        f.startup(zks);
+        Assert.assertTrue("waiting for server being up ",
+                ClientBase.waitForServerUp(HOSTPORT,CONNECTION_TIMEOUT));
+        ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, this);
+
+        // generate some transactions that will get logged
+        try {
+            for (int i = 0; i< NUM_MESSAGES; i++) {
+                zk.create("/invalidsnap-" + i, new byte[0], Ids.OPEN_ACL_UNSAFE,
+                        CreateMode.PERSISTENT);
+            }
+        } finally {
+            zk.close();
+        }
+        f.shutdown();
+        Assert.assertTrue("waiting for server to shutdown",
+                ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
+
+        // now verify that the FileTxnLog reads every transaction only once
+	File logDir = new File(tmpDir, FileTxnSnapLog.version + FileTxnSnapLog.VERSION);
+	FileTxnLog txnLog = new FileTxnLog(logDir);
+
+        TxnIterator itr = txnLog.read(0);
+        long expectedZxid = 0;
+        long lastZxid = 0;
+        TxnHeader hdr;
+        do {
+            hdr = itr.getHeader();
+            expectedZxid++;
+            Assert.assertTrue("not the same transaction. lastZxid=" + lastZxid + ", zxid=" + hdr.getZxid(), lastZxid != hdr.getZxid());
+            Assert.assertTrue("excepting next transaction. expected=" + expectedZxid + ", retreived=" + hdr.getZxid(), (hdr.getZxid() == expectedZxid));
+            lastZxid = hdr.getZxid();
+        }while(itr.next());
+	
+        Assert.assertTrue("processed all transactions. " + expectedZxid + " == " + TOTAL_TRANSACTIONS, (expectedZxid == TOTAL_TRANSACTIONS));
+    }
+
+
+
+
+    public void process(WatchedEvent event) {
+        // do nothing
+    }
+
+}