james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [06/10] james-project git commit: JAMES-2334 arbitrary name for each rabbit docker cluster container
Date Wed, 24 Oct 2018 01:51:32 GMT
JAMES-2334 arbitrary name for each rabbit docker cluster container

In cluster environment, there is a relationship between rabbitmq nodename
suffix (domain part) and rabbitmq container hostname. If they are not the same,
rabbitmq docker cannot start.

- arbitrary rabbit docker name by adding an UUID string at the suffix.
- force docker rabbit mq docker's name & hostname & nodename suffix use same value.
Eg `my-rabbit-07f1a987-1704-4a48-b77d-02fef7e6162a` and `rabbit@my-rabbit-07f1a987-1704-4a48-b77d-02fef7e6162a`
- always set rabbitmq node name env. It doesn't hurt at all to set rabbitmq
node name env on both single rabbit and cluster rabbit tests. This make docker rabbit
node name setup simpler and could remove a parameter from constructor


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0c4c3672
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0c4c3672
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0c4c3672

Branch: refs/heads/master
Commit: 0c4c3672488b193d5020f641faeac984e31a0583
Parents: bc6eb25
Author: duc <dtran@linagora.com>
Authored: Fri Oct 19 21:44:00 2018 +0700
Committer: Benoit Tellier <btellier@linagora.com>
Committed: Wed Oct 24 08:48:24 2018 +0700

----------------------------------------------------------------------
 .../DockerClusterRabbitMQExtension.java         | 21 ++++----
 .../james/backend/rabbitmq/DockerRabbitMQ.java  | 50 +++++++++++---------
 .../backend/rabbitmq/RabbitMQClusterTest.java   |  6 +--
 3 files changed, 40 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0c4c3672/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
index 81f62a5..b787239 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerClusterRabbitMQExtension.java
@@ -19,6 +19,7 @@
 package org.apache.james.backend.rabbitmq;
 
 import java.nio.charset.StandardCharsets;
+import java.util.UUID;
 
 import org.apache.james.util.Runnables;
 import org.junit.jupiter.api.extension.AfterAllCallback;
@@ -38,12 +39,9 @@ import com.rabbitmq.client.Address;
 
 public class DockerClusterRabbitMQExtension implements BeforeAllCallback, BeforeEachCallback,
AfterEachCallback, AfterAllCallback, ParameterResolver {
 
-    private static final String RABBIT_1_NODE_NAME = "rabbit@rabbit1";
-    private static final String RABBIT_2_NODE_NAME = "rabbit@rabbit2";
-    private static final String RABBIT_3_NODE_NAME = "rabbit@rabbit3";
-    static final String RABBIT_1 = "rabbit1";
-    static final String RABBIT_2 = "rabbit2";
-    static final String RABBIT_3 = "rabbit3";
+    private static final String RABBIT_1 = "rabbit1";
+    private static final String RABBIT_2 = "rabbit2";
+    private static final String RABBIT_3 = "rabbit3";
     private DockerRabbitMQCluster cluster;
     private Network network;
     private DockerRabbitMQ rabbitMQ1;
@@ -59,9 +57,10 @@ public class DockerClusterRabbitMQExtension implements BeforeAllCallback,
Before
             .createNetworkCmdModifiers(ImmutableList.of())
             .build();
 
-        rabbitMQ1 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_1, cookie, RABBIT_1_NODE_NAME,
network);
-        rabbitMQ2 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_2, cookie, RABBIT_2_NODE_NAME,
network);
-        rabbitMQ3 = DockerRabbitMQ.withCookieAndNodeName(RABBIT_3, cookie, RABBIT_3_NODE_NAME,
network);
+        String clusterIdentity = UUID.randomUUID().toString();
+        rabbitMQ1 = DockerRabbitMQ.withCookieAndHostName(RABBIT_1, clusterIdentity, cookie,
network);
+        rabbitMQ2 = DockerRabbitMQ.withCookieAndHostName(RABBIT_2, clusterIdentity, cookie,
network);
+        rabbitMQ3 = DockerRabbitMQ.withCookieAndHostName(RABBIT_3, clusterIdentity, cookie,
network);
 
         Runnables.runParallel(
             rabbitMQ1::start,
@@ -137,9 +136,9 @@ public class DockerClusterRabbitMQExtension implements BeforeAllCallback,
Before
 
         public void detach() {
             rabbitMQ3.performIfRunning(DockerRabbitMQ::reset);
-            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(RABBIT_3_NODE_NAME));
+            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(rabbitMQ3.getNodeName()));
             rabbitMQ2.performIfRunning(DockerRabbitMQ::reset);
