Search in sources :

Example 1 with DriverConfigLoader

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

the class CassandraAutoConfigurationIntegrationTests method whenTheContextIsClosedThenTheDriverConfigLoaderIsClosed.

@Test
void whenTheContextIsClosedThenTheDriverConfigLoaderIsClosed() {
    this.contextRunner.withUserConfiguration(DriverConfigLoaderSpyConfiguration.class).run((context) -> {
        assertThat(((BeanDefinitionRegistry) context.getSourceApplicationContext()).getBeanDefinition("cassandraDriverConfigLoader").getDestroyMethodName()).isEmpty();
        // Initialize lazy bean
        context.getBean(CqlSession.class);
        DriverConfigLoader driverConfigLoader = context.getBean(DriverConfigLoader.class);
        context.close();
        then(driverConfigLoader).should().close();
    });
}
Also used : DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) Test(org.junit.jupiter.api.Test)

Example 2 with DriverConfigLoader

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

the class CassandraInterpreter method open.

@Override
public void open() {
    final String[] addresses = getProperty(CASSANDRA_HOSTS, DEFAULT_HOST).trim().split(",");
    final int port = parseInt(getProperty(CASSANDRA_PORT, DEFAULT_PORT));
    Collection<InetSocketAddress> hosts = new ArrayList<>();
    for (String address : addresses) {
        if (!StringUtils.isBlank(address)) {
            LOGGER.debug("Adding contact point: {}", address);
            if (InetAddresses.isInetAddress(address)) {
                hosts.add(new InetSocketAddress(address, port));
            } else {
                hosts.add(InetSocketAddress.createUnresolved(address, port));
            }
        }
    }
    LOGGER.info("Bootstrapping Cassandra Java Driver to connect to {} on port {}", getProperty(CASSANDRA_HOSTS), port);
    DriverConfigLoader loader = createLoader();
    LOGGER.debug("Creating cluster builder");
    CqlSessionBuilder clusterBuilder = CqlSession.builder().withApplicationName("Zeppelin").withApplicationVersion("");
    if (!hosts.isEmpty()) {
        LOGGER.debug("Adding contact points");
        clusterBuilder.addContactPoints(hosts);
    }
    String username = getProperty(CASSANDRA_CREDENTIALS_USERNAME, NONE_VALUE).trim();
    String password = getProperty(CASSANDRA_CREDENTIALS_PASSWORD, NONE_VALUE).trim();
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password) && !NONE_VALUE.equalsIgnoreCase(username) && !NONE_VALUE.equalsIgnoreCase(password)) {
        LOGGER.debug("Adding credentials. Username = {}", username);
        clusterBuilder.withAuthCredentials(username, password);
    }
    String keyspace = getProperty(CASSANDRA_KEYSPACE_NAME, DEFAULT_KEYSPACE);
    if (StringUtils.isNotBlank(keyspace) && !DEFAULT_KEYSPACE.equalsIgnoreCase(keyspace)) {
        LOGGER.debug("Set default keyspace");
        clusterBuilder.withKeyspace(keyspace);
    }
    final String runWithSSL = getProperty(CASSANDRA_WITH_SSL, "false");
    if ("true".equalsIgnoreCase(runWithSSL)) {
        LOGGER.debug("Using SSL");
        try {
            final SSLContext sslContext;
            {
                final KeyStore trustStore = KeyStore.getInstance("JKS");
                final InputStream stream = Files.newInputStream(Paths.get(getProperty(CASSANDRA_TRUSTSTORE_PATH)));
                trustStore.load(stream, getProperty(CASSANDRA_TRUSTSTORE_PASSWORD).toCharArray());
                final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
                trustManagerFactory.init(trustStore);
                sslContext = SSLContext.getInstance("TLS");
                sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
            }
            clusterBuilder = clusterBuilder.withSslContext(sslContext);
        } catch (Exception e) {
            LOGGER.error("Exception initializing SSL {}", e.toString());
        }
    } else {
        LOGGER.debug("Not using SSL");
    }
    LOGGER.debug("Creating CqlSession");
    session = clusterBuilder.withConfigLoader(loader).build();
    LOGGER.debug("Session configuration");
    for (Map.Entry<String, Object> entry : session.getContext().getConfig().getDefaultProfile().entrySet()) {
        LOGGER.debug("{} = {}", entry.getKey(), entry.getValue().toString());
    }
    LOGGER.debug("Creating helper");
    helper = new InterpreterLogic(session, properties);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DriverConfigLoader

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

the class CQLSessionBuilder method build.

/**
 * @param configuration configuration to use
 * @param baseHostnames will be used only if base configuration is enabled
 * @param baseDefaultPort will be used only if base configuration is enabled
 * @param baseConfigurationLoaderBuilder will be used only if base configuration is enabled
 * @return Returns constructed CqlSession
 */
