Elffuss BraidHub

kikocis/java-driver public

Merge pull request #650 from datastax/java1154

JAVA-1154: Allow individual Statement to cancel the read timeout.
Changeset7fc038fde76e9504a4888b53d864377f3bf7a7d3e04ef2c505070984bcf72361
AuthorOlivier Michallat <olim7t@users.noreply.github.com> · 2016-04-19 12:00 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-04-19 12:00 +02:00
Parentsd4e62c9f03b8, 51a5bab7c4ed

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -5,6 +5,7 @@
55 - [bug] JAVA-1132: Executing bound statement with no variables results in exception with protocol v1.
66 - [improvement] JAVA-1040: SimpleStatement parameters support in QueryLogger.
77 - [improvement] JAVA-1151: Fail fast if HdrHistogram is not in the classpath.
8+- [improvement] JAVA-1154: Allow individual Statement to cancel the read timeout.
89
910 Merged from 2.1 branch:
1011
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +4 −4
@@ -506,11 +506,11 @@
506506 return write(callback, -1, true);
507507 }
508508
509- ResponseHandler write(ResponseCallback callback, long readTimeoutMillis, boolean startTimeout) throws ConnectionException, BusyConnectionException {
509+ ResponseHandler write(ResponseCallback callback, long statementReadTimeoutMillis, boolean startTimeout) throws ConnectionException, BusyConnectionException {
510510
511511 Message.Request request = callback.request();
512512
513- ResponseHandler handler = new ResponseHandler(this, readTimeoutMillis, callback);
513+ ResponseHandler handler = new ResponseHandler(this, statementReadTimeoutMillis, callback);
514514 dispatcher.add(handler);
515515 request.setStreamId(handler.streamId);
516516
@@ -1228,9 +1228,9 @@
12281228
12291229 private final AtomicBoolean isCancelled = new AtomicBoolean();
12301230
1231- ResponseHandler(Connection connection, long readTimeoutMillis, ResponseCallback callback) throws BusyConnectionException {
1231+ ResponseHandler(Connection connection, long statementReadTimeoutMillis, ResponseCallback callback) throws BusyConnectionException {
12321232 this.connection = connection;
1233- this.readTimeoutMillis = (readTimeoutMillis > 0) ? readTimeoutMillis : connection.factory.getReadTimeoutMillis();
1233+ this.readTimeoutMillis = (statementReadTimeoutMillis >= 0) ? statementReadTimeoutMillis : connection.factory.getReadTimeoutMillis();
12341234 this.streamId = connection.dispatcher.streamIdHandler.next();
12351235 if (streamId == -1)
12361236 throw new BusyConnectionException(connection.address);
modified driver-core/src/main/java/com/datastax/driver/core/SocketOptions.java +2 −4
@@ -123,11 +123,9 @@
123123 * </li>
124124 * </ul>
125125 * <p/>
126- * Setting a value of 0 disables read timeouts.
127- * <p/>
128- * The default value is {@link #DEFAULT_READ_TIMEOUT_MILLIS}.
126+ * If you don't call this method, the default value is {@link #DEFAULT_READ_TIMEOUT_MILLIS}.
129127 *
130- * @param readTimeoutMillis the timeout to set.
128+ * @param readTimeoutMillis the timeout to set. If it is less than or equal to 0, read timeouts are disabled.
131129 * @return this {@code SocketOptions}.
132130 */
133131 public SocketOptions setReadTimeoutMillis(int readTimeoutMillis) {
modified driver-core/src/main/java/com/datastax/driver/core/Statement.java +4 −1
@@ -19,6 +19,7 @@
1919 import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException;
2020 import com.datastax.driver.core.policies.RetryPolicy;
2121 import com.datastax.driver.core.querybuilder.BuiltStatement;
22+import com.google.common.base.Preconditions;
2223 import com.google.common.collect.ImmutableMap;
2324
2425 import java.nio.ByteBuffer;
@@ -330,10 +331,12 @@
330331 * You should override this only for statements for which the coordinator may allow a longer server-side
331332 * timeout (for example aggregation queries).
332333 *
333- * @param readTimeoutMillis the timeout to set. Must be greater than 0 (or the default will be used).
334+ * @param readTimeoutMillis the timeout to set. Negative values are not allowed. If it is 0, the read timeout will
335+ * be disabled for this statement.
334336 * @return this {@code Statement} object.
335337 */
336338 public Statement setReadTimeoutMillis(int readTimeoutMillis) {
339+ Preconditions.checkArgument(readTimeoutMillis >= 0, "read timeout must be >= 0");
337340 this.readTimeoutMillis = readTimeoutMillis;
338341 return this;
339342 }
modified driver-core/src/test/java/com/datastax/driver/core/ReadTimeoutTest.java +6 −0
@@ -58,4 +58,10 @@
5858 Statement statement = new SimpleStatement(query).setReadTimeoutMillis(200);
5959 session.execute(statement);
6060 }
61+
62+ @Test(groups = "short")
63+ public void should_disable_timeout_if_set_to_zero_at_statement_level() {
64+ Statement statement = new SimpleStatement(query).setReadTimeoutMillis(0);
65+ session.execute(statement);
66+ }
6167 }