Elffuss BraidHub

kikocis/java-driver public

Merge branch 'java614' into 2.0.9_fixes
Changeset9913b399123d389f87d6bfb74af975697795cc3d50f749ac4f8cc6d10e1444f2
Authorolim7t <omichallat+github@gmail.com> · 2015-01-30 15:48 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-01-30 15:48 +01:00
Parentsb2cec176a184, 00adccdfeee8

Browse the files at this changeset

9 files changed

modified driver-core/CHANGELOG.rst +6 −0
@@ -4,6 +4,12 @@
44 2.0.9.1:
55 --------
66
7+- [bug] Prevent race between cancellation and query completion (JAVA-614)
8+- [bug] Prevent cancel and timeout from cancelling unrelated ResponseHandler if
9+ streamId was already released and reused (JAVA-632).
10+- [bug] Fix issue when newly opened pool fails before we could mark the node UP
11+ (JAVA-642)
12+
713
814 2.0.9:
915 ------
modified driver-core/pom.xml +14 −0
@@ -89,6 +89,20 @@
8989 <scope>test</scope>
9090 </dependency>
9191
92+ <dependency>
93+ <groupId>org.scassandra</groupId>
94+ <artifactId>java-client</artifactId>
95+ <!-- N.B. later versions of scassandra require JDK 7 -->
96+ <version>0.4.1</version>
97+ <scope>test</scope>
98+ <exclusions>
99+ <exclusion>
100+ <groupId>ch.qos.logback</groupId>
101+ <artifactId>logback-classic</artifactId>
102+ </exclusion>
103+ </exclusions>
104+ </dependency>
105+
92106 </dependencies>
93107
94108 <build>
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +30 −12
@@ -276,9 +276,19 @@
276276 // sure the "suspected" mechanism work as expected
277277 Host host = factory.manager.metadata.getHost(address);
278278 if (host != null) {
279+
280+ // If the host was reconnecting, and this error happens right after we opened a connection pool, but
281+ // before we could mark the node UP, we don't want to go through the SUSPECTED state, because that can
282+ // lead to a race condition that leaves the node UP with a closed pool.
283+ boolean belongsToReconnectingPool = host.state != Host.State.UP &&
284+ this instanceof PooledConnection &&
285+ (((PooledConnection)this).pool == null || !((PooledConnection)this).pool.isClosed());
286+
287+ boolean markSuspected = isInitialized && !belongsToReconnectingPool;
288+
279289 // This will trigger onDown, including when the defunct Connection is part of a reconnection attempt, which is redundant.
280290 // This is not too much of a problem since calling onDown on a node that is already down has no effect.
281- boolean isDown = factory.manager.signalConnectionFailure(host, ce, host.wasJustAdded(), isInitialized);
291+ boolean isDown = factory.manager.signalConnectionFailure(host, ce, host.wasJustAdded(), markSuspected);
282292 notifyOwnerWhenDefunct(isDown);
283293 }
284294
@@ -364,12 +374,12 @@
364374 * never leave a handler that won't get an answer or be errored out.
365375 */
366376 if (isDefunct) {
367- dispatcher.removeHandler(handler.streamId, true);
377+ dispatcher.removeHandler(handler, true);
368378 throw new ConnectionException(address, "Write attempt on defunct connection");
369379 }
370380
371381 if (isClosed()) {
372- dispatcher.removeHandler(handler.streamId, true);
382+ dispatcher.removeHandler(handler, true);
373383 throw new ConnectionException(address, "Connection has been closed");
374384 }
375385
@@ -390,7 +400,7 @@
390400 logger.debug("{} Error writing request {}", Connection.this, request);
391401 // Remove this handler from the dispatcher so it don't get notified of the error
392402 // twice (we will fail that method already)
393- dispatcher.removeHandler(handler.streamId, true);
403+ dispatcher.removeHandler(handler, true);
394404
395405 final ConnectionException ce;
396406 if (writeFuture.getCause() instanceof java.nio.channels.ClosedChannelException) {
@@ -623,19 +633,27 @@
623633 assert old == null;
624634 }
625635
626- public void removeHandler(int streamId, boolean releaseStreamId) {
636+ public void removeHandler(ResponseHandler handler, boolean releaseStreamId) {
627637
628638 // If we don't release the ID, mark first so that we can rely later on the fact that if
629639 // we receive a response for an ID with no handler, it's that this ID has been marked.
630640 if (!releaseStreamId)
631- streamIdHandler.mark(streamId);
632-
633- ResponseHandler handler = pending.remove(streamId);
634- if (handler != null)
635- handler.cancelTimeout();
641+ streamIdHandler.mark(handler.streamId);
642+
643+ // If a RequestHandler is cancelled right when the response arrives, this method (called with releaseStreamId=false) will race with messageReceived.
644+ // messageReceived could have already released the streamId, which could have already been reused by another request. We must not remove the handler
645+ // if it's not ours, because that would cause the other request to hang forever.
646+ boolean removed = pending.remove(handler.streamId, handler);
647+ if (!removed) {
648+ // We raced, so if we marked the streamId above, that was wrong.
649+ if (!releaseStreamId)
650+ streamIdHandler.unmark(handler.streamId);
651+ return;
652+ }
653+ handler.cancelTimeout();
636654
637655 if (releaseStreamId)
638- streamIdHandler.release(streamId);
656+ streamIdHandler.release(handler.streamId);
639657
640658 if (isClosed())
641659 tryTerminate(false);
@@ -864,7 +882,7 @@
864882 // request and there is no point in holding the handler, but we don't release the streamId. If we
865883 // were, a new request could reuse that ID but get the answer to the request we just gave up on instead
866884 // of its own answer, and we would have no way to detect that.
867- connection.dispatcher.removeHandler(streamId, false);
885+ connection.dispatcher.removeHandler(this, false);
868886 if (connection instanceof PooledConnection)
869887 ((PooledConnection)connection).release();
870888 }
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +2 −2
@@ -260,6 +260,8 @@
260260 }
261261
262262 public void returnConnection(PooledConnection connection) {
263+ int inFlight = connection.inFlight.decrementAndGet();
264+
263265 if (isClosed()) {
264266 close(connection);
265267 return;
@@ -271,8 +273,6 @@
271273 return;
272274 }
273275
274- int inFlight = connection.inFlight.decrementAndGet();
275-
276276 if (trash.contains(connection)) {
277277 if (inFlight == 0 && trash.remove(connection))
278278 close(connection);
modified driver-core/src/main/java/com/datastax/driver/core/PooledConnection.java +1 −1
@@ -23,7 +23,7 @@
2323 */
2424 class PooledConnection extends Connection {
2525
26- private final HostConnectionPool pool;
26+ final HostConnectionPool pool;
2727
2828 /** Used in {@link HostConnectionPool} to handle races between two threads trying to trash the same connection */
2929 final AtomicBoolean markForTrash = new AtomicBoolean();
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +44 −12
@@ -56,6 +56,7 @@
5656 private volatile List<Host> triedHosts;
5757 private volatile HostConnectionPool currentPool;
5858 private final AtomicReference<QueryState> queryStateRef;
59+ private volatile boolean shouldCancelConnectionHandler;
5960
6061 // This represents the number of times a retry has been triggered by the RetryPolicy (this is different from
6162 // queryStateRef.get().retryCount, because some retries don't involve the policy, for example after an
@@ -67,7 +68,6 @@
6768
6869 private volatile Map<InetSocketAddress, Throwable> errors;
6970
70- private volatile boolean isCanceled;
7171 private volatile Connection.ResponseHandler connectionHandler;
7272
7373 private final Timer.Context timerContext;
@@ -99,7 +99,7 @@
9999
100100 public void sendRequest() {
101101 try {
102- while (queryPlan.hasNext() && !isCanceled) {
102+ while (queryPlan.hasNext() && queryStateRef.get() != QueryState.CANCELLED) {
103103 Host host = queryPlan.next();
104104 logger.trace("Querying node {}", host);
105105 if (query(host))
@@ -126,7 +126,7 @@
126126 triedHosts.add(current);
127127 }
128128 current = host;
129- connectionHandler = connection.write(this);
129+ write(connection, this);
130130 return true;
131131 } catch (ConnectionException e) {
132132 // If we have any problem with the connection, move to the next node.
@@ -154,6 +154,33 @@
154154 }
155155 }
156156
157+ private void write(Connection connection, Connection.ResponseCallback responseCallback) throws ConnectionException, BusyConnectionException {
158+ // Make sure cancel() does not see a stale connectionHandler if it sees the new query state
159+ // before connection.write has completed
160+ connectionHandler = null;
161+
162+ // Set query state to "in progress" at next iteration
163+ while (true) {
164+ QueryState previous = queryStateRef.get();
165+ assert !previous.inProgress;
166+ if (previous == QueryState.CANCELLED) {
167+ return;
168+ }
169+ if (queryStateRef.compareAndSet(previous, previous.startNext()))
170+ break;
171+ }
172+
173+ connectionHandler = connection.write(responseCallback);
174+
175+ // N.B: onSet/onException could have already been called at this point
176+
177+ // If cancel was called after we set the state to "in progress", but before connection.write, it might have seen a null connectionHandler.
178+ // In this case we need to cancel it now to release the connection (because the next onSet/onException/onTimeout will see the CANCELLED
179+ // state and return immediately).
180+ if (shouldCancelConnectionHandler)
181+ connectionHandler.cancelHandler();
182+ }
183+
157184 private void logError(InetSocketAddress address, Throwable exception) {
158185 logger.debug("Error querying {}, trying next host (error is: {})", address, exception.toString());
159186 if (errors == null)
@@ -162,8 +189,6 @@
162189 }
163190
164191 private void retry(final boolean retryCurrent, ConsistencyLevel newConsistencyLevel) {
165- queryStateRef.set(queryStateRef.get().startNext());
166-
167192 final Host h = current;
168193 this.retryConsistencyLevel = newConsistencyLevel;
169194
@@ -171,6 +196,8 @@
171196 manager.executor().execute(new Runnable() {
172197 @Override
173198 public void run() {
199+ if (queryStateRef.get() == QueryState.CANCELLED)
200+ return;
174201 try {
175202 if (retryCurrent) {
176203 if (query(h))
@@ -185,9 +212,13 @@
185212 }
186213
187214 public void cancel() {
188- isCanceled = true;
189- if (connectionHandler != null)
190- connectionHandler.cancelHandler();
215+ QueryState previous = queryStateRef.getAndSet(QueryState.CANCELLED);
216+ if (previous.inProgress) {
217+ if (connectionHandler != null)
218+ connectionHandler.cancelHandler();
219+ else
220+ shouldCancelConnectionHandler = true;
221+ }
191222 }
192223
193224 @Override
@@ -388,9 +419,8 @@
388419 connection.setKeyspace(prepareKeyspace);
389420 }
390421
391- queryStateRef.set(queryStateRef.get().startNext());
392422 try {
393- connection.write(prepareAndRetry(toPrepare.getQueryString()));
423+ write(connection, prepareAndRetry(toPrepare.getQueryString()));
394424 } finally {
395425 // Always reset the previous keyspace if needed
396426 if (connection.keyspace() == null || !connection.keyspace().equals(currentKeyspace))
@@ -583,10 +613,12 @@
583613 }
584614
585615 // This is used to prevent races between request completion (either success or error) and timeout.
586- // A retry is in progress once we have written the request to the connection and until we get back a response or a timeout.
616+ // A retry is in progress once we have written the request to the connection and until we get back a response (see onSet
617+ // or onException) or a timeout (see onTimeout).
587618 // The count increments on each retry.
588619 static class QueryState {
589- static QueryState INITIAL = new QueryState(0, true);
620+ static final QueryState INITIAL = new QueryState(-1, false);
621+ static final QueryState CANCELLED = new QueryState(Integer.MIN_VALUE, false);
590622
591623 final int retryCount;
592624 final boolean inProgress;
added driver-core/src/test/java/com/datastax/driver/core/RequestHandlerTest.java +72 −0
@@ -0,0 +1,72 @@
1+package com.datastax.driver.core;
2+
3+import java.util.concurrent.TimeUnit;
4+import java.util.concurrent.TimeoutException;
5+
6+import com.google.common.collect.ImmutableMap;
7+import org.scassandra.Scassandra;
8+import org.scassandra.http.client.PrimingRequest;
9+import org.testng.annotations.Test;
10+
11+import static org.assertj.core.api.Assertions.assertThat;
12+
13+public class RequestHandlerTest {
14+
15+ @Test(groups = "long")
16+ public void should_handle_race_between_response_and_cancellation() {
17+ Scassandra scassandra = TestUtils.createScassandraServer();
18+ Cluster cluster = null;
19+
20+ try {
21+ // Use a mock server that takes a constant time to reply
22+ scassandra.start();
23+ scassandra.primingClient().prime(
24+ PrimingRequest.queryBuilder()
25+ .withQuery("mock query")
26+ .withRows(ImmutableMap.of("key", 1))
27+ .withFixedDelay(10)
28+ .build()
29+ );
30+
31+ cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(scassandra.getBinaryPort())
32+ .withPoolingOptions(new PoolingOptions()
33+ .setCoreConnectionsPerHost(HostDistance.LOCAL, 1)
34+ .setMaxConnectionsPerHost(HostDistance.LOCAL, 1))
35+ .build();
36+
37+ Session session = cluster.connect();
38+
39+ // To reproduce, we need to cancel the query exactly when the reply arrives.
40+ // Run a few queries to estimate how much that will take.
41+ int samples = 100;
42+ long start = System.currentTimeMillis();
43+ for (int i = 0; i < samples; i++) {
44+ session.execute("mock query");
45+ }
46+ long elapsed = System.currentTimeMillis() - start;
47+ long queryDuration = elapsed / samples;
48+
49+ // Now run queries and cancel them after that estimated time
50+ for (int i = 0; i < 2000; i++) {
51+ ResultSetFuture future = session.executeAsync("mock query");
52+ try {
53+ future.getUninterruptibly(queryDuration, TimeUnit.MILLISECONDS);
54+ } catch (TimeoutException e) {
55+ future.cancel(true);
56+ }
57+ }
58+
59+ PooledConnection connection = getSingleConnection(session);
60+ assertThat(connection.inFlight.get()).isEqualTo(0);
61+ } finally {
62+ if (cluster != null)
63+ cluster.close();
64+ scassandra.stop();
65+ }
66+ }
67+
68+ private PooledConnection getSingleConnection(Session session) {
69+ HostConnectionPool pool = ((SessionManager)session).pools.values().iterator().next();
70+ return pool.connections.get(0);
71+ }
72+}
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +34 −0
@@ -15,14 +15,18 @@
1515 */
1616 package com.datastax.driver.core;
1717
18+import java.io.IOException;
1819 import java.math.BigDecimal;
1920 import java.math.BigInteger;
2021 import java.net.InetAddress;
22+import java.net.ServerSocket;
2123 import java.nio.ByteBuffer;
2224 import java.util.*;
2325
2426 import static org.testng.Assert.fail;
2527
28+import org.scassandra.Scassandra;
29+import org.scassandra.ScassandraFactory;
2630 import org.slf4j.Logger;
2731 import org.slf4j.LoggerFactory;
2832 import org.testng.SkipException;
@@ -382,4 +386,34 @@
382386 fail(address + " not found in cluster metadata");
383387 return null; // never reached
384388 }
389+
390+ /**
391+ * @return A Scassandra instance with an arbitrarily chosen binary port from 8042-8142 and admin port from
392+ * 8052-8152.
393+ */
394+ public static Scassandra createScassandraServer() {
395+ int binaryPort = findAvailablePort(8042);
396+ int adminPort = findAvailablePort(8052);
397+ return ScassandraFactory.createServer(binaryPort, adminPort);
398+ }
399+
400+ /**
401+ * @param startingWith The first port to try, if unused will keep trying the next port until one is found up to
402+ * 100 subsequent ports.
403+ * @return A local port that is currently unused.
404+ */
405+ public static int findAvailablePort(int startingWith) {
406+ IOException last = null;
407+ for(int port = startingWith; port < startingWith+100; port++) {
408+ try {
409+ ServerSocket s = new ServerSocket(port);
410+ s.close();
411+ return port;
412+ } catch (IOException e) {
413+ last = e;
414+ }
415+ }
416+ // If for whatever reason a port could not be acquired throw the last encountered exception.
417+ throw new RuntimeException("Could not acquire an available port", last);
418+ }
385419 }
modified driver-core/src/test/resources/log4j.properties +3 −0
@@ -1,6 +1,9 @@
11 # Set root logger level to DEBUG and its only appender to A1.
22 log4j.rootLogger=INFO, A1
33
4+# Scassandra's info log is a bit verbose
5+log4j.logger.org.scassandra=WARN
6+
47 # A1 is set to be a ConsoleAppender.
58 log4j.appender.A1=org.apache.log4j.ConsoleAppender
69