Skip to content

Commit

Permalink
allow passing in cassandra credentials via hector or astyanax
Browse files Browse the repository at this point in the history
  • Loading branch information
boriwo committed Oct 6, 2014
1 parent 0c8e578 commit c8a8351
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions config/cmb.properties
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ cmb.cassandra.readConsistencyLevel=QUORUM
cmb.cassandra.writeConsistencyLevel=QUORUM
cmb.cassandra.thriftSocketTimeOutMS=10000
cmb.cassandra.dataCenter=
#cmb.cassandra.username=
#cmb.cassandra.password=

cmb.astyanax.maxConnectionsPerNode=10
cmb.astyanax.connectionWaitTimeOutMS=2000
Expand All @@ -126,8 +128,6 @@ cmb.hector.balancingPolicy=RoundRobinBalancingPolicy

cmb.hector.autoDiscovery=true
cmb.hector.autoDiscoveryDelaySeconds=60
#cmb.hector.username=
#cmb.hector.password=

# only change cassandra keyspace names if you also changed them in schema.txt

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;
import org.jfree.util.Log;

import com.comcast.cmb.common.controller.CMBControllerServlet;
import com.comcast.cmb.common.util.CMBErrorCodes;
import com.comcast.cmb.common.util.CMBProperties;
import com.comcast.cmb.common.util.PersistenceException;
import com.comcast.cmb.common.util.ValueAccumulator.AccumulatorName;
import com.netflix.astyanax.AstyanaxContext;
import com.netflix.astyanax.AuthenticationCredentials;
import com.netflix.astyanax.ColumnListMutation;
import com.netflix.astyanax.Keyspace;
import com.netflix.astyanax.MutationBatch;
Expand All @@ -42,6 +44,7 @@
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolConfigurationImpl;
import com.netflix.astyanax.connectionpool.impl.ConnectionPoolType;
import com.netflix.astyanax.connectionpool.impl.CountingConnectionPoolMonitor;
import com.netflix.astyanax.connectionpool.impl.SimpleAuthenticationCredentials;
import com.netflix.astyanax.impl.AstyanaxConfigurationImpl;
import com.netflix.astyanax.model.Column;
import com.netflix.astyanax.model.ColumnFamily;
Expand Down Expand Up @@ -89,14 +92,20 @@ private void initPersistence() {
keyspaceNames.add(CMBProperties.getInstance().getCQSKeyspace());

String dataCenter = CMBProperties.getInstance().getCassandraDataCenter();
String username = CMBProperties.getInstance().getCassandraUsername();
String password = CMBProperties.getInstance().getCassandraPassword();

for (String k : keyspaceNames) {
//configure pool

ConnectionPoolConfigurationImpl connectionPoolConfiguration = new ConnectionPoolConfigurationImpl("CMBAstyananxConnectionPool")
.setMaxConnsPerHost(CMBProperties.getInstance().getAstyanaxMaxConnectionsPerNode())
.setSocketTimeout(CMBProperties.getInstance().getCassandraThriftSocketTimeOutMS())
.setConnectTimeout(CMBProperties.getInstance().getAstyanaxConnectionWaitTimeOutMS())
.setSeeds(AbstractDurablePersistence.CLUSTER_URL);

if (username != null && password != null) {
connectionPoolConfiguration.setAuthenticationCredentials(new SimpleAuthenticationCredentials(username, password));
}

if (dataCenter != null && !dataCenter.equals("")) {
connectionPoolConfiguration.setLocalDatacenter(dataCenter);
Expand All @@ -105,7 +114,7 @@ private void initPersistence() {
AstyanaxContext<Keyspace> context = new AstyanaxContext.Builder()
.forCluster(CLUSTER_NAME)
.forKeyspace(k)
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.withAstyanaxConfiguration(new AstyanaxConfigurationImpl()
.setDiscoveryType(NodeDiscoveryType.RING_DESCRIBE)
.setConnectionPoolType(ConnectionPoolType.TOKEN_AWARE)
.setDefaultReadConsistencyLevel(ConsistencyLevel.valueOf("CL_"+CMBProperties.getInstance().getReadConsistencyLevel()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class CassandraHectorPersistence extends AbstractDurablePersistence {

private static final int hectorPoolSize = CMBProperties.getInstance().getHectorPoolSize();
private static final String hectorBalancingPolicy = CMBProperties.getInstance().getHectorBalancingPolicy();
private static final Map<String, String> credentials = CMBProperties.getInstance().getHectorCredentials();
private static final Map<String, String> credentials = CMBProperties.getInstance().getCassandraCredentials();

private static Cluster cluster;
private static Map<String, Keyspace> keyspaces;
Expand Down
31 changes: 23 additions & 8 deletions src/com/comcast/cmb/common/util/CMBProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public enum IO_MODE {
private final boolean hectorAutoDiscovery;
private final int hectorAutoDiscoveryDelaySeconds;
private final String cassandraDataCenter;
private final Map<String, String> hectorCredentials;
private final Map<String, String> cassandraCredentials;

private final String region;

Expand Down Expand Up @@ -287,12 +287,13 @@ private CMBProperties() {
hectorAutoDiscoveryDelaySeconds = Integer.parseInt(props.getProperty("cmb.hector.autoDiscoveryDelaySeconds", "60"));
hectorAutoDiscovery = Boolean.parseBoolean(props.getProperty("cmb.hector.autoDiscovery", "true"));
hectorBalancingPolicy = props.getProperty("cmb.hector.balancingPolicy", "RoundRobinBalancingPolicy");
if (props.getProperty("cmb.hector.username") != null) {
hectorCredentials = new HashMap<String, String>(2);
hectorCredentials.put("username", props.getProperty("cmb.hector.username", ""));
hectorCredentials.put("password", props.getProperty("cmb.hector.password", ""));

if (props.getProperty("cmb.cassandra.username") != null && props.getProperty("cmb.cassandra.password") != null) {
cassandraCredentials = new HashMap<String, String>(2);
cassandraCredentials.put("username", props.getProperty("cmb.cassandra.username", ""));
cassandraCredentials.put("password", props.getProperty("cmb.cassandra.password", ""));
} else {
hectorCredentials = null;
cassandraCredentials = null;
}

smtpHostName = props.getProperty("cmb.cns.smtp.hostname");
Expand Down Expand Up @@ -809,8 +810,22 @@ public int getHectorAutoDiscoveryDelaySeconds() {
return hectorAutoDiscoveryDelaySeconds;
}

public Map<String, String> getHectorCredentials() {
return hectorCredentials;
public Map<String, String> getCassandraCredentials() {
return cassandraCredentials;
}

public String getCassandraUsername() {
if (cassandraCredentials == null) {
return null;
}
return cassandraCredentials.get("username");
}

public String getCassandraPassword() {
if (cassandraCredentials == null) {
return null;
}
return cassandraCredentials.get("password");
}

public boolean isCNSPublisherEnabled() {
Expand Down

0 comments on commit c8a8351

Please sign in to comment.