public CqlSession build(Configuration configuration, String[] baseHostnames, int baseDefaultPort, Duration baseConnectionTimeoutMS, CQLProgrammaticConfigurationLoaderBuilder baseConfigurationLoaderBuilder) throws PermanentBackendException {
    final List<String> contactPoints = new ArrayList<>(baseHostnames.length);
    for (String contactPoint : baseHostnames) {
        if (!contactPoint.contains(":")) {
            contactPoint += ":" + baseDefaultPort;
        }
        contactPoints.add(contactPoint);
    }
    final CqlSessionBuilder builder = CqlSession.builder();
    Stack<DriverConfigLoader> driverConfigLoadersToUse = new Stack<>();
    if (configuration.get(CQLConfigOptions.BASE_PROGRAMMATIC_CONFIGURATION_ENABLED)) {
        driverConfigLoadersToUse.push(baseConfigurationLoaderBuilder.build(configuration, contactPoints, baseConnectionTimeoutMS));
    }
    if (configuration.has(CQLConfigOptions.URL_CONFIGURATION)) {
        String stringUrlRepresentation = configuration.get(CQLConfigOptions.URL_CONFIGURATION);
        URL url;
        try {
            url = new URL(stringUrlRepresentation);
        } catch (MalformedURLException e) {
            throw new PermanentBackendException("Malformed URL: " + stringUrlRepresentation, e);
        }
        driverConfigLoadersToUse.push(DriverConfigLoader.fromUrl(url));
    }
    if (configuration.has(CQLConfigOptions.STRING_CONFIGURATION)) {
        String stringConfiguration = configuration.get(CQLConfigOptions.STRING_CONFIGURATION);
        driverConfigLoadersToUse.push(DriverConfigLoader.fromString(stringConfiguration));
    }
    if (configuration.has(CQLConfigOptions.RESOURCE_CONFIGURATION)) {
        String resourceConfigurationPath = configuration.get(CQLConfigOptions.RESOURCE_CONFIGURATION);
        driverConfigLoadersToUse.push(DriverConfigLoader.fromClasspath(resourceConfigurationPath));
    }
    if (configuration.has(CQLConfigOptions.FILE_CONFIGURATION)) {
        String fileConfigurationPath = configuration.get(CQLConfigOptions.FILE_CONFIGURATION);
        driverConfigLoadersToUse.push(DriverConfigLoader.fromFile(new File(fileConfigurationPath)));
    }
    if (!driverConfigLoadersToUse.empty()) {
        DriverConfigLoader composedDriverConfigLoader = driverConfigLoadersToUse.pop();
        while (!driverConfigLoadersToUse.empty()) {
            composedDriverConfigLoader = DriverConfigLoader.compose(composedDriverConfigLoader, driverConfigLoadersToUse.pop());
        }
        builder.withConfigLoader(composedDriverConfigLoader);
    }
    return builder.build();
}
Also used : MalformedURLException(java.net.MalformedURLException) CqlSessionBuilder(com.datastax.oss.driver.api.core.CqlSessionBuilder) PermanentBackendException(org.janusgraph.diskstorage.PermanentBackendException) ArrayList(java.util.ArrayList) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) File(java.io.File) URL(java.net.URL) Stack(java.util.Stack)

Example 4 with DriverConfigLoader

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

the class CQLConfigTest method defaultProgrammaticConfigurationShouldUseJanusGraphDefaultRequestTimeout.

@Test
public void defaultProgrammaticConfigurationShouldUseJanusGraphDefaultRequestTimeout() {
    // Create a CQLProgrammaticConfigurationLoaderBuilder from default values
    Configuration config = new ModifiableConfiguration(ROOT_NS, getConfiguration(), BasicConfiguration.Restriction.NONE);
    CQLProgrammaticConfigurationLoaderBuilder builder = new CQLProgrammaticConfigurationLoaderBuilder();
    // DriverConfigLoader should use CQLConfigOptions.REQUEST_TIMEOUT default value and not DataStax default value of 2s
    DriverConfigLoader loader = builder.build(config, Collections.emptyList(), Duration.of(1, ChronoUnit.SECONDS));
    Duration requestTimeout = loader.getInitialConfig().getDefaultProfile().getDuration(DefaultDriverOption.REQUEST_TIMEOUT);
    assertEquals(Duration.of(REQUEST_TIMEOUT.getDefaultValue(), ChronoUnit.MILLIS), requestTimeout);
}
Also used : WriteConfiguration(org.janusgraph.diskstorage.configuration.WriteConfiguration) BasicConfiguration(org.janusgraph.diskstorage.configuration.BasicConfiguration) Configuration(org.janusgraph.diskstorage.configuration.Configuration) GraphDatabaseConfiguration(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) CQLProgrammaticConfigurationLoaderBuilder(org.janusgraph.diskstorage.cql.builder.CQLProgrammaticConfigurationLoaderBuilder) ModifiableConfiguration(org.janusgraph.diskstorage.configuration.ModifiableConfiguration) DriverConfigLoader(com.datastax.oss.driver.api.core.config.DriverConfigLoader) Duration(java.time.Duration) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with DriverConfigLoader

use of com.datastax.oss.driver.api.core.config.DriverConfigLoader 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)

Aggregations

DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)5 CqlSessionBuilder (com.datastax.oss.driver.api.core.CqlSessionBuilder)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Test (org.junit.jupiter.api.Test)2 DefaultDriverOption (com.datastax.oss.driver.api.core.config.DefaultDriverOption)1 DriverOption (com.datastax.oss.driver.api.core.config.DriverOption)1 ProgrammaticDriverConfigLoaderBuilder (com.datastax.oss.driver.api.core.config.ProgrammaticDriverConfigLoaderBuilder)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 KeyStore (java.security.KeyStore)1 Duration (java.time.Duration)1 Stack (java.util.Stack)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)1