Skip to content

Commit

Permalink
JAMES-2202 Handle a read and a write alias
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Nov 1, 2017
1 parent fc927f0 commit 27fe978
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 28 deletions.
Expand Up @@ -22,6 +22,7 @@
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;

import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest;
Expand All @@ -33,7 +34,6 @@
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

public class IndexCreationFactory {

Expand All @@ -43,18 +43,18 @@ public class IndexCreationFactory {
public static final String CASE_INSENSITIVE = "case_insensitive";

private IndexName indexName;
private ImmutableList.Builder<AliasName> aliases;
private ArrayList<AliasName> aliases;
private Optional<Integer> nbShards;
private Optional<Integer> nbReplica;

public IndexCreationFactory() {
indexName = null;
aliases = ImmutableList.builder();
aliases = new ArrayList<>();
nbShards = Optional.empty();
nbReplica = Optional.empty();
}

public IndexCreationFactory onIndex(IndexName indexName) {
public IndexCreationFactory useIndex(IndexName indexName) {
Preconditions.checkNotNull(indexName);
this.indexName = indexName;
return this;
Expand All @@ -67,11 +67,13 @@ public IndexCreationFactory addAlias(AliasName aliasName) {
}

public IndexCreationFactory nbShards(int nbShards) {
Preconditions.checkArgument(nbShards > 0, "You need the number of shards to be strictly positive");
this.nbShards = Optional.of(nbShards);
return this;
}

public IndexCreationFactory nbReplica(int nbReplica) {
Preconditions.checkArgument(nbReplica >= 0, "You need the number of replica to be positive");
this.nbReplica = Optional.of(nbReplica);
return this;
}
Expand All @@ -82,8 +84,7 @@ public Client createIndexAndAliases(Client client) {
createIndexIfNeeded(client, indexName, generateSetting(
nbShards.orElse(DEFAULT_NB_SHARDS),
nbReplica.orElse(DEFAULT_NB_REPLICA)));
aliases.build()
.forEach(alias -> createAliasIfNeeded(client, indexName, alias));
aliases.forEach(alias -> createAliasIfNeeded(client, indexName, alias));
} catch (IOException e) {
LOGGER.error("Error while creating index : ", e);
}
Expand Down
Expand Up @@ -59,7 +59,7 @@ public void setup() throws IOException {
node = embeddedElasticSearch.getNode();
TestingClientProvider clientProvider = new TestingClientProvider(node);
new IndexCreationFactory()
.onIndex(INDEX_NAME)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
DeleteByQueryPerformer deleteByQueryPerformer = new DeleteByQueryPerformer(clientProvider.get(),
Expand Down
Expand Up @@ -19,6 +19,8 @@

package org.apache.james.backends.es;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.apache.james.backends.es.utils.TestingClientProvider;
import org.junit.Before;
import org.junit.Rule;
Expand All @@ -42,16 +44,56 @@ public class IndexCreationFactoryTest {
public void setUp() {
clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode());
new IndexCreationFactory()
.onIndex(INDEX_NAME)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
}

@Test
public void createIndexAndAliasShouldNotThrowWhenCalledSeveralTime() {
new IndexCreationFactory()
.onIndex(INDEX_NAME)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
}

@Test
public void useIndexShouldThrowWhenNull() {
assertThatThrownBy(() ->
new IndexCreationFactory()
.useIndex(null))
.isInstanceOf(NullPointerException.class);
}

@Test
public void addAliasShouldThrowWhenNull() {
assertThatThrownBy(() ->
new IndexCreationFactory()
.addAlias(null))
.isInstanceOf(NullPointerException.class);
}

@Test
public void nbReplicaShouldThrowWhenNegative() {
assertThatThrownBy(() ->
new IndexCreationFactory()
.nbReplica(-1))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void nbShardsShouldThrowWhenNegative() {
assertThatThrownBy(() ->
new IndexCreationFactory()
.nbShards(-1))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void nbShardsShouldThrowWhenZero() {
assertThatThrownBy(() ->
new IndexCreationFactory()
.nbShards(0))
.isInstanceOf(IllegalArgumentException.class);
}
}
Expand Up @@ -47,7 +47,7 @@ public class NodeMappingFactoryTest {
public void setUp() throws Exception {
clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode());
new IndexCreationFactory()
.onIndex(INDEX_NAME)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
NodeMappingFactory.applyMapping(clientProvider.get(),
Expand Down
Expand Up @@ -68,7 +68,7 @@ public class ScrollIterableTest {
public void setUp() throws Exception {
clientProvider = new TestingClientProvider(embeddedElasticSearch.getNode());
new IndexCreationFactory()
.onIndex(INDEX_NAME)
.useIndex(INDEX_NAME)
.addAlias(ALIAS_NAME)
.createIndexAndAliases(clientProvider.get());
embeddedElasticSearch.awaitForElasticSearch();
Expand Down
Expand Up @@ -111,9 +111,9 @@ protected void await() {
protected void initializeMailboxManager() throws Exception {
Client client = NodeMappingFactory.applyMapping(
new IndexCreationFactory()
.onIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.addAlias( MailboxElasticSearchConstants.DEFAULT_MAILBOX_READ_ALIAS)
.addAlias( MailboxElasticSearchConstants.DEFAULT_MAILBOX_WRITE_ALIAS)
.useIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_READ_ALIAS)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_WRITE_ALIAS)
.createIndexAndAliases(new TestingClientProvider(embeddedElasticSearch.getNode()).get()),
MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX,
MailboxElasticSearchConstants.MESSAGE_TYPE,
Expand Down
Expand Up @@ -96,7 +96,7 @@ public void afterTest() throws Exception {
private void initFields() {
Client client = NodeMappingFactory.applyMapping(
new IndexCreationFactory()
.onIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.useIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_WRITE_ALIAS)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_READ_ALIAS)
.createIndexAndAliases(new TestingClientProvider(embeddedElasticSearch.getNode()).get()),
Expand Down
Expand Up @@ -91,35 +91,35 @@ private static IndexAttachments provideIndexAttachments(PropertiesConfiguration
}

private static ImmutableList<Host> getHosts(PropertiesConfiguration propertiesReader) throws ConfigurationException {
Optional<String> monoHostAddress = Optional.ofNullable(
Optional<String> masterHost = Optional.ofNullable(
propertiesReader.getString(ELASTICSEARCH_MASTER_HOST, null));
Optional<Integer> monoHostPort = Optional.ofNullable(
Optional<Integer> masterPort = Optional.ofNullable(
propertiesReader.getInteger(ELASTICSEARCH_PORT, null));
Optional<String> multiHosts = Optional.ofNullable(
propertiesReader.getString(ELASTICSEARCH_HOSTS, null));

validateHostsConfigurationOptions(monoHostAddress, monoHostPort, multiHosts);
validateHostsConfigurationOptions(masterHost, masterPort, multiHosts);

if (monoHostAddress.isPresent()) {
if (masterHost.isPresent()) {
return ImmutableList.of(
Host.from(monoHostAddress.get(),
monoHostPort.get()));
Host.from(masterHost.get(),
masterPort.get()));
} else {
return Host.parseHosts(multiHosts.get(), DEFAULT_PORT);
}
}

@VisibleForTesting
static void validateHostsConfigurationOptions(Optional<String> monoHostAddress,
Optional<Integer> monoHostPort,
static void validateHostsConfigurationOptions(Optional<String> masterHost,
Optional<Integer> masterPort,
Optional<String> multiHosts) throws ConfigurationException {
if (monoHostAddress.isPresent() != monoHostPort.isPresent()) {
if (masterHost.isPresent() != masterPort.isPresent()) {
throw new ConfigurationException(ELASTICSEARCH_MASTER_HOST + " and " + ELASTICSEARCH_PORT + " should be specified together");
}
if (multiHosts.isPresent() && monoHostAddress.isPresent()) {
if (multiHosts.isPresent() && masterHost.isPresent()) {
throw new ConfigurationException("You should choose between mono host set up and " + ELASTICSEARCH_HOSTS);
}
if (!multiHosts.isPresent() && !monoHostAddress.isPresent()) {
if (!multiHosts.isPresent() && !masterHost.isPresent()) {
throw new ConfigurationException("You should specify either (" + ELASTICSEARCH_MASTER_HOST + " and " + ELASTICSEARCH_PORT + ") or " + ELASTICSEARCH_HOSTS);
}
}
Expand Down
Expand Up @@ -100,7 +100,7 @@ protected AliasName provideWriteAliasName(ElasticSearchConfiguration configurati
@Singleton
protected IndexCreationFactory provideIndexCreationFactory(ElasticSearchConfiguration configuration) {
return new IndexCreationFactory()
.onIndex(configuration.getIndexName())
.useIndex(configuration.getIndexName())
.addAlias(configuration.getReadAliasName())
.addAlias(configuration.getWriteAliasName())
.nbShards(configuration.getNbShards())
Expand Down
Expand Up @@ -51,7 +51,7 @@ protected Client provideClientProvider() {
Client client = new TestingClientProvider(embeddedElasticSearch.getNode()).get();

new IndexCreationFactory()
.onIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.useIndex(MailboxElasticSearchConstants.DEFAULT_MAILBOX_INDEX)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_READ_ALIAS)
.addAlias(MailboxElasticSearchConstants.DEFAULT_MAILBOX_WRITE_ALIAS)
.createIndexAndAliases(client);
Expand Down

0 comments on commit 27fe978

Please sign in to comment.