-            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(RABBIT_2_NODE_NAME));
+            rabbitMQ1.performIfRunning(rabbitMQ -> rabbitMQ.forgetNode(rabbitMQ2.getNodeName()));
             rabbitMQ1.performIfRunning(DockerRabbitMQ::reset);
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/0c4c3672/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
index 1b53492..47c99ea 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/DockerRabbitMQ.java
@@ -44,21 +44,23 @@ import com.rabbitmq.client.ConnectionFactory;
 public class DockerRabbitMQ {
     private static final Logger LOGGER = LoggerFactory.getLogger(DockerRabbitMQ.class);
 
-    private static final String DEFAULT_RABBIT_NODE = "my-rabbit";
+    private static final String DEFAULT_RABBIT_HOST_NAME_PREFIX = "my-rabbit";
+    private static final String DEFAULT_RABBIT_NODE_NAME_PREFIX = "rabbit";
     private static final int DEFAULT_RABBITMQ_PORT = 5672;
     private static final int DEFAULT_RABBITMQ_ADMIN_PORT = 15672;
     private static final String DEFAULT_RABBITMQ_USERNAME = "guest";
     private static final String DEFAULT_RABBITMQ_PASSWORD = "guest";
     private static final String RABBITMQ_ERLANG_COOKIE = "RABBITMQ_ERLANG_COOKIE";
     private static final String RABBITMQ_NODENAME = "RABBITMQ_NODENAME";
-    private static final Duration TEN_MINUTE_TIMEOUT = Duration.ofMinutes(10);
+    private static final Duration TEN_MINUTES_TIMEOUT = Duration.ofMinutes(10);
 
     private final GenericContainer<?> container;
-    private final Optional<String> nodeName;
+    private final String nodeName;
+    private final String rabbitHostName;
+    private final String hostNameSuffix;
 
-    public static DockerRabbitMQ withCookieAndNodeName(String hostName, String erlangCookie,
String nodeName, Network network) {
-        return new DockerRabbitMQ(Optional.ofNullable(hostName), Optional.ofNullable(erlangCookie),
Optional.ofNullable(nodeName),
-            Optional.of(network));
+    public static DockerRabbitMQ withCookieAndHostName(String hostNamePrefix, String clusterIdentity,
String erlangCookie, Network network) {
+        return new DockerRabbitMQ(Optional.ofNullable(hostNamePrefix), Optional.ofNullable(clusterIdentity),
Optional.ofNullable(erlangCookie), Optional.of(network));
     }
 
     public static DockerRabbitMQ withoutCookie() {
@@ -66,32 +68,34 @@ public class DockerRabbitMQ {
     }
 
     @SuppressWarnings("resource")
-    private DockerRabbitMQ(Optional<String> hostName, Optional<String> erlangCookie,
Optional<String> nodeName, Optional<Network> net) {
-        container = new GenericContainer<>(Images.RABBITMQ)
-                .withCreateContainerCmdModifier(cmd -> cmd.withName(hostName.orElse(randomName())))
-                .withCreateContainerCmdModifier(cmd -> cmd.withHostName(hostName.orElse(DEFAULT_RABBIT_NODE)))
+    private DockerRabbitMQ(Optional<String> hostNamePrefix, Optional<String>
clusterIdentity, Optional<String> erlangCookie, Optional<Network> net) {
+        this.hostNameSuffix = clusterIdentity.orElse(UUID.randomUUID().toString());
+        this.rabbitHostName = hostName(hostNamePrefix);
+        this.container = new GenericContainer<>(Images.RABBITMQ)
+                .withCreateContainerCmdModifier(cmd -> cmd.withName(this.rabbitHostName))
+                .withCreateContainerCmdModifier(cmd -> cmd.withHostName(this.rabbitHostName))
                 .withExposedPorts(DEFAULT_RABBITMQ_PORT, DEFAULT_RABBITMQ_ADMIN_PORT)
                 .waitingFor(waitStrategy())
                 .withLogConsumer(frame -> LOGGER.debug(frame.getUtf8String()))
                 .withCreateContainerCmdModifier(cmd -> cmd.getHostConfig()
                     .withTmpFs(ImmutableMap.of("/var/lib/rabbitmq/mnesia", "rw,noexec,nosuid,size=100m")));
-        net.ifPresent(container::withNetwork);
-        erlangCookie.ifPresent(cookie -> container.withEnv(RABBITMQ_ERLANG_COOKIE, cookie));
-        nodeName.ifPresent(name -> container.withEnv(RABBITMQ_NODENAME, name));
-        this.nodeName = nodeName;
+        net.ifPresent(this.container::withNetwork);
+        erlangCookie.ifPresent(cookie -> this.container.withEnv(RABBITMQ_ERLANG_COOKIE,
cookie));
+        this.nodeName = DEFAULT_RABBIT_NODE_NAME_PREFIX + "@" + this.rabbitHostName;
+        this.container.withEnv(RABBITMQ_NODENAME, this.nodeName);
     }
 
     private WaitStrategy waitStrategy() {
         return new WaitAllStrategy()
             .withStrategy(Wait.forHttp("").forPort(DEFAULT_RABBITMQ_ADMIN_PORT)
                 .withRateLimiter(RateLimiters.TWENTIES_PER_SECOND)
-                .withStartupTimeout(TEN_MINUTE_TIMEOUT))
-            .withStrategy(new RabbitMQWaitStrategy(this, TEN_MINUTE_TIMEOUT))
-            .withStartupTimeout(TEN_MINUTE_TIMEOUT);
+                .withStartupTimeout(TEN_MINUTES_TIMEOUT))
+            .withStrategy(new RabbitMQWaitStrategy(this, TEN_MINUTES_TIMEOUT))
+            .withStartupTimeout(TEN_MINUTES_TIMEOUT);
     }
 
-    private String randomName() {
-        return UUID.randomUUID().toString();
+    private String hostName(Optional<String> hostNamePrefix) {
+        return hostNamePrefix.orElse(DEFAULT_RABBIT_HOST_NAME_PREFIX) + "-" + hostNameSuffix;
     }
 
     private String getHostIp() {
@@ -140,8 +144,8 @@ public class DockerRabbitMQ {
         return container;
     }
 
-    public String node() {
-        return nodeName.get();
+    public String getNodeName() {
+        return nodeName;
     }
 
     public void join(DockerRabbitMQ rabbitMQ) throws Exception {
@@ -160,7 +164,7 @@ public class DockerRabbitMQ {
 
     private void joinCluster(DockerRabbitMQ rabbitMQ) throws java.io.IOException, InterruptedException
{
         String stdout = container()
-            .execInContainer("rabbitmqctl", "join_cluster", rabbitMQ.node())
+            .execInContainer("rabbitmqctl", "join_cluster", rabbitMQ.getNodeName())
             .getStdout();
         LOGGER.debug("join_cluster: {}", stdout);
     }
@@ -185,7 +189,7 @@ public class DockerRabbitMQ {
 
     public void forgetNode(String removalClusterNodeName) throws Exception {
         String stdout = container()
-            .execInContainer("rabbitmqctl", "-n", this.nodeName.get(), "forget_cluster_node",
removalClusterNodeName)
+            .execInContainer("rabbitmqctl", "-n", this.nodeName, "forget_cluster_node", removalClusterNodeName)
             .getStdout();
         LOGGER.debug("forget_cluster_node: {}", stdout);
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/0c4c3672/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
----------------------------------------------------------------------
diff --git a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
index bf9a875..5e50b4b 100644
--- a/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
+++ b/backends-common/rabbitmq/src/test/java/org/apache/james/backend/rabbitmq/RabbitMQClusterTest.java
@@ -97,9 +97,9 @@ class RabbitMQClusterTest {
 
             assertThat(stdout)
                 .contains(
-                    DockerClusterRabbitMQExtension.RABBIT_1,
-                    DockerClusterRabbitMQExtension.RABBIT_2,
-                    DockerClusterRabbitMQExtension.RABBIT_3);
+                    cluster.getRabbitMQ1().getNodeName(),
+                    cluster.getRabbitMQ2().getNodeName(),
+                    cluster.getRabbitMQ3().getNodeName());
         }
 
         @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message