james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From btell...@apache.org
Subject [10/15] james-project git commit: JAMES-2202 Solve bug when multiple hosts are passed to ElasticSearch configuration
Date Wed, 01 Nov 2017 11:00:08 GMT
JAMES-2202 Solve bug when multiple hosts are passed to ElasticSearch configuration


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

Branch: refs/heads/master
Commit: 76699b3cb4313ca2f73c4da758f328b68aa19a1f
Parents: 0480d7c
Author: benwa <btellier@linagora.com>
Authored: Thu Oct 26 09:44:48 2017 +0700
Committer: benwa <btellier@linagora.com>
Committed: Wed Nov 1 17:55:12 2017 +0700

----------------------------------------------------------------------
 .../mailbox/ElasticSearchConfiguration.java     | 17 ++++++++----
 .../mailbox/ElasticSearchConfigurationTest.java | 29 ++++++++++++++++----
 .../main/java/org/apache/james/util/Host.java   |  2 +-
 3 files changed, 35 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/76699b3c/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
index 3fc502f..e73ecc7 100644
--- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchConfiguration.java
@@ -19,6 +19,8 @@
 
 package org.apache.james.modules.mailbox;
 
+import java.util.Arrays;
+import java.util.List;
 import java.util.Objects;
 import java.util.Optional;
 
@@ -30,6 +32,7 @@ import org.apache.james.mailbox.elasticsearch.IndexAttachments;
 import org.apache.james.mailbox.elasticsearch.MailboxElasticSearchConstants;
 import org.apache.james.util.Host;
 
