kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 033031ba2befe5f8f20509a8fb44561657d379d84f090e0f9e6ae1d42b32af3a |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-06-27 12:08 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-08-27 18:43 +02:00 |
| Parents | 8d5319df770c |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +38 −23
| @@ -242,18 +242,18 @@ | ||
| 242 | 242 | return; |
| 243 | 243 | } |
| 244 | 244 | |
| 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 | + } | |
| 250 | 250 | |
| 251 | - if (trash.contains(connection) && inFlight == 0) { | |
| 252 | - if (trash.remove(connection)) | |
| 253 | - close(connection); | |
| 254 | - return; | |
| 255 | - } | |
| 251 | + int inFlight = connection.inFlight.decrementAndGet(); | |
| 256 | 252 | |
| 253 | + if (trash.contains(connection)) { | |
| 254 | + if (inFlight == 0 && trash.remove(connection)) | |
| 255 | + close(connection); | |
| 256 | + } else { | |
| 257 | 257 | if (connections.size() > options().getCoreConnectionsPerHost(hostDistance) && inFlight <= options().getMinSimultaneousRequestsPerConnectionThreshold(hostDistance)) { |
| 258 | 258 | trashConnection(connection); |
| 259 | 259 | } else if (connection.maxAvailableStreams() < MIN_AVAILABLE_STREAMS) { |
| @@ -267,23 +267,35 @@ | ||
| 267 | 267 | // Trash the connection and create a new one, but we don't call trashConnection |
| 268 | 268 | // directly because we want to make sure the connection is always trashed. |
| 269 | 269 | private void replaceConnection(PooledConnection connection) { |
| 270 | - open.decrementAndGet(); | |
| 270 | + if (connection.markForTrash.compareAndSet(false, true)) | |
| 271 | + open.decrementAndGet(); | |
| 271 | 272 | maybeSpawnNewConnection(); |
| 272 | 273 | doTrashConnection(connection); |
| 273 | 274 | } |
| 274 | 275 | |
| 275 | 276 | 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 | + } | |
| 281 | 289 | |
| 282 | - if (open.compareAndSet(opened, opened - 1)) | |
| 283 | - break; | |
| 290 | + doTrashConnection(connection); | |
| 284 | 291 | } |
| 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. | |
| 287 | 299 | return true; |
| 288 | 300 | } |
| 289 | 301 | |
| @@ -356,7 +368,9 @@ | ||
| 356 | 368 | manager.blockingExecutor().submit(newConnectionTask); |
| 357 | 369 | } |
| 358 | 370 | |
| 359 | - void replace(final Connection connection) { | |
| 371 | + void replaceDefunctConnection(final PooledConnection connection) { | |
| 372 | + if (connection.markForTrash.compareAndSet(false, true)) | |
| 373 | + open.decrementAndGet(); | |
| 360 | 374 | connections.remove(connection); |
| 361 | 375 | connection.closeAsync(); |
| 362 | 376 | manager.blockingExecutor().submit(new Runnable() { |
| @@ -398,11 +412,12 @@ | ||
| 398 | 412 | private List<CloseFuture> discardAvailableConnections() { |
| 399 | 413 | |
| 400 | 414 | List<CloseFuture> futures = new ArrayList<CloseFuture>(connections.size()); |
| 401 | - for (Connection connection : connections) { | |
| 415 | + for (final PooledConnection connection : connections) { | |
| 402 | 416 | CloseFuture future = connection.closeAsync(); |
| 403 | 417 | future.addListener(new Runnable() { |
| 404 | 418 | public void run() { |
| 405 | - open.decrementAndGet(); | |
| 419 | + if (connection.markForTrash.compareAndSet(false, true)) | |
| 420 | + open.decrementAndGet(); | |
| 406 | 421 | } |
| 407 | 422 | }, MoreExecutors.sameThreadExecutor()); |
| 408 | 423 | futures.add(future); |
modified driver-core/src/main/java/com/datastax/driver/core/PooledConnection.java +5 −1
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.net.InetSocketAddress; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; | |
| 19 | 20 | |
| 20 | 21 | /** |
| 21 | 22 | * A connection that is associated to a pool. |
| @@ -24,6 +25,9 @@ | ||
| 24 | 25 | |
| 25 | 26 | private final HostConnectionPool pool; |
| 26 | 27 | |
| 28 | + /** Used in {@link HostConnectionPool} to handle races between two threads trying to trash the same connection */ | |
| 29 | + final AtomicBoolean markForTrash = new AtomicBoolean(); | |
| 30 | + | |
| 27 | 31 | PooledConnection(String name, InetSocketAddress address, Factory factory, HostConnectionPool pool) throws ConnectionException, InterruptedException, UnsupportedProtocolVersionException, ClusterNameMismatchException { |
| 28 | 32 | super(name, address, factory); |
| 29 | 33 | this.pool = pool; |
| @@ -47,7 +51,7 @@ | ||
| 47 | 51 | if (hostIsDown) { |
| 48 | 52 | pool.closeAsync().force(); |
| 49 | 53 | } else { |
| 50 | - pool.replace(this); | |
| 54 | + pool.replaceDefunctConnection(this); | |
| 51 | 55 | } |
| 52 | 56 | } |
| 53 | 57 | } |