kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 434273cc91aeeddfd5c54426cba2767dc04ec92ce9af8767418f5d7418b85219 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-01-14 18:22 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-25 15:30 +01:00 |
| Parents | bcf5d9263751 |
7 files changed
modified changelog/README.md +1 −0
| @@ -5,6 +5,7 @@ | ||
| 5 | 5 | - [bug] JAVA-1034: fix metadata parser for collections of custom types. |
| 6 | 6 | - [improvement] JAVA-1035: Expose host broadcast_address and listen_address if available. |
| 7 | 7 | - [new feature] JAVA-1037: Allow named parameters in simple statements. |
| 8 | +- [improvement] JAVA-1033: Allow per-statement read timeout. | |
| 8 | 9 | |
| 9 | 10 | Merged from 2.1 branch: |
| 10 | 11 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +7 −6
| @@ -489,14 +489,14 @@ | ||
| 489 | 489 | } |
| 490 | 490 | |
| 491 | 491 | ResponseHandler write(ResponseCallback callback) throws ConnectionException, BusyConnectionException { |
| 492 | - return write(callback, true); | |
| 492 | + return write(callback, -1, true); | |
| 493 | 493 | } |
| 494 | 494 | |
| 495 | - ResponseHandler write(ResponseCallback callback, boolean startTimeout) throws ConnectionException, BusyConnectionException { | |
| 495 | + ResponseHandler write(ResponseCallback callback, long readTimeoutMillis, boolean startTimeout) throws ConnectionException, BusyConnectionException { | |
| 496 | 496 | |
| 497 | 497 | Message.Request request = callback.request(); |
| 498 | 498 | |
| 499 | - ResponseHandler handler = new ResponseHandler(this, callback); | |
| 499 | + ResponseHandler handler = new ResponseHandler(this, readTimeoutMillis, callback); | |
| 500 | 500 | dispatcher.add(handler); |
| 501 | 501 | request.setStreamId(handler.streamId); |
| 502 | 502 | |
| @@ -1213,14 +1213,16 @@ | ||
| 1213 | 1213 | final int streamId; |
| 1214 | 1214 | final ResponseCallback callback; |
| 1215 | 1215 | final int retryCount; |
| 1216 | + private final long readTimeoutMillis; | |
| 1216 | 1217 | |
| 1217 | 1218 | private final long startTime; |
| 1218 | 1219 | private volatile Timeout timeout; |
| 1219 | 1220 | |
| 1220 | 1221 | private final AtomicBoolean isCancelled = new AtomicBoolean(); |
| 1221 | 1222 | |
| 1222 | - ResponseHandler(Connection connection, ResponseCallback callback) throws BusyConnectionException { | |
| 1223 | + ResponseHandler(Connection connection, long readTimeoutMillis, ResponseCallback callback) throws BusyConnectionException { | |
| 1223 | 1224 | this.connection = connection; |
| 1225 | + this.readTimeoutMillis = (readTimeoutMillis > 0) ? readTimeoutMillis : connection.factory.getReadTimeoutMillis(); | |
| 1224 | 1226 | this.streamId = connection.dispatcher.streamIdHandler.next(); |
| 1225 | 1227 | if (streamId == -1) |
| 1226 | 1228 | throw new BusyConnectionException(connection.address); |
| @@ -1231,8 +1233,7 @@ | ||
| 1231 | 1233 | } |
| 1232 | 1234 | |
| 1233 | 1235 | 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); | |
| 1236 | 1237 | } |
| 1237 | 1238 | |
| 1238 | 1239 | void cancelTimeout() { |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +1 −1
| @@ -349,7 +349,7 @@ | ||
| 349 | 349 | break; |
| 350 | 350 | } |
| 351 | 351 | |
| 352 | - connectionHandler = connection.write(responseCallback, false); | |
| 352 | + connectionHandler = connection.write(responseCallback, statement.getReadTimeoutMillis(), false); | |
| 353 | 353 | // Only start the timeout when we're sure connectionHandler is set. This avoids an edge case where onTimeout() was triggered |
| 354 | 354 | // *before* the call to connection.write had returned. |
| 355 | 355 | connectionHandler.startTimeout(); |
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +26 −0
| @@ -63,6 +63,7 @@ | ||
| 63 | 63 | private volatile boolean traceQuery; |
| 64 | 64 | private volatile int fetchSize; |
| 65 | 65 | private volatile long defaultTimestamp = Long.MIN_VALUE; |
| 66 | + private volatile long readTimeoutMillis = Long.MIN_VALUE; | |
| 66 | 67 | private volatile RetryPolicy retryPolicy; |
| 67 | 68 | private volatile ByteBuffer pagingState; |
| 68 | 69 | protected volatile Boolean idempotent; |
| @@ -321,6 +322,31 @@ | ||
| 321 | 322 | return defaultTimestamp; |
| 322 | 323 | } |
| 323 | 324 | |
| 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 | + | |
| 324 | 350 | /** |
| 325 | 351 | * Sets the paging state. |
| 326 | 352 | * <p/> |
modified driver-core/src/main/java/com/datastax/driver/core/StatementWrapper.java +74 −18
| @@ -20,6 +20,7 @@ | ||
| 20 | 20 | import com.datastax.driver.core.policies.SpeculativeExecutionPolicy; |
| 21 | 21 | |
| 22 | 22 | import java.nio.ByteBuffer; |
| 23 | +import java.util.Map; | |
| 23 | 24 | |
| 24 | 25 | /** |
| 25 | 26 | * Base class for custom {@link Statement} implementations that wrap another statement. |
| @@ -81,8 +82,8 @@ | ||
| 81 | 82 | } |
| 82 | 83 | |
| 83 | 84 | @Override |
| 84 | - public Statement disableTracing() { | |
| 85 | - return wrapped.disableTracing(); | |
| 85 | + public ConsistencyLevel getConsistencyLevel() { | |
| 86 | + return wrapped.getConsistencyLevel(); | |
| 86 | 87 | } |
| 87 | 88 | |
| 88 | 89 | @Override |
| @@ -90,14 +91,19 @@ | ||
| 90 | 91 | return wrapped.setSerialConsistencyLevel(serialConsistency); |
| 91 | 92 | } |
| 92 | 93 | |
| 94 | + @Override | |
| 95 | + public ConsistencyLevel getSerialConsistencyLevel() { | |
| 96 | + return wrapped.getSerialConsistencyLevel(); | |
| 97 | + } | |
| 98 | + | |
| 93 | 99 | @Override |
| 94 | 100 | public Statement enableTracing() { |
| 95 | 101 | return wrapped.enableTracing(); |
| 96 | 102 | } |
| 97 | 103 | |
| 98 | 104 | @Override |
| 99 | - public ByteBuffer getPagingState() { | |
| 100 | - return wrapped.getPagingState(); | |
| 105 | + public Statement disableTracing() { | |
| 106 | + return wrapped.disableTracing(); | |
| 101 | 107 | } |
| 102 | 108 | |
| 103 | 109 | @Override |
| @@ -106,13 +112,13 @@ | ||
| 106 | 112 | } |
| 107 | 113 | |
| 108 | 114 | @Override |
| 109 | - public RetryPolicy getRetryPolicy() { | |
| 110 | - return wrapped.getRetryPolicy(); | |
| 115 | + public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 116 | + return wrapped.getRoutingKey(protocolVersion, codecRegistry); | |
| 111 | 117 | } |
| 112 | 118 | |
| 113 | 119 | @Override |
| 114 | - public ByteBuffer getRoutingKey(ProtocolVersion protocolVersion, CodecRegistry codecRegistry) { | |
| 115 | - return wrapped.getRoutingKey(protocolVersion, codecRegistry); | |
| 120 | + public String getKeyspace() { | |
| 121 | + return wrapped.getKeyspace(); | |
| 116 | 122 | } |
| 117 | 123 | |
| 118 | 124 | @Override |
| @@ -121,8 +127,38 @@ | ||
| 121 | 127 | } |
| 122 | 128 | |
| 123 | 129 | @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(); | |
| 126 | 162 | } |
| 127 | 163 | |
| 128 | 164 | @Override |
| @@ -131,22 +167,42 @@ | ||
| 131 | 167 | } |
| 132 | 168 | |
| 133 | 169 | @Override |
| 134 | - public ConsistencyLevel getSerialConsistencyLevel() { | |
| 135 | - return wrapped.getSerialConsistencyLevel(); | |
| 170 | + public Statement setPagingState(PagingState pagingState) { | |
| 171 | + return wrapped.setPagingState(pagingState); | |
| 136 | 172 | } |
| 137 | 173 | |
| 138 | 174 | @Override |
| 139 | - public String getKeyspace() { | |
| 140 | - return wrapped.getKeyspace(); | |
| 175 | + public Statement setPagingStateUnsafe(byte[] pagingState) { | |
| 176 | + return wrapped.setPagingStateUnsafe(pagingState); | |
| 141 | 177 | } |
| 142 | 178 | |
| 143 | 179 | @Override |
| 144 | - public int getFetchSize() { | |
| 145 | - return wrapped.getFetchSize(); | |
| 180 | + public ByteBuffer getPagingState() { | |
| 181 | + return wrapped.getPagingState(); | |
| 146 | 182 | } |
| 147 | 183 | |
| 148 | 184 | @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); | |
| 151 | 207 | } |
| 152 | 208 | } |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +1 −1
| @@ -1057,7 +1057,7 @@ | ||
| 1057 | 1057 | static MockRequest send(HostConnectionPool pool) throws ConnectionException, BusyConnectionException, TimeoutException { |
| 1058 | 1058 | // Create a MockRequest and spy on it. Create a response handler and add it to the connection's dispatcher. |
| 1059 | 1059 | 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); | |
| 1061 | 1061 | request.connection.dispatcher.add(request.responseHandler); |
| 1062 | 1062 | return request; |
| 1063 | 1063 | } |
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 | +} |