Elffuss BraidHub

kikocis/java-driver public

Update pooling doc for protocol v3.
Changeset1108322f3c72dd3e4cdf9aa67f03fdfea23a88b4efd643136c47ad218cc62ef4
Authorolim7t <omichallat+github@gmail.com> · 2015-05-11 18:33 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2015-05-11 19:17 +02:00
Parents1aa45c08eb45

Browse the files at this changeset

1 file changed

modified features/pooling/README.md +74 −29
@@ -11,7 +11,7 @@
1111 * the driver writes a request containing the stream id and the query on
1212 the connection, and then proceeds without waiting for the response (if
1313 you're using the asynchronous API, this is when the driver will send you
14- back a [ResultSetFuture][result_set_future]).
14+ back a [ResultSetFuture][result_set_future]).
1515 Once the request has been written to the
1616 connection, we say that it is *in flight*;
1717 * at some point, Cassandra will send back a response on the connection.
@@ -19,21 +19,23 @@
1919 trigger a callback that will complete the corresponding query (this is
2020 the point where your `ResultSetFuture` will get completed).
2121
22-Version 2.0.x of the driver uses version 1 of the binary protocol when
23-connecting to Cassandra 1.2, and version 2 when connecting to Cassandra
24-2.0 or higher. In both cases, that gives you **128 stream ids per
25-connection**.
22+To increase the number of concurrent requests, we use a pool of
23+connections to each host. **For each `Session` object, there is one
24+connection pool per connected host**.
2625
27-Because the number of stream ids per connection is limited, we use a
28-pool with multiple connections to each host. **For each `Session`
29-object, there is one connection pool per connected host**. The number of
30-connections per pool depends on the configuration, this will be
31-described in the next section.
26+The number of connections and stream ids depends on the [native protocol
27+version](../native_protocol/):
28+
29+* **protocol v2 or below**: there are **128 stream ids per connection.**
30+ The number of connections per pool is configurable (this will be
31+ described in the next section).
32+* v3 or above: there are **up to 32768 stream ids per connection**, and
33+ **1 connection per pool**.
3234
3335 ```ditaa
34-+-------+1 n+-------+1 n+----+1 n+----------+1 128+-------+
35-|Cluster+-----+Session+-----+Pool+-----+Connection+-------+Request+
36-+-------+ +-------+ +----+ +----------+ +-------+
36++-------+1 n+-------+1 n+----+1 n/1+----------+1 128/32K+-------+
37+|Cluster+-----+Session+-----+Pool+-------+Connection+-----------+Request+
38++-------+ +-------+ +----+ +----------+ +-------+
3739 ```
3840
3941 ### Configuring the connection pool
@@ -60,7 +62,7 @@
6062 // customize options...
6163 ```
6264
63-#### Pool size
65+#### Pool size (protocol v2 only)
6466
6567 Connection pools have a variable size, which gets adjusted automatically
6668 depending on the current load. There will always be at least a *core*
@@ -82,12 +84,30 @@
8284 [JAVA-662](https://datastax-oss.atlassian.net/browse/JAVA-662) will
8385 address this issue.
8486
85-[PoolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold][msrpct]
87+[PoolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold][msrpct]
8688 has a slightly misleading name: it does not limit the number of requests per
8789 connection, but only determines the threshold that triggers the creation
8890 of a new connection when the pool is not at its maximum capacity. In
8991 general, you shouldn't have to change its default value.
9092
93+#### Number of simultaneous requests per host (protocol v3 only)
94+
95+Protocol v3 uses a single connection per host. While the number of
96+simultaneous requests can go up to 32768, it is limited by
97+[PoolingOptions.setMaxSimultaneousRequestsPerHostThreshold][msrpht]. The
98+goal of this option is to throttle the load that each client can
99+generate on the server. By default, it is set to 1024 for local hosts,
100+and 256 for remote hosts (this was chosen to match the capacity of a v2
101+pool with the default configuration).
102+
103+If your performance tests show that your cluster can handle a higher
104+load, you can raise this threshold:
105+
106+```java
107+poolingOptions
108+ .setMaxSimultaneousRequestsPerHostThreshold(HostDistance.LOCAL, 20000)
109+ .setMaxSimultaneousRequestsPerHostThreshold(HostDistance.REMOTE, 1000);
110+```
91111
92112 #### Heartbeat
93113
@@ -151,16 +171,37 @@
151171 requests, and maximum capacity for each host, every 5 seconds:
152172
153173 ```java
174+final LoadBalancingPolicy loadBalancingPolicy =
175+ cluster.getConfiguration().getPolicies().getLoadBalancingPolicy();
176+PoolingOptions poolingOptions = cluster.getConfiguration().getPoolingOptions();
177+ProtocolOptions protocolOptions = cluster.getConfiguration().getProtocolOptions();
178+
179+final Map<HostDistance, Integer> requestsPerConnection;
180+ProtocolVersion protocolVersion = protocolOptions.getProtocolVersionEnum();
181+if (protocolVersion == ProtocolVersion.V3) {
182+ requestsPerConnection = ImmutableMap.of(
183+ HostDistance.LOCAL,
184+ poolingOptions.getMaxSimultaneousRequestsPerHostThreshold(HostDistance.LOCAL),
185+ HostDistance.REMOTE,
186+ poolingOptions.getMaxSimultaneousRequestsPerHostThreshold(HostDistance.REMOTE));
187+} else {
188+ requestsPerConnection = ImmutableMap.of(
189+ HostDistance.LOCAL, 128,
190+ HostDistance.REMOTE, 128);
191+}
192+
154193 ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1);
155194 scheduled.scheduleAtFixedRate(new Runnable() {
156195 @Override
157196 public void run() {
158197 Session.State state = session.getState();
159198 for (Host host : state.getConnectedHosts()) {
199+ HostDistance distance = loadBalancingPolicy.distance(host);
160200 int connections = state.getOpenConnections(host);
161201 int inFlightQueries = state.getInFlightQueries(host);
162202 System.out.printf("%s connections=%d current load=%d max load=%d%n",
163- host, connections, inFlightQueries, connections * 128);
203+ host, connections, inFlightQueries,
204+ connections * requestsPerConnection.get(distance));
164205 }
165206 }
166207 }, 5, 5, TimeUnit.SECONDS);
@@ -172,16 +213,20 @@
172213
173214 If you find that the current load stays close or equal to the maximum
174215 load at all time, it's a sign that your connection pools are saturated
175-and you should raise the max connections per host. On the other hand, if
176-the load is often less than core * 128, your pools are underused and you
177-could get away with less core connections.
178-
179-[result_set_future]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/ResultSetFuture.html
180-[pooling_options]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/PoolingOptions.html
181-[lbp]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/policies/LoadBalancingPolicy.html
182-[msrpct]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/PoolingOptions.html#setMaxSimultaneousRequestsPerConnectionThreshold(com.datastax.driver.core.HostDistance,%20int)
183-[rtm]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/SocketOptions.html#getReadTimeoutMillis()
184-[exec_async]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Session.html#executeAsync(com.datastax.driver.core.Statement)
185-[ptm]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/PoolingOptions.html#setPoolTimeoutMillis(int)
186-[nhae]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/exceptions/NoHostAvailableException.html
187-[get_state]:http://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Session.html#getState()
No newline at the end of the file
216+and you should raise the max connections per host (protocol v2) or max
217+requests per host (protocol v3).
218+
219+If you're using protocol v2 and the load is often less than core * 128,
220+your pools are underused and you could get away with less core
221+connections.
222+
223+[result_set_future]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/ResultSetFuture.html
224+[pooling_options]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PoolingOptions.html
225+[lbp]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/policies/LoadBalancingPolicy.html
226+[msrpct]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PoolingOptions.html#setMaxSimultaneousRequestsPerConnectionThreshold(com.datastax.driver.core.HostDistance,%20int)
227+[msrpht]: http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PoolingOptions.html#setMaxSimultaneousRequestsPerHostThreshold(com.datastax.driver.core.HostDistance,%20int)
228+[rtm]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/SocketOptions.html#getReadTimeoutMillis()
229+[exec_async]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html#executeAsync(com.datastax.driver.core.Statement)
230+[ptm]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/PoolingOptions.html#setPoolTimeoutMillis(int)
231+[nhae]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/exceptions/NoHostAvailableException.html
232+[get_state]:http://docs.datastax.com/en/drivers/java/2.1/com/datastax/driver/core/Session.html#getState()