Skip to content

Commit ad6b01d

Browse files
committed
Add control connection timeout property for Cassandra
Closes gh-24189
1 parent 5000051 commit ad6b01d

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java

+9
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.beans.factory.ObjectProvider;
4141
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
4242
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Connection;
43+
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Controlconnection;
4344
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Request;
4445
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Throttler;
4546
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.ThrottlerType;
@@ -127,6 +128,7 @@ private Config cassandraConfiguration(CassandraProperties properties) {
127128
mapConnectionOptions(properties, options);
128129
mapPoolingOptions(properties, options);
129130
mapRequestOptions(properties, options);
131+
mapControlConnectionOptions(properties, options);
130132
map.from(mapContactPoints(properties))
131133
.to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
132134
map.from(properties.getLocalDatacenter()).to(
@@ -178,6 +180,13 @@ private void mapRequestOptions(CassandraProperties properties, CassandraDriverOp
178180
(drainInterval) -> options.add(DefaultDriverOption.REQUEST_THROTTLER_DRAIN_INTERVAL, drainInterval));
179181
}
180182

183+
private void mapControlConnectionOptions(CassandraProperties properties, CassandraDriverOptions options) {
184+
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
185+
Controlconnection controlProperties = properties.getControlconnection();
186+
map.from(controlProperties::getTimeout).asInt(Duration::toMillis)
187+
.to((timeout) -> options.add(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, timeout));
188+
}
189+
181190
private List<String> mapContactPoints(CassandraProperties properties) {
182191
return properties.getContactPoints().stream()
183192
.map((candidate) -> formatContactPoint(candidate, properties.getPort())).collect(Collectors.toList());

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraProperties.java

+26
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public class CassandraProperties {
105105
*/
106106
private final Request request = new Request();
107107

108+
/**
109+
* Control connection configuration.
110+
*/
111+
private final Controlconnection controlconnection = new Controlconnection();
112+
108113
public String getKeyspaceName() {
109114
return this.keyspaceName;
110115
}
@@ -259,6 +264,10 @@ public Request getRequest() {
259264
return this.request;
260265
}
261266

267+
public Controlconnection getControlconnection() {
268+
return this.controlconnection;
269+
}
270+
262271
public static class Connection {
263272

264273
/**
@@ -386,6 +395,23 @@ public void setHeartbeatInterval(Duration heartbeatInterval) {
386395

387396
}
388397

398+
public static class Controlconnection {
399+
400+
/**
401+
* Timeout to use for control queries.
402+
*/
403+
private Duration timeout = Duration.ofSeconds(5);
404+
405+
public Duration getTimeout() {
406+
return this.timeout;
407+
}
408+
409+
public void setTimeout(Duration timeout) {
410+
this.timeout = timeout;
411+
}
412+
413+
}
414+
389415
public static class Throttler {
390416

391417
/**

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ void driverConfigLoaderCustomizeRequestOptions() {
166166
});
167167
}
168168

169+
@Test
170+
void driverConfigLoaderCustomizeControlConnectionOptions() {
171+
this.contextRunner.withPropertyValues("spring.data.cassandra.controlconnection.timeout=200ms")
172+
.run((context) -> {
173+
DriverExecutionProfile config = context.getBean(DriverConfigLoader.class).getInitialConfig()
174+
.getDefaultProfile();
175+
assertThat(config.getInt(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT)).isEqualTo(200);
176+
});
177+
}
178+
169179
@Test
170180
void driverConfigLoaderUsePassThroughLimitingRequestThrottlerByDefault() {
171181
this.contextRunner.withPropertyValues().run((context) -> {

0 commit comments

Comments
 (0)