Skip to content

Commit ba1bc45

Browse files
committed
Polish "Add PoolingOptions to CasandraProperties"
Closes gh-7946
1 parent 2aafc7d commit ba1bc45

File tree

4 files changed

+106
-69
lines changed

4 files changed

+106
-69
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@
1717
package org.springframework.boot.autoconfigure.cassandra;
1818

1919
import java.util.List;
20-
import java.util.Map;
2120

2221
import com.datastax.driver.core.Cluster;
23-
import com.datastax.driver.core.HostDistance;
2422
import com.datastax.driver.core.PoolingOptions;
2523
import com.datastax.driver.core.QueryOptions;
2624
import com.datastax.driver.core.SocketOptions;
@@ -133,12 +131,13 @@ private SocketOptions getSocketOptions() {
133131
}
134132

135133
private PoolingOptions getPoolingOptions() {
134+
CassandraProperties.Pool pool = this.properties.getPool();
136135
PoolingOptions options = new PoolingOptions();
137-
options.setHeartbeatIntervalSeconds(this.properties.getHeartbeatIntervalSeconds());
138-
options.setMaxQueueSize(this.properties.getMaxQueueSize());
139-
for (Map.Entry<HostDistance, Integer> entry : this.properties.getMaxRequestsPerConnection().entrySet()) {
140-
options.setMaxRequestsPerConnection(entry.getKey(), entry.getValue());
141-
}
136+
options.setIdleTimeoutSeconds(pool.getIdleTimeout());
137+
options.setPoolTimeoutMillis(pool.getPoolTimeout());
138+
options.setHeartbeatIntervalSeconds(pool.getHeartbeatInterval());
139+
options.setMaxQueueSize(pool.getMaxQueueSize());
142140
return options;
143141
}
142+
144143
}

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

Lines changed: 72 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.cassandra;
1818

