Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce CassandraDriverOptionsConfigurer and allow specifying a configuration file [DATACASS-798] #965

Closed
spring-projects-issues opened this issue Sep 1, 2020 · 4 comments
Assignees
Labels
in: core type: enhancement

Comments

@spring-projects-issues
Copy link

spring-projects-issues commented Sep 1, 2020

Abhijit Sarkar opened DATACASS-798 and commented

It seems almost every Cassandra @Configuration class declares its own private static class CassandraDriverOptions; AbstractSessionConfiguration does, CassandraAutoConfiguration does too. Apart from duplication of code, hiding the driver options from the users means configurations that are not exposed through CassandraProperties can't be easily done.

It would make a lot of sense to make the CassandraDriverOptions public, create a CassandraDriverOptionsConfigurer type, and call user provided implementations of the latter while building the options.

Currently I do something like this, which is a lot of unnecessary code:

override fun cassandraSession(): CqlSessionFactoryBean {
    return super.cassandraSession().apply {
        setSessionBuilderConfigurer {
            if (cassandraProperties.username?.isNotEmpty() == true) {
                it.withAuthProvider(
                    ProgrammaticPlainTextAuthProvider(
                        cassandraProperties.username, cassandraProperties.password
                    )
                )
            }
            it.withConfigLoader(cassandraDriverConfigLoader())
        }
    }
}

@Bean
fun cassandraDriverConfigLoader(): DriverConfigLoader {
    val builder = DefaultProgrammaticDriverConfigLoaderBuilder({
        ConfigFactory.invalidateCaches()
        ConfigFactory.defaultOverrides()
            .withFallback(driverConfig())
            .withFallback(ConfigFactory.defaultReference())
            .resolve()
    }, DefaultDriverConfigLoader.DEFAULT_ROOT_PATH)

    return builder.build()
}
@ConditionalOnMissingBean
@Bean
fun driverConfig(): Config {
    val options = CassandraDriverOptions()
    sessionName
        ?.takeIf { StringUtils.hasText(it) }
        ?.also { options.add(DefaultDriverOption.SESSION_NAME, it) }
    compressionType
        ?.also { options.add(DefaultDriverOption.PROTOCOL_COMPRESSION, it) }
    myCassandraProperties.ssl?.keyStore
        ?.also { options.add(DefaultDriverOption.SSL_KEYSTORE_PATH, it) }
    myCassandraProperties.ssl?.keyStorePassword
        ?.also { options.add(DefaultDriverOption.SSL_KEYSTORE_PASSWORD, it) }
    myCassandraProperties.ssl?.trustStore
        ?.also { options.add(DefaultDriverOption.SSL_TRUSTSTORE_PATH, it) }
    myCassandraProperties.ssl?.trustStorePassword
        ?.also { options.add(DefaultDriverOption.SSL_TRUSTSTORE_PASSWORD, it) }
    myCassandraProperties.protocolVersion
        .takeIf { it > 0 }
        ?.also { pv ->
            options.add(
                DefaultDriverOption.PROTOCOL_VERSION,
                DefaultProtocolVersion.values().first { it.code == pv })
        }
    // Related to spring.data.cassandra.local-datacenter
    myCassandraProperties.policies?.loadBalancing
        ?.also { options.add(DefaultDriverOption.LOAD_BALANCING_POLICY_CLASS, it) }
    myCassandraProperties.policies?.retry
        ?.also { options.add(DefaultDriverOption.RETRY_POLICY_CLASS, it) }

    return options.build()
}

Affects: 3.0.3 (Neumann SR3)

Backported to: 3.1.2 (2020.0.2)

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Sep 1, 2020

Mark Paluch commented

CassandraDriverOptions is an internal utility that is used as holder for driver options. Instead of exposing the internal utility, it would make sense to introduce a customizer for ProgrammaticDriverConfigLoaderBuilder. (See DriverConfigLoaderBuilderCustomizer for reference).

By customizing the actual builder you can use the public driver API to apply your customizations.

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Sep 1, 2020

Abhijit Sarkar commented

Mark Paluch I looked at DriverConfigLoaderBuilderCustomizer, and wasn't sure how to use it. Is what I'm asking for already available here CassandraAutoConfiguration.java#L113? But CassandraAutoConfiguration is not invoked if AbstractCassandraConfiguration is subclassed by user

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Sep 1, 2020

Mark Paluch commented

DriverConfigLoaderBuilderCustomizer is part of Spring Boot. We would copy the concept into Spring Data Cassandra so you can apply customization in the same way by just using Spring Data Cassandra, without Spring Boot

@spring-projects-issues
Copy link
Author

spring-projects-issues commented Sep 1, 2020

Abhijit Sarkar commented

I see, makes sense. Although the number of people that actually use Spring data without Boot is probably only a handful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core type: enhancement
Projects
None yet
Development

No branches or pull requests

2 participants