Search in sources :

Example 1 with ProgrammaticDriverConfigLoaderBuilder

use of com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder in project spring-boot by spring-projects.

the class CassandraAutoConfiguration method cassandraDriverConfigLoader.

@Bean(destroyMethod = "")
@ConditionalOnMissingBean
public DriverConfigLoader cassandraDriverConfigLoader(CassandraProperties properties, ObjectProvider<DriverConfigLoaderBuilderCustomizer> builderCustomizers) {
    ProgrammaticDriverConfigLoaderBuilder builder = new DefaultProgrammaticDriverConfigLoaderBuilder(() -> cassandraConfiguration(properties), DefaultDriverConfigLoader.DEFAULT_ROOT_PATH);
    builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
    return builder.build();
}
Also used : ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder) DefaultProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder) DefaultProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with ProgrammaticDriverConfigLoaderBuilder

use of com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder in project janusgraph by JanusGraph.

the class CQLProgrammaticConfigurationLoaderBuilder method build.

public DriverConfigLoader build(Configuration configuration, List<String> contactPoints, Duration connectionTimeoutMS) {
    ProgrammaticDriverConfigLoaderBuilder configLoaderBuilder = DriverConfigLoader.programmaticBuilder();
    configLoaderBuilder.withStringList(DefaultDriverOption.CONTACT_POINTS, contactPoints);
    configLoaderBuilder.withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, configuration.get(LOCAL_DATACENTER));
    configLoaderBuilder.withString(DefaultDriverOption.SESSION_NAME, configuration.get(SESSION_NAME));
    configLoaderBuilder.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofMillis(configuration.get(REQUEST_TIMEOUT)));
    configLoaderBuilder.withDuration(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT, connectionTimeoutMS);
    if (configuration.get(PROTOCOL_VERSION) != 0) {
        configLoaderBuilder.withInt(DefaultDriverOption.PROTOCOL_VERSION, configuration.get(PROTOCOL_VERSION));
    }
    if (configuration.has(AUTH_USERNAME) && configuration.has(AUTH_PASSWORD)) {
        configLoaderBuilder.withClass(DefaultDriverOption.AUTH_PROVIDER_CLASS, PlainTextAuthProvider.class).withString(DefaultDriverOption.AUTH_PROVIDER_USER_NAME, configuration.get(AUTH_USERNAME)).withString(DefaultDriverOption.AUTH_PROVIDER_PASSWORD, configuration.get(AUTH_PASSWORD));
    }
    if (configuration.get(SSL_ENABLED)) {
        configLoaderBuilder.withClass(DefaultDriverOption.SSL_ENGINE_FACTORY_CLASS, DefaultSslEngineFactory.class).withString(DefaultDriverOption.SSL_TRUSTSTORE_PATH, configuration.get(SSL_TRUSTSTORE_LOCATION)).withString(DefaultDriverOption.SSL_TRUSTSTORE_PASSWORD, configuration.get(SSL_TRUSTSTORE_PASSWORD)).withBoolean(DefaultDriverOption.SSL_HOSTNAME_VALIDATION, configuration.get(SSL_HOSTNAME_VALIDATION));
        if (configuration.get(SSL_CLIENT_AUTHENTICATION_ENABLED)) {
            configLoaderBuilder.withString(DefaultDriverOption.SSL_KEYSTORE_PATH, configuration.get(SSL_KEYSTORE_LOCATION)).withString(DefaultDriverOption.SSL_KEYSTORE_PASSWORD, configuration.get(SSL_KEYSTORE_KEY_PASSWORD));
        }
    }
    configLoaderBuilder.withInt(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, configuration.get(LOCAL_MAX_CONNECTIONS_PER_HOST));
    configLoaderBuilder.withInt(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE, configuration.get(REMOTE_MAX_CONNECTIONS_PER_HOST));
    configLoaderBuilder.withInt(DefaultDriverOption.CONNECTION_MAX_REQUESTS, configuration.get(MAX_REQUESTS_PER_CONNECTION));
    if (configuration.has(HEARTBEAT_INTERVAL)) {
        configLoaderBuilder.withDuration(DefaultDriverOption.HEARTBEAT_INTERVAL, Duration.ofMillis(configuration.get(HEARTBEAT_INTERVAL)));
    }
    if (configuration.has(HEARTBEAT_TIMEOUT)) {
        configLoaderBuilder.withDuration(DefaultDriverOption.HEARTBEAT_TIMEOUT, Duration.ofMillis(configuration.get(HEARTBEAT_TIMEOUT)));
    }
    if (configuration.has(METADATA_SCHEMA_ENABLED)) {
        configLoaderBuilder.withBoolean(DefaultDriverOption.METADATA_SCHEMA_ENABLED, configuration.get(METADATA_SCHEMA_ENABLED));
    }
    if (configuration.has(METADATA_TOKEN_MAP_ENABLED)) {
        configLoaderBuilder.withBoolean(DefaultDriverOption.METADATA_TOKEN_MAP_ENABLED, configuration.get(METADATA_TOKEN_MAP_ENABLED));
    }
    // Keep to 0 for the time being: https://groups.google.com/a/lists.datastax.com/forum/#!topic/java-driver-user/Bc0gQuOVVL0
    // Ideally we want to batch all tables initialisations to happen together when opening a new keyspace
    configLoaderBuilder.withInt(DefaultDriverOption.METADATA_SCHEMA_WINDOW, 0);
    configureCqlNetty(configuration, configLoaderBuilder);
    if (configuration.get(BASIC_METRICS)) {
        configureMetrics(configuration, configLoaderBuilder);
    }
    configureRequestTracker(configuration, configLoaderBuilder);
    if (configuration.has(SESSION_LEAK_THRESHOLD)) {
        configLoaderBuilder.withInt(DefaultDriverOption.SESSION_LEAK_THRESHOLD, configuration.get(SESSION_LEAK_THRESHOLD));
    }
    return configLoaderBuilder.build();
}
Also used : DefaultSslEngineFactory(com.datastax.oss.driver.internal.core.ssl.DefaultSslEngineFactory) ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)

