You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by da...@apache.org on 2017/06/15 14:00:19 UTC
aries-containers git commit: More health check configuration.
Repository: aries-containers
Updated Branches:
refs/heads/master 6289b71d1 -> 182343a7f
More health check configuration.
Project: http://git-wip-us.apache.org/repos/asf/aries-containers/repo
Commit: http://git-wip-us.apache.org/repos/asf/aries-containers/commit/182343a7
Tree: http://git-wip-us.apache.org/repos/asf/aries-containers/tree/182343a7
Diff: http://git-wip-us.apache.org/repos/asf/aries-containers/diff/182343a7
Branch: refs/heads/master
Commit: 182343a7fb5def1bf0f05c0a9813f466c79b7113
Parents: 6289b71
Author: David Bosschaert <da...@apache.org>
Authored: Thu Jun 15 15:00:03 2017 +0100
Committer: David Bosschaert <da...@apache.org>
Committed: Thu Jun 15 15:00:03 2017 +0100
----------------------------------------------------------------------
.../apache/aries/containers/HealthCheck.java | 87 ++++++++++++++++----
.../aries/containers/api/ServiceConfigTest.java | 21 ++++-
2 files changed, 90 insertions(+), 18 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/aries-containers/blob/182343a7/containers-api/src/main/java/org/apache/aries/containers/HealthCheck.java
----------------------------------------------------------------------
diff --git a/containers-api/src/main/java/org/apache/aries/containers/HealthCheck.java b/containers-api/src/main/java/org/apache/aries/containers/HealthCheck.java
index 5d492f8..b8b2129 100644
--- a/containers-api/src/main/java/org/apache/aries/containers/HealthCheck.java
+++ b/containers-api/src/main/java/org/apache/aries/containers/HealthCheck.java
@@ -48,11 +48,8 @@ public class HealthCheck {
HTTPS,
/**
- * Health check defined as a TCP port connection. If opening a TCP port to
- * succeeds the health check passes. The port to connect to is specified
- * via {@link HealthCheck#getParameters()}. An port index is specified via
- * {@code $PORT0}, {@code $PORT1} etc. An actual port is specified as a number,
- * e.g. {@code 8080}.
+ * Health check defined as a TCP port connection. If opening a TCP port
+ * succeeds the health check passes.
*/
TCP,
@@ -76,15 +73,19 @@ public class HealthCheck {
private final int interval;
private final int timeout;
private final int maxFailures;
+ private final Integer port;
+ private final Integer portIndex;
private HealthCheck(String parameters, Type type, int gracePeriod, int interval,
- int timeout, int maxFailures) {
+ int timeout, int maxFailures, Integer port, Integer portIndex) {
this.parameters = parameters;
this.type = type;
this.gracePeriod = gracePeriod;
this.interval = interval;
this.timeout = timeout;
this.maxFailures = maxFailures;
+ this.port = port;
+ this.portIndex = portIndex;
}
/**
@@ -124,6 +125,26 @@ public class HealthCheck {
}
/**
+ * The actual port number to use for the health check. This is the external port of the container
+ * and should only be used if the external port of the container is used and does not change.
+ * If the external port is not predefined, use {@link #getPortIndex()} instead.
+ * @return The port or {@code null} if the port is not specified.
+ */
+ public Integer getPort() {
+ return port;
+ }
+
+ /**
+ * The zero-based port index to use for health checks. This is the external port of the container,
+ * but the actual port number is assigned dynamically when the container is started.
+ * As the index is zero-based, the first port has index {@code 0}.
+ * @return The port index or {@code null} if no port index was specified.
+ */
+ public Integer getPortIndex() {
+ return portIndex;
+ }
+
+ /**
* @return The number of seconds after which a health check is considered a failure, regardless
* of the obtained result.
*/
@@ -135,10 +156,12 @@ public class HealthCheck {
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
result = prime * result + gracePeriod;
result = prime * result + interval;
result = prime * result + maxFailures;
+ result = prime * result + ((parameters == null) ? 0 : parameters.hashCode());
+ result = prime * result + ((port == null) ? 0 : port.hashCode());
+ result = prime * result + ((portIndex == null) ? 0 : portIndex.hashCode());
result = prime * result + timeout;
result = prime * result + ((type == null) ? 0 : type.hashCode());
return result;
@@ -153,23 +176,30 @@ public class HealthCheck {
if (getClass() != obj.getClass())
return false;
HealthCheck other = (HealthCheck) obj;
+ if (gracePeriod != other.gracePeriod)
+ return false;
+ if (interval != other.interval)
+ return false;
+ if (maxFailures != other.maxFailures)
+ return false;
if (parameters == null) {
if (other.parameters != null)
return false;
} else if (!parameters.equals(other.parameters))
return false;
- if (gracePeriod != other.gracePeriod)
- return false;
- if (interval != other.interval)
+ if (port == null) {
+ if (other.port != null)
+ return false;
+ } else if (!port.equals(other.port))
return false;
- if (maxFailures != other.maxFailures)
+ if (portIndex == null) {
+ if (other.portIndex != null)
+ return false;
+ } else if (!portIndex.equals(other.portIndex))
return false;
if (timeout != other.timeout)
return false;
- if (type == null) {
- if (other.type != null)
- return false;
- } else if (!type.equals(other.type))
+ if (type != other.type)
return false;
return true;
}
@@ -191,8 +221,10 @@ public class HealthCheck {
private final Type type;
private int gracePeriod = 300;
private int interval = 60;
- private int timeout = 20;
private int maxFailures = 3;
+ private Integer port;
+ private Integer portIndex;
+ private int timeout = 20;
Builder(Type type) {
this.type = type;
@@ -239,6 +271,26 @@ public class HealthCheck {
}
/**
+ * Specify the port for health checks.
+ * @param port The actual port number to use for the health check.
+ * @return the current builder for further building.
+ */
+ public Builder port(int port) {
+ this.port = port;
+ return this;
+ }
+
+ /**
+ * Specify the port index for health checks.
+ * @param idx The port index to use for the health check.
+ * @return the current builder for further building.
+ */
+ public Builder portIndex(int idx) {
+ this.portIndex = idx;
+ return this;
+ }
+
+ /**
* Specify the timeout.
* @param t The timout in seconds.
* @return the current builder for further building.
@@ -249,7 +301,8 @@ public class HealthCheck {
}
public HealthCheck build() {
- return new HealthCheck(parameters, type, gracePeriod, interval, timeout, maxFailures);
+ return new HealthCheck(parameters, type, gracePeriod, interval, timeout, maxFailures,
+ port, portIndex);
}
}
}
http://git-wip-us.apache.org/repos/asf/aries-containers/blob/182343a7/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java
----------------------------------------------------------------------
diff --git a/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java b/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java
index 934ec4c..9e5ff67 100644
--- a/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java
+++ b/containers-api/src/test/java/org/apache/aries/containers/api/ServiceConfigTest.java
@@ -28,6 +28,7 @@ import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
public class ServiceConfigTest {
@Test
@@ -78,11 +79,29 @@ public class ServiceConfigTest {
@Test
public void testHealthCheck() {
HealthCheck hc = HealthCheck.builder(HealthCheck.Type.HTTP).
- parameters("/index.html").build();
+ parameters("/index.html").portIndex(0).build();
ServiceConfig sc = ServiceConfig.builder("mysvc", "animg").
healthCheck(hc).build();
assertEquals(1, sc.getHealthChecks().size());
assertEquals(hc, sc.getHealthChecks().get(0));
}
+
+ @Test
+ public void testHealthCheck2() {
+ HealthCheck hc = HealthCheck.builder(HealthCheck.Type.COMMAND).build();
+ assertEquals(HealthCheck.Type.COMMAND, hc.getType());
+ assertNull(hc.getPort());
+ assertNull(hc.getPortIndex());
+
+ HealthCheck hc2 = HealthCheck.builder(HealthCheck.Type.TCP).port(1234).build();
+ assertEquals(HealthCheck.Type.TCP, hc2.getType());
+ assertEquals(1234, (int) hc2.getPort());
+ assertNull(hc2.getPortIndex());
+
+ HealthCheck hc3 = HealthCheck.builder(HealthCheck.Type.HTTP).portIndex(0).build();
+ assertEquals(HealthCheck.Type.HTTP, hc3.getType());
+ assertNull(hc3.getPort());
+ assertEquals(new Integer(0), hc3.getPortIndex());
+ }
}