kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | ed90b7b4a2f8918beb1837f4547c7419fdc4de3e452fa6e99b26e5abb3a1c81c |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-09-02 11:18 -07:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-09-23 22:41 -07:00 |
| Parents | da9ef71e7257 |
12 files changed
modified changelog/README.md +1 −0
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | - [improvement] JAVA-1293: Make DecoderForStreamIdSize.MAX_FRAME_LENGTH configurable. |
| 17 | 17 | - [improvement] JAVA-1053: Add a metric for authentication errors |
| 18 | 18 | - [improvement] JAVA-1263: Eliminate unnecessary memory copies in FrameCompressor implementations. |
| 19 | +- [improvement] JAVA-893: Make connection pool non-blocking | |
| 19 | 20 | |
| 20 | 21 | |
| 21 | 22 | ### 3.0.3 |
modified driver-core/src/main/java/com/datastax/driver/core/Connection.java +13 −5
| @@ -19,6 +19,8 @@ | ||
| 19 | 19 | import com.datastax.driver.core.exceptions.*; |
| 20 | 20 | import com.datastax.driver.core.utils.MoreFutures; |
| 21 | 21 | import com.google.common.annotations.VisibleForTesting; |
| 22 | +import com.google.common.base.Function; | |
| 23 | +import com.google.common.base.Objects; | |
| 22 | 24 | import com.google.common.collect.Lists; |
| 23 | 25 | import com.google.common.collect.MapMaker; |
| 24 | 26 | import com.google.common.util.concurrent.*; |
| @@ -95,8 +97,9 @@ | ||
| 95 | 97 | |
| 96 | 98 | private final AtomicReference<Owner> ownerRef = new AtomicReference<Owner>(); |
| 97 | 99 | |
| 100 | + private final ListenableFuture<Connection> thisFuture; | |
| 101 | + | |
| 98 | 102 | /** |
| 99 | - * /** | |
| 100 | 103 | * Create a new connection to a Cassandra node and associate it with the given pool. |
| 101 | 104 | * |
| 102 | 105 | * @param name the connection name |
| @@ -111,6 +114,7 @@ | ||
| 111 | 114 | this.dispatcher = new Dispatcher(); |
| 112 | 115 | this.name = name; |
| 113 | 116 | this.ownerRef.set(owner); |
| 117 | + this.thisFuture = Futures.immediateFuture(this); | |
| 114 | 118 | } |
| 115 | 119 | |
| 116 | 120 | /** |
| @@ -483,16 +487,20 @@ | ||
| 483 | 487 | } |
| 484 | 488 | } |
| 485 | 489 | |
| 486 | - ListenableFuture<Void> setKeyspaceAsync(final String keyspace) throws ConnectionException, BusyConnectionException { | |
| 490 | + ListenableFuture<Connection> setKeyspaceAsync(final String keyspace) throws ConnectionException, BusyConnectionException { | |
| 491 | + if (Objects.equal(this.keyspace, keyspace)) { | |
| 492 | + return thisFuture; | |
| 493 | + } | |
| 494 | + | |
| 487 | 495 | logger.trace("{} Setting keyspace {}", this, keyspace); |
| 488 | 496 | // Note: we quote the keyspace below, because the name is the one coming from Cassandra, so it's in the right case already |
| 489 | 497 | Future future = write(new Requests.Query("USE \"" + keyspace + '"')); |
| 490 | - return Futures.transform(future, new AsyncFunction<Message.Response, Void>() { | |
| 498 | + return Futures.transform(future, new Function<Message.Response, Connection>() { | |
| 491 | 499 | @Override |
| 492 | - public ListenableFuture<Void> apply(Message.Response response) throws Exception { | |
| 500 | + public Connection apply(Message.Response response) { | |
| 493 | 501 | if (response instanceof SetKeyspace) { |
| 494 | 502 | Connection.this.keyspace = ((SetKeyspace) response).keyspace; |
| 495 | - return MoreFutures.VOID_SUCCESS; | |
| 503 | + return Connection.this; | |
| 496 | 504 | } else if (response.type == ERROR) { |
| 497 | 505 | Responses.Error error = (Responses.Error) response; |
| 498 | 506 | throw defunct(error.asException(address)); |
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +75 −114
| @@ -16,6 +16,7 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.AuthenticationException; |
| 19 | +import com.datastax.driver.core.exceptions.BusyPoolException; | |
| 19 | 20 | import com.datastax.driver.core.exceptions.ConnectionException; |
| 20 | 21 | import com.datastax.driver.core.exceptions.UnsupportedProtocolVersionException; |
| 21 | 22 | import com.datastax.driver.core.utils.MoreFutures; |
| @@ -26,16 +27,13 @@ | ||
| 26 | 27 | import org.slf4j.Logger; |
| 27 | 28 | import org.slf4j.LoggerFactory; |
| 28 | 29 | |
| 29 | -import java.util.ArrayList; | |
| 30 | -import java.util.List; | |
| 31 | -import java.util.ListIterator; | |
| 32 | -import java.util.Set; | |
| 33 | -import java.util.concurrent.*; | |
| 30 | +import java.util.*; | |
| 31 | +import java.util.concurrent.ConcurrentLinkedQueue; | |
| 32 | +import java.util.concurrent.CopyOnWriteArrayList; | |
| 33 | +import java.util.concurrent.CopyOnWriteArraySet; | |
| 34 | +import java.util.concurrent.Executor; | |
| 34 | 35 | import java.util.concurrent.atomic.AtomicInteger; |
| 35 | 36 | import java.util.concurrent.atomic.AtomicReference; |
| 36 | -import java.util.concurrent.locks.Condition; | |
| 37 | -import java.util.concurrent.locks.Lock; | |
| 38 | -import java.util.concurrent.locks.ReentrantLock; | |
| 39 | 37 | |
| 40 | 38 | import static com.datastax.driver.core.Connection.State.*; |
| 41 | 39 | |
| @@ -62,9 +60,8 @@ | ||
| 62 | 60 | @VisibleForTesting |
| 63 | 61 | final Set<Connection> trash = new CopyOnWriteArraySet<Connection>(); |
| 64 | 62 | |
| 65 | - private volatile int waiter = 0; | |
| 66 | - private final Lock waitLock = new ReentrantLock(true); | |
| 67 | - private final Condition hasAvailableConnection = waitLock.newCondition(); | |
| 63 | + private final Queue<SettableFuture<Connection>> pendingBorrows = new ConcurrentLinkedQueue<SettableFuture<Connection>>(); | |
| 64 | + private final AtomicInteger pendingBorrowCount = new AtomicInteger(); | |
| 68 | 65 | |
| 69 | 66 | private final Runnable newConnectionTask; |
| 70 | 67 | |
| @@ -81,7 +78,7 @@ | ||
| 81 | 78 | // following threshold, we just replace the connection by a new one. |
| 82 | 79 | private final int minAllowedStreams; |
| 83 | 80 | |
| 84 | - public HostConnectionPool(Host host, HostDistance hostDistance, SessionManager manager) { | |
| 81 | + HostConnectionPool(Host host, HostDistance hostDistance, SessionManager manager) { | |
| 85 | 82 | assert hostDistance != HostDistance.IGNORED; |
| 86 | 83 | this.host = host; |
| 87 | 84 | this.hostDistance = hostDistance; |
| @@ -196,17 +193,13 @@ | ||
| 196 | 193 | return manager.configuration().getPoolingOptions(); |
| 197 | 194 | } |
| 198 | 195 | |
| 199 | - public Connection borrowConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { | |
| 196 | + ListenableFuture<Connection> borrowConnection(int maxQueueSize) { | |
| 200 | 197 | Phase phase = this.phase.get(); |
| 201 | 198 | if (phase != Phase.READY) |
| 202 | - // Note: throwing a ConnectionException is probably fine in practice as it will trigger the creation of a new host. | |
| 203 | - // That being said, maybe having a specific exception could be cleaner. | |
| 204 | - throw new ConnectionException(host.getSocketAddress(), "Pool is " + phase); | |
| 199 | + return Futures.immediateFailedFuture(new ConnectionException(host.getSocketAddress(), "Pool is " + phase)); | |
| 205 | 200 | |
| 206 | 201 | if (connections.isEmpty()) { |
| 207 | - if (!host.convictionPolicy.canReconnectNow()) | |
| 208 | - throw new TimeoutException("Connection pool is empty, currently trying to reestablish connections"); | |
| 209 | - else { | |
| 202 | + if (host.convictionPolicy.canReconnectNow()) { | |
| 210 | 203 | int coreSize = options().getCoreConnectionsPerHost(hostDistance); |
| 211 | 204 | if (coreSize == 0) { |
| 212 | 205 | maybeSpawnNewConnection(); |
| @@ -218,10 +211,7 @@ | ||
| 218 | 211 | manager.blockingExecutor().submit(newConnectionTask); |
| 219 | 212 | } |
| 220 | 213 | } |
| 221 | - Connection c = waitForConnection(timeout, unit); | |
| 222 | - totalInFlight.incrementAndGet(); | |
| 223 | - c.setKeyspace(manager.poolsState.keyspace); | |
| 224 | - return c; | |
| 214 | + return enqueue(maxQueueSize); | |
| 225 | 215 | } |
| 226 | 216 | } |
| 227 | 217 | |
| @@ -238,18 +228,17 @@ | ||
| 238 | 228 | if (leastBusy == null) { |
| 239 | 229 | // We could have raced with a shutdown since the last check |
| 240 | 230 | if (isClosed()) |
| 241 | - throw new ConnectionException(host.getSocketAddress(), "Pool is shutdown"); | |
| 231 | + return Futures.immediateFailedFuture(new ConnectionException(host.getSocketAddress(), "Pool is shutdown")); | |
| 242 | 232 | // This might maybe happen if the number of core connections per host is 0 and a connection was trashed between |
| 243 | 233 | // the previous check to connections and now. But in that case, the line above will have trigger the creation of |
| 244 | 234 | // a new connection, so just wait that connection and move on |
| 245 | - leastBusy = waitForConnection(timeout, unit); | |
| 235 | + return enqueue(maxQueueSize); | |
| 246 | 236 | } else { |
| 247 | 237 | while (true) { |
| 248 | 238 | int inFlight = leastBusy.inFlight.get(); |
| 249 | 239 | |
| 250 | 240 | if (inFlight >= Math.min(leastBusy.maxAvailableStreams(), options().getMaxRequestsPerConnection(hostDistance))) { |
| 251 | - leastBusy = waitForConnection(timeout, unit); | |
| 252 | - break; | |
| 241 | + return enqueue(maxQueueSize); | |
| 253 | 242 | } |
| 254 | 243 | |
| 255 | 244 | if (leastBusy.inFlight.compareAndSet(inFlight, inFlight + 1)) |
| @@ -276,96 +265,37 @@ | ||
| 276 | 265 | maybeSpawnNewConnection(); |
| 277 | 266 | } |
| 278 | 267 | |
| 279 | - leastBusy.setKeyspace(manager.poolsState.keyspace); | |
| 280 | - return leastBusy; | |
| 281 | - } | |
| 282 | - | |
| 283 | - private void awaitAvailableConnection(long timeout, TimeUnit unit) throws InterruptedException { | |
| 284 | - waitLock.lock(); | |
| 285 | - waiter++; | |
| 286 | - try { | |
| 287 | - hasAvailableConnection.await(timeout, unit); | |
| 288 | - } finally { | |
| 289 | - waiter--; | |
| 290 | - waitLock.unlock(); | |
| 291 | - } | |
| 292 | - } | |
| 293 | - | |
| 294 | - private void signalAvailableConnection() { | |
| 295 | - // Quick check if it's worth signaling to avoid locking | |
| 296 | - if (waiter == 0) | |
| 297 | - return; | |
| 298 | - | |
| 299 | - waitLock.lock(); | |
| 300 | - try { | |
| 301 | - hasAvailableConnection.signal(); | |
| 302 | - } finally { | |
| 303 | - waitLock.unlock(); | |
| 304 | - } | |
| 268 | + return leastBusy.setKeyspaceAsync(manager.poolsState.keyspace); | |
| 305 | 269 | } |
| 306 | 270 | |
| 307 | - private void signalAllAvailableConnection() { | |
| 308 | - // Quick check if it's worth signaling to avoid locking | |
| 309 | - if (waiter == 0) | |
| 310 | - return; | |
| 311 | - | |
| 312 | - waitLock.lock(); | |
| 313 | - try { | |
| 314 | - hasAvailableConnection.signalAll(); | |
| 315 | - } finally { | |
| 316 | - waitLock.unlock(); | |
| 271 | + private ListenableFuture<Connection> enqueue(int maxQueueSize) { | |
| 272 | + if (maxQueueSize == 0) { | |
| 273 | + return Futures.immediateFailedFuture(new BusyPoolException(host.getSocketAddress(), 0)); | |
| 317 | 274 | } |
| 318 | - } | |
| 319 | 275 | |
| 320 | - private Connection waitForConnection(long timeout, TimeUnit unit) throws ConnectionException, TimeoutException { | |
| 321 | - if (timeout == 0) | |
| 322 | - throw new TimeoutException("All connections are busy and pool timeout is 0"); | |
| 323 | - | |
| 324 | - long start = System.nanoTime(); | |
| 325 | - long remaining = timeout; | |
| 326 | - do { | |
| 327 | - try { | |
| 328 | - awaitAvailableConnection(remaining, unit); | |
| 329 | - } catch (InterruptedException e) { | |
| 330 | - Thread.currentThread().interrupt(); | |
| 331 | - // If we're interrupted fine, check if there is a connection available but stop waiting otherwise | |
| 332 | - timeout = 0; // this will make us stop the loop if we don't get a connection right away | |
| 276 | + while (true) { | |
| 277 | + int count = pendingBorrowCount.get(); | |
| 278 | + if (count >= maxQueueSize) { | |
| 279 | + return Futures.immediateFailedFuture(new BusyPoolException(host.getSocketAddress(), maxQueueSize)); | |
| 333 | 280 | } |
| 334 | - | |
| 335 | - if (isClosed()) | |
| 336 | - throw new ConnectionException(host.getSocketAddress(), "Pool is shutdown"); | |
| 337 | - | |
| 338 | - int minInFlight = Integer.MAX_VALUE; | |
| 339 | - Connection leastBusy = null; | |
| 340 | - for (Connection connection : connections) { | |
| 341 | - int inFlight = connection.inFlight.get(); | |
| 342 | - if (inFlight < minInFlight) { | |
| 343 | - minInFlight = inFlight; | |
| 344 | - leastBusy = connection; | |
| 345 | - } | |
| 281 | + if (pendingBorrowCount.compareAndSet(count, count + 1)) { | |
| 282 | + break; | |
| 346 | 283 | } |
| 284 | + } | |
| 347 | 285 | |
| 348 | - // If we race with shutdown, leastBusy could be null. In that case we just loop and we'll throw on the next | |
| 349 | - // iteration anyway | |
| 350 | - if (leastBusy != null) { | |
| 351 | - while (true) { | |
| 352 | - int inFlight = leastBusy.inFlight.get(); | |
| 353 | - | |
| 354 | - if (inFlight >= Math.min(leastBusy.maxAvailableStreams(), options().getMaxRequestsPerConnection(hostDistance))) | |
| 355 | - break; | |
| 356 | - | |
| 357 | - if (leastBusy.inFlight.compareAndSet(inFlight, inFlight + 1)) | |
| 358 | - return leastBusy; | |
| 359 | - } | |
| 360 | - } | |
| 286 | + SettableFuture<Connection> future = SettableFuture.create(); | |
| 287 | + pendingBorrows.add(future); | |
| 361 | 288 | |
| 362 | - remaining = timeout - Cluster.timeSince(start, unit); | |
| 363 | - } while (remaining > 0); | |
| 289 | + // If we raced with shutdown, make sure the future will be completed. This has no effect if it was properly | |
| 290 | + // handled in closeAsync. | |
| 291 | + if (phase.get() == Phase.CLOSING) { | |
| 292 | + future.setException(new ConnectionException(host.getSocketAddress(), "Pool is shutdown")); | |
| 293 | + } | |
| 364 | 294 | |
| 365 | - throw new TimeoutException("All connections are busy"); | |
| 295 | + return future; | |
| 366 | 296 | } |
| 367 | 297 | |
| 368 | - public void returnConnection(Connection connection) { | |
| 298 | + void returnConnection(Connection connection) { | |
| 369 | 299 | connection.inFlight.decrementAndGet(); |
| 370 | 300 | totalInFlight.decrementAndGet(); |
| 371 | 301 | |
| @@ -384,7 +314,37 @@ | ||
| 384 | 314 | if (connection.maxAvailableStreams() < minAllowedStreams) { |
| 385 | 315 | replaceConnection(connection); |
| 386 | 316 | } else { |
| 387 | - signalAvailableConnection(); | |
| 317 | + dequeue(connection); | |
| 318 | + } | |
| 319 | + } | |
| 320 | + } | |
| 321 | + | |
| 322 | + // When a connection gets returned to the pool, check if there are pending borrows that can be completed with it. | |
| 323 | + private void dequeue(Connection connection) { | |
| 324 | + while (!pendingBorrows.isEmpty()) { | |
| 325 | + | |
| 326 | + // We can only reuse the connection if it's under its maximum number of inFlight requests. | |
| 327 | + // Do this atomically, as we could be competing with other borrowConnection or dequeue calls. | |
| 328 | + while (true) { | |
| 329 | + int inFlight = connection.inFlight.get(); | |
| 330 | + if (inFlight >= Math.min(connection.maxAvailableStreams(), options().getMaxRequestsPerConnection(hostDistance))) { | |
| 331 | + // Connection is full again, stop dequeuing | |
| 332 | + return; | |
| 333 | + } | |
| 334 | + if (connection.inFlight.compareAndSet(inFlight, inFlight + 1)) { | |
| 335 | + // We acquired the right to reuse the connection for one request, proceed | |
| 336 | + break; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + SettableFuture<Connection> pendingBorrow = pendingBorrows.poll(); | |
| 341 | + if (pendingBorrow == null) { | |
| 342 | + // Another thread has emptied the queue since our last check, restore the count | |
| 343 | + connection.inFlight.decrementAndGet(); | |
| 344 | + } else { | |
| 345 | + totalInFlight.incrementAndGet(); | |
| 346 | + pendingBorrowCount.decrementAndGet(); | |
| 347 | + pendingBorrow.set(connection); | |
| 388 | 348 | } |
| 389 | 349 | } |
| 390 | 350 | } |
| @@ -465,7 +425,7 @@ | ||
| 465 | 425 | return false; |
| 466 | 426 | } |
| 467 | 427 | |
| 468 | - signalAvailableConnection(); | |
| 428 | + dequeue(newConnection); | |
| 469 | 429 | return true; |
| 470 | 430 | } catch (InterruptedException e) { |
| 471 | 431 | Thread.currentThread().interrupt(); |
| @@ -600,11 +560,11 @@ | ||
| 600 | 560 | connection.closeAsync(); |
| 601 | 561 | } |
| 602 | 562 | |
| 603 | - public final boolean isClosed() { | |
| 563 | + final boolean isClosed() { | |
| 604 | 564 | return closeFuture.get() != null; |
| 605 | 565 | } |
| 606 | 566 | |
| 607 | - public final CloseFuture closeAsync() { | |
| 567 | + final CloseFuture closeAsync() { | |
| 608 | 568 | |
| 609 | 569 | CloseFuture future = closeFuture.get(); |
| 610 | 570 | if (future != null) |
| @@ -612,8 +572,9 @@ | ||
| 612 | 572 | |
| 613 | 573 | phase.set(Phase.CLOSING); |
| 614 | 574 | |
| 615 | - // Wake up all threads that wait | |
| 616 | - signalAllAvailableConnection(); | |
| 575 | + for (SettableFuture<Connection> pendingBorrow : pendingBorrows) { | |
| 576 | + pendingBorrow.setException(new ConnectionException(host.getSocketAddress(), "Pool is shutdown")); | |
| 577 | + } | |
| 617 | 578 | |
| 618 | 579 | future = new CloseFuture.Forwarding(discardAvailableConnections()); |
| 619 | 580 | |
| @@ -622,7 +583,7 @@ | ||
| 622 | 583 | : closeFuture.get(); // We raced, it's ok, return the future that was actually set |
| 623 | 584 | } |
| 624 | 585 | |
| 625 | - public int opened() { | |
| 586 | + int opened() { | |
| 626 | 587 | return open.get(); |
| 627 | 588 | } |
| 628 | 589 | |
| @@ -657,7 +618,7 @@ | ||
| 657 | 618 | |
| 658 | 619 | // This creates connections if we have less than core connections (if we |
| 659 | 620 | // have more than core, connection will just get trash when we can). |
| 660 | - public void ensureCoreConnections() { | |
| 621 | + void ensureCoreConnections() { | |
| 661 | 622 | if (isClosed()) |
| 662 | 623 | return; |
| 663 | 624 |
modified driver-core/src/main/java/com/datastax/driver/core/PoolingOptions.java +40 −16
| @@ -128,10 +128,16 @@ | ||
| 128 | 128 | public static final int DEFAULT_IDLE_TIMEOUT_SECONDS = 120; |
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | - * The default value for {@link #getPoolTimeoutMillis()} ({@value}). | |
| 131 | + * @deprecated see {@link #setPoolTimeoutMillis(int)} | |
| 132 | 132 | */ |
| 133 | + @Deprecated | |
| 133 | 134 | public static final int DEFAULT_POOL_TIMEOUT_MILLIS = 5000; |
| 134 | 135 | |
| 136 | + /** | |
| 137 | + * The default value for {@link #getMaxQueueSize()} ({@value}). | |
| 138 | + */ | |
| 139 | + public static final int DEFAULT_MAX_QUEUE_SIZE = 256; | |
| 140 | + | |
| 135 | 141 | /** |
| 136 | 142 | * The default value for {@link #getHeartbeatIntervalSeconds()} ({@value}). |
| 137 | 143 | */ |
| @@ -151,7 +157,7 @@ | ||
| 151 | 157 | private volatile int maxRequestsPerConnectionRemote = UNSET; |
| 152 | 158 | |
| 153 | 159 | private volatile int idleTimeoutSeconds = DEFAULT_IDLE_TIMEOUT_SECONDS; |
| 154 | - private volatile int poolTimeoutMillis = DEFAULT_POOL_TIMEOUT_MILLIS; | |
| 160 | + private volatile int maxQueueSize = DEFAULT_MAX_QUEUE_SIZE; | |
| 155 | 161 | private volatile int heartbeatIntervalSeconds = DEFAULT_HEARTBEAT_INTERVAL_SECONDS; |
| 156 | 162 | |
| 157 | 163 | private volatile Executor initializationExecutor = DEFAULT_INITIALIZATION_EXECUTOR; |
| @@ -400,30 +406,48 @@ | ||
| 400 | 406 | } |
| 401 | 407 | |
| 402 | 408 | /** |
| 403 | - * Returns the timeout when trying to acquire a connection from a host's pool. | |
| 404 | - * | |
| 405 | - * @return the timeout. | |
| 409 | + * @deprecated see {@link #setPoolTimeoutMillis(int)}. This method always returns 0. | |
| 406 | 410 | */ |
| 411 | + @Deprecated | |
| 407 | 412 | public int getPoolTimeoutMillis() { |
| 408 | - return poolTimeoutMillis; | |
| 413 | + return 0; | |
| 414 | + } | |
| 415 | + | |
| 416 | + /** | |
| 417 | + * @deprecated the connection pool does not use a timeout anymore, incoming requests are now throttled with a | |
| 418 | + * threshold on the {@link #setMaxQueueSize(int) queue size}. This method has no effect. | |
| 419 | + */ | |
| 420 | + @Deprecated | |
| 421 | + public PoolingOptions setPoolTimeoutMillis(int poolTimeoutMillis) { | |
| 422 | + return this; | |
| 423 | + } | |
| 424 | + | |
| 425 | + /** | |
| 426 | + * Returns the maximum number of requests that get enqueued if no connection is available. | |
| 427 | + * | |
| 428 | + * @return the maximum queue size. | |
| 429 | + */ | |
| 430 | + public int getMaxQueueSize() { | |
| 431 | + return maxQueueSize; | |
| 409 | 432 | } |
| 410 | 433 | |
| 411 | 434 | /** |
| 412 | - * Sets the timeout when trying to acquire a connection from a host's pool. | |
| 435 | + * Sets the maximum number of requests that get enqueued if no connection is available. | |
| 413 | 436 | * <p/> |
| 414 | - * If no connection is available within that time, the driver will try the | |
| 415 | - * next host from the query plan. | |
| 437 | + * If the queue grows past this value, new requests will be rejected immediately (and the driver will move to the | |
| 438 | + * next host in the query plan). This limit is per connection pool, not global to the driver. | |
| 416 | 439 | * <p/> |
| 417 | - * The default is 5 seconds. If this option is set to zero, the driver won't wait at all. | |
| 440 | + * The default value is {@value DEFAULT_MAX_QUEUE_SIZE}. If this option is set to zero, the driver will never | |
| 441 | + * enqueue requests. | |
| 418 | 442 | * |
| 419 | - * @param poolTimeoutMillis the new value in milliseconds. | |
| 443 | + * @param maxQueueSize the new value. | |
| 420 | 444 | * @return this {@code PoolingOptions} |
| 421 | - * @throws IllegalArgumentException if the timeout is negative. | |
| 445 | + * @throws IllegalArgumentException if the value is negative. | |
| 422 | 446 | */ |
| 423 | - public PoolingOptions setPoolTimeoutMillis(int poolTimeoutMillis) { | |
| 424 | - if (poolTimeoutMillis < 0) | |
| 425 | - throw new IllegalArgumentException("Pool timeout must be positive"); | |
| 426 | - this.poolTimeoutMillis = poolTimeoutMillis; | |
| 447 | + public PoolingOptions setMaxQueueSize(int maxQueueSize) { | |
| 448 | + if (maxQueueSize < 0) | |
| 449 | + throw new IllegalArgumentException("Max queue size must be positive"); | |
| 450 | + this.maxQueueSize = maxQueueSize; | |
| 427 | 451 | return this; |
| 428 | 452 | } |
| 429 | 453 |
modified driver-core/src/main/java/com/datastax/driver/core/RequestHandler.java +61 −57
| @@ -21,6 +21,9 @@ | ||
| 21 | 21 | import com.datastax.driver.core.policies.RetryPolicy.RetryDecision.Type; |
| 22 | 22 | import com.datastax.driver.core.policies.SpeculativeExecutionPolicy.SpeculativeExecutionPlan; |
| 23 | 23 | import com.google.common.collect.Sets; |
| 24 | +import com.google.common.util.concurrent.FutureCallback; | |
| 25 | +import com.google.common.util.concurrent.Futures; | |
| 26 | +import com.google.common.util.concurrent.ListenableFuture; | |
| 24 | 27 | import io.netty.util.Timeout; |
| 25 | 28 | import io.netty.util.TimerTask; |
| 26 | 29 | import org.slf4j.Logger; |
| @@ -108,7 +111,7 @@ | ||
| 108 | 111 | |
| 109 | 112 | SpeculativeExecution execution = new SpeculativeExecution(request, position); |
| 110 | 113 | runningExecutions.add(execution); |
| 111 | - execution.sendRequest(); | |
| 114 | + execution.findNextHostAndQuery(); | |
| 112 | 115 | } |
| 113 | 116 | |
| 114 | 117 | private void scheduleExecution(long delayMillis) { |
| @@ -261,12 +264,10 @@ | ||
| 261 | 264 | logger.trace("[{}] Starting", id); |
| 262 | 265 | } |
| 263 | 266 | |
| 264 | - void sendRequest() { | |
| 267 | + void findNextHostAndQuery() { | |
| 265 | 268 | try { |
| 266 | 269 | Host host; |
| 267 | 270 | while (!isDone.get() && (host = queryPlan.next()) != null && !queryStateRef.get().isCancelled()) { |
| 268 | - if (logger.isTraceEnabled()) | |
| 269 | - logger.trace("[{}] Querying node {}", id, host); | |
| 270 | 271 | if (query(host)) |
| 271 | 272 | return; |
| 272 | 273 | } |
| @@ -278,48 +279,63 @@ | ||
| 278 | 279 | } |
| 279 | 280 | |
| 280 | 281 | private boolean query(final Host host) { |
| 281 | - HostConnectionPool currentPool = manager.pools.get(host); | |
| 282 | - if (currentPool == null || currentPool.isClosed()) | |
| 282 | + HostConnectionPool pool = manager.pools.get(host); | |
| 283 | + if (pool == null || pool.isClosed()) | |
| 283 | 284 | return false; |
| 284 | 285 | |
| 286 | + if (logger.isTraceEnabled()) | |
| 287 | + logger.trace("[{}] Querying node {}", id, host); | |
| 288 | + | |
| 285 | 289 | if (allowSpeculativeExecutions && nextExecutionScheduled.compareAndSet(false, true)) |
| 286 | 290 | scheduleExecution(speculativeExecutionPlan.nextExecution(host)); |
| 287 | 291 | |
| 288 | - Connection connection = null; | |
| 289 | - try { | |
| 290 | - connection = currentPool.borrowConnection(manager.configuration().getPoolingOptions().getPoolTimeoutMillis(), TimeUnit.MILLISECONDS); | |
| 291 | - if (current != null) { | |
| 292 | - if (triedHosts == null) | |
| 293 | - triedHosts = new CopyOnWriteArrayList<Host>(); | |
| 294 | - triedHosts.add(current); | |
| 292 | + ListenableFuture<Connection> connectionFuture = pool.borrowConnection( | |
| 293 | + manager.configuration().getPoolingOptions().getMaxQueueSize()); | |
| 294 | + Futures.addCallback(connectionFuture, new FutureCallback<Connection>() { | |
| 295 | + @Override | |
| 296 | + public void onSuccess(Connection connection) { | |
| 297 | + if (current != null) { | |
| 298 | + if (triedHosts == null) | |
| 299 | + triedHosts = new CopyOnWriteArrayList<Host>(); | |
| 300 | + triedHosts.add(current); | |
| 301 | + } | |
| 302 | + current = host; | |
| 303 | + try { | |
| 304 | + write(connection, SpeculativeExecution.this); | |
| 305 | + } catch (ConnectionException e) { | |
| 306 | + // If we have any problem with the connection, move to the next node. | |
| 307 | + if (metricsEnabled()) | |
| 308 | + metrics().getErrorMetrics().getConnectionErrors().inc(); | |
| 309 | + if (connection != null) | |
| 310 | + connection.release(); | |
| 311 | + logError(host.getSocketAddress(), e); | |
| 312 | + findNextHostAndQuery(); | |
| 313 | + } catch (BusyConnectionException e) { | |
| 314 | + // The pool shouldn't have give us a busy connection unless we've maxed up the pool, so move on to the next host. | |
| 315 | + connection.release(); | |
| 316 | + logError(host.getSocketAddress(), e); | |
| 317 | + findNextHostAndQuery(); | |
| 318 | + } catch (RuntimeException e) { | |
| 319 | + if (connection != null) | |
| 320 | + connection.release(); | |
| 321 | + logger.error("Unexpected error while querying " + host.getAddress(), e); | |
| 322 | + logError(host.getSocketAddress(), e); | |
| 323 | + findNextHostAndQuery(); | |
| 324 | + } | |
| 295 | 325 | } |
| 296 | - current = host; | |
| 297 | - write(connection, this); | |
| 298 | - return true; | |
| 299 | - } catch (ConnectionException e) { | |
| 300 | - // If we have any problem with the connection, move to the next node. | |
| 301 | - if (metricsEnabled()) | |
| 302 | - metrics().getErrorMetrics().getConnectionErrors().inc(); | |
| 303 | - if (connection != null) | |
| 304 | - connection.release(); | |
| 305 | - logError(host.getSocketAddress(), e); | |
| 306 | - return false; | |
| 307 | - } catch (BusyConnectionException e) { | |
| 308 | - // The pool shouldn't have give us a busy connection unless we've maxed up the pool, so move on to the next host. | |
| 309 | - connection.release(); | |
| 310 | - logError(host.getSocketAddress(), e); | |
| 311 | - return false; | |
| 312 | - } catch (TimeoutException e) { | |
| 313 | - // We timeout, log it but move to the next node. | |
| 314 | - logError(host.getSocketAddress(), new DriverException("Timeout while trying to acquire available connection (you may want to increase the driver number of per-host connections)", e)); | |
| 315 | - return false; | |
| 316 | - } catch (RuntimeException e) { | |
| 317 | - if (connection != null) | |
| 318 | - connection.release(); | |
| 319 | - logger.error("Unexpected error while querying " + host.getAddress(), e); | |
| 320 | - logError(host.getSocketAddress(), e); | |
| 321 | - return false; | |
| 322 | - } | |
| 326 | + | |
| 327 | + @Override | |
| 328 | + public void onFailure(Throwable t) { | |
| 329 | + if (t instanceof BusyPoolException) { | |
| 330 | + logError(host.getSocketAddress(), t); | |
| 331 | + } else { | |
| 332 | + logger.error("Unexpected error while querying " + host.getAddress(), t); | |
| 333 | + logError(host.getSocketAddress(), t); | |
| 334 | + } | |
| 335 | + findNextHostAndQuery(); | |
| 336 | + } | |
| 337 | + }); | |
| 338 | + return true; | |
| 323 | 339 | } |
| 324 | 340 | |
| 325 | 341 | private void write(Connection connection, Connection.ResponseCallback responseCallback) throws ConnectionException, BusyConnectionException { |
| @@ -382,23 +398,11 @@ | ||
| 382 | 398 | if (newConsistencyLevel != null) |
| 383 | 399 | this.retryConsistencyLevel = newConsistencyLevel; |
| 384 | 400 | |
| 385 | - // We should not retry on the current thread as this will be an IO thread. | |
| 386 | - manager.executor().execute(new Runnable() { | |
| 387 | - @Override | |
| 388 | - public void run() { | |
| 389 | - if (queryStateRef.get().isCancelled()) | |
| 390 | - return; | |
| 391 | - try { | |
| 392 | - if (retryCurrent) { | |
| 393 | - if (query(h)) | |
| 394 | - return; | |
| 395 | - } | |
| 396 | - sendRequest(); | |
| 397 | - } catch (Exception e) { | |
| 398 | - setFinalException(null, new DriverInternalError("Unexpected exception while retrying query", e)); | |
| 399 | - } | |
| 400 | - } | |
| 401 | - }); | |
| 401 | + if (queryStateRef.get().isCancelled()) | |
| 402 | + return; | |
| 403 | + | |
| 404 | + if (!retryCurrent || !query(h)) | |
| 405 | + findNextHostAndQuery(); | |
| 402 | 406 | } |
| 403 | 407 | |
| 404 | 408 | private void logError(InetSocketAddress address, Throwable exception) { |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +23 −15
| @@ -623,22 +623,30 @@ | ||
| 623 | 623 | |
| 624 | 624 | try { |
| 625 | 625 | // Preparing is not critical: if it fails, it will fix itself later when the user tries to execute |
| 626 | - // the prepared query. So don't block if no connection is available, simply abort. | |
| 627 | - final Connection c = entry.getValue().borrowConnection(0, TimeUnit.MILLISECONDS); | |
| 628 | - ListenableFuture<Response> future = c.write(new Requests.Prepare(query)); | |
| 629 | - Futures.addCallback(future, new FutureCallback<Response>() { | |
| 630 | - @Override | |
| 631 | - public void onSuccess(Response result) { | |
| 632 | - c.release(); | |
| 633 | - } | |
| 626 | + // the prepared query. So don't wait if no connection is available, simply abort. | |
| 627 | + ListenableFuture<Connection> connectionFuture = entry.getValue().borrowConnection(0); | |
| 628 | + ListenableFuture<Response> prepareFuture = Futures.transform(connectionFuture, | |
| 629 | + new AsyncFunction<Connection, Response>() { | |
| 630 | + @Override | |
| 631 | + public ListenableFuture<Response> apply(final Connection c) throws Exception { | |
| 632 | + Connection.Future responseFuture = c.write(new Requests.Prepare(query)); | |
| 633 | + Futures.addCallback(responseFuture, new FutureCallback<Response>() { | |
| 634 | + @Override | |
| 635 | + public void onSuccess(Response result) { | |
| 636 | + c.release(); | |
| 637 | + } | |
| 634 | 638 | |
| 635 | - @Override | |
| 636 | - public void onFailure(Throwable t) { | |
| 637 | - logger.debug(String.format("Unexpected error while preparing query (%s) on %s", query, entry.getKey()), t); | |
| 638 | - c.release(); | |
| 639 | - } | |
| 640 | - }); | |
| 641 | - futures.add(future); | |
| 639 | + @Override | |
| 640 | + public void onFailure(Throwable t) { | |
| 641 | + logger.debug(String.format("Unexpected error while preparing query (%s) on %s", | |
| 642 | + query, entry.getKey()), t); | |
| 643 | + c.release(); | |
| 644 | + } | |
| 645 | + }); | |
| 646 | + return responseFuture; | |
| 647 | + } | |
| 648 | + }); | |
| 649 | + futures.add(prepareFuture); | |
| 642 | 650 | } catch (Exception e) { |
| 643 | 651 | // Again, not being able to prepare the query right now is no big deal, so just ignore |
| 644 | 652 | } |
added driver-core/src/main/java/com/datastax/driver/core/exceptions/BusyPoolException.java +80 −0
| @@ -0,0 +1,80 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core.exceptions; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.HostDistance; | |
| 19 | +import com.datastax.driver.core.Statement; | |
| 20 | + | |
| 21 | +import java.net.InetAddress; | |
| 22 | +import java.net.InetSocketAddress; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Indicates that a connection pool has run out of available connections. | |
| 26 | + * <p/> | |
| 27 | + * This happens if the pool has no connections (for example if it's currently reconnecting to its host), or if all | |
| 28 | + * connections have reached their maximum number of in flight queries. The query will be retried on the next host in the | |
| 29 | + * {@link com.datastax.driver.core.policies.LoadBalancingPolicy#newQueryPlan(String, Statement) query plan}. | |
| 30 | + * <p/> | |
| 31 | + * This exception is a symptom that the driver is experiencing a high workload. If it happens regularly on all hosts, | |
| 32 | + * you should consider tuning one (or a combination of) the following pooling options: | |
| 33 | + * <ul> | |
| 34 | + * <li>{@link com.datastax.driver.core.PoolingOptions#setMaxRequestsPerConnection(HostDistance, int)}: maximum number of | |
| 35 | + * requests per connection;</li> | |
| 36 | + * <li>{@link com.datastax.driver.core.PoolingOptions#setMaxConnectionsPerHost(HostDistance, int)}: maximum number of | |
| 37 | + * connections in the pool;</li> | |
| 38 | + * <li>{@link com.datastax.driver.core.PoolingOptions#setMaxQueueSize(int)}: maximum number of enqueued requests before | |
| 39 | + * this exception is thrown.</li> | |
| 40 | + * </ul> | |
| 41 | + */ | |
| 42 | +public class BusyPoolException extends DriverException implements CoordinatorException { | |
| 43 | + | |
| 44 | + private static final long serialVersionUID = 0; | |
| 45 | + | |
| 46 | + private final InetSocketAddress address; | |
| 47 | + private final int queueSize; | |
| 48 | + | |
| 49 | + public BusyPoolException(InetSocketAddress address, int queueSize) { | |
| 50 | + this(address, queueSize, null); | |
| 51 | + } | |
| 52 | + | |
| 53 | + private BusyPoolException(InetSocketAddress address, int queueSize, Throwable cause) { | |
| 54 | + super(buildMessage(address, queueSize), cause); | |
| 55 | + this.address = address; | |
| 56 | + this.queueSize = queueSize; | |
| 57 | + } | |
| 58 | + | |
| 59 | + private static String buildMessage(InetSocketAddress address, int queueSize) { | |
| 60 | + return String.format("[%s] Pool is busy (no available connection and the queue has reached its max size %d)", | |
| 61 | + address.getAddress(), | |
| 62 | + queueSize); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public InetAddress getHost() { | |
| 67 | + return address.getAddress(); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public InetSocketAddress getAddress() { | |
| 72 | + return address; | |
| 73 | + } | |
| 74 | + | |
| 75 | + @Override | |
| 76 | + public BusyPoolException copy() { | |
| 77 | + return new BusyPoolException(address, queueSize, this); | |
| 78 | + } | |
| 79 | + | |
| 80 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/ConnectionReleaseTest.java +1 −3
| @@ -110,9 +110,7 @@ | ||
| 110 | 110 | } |
| 111 | 111 | }); |
| 112 | 112 | |
| 113 | - // Wait for up to pool timeout for response. If future is not complete by then, we can assume | |
| 114 | - // that it is likely that the execute is hung because the connection wasn't released yet. | |
| 115 | - long waitTimeInMs = cluster.getConfiguration().getPoolingOptions().getPoolTimeoutMillis(); | |
| 113 | + long waitTimeInMs = 2000; | |
| 116 | 114 | try { |
| 117 | 115 | ResultSet result = future.get(waitTimeInMs, TimeUnit.MILLISECONDS); |
| 118 | 116 | assertThat(result.one().getString("n")).isEqualTo("world"); |
modified driver-core/src/test/java/com/datastax/driver/core/HostConnectionPoolTest.java +217 −153
| @@ -19,7 +19,9 @@ | ||
| 19 | 19 | import com.datastax.driver.core.exceptions.BusyConnectionException; |
| 20 | 20 | import com.datastax.driver.core.exceptions.ConnectionException; |
| 21 | 21 | import com.datastax.driver.core.policies.ConstantReconnectionPolicy; |
| 22 | +import com.datastax.driver.core.utils.MoreFutures; | |
| 22 | 23 | import com.google.common.base.Predicate; |
| 24 | +import com.google.common.base.Throwables; | |
| 23 | 25 | import com.google.common.util.concurrent.*; |
| 24 | 26 | import org.scassandra.cql.PrimitiveType; |
| 25 | 27 | import org.scassandra.http.client.PrimingRequest; |
| @@ -27,6 +29,7 @@ | ||
| 27 | 29 | import org.testng.annotations.Test; |
| 28 | 30 | |
| 29 | 31 | import java.net.InetSocketAddress; |
| 32 | +import java.util.Collections; | |
| 30 | 33 | import java.util.Iterator; |
| 31 | 34 | import java.util.List; |
| 32 | 35 | import java.util.concurrent.*; |
| @@ -35,8 +38,8 @@ | ||
| 35 | 38 | |
| 36 | 39 | import static com.datastax.driver.core.Assertions.assertThat; |
| 37 | 40 | import static com.datastax.driver.core.ConditionChecker.check; |
| 41 | +import static com.datastax.driver.core.PoolingOptions.NEW_CONNECTION_THRESHOLD_LOCAL_KEY; | |
| 38 | 42 | import static com.google.common.collect.Lists.newArrayList; |
| 39 | -import static java.util.Collections.singletonList; | |
| 40 | 43 | import static java.util.concurrent.TimeUnit.SECONDS; |
| 41 | 44 | import static org.mockito.Mockito.*; |
| 42 | 45 | import static org.scassandra.http.client.ClosedConnectionReport.CloseType.CLOSE; |
| @@ -45,6 +48,9 @@ | ||
| 45 | 48 | |
| 46 | 49 | public class HostConnectionPoolTest extends ScassandraTestBase.PerClassCluster { |
| 47 | 50 | |
| 51 | + static final Integer NEW_CONNECTION_THRESHOLD = PoolingOptions.DEFAULTS.get(ProtocolVersion.V1) | |
| 52 | + .get(NEW_CONNECTION_THRESHOLD_LOCAL_KEY); | |
| 53 | + | |
| 48 | 54 | @BeforeClass(groups = {"short", "long"}) |
| 49 | 55 | public void reinitializeCluster() { |
| 50 | 56 | // Don't use the provided cluster, each test will create its own instead. |
| @@ -67,93 +73,56 @@ | ||
| 67 | 73 | }).becomesTrue(); |
| 68 | 74 | } |
| 69 | 75 | |
| 70 | - /** | |
| 71 | - * Sends 101 requests on pool, asserting that the connection used is in expectedConnections, except for the last request. | |
| 72 | - */ | |
| 73 | - private List<MockRequest> fillConnectionToThreshold(HostConnectionPool pool, List<Connection> expectedConnections) throws ConnectionException, BusyConnectionException, TimeoutException { | |
| 74 | - List<MockRequest> requests = sendRequests(100, pool, expectedConnections); | |
| 75 | - requests.add(MockRequest.send(pool)); | |
| 76 | - return requests; | |
| 77 | - } | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * Sends count requests on pool. | |
| 81 | - */ | |
| 82 | - private List<MockRequest> sendRequests(int count, HostConnectionPool pool) throws ConnectionException, BusyConnectionException, TimeoutException { | |
| 83 | - return sendRequests(count, pool, null); | |
| 84 | - } | |
| 85 | - | |
| 86 | - /** | |
| 87 | - * Sends count requests on pool, asserting that the connection used is in expectedConnections. | |
| 88 | - */ | |
| 89 | - private List<MockRequest> sendRequests(int count, HostConnectionPool pool, List<Connection> expectedConnections) throws ConnectionException, BusyConnectionException, TimeoutException { | |
| 90 | - List<MockRequest> requests = newArrayList(); | |
| 91 | - for (int i = 0; i < count; i++) { | |
| 92 | - MockRequest request = MockRequest.send(pool); | |
| 93 | - requests.add(request); | |
| 94 | - if (expectedConnections != null) | |
| 95 | - assertThat(expectedConnections).contains(request.connection); | |
| 96 | - } | |
| 97 | - return requests; | |
| 98 | - } | |
| 99 | - | |
| 100 | - /** | |
| 101 | - * Completes count requests by simulating a successful response. | |
| 102 | - */ | |
| 103 | - private void completeRequests(int count, List<MockRequest> requests) { | |
| 104 | - Iterator<MockRequest> requestIt = requests.iterator(); | |
| 105 | - for (int i = 0; i < count; i++) { | |
| 106 | - if (requestIt.hasNext()) { | |
| 107 | - MockRequest request = requestIt.next(); | |
| 108 | - request.simulateSuccessResponse(); | |
| 109 | - requestIt.remove(); | |
| 110 | - } else { | |
| 111 | - break; | |
| 112 | - } | |
| 76 | + private void assertBorrowedConnections(Iterable<MockRequest> requests, List<Connection> expectedConnections) { | |
| 77 | + for (MockRequest request : requests) { | |
| 78 | + assertThat(expectedConnections).contains(request.getConnection()); | |
| 113 | 79 | } |
| 114 | 80 | } |
| 115 | 81 | |
| 116 | - /** | |
| 117 | - * Completes all requests by simulating a successful response. | |
| 118 | - */ | |
| 119 | - private void completeRequests(List<MockRequest> requests) { | |
| 120 | - for (MockRequest request : requests) { | |
| 121 | - request.simulateSuccessResponse(); | |
| 122 | - } | |
| 82 | + private void assertBorrowedConnection(Iterable<MockRequest> requests, Connection expectedConnection) { | |
| 83 | + assertBorrowedConnections(requests, Collections.singletonList(expectedConnection)); | |
| 123 | 84 | } |
| 124 | 85 | |
| 125 | 86 | /** |
| 126 | - * Ensures that if a fixed-sized pool has filled its core connections that borrowConnection will timeout instead | |
| 127 | - * of creating a new connection. | |
| 87 | + * Ensures that if a fixed-sized pool has filled its core connections and reached its maximum number of enqueued | |
| 88 | + * requests, then borrowConnection will fail instead of creating a new connection. | |
| 128 | 89 | * |
| 129 | 90 | * @jira_ticket JAVA-419 |
| 130 | 91 | * @test_category connection:connection_pool |
| 131 | 92 | * @since 2.0.10, 2.1.6 |
| 132 | 93 | */ |
| 133 | 94 | @Test(groups = "short") |
| 134 | - public void fixed_size_pool_should_fill_its_core_connections_and_then_timeout() throws ConnectionException, TimeoutException, BusyConnectionException { | |
| 95 | + public void fixed_size_pool_should_fill_its_core_connections_and_queue_and_then_reject() { | |
| 135 | 96 | Cluster cluster = createClusterBuilder().build(); |
| 136 | - List<MockRequest> requests = newArrayList(); | |
| 97 | + List<MockRequest> allRequests = newArrayList(); | |
| 137 | 98 | try { |
| 138 | 99 | HostConnectionPool pool = createPool(cluster, 2, 2); |
| 100 | + int maxQueueSize = 256; | |
| 139 | 101 | |
| 140 | 102 | assertThat(pool.connections.size()).isEqualTo(2); |
| 141 | 103 | List<Connection> coreConnections = newArrayList(pool.connections); |
| 142 | - requests.addAll(sendRequests(256, pool, coreConnections)); | |
| 143 | - | |
| 104 | + // fill connections | |
| 105 | + List<MockRequest> requests = MockRequest.sendMany(2 * 128, pool); | |
| 106 | + assertBorrowedConnections(requests, coreConnections); | |
| 107 | + allRequests.addAll(requests); | |
| 108 | + // fill queue | |
| 109 | + allRequests.addAll(MockRequest.sendMany(maxQueueSize, pool, maxQueueSize)); | |
| 110 | + | |
| 111 | + // add one more request, it should be rejected because the queue is full | |
| 112 | + MockRequest failedBorrow = MockRequest.send(pool, maxQueueSize); | |
| 144 | 113 | try { |
| 145 | - MockRequest.send(pool); | |
| 146 | - Assertions.fail("Expected a TimeoutException"); | |
| 147 | - } catch (TimeoutException e) { /*expected*/} | |
| 114 | + failedBorrow.getConnection(); | |
| 115 | + fail("Expected a ConnectionException"); | |
| 116 | + } catch (ConnectionException e) { /*expected*/} | |
| 148 | 117 | } finally { |
| 149 | - completeRequests(requests); | |
| 118 | + MockRequest.completeAll(allRequests); | |
| 150 | 119 | cluster.close(); |
| 151 | 120 | } |
| 152 | 121 | } |
| 153 | 122 | |
| 154 | 123 | /** |
| 155 | 124 | * Ensures that if a variable-sized pool has filled up to its maximum connections that borrowConnection will |
| 156 | - * timeout instead of creating a new connection. | |
| 125 | + * return a failed future instead of creating a new connection. | |
| 157 | 126 | * |
| 158 | 127 | * @jira_ticket JAVA-419 |
| 159 | 128 | * @test_category connection:connection_pool |
| @@ -162,41 +131,42 @@ | ||
| 162 | 131 | @Test(groups = "short") |
| 163 | 132 | public void variable_size_pool_should_fill_its_connections_and_then_timeout() throws Exception { |
| 164 | 133 | Cluster cluster = createClusterBuilder().build(); |
| 165 | - List<MockRequest> requests = newArrayList(); | |
| 134 | + List<MockRequest> allRequests = newArrayList(); | |
| 166 | 135 | try { |
| 167 | 136 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| 168 | 137 | Connection.Factory factory = spy(cluster.manager.connectionFactory); |
| 169 | 138 | cluster.manager.connectionFactory = factory; |
| 170 | 139 | |
| 171 | 140 | assertThat(pool.connections.size()).isEqualTo(1); |
| 172 | - List<Connection> coreConnections = newArrayList(pool.connections); | |
| 141 | + Connection coreConnection = pool.connections.get(0); | |
| 173 | 142 | |
| 174 | 143 | // Fill enough connections to hit the threshold. |
| 175 | - requests.addAll(fillConnectionToThreshold(pool, coreConnections)); | |
| 144 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 145 | + assertBorrowedConnection(requests, coreConnection); | |
| 146 | + allRequests.addAll(requests); | |
| 147 | + allRequests.add(MockRequest.send(pool)); | |
| 176 | 148 | |
| 177 | 149 | // Allow time for new connection to be spawned. |
| 178 | 150 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 179 | 151 | assertPoolSize(pool, 2); |
| 180 | 152 | |
| 181 | 153 | // Borrow more and ensure the connection returned is a non-core connection. |
| 182 | - for (int i = 0; i < 100; i++) { | |
| 154 | + for (int i = 0; i < NEW_CONNECTION_THRESHOLD; i++) { | |
| 183 | 155 | MockRequest request = MockRequest.send(pool); |
| 184 | - assertThat(coreConnections).doesNotContain(request.connection); | |
| 185 | - requests.add(request); | |
| 156 | + assertThat(request.getConnection()).isNotEqualTo(coreConnection); | |
| 157 | + allRequests.add(request); | |
| 186 | 158 | } |
| 187 | 159 | |
| 188 | 160 | // Fill remaining connections (28 + 28) - 1 |
| 189 | - requests.addAll(sendRequests(55, pool)); | |
| 161 | + allRequests.addAll(MockRequest.sendMany(55, pool)); | |
| 190 | 162 | |
| 191 | - boolean timedOut = false; | |
| 163 | + MockRequest failedBorrow = MockRequest.send(pool); | |
| 192 | 164 | try { |
| 193 | - MockRequest.send(pool); | |
| 194 | - } catch (TimeoutException e) { | |
| 195 | - timedOut = true; | |
| 196 | - } | |
| 197 | - assertThat(timedOut).isTrue(); | |
| 165 | + failedBorrow.getConnection(); | |
| 166 | + fail("Expected a ConnectionException"); | |
| 167 | + } catch (ConnectionException e) { /*expected*/} | |
| 198 | 168 | } finally { |
| 199 | - completeRequests(requests); | |
| 169 | + MockRequest.completeAll(allRequests); | |
| 200 | 170 | cluster.close(); |
| 201 | 171 | } |
| 202 | 172 | } |
| @@ -211,7 +181,7 @@ | ||
| 211 | 181 | @Test(groups = "short") |
| 212 | 182 | public void should_add_extra_connection_when_core_full() throws Exception { |
| 213 | 183 | Cluster cluster = createClusterBuilder().build(); |
| 214 | - List<MockRequest> requests = newArrayList(); | |
| 184 | + List<MockRequest> allRequests = newArrayList(); | |
| 215 | 185 | try { |
| 216 | 186 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| 217 | 187 | Connection.Factory factory = spy(cluster.manager.connectionFactory); |
| @@ -219,13 +189,16 @@ | ||
| 219 | 189 | Connection core = pool.connections.get(0); |
| 220 | 190 | |
| 221 | 191 | // Fill core connection + 1 |
| 222 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(core))); | |
| 192 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 193 | + assertBorrowedConnection(requests, core); | |
| 194 | + allRequests.addAll(requests); | |
| 195 | + allRequests.add(MockRequest.send(pool)); | |
| 223 | 196 | |
| 224 | 197 | // Reaching the threshold should have triggered the creation of an extra one |
| 225 | 198 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 226 | 199 | assertPoolSize(pool, 2); |
| 227 | 200 | } finally { |
| 228 | - completeRequests(requests); | |
| 201 | + MockRequest.completeAll(allRequests); | |
| 229 | 202 | cluster.close(); |
| 230 | 203 | } |
| 231 | 204 | } |
| @@ -241,14 +214,17 @@ | ||
| 241 | 214 | @Test(groups = "long") |
| 242 | 215 | public void should_resurrect_trashed_connection_within_idle_timeout() throws Exception { |
| 243 | 216 | Cluster cluster = createClusterBuilder().withPoolingOptions(new PoolingOptions().setIdleTimeoutSeconds(20)).build(); |
| 244 | - List<MockRequest> requests = newArrayList(); | |
| 217 | + List<MockRequest> allRequests = newArrayList(); | |
| 245 | 218 | try { |
| 246 | 219 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| 247 | 220 | Connection.Factory factory = spy(cluster.manager.connectionFactory); |
| 248 | 221 | cluster.manager.connectionFactory = factory; |
| 249 | 222 | Connection connection1 = pool.connections.get(0); |
| 250 | 223 | |
| 251 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); | |
| 224 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 225 | + assertBorrowedConnections(requests, Collections.singletonList(connection1)); | |
| 226 | + allRequests.addAll(requests); | |
| 227 | + allRequests.add(MockRequest.send(pool)); | |
| 252 | 228 | |
| 253 | 229 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 254 | 230 | assertPoolSize(pool, 2); |
| @@ -258,7 +234,7 @@ | ||
| 258 | 234 | assertThat(connection2.inFlight.get()).isEqualTo(0); |
| 259 | 235 | |
| 260 | 236 | // Go back under the capacity of 1 connection |
| 261 | - completeRequests(51, requests); | |
| 237 | + MockRequest.completeMany(51, allRequests); | |
| 262 | 238 | |
| 263 | 239 | assertThat(connection1.inFlight.get()).isEqualTo(50); |
| 264 | 240 | assertThat(connection2.inFlight.get()).isEqualTo(0); |
| @@ -269,7 +245,7 @@ | ||
| 269 | 245 | assertThat(pool.trash).containsExactly(connection1); |
| 270 | 246 | |
| 271 | 247 | // Now borrow enough to go just under the 1 connection threshold |
| 272 | - requests.addAll(sendRequests(50, pool)); | |
| 248 | + allRequests.addAll(MockRequest.sendMany(50, pool)); | |
| 273 | 249 | |
| 274 | 250 | assertThat(pool.connections).containsExactly(connection2); |
| 275 | 251 | assertThat(pool.trash).containsExactly(connection1); |
| @@ -277,7 +253,7 @@ | ||
| 277 | 253 | assertThat(connection2.inFlight.get()).isEqualTo(50); |
| 278 | 254 | |
| 279 | 255 | // Borrowing one more time should resurrect the trashed connection |
| 280 | - requests.addAll(sendRequests(1, pool)); | |
| 256 | + allRequests.addAll(MockRequest.sendMany(1, pool)); | |
| 281 | 257 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 282 | 258 | assertPoolSize(pool, 2); |
| 283 | 259 | |
| @@ -286,7 +262,7 @@ | ||
| 286 | 262 | assertThat(connection1.inFlight.get()).isEqualTo(50); |
| 287 | 263 | assertThat(connection2.inFlight.get()).isEqualTo(51); |
| 288 | 264 | } finally { |
| 289 | - completeRequests(requests); | |
| 265 | + MockRequest.completeAll(allRequests); | |
| 290 | 266 | cluster.close(); |
| 291 | 267 | } |
| 292 | 268 | } |
| @@ -302,14 +278,17 @@ | ||
| 302 | 278 | @Test(groups = "long") |
| 303 | 279 | public void should_not_resurrect_trashed_connection_after_idle_timeout() throws Exception { |
| 304 | 280 | Cluster cluster = createClusterBuilder().withPoolingOptions(new PoolingOptions().setIdleTimeoutSeconds(20)).build(); |
| 305 | - List<MockRequest> requests = newArrayList(); | |
| 281 | + List<MockRequest> allRequests = newArrayList(); | |
| 306 | 282 | try { |
| 307 | 283 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| 308 | 284 | Connection.Factory factory = spy(cluster.manager.connectionFactory); |
| 309 | 285 | cluster.manager.connectionFactory = factory; |
| 310 | 286 | Connection connection1 = pool.connections.get(0); |
| 311 | 287 | |
| 312 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); | |
| 288 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 289 | + assertBorrowedConnection(requests, connection1); | |
| 290 | + allRequests.addAll(requests); | |
| 291 | + allRequests.add(MockRequest.send(pool)); | |
| 313 | 292 | |
| 314 | 293 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 315 | 294 | assertPoolSize(pool, 2); |
| @@ -320,7 +299,7 @@ | ||
| 320 | 299 | assertThat(connection2.inFlight.get()).isEqualTo(0); |
| 321 | 300 | |
| 322 | 301 | // Go back under the capacity of 1 connection |
| 323 | - completeRequests(51, requests); | |
| 302 | + MockRequest.completeMany(51, allRequests); | |
| 324 | 303 | |
| 325 | 304 | assertThat(connection1.inFlight.get()).isEqualTo(50); |
| 326 | 305 | assertThat(connection2.inFlight.get()).isEqualTo(0); |
| @@ -331,7 +310,7 @@ | ||
| 331 | 310 | assertThat(pool.trash).containsExactly(connection1); |
| 332 | 311 | |
| 333 | 312 | // Return trashed connection down to 0 inFlight |
| 334 | - completeRequests(50, requests); | |
| 313 | + MockRequest.completeMany(50, allRequests); | |
| 335 | 314 | assertThat(connection1.inFlight.get()).isEqualTo(0); |
| 336 | 315 | |
| 337 | 316 | // Give enough time for trashed connection to be cleaned up from the trash: |
| @@ -341,19 +320,22 @@ | ||
| 341 | 320 | assertThat(connection1.isClosed()).isTrue(); |
| 342 | 321 | |
| 343 | 322 | // Fill the live connection to go over the threshold where a second one is needed |
| 344 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(connection2))); | |
| 323 | + requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 324 | + assertBorrowedConnection(requests, connection2); | |
| 325 | + allRequests.addAll(requests); | |
| 326 | + allRequests.add(MockRequest.send(pool)); | |
| 345 | 327 | assertThat(connection2.inFlight.get()).isEqualTo(101); |
| 346 | 328 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 347 | 329 | assertPoolSize(pool, 2); |
| 348 | 330 | |
| 349 | 331 | // Borrow again to get the new connection |
| 350 | 332 | MockRequest request = MockRequest.send(pool); |
| 351 | - requests.add(request); | |
| 352 | - assertThat(request.connection) | |
| 333 | + allRequests.add(request); | |
| 334 | + assertThat(request.getConnection()) | |
| 353 | 335 | .isNotEqualTo(connection2) // should not be the full connection |
| 354 | 336 | .isNotEqualTo(connection1); // should not be the previously trashed one |
| 355 | 337 | } finally { |
| 356 | - completeRequests(requests); | |
| 338 | + MockRequest.completeAll(allRequests); | |
| 357 | 339 | cluster.close(); |
| 358 | 340 | } |
| 359 | 341 | } |
| @@ -368,7 +350,7 @@ | ||
| 368 | 350 | @Test(groups = "long") |
| 369 | 351 | public void should_not_close_trashed_connection_until_no_in_flight() throws Exception { |
| 370 | 352 | Cluster cluster = createClusterBuilder().withPoolingOptions(new PoolingOptions().setIdleTimeoutSeconds(20)).build(); |
| 371 | - List<MockRequest> requests = newArrayList(); | |
| 353 | + List<MockRequest> allRequests = newArrayList(); | |
| 372 | 354 | |
| 373 | 355 | try { |
| 374 | 356 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| @@ -377,13 +359,16 @@ | ||
| 377 | 359 | Connection connection1 = pool.connections.get(0); |
| 378 | 360 | |
| 379 | 361 | // Fill core connection enough to trigger creation of another one |
| 380 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(connection1))); | |
| 362 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 363 | + assertBorrowedConnections(requests, Collections.singletonList(connection1)); | |
| 364 | + allRequests.addAll(requests); | |
| 365 | + allRequests.add(MockRequest.send(pool)); | |
| 381 | 366 | |
| 382 | 367 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 383 | 368 | assertThat(pool.connections).hasSize(2); |
| 384 | 369 | |
| 385 | 370 | // Return enough times to get back under the threshold where one connection is enough |
| 386 | - completeRequests(50, requests); | |
| 371 | + MockRequest.completeMany(50, allRequests); | |
| 387 | 372 | |
| 388 | 373 | // Give enough time for one connection to be trashed. Due to the implementation, this will be the first one. |
| 389 | 374 | // It still has in-flight requests so should not get closed. |
| @@ -393,7 +378,7 @@ | ||
| 393 | 378 | assertThat(connection1.isClosed()).isFalse(); |
| 394 | 379 | |
| 395 | 380 | // Consume all inFlight requests on the trashed connection. |
| 396 | - completeRequests(51, requests); | |
| 381 | + MockRequest.completeMany(51, allRequests); | |
| 397 | 382 | |
| 398 | 383 | // Sleep enough time for the connection to be consider idled and closed. |
| 399 | 384 | Uninterruptibles.sleepUninterruptibly(30, TimeUnit.SECONDS); |
| @@ -404,7 +389,7 @@ | ||
| 404 | 389 | assertThat(pool.connections).doesNotContain(connection1); |
| 405 | 390 | assertThat(pool.trash).doesNotContain(connection1); |
| 406 | 391 | } finally { |
| 407 | - completeRequests(requests); | |
| 392 | + MockRequest.completeAll(allRequests); | |
| 408 | 393 | cluster.close(); |
| 409 | 394 | } |
| 410 | 395 | } |
| @@ -420,14 +405,17 @@ | ||
| 420 | 405 | @Test(groups = "short") |
| 421 | 406 | public void should_trash_on_returning_connection_with_insufficient_streams() throws Exception { |
| 422 | 407 | Cluster cluster = createClusterBuilder().build(); |
| 423 | - List<MockRequest> requests = newArrayList(); | |
| 408 | + List<MockRequest> allRequests = newArrayList(); | |
| 424 | 409 | try { |
| 425 | 410 | HostConnectionPool pool = createPool(cluster, 1, 2); |
| 426 | 411 | Connection.Factory factory = spy(cluster.manager.connectionFactory); |
| 427 | 412 | cluster.manager.connectionFactory = factory; |
| 428 | 413 | Connection core = pool.connections.get(0); |
| 429 | 414 | |
| 430 | - requests.addAll(fillConnectionToThreshold(pool, singletonList(core))); | |
| 415 | + List<MockRequest> requests = MockRequest.sendMany(NEW_CONNECTION_THRESHOLD, pool); | |
| 416 | + assertBorrowedConnections(requests, Collections.singletonList(core)); | |
| 417 | + allRequests.addAll(requests); | |
| 418 | + allRequests.add(MockRequest.send(pool)); | |
| 431 | 419 | |
| 432 | 420 | verify(factory, after(2000).times(1)).open(any(HostConnectionPool.class)); |
| 433 | 421 | assertThat(pool.connections).hasSize(2); |
| @@ -437,7 +425,7 @@ | ||
| 437 | 425 | pool.connections.set(1, extra1); |
| 438 | 426 | |
| 439 | 427 | // Borrow 10 times to ensure pool is utilized. |
| 440 | - requests.addAll(sendRequests(10, pool)); | |
| 428 | + allRequests.addAll(MockRequest.sendMany(10, pool)); | |
| 441 | 429 | assertThat(pool.connections).hasSize(2); |
| 442 | 430 | |
| 443 | 431 | // stub the maxAvailableStreams method to return 0, indicating there are no remaining streams. |
| @@ -449,7 +437,7 @@ | ||
| 449 | 437 | pool.returnConnection(extra1); |
| 450 | 438 | assertThat(pool.trash).hasSize(1); |
| 451 | 439 | } finally { |
| 452 | - completeRequests(requests); | |
| 440 | + MockRequest.completeAll(allRequests); | |
| 453 | 441 | cluster.close(); |
| 454 | 442 | } |
| 455 | 443 | } |
| @@ -580,7 +568,7 @@ | ||
| 580 | 568 | .setConnectTimeoutMillis(readTimeout) |
| 581 | 569 | .setReadTimeoutMillis(reconnectInterval)) |
| 582 | 570 | .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)).build(); |
| 583 | - List<MockRequest> requests = newArrayList(); | |
| 571 | + List<MockRequest> allRequests = newArrayList(); | |
| 584 | 572 | try { |
| 585 | 573 | cluster.init(); |
| 586 | 574 | |
| @@ -608,8 +596,8 @@ | ||
| 608 | 596 | |
| 609 | 597 | // The borrowed connection should be the open one. |
| 610 | 598 | MockRequest request = MockRequest.send(pool); |
| 611 | - requests.add(request); | |
| 612 | - assertThat(request.connection).isEqualTo(core1); | |
| 599 | + allRequests.add(request); | |
| 600 | + assertThat(request.getConnection()).isEqualTo(core1); | |
| 613 | 601 | |
| 614 | 602 | // Should not have tried to create a new core connection since reconnection time had not elapsed. |
| 615 | 603 | verify(factory, never()).open(any(HostConnectionPool.class)); |
| @@ -620,8 +608,8 @@ | ||
| 620 | 608 | // Attempt to borrow connection, this should trigger ensureCoreConnections thus spawning a new connection. |
| 621 | 609 | blockingExecutor.reset(); |
| 622 | 610 | request = MockRequest.send(pool); |
| 623 | - requests.add(request); | |
| 624 | - assertThat(request.connection).isEqualTo(core1); | |
| 611 | + allRequests.add(request); | |
| 612 | + assertThat(request.getConnection()).isEqualTo(core1); | |
| 625 | 613 | |
| 626 | 614 | // Should have tried to open up to core connections as result of borrowing a connection past reconnect time and not being at core. |
| 627 | 615 | blockingExecutor.blockUntilNextTaskCompleted(); |
| @@ -638,23 +626,23 @@ | ||
| 638 | 626 | |
| 639 | 627 | // Try to borrow a connection, the pool should grow. |
| 640 | 628 | blockingExecutor.reset(); |
| 641 | - requests.add(MockRequest.send(pool)); | |
| 629 | + allRequests.add(MockRequest.send(pool)); | |
| 642 | 630 | blockingExecutor.blockUntilNextTaskCompleted(); |
| 643 | 631 | verify(factory).open(any(HostConnectionPool.class)); |
| 644 | 632 | reset(factory); |
| 645 | 633 | |
| 646 | 634 | // Another core connection should be opened as result of another request to get us up to core connections. |
| 647 | 635 | blockingExecutor.reset(); |
| 648 | - requests.add(MockRequest.send(pool)); | |
| 636 | + allRequests.add(MockRequest.send(pool)); | |
| 649 | 637 | blockingExecutor.blockUntilNextTaskCompleted(); |
| 650 | 638 | verify(factory).open(any(HostConnectionPool.class)); |
| 651 | 639 | reset(factory); |
| 652 | 640 | |
| 653 | 641 | // Sending another request should not grow the pool any more, since we are now at core connections. |
| 654 | - requests.add(MockRequest.send(pool)); | |
| 642 | + allRequests.add(MockRequest.send(pool)); | |
| 655 | 643 | verify(factory, after((reconnectInterval + readTimeout) * 2).never()).open(any(HostConnectionPool.class)); |
| 656 | 644 | } finally { |
| 657 | - completeRequests(requests); | |
| 645 | + MockRequest.completeAll(allRequests); | |
| 658 | 646 | cluster.close(); |
| 659 | 647 | } |
| 660 | 648 | } |
| @@ -677,7 +665,7 @@ | ||
| 677 | 665 | .setConnectTimeoutMillis(readTimeout) |
| 678 | 666 | .setReadTimeoutMillis(reconnectInterval)) |
| 679 | 667 | .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)).build(); |
| 680 | - List<MockRequest> requests = newArrayList(); | |
| 668 | + List<MockRequest> allRequests = newArrayList(); | |
| 681 | 669 | try { |
| 682 | 670 | cluster.init(); |
| 683 | 671 | |
| @@ -691,7 +679,7 @@ | ||
| 691 | 679 | List<MockRequest> core0requests = newArrayList(); |
| 692 | 680 | for (int i = 0; i < 101; i++) { |
| 693 | 681 | MockRequest request = MockRequest.send(pool); |
| 694 | - assertThat(request.connection).isEqualTo(core0); | |
| 682 | + assertThat(request.getConnection()).isEqualTo(core0); | |
| 695 | 683 | core0requests.add(request); |
| 696 | 684 | } |
| 697 | 685 | |
| @@ -712,13 +700,15 @@ | ||
| 712 | 700 | |
| 713 | 701 | // Since core0 was closed, all of it's requests should have errored. |
| 714 | 702 | for (MockRequest request : core0requests) { |
| 715 | - verify(request, times(1)).onException(any(Connection.class), any(Exception.class), anyLong(), anyInt()); | |
| 703 | + assertThat(request.state.get()).isEqualTo(MockRequest.State.FAILED); | |
| 716 | 704 | } |
| 717 | 705 | |
| 718 | 706 | assertThat(cluster).host(1).hasState(Host.State.UP); |
| 719 | 707 | |
| 720 | 708 | // Create enough inFlight requests to fill connection. |
| 721 | - requests.addAll(sendRequests(100, pool, singletonList(extra1))); | |
| 709 | + List<MockRequest> requests = MockRequest.sendMany(100, pool); | |
| 710 | + assertBorrowedConnections(requests, Collections.singletonList(extra1)); | |
| 711 | + allRequests.addAll(requests); | |
| 722 | 712 | assertThat(pool.connections).hasSize(1); |
| 723 | 713 | |
| 724 | 714 | // A new connection should never have been spawned since we didn't max out core. |
| @@ -726,8 +716,8 @@ | ||
| 726 | 716 | |
| 727 | 717 | // Borrow another connection, since we exceed max another connection should be opened. |
| 728 | 718 | MockRequest request = MockRequest.send(pool); |
| 729 | - requests.add(request); | |
| 730 | - assertThat(request.connection).isEqualTo(extra1); | |
| 719 | + allRequests.add(request); | |
| 720 | + assertThat(request.getConnection()).isEqualTo(extra1); | |
| 731 | 721 | |
| 732 | 722 | // After some time the a connection should attempt to be opened (but will fail). |
| 733 | 723 | verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class)); |
| @@ -745,8 +735,8 @@ | ||
| 745 | 735 | |
| 746 | 736 | // Borrow another connection, since we exceed max another connection should be opened. |
| 747 | 737 | request = MockRequest.send(pool); |
| 748 | - requests.add(request); | |
| 749 | - assertThat(request.connection).isEqualTo(extra1); | |
| 738 | + allRequests.add(request); | |
| 739 | + assertThat(request.getConnection()).isEqualTo(extra1); | |
| 750 | 740 | |
| 751 | 741 | // Wait some reasonable amount of time for connection to reestablish then check pool size. |
| 752 | 742 | Uninterruptibles.sleepUninterruptibly(readTimeout, TimeUnit.MILLISECONDS); |
| @@ -755,10 +745,10 @@ | ||
| 755 | 745 | |
| 756 | 746 | // Borrowed connection should be the newly spawned connection since the other one has some inflight requests. |
| 757 | 747 | request = MockRequest.send(pool); |
| 758 | - requests.add(request); | |
| 759 | - assertThat(request.connection).isNotEqualTo(core0).isNotEqualTo(extra1); | |
| 748 | + allRequests.add(request); | |
| 749 | + assertThat(request.getConnection()).isNotEqualTo(core0).isNotEqualTo(extra1); | |
| 760 | 750 | } finally { |
| 761 | - completeRequests(requests); | |
| 751 | + MockRequest.completeAll(allRequests); | |
| 762 | 752 | cluster.close(); |
| 763 | 753 | } |
| 764 | 754 | } |
| @@ -781,7 +771,7 @@ | ||
| 781 | 771 | .setConnectTimeoutMillis(readTimeout) |
| 782 | 772 | .setReadTimeoutMillis(reconnectInterval)) |
| 783 | 773 | .withReconnectionPolicy(new ConstantReconnectionPolicy(1000)).build(); |
| 784 | - List<MockRequest> requests = newArrayList(); | |
| 774 | + List<MockRequest> allRequests = newArrayList(); | |
| 785 | 775 | try { |
| 786 | 776 | cluster.init(); |
| 787 | 777 | |
| @@ -811,13 +801,13 @@ | ||
| 811 | 801 | // we need to wait for connections to be established between requests for the pool |
| 812 | 802 | // to spawn new connections (since it only allows one simultaneous creation). |
| 813 | 803 | for (int i = 5; i <= 8; i++) { |
| 814 | - requests.add(MockRequest.send(pool)); | |
| 804 | + allRequests.add(MockRequest.send(pool)); | |
| 815 | 805 | verify(factory, timeout(readTimeout)).open(any(HostConnectionPool.class)); |
| 816 | 806 | reset(factory); |
| 817 | 807 | assertPoolSize(pool, i); |
| 818 | 808 | } |
| 819 | 809 | } finally { |
| 820 | - completeRequests(requests); | |
| 810 | + MockRequest.completeAll(allRequests); | |
| 821 | 811 | cluster.close(); |
| 822 | 812 | } |
| 823 | 813 | } |
| @@ -825,15 +815,15 @@ | ||
| 825 | 815 | /** |
| 826 | 816 | * Ensures that if all connections fail on pool init that the host and subsequently the |
| 827 | 817 | * control connection is not marked down since the control connection is still active. |
| 828 | - * on the pool that a TimeoutException is yielded if we are still in the reconnection window | |
| 818 | + * The test also ensures that borrow attempts on the pool fail if we are still in the reconnection window | |
| 829 | 819 | * according to the ConvictionPolicy. |
| 830 | 820 | * |
| 831 | 821 | * @jira_ticket JAVA-544 |
| 832 | 822 | * @test_category connection:connection_pool |
| 833 | 823 | * @since 2.0.11 |
| 834 | 824 | */ |
| 835 | - @Test(groups = "short", expectedExceptions = TimeoutException.class) | |
| 836 | - public void should_throw_exception_if_convicted_and_no_connections_available() throws Exception { | |
| 825 | + @Test(groups = "short") | |
| 826 | + public void should_throw_exception_if_convicted_and_no_connections_available() { | |
| 837 | 827 | int readTimeout = 1000; |
| 838 | 828 | int reconnectInterval = 1000; |
| 839 | 829 | Cluster cluster = this.createClusterBuilder() |
| @@ -862,7 +852,11 @@ | ||
| 862 | 852 | assertThat(cluster).host(1).hasState(Host.State.UP); |
| 863 | 853 | assertThat(cluster).hasOpenControlConnection(); |
| 864 | 854 | |
| 865 | - MockRequest.send(pool); | |
| 855 | + MockRequest failedBorrow = MockRequest.send(pool); | |
| 856 | + try { | |
| 857 | + failedBorrow.getConnection(); | |
| 858 | + fail("Expected a ConnectionException"); | |
| 859 | + } catch (ConnectionException e) { /*expected*/} | |
| 866 | 860 | } finally { |
| 867 | 861 | cluster.close(); |
| 868 | 862 | } |
| @@ -871,7 +865,7 @@ | ||
| 871 | 865 | |
| 872 | 866 | /** |
| 873 | 867 | * Ensures that if all connections fail on pool init that the host and subsequently the |
| 874 | - * control connection is not marked down. The test also ensures that when making requests | |
| 868 | + * control connection is not marked down. The test also ensures that when making requests | |
| 875 | 869 | * on the pool after the conviction period that all core connections are created. |
| 876 | 870 | * |
| 877 | 871 | * @jira_ticket JAVA-544 |
| @@ -912,12 +906,13 @@ | ||
| 912 | 906 | Uninterruptibles.sleepUninterruptibly(reconnectInterval, TimeUnit.MILLISECONDS); |
| 913 | 907 | |
| 914 | 908 | reset(factory); |
| 915 | - MockRequest request = MockRequest.send(pool); | |
| 916 | - request.simulateSuccessResponse(); | |
| 909 | + MockRequest request = MockRequest.send(pool, 1); | |
| 917 | 910 | |
| 918 | 911 | // Should create up to core connections. |
| 919 | 912 | verify(factory, timeout(readTimeout * 8).times(8)).open(any(HostConnectionPool.class)); |
| 920 | 913 | assertPoolSize(pool, 8); |
| 914 | + | |
| 915 | + request.simulateSuccessResponse(); | |
| 921 | 916 | } finally { |
| 922 | 917 | cluster.close(); |
| 923 | 918 | } |
| @@ -946,6 +941,9 @@ | ||
| 946 | 941 | cluster.init(); |
| 947 | 942 | assertThat(cluster).hasOpenControlConnection(); |
| 948 | 943 | |
| 944 | + Connection.Factory factory = spy(cluster.manager.connectionFactory); | |
| 945 | + cluster.manager.connectionFactory = factory; | |
| 946 | + | |
| 949 | 947 | HostConnectionPool pool = createPool(cluster, 0, 2); |
| 950 | 948 | |
| 951 | 949 | // Pool should be empty. |
| @@ -955,12 +953,14 @@ | ||
| 955 | 953 | assertThat(cluster).host(1).hasState(Host.State.UP); |
| 956 | 954 | assertThat(cluster).hasOpenControlConnection(); |
| 957 | 955 | |
| 958 | - // Send a request, this should create a connection. | |
| 959 | - MockRequest request = MockRequest.send(pool); | |
| 960 | - request.simulateSuccessResponse(); | |
| 956 | + reset(factory); | |
| 957 | + MockRequest request = MockRequest.send(pool, 1); | |
| 961 | 958 | |
| 962 | 959 | // Should create up to core connections. |
| 960 | + verify(factory, timeout(readTimeout).times(1)).open(any(HostConnectionPool.class)); | |
| 963 | 961 | assertThat(pool.connections).hasSize(1); |
| 962 | + | |
| 963 | + request.simulateSuccessResponse(); | |
| 964 | 964 | } finally { |
| 965 | 965 | cluster.close(); |
| 966 | 966 | } |
| @@ -979,10 +979,10 @@ | ||
| 979 | 979 | } |
| 980 | 980 | |
| 981 | 981 | /** |
| 982 | - * <p> | |
| 982 | + * <p/> | |
| 983 | 983 | * This test uses a table named "Java349" with 1000 column and performs asynchronously 100k insertions. While the |
| 984 | 984 | * insertions are being executed, the number of opened connection is monitored. |
| 985 | - * <p> | |
| 985 | + * <p/> | |
| 986 | 986 | * If at anytime, the number of opened connections is negative, this test will fail. |
| 987 | 987 | * |
| 988 | 988 | * @jira_ticket JAVA-349 |
| @@ -1061,51 +1061,115 @@ | ||
| 1061 | 1061 | |
| 1062 | 1062 | enum State {START, COMPLETED, FAILED, TIMED_OUT} |
| 1063 | 1063 | |
| 1064 | - final Connection connection; | |
| 1065 | - private Connection.ResponseHandler responseHandler; | |
| 1064 | + final ListenableFuture<Connection> connectionFuture; | |
| 1065 | + private volatile Connection.ResponseHandler responseHandler; | |
| 1066 | + | |
| 1067 | + final AtomicReference<State> state = new AtomicReference<State>(State.START); | |
| 1068 | + | |
| 1069 | + static MockRequest send(HostConnectionPool pool) { | |
| 1070 | + return send(pool, 0); | |
| 1071 | + } | |
| 1072 | + | |
| 1073 | + static MockRequest send(HostConnectionPool pool, int maxQueueSize) throws ConnectionException, BusyConnectionException { | |
| 1074 | + return new MockRequest(pool, maxQueueSize); | |
| 1075 | + } | |
| 1076 | + | |
| 1077 | + private static List<MockRequest> sendMany(int count, HostConnectionPool pool) throws ConnectionException { | |
| 1078 | + return sendMany(count, pool, 0); | |
| 1079 | + } | |
| 1080 | + | |
| 1081 | + private static List<MockRequest> sendMany(int count, HostConnectionPool pool, int maxQueueSize) throws ConnectionException { | |
| 1082 | + List<MockRequest> requests = newArrayList(); | |
| 1083 | + for (int i = 0; i < count; i++) { | |
| 1084 | + MockRequest request = send(pool, maxQueueSize); | |
| 1085 | + requests.add(request); | |
| 1086 | + } | |
| 1087 | + return requests; | |
| 1088 | + } | |
| 1066 | 1089 | |
| 1067 | - private final AtomicReference<State> state = new AtomicReference<State>(State.START); | |
| 1090 | + /** | |
| 1091 | + * Completes count requests by simulating a successful response. | |
| 1092 | + */ | |
| 1093 | + private static void completeMany(int count, List<MockRequest> requests) { | |
| 1094 | + Iterator<MockRequest> requestIt = requests.iterator(); | |
| 1095 | + for (int i = 0; i < count; i++) { | |
| 1096 | + if (requestIt.hasNext()) { | |
| 1097 | + MockRequest request = requestIt.next(); | |
| 1098 | + request.simulateSuccessResponse(); | |
| 1099 | + requestIt.remove(); | |
| 1100 | + } else { | |
| 1101 | + break; | |
| 1102 | + } | |
| 1103 | + } | |
| 1104 | + } | |
| 1068 | 1105 | |
| 1069 | - static MockRequest send(HostConnectionPool pool) throws ConnectionException, BusyConnectionException, TimeoutException { | |
| 1070 | - // Create a MockRequest and spy on it. Create a response handler and add it to the connection's dispatcher. | |
| 1071 | - MockRequest request = spy(new MockRequest(pool)); | |
| 1072 | - request.responseHandler = new Connection.ResponseHandler(request.connection, -1, request); | |
| 1073 | - request.connection.dispatcher.add(request.responseHandler); | |
| 1074 | - return request; | |
| 1106 | + /** | |
| 1107 | + * Completes all requests by simulating a successful response. | |
| 1108 | + */ | |
| 1109 | + private static void completeAll(List<MockRequest> requests) { | |
| 1110 | + for (MockRequest request : requests) { | |
| 1111 | + request.simulateSuccessResponse(); | |
| 1112 | + } | |
| 1075 | 1113 | } |
| 1076 | 1114 | |
| 1077 | - private MockRequest(HostConnectionPool pool) throws ConnectionException, TimeoutException, BusyConnectionException { | |
| 1078 | - connection = pool.borrowConnection(500, TimeUnit.MILLISECONDS); | |
| 1115 | + private MockRequest(HostConnectionPool pool, int maxQueueSize) throws ConnectionException { | |
| 1116 | + this.connectionFuture = pool.borrowConnection(maxQueueSize); | |
| 1117 | + Futures.addCallback(this.connectionFuture, new MoreFutures.SuccessCallback<Connection>() { | |
| 1118 | + @Override | |
| 1119 | + public void onSuccess(Connection connection) { | |
| 1120 | + MockRequest thisRequest = MockRequest.this; | |
| 1121 | + thisRequest.responseHandler = new Connection.ResponseHandler(connection, -1, thisRequest); | |
| 1122 | + connection.dispatcher.add(thisRequest.responseHandler); | |
| 1123 | + } | |
| 1124 | + }); | |
| 1079 | 1125 | } |
| 1080 | 1126 | |
| 1081 | 1127 | void simulateSuccessResponse() { |
| 1082 | - onSet(connection, null, 0, 0); | |
| 1128 | + onSet(getConnection(), null, 0, 0); | |
| 1083 | 1129 | } |
| 1084 | 1130 | |
| 1085 | 1131 | @SuppressWarnings("unused") |
| 1086 | 1132 | void simulateErrorResponse() { |
| 1087 | - onException(connection, null, 0, 0); | |
| 1133 | + onException(getConnection(), null, 0, 0); | |
| 1088 | 1134 | } |
| 1089 | 1135 | |
| 1090 | 1136 | @SuppressWarnings("unused") |
| 1091 | 1137 | void simulateTimeout() { |
| 1092 | - if (onTimeout(connection, 0, 0)) | |
| 1138 | + if (onTimeout(getConnection(), 0, 0)) | |
| 1093 | 1139 | responseHandler.cancelHandler(); |
| 1094 | 1140 | } |
| 1095 | 1141 | |
| 1142 | + // Get the connection, assuming pool acquisition completed | |
| 1143 | + Connection getConnection() { | |
| 1144 | + try { | |
| 1145 | + if (!connectionFuture.isDone()) { | |
| 1146 | + throw new AssertionError("Borrow call did not complete"); | |
| 1147 | + } | |
| 1148 | + return Uninterruptibles.getUninterruptibly(connectionFuture); | |
| 1149 | + } catch (ExecutionException e) { | |
| 1150 | + throw Throwables.propagate(e.getCause()); | |
| 1151 | + } | |
| 1152 | + } | |
| 1153 | + | |
| 1096 | 1154 | @Override |
| 1097 | 1155 | public void onSet(Connection connection, Message.Response response, long latency, int retryCount) { |
| 1156 | + assertThat(connectionFuture.isDone()).isTrue(); | |
| 1157 | + try { | |
| 1158 | + assertThat(Uninterruptibles.getUninterruptibly(connectionFuture)).isNotNull(); | |
| 1159 | + } catch (ExecutionException e) { | |
| 1160 | + Throwables.propagate(e.getCause()); | |
| 1161 | + } | |
| 1098 | 1162 | if (state.compareAndSet(State.START, State.COMPLETED)) { |
| 1099 | - connection.release(); | |
| 1100 | 1163 | connection.dispatcher.removeHandler(responseHandler, true); |
| 1164 | + connection.release(); | |
| 1101 | 1165 | } |
| 1102 | 1166 | } |
| 1103 | 1167 | |
| 1104 | 1168 | @Override |
| 1105 | 1169 | public void onException(Connection connection, Exception exception, long latency, int retryCount) { |
| 1106 | 1170 | if (state.compareAndSet(State.START, State.FAILED)) { |
| 1107 | - connection.release(); | |
| 1108 | 1171 | connection.dispatcher.removeHandler(responseHandler, true); |
| 1172 | + connection.release(); | |
| 1109 | 1173 | } |
| 1110 | 1174 | } |
| 1111 | 1175 |
modified manual/async/README.md +0 −12
| @@ -130,21 +130,9 @@ | ||
| 130 | 130 | issues, you should create your `Cluster` instances while bootstrapping |
| 131 | 131 | your application, and call `init` immediately. If you need to create new |
| 132 | 132 | instances at runtime, make sure this does not happen on an I/O thread. |
| 133 | -* if a connection pool is busy, the driver will block until a connection | |
| 134 | - becomes available. To avoid this, set | |
| 135 | - [PoolingOptions.poolTimeoutMillis][setPoolTimeoutMillis] to 0; the | |
| 136 | - driver will not block, just move to the next host immediately. | |
| 137 | -* if the session is set to a specific keyspace (either at startup or by | |
| 138 | - issuing a `USE` statement on a running session), the keyspace needs to | |
| 139 | - be propagated to any newly created connection. This is done when the | |
| 140 | - connection is first borrowed from the connection pool, and currently | |
| 141 | - blocks. To avoid any issue, only use a session with no keyspace set | |
| 142 | - (i.e. created by `Cluster#connect()`). This will be addressed in | |
| 143 | - [JAVA-893](https://datastax-oss.atlassian.net/browse/JAVA-893). | |
| 144 | 133 | * trying to read fields from a [query trace] will block if the trace |
| 145 | 134 | hasn't been fetched already. |
| 146 | 135 | |
| 147 | 136 | [ListenableFuture]: https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained |
| 148 | 137 | [init]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Cluster.html#init-- |
| 149 | -[setPoolTimeoutMillis]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setPoolTimeoutMillis-int- | |
| 150 | 138 | [query trace]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/QueryTrace.html |
modified manual/pooling/README.md +24 −23
| @@ -180,24 +180,24 @@ | ||
| 180 | 180 | message. |
| 181 | 181 | |
| 182 | 182 | |
| 183 | -#### Acquisition timeout | |
| 183 | +#### Acquisition queue | |
| 184 | 184 | |
| 185 | 185 | When the driver tries to send a request to a host, it will first try to |
| 186 | 186 | acquire a connection from this host's pool. If the pool is busy (i.e. |
| 187 | 187 | all connections are already handling their maximum number of in flight |
| 188 | -requests), the client thread will block for a while, until a connection | |
| 189 | -becomes available (note that this will block even if you're using the | |
| 190 | -asynchronous API, like [Session.executeAsync][exec_async]). | |
| 188 | +requests), the acquisition attempt gets enqueued until a connection becomes | |
| 189 | +available again. | |
| 191 | 190 | |
| 192 | -The time that the driver blocks is controlled by | |
| 193 | -[PoolingOptions.setPoolTimeoutMillis][ptm]. If there is still no connection | |
| 194 | -available after this timeout, the driver will try the next host. | |
| 191 | +The size of that queue is controlled by [PoolingOptions.setMaxQueueSize][smqs]. | |
| 192 | +If the queue has already reached its limit, further attempts to acquire a | |
| 193 | +connection will be rejected immediately: the driver will move on and try to | |
| 194 | +acquire a connection from the next host's pool. The limit can be set to 0 to | |
| 195 | +disable queueing entirely. | |
| 195 | 196 | |
| 196 | -For some applications, blocking is not acceptable, and it is preferable | |
| 197 | -to fail fast if the request cannot be fulfilled. If that's your case, | |
| 198 | -set the pool timeout to 0. If all hosts are busy, you will get a | |
| 199 | -[NoHostAvailableException][nhae] (if you look at the exception's details, you | |
| 200 | -will see a `java.util.concurrent.TimeoutException` for each host). | |
| 197 | +If all hosts are busy with a full queue, the request will fail with a | |
| 198 | +[NoHostAvailableException][nhae]. If you inspect the map returns by this | |
| 199 | +exception's [getErrors] method, you will see a [BusyPoolException] for | |
| 200 | +each host. | |
| 201 | 201 | |
| 202 | 202 | |
| 203 | 203 | ### Monitoring and tuning the pool |
| @@ -278,14 +278,15 @@ | ||
| 278 | 278 | [newConnectionThreshold][nct] so that enough connections are added by |
| 279 | 279 | the time you reach the bottleneck. |
| 280 | 280 | |
| 281 | -[result_set_future]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ResultSetFuture.html | |
| 282 | -[pooling_options]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html | |
| 283 | -[lbp]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/LoadBalancingPolicy.html | |
| 284 | -[nct]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setNewConnectionThreshold-com.datastax.driver.core.HostDistance-int- | |
| 285 | -[mrpc]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setMaxRequestsPerConnection-com.datastax.driver.core.HostDistance-int- | |
| 286 | -[sits]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setIdleTimeoutSeconds-int- | |
| 287 | -[rtm]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#getReadTimeoutMillis-- | |
| 288 | -[exec_async]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Session.html#executeAsync-com.datastax.driver.core.Statement- | |
| 289 | -[ptm]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setPoolTimeoutMillis-int- | |
| 290 | -[nhae]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/exceptions/NoHostAvailableException.html | |
| 291 | -[get_state]:http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Session.html#getState-- | |
| 281 | +[result_set_future]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/ResultSetFuture.html | |
| 282 | +[pooling_options]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html | |
| 283 | +[lbp]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/policies/LoadBalancingPolicy.html | |
| 284 | +[nct]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setNewConnectionThreshold-com.datastax.driver.core.HostDistance-int- | |
| 285 | +[mrpc]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setMaxRequestsPerConnection-com.datastax.driver.core.HostDistance-int- | |
| 286 | +[sits]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setIdleTimeoutSeconds-int- | |
| 287 | +[rtm]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/SocketOptions.html#getReadTimeoutMillis-- | |
| 288 | +[smqs]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/PoolingOptions.html#setMaxQueueSize-int- | |
| 289 | +[nhae]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/exceptions/NoHostAvailableException.html | |
| 290 | +[getErrors]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/exceptions/NoHostAvailableException.html#getErrors-- | |
| 291 | +[get_state]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/Session.html#getState-- | |
| 292 | +[BusyPoolException]: http://docs.datastax.com/en/drivers/java/3.0/com/datastax/driver/core/exceptions/BusyPoolException.html |
modified upgrade_guide/README.md +16 −0
| @@ -3,6 +3,22 @@ | ||
| 3 | 3 | The purpose of this guide is to detail changes made by successive |
| 4 | 4 | versions of the Java driver. |
| 5 | 5 | |
| 6 | +### 3.0.4 | |
| 7 | + | |
| 8 | +The connection pool is now fully non-blocking ([JAVA-893](https://datastax-oss.atlassian.net/browse/JAVA-893)), | |
| 9 | +which greatly improves asynchronous programming: | |
| 10 | + | |
| 11 | +* `Session.executeAsync` won't ever block the user thread anymore; | |
| 12 | +* you can now safely run async queries on a session connected to a keyspace. | |
| 13 | + | |
| 14 | +This new implementation brings a couple of changes: | |
| 15 | + | |
| 16 | +* pool saturation is no longer handled by a timeout, but instead by a bounded queue. As a consequence, | |
| 17 | + `PoolingOptions.setPoolTimeoutMillis` has been deprecated, and replaced by `setMaxQueueSize`; | |
| 18 | +* a saturated pool will now throw `BusyPoolException` instead of `TimeoutException` (note that this exception is not | |
| 19 | + rethrown directly to the client, but wrapped in `NoHostAvailableException.getErrors()`). | |
| 20 | + | |
| 21 | + | |
| 6 | 22 | ### 3.0.0 |
| 7 | 23 | |
| 8 | 24 | This version brings parity with Cassandra 2.2 and 3.0. |