Skip to content

Commit 9f3e528

Browse files
committed
Revert "Add idle timeout to the connection pool (JAVA-419)."
This reverts commits f84cdc1, 38edc2e and f510750. The implementation needs to be reconsidered (see JAVA-573). Due to planning constraints, we are not able to address this in 2.0.9. The feature will be reintroduced in a later version.
1 parent 34a500b commit 9f3e528

7 files changed

Lines changed: 11 additions & 261 deletions

File tree

driver-core/CHANGELOG.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ CHANGELOG
4747
- [improvement] Shuffle the replicas in TokenAwarePolicy.newQueryPlan (JAVA-504)
4848
- [improvement] Make schema agreement wait tuneable (JAVA-507)
4949
- [improvement] Document how to inject the driver metrics into another registry (JAVA-494)
50-
- [improvement] Add idle timeout to the connection pool (JAVA-419)
5150
- [bug] LatencyAwarePolicy does not shutdown executor on invocation of close (JAVA-516)
5251
- [improvement] Throw an exception when DCAwareRoundRobinPolicy is built with
5352
an explicit but null or empty local datacenter (JAVA-451).

driver-core/src/main/java/com/datastax/driver/core/Cluster.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,6 @@ private Manager(String clusterName, List<InetSocketAddress> contactPoints, Confi
11611161

11621162
this.metrics = configuration.getMetricsOptions() == null ? null : new Metrics(this);
11631163
this.listeners = new CopyOnWriteArraySet<Host.StateListener>(listeners);
1164-
1165-
this.scheduledTasksExecutor.scheduleWithFixedDelay(new TrashIdleConnectionsTask(), 10, 10, TimeUnit.SECONDS);
11661164
}
11671165

11681166
// Initialization is not too performance intensive and in practice there shouldn't be contention
@@ -2175,19 +2173,6 @@ public void run() {
21752173
}).start();
21762174
}
21772175
}
2178-
2179-
private class TrashIdleConnectionsTask implements Runnable {
2180-
@Override public void run() {
2181-
try {
2182-
long now = System.currentTimeMillis();
2183-
for (SessionManager session : sessions) {
2184-
session.trashIdleConnections(now);
2185-
}
2186-
} catch (Exception e) {
2187-
logger.warn("Error while trashing idle connections", e);
2188-
}
2189-
}
2190-
}
21912176
}
21922177

21932178
/**

driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -265,24 +265,15 @@ public void returnConnection(PooledConnection connection) {
265265
close(connection);
266266
} else {
267267
if (connections.size() > options().getCoreConnectionsPerHost(hostDistance) && inFlight <= options().getMinSimultaneousRequestsPerConnectionThreshold(hostDistance)) {
268-
connection.setTrashTimeIn(options().getIdleTimeoutSeconds());
268+
trashConnection(connection);
269269
} else if (connection.maxAvailableStreams() < MIN_AVAILABLE_STREAMS) {
270270
replaceConnection(connection);
271271
} else {
272-
connection.cancelTrashTime();
273272
signalAvailableConnection();
274273
}
275274
}
276275
}
277276

278-
void trashIdleConnections(long now) {
279-
for (PooledConnection connection : connections) {
280-
if (connection.getTrashTime() < now) {
281-
trashConnection(connection);
282-
}
283-
}
284-
}
285-
286277
// Trash the connection and create a new one, but we don't call trashConnection
287278
// directly because we want to make sure the connection is always trashed.
288279
private void replaceConnection(PooledConnection connection) {
@@ -299,7 +290,6 @@ private boolean trashConnection(PooledConnection connection) {
299290
int opened = open.get();
300291
if (opened <= options().getCoreConnectionsPerHost(hostDistance)) {
301292
connection.markForTrash.set(false);
302-
connection.cancelTrashTime();
303293
return false;
304294
}
305295

@@ -309,9 +299,13 @@ private boolean trashConnection(PooledConnection connection) {
309299

310300
doTrashConnection(connection);
311301
}
312-
// If compareAndSet failed, it means we raced with another thread that will execute doTrashConnection.
313-
// Since trashConnection is called from a scheduled task, we're sure that the current thread did not modify
314-
// inFlight, so the other thread will take care of closing the connection if necessary (i.e. if inFlight == 0).
302+
// If compareAndSet failed, it means we raced and another thread will execute doTrashConnection.
303+
// If the connection needs to be closed (inFlight == 0), we don't need to do it here because the other thread will necessarily do
304+
// it:
305+
// - the current thread decremented inFlight in returnConnection
306+
// - we know it did it before the connection was trashed, because otherwise it would have entered `if (trash.contains(connection))`
307+
// in returnConnection and not arrived here.
308+
// - so the other thread will see the up-to-date value of inFlight and take appropriate action.
315309
return true;
316310
}
317311

driver-core/src/main/java/com/datastax/driver/core/PooledConnection.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ class PooledConnection extends Connection {
2525

2626
private final HostConnectionPool pool;
2727

28-
/** The instant when the connection should be trashed after being idle for too long */
29-
private volatile long trashTime = Long.MAX_VALUE;
30-
3128
/** Used in {@link HostConnectionPool} to handle races between two threads trying to trash the same connection */
3229
final AtomicBoolean markForTrash = new AtomicBoolean();
3330

