Skip to content

Commit df7b383

Browse files
committed
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
1 parent eb08ad0 commit df7b383

File tree

4 files changed

+31
-14
lines changed

4 files changed

+31
-14
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ private Config cassandraConfiguration(CassandraProperties properties) {
114114
mapPoolingOptions(properties, options);
115115
map.from(properties::getContactPoints)
116116
.to((contactPoints) -> options.add(DefaultDriverOption.CONTACT_POINTS, contactPoints));
117+
map.from(properties.getLocalDatacenter()).to(
118+
(localDatacenter) -> options.add(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, localDatacenter));
117119
ConfigFactory.invalidateCaches();
118120
return ConfigFactory.defaultOverrides().withFallback(options.build())
119121
.withFallback(ConfigFactory.defaultReference()).resolve();

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ public class CassandraProperties {
5555
*/
5656
private final List<String> contactPoints = new ArrayList<>(Collections.singleton("127.0.0.1:9042"));
5757

58+
/**
59+
* Datacenter that is considered "local". Contact points should be from this
60+
* datacenter.
61+
*/
62+
private String localDatacenter;
63+
5864
/**
5965
* Login user of the server.
6066
*/
@@ -141,6 +147,14 @@ public List<String> getContactPoints() {
141147
return this.contactPoints;
142148
}
143149

150+
public String getLocalDatacenter() {
151+
return this.localDatacenter;
152+
}
153+
154+
public void setLocalDatacenter(String localDatacenter) {
155+
this.localDatacenter = localDatacenter;
156+
}
157+
144158
public String getUsername() {
145159
return this.username;
146160
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ void driverConfigLoaderWithDefaultConfiguration() {
5151
});
5252
}
5353

54+
@Test
55+
void driverConfigLoaderWithContactPoints() {
56+
this.contextRunner.withPropertyValues("spring.data.cassandra.contact-points=cluster.example.com:9042",
57+
"spring.data.cassandra.local-datacenter=cassandra-eu1").run((context) -> {
58+
assertThat(context).hasSingleBean(DriverConfigLoader.class);
59+
DriverExecutionProfile configuration = context.getBean(DriverConfigLoader.class).getInitialConfig()
60+
.getDefaultProfile();
61+
assertThat(configuration.getStringList(DefaultDriverOption.CONTACT_POINTS))
62+
.containsOnly("cluster.example.com:9042");
63+
assertThat(configuration.getString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER))
64+
.isEqualTo("cassandra-eu1");
65+
});
66+
}
67+
5468
@Test
5569
void driverConfigLoaderWithCustomSessionName() {
5670
this.contextRunner.withPropertyValues("spring.data.cassandra.session-name=testcluster").run((context) -> {

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

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929

3030
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
3131
import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration;
32-
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
3332
import org.springframework.boot.autoconfigure.data.cassandra.city.City;
3433
import org.springframework.boot.test.util.TestPropertyValues;
3534
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
36-
import org.springframework.context.annotation.Bean;
37-
import org.springframework.context.annotation.Configuration;
3835
import org.springframework.data.cassandra.config.SchemaAction;
3936
import org.springframework.data.cassandra.config.SessionFactoryFactoryBean;
4037

@@ -58,9 +55,9 @@ class CassandraDataAutoConfigurationIntegrationTests {
5855
@BeforeEach
5956
void setUp() {
6057
this.context = new AnnotationConfigApplicationContext();
61-
this.context.register(TestConfiguration.class);
6258
TestPropertyValues
6359
.of("spring.data.cassandra.contact-points:localhost:" + cassandra.getFirstMappedPort(),
60+
"spring.data.cassandra.local-datacenter=datacenter1",
6461
"spring.data.cassandra.read-timeout=24000", "spring.data.cassandra.connect-timeout=10000")
6562
.applyTo(this.context.getEnvironment());
6663
}
@@ -105,14 +102,4 @@ private void createTestKeyspaceIfNotExists() {
105102
}
106103
}
107104

108-
@Configuration
109-
static class TestConfiguration {
110-
111-
@Bean
112-
CqlSessionBuilderCustomizer sessionCustomizer() {
113-
return (builder) -> builder.withLocalDatacenter("datacenter1");
114-
}
115-
116-
}
117-
118105
}

0 commit comments

Comments
 (0)