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 bt...@apache.org on 2019/05/30 07:22:28 UTC

[james-project] 17/19: JAMES-2763 ElasticSearchStartUpCheck impl

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

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

commit 8c6f24c90cacdb9afddc8f93ac7c90551344ad93
Author: Tran Tien Duc <dt...@linagora.com>
AuthorDate: Mon May 27 18:00:28 2019 +0700

    JAMES-2763 ElasticSearchStartUpCheck impl
---
 .../mailbox/ElasticSearchMailboxModule.java        |  5 ++
 .../modules/mailbox/ElasticSearchStartUpCheck.java | 86 ++++++++++++++++++++++
 ...esWithNonCompatibleElasticSearchServerTest.java | 13 +++-
 server/container/lifecycle-api/pom.xml             | 10 +++
 .../apache/james/lifecycle/api/StartUpCheck.java   | 19 +++++
 .../james/lifecycle/api/StartUpCheckTest.java      | 37 ++++++++++
 6 files changed, 168 insertions(+), 2 deletions(-)

diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
index 9c43034..46e1803 100644
--- a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java
@@ -37,6 +37,7 @@ import org.apache.commons.lang3.time.DurationFormatUtils;
 import org.apache.james.backends.es.ClientProviderImpl;
 import org.apache.james.backends.es.ElasticSearchConfiguration;
 import org.apache.james.backends.es.ElasticSearchIndexer;
+import org.apache.james.lifecycle.api.StartUpCheck;
 import org.apache.james.lifecycle.api.Startable;
 import org.apache.james.mailbox.elasticsearch.ElasticSearchMailboxConfiguration;
 import org.apache.james.mailbox.elasticsearch.IndexAttachments;
@@ -134,6 +135,10 @@ public class ElasticSearchMailboxModule extends AbstractModule {
         Multibinder.newSetBinder(binder(), ConfigurationPerformer.class)
             .addBinding()
             .to(ElasticSearchMailboxIndexCreationPerformer.class);
+
+        Multibinder.newSetBinder(binder(), StartUpCheck.class)
+            .addBinding()
+            .to(ElasticSearchStartUpCheck.class);
     }
 
     @Provides
