Elffuss BraidHub

kikocis/java-driver public

3.1.x · 6.9 KiBRenderedRawPermalink
1## Socket options
2
3[SocketOptions] controls various low-level parameters related to TCP connections between the driver and Cassandra.
4
5You can provide these when initializing the cluster:
6
7```java
8Cluster cluster = Cluster.builder()
9 .addContactPoint("127.0.0.1")
10 .withSocketOptions(
11 new SocketOptions()
12 .setConnectTimeoutMillis(2000))
13 .build();
14```
15
16If you don't, the driver uses a default configuration (see the javadocs of the setter methods of [SocketOptions] for the
17default values).
18
19You can retrieve and change the options at runtime:
20
21```java
22SocketOptions socketOptions = cluster.getConfiguration().getSocketOptions();
23socketOptions.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
33giving up.
34
35Other options control the usual low-level TCP parameters (refer to their individual javadoc for details):
36[setKeepAlive], [setReceiveBufferSize], [setReuseAddress], [setSendBufferSize], [setSoLinger], [setTcpNoDelay]. Most of
37these options are not set explicitly by the driver, so they get the default value from the underlying TCP transport.
38One exception is `setTcpNoDelay`, which is forced to `true` (meaning that Nagle's algorithm is *disabled* for driver
39connections).
40
41### Driver read timeout
42
43[setReadTimeoutMillis] controls how long the driver waits for a response *from a given Cassandra node* before
44considering it unresponsive.
45
46Cassandra 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
50counter_write_request_timeout_in_ms: 5000
51range_request_timeout_in_ms: 10000
52read_request_timeout_in_ms: 5000
53request_timeout_in_ms: 10000
54truncate_request_timeout_in_ms: 60000
55write_request_timeout_in_ms: 2000
56```
57
58The goal of `setReadTimeoutMillis` is to give up on a node if it took longer than these thresholds to reply, on the
59assumption that there's probably something wrong with it. Therefore it should be set **higher than the server-side
60timeouts**.
61
62The 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
64timeout on a specific statement:
65
66```java
67session.execute(
68 new SimpleStatement("TRUNCATE tmp").setReadTimeoutMillis(65000));
69```
70
71Do not set the read timeout too low, or the driver might give up on requests that had a chance of succeeding.
72
73If 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
79It should be clear by now that `setReadTimeoutMillis` is *per node*, not per query. If the driver retries on 4 different
80nodes, the overall execution time could theoretically be up to 4 times the read timeout. If you want a per query timeout,
81use the following pattern:
82
83```java
84import com.google.common.base.Throwables;
85
86ResultSet 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
100A complementary approach is to enable [speculative executions](../speculative_execution/), to have the driver query
101multiple 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
105Unfortunately, the term "read timeout" clashes with another concept that is not directly related: a Cassandra node may
106reply with a [READ_TIMEOUT](../retries/#on-read-timeout) error when it didn't hear back from enough replicas during a
107read query.
108
109To 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
118We 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