Elffuss BraidHub

kikocis/java-driver public

Make timeout on saturated pool customizable (JAVA-457).
Changesetf3d4bfbe8473805fbd16aa174d1d7705bbe0f2dbf11310ea2d1c3e43c8790790
Authorolim7t <omichallat+github@gmail.com> · 2014-10-22 21:13 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2014-10-23 16:41 +02:00
Parents9eb596ab550a

Browse the files at this changeset

5 files changed

modified driver-core/CHANGELOG.rst +1 −0
@@ -25,6 +25,7 @@
2525 - [improvement] Throw an exception when DCAwareRoundRobinPolicy is built with
2626 an explicit but null or empty local datacenter (JAVA-451).
2727 - [bug] Fix check for local contact points in DCAware policy's init (JAVA-511)
28+- [improvement] Make timeout on saturated pool customizable (JAVA-457)
2829
2930
3031 2.0.6:
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +3 −0
@@ -199,6 +199,9 @@
199199 }
200200
201201 private PooledConnection waitForConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException {
202+ if (timeout == 0)
203+ throw new TimeoutException();
204+
202205 long start = System.nanoTime();
203206 long remaining = timeout;
204207 do {
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +31 −0
@@ -52,6 +52,7 @@
5252 private static final int DEFAULT_MAX_POOL_REMOTE = 2;
5353
5454 private static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120;
55+ private static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000;
5556
5657 private volatile Cluster.Manager manager;
5758
@@ -62,6 +63,7 @@
6263 private final int[] maxConnections = new int[] { DEFAULT_MAX_POOL_LOCAL , DEFAULT_MAX_POOL_REMOTE, 0 };
6364
6465 private volatile int idleTimeoutSeconds = DEFAULT_IDLE_TIMEOUT_SECONDS;
66+ private volatile int poolTimeoutMillis = DEFAULT_POOL_TIMEOUT_MILLIS;
6567
6668 public PoolingOptions() {}
6769
@@ -248,6 +250,35 @@
248250 return this;
249251 }
250252
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+
251282 /**
252283 * Requests the driver to re-evaluate the {@link HostDistance} (through the configured
253284 * {@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 @@
113113
114114 PooledConnection connection = null;
115115 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);
119117 if (current != null) {
120118 if (triedHosts == null)
121119 triedHosts = new ArrayList<Host>();
@@ -134,8 +132,7 @@
134132 return false;
135133 } catch (BusyConnectionException e) {
136134 // 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();
139136 logError(host.getSocketAddress(), e);
140137 return false;
141138 } catch (TimeoutException e) {
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTimeoutTest.java +51 −1
@@ -11,9 +11,12 @@
1111
1212 import static org.assertj.core.api.Assertions.assertThat;
1313
14+import com.datastax.driver.core.exceptions.DriverException;
15+import com.datastax.driver.core.exceptions.NoHostAvailableException;
16+
1417 public class HostConnectionPoolTimeoutTest {
1518 @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 {
1720 CCMBridge ccm = null;
1821 Cluster cluster = null;
1922 try {
@@ -103,6 +106,53 @@
103106 }
104107 }
105108
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+
106156 /**
107157 * An auth provider that counts how many authenticators it has returned.
108158 */