| 1 | ## Socket options |
| 2 | |
| 3 | [SocketOptions] controls various low-level parameters related to TCP connections between the driver and Cassandra. |
| 4 | |
| 5 | You can provide these when initializing the cluster: |
| 6 | |
| 7 | ```java |
| 8 | Cluster cluster = Cluster.builder() |
| 9 | .addContactPoint("127.0.0.1") |
| 10 | .withSocketOptions( |
| 11 | new SocketOptions() |
| 12 | .setConnectTimeoutMillis(2000)) |
| 13 | .build(); |
| 14 | ``` |
| 15 | |
| 16 | If you don't, the driver uses a default configuration (see the javadocs of the setter methods of [SocketOptions] for the |
| 17 | default values). |
| 18 | |
| 19 | You can retrieve and change the options at runtime: |
| 20 | |
| 21 | ```java |
| 22 | SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions(); |
| 23 | socketOptions.setConnectTimeoutMillis(3000); |
| 24 | ``` |
| 25 | |
| 26 | * changes to the [read timeout](#driver-read-timeout) will be taken into account for future request executions; |
| 27 | * changes to any other option will be taken into account for future connections (connections that were already opened at |
| 28 | the time of the change are unaffected, they keep the old values). |
| 29 | |
| 30 | ### TCP options |
| 31 | |
| 32 | [setConnectTimeoutMillis] defines how long the driver waits to establish a new connection to a Cassandra node before |
| 33 | giving up. |
| 34 | |
| 35 | Other options control the usual low-level TCP parameters (refer to their individual javadoc for details): |
| 36 | [setKeepAlive], [setReceiveBufferSize], [setReuseAddress], [setSendBufferSize], [setSoLinger], [setTcpNoDelay]. Most of |
| 37 | these options are not set explicitly by the driver, so they get the default value from the underlying TCP transport. |
| 38 | One exception is `setTcpNoDelay`, which is forced to `true` (meaning that Nagle's algorithm is *disabled* for driver |
| 39 | connections). |
| 40 | |
| 41 | ### Driver read timeout |
| 42 | |
| 43 | [setReadTimeoutMillis] controls how long the driver waits for a response *from a given Cassandra node* before |
| 44 | considering it unresponsive. |
| 45 | |
| 46 | Cassandra normally provides a guaranteed response time for each type of query, as shown by these parameters in |
| 47 | `cassandra.yaml` (here with their default values): |
| 48 | |
| 49 | ```yaml |
| 50 | counter_write_request_timeout_in_ms: 5000 |
| 51 | range_request_timeout_in_ms: 10000 |
| 52 | read_request_timeout_in_ms: 5000 |
| 53 | request_timeout_in_ms: 10000 |
| 54 | truncate_request_timeout_in_ms: 60000 |
| 55 | write_request_timeout_in_ms: 2000 |
| 56 | ``` |
| 57 | |
| 58 | The goal of `setReadTimeoutMillis` is to give up on a node if it took longer than these thresholds to reply, on the |
| 59 | assumption that there's probably something wrong with it. Therefore it should be set **higher than the server-side |
| 60 | timeouts**. |
| 61 | |
| 62 | The default value is **12 seconds**. For truncate queries (where the server timeout is 60 seconds) or aggregate queries |
| 63 | (where `read_request_timeout_in_ms` applies per page of processed data, not to the whole query), you can increase the |
| 64 | timeout on a specific statement: |
| 65 | |
| 66 | ```java |
| 67 | session.execute( |
| 68 | new SimpleStatement("TRUNCATE tmp").setReadTimeoutMillis(65000)); |
| 69 | ``` |
| 70 | |
| 71 | Do not set the read timeout too low, or the driver might give up on requests that had a chance of succeeding. |
| 72 | |
| 73 | If the timeout is reached, the driver will receive an [OperationTimedOutException], and invoke [onRequestError] on the |
| 74 | [retry policy](../retries/) to decide what to do (the default is to retry on the next node in the |
| 75 | [query plan](../load_balancing/#query-plan)). |
| 76 | |
| 77 | #### Limiting overall query time |
| 78 | |
| 79 | It should be clear by now that `setReadTimeoutMillis` is *per node*, not per query. If the driver retries on 4 different |
| 80 | nodes, the overall execution time could theoretically be up to 4 times the read timeout. If you want a per query timeout, |
| 81 | use the following pattern: |
| 82 | |
| 83 | ```java |
| 84 | import com.google.common.base.Throwables; |
| 85 | |
| 86 | ResultSet execute(Statement statement, long timeout, TimeUnit unit) |
| 87 | throws InterruptedException, TimeoutException { |
| 88 | ResultSetFuture future = session.executeAsync(statement); |
| 89 | try { |
| 90 | return future.get(timeout, unit); |
| 91 | } catch (ExecutionException e) { |
| 92 | throw Throwables.propagate(e.getCause()); |
| 93 | } catch (TimeoutException e) { |
| 94 | future.cancel(true); |
| 95 | throw e; |
| 96 | } |
| 97 | } |
| 98 | ``` |
| 99 | |
| 100 | A complementary approach is to enable [speculative executions](../speculative_execution/), to have the driver query |
| 101 | multiple nodes in parallel. This way you won't have to wait for the full timeout if the first node is unresponsive. |
| 102 | |
| 103 | #### Driver read timeout vs. server read timeout |
| 104 | |
| 105 | Unfortunately, the term "read timeout" clashes with another concept that is not directly related: a Cassandra node may |
| 106 | reply with a [READ_TIMEOUT](../retries/#on-read-timeout) error when it didn't hear back from enough replicas during a |
| 107 | read query. |
| 108 | |
| 109 | To clarify: |
| 110 | |
| 111 | * **driver read timeout:** the driver did not receive any response from the current coordinator within |
| 112 | `SocketOptions.setReadTimeoutMillis`. It invokes [onRequestError] on the [retry policy](../retries/) with an |
| 113 | [OperationTimedOutException] to decide what to do. |
| 114 | * **server read timeout:** the driver *did* receive a response, but that response indicates that the coordinator timed |
| 115 | out while waiting for other replicas. It invokes [onReadTimeout] on the [retry policy](../retries/) to decide what to |
| 116 | do. |
| 117 | |
| 118 | We might rename `SocketOptions.setReadTimeoutMillis` in a future version to clear up any confusion. |
| 119 | |
| 120 | [SocketOptions]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html |
| 121 | [setReadTimeoutMillis]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setReadTimeoutMillis-int- |
| 122 | [setConnectTimeoutMillis]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setConnectTimeoutMillis-int- |
| 123 | [setKeepAlive]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setKeepAlive-boolean- |
| 124 | [setReceiveBufferSize]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setReceiveBufferSize-int- |
| 125 | [setReuseAddress]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setReuseAddress-boolean- |
| 126 | [setSendBufferSize]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setSendBufferSize-int- |
| 127 | [setSoLinger]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setSoLinger-int- |
| 128 | [setTcpNoDelay]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#setTcpNoDelay-boolean- |
| 129 | [onReadTimeout]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/RetryPolicy.html#onReadTimeout-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-int-int-boolean-int- |
| 130 | [onRequestError]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/RetryPolicy.html#onRequestError-com.datastax.driver.core.Statement-com.datastax.driver.core.ConsistencyLevel-com.datastax.driver.core.exceptions.DriverException-int- |
| 131 | [OperationTimedOutException]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/exceptions/OperationTimedOutException.html |