Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 50BA0200B89 for ; Wed, 21 Sep 2016 13:53:08 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 4F3A8160ADB; Wed, 21 Sep 2016 11:53:08 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 6C534160ACF for ; Wed, 21 Sep 2016 13:53:07 +0200 (CEST) Received: (qmail 72492 invoked by uid 500); 21 Sep 2016 11:53:06 -0000 Mailing-List: contact commits-help@eagle.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@eagle.incubator.apache.org Delivered-To: mailing list commits@eagle.incubator.apache.org Received: (qmail 72483 invoked by uid 99); 21 Sep 2016 11:53:06 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Sep 2016 11:53:06 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id 3F7C91A09B4 for ; Wed, 21 Sep 2016 11:53:06 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.646 X-Spam-Level: X-Spam-Status: No, score=-4.646 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-1.426] autolearn=disabled Received: from mx1-lw-eu.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id Nb3ue37TvQf2 for ; Wed, 21 Sep 2016 11:53:05 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-eu.apache.org (ASF Mail Server at mx1-lw-eu.apache.org) with SMTP id 219325FE34 for ; Wed, 21 Sep 2016 11:53:03 +0000 (UTC) Received: (qmail 72468 invoked by uid 99); 21 Sep 2016 11:53:03 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Sep 2016 11:53:03 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 2015BDFE60; Wed, 21 Sep 2016 11:53:03 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: hao@apache.org To: commits@eagle.incubator.apache.org Message-Id: <11541b8bc32143ab9c68135dd48e9c85@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: incubator-eagle git commit: [EAGLE-560] Retry embedded zookeeper port by port +1 when conflicts Date: Wed, 21 Sep 2016 11:53:03 +0000 (UTC) archived-at: Wed, 21 Sep 2016 11:53:08 -0000 Repository: incubator-eagle Updated Branches: refs/heads/master 187fbdedd -> 269cdcf87 [EAGLE-560] Retry embedded zookeeper port by port +1 when conflicts https://issues.apache.org/jira/browse/EAGLE-560 Author: Hao Chen Closes #447 from haoch/EAGLE-560. Project: http://git-wip-us.apache.org/repos/asf/incubator-eagle/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-eagle/commit/269cdcf8 Tree: http://git-wip-us.apache.org/repos/asf/incubator-eagle/tree/269cdcf8 Diff: http://git-wip-us.apache.org/repos/asf/incubator-eagle/diff/269cdcf8 Branch: refs/heads/master Commit: 269cdcf87e47dede5c288a2a247c00a6ac62941b Parents: 187fbde Author: Hao Chen Authored: Wed Sep 21 19:50:10 2016 +0800 Committer: Hao Chen Committed: Wed Sep 21 19:50:10 2016 +0800 ---------------------------------------------------------------------- .../eagle/alert/utils/ZookeeperEmbedded.java | 50 ++++++++++++++++++-- .../alert/coordinator/CoordinatorTest.java | 6 +-- 2 files changed, 47 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/269cdcf8/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/utils/ZookeeperEmbedded.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/utils/ZookeeperEmbedded.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/utils/ZookeeperEmbedded.java index da2de0d..ecaa276 100644 --- a/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/utils/ZookeeperEmbedded.java +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-common/src/main/java/org/apache/eagle/alert/utils/ZookeeperEmbedded.java @@ -22,27 +22,67 @@ import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.imps.CuratorFrameworkState; import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.test.TestingServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; import java.io.IOException; +import java.net.BindException; public class ZookeeperEmbedded { + private static final Logger LOG = LoggerFactory.getLogger(ZookeeperEmbedded.class); + private static final int MAX_RETRIES = 3; private TestingServer server; private CuratorFramework zookeeper; private int port; private File logDir; + /** + * Create zookeeper testing server. + * + * @param port initial zookeeper port + */ public ZookeeperEmbedded(int port) { this.port = port; this.logDir = new File(System.getProperty("java.io.tmpdir"), "zk/logs/zookeeper-test-" + port); } - public void start() throws Exception { + /** + * Try to start zookeeper, if failed, retry with port+1. + * + * @return finally bound port + */ + public int start() throws Exception { FileUtils.deleteQuietly(logDir); - server = new TestingServer(this.port, this.logDir); - ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); - zookeeper = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy); - zookeeper.start(); + int i = 0; + boolean success = false; + Exception lastException = null; + while (!success && i < MAX_RETRIES) { + try { + server = new TestingServer(this.port, this.logDir); + ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); + zookeeper = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy); + zookeeper.start(); + success = true; + } catch (BindException exception) { + lastException = exception; + i++; + LOG.warn("Port {} was taken, trying {}", this.port, this.port + i); + this.port = this.port + i; + try { + server.close(); + zookeeper.close(); + } catch (Throwable throwable) { + // ignored + } + } + } + if (!success) { + LOG.error("Failed to start zookeeper after trying {} times", MAX_RETRIES); + throw lastException; + } + return this.port; } public String getConnectionString() { http://git-wip-us.apache.org/repos/asf/incubator-eagle/blob/269cdcf8/eagle-core/eagle-alert-parent/eagle-alert/alert-coordinator/src/test/java/org/apache/alert/coordinator/CoordinatorTest.java ---------------------------------------------------------------------- diff --git a/eagle-core/eagle-alert-parent/eagle-alert/alert-coordinator/src/test/java/org/apache/alert/coordinator/CoordinatorTest.java b/eagle-core/eagle-alert-parent/eagle-alert/alert-coordinator/src/test/java/org/apache/alert/coordinator/CoordinatorTest.java index 79056d4..f55a53e 100644 --- a/eagle-core/eagle-alert-parent/eagle-alert/alert-coordinator/src/test/java/org/apache/alert/coordinator/CoordinatorTest.java +++ b/eagle-core/eagle-alert-parent/eagle-alert/alert-coordinator/src/test/java/org/apache/alert/coordinator/CoordinatorTest.java @@ -51,10 +51,9 @@ public class CoordinatorTest { @BeforeClass public static void setup() throws Exception { - zkEmbed = new ZookeeperEmbedded(2181); - zkEmbed.start(); - + int zkPort = zkEmbed.start(); + System.setProperty("coordinator.zkConfig.zkQuorum","localhost:"+ zkPort); } @AfterClass @@ -127,7 +126,6 @@ public class CoordinatorTest { @Test public void test_main() throws Exception { before(); - Coordinator.main(null); }