use of com.datastax.driver.core.policies.LoadBalancingPolicy in project spring-boot by spring-projects.
the class CassandraAutoConfiguration method cluster.
@Bean
@ConditionalOnMissingBean
public Cluster cluster() {
CassandraProperties properties = this.properties;
Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withPort(properties.getPort());
if (properties.getUsername() != null) {
builder.withCredentials(properties.getUsername(), properties.getPassword());
}
if (properties.getCompression() != null) {
builder.withCompression(properties.getCompression());
}
if (properties.getLoadBalancingPolicy() != null) {
LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
builder.withLoadBalancingPolicy(policy);
}
builder.withQueryOptions(getQueryOptions());
if (properties.getReconnectionPolicy() != null) {
ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
builder.withReconnectionPolicy(policy);
}
if (properties.getRetryPolicy() != null) {
RetryPolicy policy = instantiate(properties.getRetryPolicy());
builder.withRetryPolicy(policy);
}
builder.withSocketOptions(getSocketOptions());
if (properties.isSsl()) {
builder.withSSL();
}
String points = properties.getContactPoints();
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
customize(builder);
return builder.build();
}
use of com.datastax.driver.core.policies.LoadBalancingPolicy in project atlasdb by palantir.
the class CqlKeyValueService method initializeConnectionPool.
@SuppressWarnings("CyclomaticComplexity")
protected void initializeConnectionPool() {
Collection<InetSocketAddress> configuredHosts = config.servers();
Cluster.Builder clusterBuilder = Cluster.builder();
clusterBuilder.addContactPointsWithPorts(configuredHosts);
// for JMX metrics
clusterBuilder.withClusterName("atlas_cassandra_cluster_" + config.getKeyspaceOrThrow());
clusterBuilder.withCompression(Compression.LZ4);
if (config.sslConfiguration().isPresent()) {
SSLContext sslContext = SslSocketFactories.createSslContext(config.sslConfiguration().get());
SSLOptions sslOptions = new SSLOptions(sslContext, SSLOptions.DEFAULT_SSL_CIPHER_SUITES);
clusterBuilder.withSSL(sslOptions);
} else if (config.ssl().isPresent() && config.ssl().get()) {
clusterBuilder.withSSL();
}
PoolingOptions poolingOptions = new PoolingOptions();
poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, config.poolSize());
poolingOptions.setMaxRequestsPerConnection(HostDistance.REMOTE, config.poolSize());
poolingOptions.setPoolTimeoutMillis(config.cqlPoolTimeoutMillis());
clusterBuilder.withPoolingOptions(poolingOptions);
// defaults for queries; can override on per-query basis
QueryOptions queryOptions = new QueryOptions();
queryOptions.setFetchSize(config.fetchBatchCount());
clusterBuilder.withQueryOptions(queryOptions);
// Refuse to talk to nodes twice as (latency-wise) slow as the best one, over a timescale of 100ms,
// and every 10s try to re-evaluate ignored nodes performance by giving them queries again.
// Note we are being purposely datacenter-irreverent here, instead relying on latency alone
// to approximate what DCAwareRR would do;
// this is because DCs for Atlas are always quite latency-close and should be used this way,
// not as if we have some cross-country backup DC.
LoadBalancingPolicy policy = LatencyAwarePolicy.builder(new RoundRobinPolicy()).build();
// If user wants, do not automatically add in new nodes to pool (useful during DC migrations / rebuilds)
if (!config.autoRefreshNodes()) {
policy = new WhiteListPolicy(policy, configuredHosts);
}
// also try and select coordinators who own the data we're talking about to avoid an extra hop,
// but also shuffle which replica we talk to for a load balancing that comes at the expense
// of less effective caching
policy = new TokenAwarePolicy(policy, true);
clusterBuilder.withLoadBalancingPolicy(policy);
Metadata metadata;
try {
cluster = clusterBuilder.build();
// special; this is the first place we connect to
metadata = cluster.getMetadata();
// hosts, this is where people will see failures
} catch (NoHostAvailableException e) {
if (e.getMessage().contains("Unknown compression algorithm")) {
clusterBuilder.withCompression(Compression.NONE);
cluster = clusterBuilder.build();
metadata = cluster.getMetadata();
} else {
throw e;
}
} catch (IllegalStateException e) {
// god dammit datastax what did I do to _you_
if (e.getMessage().contains("requested compression is not available")) {
clusterBuilder.withCompression(Compression.NONE);
cluster = clusterBuilder.build();
metadata = cluster.getMetadata();
} else {
throw e;
}
}
session = cluster.connect();
clusterBuilder.withSocketOptions(new SocketOptions().setReadTimeoutMillis(CassandraConstants.LONG_RUNNING_QUERY_SOCKET_TIMEOUT_MILLIS));
longRunningQueryCluster = clusterBuilder.build();
longRunningQuerySession = longRunningQueryCluster.connect();
cqlStatementCache = new CqlStatementCache(session, longRunningQuerySession);
cqlKeyValueServices = new CqlKeyValueServices();
if (log.isInfoEnabled()) {
StringBuilder hostInfo = new StringBuilder();
for (Host host : metadata.getAllHosts()) {
hostInfo.append(String.format("Datatacenter: %s; Host: %s; Rack: %s%n", host.getDatacenter(), host.getAddress(), host.getRack()));
}
log.info("Initialized cassandra cluster using new API with hosts {}, seen keyspaces {}, cluster name {}", hostInfo.toString(), metadata.getKeyspaces(), metadata.getClusterName());
}
}
use of com.datastax.driver.core.policies.LoadBalancingPolicy in project tutorials by eugenp.
the class CassandraConfiguration method cluster.
@Bean
public Cluster cluster(CassandraProperties properties) {
Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withProtocolVersion(protocolVersion).withPort(getPort(properties));
if (properties.getUsername() != null) {
builder.withCredentials(properties.getUsername(), properties.getPassword());
}
if (properties.getCompression() != null) {
builder.withCompression(properties.getCompression());
}
if (properties.getLoadBalancingPolicy() != null) {
LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
builder.withLoadBalancingPolicy(policy);
}
builder.withQueryOptions(getQueryOptions(properties));
if (properties.getReconnectionPolicy() != null) {
ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
builder.withReconnectionPolicy(policy);
}
if (properties.getRetryPolicy() != null) {
RetryPolicy policy = instantiate(properties.getRetryPolicy());
builder.withRetryPolicy(policy);
}
builder.withSocketOptions(getSocketOptions(properties));
if (properties.isSsl()) {
builder.withSSL();
}
String points = properties.getContactPoints();
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
Cluster cluster = builder.build();
cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(CustomZonedDateTimeCodec.instance);
if (metricRegistry != null) {
cluster.init();
metricRegistry.registerAll(cluster.getMetrics().getRegistry());
}
return cluster;
}
use of com.datastax.driver.core.policies.LoadBalancingPolicy in project java-driver by datastax.
the class HostAssert method isAtDistance.
public HostAssert isAtDistance(HostDistance expected) {
LoadBalancingPolicy loadBalancingPolicy = cluster.manager.loadBalancingPolicy();
assertThat(loadBalancingPolicy.distance(actual)).isEqualTo(expected);
return this;
}
use of com.datastax.driver.core.policies.LoadBalancingPolicy in project eventapis by kloiasoft.
the class CassandraSession method cluster.
/* private Cluster cluster() {
Cluster.Builder builder = Cluster.builder();
Arrays.stream(eventStoreConfig.getContactPoints().split(";")).forEach(s -> {
String[] hostPort = s.split(":");
builder.addContactPointsWithPorts(new InetSocketAddress(hostPort[0], Integer.parseInt(hostPort[1])));
});
builder.withPoolingOptions(eventStoreConfig.getPoolingOptions());
Cluster cluster = builder.build();
cluster.register(QueryLogger.builder().build());
return cluster.init();
}*/
private Cluster cluster() {
EventStoreConfig properties = eventStoreConfig;
Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withPort(properties.getPort());
if (properties.getUsername() != null) {
builder.withCredentials(properties.getUsername(), properties.getPassword());
}
if (properties.getCompression() != null) {
builder.withCompression(properties.getCompression());
}
if (properties.getLoadBalancingPolicy() != null) {
LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy());
builder.withLoadBalancingPolicy(policy);
}
builder.withQueryOptions(getQueryOptions());
if (properties.getReconnectionPolicy() != null) {
ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy());
builder.withReconnectionPolicy(policy);
}
if (properties.getRetryPolicy() != null) {
RetryPolicy policy = instantiate(properties.getRetryPolicy());
builder.withRetryPolicy(policy);
}
builder.withSocketOptions(getSocketOptions());
if (properties.isSsl()) {
builder.withSSL();
}
String points = properties.getContactPoints();
builder.addContactPointsWithPorts(Arrays.stream(StringUtils.split(points, ",")).map(s -> {
String[] split = s.split(":");
String host = split[0];
int port = properties.getPort();
try {
port = Integer.parseInt(split[1]);
} catch (Exception e) {
log.trace(e.getMessage());
}
return new InetSocketAddress(host, port);
}).collect(Collectors.toList()));
return builder.build();
}
Aggregations