Search in sources :

Example 1 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project spring-boot by spring-projects.

the class CassandraAutoConfiguration method getSocketOptions.

private SocketOptions getSocketOptions() {
    SocketOptions options = new SocketOptions();
    options.setConnectTimeoutMillis(this.properties.getConnectTimeoutMillis());
    options.setReadTimeoutMillis(this.properties.getReadTimeoutMillis());
    return options;
}
Also used : SocketOptions(com.datastax.driver.core.SocketOptions)

Example 2 with SocketOptions

use of com.datastax.driver.core.SocketOptions 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)

Example 3 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project tutorials by eugenp.

the class CassandraConfiguration method getSocketOptions.

private SocketOptions getSocketOptions(CassandraProperties properties) {
    SocketOptions options = new SocketOptions();
    options.setConnectTimeoutMillis(properties.getConnectTimeoutMillis());
    options.setReadTimeoutMillis(properties.getReadTimeoutMillis());
    return options;
}
Also used : SocketOptions(com.datastax.driver.core.SocketOptions)

Example 4 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project eventapis by kloiasoft.

the class CassandraSession method getSocketOptions.

private SocketOptions getSocketOptions() {
    SocketOptions options = new SocketOptions();
    options.setConnectTimeoutMillis(this.eventStoreConfig.getConnectTimeoutMillis());
    options.setReadTimeoutMillis(this.eventStoreConfig.getReadTimeoutMillis());
    return options;
}
Also used : SocketOptions(com.datastax.driver.core.SocketOptions)

Example 5 with SocketOptions

use of com.datastax.driver.core.SocketOptions 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(config.getProtocolVersion());
    List<String> contactPoints = requireNonNull(config.getContactPoints(), "contactPoints is null");
    checkArgument(!contactPoints.isEmpty(), "empty contactPoints");
    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());
    }
    if (config.isTlsEnabled()) {
        SslContextProvider sslContextProvider = new SslContextProvider(config.getKeystorePath(), config.getKeystorePassword(), config.getTruststorePath(), config.getTruststorePassword());
        sslContextProvider.buildSslContext().ifPresent(context -> clusterBuilder.withSSL(JdkSSLOptions.builder().withSSLContext(context).build()));
    }
    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, new ReopeningCluster(() -> {
        contactPoints.forEach(clusterBuilder::addContactPoint);
        return clusterBuilder.build();
    }), config.getNoHostAvailableRetryTimeout());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) SocketOptions(com.datastax.driver.core.SocketOptions) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) QueryOptions(com.datastax.driver.core.QueryOptions) ConstantSpeculativeExecutionPolicy(com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) SslContextProvider(com.facebook.presto.cassandra.util.SslContextProvider) ExponentialReconnectionPolicy(com.datastax.driver.core.policies.ExponentialReconnectionPolicy) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

SocketOptions (com.datastax.driver.core.SocketOptions)11 Cluster (com.datastax.driver.core.Cluster)4 QueryOptions (com.datastax.driver.core.QueryOptions)4 PoolingOptions (com.datastax.driver.core.PoolingOptions)3 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)3 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)3 SSLOptions (com.datastax.driver.core.SSLOptions)2 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)2 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)2 WhiteListPolicy (com.datastax.driver.core.policies.WhiteListPolicy)2 InetSocketAddress (java.net.InetSocketAddress)2 AuthProvider (com.datastax.driver.core.AuthProvider)1 Host (com.datastax.driver.core.Host)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 Metadata (com.datastax.driver.core.Metadata)1 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1 ConstantSpeculativeExecutionPolicy (com.datastax.driver.core.policies.ConstantSpeculativeExecutionPolicy)1 ExponentialReconnectionPolicy (com.datastax.driver.core.policies.ExponentialReconnectionPolicy)1 LoggingRetryPolicy (com.datastax.driver.core.policies.LoggingRetryPolicy)1