I'm getting sporadic timeout exceptions from the Cassandra driver in a Spring Boot project, in spite of having what I think are the appropriate yml configuration properties.
spring:
cassandra:
contact-points: localhost
local-datacenter: datacenter1
keyspace-name: my_keyspace
request:
consistency: quorum
serial-consistency: quorum
page-size: 2500
timeout: 20s
connection:
init-query-timeout: 20s
connect-timeout: 20s
controlconnection:
timeout: 20s
As a workaround, I am able to prevent timeouts by configuring properties in Java code.
public class CassandraConfig extends AbstractCassandraConfiguration {
@Bean
public CqlSessionFactoryBean cassandraSession() {
CqlSessionFactoryBean sessionFactoryBean = super.cassandraSession();
setProperties(sessionFactoryBean);
return sessionFactoryBean;
}
private void setProperties(CqlSessionFactoryBean sessionFactoryBean) {
sessionFactoryBean.setUsername(...);
sessionFactoryBean.setPassword(...);
sessionFactoryBean.setPort(...);
sessionFactoryBean.setLocalDatacenter(...);
// These timeouts should already be configured in application.yml but seem to be ignored?
//
sessionFactoryBean.setSessionBuilderConfigurer(config -> config.withConfigLoader(
DriverConfigLoader.programmaticBuilder()
.withDuration(DefaultDriverOption.CONNECTION_INIT_QUERY_TIMEOUT, Duration.ofSeconds(20))
.withDuration(DefaultDriverOption.REQUEST_TIMEOUT, Duration.ofSeconds(20))
.build()
));
}
}
I've found a tantalizing hint of what might be happening, in a comment.
It may be a side-effect of you trying to solve your problem, but in its current form your project is mostly configuring Cassandra itself. This means that Spring Boot's auto-configuration has largely backed off. As a result any
spring.data.cassandra.*
properties that you have set won't have any effect unless you're using them in your own code.
So my question is, what exactly are the Cassandra configurations that can cause Spring Boot's configurations to "back off"? For example, does any occurrence of a CqlSessionFactoryBean
have this effect, regardless of its properties?
Dependencies:
- org.springframework.data:spring-data-cassandra:4.3.0
- com.datastax.cassandra:cassandra-driver-core:3.10.0