You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by rc...@apache.org on 2020/07/15 04:36:17 UTC

[james-project] 09/09: JAMES-3266 Move disable ElasticSearch option into a separate search.properties configuration file

This is an automated email from the ASF dual-hosted git repository.

rcordier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit c4fafaf7633afca1c995012a196a1b4ff3ed2ef0
Author: Raphael Ouazana <ra...@linagora.com>
AuthorDate: Wed Jul 8 17:37:59 2020 +0200

    JAMES-3266 Move disable ElasticSearch option into a separate search.properties configuration file
---
 .../java/org/apache/james/SearchConfiguration.java | 20 ++++---
 .../org/apache/james/SearchConfigurationTest.java  | 67 ++++++++++++++++++++++
 src/site/xdoc/server/config-elasticsearch.xml      |  4 +-
 3 files changed, 82 insertions(+), 9 deletions(-)

diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/SearchConfiguration.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/SearchConfiguration.java
index 9bc4f5c..0840e58 100644
--- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/SearchConfiguration.java
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/SearchConfiguration.java
@@ -19,8 +19,6 @@
 
 package org.apache.james;
 
-import static org.apache.james.modules.mailbox.ElasticSearchMailboxModule.ELASTICSEARCH_CONFIGURATION_NAME;
-
 import java.io.FileNotFoundException;
 
 import org.apache.commons.configuration2.Configuration;
@@ -30,6 +28,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class SearchConfiguration {
+    public static final String SEARCH_CONFIGURATION_NAME = "search";
 
     public enum Implementation {
         Scanning,
@@ -38,19 +37,26 @@ public class SearchConfiguration {
 
     public static SearchConfiguration parse(PropertiesProvider propertiesProvider) throws ConfigurationException {
         try {
-            Configuration configuration = propertiesProvider.getConfiguration(ELASTICSEARCH_CONFIGURATION_NAME);
+            Configuration configuration = propertiesProvider.getConfiguration(SEARCH_CONFIGURATION_NAME);
             return SearchConfiguration.from(configuration);
         } catch (FileNotFoundException e) {
-            LOGGER.warn("Could not find {} configuration file, enabling elasticsearch by default", ELASTICSEARCH_CONFIGURATION_NAME);
+            LOGGER.debug("Could not find {} configuration file, enabling elasticsearch by default", SEARCH_CONFIGURATION_NAME);
             return elasticSearch();
         }
     }
 
-    static SearchConfiguration from(Configuration configuration) {
-        if (configuration.getBoolean("enabled", true)) {
+    static SearchConfiguration from(Configuration configuration) throws ConfigurationException {
+        String searchOption = configuration.getString("implementation", Implementation.ElasticSearch.name());
+        if (searchOption.equalsIgnoreCase(Implementation.ElasticSearch.name())) {
             return elasticSearch();
         }
-        return scanning();
+        if (searchOption.equalsIgnoreCase(Implementation.Scanning.name())) {
+            return scanning();
+        }
+        throw new ConfigurationException(String.format("'implementation' parameter in '%s.properties' should have '%s' or '%s' value",
+                SEARCH_CONFIGURATION_NAME,
+                Implementation.ElasticSearch.name(),
+                Implementation.Scanning.name()));
     }
 
     public static SearchConfiguration scanning() {
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/SearchConfigurationTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/SearchConfigurationTest.java
new file mode 100644
index 0000000..ecb0a62
--- /dev/null
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/SearchConfigurationTest.java
@@ -0,0 +1,67 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you under the Apache License, Version 2.0 (the            *
+ * "License"); you may not use this file except in compliance   *
+ * with the License.  You may obtain a copy of the License at   *
+ *                                                              *
+ *   http://www.apache.org/licenses/LICENSE-2.0                 *
+ *                                                              *
+ * Unless required by applicable law or agreed to in writing,   *
+ * software distributed under the License is distributed on an  *
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
+ * KIND, either express or implied.  See the License for the    *
+ * specific language governing permissions and limitations      *
+ * under the License.                                           *
+ ****************************************************************/
+
+package org.apache.james;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+import org.apache.commons.configuration2.PropertiesConfiguration;
+import org.apache.commons.configuration2.ex.ConfigurationException;
+import org.junit.jupiter.api.Test;
+
+class SearchConfigurationTest {
+    @Test
+    void unknownSearchImplementationShouldThrow() {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("implementation", "unknown");
+        assertThatThrownBy(() -> SearchConfiguration.from(configuration))
+            .isInstanceOf(ConfigurationException.class);
+    }
+
+    @Test
+    void searchImplementationShouldReturnElasticSearchByDefault() throws Exception {
+        assertThat(SearchConfiguration.from(new PropertiesConfiguration()).getImplementation())
+            .isEqualTo(SearchConfiguration.Implementation.ElasticSearch);
+    }
+
+    @Test
+    void searchImplementationShouldReturnElasticSearchWhenSetToElasticSearch() throws Exception {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("implementation", "ElasticSearch");
+        assertThat(SearchConfiguration.from(configuration).getImplementation())
+                .isEqualTo(SearchConfiguration.Implementation.ElasticSearch);
+    }
+
+    @Test
+    void searchImplementationShouldReturnElasticSearchWhenSetToElasticSearchWithAlternativeCase() throws Exception {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("implementation", "elasticsearch");
+        assertThat(SearchConfiguration.from(configuration).getImplementation())
+                .isEqualTo(SearchConfiguration.Implementation.ElasticSearch);
+    }
+
+    @Test
+    void searchImplementationShouldReturnScanningSearchWhenSetToScanningSearch() throws Exception {
+        PropertiesConfiguration configuration = new PropertiesConfiguration();
+        configuration.addProperty("implementation", "scanning");
+        assertThat(SearchConfiguration.from(configuration).getImplementation())
+                .isEqualTo(SearchConfiguration.Implementation.Scanning);
+    }
+}
diff --git a/src/site/xdoc/server/config-elasticsearch.xml b/src/site/xdoc/server/config-elasticsearch.xml
index 866f887..ed085de 100644
--- a/src/site/xdoc/server/config-elasticsearch.xml
+++ b/src/site/xdoc/server/config-elasticsearch.xml
@@ -117,8 +117,8 @@
 
       <p>
           ElasticSearch component can be disabled but consider it would make search feature to not work. In particular it will break JMAP protocol and SEARCH IMAP comment in an nondeterministic way.
-          This is controlled via the <code>enabled</code> property (boolean, defaults
-          to true). Setting this configuration parameter to <code>false</code> will effectively disable ElasticSearch, no
+          This is controlled in the <code>search.properties</code> file via the <code>implementation</code> property (defaults
+          to <code>ElasticSearch</code>). Setting this configuration parameter to <code>scanning</code> will effectively disable ElasticSearch, no
           further indexation will be done however searches will rely on the scrolling search, leading to expensive and longer
           searches. Disabling ElasticSearch requires no extra action, however
           <a href="https://github.com/apache/james-project/blob/master/src/site/markdown/server/manage-webadmin.md#reindexing-all-mails">


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