Browse Source

ZOOKEEPER-1642. Leader loading database twice (fpj via camille)

git-svn-id: https://svn.apache.org/repos/asf/zookeeper/trunk@1483440 13f79535-47bb-0310-9956-ffa450edef68
Camille Fournier 12 years ago
parent
commit
d9ad361538
2 changed files with 27 additions and 1 deletions
  1. 2 0
      CHANGES.txt
  2. 25 1
      src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java

+ 2 - 0
CHANGES.txt

@@ -352,6 +352,8 @@ BUGFIXES:
 
   ZOOKEEPER-1324. Remove Duplicate NEWLEADER packets 
   from the Leader to the Follower. (Thawan, fpj via fpj)
+  
+  ZOOKEEPER-1642. Leader Loading Database Twice (fpj via camille)
 
 IMPROVEMENTS:
 

+ 25 - 1
src/java/main/org/apache/zookeeper/server/ZooKeeperServer.java

@@ -230,7 +230,31 @@ public class ZooKeeperServer implements SessionExpirer, ServerStats.Provider {
      *  Restore sessions and data
      */
     public void loadData() throws IOException, InterruptedException {
-        setZxid(zkDb.loadDataBase());
+        /*
+         * When a new leader starts executing Leader#lead, it 
+         * invokes this method. The database, however, has been
+         * initialized before running leader election so that
+         * the server could pick its zxid for its initial vote.
+         * It does it by invoking QuorumPeer#getLastLoggedZxid.
+         * Consequently, we don't need to initialize it once more
+         * and avoid the penalty of loading it a second time. Not 
+         * reloading it is particularly important for applications
+         * that host a large database.
+         * 
+         * The following if block checks whether the database has
+         * been initialized or not. Note that this method is
+         * invoked by at least one other method: 
+         * ZooKeeperServer#startdata.
+         *  
+         * See ZOOKEEPER-1642 for more detail.
+         */
+        if(zkDb.isInitialized()){
+            setZxid(zkDb.getDataTreeLastProcessedZxid());
+        }
+        else {
+            setZxid(zkDb.loadDataBase());
+        }
+        
         // Clean up dead sessions
         LinkedList<Long> deadSessions = new LinkedList<Long>();
         for (Long session : zkDb.getSessions()) {