Elffuss BraidHub

kikocis/java-driver public

Fix negative HostConnectionPool open count (JAVA-349).
Changeset033031ba2befe5f8f20509a8fb44561657d379d84f090e0f9e6ae1d42b32af3a
Authorolim7t <omichallat+github@gmail.com> · 2014-06-27 12:08 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-08-27 18:43 +02:00
Parents8d5319df770c

Browse the files at this changeset

2 files changed

modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +38 −23
@@ -242,18 +242,18 @@
242242 return;
243243 }
244244
245- int inFlight = connection.inFlight.decrementAndGet();
246-
247- // If the connection is defunct, we have already replaced it or closed
248- // the pool as part of marking it.
249- if (!connection.isDefunct()) {
245+ if (connection.isDefunct()) {
246+ // As part of making it defunct, we have already replaced it or
247+ // closed the pool.
248+ return;
249+ }
250250
251- if (trash.contains(connection) && inFlight == 0) {
252- if (trash.remove(connection))
253- close(connection);
254- return;
255- }
251+ int inFlight = connection.inFlight.decrementAndGet();
256252
253+ if (trash.contains(connection)) {
254+ if (inFlight == 0 && trash.remove(connection))
255+ close(connection);
256+ } else {
257257 if (connections.size() > options().getCoreConnectionsPerHost(hostDistance) && inFlight <= options().getMinSimultaneousRequestsPerConnectionThreshold(hostDistance)) {
258258 trashConnection(connection);
259259 } else if (connection.maxAvailableStreams() < MIN_AVAILABLE_STREAMS) {
@@ -267,23 +267,35 @@
267267 // Trash the connection and create a new one, but we don't call trashConnection
268268 // directly because we want to make sure the connection is always trashed.
269269 private void replaceConnection(PooledConnection connection) {
270- open.decrementAndGet();
270+ if (connection.markForTrash.compareAndSet(false, true))
271+ open.decrementAndGet();
271272 maybeSpawnNewConnection();
272273 doTrashConnection(connection);
273274 }
274275
275276 private boolean trashConnection(PooledConnection connection) {
276- // First, make sure we don't go below core connections
277- for(;;) {
278- int opened = open.get();
279- if (opened <= options().getCoreConnectionsPerHost(hostDistance))
280- return false;
277+ if (connection.markForTrash.compareAndSet(false, true)) {
278+ // First, make sure we don't go below core connections
279+ for (;;) {
280+ int opened = open.get();
281+ if (opened <= options().getCoreConnectionsPerHost(hostDistance)) {
282+ connection.markForTrash.set(false);
283+ return false;
284+ }
285+
286+ if (open.compareAndSet(opened, opened - 1))
287+ break;
288+ }
281289
282- if (open.compareAndSet(opened, opened - 1))
283- break;
290+ doTrashConnection(connection);
284291 }
285-
286- doTrashConnection(connection);
292+ // If compareAndSet failed, it means we raced and another thread will execute doTrashConnection.
293+ // If the connection needs to be closed (inFlight == 0), we don't need to do it here because the other thread will necessarily do
294+ // it:
295+ // - the current thread decremented inFlight in returnConnection
296+ // - we know it did it before the connection was trashed, because otherwise it would have entered `if (trash.contains(connection))`
297+ // in returnConnection and not arrived here.
298+ // - so the other thread will see the up-to-date value of inFlight and take appropriate action.
287299 return true;
288300 }
289301
@@ -356,7 +368,9 @@
356368 manager.blockingExecutor().submit(newConnectionTask);
357369 }
358370
359- void replace(final Connection connection) {
371+ void replaceDefunctConnection(final PooledConnection connection) {
372+ if (connection.markForTrash.compareAndSet(false, true))
373+ open.decrementAndGet();
360374 connections.remove(connection);
361375 connection.closeAsync();
362376 manager.blockingExecutor().submit(new Runnable() {
@@ -398,11 +412,12 @@
398412 private List<CloseFuture> discardAvailableConnections() {
399413
400414 List<CloseFuture> futures = new ArrayList<CloseFuture>(connections.size());
401- for (Connection connection : connections) {
415+ for (final PooledConnection connection : connections) {
402416 CloseFuture future = connection.closeAsync();
403417 future.addListener(new Runnable() {
404418 public void run() {
405- open.decrementAndGet();
419+ if (connection.markForTrash.compareAndSet(false, true))
420+ open.decrementAndGet();
406421 }
407422 }, MoreExecutors.sameThreadExecutor());
408423 futures.add(future);
modified driver-core/src/main/java/com/datastax/driver/core/PooledConnection.java +5 −1
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import java.net.InetSocketAddress;
19+import java.util.concurrent.atomic.AtomicBoolean;
1920
2021 /**
2122 * A connection that is associated to a pool.
@@ -24,6 +25,9 @@
2425
2526 private final HostConnectionPool pool;
2627
28+ /** Used in {@link HostConnectionPool} to handle races between two threads trying to trash the same connection */
29+ final AtomicBoolean markForTrash = new AtomicBoolean();
30+
2731 PooledConnection(String name, InetSocketAddress address, Factory factory, HostConnectionPool pool) throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException {
2832 super(name, address, factory);
2933 this.pool = pool;
@@ -47,7 +51,7 @@
4751 if (hostIsDown) {
4852 pool.closeAsync().force();
4953 } else {
50- pool.replace(this);
54+ pool.replaceDefunctConnection(this);
5155 }
5256 }
5357 }