Elffuss BraidHub

kikocis/java-driver public

Merge pull request #571 from datastax/java1033

JAVA-1033: Allow per-statement read timeout.
Changeset9b2e3dd1def49746b87b693d52688174617547669c2cb3f73e6ae4ef31728beb
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-25 15:33 +01:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-01-25 15:33 +01:00
Parentsbcf5d9263751, 434273cc91ae

Browse the files at this changeset

7 files changed

modified changelog/README.md +1 −0
@@ -5,6 +5,7 @@
55 - [bug] JAVA-1034: fix metadata parser for collections of custom types.
66 - [improvement] JAVA-1035: Expose host broadcast_address and listen_address if available.
77 - [new feature] JAVA-1037: Allow named parameters in simple statements.
8+- [improvement] JAVA-1033: Allow per-statement read timeout.
89
910 Merged from 2.1 branch:
1011
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +7 −6
@@ -489,14 +489,14 @@
489489 }
490490
491491 ResponseHandler write(ResponseCallback callback) throws ConnectionException, BusyConnectionException {
492- return write(callback, true);
492+ return write(callback, -1, true);
493493 }
494494
495- ResponseHandler write(ResponseCallback callback, boolean startTimeout) throws ConnectionException, BusyConnectionException {
495+ ResponseHandler write(ResponseCallback callback, long readTimeoutMillis, boolean startTimeout) throws ConnectionException, BusyConnectionException {
496496
497497 Message.Request request = callback.request();
498498
499- ResponseHandler handler = new ResponseHandler(this, callback);
499+ ResponseHandler handler = new ResponseHandler(this, readTimeoutMillis, callback);
500500 dispatcher.add(handler);
501501 request.setStreamId(handler.streamId);
502502
@@ -1213,14 +1213,16 @@
12131213 final int streamId;
12141214 final ResponseCallback callback;
12151215 final int retryCount;
1216+ private final long readTimeoutMillis;
12161217
12171218 private final long startTime;
12181219 private volatile Timeout timeout;
12191220
12201221 private final AtomicBoolean isCancelled = new AtomicBoolean();
12211222
1222- ResponseHandler(Connection connection, ResponseCallback callback) throws BusyConnectionException {
1223+ ResponseHandler(Connection connection, long readTimeoutMillis, ResponseCallback callback) throws BusyConnectionException {
12231224 this.connection = connection;
1225+ this.readTimeoutMillis = (readTimeoutMillis > 0) ? readTimeoutMillis : connection.factory.getReadTimeoutMillis();
12241226 this.streamId = connection.dispatcher.streamIdHandler.next();
12251227 if (streamId == -1)
12261228 throw new BusyConnectionException(connection.address);
@@ -1231,8 +1233,7 @@
12311233 }
12321234
12331235 void startTimeout() {
1234- long timeoutMs = connection.factory.getReadTimeoutMillis();
1235- this.timeout = timeoutMs <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), timeoutMs, TimeUnit.MILLISECONDS);
1236+ this.timeout = this.readTimeoutMillis <= 0 ? null : connection.factory.timer.newTimeout(onTimeoutTask(), this.readTimeoutMillis, TimeUnit.MILLISECONDS);
12361237 }
12371238
12381239 void cancelTimeout() {
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −1
@@ -349,7 +349,7 @@
349349 break;
350350 }
351351
352- connectionHandler = connection.write(responseCallback, false);
352+ connectionHandler = connection.write(responseCallback, statement.getReadTimeoutMillis(), false);
353353 // Only start the timeout when we're sure connectionHandler is set. This avoids an edge case where onTimeout() was triggered
354354 // *before* the call to connection.write had returned.
355355 connectionHandler.startTimeout();
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +26 −0
@@ -63,6 +63,7 @@
6363 private volatile boolean traceQuery;
6464 private volatile int fetchSize;
6565 private volatile long defaultTimestamp = Long.MIN_VALUE;
66+ private volatile long readTimeoutMillis = Long.MIN_VALUE;
6667 private volatile RetryPolicy retryPolicy;
6768 private volatile ByteBuffer pagingState;
6869 protected volatile Boolean idempotent;
@@ -321,6 +322,31 @@
321322 return defaultTimestamp;
322323 }
323324
325+ /**
326+ * Overrides the default per-host read timeout ({@link SocketOptions#getReadTimeoutMillis()})
327+ * for this statement.
328+ * <p/>
329+ * You should only override this only for statements for which the coordinator may allow a longer server-side
330+ * timeout (for example aggregation queries).
331+ *
332+ * @param readTimeoutMillis the timeout to set. Must be greater than 0 (or the default will be used).
333+ * @return this {@code Statement} object.
334+ */
335+ public Statement setReadTimeoutMillis(long readTimeoutMillis) {
336+ this.readTimeoutMillis = readTimeoutMillis;
337+ return this;
338+ }
339+
340+ /**
341+ * Return the per-host read timeout that was set for this statement.
342+ *
343+ * @return the timeout. Note that a negative value means that the default
344+ * {@link SocketOptions#getReadTimeoutMillis()} will be used.
345+ */
346+ public long getReadTimeoutMillis() {
347+ return readTimeoutMillis;
348+ }
349+
324350 /**
325351 * Sets the paging state.
326352 * <p/>
modified driver-core/src/main/java/com/datastax/driver/core/StatementWrapper.java +74 −18
@@ -20,6 +20,7 @@
2020 import com.datastax.driver.core.policies.SpeculativeExecutionPolicy;
2121
2222 import java.nio.ByteBuffer;
23+import java.util.Map;
2324
2425 /**
2526 * Base class for custom {@link Statement} implementations that wrap another statement.
@@ -81,8 +82,8 @@
8182 }
8283
8384 @Override
84- public Statement disableTracing() {
85- return wrapped.disableTracing();
85+ public ConsistencyLevel getConsistencyLevel() {
86+ return wrapped.getConsistencyLevel();
8687 }
8788
8889 @Override
@@ -90,14 +91,19 @@
9091 return wrapped.setSerialConsistencyLevel(serialConsistency);
9192 }
9293
94+ @Override
95+ public ConsistencyLevel getSerialConsistencyLevel() {
96+ return wrapped.getSerialConsistencyLevel();
97+ }
98+
9399 @Override
94100 public Statement enableTracing() {
95101 return wrapped.enableTracing();
96102 }
97103
98104 @Override
99- public ByteBuffer getPagingState() {
100- return wrapped.getPagingState();
105+ public Statement disableTracing() {
106+ return wrapped.disableTracing();
101107 }
102108
103109 @Override
@@ -106,13 +112,13 @@
106112 }
107113
108114 @Override
109- public RetryPolicy getRetryPolicy() {
110- return wrapped.getRetryPolicy();
115+ public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
116+ return wrapped.getRoutingKey(protocolVersion, codecRegistry);
111117 }
112118
113119 @Override
114- public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
115- return wrapped.getRoutingKey(protocolVersion, codecRegistry);
120+ public String getKeyspace() {
121+ return wrapped.getKeyspace();
116122 }
117123
118124 @Override
@@ -121,8 +127,38 @@
121127 }
122128
123129 @Override
124- public ConsistencyLevel getConsistencyLevel() {
125- return wrapped.getConsistencyLevel();
130+ public RetryPolicy getRetryPolicy() {
131+ return wrapped.getRetryPolicy();
132+ }
133+
134+ @Override
135+ public Statement setFetchSize(int fetchSize) {
136+ return wrapped.setFetchSize(fetchSize);
137+ }
138+
139+ @Override
140+ public int getFetchSize() {
141+ return wrapped.getFetchSize();
142+ }
143+
144+ @Override
145+ public Statement setDefaultTimestamp(long defaultTimestamp) {
146+ return wrapped.setDefaultTimestamp(defaultTimestamp);
147+ }
148+
149+ @Override
150+ public long getDefaultTimestamp() {
151+ return wrapped.getDefaultTimestamp();
152+ }
153+
154+ @Override
155+ public Statement setReadTimeoutMillis(long readTimeoutMillis) {
156+ return wrapped.setReadTimeoutMillis(readTimeoutMillis);
157+ }
158+
159+ @Override
160+ public long getReadTimeoutMillis() {
161+ return wrapped.getReadTimeoutMillis();
126162 }
127163
128164 @Override
@@ -131,22 +167,42 @@
131167 }
132168
133169 @Override
134- public ConsistencyLevel getSerialConsistencyLevel() {
135- return wrapped.getSerialConsistencyLevel();
170+ public Statement setPagingState(PagingState pagingState) {
171+ return wrapped.setPagingState(pagingState);
136172 }
137173
138174 @Override
139- public String getKeyspace() {
140- return wrapped.getKeyspace();
175+ public Statement setPagingStateUnsafe(byte[] pagingState) {
176+ return wrapped.setPagingStateUnsafe(pagingState);
141177 }
142178
143179 @Override
144- public int getFetchSize() {
145- return wrapped.getFetchSize();
180+ public ByteBuffer getPagingState() {
181+ return wrapped.getPagingState();
146182 }
147183
148184 @Override
149- public Statement setFetchSize(int fetchSize) {
150- return wrapped.setFetchSize(fetchSize);
185+ public Statement setIdempotent(boolean idempotent) {
186+ return wrapped.setIdempotent(idempotent);
187+ }
188+
189+ @Override
190+ public Boolean isIdempotent() {
191+ return wrapped.isIdempotent();
192+ }
193+
194+ @Override
195+ public boolean isIdempotentWithDefault(QueryOptions queryOptions) {
196+ return wrapped.isIdempotentWithDefault(queryOptions);
197+ }
198+
199+ @Override
200+ public Map<String, ByteBuffer> getOutgoingPayload() {
201+ return wrapped.getOutgoingPayload();
202+ }
203+
204+ @Override
205+ public Statement setOutgoingPayload(Map<String, ByteBuffer> payload) {
206+ return wrapped.setOutgoingPayload(payload);
151207 }
152208 }
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +1 −1
@@ -1057,7 +1057,7 @@
10571057 static MockRequest send(HostConnectionPool pool) throws ConnectionException, BusyConnectionException, TimeoutException {
10581058 // Create a MockRequest and spy on it. Create a response handler and add it to the connection's dispatcher.
10591059 MockRequest request = spy(new MockRequest(pool));
1060- request.responseHandler = new Connection.ResponseHandler(request.connection, request);
1060+ request.responseHandler = new Connection.ResponseHandler(request.connection, -1, request);
10611061 request.connection.dispatcher.add(request.responseHandler);
10621062 return request;
10631063 }
added driver-core/src/test/java/com/datastax/driver/core/ReadTimeoutTest.java +61 −0
@@ -0,0 +1,61 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.core;
17+
18+import com.datastax.driver.core.exceptions.NoHostAvailableException;
19+import com.datastax.driver.core.exceptions.OperationTimedOutException;
20+import org.testng.annotations.BeforeMethod;
21+import org.testng.annotations.Test;
22+
23+import static org.assertj.core.api.Assertions.assertThat;
24+import static org.assertj.core.api.Assertions.fail;
25+import static org.scassandra.http.client.PrimingRequest.queryBuilder;
26+import static org.scassandra.http.client.PrimingRequest.then;
27+
28+public class ReadTimeoutTest extends ScassandraTestBase.PerClassCluster {
29+
30+ String query = "SELECT foo FROM bar";
31+
32+ @BeforeMethod(groups = "short")
33+ public void setup() {
34+ primingClient.prime(
35+ queryBuilder()
36+ .withQuery(query)
37+ .withThen(then().withFixedDelay(100L))
38+ .build()
39+ );
40+
41+ // Set default timeout too low
42+ cluster.getConfiguration().getSocketOptions().setReadTimeoutMillis(10);
43+ }
44+
45+ @Test(groups = "short")
46+ public void should_use_default_timeout_if_not_overridden_by_statement() {
47+ try {
48+ session.execute(query);
49+ fail("expected a timeout");
50+ } catch (NoHostAvailableException e) {
51+ Throwable t = e.getErrors().values().iterator().next();
52+ assertThat(t).isInstanceOf(OperationTimedOutException.class);
53+ }
54+ }
55+
56+ @Test(groups = "short")
57+ public void should_use_statement_timeout_if_overridden() {
58+ Statement statement = new SimpleStatement(query).setReadTimeoutMillis(200);
59+ session.execute(statement);
60+ }
61+}