kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | dc165191aa63661aa410a0388bdc44a145680101dcb180f2972de06f7c482758 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-05-14 16:53 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-14 19:27 +02:00 |
| Parents | e464b525d39a |
1 file changed
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +7 −9
| @@ -72,7 +72,7 @@ | ||
| 72 | 72 | private final AtomicReference<CloseFuture> closeFuture = new AtomicReference<CloseFuture>(); |
| 73 | 73 | |
| 74 | 74 | private enum Phase { INITIALIZING, READY, INIT_FAILED, CLOSING } |
| 75 | - private volatile Phase phase = Phase.INITIALIZING; | |
| 75 | + private final AtomicReference<Phase> phase = new AtomicReference<Phase>(Phase.INITIALIZING); | |
| 76 | 76 | |
| 77 | 77 | public HostConnectionPool(final Host host, HostDistance hostDistance, final SessionManager manager){ |
| 78 | 78 | assert hostDistance != HostDistance.IGNORED; |
| @@ -134,14 +134,14 @@ | ||
| 134 | 134 | forceClose(connections); |
| 135 | 135 | } else { |
| 136 | 136 | logger.trace("Created connection pool to host {}", host); |
| 137 | - phase = Phase.READY; | |
| 137 | + phase.compareAndSet(Phase.INITIALIZING, Phase.READY); | |
| 138 | 138 | initFuture.set(null); |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | @Override |
| 143 | 143 | public void onFailure(Throwable t) { |
| 144 | - phase = Phase.INIT_FAILED; | |
| 144 | + phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED); | |
| 145 | 145 | forceClose(connections); |
| 146 | 146 | initFuture.setException(t); |
| 147 | 147 | } |
| @@ -161,9 +161,7 @@ | ||
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | public Connection borrowConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { |
| 164 | - if (phase == Phase.INITIALIZING) | |
| 165 | - throw new ConnectionException(host.getSocketAddress(), "Pool is initializing."); | |
| 166 | - | |
| 164 | + Phase phase = this.phase.get(); | |
| 167 | 165 | if (phase != Phase.READY) |
| 168 | 166 | // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host. |
| 169 | 167 | // That being said, maybe having a specific exception could be cleaner. |
| @@ -393,7 +391,7 @@ | ||
| 393 | 391 | break; |
| 394 | 392 | } |
| 395 | 393 | |
| 396 | - if (phase != Phase.READY) { | |
| 394 | + if (phase.get() != Phase.READY) { | |
| 397 | 395 | open.decrementAndGet(); |
| 398 | 396 | return false; |
| 399 | 397 | } |
| @@ -410,7 +408,7 @@ | ||
| 410 | 408 | newConnection.state.compareAndSet(RESURRECTING, OPEN); // no-op if it was already OPEN |
| 411 | 409 | |
| 412 | 410 | // We might have raced with pool shutdown since the last check; ensure the connection gets closed in case the pool did not do it. |
| 413 | - if (phase != Phase.READY && !newConnection.isClosed()) { | |
| 411 | + if (isClosed() && !newConnection.isClosed()) { | |
| 414 | 412 | close(newConnection); |
| 415 | 413 | open.decrementAndGet(); |
| 416 | 414 | return false; |
| @@ -555,7 +553,7 @@ | ||
| 555 | 553 | if (future != null) |
| 556 | 554 | return future; |
| 557 | 555 | |
| 558 | - phase = Phase.CLOSING; | |
| 556 | + phase.set(Phase.CLOSING); | |
| 559 | 557 | |
| 560 | 558 | // Wake up all threads that wait |
| 561 | 559 | signalAllAvailableConnection(); |