Elffuss BraidHub

kikocis/java-driver public

Avoid race between closing pool and marking it ready (JAVA-768).
Changesetb085b0df1f743ebfde85b503e47cba044492d54c04fb51c2dff513346dd3450c
Authorolim7t <omichallat+github@gmail.com> · 2015-05-13 16:49 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-05-19 09:57 +02:00
Parents9e0cf2e1fe2a

Browse the files at this changeset

5 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -37,6 +37,7 @@
3737 - [bug] Fix buffer leaks when compression is enabled (JAVA-734).
3838 - [improvement] Use Netty's pooled ByteBufAllocator by default (JAVA-756)
3939 - [improvement] Expose "unsafe" paging state API (JAVA-759)
40+- [bug] Prevent race during pool initialization (JAVA-768)
4041
4142
4243 2.1.5:
modified driver-core/src/main/java/com/datastax/driver/core/DynamicConnectionPool.java +6 −11
@@ -119,14 +119,14 @@
119119 forceClose(connections);
120120 } else {
121121 logger.trace("Created connection pool to host {}", host);
122- phase = Phase.READY;
122+ phase.compareAndSet(Phase.INITIALIZING, Phase.READY);
123123 initFuture.set(null);
124124 }
125125 }
126126
127127 @Override
128128 public void onFailure(Throwable t) {
129- phase = Phase.INIT_FAILED;
129+ phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED);
130130 forceClose(connections);
131131 initFuture.setException(t);
132132 }
@@ -147,9 +147,7 @@
147147
148148 @Override
149149 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();
153151 if (phase != Phase.READY)
154152 // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host.
155153 // That being said, maybe having a specific exception could be cleaner.
@@ -380,7 +378,7 @@
380378 break;
381379 }
382380
383- if (phase != Phase.READY) {
381+ if (phase.get() != Phase.READY) {
384382 open.decrementAndGet();
385383 return false;
386384 }
@@ -397,7 +395,7 @@
397395 newConnection.state.compareAndSet(RESURRECTING, OPEN); // no-op if it was already OPEN
398396
399397 // 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()) {
401399 close(newConnection);
402400 open.decrementAndGet();
403401 return false;
@@ -534,13 +532,10 @@
534532 }
535533
536534 protected CloseFuture makeCloseFuture() {
537- phase = Phase.CLOSING;
538-
539535 // Wake up all threads that wait
540536 signalAllAvailableConnection();
541537
542- CloseFuture future = new CloseFuture.Forwarding(discardAvailableConnections());
543- return future;
538+ return new CloseFuture.Forwarding(discardAvailableConnections());
544539 }
545540
546541 private List<CloseFuture> discardAvailableConnections() {
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +3 −1
@@ -47,7 +47,7 @@
4747 protected final AtomicReference<CloseFuture> closeFuture = new AtomicReference<CloseFuture>();
4848
4949 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);
5151
5252 protected HostConnectionPool(Host host, HostDistance hostDistance, SessionManager manager) {
5353 assert hostDistance != HostDistance.IGNORED;
@@ -91,6 +91,8 @@
9191 if (future != null)
9292 return future;
9393
94+ phase.set(Phase.CLOSING);
95+
9496 future = makeCloseFuture();
9597
9698 return closeFuture.compareAndSet(null, future)
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +0 −1
@@ -214,7 +214,6 @@
214214 Futures.addCallback(poolInitFuture, new FutureCallback<Void>() {
215215 @Override
216216 public void onSuccess(Void result) {
217- // If we raced with a session shutdown, ensure that the pool will be closed.
218217 HostConnectionPool previous = pools.put(host, newPool);
219218 if (previous == null) {
220219 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 @@
9696 Futures.addCallback(connectionFuture, new FutureCallback<Void>() {
9797 @Override
9898 public void onSuccess(Void result) {
99+ connectionRef.set(connection);
100+ open.set(true);
99101 if (isClosed()) {
100102 initFuture.setException(new ConnectionException(host.getSocketAddress(), "Pool was closed during initialization"));
101103 // we're not sure if closeAsync() saw the connection, so ensure it gets closed
102104 connection.closeAsync().force();
103105 } else {
104106 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);
108108 initFuture.set(null);
109109 }
110110 }
111111
112112 @Override
113113 public void onFailure(Throwable t) {
114- phase = Phase.INIT_FAILED;
114+ phase.compareAndSet(Phase.INITIALIZING, Phase.INIT_FAILED);
115115 initFuture.setException(t);
116116 }
117117 }, initExecutor);
@@ -125,9 +125,7 @@
125125
126126 @Override
127127 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();
131129 if (phase != Phase.READY)
132130 // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host.
133131 // That being said, maybe having a specific exception could be cleaner.
@@ -282,14 +280,24 @@
282280 if (!open.compareAndSet(false, true))
283281 return false;
284282
285- if (phase != Phase.READY) {
283+ if (phase.get() != Phase.READY) {
286284 open.set(false);
287285 return false;
288286 }
289287
290288 // Now really open the connection
291289 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+
293301 signalAvailableConnection();
294302 return true;
295303 } catch (InterruptedException e) {
@@ -322,7 +330,6 @@
322330 if (!scheduledForCreation.compareAndSet(false, true))
323331 return;
324332
325- logger.debug("Creating new connection on busy pool to {}", host);
326333 manager.blockingExecutor().submit(newConnectionTask);
327334 }
328335
@@ -348,8 +355,6 @@
348355 }
349356
350357 protected CloseFuture makeCloseFuture() {
351- phase = Phase.CLOSING;
352-
353358 // Wake up all threads that wait
354359 signalAllAvailableConnection();
355360