kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 1108322f3c72dd3e4cdf9aa67f03fdfea23a88b4efd643136c47ad218cc62ef4 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-05-11 18:33 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-05-11 19:17 +02:00 |
| Parents | 1aa45c08eb45 |
1 file changed
modified features/pooling/README.md +74 −29
| @@ -11,7 +11,7 @@ | ||
| 11 | 11 | * the driver writes a request containing the stream id and the query on |
| 12 | 12 | the connection, and then proceeds without waiting for the response (if |
| 13 | 13 | 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]). | |
| 15 | 15 | Once the request has been written to the |
| 16 | 16 | connection, we say that it is *in flight*; |
| 17 | 17 | * at some point, Cassandra will send back a response on the connection. |
| @@ -19,21 +19,23 @@ | ||
| 19 | 19 | trigger a callback that will complete the corresponding query (this is |
| 20 | 20 | the point where your `ResultSetFuture` will get completed). |
| 21 | 21 | |
| 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**. | |
| 26 | 25 | |
| 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**. | |
| 32 | 34 | |
| 33 | 35 | ```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 | ++-------+ +-------+ +----+ +----------+ +-------+ | |
| 37 | 39 | ``` |
| 38 | 40 | |
| 39 | 41 | ### Configuring the connection pool |
| @@ -60,7 +62,7 @@ | ||
| 60 | 62 | // customize options... |
| 61 | 63 | ``` |
| 62 | 64 | |
| 63 | -#### Pool size | |
| 65 | +#### Pool size (protocol v2 only) | |
| 64 | 66 | |
| 65 | 67 | Connection pools have a variable size, which gets adjusted automatically |
| 66 | 68 | depending on the current load. There will always be at least a *core* |
| @@ -82,12 +84,30 @@ | ||
| 82 | 84 | [JAVA-662](https://datastax-oss.atlassian.net/browse/JAVA-662) will |
| 83 | 85 | address this issue. |
| 84 | 86 | |
| 85 | -[PoolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold][msrpct] | |
| 87 | +[PoolingOptions.setMaxSimultaneousRequestsPerConnectionThreshold][msrpct] | |
| 86 | 88 | has a slightly misleading name: it does not limit the number of requests per |
| 87 | 89 | connection, but only determines the threshold that triggers the creation |
| 88 | 90 | of a new connection when the pool is not at its maximum capacity. In |
| 89 | 91 | general, you shouldn't have to change its default value. |
| 90 | 92 | |
| 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 | +``` | |
| 91 | 111 | |
| 92 | 112 | #### Heartbeat |
| 93 | 113 | |
| @@ -151,16 +171,37 @@ | ||
| 151 | 171 | requests, and maximum capacity for each host, every 5 seconds: |
| 152 | 172 | |
| 153 | 173 | ```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 | + | |
| 154 | 193 | ScheduledExecutorService scheduled = Executors.newScheduledThreadPool(1); |
| 155 | 194 | scheduled.scheduleAtFixedRate(new Runnable() { |
| 156 | 195 | @Override |
| 157 | 196 | public void run() { |
| 158 | 197 | Session.State state = session.getState(); |
| 159 | 198 | for (Host host : state.getConnectedHosts()) { |
| 199 | + HostDistance distance = loadBalancingPolicy.distance(host); | |
| 160 | 200 | int connections = state.getOpenConnections(host); |
| 161 | 201 | int inFlightQueries = state.getInFlightQueries(host); |
| 162 | 202 | 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)); | |
| 164 | 205 | } |
| 165 | 206 | } |
| 166 | 207 | }, 5, 5, TimeUnit.SECONDS); |
| @@ -172,16 +213,20 @@ | ||
| 172 | 213 | |
| 173 | 214 | If you find that the current load stays close or equal to the maximum |
| 174 | 215 | 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() |