Example 3 with ProgrammaticDriverConfigLoaderBuilder

use of com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder in project zeppelin by apache.

the class CassandraInterpreter method createLoader.

private DriverConfigLoader createLoader() {
    LOGGER.debug("Creating programmatic config loader");
    // start generation of the config
    ProgrammaticDriverConfigLoaderBuilder configBuilder = DriverConfigLoader.programmaticBuilder();
    Map<DriverOption, String> allOptions = new HashMap<>();
    // set options from main configuration
    String ts = getProperty(CASSANDRA_SOCKET_CONNECTION_TIMEOUT_MILLIS, CassandraInterpreter.DEFAULT_CONNECTION_TIMEOUT) + MILLISECONDS_STR;
    allOptions.put(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, ts);
    allOptions.put(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, ts);
    allOptions.put(DefaultDriverOption.REQUEST_TIMEOUT, getProperty(CASSANDRA_SOCKET_READ_TIMEOUT_MILLIS, CassandraInterpreter.DEFAULT_READ_TIMEOUT) + MILLISECONDS_STR);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_TCP_NO_DELAY, CassandraInterpreter.DEFAULT_TCP_NO_DELAY), DefaultDriverOption.SOCKET_TCP_NODELAY);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_KEEP_ALIVE), DefaultDriverOption.SOCKET_KEEP_ALIVE);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_RECEIVED_BUFFER_SIZE_BYTES), DefaultDriverOption.SOCKET_RECEIVE_BUFFER_SIZE);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_SEND_BUFFER_SIZE_BYTES), DefaultDriverOption.SOCKET_SEND_BUFFER_SIZE);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_REUSE_ADDRESS), DefaultDriverOption.SOCKET_REUSE_ADDRESS);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_SOCKET_SO_LINGER), DefaultDriverOption.SOCKET_LINGER_INTERVAL);
    addIfNotBlank(allOptions, getProperty(CASSANDRA_QUERY_DEFAULT_IDEMPOTENCE), DefaultDriverOption.REQUEST_DEFAULT_IDEMPOTENCE);
    allOptions.put(DefaultDriverOption.REQUEST_CONSISTENCY, getProperty(CASSANDRA_QUERY_DEFAULT_CONSISTENCY, CassandraInterpreter.DEFAULT_CONSISTENCY));
    allOptions.put(DefaultDriverOption.REQUEST_SERIAL_CONSISTENCY, getProperty(CASSANDRA_QUERY_DEFAULT_SERIAL_CONSISTENCY, CassandraInterpreter.DEFAULT_SERIAL_CONSISTENCY));
    allOptions.put(DefaultDriverOption.REQUEST_PAGE_SIZE, getProperty(CASSANDRA_QUERY_DEFAULT_FETCH_SIZE, CassandraInterpreter.DEFAULT_FETCH_SIZE));
    ts = getProperty(CASSANDRA_PROTOCOL_VERSION, DEFAULT_PROTOCOL_VERSION);
    if (!DEFAULT_VALUE.equalsIgnoreCase(ts)) {
        // for compatibility with previous configurations
        if (ts.equals("4") || ts.equals("3")) {
            ts = "V" + ts;
        }
        allOptions.put(DefaultDriverOption.PROTOCOL_VERSION, ts);
    }
    addIfNotBlank(allOptions, getProperty(CASSANDRA_COMPRESSION_PROTOCOL, CassandraInterpreter.DEFAULT_COMPRESSION).toLowerCase(), DefaultDriverOption.PROTOCOL_COMPRESSION);
    addIfNotBlankOrDefault(allOptions, getProperty(CASSANDRA_RETRY_POLICY, DEFAULT_POLICY), DefaultDriverOption.RETRY_POLICY_CLASS);
    addIfNotBlankOrDefault(allOptions, getProperty(CASSANDRA_RECONNECTION_POLICY, DEFAULT_POLICY), DefaultDriverOption.RECONNECTION_POLICY_CLASS);
    addIfNotBlankOrDefault(allOptions, getProperty(CASSANDRA_SPECULATIVE_EXECUTION_POLICY, DEFAULT_POLICY), DefaultDriverOption.SPECULATIVE_EXECUTION_POLICY_CLASS);
    allOptions.put(DefaultDriverOption.CONNECTION_POOL_LOCAL_SIZE, getProperty(CASSANDRA_POOLING_CONNECTION_PER_HOST_LOCAL, DEFAULT_CONNECTIONS_PER_HOST));
    allOptions.put(DefaultDriverOption.CONNECTION_POOL_REMOTE_SIZE, getProperty(CASSANDRA_POOLING_CONNECTION_PER_HOST_REMOTE, DEFAULT_CONNECTIONS_PER_HOST));
    allOptions.put(DefaultDriverOption.CONNECTION_MAX_REQUESTS, getProperty(CASSANDRA_POOLING_MAX_REQUESTS_PER_CONNECTION, DEFAULT_MAX_REQUEST_PER_CONNECTION));
    allOptions.put(DefaultDriverOption.HEARTBEAT_INTERVAL, getProperty(CASSANDRA_POOLING_HEARTBEAT_INTERVAL_SECONDS, DEFAULT_HEARTBEAT_INTERVAL) + SECONDS_STR);
    ts = getProperty(CASSANDRA_POOLING_POOL_TIMEOUT_MILLIS, DEFAULT_POOL_TIMEOUT) + MILLISECONDS_STR;
    allOptions.put(DefaultDriverOption.HEARTBEAT_TIMEOUT, ts);
    allOptions.put(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, ts);
    allOptions.put(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS, "DcInferringLoadBalancingPolicy");
    allOptions.put(DefaultDriverOption.RESOLVE_CONTACT_POINTS, "false");
    allOptions.put(DefaultDriverOption.CONTROL_CONNECTION_AGREEMENT_TIMEOUT, getProperty(CASSANDRA_MAX_SCHEMA_AGREEMENT_WAIT_SECONDS, DEFAULT_MAX_SCHEMA_AGREEMENT_WAIT_SECONDS) + SECONDS_STR);
    // extract additional options that may override values set by main configuration
    for (String pname : properties.stringPropertyNames()) {
        if (pname.startsWith(DATASTAX_JAVA_DRIVER_PREFIX)) {
            String pvalue = properties.getProperty(pname);
            LOGGER.info("Custom config values: {} = {}", pname, pvalue);
            String shortName = pname.substring(DATASTAX_JAVA_DRIVER_PREFIX.length());
            if (optionMap.containsKey(shortName)) {
                allOptions.put(optionMap.get(shortName), pvalue);
            } else {
                LOGGER.warn("Incorrect option name: {}", pname);
            }
        }
    }
    for (Map.Entry<DriverOption, String> entry : allOptions.entrySet()) {
        configBuilder.withString(entry.getKey(), entry.getValue());
    }
    DriverConfigLoader loader = configBuilder.endProfile().build();
    LOGGER.debug("Config loader is created");
    return loader;
}
Also used : DefaultDriverOption(com.datastax.oss.driver.api.core.config.DefaultDriverOption) DriverOption(com.datastax.oss.driver.api.core.config.DriverOption) HashMap(java.util.HashMap) ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with ProgrammaticDriverConfigLoaderBuilder