19-
import java.util.HashMap;
20-
import java.util.Map;
21-
2219
import com.datastax.driver.core.ConsistencyLevel;
23-
import com.datastax.driver.core.HostDistance;
24-
import com.datastax.driver.core.PoolingOptions;
2520
import com.datastax.driver.core.ProtocolOptions;
2621
import com.datastax.driver.core.ProtocolOptions.Compression;
2722
import com.datastax.driver.core.QueryOptions;
@@ -38,6 +33,7 @@
3833
* @author Julien Dubois
3934
* @author Phillip Webb
4035
* @author Mark Paluch
36+
* @author Stephane Nicoll
4137
* @since 1.3.0
4238
*/
4339
@ConfigurationProperties(prefix = "spring.data.cassandra")
@@ -118,21 +114,6 @@ public class CassandraProperties {
118114
*/
119115
private int readTimeoutMillis = SocketOptions.DEFAULT_READ_TIMEOUT_MILLIS;
120116

121-
/**
122-
* Pooling option: heartbeat interval.
123-
*/
124-
private int heartbeatIntervalSeconds = PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS;
125-
126-
/**
127-
* Pooling option: max queue size.
128-
*/
129-
private int maxQueueSize = PoolingOptions.DEFAULT_MAX_QUEUE_SIZE;
130-
131-
/**
132-
* Pooling option: max requests per connection.
133-
*/
134-
private Map<HostDistance, Integer> maxRequestsPerConnection = new HashMap<HostDistance, Integer>();
135-
136117
/**
137118
* Schema action to take at startup.
138119
*/
@@ -143,6 +124,11 @@ public class CassandraProperties {
143124
*/
144125
private boolean ssl = false;
145126

127+
/**
128+
* Pool configuration.
129+
*/
130+
private final Pool pool = new Pool();
131+
146132
public String getKeyspaceName() {
147133
return this.keyspaceName;
148134
}
@@ -265,30 +251,6 @@ public void setReadTimeoutMillis(int readTimeoutMillis) {
265251
this.readTimeoutMillis = readTimeoutMillis;
266252
}
267253

268-
public int getHeartbeatIntervalSeconds() {
269-
return this.heartbeatIntervalSeconds;
270-
}
271-
272-
public void setHeartbeatIntervalSeconds(int heartbeatIntervalSeconds) {
273-
this.heartbeatIntervalSeconds = heartbeatIntervalSeconds;
274-
}
275-
276-
public int getMaxQueueSize() {
277-
return this.maxQueueSize;
278-
}
279-
280-
public void setMaxQueueSize(int maxQueueSize) {
281-
this.maxQueueSize = maxQueueSize;
282-
}
283-
284-
public Map<HostDistance, Integer> getMaxRequestsPerConnection() {
285-
return this.maxRequestsPerConnection;
286-
}
287-
288-
public void setMaxRequestsPerConnection(Map<HostDistance, Integer> maxRequestsPerConnection) {
289-
this.maxRequestsPerConnection = maxRequestsPerConnection;
290-
}
291-
292254
public boolean isSsl() {
293255
return this.ssl;
294256
}
@@ -305,4 +267,69 @@ public void setSchemaAction(String schemaAction) {
305267
this.schemaAction = schemaAction;
306268
}
307269

270+
public Pool getPool() {
271+
return this.pool;
272+
}
273+
274+
/**
275+
* Pool properties.
276+
*/
277+
public static class Pool {
278+
279+
/**
280+
* Idle timeout (in seconds) before an idle connection is removed.
281+
*/
282+
private int idleTimeout = 120;
283+
284+
/**
285+
* Pool timeout (in milliseconds) when trying to acquire a connection from a
286+
* host's pool.
287+
*/
288+
private int poolTimeout = 5000;
289+
290+
/**
291+
* Heartbeat interval (in seconds) after which a message is sent on an idle
292+
* connection to make sure it's still alive.
293+
*/
294+
private int heartbeatInterval = 30;
295+
296+
/**
297+
* Maximum number of requests that get enqueued if no connection is available.
298+
*/
299+
private int maxQueueSize = 256;
300+
301+
public int getIdleTimeout() {
302+
return this.idleTimeout;
303+
}
304+
305+
public void setIdleTimeout(int idleTimeout) {
306+
this.idleTimeout = idleTimeout;
307+
}
308+
309+
public int getPoolTimeout() {
310+
return this.poolTimeout;
311+
}
312+
313+
public void setPoolTimeout(int poolTimeout) {
314+
this.poolTimeout = poolTimeout;
315+
}
316+
317+
public int getHeartbeatInterval() {
318+
return this.heartbeatInterval;
319+
}
320+
321+
public void setHeartbeatInterval(int heartbeatInterval) {
322+
this.heartbeatInterval = heartbeatInterval;
323+
}
324+
325+
public int getMaxQueueSize() {
326+
return this.maxQueueSize;
327+
}
328+
329+
public void setMaxQueueSize(int maxQueueSize) {
330+
this.maxQueueSize = maxQueueSize;
331+
}
332+
333+
}
334+
308335
}

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package org.springframework.boot.autoconfigure.cassandra;
1818

1919
import com.datastax.driver.core.Cluster;
20-
import com.datastax.driver.core.HostDistance;
20+
import com.datastax.driver.core.PoolingOptions;
2121
import org.junit.After;
2222
import org.junit.Test;
2323

@@ -82,27 +82,34 @@ public void customizerOverridesAutoConfig() {
8282
}
8383

8484
@Test
85-
public void heartbeatInterval() {
86-
load("spring.data.cassandra.heartbeat-interval-seconds=60");
87-
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
88-
Cluster cluster = this.context.getBean(Cluster.class);
89-
assertThat(cluster.getConfiguration().getPoolingOptions().getHeartbeatIntervalSeconds()).isEqualTo(60);
90-
}
91-
92-
@Test
93-
public void maxQueueSize() {
94-
load("spring.data.cassandra.max-queue-size=1024");
85+
public void defaultPoolOptions() {
86+
load();
9587
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
96-
Cluster cluster = this.context.getBean(Cluster.class);
97-
assertThat(cluster.getConfiguration().getPoolingOptions().getMaxQueueSize()).isEqualTo(1024);
88+
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
89+
.getConfiguration().getPoolingOptions();
90+
assertThat(poolingOptions.getIdleTimeoutSeconds())
91+
.isEqualTo(PoolingOptions.DEFAULT_IDLE_TIMEOUT_SECONDS);
92+
assertThat(poolingOptions.getPoolTimeoutMillis())
93+
.isEqualTo(PoolingOptions.DEFAULT_POOL_TIMEOUT_MILLIS);
94+
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
95+
.isEqualTo(PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
96+
assertThat(poolingOptions.getMaxQueueSize())
97+
.isEqualTo(PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
9898
}
9999

100100
@Test
101-
public void maxRequestsPerConnection() {
102-
load("spring.data.cassandra.max-requests-per-connection.local=100");
101+
public void customizePoolOptions() {
102+
load("spring.data.cassandra.pool.idle-timeout=42",
103+
"spring.data.cassandra.pool.pool-timeout=52",
104+
"spring.data.cassandra.pool.heartbeat-interval=62",
105+
"spring.data.cassandra.pool.max-queue-size=72");
103106
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
104-
Cluster cluster = this.context.getBean(Cluster.class);
105-
assertThat(cluster.getConfiguration().getPoolingOptions().getMaxRequestsPerConnection(HostDistance.LOCAL)).isEqualTo(100);
107+
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
108+
.getConfiguration().getPoolingOptions();
109+
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
110+
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
111+
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
112+
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
106113
}
107114

108115
private void load(String... environment) {

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,10 @@ content into your application; rather pick only the properties that you need.
595595
spring.data.cassandra.max-queue-size= # Pooling option: max queue size.
596596
spring.data.cassandra.port= # Port of the Cassandra server.
597597
spring.data.cassandra.password= # Login password of the server.
598+
spring.data.cassandra.pool.heartbeat-interval=30 # Heartbeat interval (in seconds) after which a message is sent on an idle connection to make sure it's still alive.
599+
spring.data.cassandra.pool.idle-timeout=120 # Idle timeout (in seconds) before an idle connection is removed.
600+
spring.data.cassandra.pool.max-queue-size=256 # Maximum number of requests that get enqueued if no connection is available.
601+
spring.data.cassandra.pool.pool-timeout=5000 # Pool timeout (in milliseconds) when trying to acquire a connection from a host's pool.
598602
spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
599603
spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
600604
spring.data.cassandra.reconnection-policy= # Reconnection policy class.

0 commit comments

Comments
 (0)