diff --git a/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchStartUpCheck.java b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchStartUpCheck.java
new file mode 100644
index 0000000..3af12d9
--- /dev/null
+++ b/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchStartUpCheck.java
@@ -0,0 +1,86 @@
+/****************************************************************
+ * 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.modules.mailbox;
+
+import java.io.IOException;
+
+import javax.inject.Inject;
+
+import org.apache.james.backends.es.ElasticSearchConfiguration;
+import org.apache.james.lifecycle.api.StartUpCheck;
+import org.elasticsearch.Version;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ElasticSearchStartUpCheck implements StartUpCheck {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchConfiguration.class);
+
+    private static final Version RECOMMENDED_ES_VERSION = Version.fromString("6.7.2");
+    private static final String VERSION_CHECKING_ERROR_MESSAGE = "Error when checking ES version";
+
+    public static final String CHECK_NAME = "ElasticSearchStartUpCheck";
+
+    private final RestHighLevelClient client;
+
+    @Inject
+    private ElasticSearchStartUpCheck(RestHighLevelClient client) {
+        this.client = client;
+    }
+
+    @Override
+    public CheckResult check() {
+        try {
+            Version esVersion = client.info(RequestOptions.DEFAULT)
+                .getVersion();
+            if (esVersion.isCompatible(RECOMMENDED_ES_VERSION)) {
+                return CheckResult.builder()
+                    .checkName(checkName())
+                    .resultType(ResultType.GOOD)
+                    .build();
+            }
+            String esVersionCompatibilityWarn = String.format(
+                "ES version(%s) is not compatible with the recommendation(%s)",
+                esVersion.toString(),
+                RECOMMENDED_ES_VERSION.toString());
+            LOGGER.warn(esVersionCompatibilityWarn);
+
+            return CheckResult.builder()
+                .checkName(checkName())
+                .resultType(ResultType.BAD)
+                .description(esVersionCompatibilityWarn)
+                .build();
+        } catch (IOException e) {
+            LOGGER.error(VERSION_CHECKING_ERROR_MESSAGE, e);
+            return CheckResult.builder()
+                .checkName(checkName())
+                .resultType(ResultType.BAD)
+                .description(VERSION_CHECKING_ERROR_MESSAGE + ": " + e.getMessage())
+                .build();
+        }
+    }
+
+    @Override
+    public String checkName() {
+        return CHECK_NAME;
+    }
+}
diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesWithNonCompatibleElasticSearchServerTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesWithNonCompatibleElasticSearchServerTest.java
index 3a4c0b5..22830c3 100644
--- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesWithNonCompatibleElasticSearchServerTest.java
+++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/JamesWithNonCompatibleElasticSearchServerTest.java
@@ -24,11 +24,13 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatThrownBy;
 
 import org.apache.james.backends.es.DockerElasticSearch;
+import org.apache.james.lifecycle.api.StartUpCheck;
+import org.apache.james.lifecycle.api.StartUpCheck.CheckResult;
 import org.apache.james.mailbox.extractor.TextExtractor;
 import org.apache.james.mailbox.store.search.PDFTextExtractor;
 import org.apache.james.modules.TestJMAPServerModule;
+import org.apache.james.modules.mailbox.ElasticSearchStartUpCheck;
 import org.apache.james.util.docker.Images;
-import org.elasticsearch.ElasticsearchStatusException;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.RegisterExtension;
@@ -58,7 +60,14 @@ class JamesWithNonCompatibleElasticSearchServerTest {
     @Test
     void jamesShouldStopWhenStartingWithANonCompatibleElasticSearchServer(GuiceJamesServer server) throws Exception {
         assertThatThrownBy(server::start)
-            .isInstanceOf(ElasticsearchStatusException.class);
+            .isInstanceOfSatisfying(
+                StartUpChecksPerformer.StartUpChecksException.class,
+                ex -> assertThat(ex.getBadChecks())
+                    .containsOnly(CheckResult.builder()
+                        .checkName(ElasticSearchStartUpCheck.CHECK_NAME)
+                        .resultType(StartUpCheck.ResultType.BAD)
+                        .description("ES version(2.4.6) is not compatible with the recommendation(6.7.2)")
+                        .build()));
 
         assertThat(server.isStarted())
             .isFalse();
diff --git a/server/container/lifecycle-api/pom.xml b/server/container/lifecycle-api/pom.xml
index 4c9e47a..8de68af 100644
--- a/server/container/lifecycle-api/pom.xml
+++ b/server/container/lifecycle-api/pom.xml
@@ -42,6 +42,16 @@
             <artifactId>commons-configuration</artifactId>
         </dependency>
         <dependency>
+            <groupId>nl.jqno.equalsverifier</groupId>
+            <artifactId>equalsverifier</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>jcl-over-slf4j</artifactId>
         </dependency>
diff --git a/server/container/lifecycle-api/src/main/java/org/apache/james/lifecycle/api/StartUpCheck.java b/server/container/lifecycle-api/src/main/java/org/apache/james/lifecycle/api/StartUpCheck.java
index 87c3420..af0d013 100644
--- a/server/container/lifecycle-api/src/main/java/org/apache/james/lifecycle/api/StartUpCheck.java
+++ b/server/container/lifecycle-api/src/main/java/org/apache/james/lifecycle/api/StartUpCheck.java
@@ -19,6 +19,7 @@
 
 package org.apache.james.lifecycle.api;
 
+import java.util.Objects;
 import java.util.Optional;
 
 import com.google.common.base.MoreObjects;
@@ -105,6 +106,24 @@ public interface StartUpCheck {
             return resultType.equals(ResultType.GOOD);
         }
 
+
+        @Override
+        public final boolean equals(Object o) {
+            if (o instanceof CheckResult) {
+                CheckResult that = (CheckResult) o;
+
+                return Objects.equals(this.name, that.name)
+                    && Objects.equals(this.resultType, that.resultType)
+                    && Objects.equals(this.description, that.description);
+            }
+            return false;
+        }
+
+        @Override
+        public final int hashCode() {
+            return Objects.hash(name, resultType, description);
+        }
+
         @Override
         public String toString() {
             return MoreObjects.toStringHelper(this)
diff --git a/server/container/lifecycle-api/src/test/java/org/apache/james/lifecycle/api/StartUpCheckTest.java b/server/container/lifecycle-api/src/test/java/org/apache/james/lifecycle/api/StartUpCheckTest.java
new file mode 100644
index 0000000..3aca485
--- /dev/null
+++ b/server/container/lifecycle-api/src/test/java/org/apache/james/lifecycle/api/StartUpCheckTest.java
@@ -0,0 +1,37 @@
+/****************************************************************
+ * 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.lifecycle.api;
+
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+
+import nl.jqno.equalsverifier.EqualsVerifier;
+
+class StartUpCheckTest {
+
+    @Nested
+    class CheckResultTest {
+        @Test
+        void shouldMatchBeanContract() {
+            EqualsVerifier.forClass(StartUpCheck.CheckResult.class)
+                .verify();
+        }
+    }
+}
\ No newline at end of file


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