use of com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder in project zipkin by openzipkin.

the class SessionBuilder method buildSession.

/**
 * Returns a connected session. Closes the cluster if any exception occurred.
 */
public static CqlSession buildSession(String contactPoints, String localDc, Map<DriverOption, Integer> poolingOptions, @Nullable AuthProvider authProvider, boolean useSsl) {
    // Some options aren't supported by builder methods. In these cases, we use driver config
    // See https://groups.google.com/a/lists.datastax.com/forum/#!topic/java-driver-user/Z8HrCDX47Q0
    ProgrammaticDriverConfigLoaderBuilder config = // server, where Thread.currentThread().getContextClassLoader() returns null
    DriverConfigLoader.programmaticBuilder(SessionBuilder.class.getClassLoader());
    // Ported from java-driver v3 PoolingOptions.setPoolTimeoutMillis as request timeout includes that
    config.withDuration(REQUEST_TIMEOUT, Duration.ofMinutes(1));
    CqlSessionBuilder builder = CqlSession.builder();
    builder.addContactPoints(parseContactPoints(contactPoints));
    if (authProvider != null)
        builder.withAuthProvider(authProvider);
    // In java-driver v3, we used LatencyAwarePolicy(DCAwareRoundRobinPolicy|RoundRobinPolicy)
    // where DCAwareRoundRobinPolicy was used if localDc != null
    // 
    // In java-driver v4, the default policy is token-aware and localDc is required. Hence, we
    // use the default load balancing policy
    // * https://github.com/datastax/java-driver/blob/master/manual/core/load_balancing/README.md
    builder.withLocalDatacenter(localDc);
    config = config.withString(REQUEST_CONSISTENCY, "LOCAL_ONE");
    // Pooling options changed dramatically from v3->v4. This is a close match.
    poolingOptions.forEach(config::withInt);
    // All Zipkin CQL writes are idempotent
    config = config.withBoolean(REQUEST_DEFAULT_IDEMPOTENCE, true);
    if (useSsl)
        config = config.withClass(SSL_ENGINE_FACTORY_CLASS, DefaultSslEngineFactory.class);
    // Log categories can enable query logging
    Logger requestLogger = LoggerFactory.getLogger(RequestLogger.class);
    if (requestLogger.isDebugEnabled()) {
        config = config.withClass(REQUEST_TRACKER_CLASS, RequestLogger.class);
        config = config.withBoolean(REQUEST_LOGGER_SUCCESS_ENABLED, true);
        // Only show bodies when TRACE is enabled
        config = config.withBoolean(REQUEST_LOGGER_VALUES, requestLogger.isTraceEnabled());
    }
    // Don't warn: ensureSchema creates the keyspace. Hence, we need to "use" it later.
    config = config.withBoolean(REQUEST_WARN_IF_SET_KEYSPACE, false);
    return builder.withConfigLoader(config.build()).build();
}
Also used : CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) RequestLogger(com.datastax.oss.driver.internal.core.tracker.RequestLogger) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) ProgrammaticDriverConfigLoaderBuilder(com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder) RequestLogger(com.datastax.oss.driver.internal.core.tracker.RequestLogger) Logger(org.slf4j.Logger)

Aggregations

ProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)4 CqlSessionBuilder (com.datastax.oss.driver.api.core.CqlSessionBuilder)1 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)1 DriverOption (com.datastax.oss.driver.api.core.config.DriverOption)1 DefaultProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.internal.core.config.typesafe.DefaultProgrammaticDriverConfigLoaderBuilder)1 DefaultSslEngineFactory (com.datastax.oss.driver.internal.core.ssl.DefaultSslEngineFactory)1 RequestLogger (com.datastax.oss.driver.internal.core.tracker.RequestLogger)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Logger (org.slf4j.Logger)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 Bean (org.springframework.context.annotation.Bean)1