+import com.github.steveash.guavate.Guavate;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 
@@ -52,6 +55,7 @@ public class ElasticSearchConfiguration {
     public static final int DEFAULT_NB_SHARDS = 1;
     public static final int DEFAULT_NB_REPLICA = 0;
     public static final int DEFAULT_PORT = 9300;
+    public static final Optional<Integer> DEFAULT_PORT_AS_OPTIONAL = Optional.of(DEFAULT_PORT);
 
     public static ElasticSearchConfiguration fromProperties(PropertiesConfiguration configuration)
throws ConfigurationException {
         int nbShards = configuration.getInt(ELASTICSEARCH_NB_SHARDS, DEFAULT_NB_SHARDS);
@@ -95,8 +99,7 @@ public class ElasticSearchConfiguration {
             propertiesReader.getString(ELASTICSEARCH_MASTER_HOST, null));
         Optional<Integer> masterPort = Optional.ofNullable(
             propertiesReader.getInteger(ELASTICSEARCH_PORT, null));
-        Optional<String> multiHosts = Optional.ofNullable(
-            propertiesReader.getString(ELASTICSEARCH_HOSTS, null));
+        List<String> multiHosts = Arrays.asList(propertiesReader.getStringArray(ELASTICSEARCH_HOSTS));
 
         validateHostsConfigurationOptions(masterHost, masterPort, multiHosts);
 
@@ -105,21 +108,23 @@ public class ElasticSearchConfiguration {
                 Host.from(masterHost.get(),
                 masterPort.get()));
         } else {
-            return Host.parseHosts(multiHosts.get(), DEFAULT_PORT);
+            return multiHosts.stream()
+                .map(ipAndPort -> Host.parse(ipAndPort, DEFAULT_PORT_AS_OPTIONAL))
+                .collect(Guavate.toImmutableList());
         }
     }
 
     @VisibleForTesting
     static void validateHostsConfigurationOptions(Optional<String> masterHost,
                                                   Optional<Integer> masterPort,
-                                                  Optional<String> multiHosts) throws
ConfigurationException {
+                                                  List<String> multiHosts) throws ConfigurationException
{
         if (masterHost.isPresent() != masterPort.isPresent()) {
             throw new ConfigurationException(ELASTICSEARCH_MASTER_HOST + " and " + ELASTICSEARCH_PORT
+ " should be specified together");
         }
-        if (multiHosts.isPresent() && masterHost.isPresent()) {
+        if (!multiHosts.isEmpty() && masterHost.isPresent()) {
             throw new ConfigurationException("You should choose between mono host set up
and " + ELASTICSEARCH_HOSTS);
         }
-        if (!multiHosts.isPresent() && !masterHost.isPresent()) {
+        if (multiHosts.isEmpty() && !masterHost.isPresent()) {
             throw new ConfigurationException("You should specify either (" + ELASTICSEARCH_MASTER_HOST
+ " and " + ELASTICSEARCH_PORT + ") or " + ELASTICSEARCH_HOSTS);
         }
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/76699b3c/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchConfigurationTest.java
----------------------------------------------------------------------
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchConfigurationTest.java
b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchConfigurationTest.java
index 514f967..9c1a3fc 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchConfigurationTest.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchConfigurationTest.java
@@ -33,6 +33,8 @@ import org.apache.james.mailbox.elasticsearch.MailboxElasticSearchConstants;
 import org.apache.james.util.Host;
 import org.junit.Test;
 
+import com.google.common.collect.ImmutableList;
+
 public class ElasticSearchConfigurationTest {
 
     @Test
@@ -253,6 +255,21 @@ public class ElasticSearchConfigurationTest {
     }
 
     @Test
+    public void getHostsShouldReturnConfiguredHostsWhenListIsUsed() throws ConfigurationException
{
+        String hostname = "myHost";
+        String hostname2 = "myOtherHost";
+        int port = 2154;
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("elasticsearch.hosts", hostname + "," + hostname2 + ":"
+ port);
+
+        ElasticSearchConfiguration elasticSearchConfiguration = ElasticSearchConfiguration.fromProperties(configuration);
+
+        assertThat(elasticSearchConfiguration.getHosts())
+            .containsOnly(Host.from(hostname, ElasticSearchConfiguration.DEFAULT_PORT),
+                Host.from(hostname2, port));
+    }
+
+    @Test
     public void getHostsShouldReturnConfiguredHosts() throws ConfigurationException {
         PropertiesConfiguration configuration = new PropertiesConfiguration();
         String hostname = "myHost";
@@ -285,7 +302,7 @@ public class ElasticSearchConfigurationTest {
             ElasticSearchConfiguration.validateHostsConfigurationOptions(
                 Optional.empty(),
                 Optional.empty(),
-                Optional.empty()))
+                ImmutableList.of()))
             .isInstanceOf(ConfigurationException.class)
             .hasMessage("You should specify either (" + ElasticSearchConfiguration.ELASTICSEARCH_MASTER_HOST
+
                 " and " + ElasticSearchConfiguration.ELASTICSEARCH_PORT +
@@ -298,7 +315,7 @@ public class ElasticSearchConfigurationTest {
             ElasticSearchConfiguration.validateHostsConfigurationOptions(
                 Optional.of("localhost"),
                 Optional.of(9200),
-                Optional.of("localhost:9200")))
+                ImmutableList.of("localhost:9200")))
             .isInstanceOf(ConfigurationException.class)
             .hasMessage("You should choose between mono host set up and " + ElasticSearchConfiguration.ELASTICSEARCH_HOSTS);
     }
@@ -309,7 +326,7 @@ public class ElasticSearchConfigurationTest {
             ElasticSearchConfiguration.validateHostsConfigurationOptions(
                 Optional.of("localhost"),
                 Optional.empty(),
-                Optional.empty()))
+                ImmutableList.of()))
             .isInstanceOf(ConfigurationException.class)
             .hasMessage(ElasticSearchConfiguration.ELASTICSEARCH_MASTER_HOST +
                 " and " + ElasticSearchConfiguration.ELASTICSEARCH_PORT + " should be specified
together");
@@ -321,7 +338,7 @@ public class ElasticSearchConfigurationTest {
         ElasticSearchConfiguration.validateHostsConfigurationOptions(
             Optional.empty(),
             Optional.of(9200),
-            Optional.empty()))
+            ImmutableList.of()))
         .isInstanceOf(ConfigurationException.class)
         .hasMessage(ElasticSearchConfiguration.ELASTICSEARCH_MASTER_HOST + " and " +
             ElasticSearchConfiguration.ELASTICSEARCH_PORT + " should be specified together");
@@ -332,7 +349,7 @@ public class ElasticSearchConfigurationTest {
         ElasticSearchConfiguration.validateHostsConfigurationOptions(
             Optional.of("localhost"),
             Optional.of(9200),
-            Optional.empty());
+            ImmutableList.of());
     }
 
     @Test
@@ -340,7 +357,7 @@ public class ElasticSearchConfigurationTest {
         ElasticSearchConfiguration.validateHostsConfigurationOptions(
             Optional.empty(),
             Optional.empty(),
-            Optional.of("localhost:9200"));
+            ImmutableList.of("localhost:9200"));
     }
 
 

http://git-wip-us.apache.org/repos/asf/james-project/blob/76699b3c/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/Host.java b/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
index 4776f06..9adc41c 100644
--- a/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/Host.java
@@ -62,7 +62,7 @@ public class Host {
         return parse(ipAndPort, Optional.empty());
     }
 
-    private static Host parse(String ipAndPort, Optional<Integer> defaultPort) {
+    public static Host parse(String ipAndPort, Optional<Integer> defaultPort) {
         Preconditions.checkNotNull(ipAndPort);
         Preconditions.checkArgument(!ipAndPort.isEmpty());
 


---------------------------------------------------------------------
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