JAMES-2202 Add an Alias in ElasticSearch
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/deb93a72 Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/deb93a72 Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/deb93a72 Branch: refs/heads/master Commit: deb93a722db3f35a0acb4423fbede5e12959ed98 Parents: aebca5f Author: benwa <btell...@linagora.com> Authored: Wed Oct 25 10:53:22 2017 +0700 Committer: benwa <btell...@linagora.com> Committed: Wed Nov 1 17:54:21 2017 +0700 ---------------------------------------------------------------------- .../org/apache/james/backends/es/AliasName.java | 49 +++++++++++++ .../james/backends/es/IndexCreationFactory.java | 42 ++++++++--- .../backends/es/IndexCreationFactoryTest.java | 51 +++++++++++++ .../backends/es/NodeMappingFactoryTest.java | 76 ++++++++++++++++++++ .../backends/es/search/ScrollIterableTest.java | 4 +- .../MailboxElasticsearchConstants.java | 2 + .../ElasticSearchIntegrationTest.java | 5 +- .../host/ElasticSearchHostSystem.java | 5 +- .../mailbox/ElasticSearchMailboxModule.java | 32 +++++++-- .../james/modules/TestElasticSearchModule.java | 4 +- .../mailbox/ElasticSearchMailboxModuleTest.java | 39 ++++++++++ src/site/xdoc/server/config-elasticsearch.xml | 5 +- 12 files changed, 293 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/AliasName.java ---------------------------------------------------------------------- diff --git a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/AliasName.java b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/AliasName.java new file mode 100644 index 0000000..80e934a --- /dev/null +++ b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/AliasName.java @@ -0,0 +1,49 @@ +/**************************************************************** + * 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.backends.es; + +import java.util.Objects; + +public class AliasName { + private final String value; + + public AliasName(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public final boolean equals(Object o) { + if (o instanceof AliasName) { + AliasName aliasName = (AliasName) o; + + return Objects.equals(this.value, aliasName.value); + } + return false; + } + + @Override + public final int hashCode() { + return Objects.hash(value); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/IndexCreationFactory.java ---------------------------------------------------------------------- diff --git a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/IndexCreationFactory.java b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/IndexCreationFactory.java index 9797ba2..2eb9e1b 100644 --- a/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/IndexCreationFactory.java +++ b/backends-common/elasticsearch/src/main/java/org/apache/james/backends/es/IndexCreationFactory.java @@ -23,6 +23,8 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import java.io.IOException; +import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest; +import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest; import org.elasticsearch.client.Client; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.indices.IndexAlreadyExistsException; @@ -36,31 +38,55 @@ public class IndexCreationFactory { private static final int DEFAULT_NB_REPLICA = 0; public static final String CASE_INSENSITIVE = "case_insensitive"; - public static Client createIndex(Client client, IndexName name, int nbShards, int nbReplica) { + public static Client createIndexAndAlias(Client client, IndexName indexName, AliasName aliasName, int nbShards, int nbReplica) { try { - return createIndex(client, name, generateSetting(nbShards, nbReplica)); + return createIndexAndAlias(client, indexName, aliasName, generateSetting(nbShards, nbReplica)); } catch (IOException e) { LOGGER.error("Error while creating index : ", e); return client; } } - public static Client createIndex(Client client, IndexName name) { - return createIndex(client, name, DEFAULT_NB_SHARDS, DEFAULT_NB_REPLICA); + public static Client createIndexAndAlias(Client client, IndexName indexName, AliasName aliasName) { + return createIndexAndAlias(client, indexName, aliasName, DEFAULT_NB_SHARDS, DEFAULT_NB_REPLICA); } - private static Client createIndex(Client client, IndexName name, XContentBuilder settings) { + private static Client createIndexAndAlias(Client client, IndexName indexName, AliasName aliasName, XContentBuilder settings) { + createIndexIfNeeded(client, indexName, settings); + createAliasIfNeeded(client, indexName, aliasName); + return client; + } + + private static void createAliasIfNeeded(Client client, IndexName indexName, AliasName aliasName) { + if (!aliasExist(client, aliasName)) { + client.admin() + .indices() + .aliases( new IndicesAliasesRequest() + .addAlias(aliasName.getValue(), indexName.getValue())) + .actionGet(); + } + } + + private static boolean aliasExist(Client client, AliasName aliasName) { + return client.admin() + .indices() + .aliasesExist(new GetAliasesRequest() + .aliases(aliasName.getValue())) + .actionGet() + .exists(); + } + + private static void createIndexIfNeeded(Client client, IndexName indexName, XContentBuilder settings) { try { client.admin() .indices() - .prepareCreate(name.getValue()) + .prepareCreate(indexName.getValue()) .setSettings(settings) .execute() .actionGet(); } catch (IndexAlreadyExistsException exception) { - LOGGER.info("Index [" + name + "] already exist"); + LOGGER.info("Index [" + indexName + "] already exist"); } - return client; } private static XContentBuilder generateSetting(int nbShards, int nbReplica) throws IOException { http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/IndexCreationFactoryTest.java ---------------------------------------------------------------------- diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/IndexCreationFactoryTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/IndexCreationFactoryTest.java new file mode 100644 index 0000000..dee1142 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/IndexCreationFactoryTest.java @@ -0,0 +1,51 @@ +/**************************************************************** + * 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.backends.es; + +import org.apache.james.backends.es.utils.TestingClientProvider; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.junit.rules.TemporaryFolder; + +public class IndexCreationFactoryTest { + public static final IndexName INDEX_NAME = new IndexName("index"); + public static final AliasName ALIAS_NAME = new AliasName("alias"); + + private TemporaryFolder temporaryFolder = new TemporaryFolder(); + private EmbeddedElasticSearch embeddedElasticSearch= new EmbeddedElasticSearch(temporaryFolder, INDEX_NAME); + + @Rule + public RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch); + + private ClientProvider clientProvider; + + @Before + public void setUp() { + clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode()); + IndexCreationFactory.createIndexAndAlias(clientProvider.get(), INDEX_NAME, ALIAS_NAME); + } + + @Test + public void createIndexAndAliasShouldNotThrowWhenCalledSeveralTime() { + IndexCreationFactory.createIndexAndAlias(clientProvider.get(), INDEX_NAME, ALIAS_NAME); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryTest.java ---------------------------------------------------------------------- diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryTest.java new file mode 100644 index 0000000..6aca8d7 --- /dev/null +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/NodeMappingFactoryTest.java @@ -0,0 +1,76 @@ +/**************************************************************** + * 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.backends.es; + +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; + +import org.apache.james.backends.es.utils.TestingClientProvider; +import org.elasticsearch.common.xcontent.XContentBuilder; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.RuleChain; +import org.junit.rules.TemporaryFolder; + +public class NodeMappingFactoryTest { + public static final String MESSAGE = "message"; + public static final IndexName INDEX_NAME = new IndexName("index"); + public static final AliasName ALIAS_NAME = new AliasName("alias"); + public static final TypeName TYPE_NAME = new TypeName("type"); + + private TemporaryFolder temporaryFolder = new TemporaryFolder(); + private EmbeddedElasticSearch embeddedElasticSearch = new EmbeddedElasticSearch(temporaryFolder, INDEX_NAME); + + @Rule + public RuleChain ruleChain = RuleChain.outerRule(temporaryFolder).around(embeddedElasticSearch); + + private ClientProvider clientProvider; + + @Before + public void setUp() throws Exception { + clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode()); + IndexCreationFactory.createIndexAndAlias(clientProvider.get(), INDEX_NAME, ALIAS_NAME); + NodeMappingFactory.applyMapping(clientProvider.get(), + INDEX_NAME, + TYPE_NAME, + getMappingsSources()); + } + + @Test + public void applyMappingShouldNotThrowWhenCalledSeveralTime() throws Exception { + NodeMappingFactory.applyMapping(clientProvider.get(), + INDEX_NAME, + TYPE_NAME, + getMappingsSources()); + } + + private XContentBuilder getMappingsSources() throws Exception { + return jsonBuilder() + .startObject() + .startObject(TYPE_NAME.getValue()) + .startObject(NodeMappingFactory.PROPERTIES) + .startObject(MESSAGE) + .field(NodeMappingFactory.TYPE, NodeMappingFactory.STRING) + .endObject() + .endObject() + .endObject() + .endObject(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/search/ScrollIterableTest.java ---------------------------------------------------------------------- diff --git a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/search/ScrollIterableTest.java b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/search/ScrollIterableTest.java index 121b8b7..4515f96 100644 --- a/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/search/ScrollIterableTest.java +++ b/backends-common/elasticsearch/src/test/java/org/apache/james/backends/es/search/ScrollIterableTest.java @@ -28,6 +28,7 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import org.apache.james.backends.es.AliasName; import org.apache.james.backends.es.ClientProvider; import org.apache.james.backends.es.EmbeddedElasticSearch; import org.apache.james.backends.es.IndexCreationFactory; @@ -52,6 +53,7 @@ public class ScrollIterableTest { public static final int SIZE = 2; public static final String MESSAGE = "message"; public static final IndexName INDEX_NAME = new IndexName("index"); + public static final AliasName ALIAS_NAME = new AliasName("alias"); public static final TypeName TYPE_NAME = new TypeName("messages"); private TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -65,7 +67,7 @@ public class ScrollIterableTest { @Before public void setUp() throws Exception { clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode()); - IndexCreationFactory.createIndex(clientProvider.get(), INDEX_NAME); + IndexCreationFactory.createIndexAndAlias(clientProvider.get(), INDEX_NAME, ALIAS_NAME); embeddedElasticSearch.awaitForElasticSearch(); NodeMappingFactory.applyMapping(clientProvider.get(), INDEX_NAME, TYPE_NAME, getMappingsSources()); } http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/MailboxElasticsearchConstants.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/MailboxElasticsearchConstants.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/MailboxElasticsearchConstants.java index 9539e53..fb89b8e 100644 --- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/MailboxElasticsearchConstants.java +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/MailboxElasticsearchConstants.java @@ -19,10 +19,12 @@ package org.apache.james.mailbox.elasticsearch; +import org.apache.james.backends.es.AliasName; import org.apache.james.backends.es.IndexName; import org.apache.james.backends.es.TypeName; public interface MailboxElasticsearchConstants { + AliasName DEFAULT_MAILBOX_ALIAS = new AliasName("mailboxAlias"); IndexName DEFAULT_MAILBOX_INDEX = new IndexName("mailbox"); TypeName MESSAGE_TYPE = new TypeName("message"); } http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIntegrationTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIntegrationTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIntegrationTest.java index eaf7764..cab06e5 100644 --- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIntegrationTest.java +++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/ElasticSearchIntegrationTest.java @@ -110,9 +110,10 @@ public class ElasticSearchIntegrationTest extends AbstractMessageSearchIndexTest @Override protected void initializeMailboxManager() throws Exception { Client client = NodeMappingFactory.applyMapping( - IndexCreationFactory.createIndex( + IndexCreationFactory.createIndexAndAlias( new TestingClientProvider(embeddedElasticSearch.getNode()).get(), - MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX), + MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, + MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS), MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, MailboxElasticsearchConstants.MESSAGE_TYPE, MailboxMappingFactory.getMappingContent()); http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java ---------------------------------------------------------------------- diff --git a/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java b/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java index 948acd8..2a90a99 100644 --- a/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java +++ b/mpt/impl/imap-mailbox/elasticsearch/src/test/java/org/apache/james/mpt/imapmailbox/elasticsearch/host/ElasticSearchHostSystem.java @@ -95,7 +95,10 @@ public class ElasticSearchHostSystem extends JamesImapHostSystem { private void initFields() { Client client = NodeMappingFactory.applyMapping( - IndexCreationFactory.createIndex(new TestingClientProvider(embeddedElasticSearch.getNode()).get(), MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX), + IndexCreationFactory.createIndexAndAlias( + new TestingClientProvider(embeddedElasticSearch.getNode()).get(), + MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, + MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS), MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, MailboxElasticsearchConstants.MESSAGE_TYPE, MailboxMappingFactory.getMappingContent()); http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/server/container/guice/cassandra-guice/src/main/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModule.java ---------------------------------------------------------------------- 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 dee84b5..ea59317 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 @@ -28,6 +28,7 @@ import javax.inject.Singleton; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.backends.es.AliasName; import org.apache.james.backends.es.ClientProviderImpl; import org.apache.james.backends.es.IndexCreationFactory; import org.apache.james.backends.es.IndexName; @@ -75,7 +76,8 @@ public class ElasticSearchMailboxModule extends AbstractModule { } @Provides - protected IndexName provideIndexName(ElasticSearchConfiguration elasticSearchConfiguration) throws ConfigurationException { + protected IndexName provideIndexName(ElasticSearchConfiguration elasticSearchConfiguration) + throws ConfigurationException { try { return Optional.ofNullable(elasticSearchConfiguration.getConfiguration() .getString("elasticsearch.index.name")) @@ -88,22 +90,37 @@ public class ElasticSearchMailboxModule extends AbstractModule { } @Provides + protected AliasName provideAliasName(ElasticSearchConfiguration elasticSearchConfiguration) + throws ConfigurationException { + try { + return Optional.ofNullable(elasticSearchConfiguration.getConfiguration() + .getString("elasticsearch.alias.name")) + .map(AliasName::new) + .orElse(MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS); + } catch (FileNotFoundException e) { + LOGGER.info("Could not find ElasticSearch configuration file. Using default alias {}", MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX.getValue()); + return MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS; + } + } + + @Provides @Singleton protected Client provideClientProvider(ElasticSearchConfiguration elasticSearchConfiguration, - IndexName indexName, + IndexName indexName, AliasName aliasName, AsyncRetryExecutor executor) throws ConfigurationException, FileNotFoundException, ExecutionException, InterruptedException { PropertiesConfiguration propertiesReader = elasticSearchConfiguration.getConfiguration(); int maxRetries = propertiesReader.getInt("elasticsearch.retryConnection.maxRetries", DEFAULT_CONNECTION_MAX_RETRIES); int minDelay = propertiesReader.getInt("elasticsearch.retryConnection.minDelay", DEFAULT_CONNECTION_MIN_DELAY); return RetryExecutorUtil.retryOnExceptions(executor, maxRetries, minDelay, NoNodeAvailableException.class) - .getWithRetry(context -> connectToCluster(propertiesReader, indexName)) + .getWithRetry(context -> connectToCluster(propertiesReader, indexName, aliasName)) .get(); } - private Client createIndexAndMapping(Client client, IndexName indexName, PropertiesConfiguration propertiesReader) { - IndexCreationFactory.createIndex(client, + private Client createIndexAndMapping(Client client, IndexName indexName, AliasName aliasName, PropertiesConfiguration propertiesReader) { + IndexCreationFactory.createIndexAndAlias(client, indexName, + aliasName, propertiesReader.getInt(ELASTICSEARCH_CONFIGURATION_NAME + ".nb.shards", DEFAULT_NB_SHARDS), propertiesReader.getInt(ELASTICSEARCH_CONFIGURATION_NAME + ".nb.replica", DEFAULT_NB_REPLICA)); NodeMappingFactory.applyMapping(client, @@ -113,10 +130,11 @@ public class ElasticSearchMailboxModule extends AbstractModule { return client; } - private Client connectToCluster(PropertiesConfiguration propertiesReader, IndexName indexName) throws ConfigurationException { + private Client connectToCluster(PropertiesConfiguration propertiesReader, IndexName indexName, AliasName aliasName) + throws ConfigurationException { LOGGER.info("Trying to connect to ElasticSearch service at {}", LocalDateTime.now()); - return createIndexAndMapping(createClient(propertiesReader), indexName, propertiesReader); + return createIndexAndMapping(createClient(propertiesReader), indexName, aliasName, propertiesReader); } @Provides http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java index 7a984ec..2b6b272 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/TestElasticSearchModule.java @@ -49,7 +49,9 @@ public class TestElasticSearchModule extends AbstractModule{ @Singleton protected Client provideClientProvider() { Client client = new TestingClientProvider(embeddedElasticSearch.getNode()).get(); - IndexCreationFactory.createIndex(client, MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX); + IndexCreationFactory.createIndexAndAlias(client, + MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, + MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS); return NodeMappingFactory.applyMapping(client, MailboxElasticsearchConstants.DEFAULT_MAILBOX_INDEX, MailboxElasticsearchConstants.MESSAGE_TYPE, http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModuleTest.java ---------------------------------------------------------------------- diff --git a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModuleTest.java b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModuleTest.java index dc590a9..41108e7 100644 --- a/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModuleTest.java +++ b/server/container/guice/cassandra-guice/src/test/java/org/apache/james/modules/mailbox/ElasticSearchMailboxModuleTest.java @@ -29,6 +29,7 @@ import java.util.Optional; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; +import org.apache.james.backends.es.AliasName; import org.apache.james.backends.es.IndexName; import org.apache.james.mailbox.elasticsearch.IndexAttachments; import org.apache.james.mailbox.elasticsearch.MailboxElasticsearchConstants; @@ -80,6 +81,44 @@ public class ElasticSearchMailboxModuleTest { } @Test + public void provideAliasNameShouldRetrievedConfiguredAliasName() throws ConfigurationException { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + String name = "name"; + configuration.addProperty("elasticsearch.alias.name", name); + + ElasticSearchMailboxModule testee = new ElasticSearchMailboxModule(); + + AliasName indexName = testee.provideAliasName(() -> configuration); + + assertThat(indexName) + .isEqualTo(new AliasName(name)); + } + + @Test + public void provideAliasNameShouldReturnDefaultAliasNameWhenNone() throws ConfigurationException { + PropertiesConfiguration configuration = new PropertiesConfiguration(); + + ElasticSearchMailboxModule testee = new ElasticSearchMailboxModule(); + + AliasName aliasName = testee.provideAliasName(() -> configuration); + + assertThat(aliasName) + .isEqualTo(MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS); + } + + @Test + public void provideAliasNameShouldReturnDefaultAliasNameWhenError() throws ConfigurationException { + ElasticSearchMailboxModule testee = new ElasticSearchMailboxModule(); + + AliasName aliasName = testee.provideAliasName(() -> { + throw new FileNotFoundException(); + }); + + assertThat(aliasName) + .isEqualTo(MailboxElasticsearchConstants.DEFAULT_MAILBOX_ALIAS); + } + + @Test public void provideIndexAttachmentsShouldReturnTrueWhenIndexAttachmentsIsTrueInConfiguration() { PropertiesConfiguration configuration = new PropertiesConfiguration(); configuration.addProperty("elasticsearch.indexAttachments", true); http://git-wip-us.apache.org/repos/asf/james-project/blob/deb93a72/src/site/xdoc/server/config-elasticsearch.xml ---------------------------------------------------------------------- diff --git a/src/site/xdoc/server/config-elasticsearch.xml b/src/site/xdoc/server/config-elasticsearch.xml index 94420c2..2b9ad23 100644 --- a/src/site/xdoc/server/config-elasticsearch.xml +++ b/src/site/xdoc/server/config-elasticsearch.xml @@ -57,7 +57,10 @@ <dt><strong>elasticsearch.nb.replica</strong></dt> <dd>Number of replica for index provisionned by James</dd> <dt><strong>elasticsearch.index.name</strong></dt> - <dd>Name of the index to use with Apache James. It will be created if missing.</dd> + <dd>Name of the index backed by the alias. It will be created if missing.</dd> + <dt><strong>elasticsearch.alias.name</strong></dt> + <dd>Name of the alias to use with Apache James. It will be created if missing. + The target of the alias is the index name configured above.</dd> <dt><strong>elasticsearch.retryConnection.maxRetries</strong></dt> <dd>Number of retries when connecting the cluster</dd> <dt><strong>elasticsearch.retryConnection.minDelay</strong></dt> --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org