Skip to content

Commit

Permalink
JAMES-2004 Cassandra should try to connect locally by default
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Apr 21, 2017
1 parent ac3fb27 commit b62d8b6
Showing 1 changed file with 38 additions and 28 deletions.
Expand Up @@ -18,18 +18,15 @@
****************************************************************/
package org.apache.james.modules.mailbox;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PerHostPercentileTracker;
import com.datastax.driver.core.QueryLogger;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.github.steveash.guavate.Guavate;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
import com.nurkiewicz.asyncretry.function.RetryCallable;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.james.backends.cassandra.components.CassandraModule;
Expand All @@ -39,29 +36,35 @@
import org.apache.james.backends.cassandra.init.ClusterWithKeyspaceCreatedFactory;
import org.apache.james.backends.cassandra.init.QueryLoggerConfiguration;
import org.apache.james.backends.cassandra.init.SessionWithInitializedTablesFactory;
import org.apache.james.filesystem.api.FileSystem;
import org.apache.james.util.Host;
import org.apache.james.utils.PropertiesProvider;
import org.apache.james.utils.RetryExecutorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PerHostPercentileTracker;
import com.datastax.driver.core.QueryLogger;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.NoHostAvailableException;
import com.github.steveash.guavate.Guavate;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import com.nurkiewicz.asyncretry.AsyncRetryExecutor;
import com.nurkiewicz.asyncretry.function.RetryCallable;

public class CassandraSessionModule extends AbstractModule {
private static final Logger LOGGER = LoggerFactory.getLogger(CassandraSessionModule.class);

private static final int DEFAULT_CONNECTION_MAX_RETRIES = 10;
private static final int DEFAULT_CONNECTION_MIN_DELAY = 5000;
private static final long CASSANDRA_HIGHEST_TRACKABLE_LATENCY_MILLIS = TimeUnit.SECONDS.toMillis(10);
public static final int DEFAULT_REPLICATION_FACTOR = 1;
public static final String DEFAULT_KEYSPACE = "apache_james";
private static final int DEFAULT_REPLICATION_FACTOR = 1;
private static final String DEFAULT_KEYSPACE = "apache_james";
private static final String CASSANDRA_NODES = "cassandra.nodes";
private static final String LOCALHOST = "127.0.0.1";

@Override
protected void configure() {
Expand All @@ -87,8 +90,8 @@ Session provideSession(CassandraSessionConfiguration configuration, Cluster clus

@Provides
@Singleton
CassandraSessionConfiguration getCassandraSessionConfiguration(FileSystem fileSystem) {
return () -> getConfiguration(fileSystem);
CassandraSessionConfiguration getCassandraSessionConfiguration(PropertiesProvider propertiesProvider) {
return () -> getConfiguration(propertiesProvider);
}

@Provides
Expand Down Expand Up @@ -123,7 +126,7 @@ private Cluster getCluster(List<Host> servers, QueryLoggerConfiguration queryLog
}

private List<Host> listCassandraServers(PropertiesConfiguration configuration) {
String[] ipAndPorts = configuration.getStringArray("cassandra.nodes");
String[] ipAndPorts = configuration.getStringArray(CASSANDRA_NODES);

return Arrays.stream(ipAndPorts)
.map(string -> Host.parseConfString(string, ClusterBuilder.DEFAULT_CASSANDRA_PORT))
Expand Down Expand Up @@ -177,7 +180,14 @@ private AsyncRetryExecutor provideAsyncRetryExecutor(ScheduledExecutorService sc
return new AsyncRetryExecutor(scheduler);
}

private PropertiesConfiguration getConfiguration(FileSystem fileSystem) throws FileNotFoundException, ConfigurationException {
return new PropertiesConfiguration(fileSystem.getFile(FileSystem.FILE_PROTOCOL_AND_CONF + "cassandra.properties"));
private PropertiesConfiguration getConfiguration(PropertiesProvider propertiesProvider) throws ConfigurationException {
try {
return propertiesProvider.getConfiguration("cassandra");
} catch (FileNotFoundException e) {
LOGGER.warn("Could not locate cassandra configuration file. Defaulting to node " + LOCALHOST + ":9042");
PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration();
propertiesConfiguration.addProperty(CASSANDRA_NODES, LOCALHOST);
return propertiesConfiguration;
}
}
}

0 comments on commit b62d8b6

Please sign in to comment.