accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From els...@apache.org
Subject [4/7] git commit: ACCUMULO-2940 Add a Watcher to not use ZooKeeper before we're connected.
Date Tue, 24 Jun 2014 01:26:01 GMT
ACCUMULO-2940 Add a Watcher to not use ZooKeeper before we're connected.

ZooKeeper connection is asynchronous, so we need to set a Watcher to know
when the client is actually connected. We should really be checking for connections
after any exception, but we hopefully won't get disconnected mid-test.


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/65782b50
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/65782b50
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/65782b50

Branch: refs/heads/1.6.1-SNAPSHOT
Commit: 65782b505478a37d01468064c6bcd5eb5cd75f88
Parents: d81b8db
Author: Josh Elser <elserj@apache.org>
Authored: Mon Jun 23 19:38:57 2014 -0400
Committer: Josh Elser <elserj@apache.org>
Committed: Mon Jun 23 19:38:57 2014 -0400

----------------------------------------------------------------------
 .../accumulo/fate/zookeeper/ZooLockTest.java    | 56 ++++++++++++++------
 1 file changed, 41 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/65782b50/test/src/test/java/org/apache/accumulo/fate/zookeeper/ZooLockTest.java
----------------------------------------------------------------------
diff --git a/test/src/test/java/org/apache/accumulo/fate/zookeeper/ZooLockTest.java b/test/src/test/java/org/apache/accumulo/fate/zookeeper/ZooLockTest.java
index 0c57250..717bf0a 100644
--- a/test/src/test/java/org/apache/accumulo/fate/zookeeper/ZooLockTest.java
+++ b/test/src/test/java/org/apache/accumulo/fate/zookeeper/ZooLockTest.java
@@ -26,6 +26,9 @@ import org.apache.accumulo.minicluster.MiniAccumuloCluster;
 import org.apache.log4j.Level;
 import org.apache.log4j.Logger;
 import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.WatchedEvent;
+import org.apache.zookeeper.Watcher;
+import org.apache.zookeeper.Watcher.Event.KeeperState;
 import org.apache.zookeeper.ZooDefs;
 import org.apache.zookeeper.ZooKeeper;
 import org.junit.AfterClass;
@@ -42,7 +45,24 @@ public class ZooLockTest {
   public static TemporaryFolder folder = new TemporaryFolder();
   
   private static MiniAccumuloCluster accumulo;
-  
+
+  static class ConnectedWatcher implements Watcher {
+    volatile boolean connected = false;
+
+    @Override
+    public synchronized void process(WatchedEvent event) {
+      if (event.getState() == KeeperState.SyncConnected) { // For ZK >3.4.... || event.getState()
== KeeperState.ConnectedReadOnly) {
+        connected = true;
+      } else {
+        connected = false;
+      }
+    }
+
+    public synchronized boolean isConnected() {
+      return connected;
+    }
+  }
+
   static class TestALW implements AsyncLockWatcher {
     
     LockLossReason reason = null;
@@ -101,8 +121,6 @@ public class ZooLockTest {
   
   @Test(timeout = 10000)
   public void testDeleteParent() throws Exception {
-    accumulo.getZooKeepers();
-    
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
     
     ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(),
parent);
@@ -134,8 +152,6 @@ public class ZooLockTest {
   
   @Test(timeout = 10000)
   public void testNoParent() throws Exception {
-    accumulo.getZooKeepers();
-    
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
     
     ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 30000, "digest", "secret".getBytes(),
parent);
@@ -156,8 +172,6 @@ public class ZooLockTest {
   
   @Test(timeout = 10000)
   public void testDeleteLock() throws Exception {
-    accumulo.getZooKeepers();
-    
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
     
     ZooReaderWriter zk = ZooReaderWriter.getInstance(accumulo.getZooKeepers(), 30000, "digest",
"secret".getBytes());
@@ -189,8 +203,6 @@ public class ZooLockTest {
   
   @Test(timeout = 10000)
   public void testDeleteWaiting() throws Exception {
-    accumulo.getZooKeepers();
-    
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
     
     ZooReaderWriter zk = ZooReaderWriter.getInstance(accumulo.getZooKeepers(), 30000, "digest",
"secret".getBytes());
@@ -257,12 +269,15 @@ public class ZooLockTest {
   
   @Test(timeout = 10000)
   public void testUnexpectedEvent() throws Exception {
-    accumulo.getZooKeepers();
-    
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
-    
-    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 30000, null);
+
+    ConnectedWatcher watcher = new ConnectedWatcher();
+    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 30000, watcher);
     zk.addAuthInfo("digest", "secret".getBytes());
+
+    while (!watcher.isConnected()) {
+      Thread.sleep(200);
+    }
     
     zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
     
@@ -302,8 +317,13 @@ public class ZooLockTest {
     
     ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 1000, "digest", "secret".getBytes(),
parent);
     
-    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, null);
+    ConnectedWatcher watcher = new ConnectedWatcher();
+    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, watcher);
     zk.addAuthInfo("digest", "secret".getBytes());
+
+    while (!watcher.isConnected()) {
+      Thread.sleep(200);
+    }
     
     for (int i = 0; i < 10; i++) {
       zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
@@ -331,8 +351,14 @@ public class ZooLockTest {
   @Test(timeout = 10000)
   public void testChangeData() throws Exception {
     String parent = "/zltest-" + this.hashCode() + "-l" + pdCount++;
-    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, null);
+    ConnectedWatcher watcher = new ConnectedWatcher();
+    ZooKeeper zk = new ZooKeeper(accumulo.getZooKeepers(), 1000, watcher);
     zk.addAuthInfo("digest", "secret".getBytes());
+
+    while (!watcher.isConnected()) {
+      Thread.sleep(200);
+    }
+    
     zk.create(parent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
     
     ZooLock zl = new ZooLock(accumulo.getZooKeepers(), 1000, "digest", "secret".getBytes(),
parent);


Mime
View raw message