Repository: zookeeper Updated Branches: refs/heads/branch-3.5 acd116af6 -> 1b855d201
ZOOKEEPER-2697: Handle graceful stop of ZookKeeper client Add a ZooKeeper.close(int timeout) method which waits for internal resources to be released Author: eolivelli <[email protected]> Reviewers: Michael Han <[email protected]> Closes #222 from eolivelli/ZOOKEEPER-2697-close-with-timeout (cherry picked from commit acfc471eca0aab57707b59ed8d19221360d19b85) Signed-off-by: Michael Han <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/zookeeper/repo Commit: http://git-wip-us.apache.org/repos/asf/zookeeper/commit/1b855d20 Tree: http://git-wip-us.apache.org/repos/asf/zookeeper/tree/1b855d20 Diff: http://git-wip-us.apache.org/repos/asf/zookeeper/diff/1b855d20 Branch: refs/heads/branch-3.5 Commit: 1b855d20129b9d308b90387ff4b088128eed89d9 Parents: acd116a Author: eolivelli <[email protected]> Authored: Mon May 1 08:19:15 2017 -0700 Committer: Michael Han <[email protected]> Committed: Mon May 1 08:19:28 2017 -0700 ---------------------------------------------------------------------- .../main/org/apache/zookeeper/ZooKeeper.java | 21 ++++++++++++++++++++ .../apache/zookeeper/test/AsyncHammerTest.java | 3 +-- .../org/apache/zookeeper/test/ClientTest.java | 7 +++---- 3 files changed, 25 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/zookeeper/blob/1b855d20/src/java/main/org/apache/zookeeper/ZooKeeper.java ---------------------------------------------------------------------- diff --git a/src/java/main/org/apache/zookeeper/ZooKeeper.java b/src/java/main/org/apache/zookeeper/ZooKeeper.java index a2f6617..9b38c19 100644 --- a/src/java/main/org/apache/zookeeper/ZooKeeper.java +++ b/src/java/main/org/apache/zookeeper/ZooKeeper.java @@ -1300,6 +1300,11 @@ public class ZooKeeper implements AutoCloseable { * <p> * Added in 3.5.3: <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">try-with-resources</a> * may be used instead of calling close directly. + * </p> + * <p> + * This method does not wait for all internal threads to exit. + * Use the {@link #close(int) } method to wait for all resources to be released + * </p> * * @throws InterruptedException */ @@ -1327,6 +1332,22 @@ public class ZooKeeper implements AutoCloseable { } /** + * Close this client object as the {@link #close() } method. + * This method will wait for internal resources to be released. + * + * @param waitForShutdownTimeoutMs timeout (in milliseconds) to wait for resources to be released. + * Use zero or a negative value to skip the wait + * @throws InterruptedException + * @return true if waitForShutdownTimeout is greater than zero and all of the resources have been released + * + * @since 3.5.4 + */ + public boolean close(int waitForShutdownTimeoutMs) throws InterruptedException { + close(); + return testableWaitForShutdown(waitForShutdownTimeoutMs); + } + + /** * Prepend the chroot to the client path (if present). The expectation of * this function is that the client path has been validated before this * function is called http://git-wip-us.apache.org/repos/asf/zookeeper/blob/1b855d20/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java ---------------------------------------------------------------------- diff --git a/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java b/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java index ebfc963..1ccdc0b 100644 --- a/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java +++ b/src/java/test/org/apache/zookeeper/test/AsyncHammerTest.java @@ -103,8 +103,7 @@ public class AsyncHammerTest extends ZKTestCase } finally { if (zk != null) { try { - zk.close(); - if (!zk.testableWaitForShutdown(CONNECTION_TIMEOUT)) { + if (!zk.close(CONNECTION_TIMEOUT)) { failed = true; LOG.error("Client did not shutdown"); } http://git-wip-us.apache.org/repos/asf/zookeeper/blob/1b855d20/src/java/test/org/apache/zookeeper/test/ClientTest.java ---------------------------------------------------------------------- diff --git a/src/java/test/org/apache/zookeeper/test/ClientTest.java b/src/java/test/org/apache/zookeeper/test/ClientTest.java index d33223d..1aaef75 100644 --- a/src/java/test/org/apache/zookeeper/test/ClientTest.java +++ b/src/java/test/org/apache/zookeeper/test/ClientTest.java @@ -47,6 +47,7 @@ import org.apache.zookeeper.proto.ReplyHeader; import org.apache.zookeeper.proto.RequestHeader; import org.apache.zookeeper.server.PrepRequestProcessor; import org.apache.zookeeper.server.util.OSMXBean; +import static org.apache.zookeeper.test.ClientBase.CONNECTION_TIMEOUT; import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -113,8 +114,7 @@ public class ClientTest extends ClientBase { LOG.info("{}",zk.testableRemoteSocketAddress()); LOG.info("{}",zk.toString()); } finally { - zk.close(); - zk.testableWaitForShutdown(CONNECTION_TIMEOUT); + zk.close(CONNECTION_TIMEOUT); LOG.info("{}",zk.testableLocalSocketAddress()); LOG.info("{}",zk.testableRemoteSocketAddress()); LOG.info("{}",zk.toString()); @@ -759,11 +759,10 @@ public class ClientTest extends ClientBase { try { for (; current < count; current++) { TestableZooKeeper zk = createClient(); - zk.close(); // we've asked to close, wait for it to finish closing // all the sub-threads otw the selector may not be // closed when we check (false positive on test Assert.failure - zk.testableWaitForShutdown(CONNECTION_TIMEOUT); + zk.close(CONNECTION_TIMEOUT); } } catch (Throwable t) { LOG.error("test Assert.failed", t);
