Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 739B911235 for ; Tue, 24 Jun 2014 01:25:59 +0000 (UTC) Received: (qmail 82891 invoked by uid 500); 24 Jun 2014 01:25:59 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 82787 invoked by uid 500); 24 Jun 2014 01:25:59 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 82758 invoked by uid 99); 24 Jun 2014 01:25:59 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Jun 2014 01:25:59 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1946F90F0F1; Tue, 24 Jun 2014 01:25:58 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: elserj@apache.org To: commits@accumulo.apache.org Date: Tue, 24 Jun 2014 01:26:01 -0000 Message-Id: In-Reply-To: <7ce9c3ae68b84b0f8a23ea8131b7c377@git.apache.org> References: <7ce9c3ae68b84b0f8a23ea8131b7c377@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [4/7] git commit: ACCUMULO-2940 Add a Watcher to not use ZooKeeper before we're connected. 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 Authored: Mon Jun 23 19:38:57 2014 -0400 Committer: Josh Elser 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);