Search in sources :

Example 1 with PoolingOptions

use of com.datastax.driver.core.PoolingOptions in project zipkin by openzipkin.

the class SessionFactoryTest method maxConnections_defaultsTo8.

@Test
public void maxConnections_defaultsTo8() {
    PoolingOptions poolingOptions = buildCluster(Cassandra3Storage.builder().build()).getConfiguration().getPoolingOptions();
    assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(8);
}
Also used : PoolingOptions(com.datastax.driver.core.PoolingOptions) Test(org.junit.Test)

Example 2 with PoolingOptions

use of com.datastax.driver.core.PoolingOptions in project zipkin by openzipkin.

the class SessionFactoryTest method maxConnections_setsMaxConnectionsPerDatacenterLocalHost.

@Test
public void maxConnections_setsMaxConnectionsPerDatacenterLocalHost() {
    PoolingOptions poolingOptions = buildCluster(Cassandra3Storage.builder().maxConnections(16).build()).getConfiguration().getPoolingOptions();
    assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(16);
}
Also used : PoolingOptions(com.datastax.driver.core.PoolingOptions) Test(org.junit.Test)

Example 3 with PoolingOptions

use of com.datastax.driver.core.PoolingOptions 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();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LatencyAwarePolicy(com.datastax.driver.core.policies.LatencyAwarePolicy) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)

Example 4 with PoolingOptions

use of com.datastax.driver.core.PoolingOptions in project zipkin by openzipkin.

the class SessionFactoryTest method maxConnections_defaultsTo8.

@Test
public void maxConnections_defaultsTo8() {
    PoolingOptions poolingOptions = buildCluster(CassandraStorage.builder().build()).getConfiguration().getPoolingOptions();
    assertThat(poolingOptions.getMaxConnectionsPerHost(HostDistance.LOCAL)).isEqualTo(8);
}
Also used : PoolingOptions(com.datastax.driver.core.PoolingOptions) Test(org.junit.Test)

Example 5 with PoolingOptions

use of com.datastax.driver.core.PoolingOptions 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());
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketOptions(com.datastax.driver.core.SocketOptions) Metadata(com.datastax.driver.core.Metadata) Cluster(com.datastax.driver.core.Cluster) SSLOptions(com.datastax.driver.core.SSLOptions) Host(com.datastax.driver.core.Host) SSLContext(javax.net.ssl.SSLContext) QueryOptions(com.datastax.driver.core.QueryOptions) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) NoHostAvailableException(com.datastax.driver.core.exceptions.NoHostAvailableException) PoolingOptions(com.datastax.driver.core.PoolingOptions) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy)

Aggregations

PoolingOptions (com.datastax.driver.core.PoolingOptions)14 Cluster (com.datastax.driver.core.Cluster)7 QueryOptions (com.datastax.driver.core.QueryOptions)5 SocketOptions (com.datastax.driver.core.SocketOptions)4 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)4 Test (org.junit.Test)4 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)3 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)3 InetSocketAddress (java.net.InetSocketAddress)3 HostDistance (com.datastax.driver.core.HostDistance)2 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)2 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)2 SSLOptions (com.datastax.driver.core.SSLOptions)2 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)2 AuthProvider (com.datastax.driver.core.AuthProvider)1 BatchStatement (com.datastax.driver.core.BatchStatement)1 Type (com.datastax.driver.core.BatchStatement.Type)1 Builder (com.datastax.driver.core.Cluster.Builder)1 Configuration (com.datastax.driver.core.Configuration)1 Host (com.datastax.driver.core.Host)1