Commit df7b3839 authored by Stephane Nicoll's avatar Stephane Nicoll

Add local-datacenter property for Cassandra

The Cassandra v4 driver does not longer have automatic local DC inference
from contact points. As a result, the "local-datacenter" property must be
set with the default load balancing policy and the contact points must
be of that data center.

This commit adds a new property for the local datacenter so that it can
be specified without the use of a customizer.

Closes gh-19779
parent eb08ad02
...@@ -114,6 +114,8 @@ public class CassandraAutoConfiguration { ...@@ -114,6 +114,8 @@ public class CassandraAutoConfiguration {
mapPoolingOptions(properties, options); mapPoolingOptions(properties, options);
map.from(properties::getContactPoints) map.from(properties::getContactPoints)
.to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints)); .to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
map.from(properties.getLocalDatacenter()).to(
(localDatacenter) -> options.add(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, localDatacenter));
ConfigFactory.invalidateCaches(); ConfigFactory.invalidateCaches();
return ConfigFactory.defaultOverrides().withFallback(options.build()) return ConfigFactory.defaultOverrides().withFallback(options.build())
.withFallback(ConfigFactory.defaultReference()).resolve(); .withFallback(ConfigFactory.defaultReference()).resolve();
......
...@@ -55,6 +55,12 @@ public class CassandraProperties { ...@@ -55,6 +55,12 @@ public class CassandraProperties {
*/ */
private final List<String> contactPoints = new ArrayList<>(Collections.singleton("127.0.0.1:9042")); private final List<String> contactPoints = new ArrayList<>(Collections.singleton("127.0.0.1:9042"));
/**
* Datacenter that is considered "local". Contact points should be from this
* datacenter.
*/
private String localDatacenter;
/** /**
* Login user of the server. * Login user of the server.
*/ */
...@@ -141,6 +147,14 @@ public class CassandraProperties { ...@@ -141,6 +147,14 @@ public class CassandraProperties {
return this.contactPoints; return this.contactPoints;
} }
public String getLocalDatacenter() {
return this.localDatacenter;
}
public void setLocalDatacenter(String localDatacenter) {
this.localDatacenter = localDatacenter;
}
public String getUsername() { public String getUsername() {
return this.username; return this.username;
} }
......
...@@ -51,6 +51,20 @@ class CassandraAutoConfigurationTests { ...@@ -51,6 +51,20 @@ class CassandraAutoConfigurationTests {
}); });
} }
@Test
void driverConfigLoaderWithContactPoints() {
this.contextRunner.withPropertyValues("spring.data.cassandra.contact-points=cluster.example.com:9042",
"spring.data.cassandra.local-datacenter=cassandra-eu1").run((context) -> {
assertThat(context).hasSingleBean(DriverConfigLoader.class);
DriverExecutionProfile configuration = context.getBean(DriverConfigLoader.class).getInitialConfig()
.getDefaultProfile();
assertThat(configuration.getStringList(DefaultDriverOption.CONTACT_POINTS))
.containsOnly("cluster.example.com:9042");
assertThat(configuration.getString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER))
.isEqualTo("cassandra-eu1");
});
}
@Test @Test
void driverConfigLoaderWithCustomSessionName() { void driverConfigLoaderWithCustomSessionName() {
this.contextRunner.withPropertyValues("spring.data.cassandra.session-name=testcluster").run((context) -> { this.contextRunner.withPropertyValues("spring.data.cassandra.session-name=testcluster").run((context) -> {
......
...@@ -29,12 +29,9 @@ import org.testcontainers.junit.jupiter.Testcontainers; ...@@ -29,12 +29,9 @@ import org.testcontainers.junit.jupiter.Testcontainers;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration; import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
import org.springframework.boot.autoconfigure.data.cassandra.city.City; import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.config.SessionFactoryFactoryBean; import org.springframework.data.cassandra.config.SessionFactoryFactoryBean;
...@@ -58,9 +55,9 @@ class CassandraDataAutoConfigurationIntegrationTests { ...@@ -58,9 +55,9 @@ class CassandraDataAutoConfigurationIntegrationTests {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class);
TestPropertyValues TestPropertyValues
.of("spring.data.cassandra.contact-points:localhost:" + cassandra.getFirstMappedPort(), .of("spring.data.cassandra.contact-points:localhost:" + cassandra.getFirstMappedPort(),
"spring.data.cassandra.local-datacenter=datacenter1",
"spring.data.cassandra.read-timeout=24000", "spring.data.cassandra.connect-timeout=10000") "spring.data.cassandra.read-timeout=24000", "spring.data.cassandra.connect-timeout=10000")
.applyTo(this.context.getEnvironment()); .applyTo(this.context.getEnvironment());
} }
...@@ -105,14 +102,4 @@ class CassandraDataAutoConfigurationIntegrationTests { ...@@ -105,14 +102,4 @@ class CassandraDataAutoConfigurationIntegrationTests {
} }
} }
@Configuration
static class TestConfiguration {
@Bean
CqlSessionBuilderCustomizer sessionCustomizer() {
return (builder) -> builder.withLocalDatacenter("datacenter1");
}
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment