kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | b085b0df1f743ebfde85b503e47cba044492d54c04fb51c2dff513346dd3450c |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-05-13 16:49 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-19 09:57 +02:00 |
| Parents | 9e0cf2e1fe2a |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -37,6 +37,7 @@ | ||
| 37 | 37 | - [bug] Fix buffer leaks when compression is enabled (JAVA-734). |
| 38 | 38 | - [improvement] Use Netty's pooled ByteBufAllocator by default (JAVA-756) |
| 39 | 39 | - [improvement] Expose "unsafe" paging state API (JAVA-759) |
| 40 | +- [bug] Prevent race during pool initialization (JAVA-768) | |
| 40 | 41 | |
| 41 | 42 | |
| 42 | 43 | 2.1.5: |
modified driver-core/src/main/java/com/datastax/driver/core/DynamicConnectionPool.java +6 −11
| @@ -119,14 +119,14 @@ | ||
| 119 | 119 | forceClose(connections); |
| 120 | 120 | } else { |
| 121 | 121 | logger.trace("Created connection pool to host {}", host); |
| 122 | - phase = Phase.READY; | |
| 122 | + phase.compareAndSet(Phase.INITIALIZING, Phase.READY); | |
| 123 | 123 | initFuture.set(null); |
| 124 | 124 | } |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | 127 | @Override |
| 128 | 128 | public void onFailure(Throwable t) { |
| 129 | - phase = Phase.INIT_FAILED; | |
| 129 | + phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED); | |
| 130 | 130 | forceClose(connections); |
| 131 | 131 | initFuture.setException(t); |
| 132 | 132 | } |
| @@ -147,9 +147,7 @@ | ||
| 147 | 147 | |
| 148 | 148 | @Override |
| 149 | 149 | public Connection borrowConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { |
| 150 | - if (phase == Phase.INITIALIZING) | |
| 151 | - throw new ConnectionException(host.getSocketAddress(), "Pool is initializing."); | |
| 152 | - | |
| 150 | + Phase phase = this.phase.get(); | |
| 153 | 151 | if (phase != Phase.READY) |
| 154 | 152 | // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host. |
| 155 | 153 | // That being said, maybe having a specific exception could be cleaner. |
| @@ -380,7 +378,7 @@ | ||
| 380 | 378 | break; |
| 381 | 379 | } |
| 382 | 380 | |
| 383 | - if (phase != Phase.READY) { | |
| 381 | + if (phase.get() != Phase.READY) { | |
| 384 | 382 | open.decrementAndGet(); |
| 385 | 383 | return false; |
| 386 | 384 | } |
| @@ -397,7 +395,7 @@ | ||
| 397 | 395 | newConnection.state.compareAndSet(RESURRECTING, OPEN); // no-op if it was already OPEN |
| 398 | 396 | |
| 399 | 397 | // We might have raced with pool shutdown since the last check; ensure the connection gets closed in case the pool did not do it. |
| 400 | - if (phase != Phase.READY && !newConnection.isClosed()) { | |
| 398 | + if (isClosed() && !newConnection.isClosed()) { | |
| 401 | 399 | close(newConnection); |
| 402 | 400 | open.decrementAndGet(); |
| 403 | 401 | return false; |
| @@ -534,13 +532,10 @@ | ||
| 534 | 532 | } |
| 535 | 533 | |
| 536 | 534 | protected CloseFuture makeCloseFuture() { |
| 537 | - phase = Phase.CLOSING; | |
| 538 | - | |
| 539 | 535 | // Wake up all threads that wait |
| 540 | 536 | signalAllAvailableConnection(); |
| 541 | 537 | |
| 542 | - CloseFuture future = new CloseFuture.Forwarding(discardAvailableConnections()); | |
| 543 | - return future; | |
| 538 | + return new CloseFuture.Forwarding(discardAvailableConnections()); | |
| 544 | 539 | } |
| 545 | 540 | |
| 546 | 541 | private List<CloseFuture> discardAvailableConnections() { |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +3 −1
| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | protected final AtomicReference<CloseFuture> closeFuture = new AtomicReference<CloseFuture>(); |
| 48 | 48 | |
| 49 | 49 | protected enum Phase { INITIALIZING, READY, INIT_FAILED, CLOSING } |
| 50 | - protected volatile Phase phase = Phase.INITIALIZING; | |
| 50 | + protected final AtomicReference<Phase> phase = new AtomicReference<Phase>(Phase.INITIALIZING); | |
| 51 | 51 | |
| 52 | 52 | protected HostConnectionPool(Host host, HostDistance hostDistance, SessionManager manager) { |
| 53 | 53 | assert hostDistance != HostDistance.IGNORED; |
| @@ -91,6 +91,8 @@ | ||
| 91 | 91 | if (future != null) |
| 92 | 92 | return future; |
| 93 | 93 | |
| 94 | + phase.set(Phase.CLOSING); | |
| 95 | + | |
| 94 | 96 | future = makeCloseFuture(); |
| 95 | 97 | |
| 96 | 98 | return closeFuture.compareAndSet(null, future) |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −1
| @@ -214,7 +214,6 @@ | ||
| 214 | 214 | Futures.addCallback(poolInitFuture, new FutureCallback<Void>() { |
| 215 | 215 | @Override |
| 216 | 216 | public void onSuccess(Void result) { |
| 217 | - // If we raced with a session shutdown, ensure that the pool will be closed. | |
| 218 | 217 | HostConnectionPool previous = pools.put(host, newPool); |
| 219 | 218 | if (previous == null) { |
| 220 | 219 | logger.debug("Added connection pool for {}", host); |
modified driver-core/src/main/java/com/datastax/driver/core/SingleConnectionPool.java +17 −12
| @@ -96,22 +96,22 @@ | ||
| 96 | 96 | Futures.addCallback(connectionFuture, new FutureCallback<Void>() { |
| 97 | 97 | @Override |
| 98 | 98 | public void onSuccess(Void result) { |
| 99 | + connectionRef.set(connection); | |
| 100 | + open.set(true); | |
| 99 | 101 | if (isClosed()) { |
| 100 | 102 | initFuture.setException(new ConnectionException(host.getSocketAddress(), "Pool was closed during initialization")); |
| 101 | 103 | // we're not sure if closeAsync() saw the connection, so ensure it gets closed |
| 102 | 104 | connection.closeAsync().force(); |
| 103 | 105 | } else { |
| 104 | 106 | logger.trace("Created connection pool to host {}", host); |
| 105 | - connectionRef.set(connection); | |
| 106 | - open.set(true); | |
| 107 | - phase = Phase.READY; | |
| 107 | + phase.compareAndSet(Phase.INITIALIZING, Phase.READY); | |
| 108 | 108 | initFuture.set(null); |
| 109 | 109 | } |
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | @Override |
| 113 | 113 | public void onFailure(Throwable t) { |
| 114 | - phase = Phase.INIT_FAILED; | |
| 114 | + phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED); | |
| 115 | 115 | initFuture.setException(t); |
| 116 | 116 | } |
| 117 | 117 | }, initExecutor); |
| @@ -125,9 +125,7 @@ | ||
| 125 | 125 | |
| 126 | 126 | @Override |
| 127 | 127 | public Connection borrowConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { |
| 128 | - if (phase == Phase.INITIALIZING) | |
| 129 | - throw new ConnectionException(host.getSocketAddress(), "Pool is initializing."); | |
| 130 | - | |
| 128 | + Phase phase = this.phase.get(); | |
| 131 | 129 | if (phase != Phase.READY) |
| 132 | 130 | // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host. |
| 133 | 131 | // That being said, maybe having a specific exception could be cleaner. |
| @@ -282,14 +280,24 @@ | ||
| 282 | 280 | if (!open.compareAndSet(false, true)) |
| 283 | 281 | return false; |
| 284 | 282 | |
| 285 | - if (phase != Phase.READY) { | |
| 283 | + if (phase.get() != Phase.READY) { | |
| 286 | 284 | open.set(false); |
| 287 | 285 | return false; |
| 288 | 286 | } |
| 289 | 287 | |
| 290 | 288 | // Now really open the connection |
| 291 | 289 | try { |
| 292 | - connectionRef.set(manager.connectionFactory().open(this)); | |
| 290 | + logger.debug("Creating new connection on busy pool to {}", host); | |
| 291 | + Connection newConnection = manager.connectionFactory().open(this); | |
| 292 | + connectionRef.set(newConnection); | |
| 293 | + | |
| 294 | + // We might have raced with pool shutdown since the last check; ensure the connection gets closed in case the pool did not do it. | |
| 295 | + if (isClosed() && !newConnection.isClosed()) { | |
| 296 | + close(newConnection); | |
| 297 | + this.open.set(false); | |
| 298 | + return false; | |
| 299 | + } | |
| 300 | + | |
| 293 | 301 | signalAvailableConnection(); |
| 294 | 302 | return true; |
| 295 | 303 | } catch (InterruptedException e) { |
| @@ -322,7 +330,6 @@ | ||
| 322 | 330 | if (!scheduledForCreation.compareAndSet(false, true)) |
| 323 | 331 | return; |
| 324 | 332 | |
| 325 | - logger.debug("Creating new connection on busy pool to {}", host); | |
| 326 | 333 | manager.blockingExecutor().submit(newConnectionTask); |
| 327 | 334 | } |
| 328 | 335 | |
| @@ -348,8 +355,6 @@ | ||
| 348 | 355 | } |
| 349 | 356 | |
| 350 | 357 | protected CloseFuture makeCloseFuture() { |
| 351 | - phase = Phase.CLOSING; | |
| 352 | - | |
| 353 | 358 | // Wake up all threads that wait |
| 354 | 359 | signalAllAvailableConnection(); |
| 355 | 360 |