use of com.datastax.driver.core.policies.TokenAwarePolicy in project zipkin by openzipkin.
the class DefaultSessionFactory method buildCluster.
// Visible for testing
static Cluster buildCluster(Cassandra3Storage cassandra) {
Cluster.Builder builder = Cluster.builder();
List<InetSocketAddress> contactPoints = parseContactPoints(cassandra);
int defaultPort = findConnectPort(contactPoints);
builder.addContactPointsWithPorts(contactPoints);
// This ends up protocolOptions.port
builder.withPort(defaultPort);
if (cassandra.username != null && cassandra.password != null) {
builder.withCredentials(cassandra.username, cassandra.password);
}
builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE);
builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder(cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy()).build()));
builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, cassandra.maxConnections));
if (cassandra.useSsl) {
builder = builder.withSSL();
}
return builder.build();
}
use of com.datastax.driver.core.policies.TokenAwarePolicy in project presto by prestodb.
the class CassandraClientModule method createCassandraSession.
@Singleton
@Provides
public static CassandraSession createCassandraSession(CassandraConnectorId connectorId, CassandraClientConfig config, JsonCodec<List<ExtraColumnMetadata>> extraColumnMetadataCodec) {
requireNonNull(config, "config is null");
requireNonNull(extraColumnMetadataCodec, "extraColumnMetadataCodec is null");
Cluster.Builder clusterBuilder = Cluster.builder().withProtocolVersion(ProtocolVersion.V3);
List<String> contactPoints = requireNonNull(config.getContactPoints(), "contactPoints is null");
checkArgument(!contactPoints.isEmpty(), "empty contactPoints");
contactPoints.forEach(clusterBuilder::addContactPoint);
clusterBuilder.withPort(config.getNativeProtocolPort());
clusterBuilder.withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 10000));
clusterBuilder.withRetryPolicy(config.getRetryPolicy().getPolicy());
LoadBalancingPolicy loadPolicy = new RoundRobinPolicy();
if (config.isUseDCAware()) {
requireNonNull(config.getDcAwareLocalDC(), "DCAwarePolicy localDC is null");
DCAwareRoundRobinPolicy.Builder builder = DCAwareRoundRobinPolicy.builder().withLocalDc(config.getDcAwareLocalDC());
if (config.getDcAwareUsedHostsPerRemoteDc() > 0) {
builder.withUsedHostsPerRemoteDc(config.getDcAwareUsedHostsPerRemoteDc());
if (config.isDcAwareAllowRemoteDCsForLocal()) {
builder.allowRemoteDCsForLocalConsistencyLevel();
}
}
loadPolicy = builder.build();
}
if (config.isUseTokenAware()) {
loadPolicy = new TokenAwarePolicy(loadPolicy, config.isTokenAwareShuffleReplicas());
}
if (config.isUseWhiteList()) {
checkArgument(!config.getWhiteListAddresses().isEmpty(), "empty WhiteListAddresses");
List<InetSocketAddress> whiteList = new ArrayList<>();
for (String point : config.getWhiteListAddresses()) {
whiteList.add(new InetSocketAddress(point, config.getNativeProtocolPort()));
}
loadPolicy = new WhiteListPolicy(loadPolicy, whiteList);
}
clusterBuilder.withLoadBalancingPolicy(loadPolicy);
SocketOptions socketOptions = new SocketOptions();
socketOptions.setReadTimeoutMillis(toIntExact(config.getClientReadTimeout().toMillis()));
socketOptions.setConnectTimeoutMillis(toIntExact(config.getClientConnectTimeout().toMillis()));
if (config.getClientSoLinger() != null) {
socketOptions.setSoLinger(config.getClientSoLinger());
}
clusterBuilder.withSocketOptions(socketOptions);
if (config.getUsername() != null && config.getPassword() != null) {
clusterBuilder.withCredentials(config.getUsername(), config.getPassword());
}
QueryOptions options = new QueryOptions();
options.setFetchSize(config.getFetchSize());
options.setConsistencyLevel(config.getConsistencyLevel());
clusterBuilder.withQueryOptions(options);
if (config.getSpeculativeExecutionLimit() > 1) {
clusterBuilder.withSpeculativeExecutionPolicy(new ConstantSpeculativeExecutionPolicy(// delay before a new execution is launched
config.getSpeculativeExecutionDelay().toMillis(), // maximum number of executions
config.getSpeculativeExecutionLimit()));
}
return new NativeCassandraSession(connectorId.toString(), extraColumnMetadataCodec, clusterBuilder.build(), config.getNoHostAvailableRetryTimeout());
}
use of com.datastax.driver.core.policies.TokenAwarePolicy in project cassandra by apache.
the class JavaDriverClient method loadBalancingPolicy.
private LoadBalancingPolicy loadBalancingPolicy(StressSettings settings) {
DCAwareRoundRobinPolicy.Builder policyBuilder = DCAwareRoundRobinPolicy.builder();
if (settings.node.datacenter != null)
policyBuilder.withLocalDc(settings.node.datacenter);
LoadBalancingPolicy ret = null;
if (settings.node.datacenter != null)
ret = policyBuilder.build();
if (settings.node.isWhiteList)
ret = new WhiteListPolicy(ret == null ? policyBuilder.build() : ret, settings.node.resolveAll(settings.port.nativePort));
return new TokenAwarePolicy(ret == null ? policyBuilder.build() : ret);
}
use of com.datastax.driver.core.policies.TokenAwarePolicy in project storm by apache.
the class ClusterFactory method make.
/**
* Creates a new Cluster based on the specified configuration.
* @param stormConf the storm configuration.
* @return a new a new {@link com.datastax.driver.core.Cluster} instance.
*/
@Override
protected Cluster make(Map<String, Object> stormConf) {
CassandraConf cassandraConf = new CassandraConf(stormConf);
Cluster.Builder cluster = Cluster.builder().withoutJMXReporting().withoutMetrics().addContactPoints(cassandraConf.getNodes()).withPort(cassandraConf.getPort()).withRetryPolicy(cassandraConf.getRetryPolicy()).withReconnectionPolicy(new ExponentialReconnectionPolicy(cassandraConf.getReconnectionPolicyBaseMs(), cassandraConf.getReconnectionPolicyMaxMs())).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy()));
final String username = cassandraConf.getUsername();
final String password = cassandraConf.getPassword();
if (StringUtils.isNotEmpty(username) && StringUtils.isNotEmpty(password)) {
cluster.withAuthProvider(new PlainTextAuthProvider(username, password));
}
QueryOptions options = new QueryOptions().setConsistencyLevel(cassandraConf.getConsistencyLevel());
cluster.withQueryOptions(options);
return cluster.build();
}
use of com.datastax.driver.core.policies.TokenAwarePolicy in project ats-framework by Axway.
the class CassandraDbProvider method connect.
/**
* Currently we connect just once and then reuse the connection.
* We do not bother with closing the connection.
*
* It is normal to use one Session per DB. The Session is thread safe.
*/
private void connect() {
if (cluster == null) {
log.info("Connecting to Cassandra server on " + this.dbHost + " at port " + this.dbPort);
// allow fetching as much data as present in the DB
QueryOptions queryOptions = new QueryOptions();
queryOptions.setFetchSize(Integer.MAX_VALUE);
queryOptions.setConsistencyLevel(ConsistencyLevel.ONE);
cluster = Cluster.builder().addContactPoint(this.dbHost).withPort(this.dbPort).withLoadBalancingPolicy(new TokenAwarePolicy(new RoundRobinPolicy())).withReconnectionPolicy(new ExponentialReconnectionPolicy(500, 30000)).withQueryOptions(queryOptions).withCredentials(this.dbUser, this.dbPassword).build();
}
if (session == null) {
log.info("Connecting to Cassandra DB with name " + this.dbName);
session = cluster.connect(dbName);
}
}
Aggregations