zookeeper-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From si...@apache.org
Subject svn commit: r1346257 - in /zookeeper/bookkeeper/trunk: CHANGES.txt bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
Date Tue, 05 Jun 2012 06:40:29 GMT
Author: sijie
Date: Tue Jun  5 06:40:28 2012
New Revision: 1346257

URL: http://svn.apache.org/viewvc?rev=1346257&view=rev
Log:
BOOKKEEPER-279: LocalBookKeeper is failing intermittently due to zkclient connection establishment
delay (Rakesh R via sijie)

Modified:
    zookeeper/bookkeeper/trunk/CHANGES.txt
    zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java

Modified: zookeeper/bookkeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/CHANGES.txt?rev=1346257&r1=1346256&r2=1346257&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/CHANGES.txt (original)
+++ zookeeper/bookkeeper/trunk/CHANGES.txt Tue Jun  5 06:40:28 2012
@@ -136,6 +136,8 @@ Release 4.1.0 - 2012-05-31
 
         BOOKKEEPER-281: BKClient is failing when zkclient connection delays (ivank via sijie)
 
+        BOOKKEEPER-279: LocalBookKeeper is failing intermittently due to zkclient connection
establishment delay (Rakesh R via sijie)
+
       hedwig-client/
 
         BOOKKEEPER-217: NPE in hedwig client when enable DEBUG (sijie via ivank)

Modified: zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
URL: http://svn.apache.org/viewvc/zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java?rev=1346257&r1=1346256&r2=1346257&view=diff
==============================================================================
--- zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
(original)
+++ zookeeper/bookkeeper/trunk/bookkeeper-server/src/main/java/org/apache/bookkeeper/util/LocalBookKeeper.java
Tue Jun  5 06:40:28 2012
@@ -26,23 +26,20 @@ import java.io.OutputStream;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
 
 import org.apache.bookkeeper.bookie.BookieException;
 import org.apache.bookkeeper.conf.ServerConfiguration;
 import org.apache.bookkeeper.proto.BookieServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
 import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.apache.zookeeper.ZooKeeper;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
 import org.apache.zookeeper.ZooDefs.Ids;
 import org.apache.zookeeper.server.NIOServerCnxnFactory;
 import org.apache.zookeeper.server.ZooKeeperServer;
@@ -68,6 +65,7 @@ public class LocalBookKeeper {
     ZooKeeperServer zks;
     ZooKeeper zkc;
     int ZooKeeperDefaultPort = 2181;
+    static int zkSessionTimeOut = 5000;
     File ZkTmpDir;
 
     //BookKeeper variables
@@ -104,11 +102,14 @@ public class LocalBookKeeper {
         LOG.debug("ZooKeeper server up: " + b);
     }
 
-    private void initializeZookeper() {
+    private void initializeZookeper() throws IOException {
         LOG.info("Instantiate ZK Client");
         //initialize the zk client with values
         try {
-            zkc = new ZooKeeper("127.0.0.1", ZooKeeperDefaultPort, new emptyWatcher());
+            ZKConnectionWatcher zkConnectionWatcher = new ZKConnectionWatcher();
+            zkc = new ZooKeeper(HOSTPORT, zkSessionTimeOut,
+                    zkConnectionWatcher);
+            zkConnectionWatcher.waitForConnection();
             zkc.create("/ledgers", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
             zkc.create("/ledgers/available", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
             // No need to create an entry for each requested bookie anymore as the
@@ -119,9 +120,6 @@ public class LocalBookKeeper {
         } catch (InterruptedException e) {
             // TODO Auto-generated catch block
             LOG.error("Interrupted while creating znodes", e);
-        } catch (IOException e) {
-            // TODO Auto-generated catch block
-            LOG.error("Exception while creating znodes", e);
         }
     }
     private void runBookies(ServerConfiguration baseConf) 
@@ -184,9 +182,30 @@ public class LocalBookKeeper {
         System.err.println("Usage: LocalBookKeeper number-of-bookies");
     }
 
-    /*	User for testing purposes, void */
-    static class emptyWatcher implements Watcher {
-        public void process(WatchedEvent event) {}
+    /* Watching SyncConnected event from ZooKeeper */
+    static class ZKConnectionWatcher implements Watcher {
+        private CountDownLatch clientConnectLatch = new CountDownLatch(1);
+
+        @Override
+        public void process(WatchedEvent event) {
+            if (event.getState() == KeeperState.SyncConnected) {
+                clientConnectLatch.countDown();
+            }
+        }
+
+        // Waiting for the SyncConnected event from the ZooKeeper server
+        public void waitForConnection() throws IOException {
+            try {
+                if (!clientConnectLatch.await(zkSessionTimeOut,
+                        TimeUnit.MILLISECONDS)) {
+                    throw new IOException(
+                            "Couldn't connect to zookeeper server");
+                }
+            } catch (InterruptedException e) {
+                throw new IOException(
+                        "Interrupted when connecting to zookeeper server", e);
+            }
+        }
     }
 
     public static boolean waitForServerUp(String hp, long timeout) {



Mime
View raw message