Search in sources :

Example 6 with LoadBalancingPolicy

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

Example 7 with LoadBalancingPolicy

use of com.datastax.driver.core.policies.LoadBalancingPolicy in project cassandra by apache.

the class CqlConfigHelper method getCluster.

public static Cluster getCluster(String[] hosts, Configuration conf, int port) {
    Optional<AuthProvider> authProvider = getAuthProvider(conf);
    Optional<SSLOptions> sslOptions = getSSLOptions(conf);
    Optional<Integer> protocolVersion = getProtocolVersion(conf);
    LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(hosts);
    SocketOptions socketOptions = getReadSocketOptions(conf);
    QueryOptions queryOptions = getReadQueryOptions(conf);
    PoolingOptions poolingOptions = getReadPoolingOptions(conf);
    Cluster.Builder builder = Cluster.builder().addContactPoints(hosts).withPort(port).withCompression(ProtocolOptions.Compression.NONE);
    if (authProvider.isPresent())
        builder.withAuthProvider(authProvider.get());
    if (sslOptions.isPresent())
        builder.withSSL(sslOptions.get());
    if (protocolVersion.isPresent()) {
        builder.withProtocolVersion(ProtocolVersion.fromInt(protocolVersion.get()));
    }
    builder.withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withQueryOptions(queryOptions).withPoolingOptions(poolingOptions);
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) AuthProvider(com.datastax.driver.core.AuthProvider) PlainTextAuthProvider(com.datastax.driver.core.PlainTextAuthProvider) JdkSSLOptions(com.datastax.driver.core.JdkSSLOptions) SSLOptions(com.datastax.driver.core.SSLOptions) QueryOptions(com.datastax.driver.core.QueryOptions)

Example 8 with LoadBalancingPolicy

use of com.datastax.driver.core.policies.LoadBalancingPolicy 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);
}
Also used : DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) WhiteListPolicy(com.datastax.driver.core.policies.WhiteListPolicy) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy)

Example 9 with LoadBalancingPolicy

use of com.datastax.driver.core.policies.LoadBalancingPolicy in project YCSB by brianfrankcooper.

the class ScyllaCQLClient method init.

/**
 * Initialize any state for this DB. Called once per DB instance; there is one
 * DB instance per client thread.
 */