@@ -63,16 +60,4 @@ protected void notifyOwnerWhenDefunct(boolean hostIsDown) {
6360
pool.replaceDefunctConnection(this);
6461
}
6562
}
66-
67-
long getTrashTime() {
68-
return trashTime;
69-
}
70-
71-
void cancelTrashTime() {
72-
trashTime = Long.MAX_VALUE;
73-
}
74-
75-
void setTrashTimeIn(int timeoutSeconds) {
76-
trashTime = System.currentTimeMillis() + 1000 * timeoutSeconds;
77-
}
7863
}

driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class PoolingOptions {
5151
private static final int DEFAULT_MAX_POOL_LOCAL = 8;
5252
private static final int DEFAULT_MAX_POOL_REMOTE = 2;
5353

54-
private static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120;
5554
private static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000;
5655

5756
private volatile Cluster.Manager manager;
@@ -62,7 +61,6 @@ public class PoolingOptions {
6261
private final int[] coreConnections = new int[] { DEFAULT_CORE_POOL_LOCAL, DEFAULT_CORE_POOL_REMOTE, 0 };
6362
private final int[] maxConnections = new int[] { DEFAULT_MAX_POOL_LOCAL , DEFAULT_MAX_POOL_REMOTE, 0 };
6463

65-
private volatile int idleTimeoutSeconds = DEFAULT_IDLE_TIMEOUT_SECONDS;
6664
private volatile int poolTimeoutMillis = DEFAULT_POOL_TIMEOUT_MILLIS;
6765

6866
public PoolingOptions() {}
@@ -223,33 +221,6 @@ public synchronized PoolingOptions setMaxConnectionsPerHost(HostDistance distanc
223221
return this;
224222
}
225223

226-
/**
227-
* Returns the timeout before an idle connection is removed.
228-
*
229-
* @return the timeout.
230-
*/
231-
public int getIdleTimeoutSeconds() {
232-
return idleTimeoutSeconds;
233-
}
234-
235-
/**
236-
* Sets the timeout before an idle connection is removed.
237-
* <p>
238-
* The order of magnitude should be a few minutes (the default is 120 seconds). The
239-
* timeout that triggers the removal has a granularity of 10 seconds.
240-
*
241-
* @param idleTimeoutSeconds the new timeout in seconds.
242-
* @return this {@code PoolingOptions}.
243-
*
244-
* @throws IllegalArgumentException if the timeout is negative.
245-
*/
246-
public PoolingOptions setIdleTimeoutSeconds(int idleTimeoutSeconds) {
247-
if (idleTimeoutSeconds < 0)
248-
throw new IllegalArgumentException("Idle timeout must be positive");
249-
this.idleTimeoutSeconds = idleTimeoutSeconds;
250-
return this;
251-
}
252-
253224
/**
254225
* Returns the timeout when trying to acquire a connection from a host's pool.
255226
*

driver-core/src/main/java/com/datastax/driver/core/SessionManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.driver.core;
1717

18+
import java.net.InetAddress;
1819
import java.net.InetSocketAddress;
1920
import java.nio.ByteBuffer;
2021
import java.util.*;
@@ -28,6 +29,8 @@
2829
import org.slf4j.Logger;
2930
import org.slf4j.LoggerFactory;
3031

32+
33+
import com.datastax.driver.core.exceptions.AuthenticationException;
3134
import com.datastax.driver.core.exceptions.DriverInternalError;
3235
import com.datastax.driver.core.exceptions.UnsupportedFeatureException;
3336
import com.datastax.driver.core.policies.LoadBalancingPolicy;
@@ -540,12 +543,6 @@ ResultSetFuture executeQuery(Message.Request msg, Statement statement) {
540543
return future;
541544
}
542545

543-
void trashIdleConnections(long now) {
544-
for (HostConnectionPool pool : pools.values()) {
545-
pool.trashIdleConnections(now);
546-
}
547-
}
548-
549546
private static class State implements Session.State {
550547

551548
private final SessionManager session;

driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTimeoutTest.java

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)