Add control connection timeout property for Cassandra

Closes gh-24189
pull/24604/head
Stephane Nicoll 4 years ago
parent 5000051891
commit ad6b01d6d0

@ -40,6 +40,7 @@ import com.typesafe.config.ConfigFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Connection; import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Connection;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Controlconnection;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Request; import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Request;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Throttler; import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.Throttler;
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.ThrottlerType; import org.springframework.boot.autoconfigure.cassandra.CassandraProperties.ThrottlerType;
@ -127,6 +128,7 @@ public class CassandraAutoConfiguration {
mapConnectionOptions(properties, options); mapConnectionOptions(properties, options);
mapPoolingOptions(properties, options); mapPoolingOptions(properties, options);
mapRequestOptions(properties, options); mapRequestOptions(properties, options);
mapControlConnectionOptions(properties, options);
map.from(mapContactPoints(properties)) map.from(mapContactPoints(properties))
.to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints)); .to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
map.from(properties.getLocalDatacenter()).to( map.from(properties.getLocalDatacenter()).to(
@ -178,6 +180,13 @@ public class CassandraAutoConfiguration {
(drainInterval) -> options.add(DefaultDriverOption.REQUEST_THROTTLER_DRAIN_INTERVAL, drainInterval)); (drainInterval) -> options.add(DefaultDriverOption.REQUEST_THROTTLER_DRAIN_INTERVAL, drainInterval));
} }
private void mapControlConnectionOptions(CassandraProperties properties, CassandraDriverOptions options) {
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
Controlconnection controlProperties = properties.getControlconnection();
map.from(controlProperties::getTimeout).asInt(Duration::toMillis)
.to((timeout) -> options.add(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT, timeout));
}
private List<String> mapContactPoints(CassandraProperties properties) { private List<String> mapContactPoints(CassandraProperties properties) {
return properties.getContactPoints().stream() return properties.getContactPoints().stream()
.map((candidate) -> formatContactPoint(candidate, properties.getPort())).collect(Collectors.toList()); .map((candidate) -> formatContactPoint(candidate, properties.getPort())).collect(Collectors.toList());

@ -105,6 +105,11 @@ public class CassandraProperties {
*/ */
private final Request request = new Request(); private final Request request = new Request();
/**
* Control connection configuration.
*/
private final Controlconnection controlconnection = new Controlconnection();
public String getKeyspaceName() { public String getKeyspaceName() {
return this.keyspaceName; return this.keyspaceName;
} }
@ -259,6 +264,10 @@ public class CassandraProperties {
return this.request; return this.request;
} }
public Controlconnection getControlconnection() {
return this.controlconnection;
}
public static class Connection { public static class Connection {
/** /**
@ -386,6 +395,23 @@ public class CassandraProperties {
} }
public static class Controlconnection {
/**
* Timeout to use for control queries.
*/
private Duration timeout = Duration.ofSeconds(5);
public Duration getTimeout() {
return this.timeout;
}
public void setTimeout(Duration timeout) {
this.timeout = timeout;
}
}
public static class Throttler { public static class Throttler {
/** /**

@ -166,6 +166,16 @@ class CassandraAutoConfigurationTests {
}); });
} }
@Test
void driverConfigLoaderCustomizeControlConnectionOptions() {
this.contextRunner.withPropertyValues("spring.data.cassandra.controlconnection.timeout=200ms")
.run((context) -> {
DriverExecutionProfile config = context.getBean(DriverConfigLoader.class).getInitialConfig()
.getDefaultProfile();
assertThat(config.getInt(DefaultDriverOption.CONTROL_CONNECTION_TIMEOUT)).isEqualTo(200);
});
}
@Test @Test
void driverConfigLoaderUsePassThroughLimitingRequestThrottlerByDefault() { void driverConfigLoaderUsePassThroughLimitingRequestThrottlerByDefault() {
this.contextRunner.withPropertyValues().run((context) -> { this.contextRunner.withPropertyValues().run((context) -> {

Loading…
Cancel
Save