kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f3d4bfbe8473805fbd16aa174d1d7705bbe0f2dbf11310ea2d1c3e43c8790790 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2014-10-22 21:13 +02:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2014-10-23 16:41 +02:00 |
| Parents | 9eb596ab550a |
5 files changed
modified driver-core/CHANGELOG.rst +1 −0
| @@ -25,6 +25,7 @@ | ||
| 25 | 25 | - [improvement] Throw an exception when DCAwareRoundRobinPolicy is built with |
| 26 | 26 | an explicit but null or empty local datacenter (JAVA-451). |
| 27 | 27 | - [bug] Fix check for local contact points in DCAware policy's init (JAVA-511) |
| 28 | +- [improvement] Make timeout on saturated pool customizable (JAVA-457) | |
| 28 | 29 | |
| 29 | 30 | |
| 30 | 31 | 2.0.6: |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +3 −0
| @@ -199,6 +199,9 @@ | ||
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | private PooledConnection waitForConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { |
| 202 | + if (timeout == 0) | |
| 203 | + throw new TimeoutException(); | |
| 204 | + | |
| 202 | 205 | long start = System.nanoTime(); |
| 203 | 206 | long remaining = timeout; |
| 204 | 207 | do { |
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +31 −0
| @@ -52,6 +52,7 @@ | ||
| 52 | 52 | private static final int DEFAULT_MAX_POOL_REMOTE = 2; |
| 53 | 53 | |
| 54 | 54 | private static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120; |
| 55 | + private static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000; | |
| 55 | 56 | |
| 56 | 57 | private volatile Cluster.Manager manager; |
| 57 | 58 | |
| @@ -62,6 +63,7 @@ | ||
| 62 | 63 | private final int[] maxConnections = new int[] { DEFAULT_MAX_POOL_LOCAL , DEFAULT_MAX_POOL_REMOTE, 0 }; |
| 63 | 64 | |
| 64 | 65 | private volatile int idleTimeoutSeconds = DEFAULT_IDLE_TIMEOUT_SECONDS; |
| 66 | + private volatile int poolTimeoutMillis = DEFAULT_POOL_TIMEOUT_MILLIS; | |
| 65 | 67 | |
| 66 | 68 | public PoolingOptions() {} |
| 67 | 69 | |
| @@ -248,6 +250,35 @@ | ||
| 248 | 250 | return this; |
| 249 | 251 | } |
| 250 | 252 | |
| 253 | + /** | |
| 254 | + * Returns the timeout when trying to acquire a connection from a host's pool. | |
| 255 | + * | |
| 256 | + * @return the timeout. | |
| 257 | + */ | |
| 258 | + public int getPoolTimeoutMillis() { | |
| 259 | + return poolTimeoutMillis; | |
| 260 | + } | |
| 261 | + | |
| 262 | + /** | |
| 263 | + * Sets the timeout when trying to acquire a connection from a host's pool. | |
| 264 | + * <p> | |
| 265 | + * If no connection is available within that time, the driver will try the | |
| 266 | + * next host from the query plan. | |
| 267 | + * <p> | |
| 268 | + * If this option is set to zero, the driver won't wait at all. | |
| 269 | + * | |
| 270 | + * @param poolTimeoutMillis the new value in milliseconds. | |
| 271 | + * @return this {@code PoolingOptions} | |
| 272 | + * | |
| 273 | + * @throws IllegalArgumentException if the timeout is negative. | |
| 274 | + */ | |
| 275 | + public PoolingOptions setPoolTimeoutMillis(int poolTimeoutMillis) { | |
| 276 | + if (poolTimeoutMillis < 0) | |
| 277 | + throw new IllegalArgumentException("Pool timeout must be positive"); | |
| 278 | + this.poolTimeoutMillis = poolTimeoutMillis; | |
| 279 | + return this; | |
| 280 | + } | |
| 281 | + | |
| 251 | 282 | /** |
| 252 | 283 | * Requests the driver to re-evaluate the {@link HostDistance} (through the configured |
| 253 | 284 | * {@link com.datastax.driver.core.policies.LoadBalancingPolicy#distance}) for every known |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +2 −5
| @@ -113,9 +113,7 @@ | ||
| 113 | 113 | |
| 114 | 114 | PooledConnection connection = null; |
| 115 | 115 | try { |
| 116 | - // Note: this is not perfectly correct to use getConnectTimeoutMillis(), but | |
| 117 | - // until we provide a more fancy to control query timeouts, it's not a bad solution either | |
| 118 | - connection = currentPool.borrowConnection(manager.configuration().getSocketOptions().getConnectTimeoutMillis(), TimeUnit.MILLISECONDS); | |
| 116 | + connection = currentPool.borrowConnection(manager.configuration().getPoolingOptions().getPoolTimeoutMillis(), TimeUnit.MILLISECONDS); | |
| 119 | 117 | if (current != null) { |
| 120 | 118 | if (triedHosts == null) |
| 121 | 119 | triedHosts = new ArrayList<Host>(); |
| @@ -134,8 +132,7 @@ | ||
| 134 | 132 | return false; |
| 135 | 133 | } catch (BusyConnectionException e) { |
| 136 | 134 | // The pool shouldn't have give us a busy connection unless we've maxed up the pool, so move on to the next host. |
| 137 | - if (connection != null) | |
| 138 | - connection.release(); | |
| 135 | + connection.release(); | |
| 139 | 136 | logError(host.getSocketAddress(), e); |
| 140 | 137 | return false; |
| 141 | 138 | } catch (TimeoutException e) { |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTimeoutTest.java +51 −1
| @@ -11,9 +11,12 @@ | ||
| 11 | 11 | |
| 12 | 12 | import static org.assertj.core.api.Assertions.assertThat; |
| 13 | 13 | |
| 14 | +import com.datastax.driver.core.exceptions.DriverException; | |
| 15 | +import com.datastax.driver.core.exceptions.NoHostAvailableException; | |
| 16 | + | |
| 14 | 17 | public class HostConnectionPoolTimeoutTest { |
| 15 | 18 | @Test(groups = "long") |
| 16 | - public void should_wait_for_timeout_before_trashing_connections() throws Exception { | |
| 19 | + public void should_wait_for_idle_timeout_before_trashing_connections() throws Exception { | |
| 17 | 20 | CCMBridge ccm = null; |
| 18 | 21 | Cluster cluster = null; |
| 19 | 22 | try { |
| @@ -103,6 +106,53 @@ | ||
| 103 | 106 | } |
| 104 | 107 | } |
| 105 | 108 | |
| 109 | + @Test(groups = "short") | |
| 110 | + public void should_timeout_immediately_if_pool_timeout_is_zero() throws Exception { | |
| 111 | + CCMBridge ccm = null; | |
| 112 | + Cluster cluster = null; | |
| 113 | + try { | |
| 114 | + ccm = CCMBridge.create("test", 1); | |
| 115 | + cluster = Cluster.builder() | |
| 116 | + .addContactPoint(CCMBridge.ipOfNode(1)) | |
| 117 | + .build(); | |
| 118 | + | |
| 119 | + cluster.getConfiguration().getPoolingOptions() | |
| 120 | + .setCoreConnectionsPerHost(HostDistance.LOCAL, 1) | |
| 121 | + .setMaxConnectionsPerHost(HostDistance.LOCAL, 1) | |
| 122 | + .setPoolTimeoutMillis(0); | |
| 123 | + | |
| 124 | + Session session = cluster.connect(); | |
| 125 | + | |
| 126 | + Host host1 = TestUtils.findHost(cluster, 1); | |
| 127 | + | |
| 128 | + // Borrow all streams to simulate a busy pool (the timeout doesn't matter here) | |
| 129 | + HostConnectionPool pool = ((SessionManager)session).pools.get(host1); | |
| 130 | + for (int i = 0; i < 128; i++) | |
| 131 | + pool.borrowConnection(1, TimeUnit.SECONDS); | |
| 132 | + | |
| 133 | + boolean threw = false; | |
| 134 | + long beforeQuery = System.nanoTime(); | |
| 135 | + try { | |
| 136 | + session.execute("select release_version from system.local"); | |
| 137 | + } catch (NoHostAvailableException e) { | |
| 138 | + long afterQuery = System.nanoTime(); | |
| 139 | + // This is a bit arbitrary, but we can expect that the time to find out there is no available connection | |
| 140 | + // is less than 20 ms | |
| 141 | + assertThat(afterQuery - beforeQuery).isLessThan(20 * 1000 * 1000); | |
| 142 | + Throwable host1Error = e.getErrors().get(host1.getSocketAddress()); | |
| 143 | + assertThat(host1Error).isInstanceOf(DriverException.class); | |
| 144 | + assertThat(host1Error.getMessage()).contains("Timeout while trying to acquire available connection"); | |
| 145 | + threw = true; | |
| 146 | + } | |
| 147 | + assertThat(threw).isTrue(); | |
| 148 | + } finally { | |
| 149 | + if (cluster != null) | |
| 150 | + cluster.close(); | |
| 151 | + if (ccm != null) | |
| 152 | + ccm.remove(); | |
| 153 | + } | |
| 154 | + } | |
| 155 | + | |
| 106 | 156 | /** |
| 107 | 157 | * An auth provider that counts how many authenticators it has returned. |
| 108 | 158 | */ |