You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eagle.apache.org by ha...@apache.org on 2016/09/21 11:53:03 UTC
incubator-eagle git commit: [EAGLE-560] Retry embedded zookeeper port
by port +1 when conflicts
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 <ha...@apache.org>
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 <ha...@apache.org>
Authored: Wed Sep 21 19:50:10 2016 +0800
Committer: Hao Chen <ha...@apache.org>
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 <code>port+1</code>.
+ *
+ * @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);
}