@Override
public void init() throws DBException {
    // Keep track of number of calls to init (for later cleanup)
    INIT_COUNT.incrementAndGet();
    // cluster/session instance for all the threads.
    synchronized (INIT_COUNT) {
        // Check if the cluster has already been initialized
        if (cluster != null) {
            return;
        }
        try {
            debug = Boolean.parseBoolean(getProperties().getProperty("debug", "false"));
            trace = Boolean.parseBoolean(getProperties().getProperty(TRACING_PROPERTY, TRACING_PROPERTY_DEFAULT));
            String host = getProperties().getProperty(HOSTS_PROPERTY);
            if (host == null) {
                throw new DBException(String.format("Required property \"%s\" missing for scyllaCQLClient", HOSTS_PROPERTY));
            }
            String[] hosts = host.split(",");
            String port = getProperties().getProperty(PORT_PROPERTY, PORT_PROPERTY_DEFAULT);
            String username = getProperties().getProperty(USERNAME_PROPERTY);
            String password = getProperties().getProperty(PASSWORD_PROPERTY);
            String keyspace = getProperties().getProperty(KEYSPACE_PROPERTY, KEYSPACE_PROPERTY_DEFAULT);
            readConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(READ_CONSISTENCY_LEVEL_PROPERTY, READ_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            writeConsistencyLevel = ConsistencyLevel.valueOf(getProperties().getProperty(WRITE_CONSISTENCY_LEVEL_PROPERTY, WRITE_CONSISTENCY_LEVEL_PROPERTY_DEFAULT));
            boolean useSSL = Boolean.parseBoolean(getProperties().getProperty(USE_SSL_CONNECTION, DEFAULT_USE_SSL_CONNECTION));
            Cluster.Builder builder;
            if ((username != null) && !username.isEmpty()) {
                builder = Cluster.builder().withCredentials(username, password).addContactPoints(hosts).withPort(Integer.parseInt(port));
                if (useSSL) {
                    builder = builder.withSSL();
                }
            } else {
                builder = Cluster.builder().withPort(Integer.parseInt(port)).addContactPoints(hosts);
            }
            final String localDC = getProperties().getProperty(TOKEN_AWARE_LOCAL_DC);
            if (localDC != null && !localDC.isEmpty()) {
                final LoadBalancingPolicy local = DCAwareRoundRobinPolicy.builder().withLocalDc(localDC).build();
                final TokenAwarePolicy tokenAware = new TokenAwarePolicy(local);
                builder = builder.withLoadBalancingPolicy(tokenAware);
                LOGGER.info("Using local datacenter with token awareness: {}\n", localDC);
                // If was not overridden explicitly, set LOCAL_QUORUM
                if (getProperties().getProperty(READ_CONSISTENCY_LEVEL_PROPERTY) == null) {
                    readConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
                }
                if (getProperties().getProperty(WRITE_CONSISTENCY_LEVEL_PROPERTY) == null) {
                    writeConsistencyLevel = ConsistencyLevel.LOCAL_QUORUM;
                }
            }
            cluster = builder.build();
            String maxConnections = getProperties().getProperty(MAX_CONNECTIONS_PROPERTY);
            if (maxConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(maxConnections));
            }
            String coreConnections = getProperties().getProperty(CORE_CONNECTIONS_PROPERTY);
            if (coreConnections != null) {
                cluster.getConfiguration().getPoolingOptions().setCoreConnectionsPerHost(HostDistance.LOCAL, Integer.parseInt(coreConnections));
            }
            String connectTimeoutMillis = getProperties().getProperty(CONNECT_TIMEOUT_MILLIS_PROPERTY);
            if (connectTimeoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setConnectTimeoutMillis(Integer.parseInt(connectTimeoutMillis));
            }
            String readTimeoutMillis = getProperties().getProperty(READ_TIMEOUT_MILLIS_PROPERTY);
            if (readTimeoutMillis != null) {
                cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(Integer.parseInt(readTimeoutMillis));
            }
            Metadata metadata = cluster.getMetadata();
            LOGGER.info("Connected to cluster: {}\n", metadata.getClusterName());
            for (Host discoveredHost : metadata.getAllHosts()) {
                LOGGER.info("Datacenter: {}; Host: {}; Rack: {}\n", discoveredHost.getDatacenter(), discoveredHost.getEndPoint().resolve().getAddress(), discoveredHost.getRack());
            }
            session = cluster.connect(keyspace);
            if (Boolean.parseBoolean(getProperties().getProperty(SCYLLA_LWT, Boolean.toString(lwt)))) {
                LOGGER.info("Using LWT\n");
                lwt = true;
                readConsistencyLevel = ConsistencyLevel.SERIAL;
                writeConsistencyLevel = ConsistencyLevel.ANY;
            } else {
                LOGGER.info("Not using LWT\n");
            }
            LOGGER.info("Read consistency: {}, Write consistency: {}\n", readConsistencyLevel.name(), writeConsistencyLevel.name());
        } catch (Exception e) {
            throw new DBException(e);
        }
    }
// synchronized
}
Also used : DBException(site.ycsb.DBException) LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) DBException(site.ycsb.DBException)

Example 10 with LoadBalancingPolicy

use of com.datastax.driver.core.policies.LoadBalancingPolicy in project jhipster-sample-app-cassandra by jhipster.

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();
    }
    builder.addContactPoints(StringUtils.toStringArray(properties.getContactPoints()));
    Cluster cluster = builder.build();
    TupleType tupleType = cluster.getMetadata().newTupleType(DataType.timestamp(), DataType.varchar());
    cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(InstantCodec.instance).register(new ZonedDateTimeCodec(tupleType));
    if (metricRegistry != null) {
        cluster.init();
        metricRegistry.registerAll(cluster.getMetrics().getRegistry());
    }
    return cluster;
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) TupleType(com.datastax.driver.core.TupleType) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) ZonedDateTimeCodec(com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec) Bean(org.springframework.context.annotation.Bean)

Aggregations

LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)11 Cluster (com.datastax.driver.core.Cluster)7 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)4 RetryPolicy (com.datastax.driver.core.policies.RetryPolicy)4 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)4 QueryOptions (com.datastax.driver.core.QueryOptions)3 SocketOptions (com.datastax.driver.core.SocketOptions)3 WhiteListPolicy (com.datastax.driver.core.policies.WhiteListPolicy)3 InetSocketAddress (java.net.InetSocketAddress)3 Bean (org.springframework.context.annotation.Bean)3 PoolingOptions (com.datastax.driver.core.PoolingOptions)2 SSLOptions (com.datastax.driver.core.SSLOptions)2 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)2 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)2 AuthProvider (com.datastax.driver.core.AuthProvider)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)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