kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 2964aed846e2c984b9331e0b93e15e333e94895223ee71070caa683586278f0a |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-03-27 15:08 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-03-27 19:11 +01:00 |
| Parents | 64a1208dbf6b |
4 files changed
modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +5 −1
| @@ -56,7 +56,7 @@ | ||
| 56 | 56 | final List<PooledConnection> connections; |
| 57 | 57 | private final AtomicInteger open; |
| 58 | 58 | /** The total number of in-flight requests on all connections of this pool. */ |
| 59 | - private final AtomicInteger totalInFlight = new AtomicInteger(); | |
| 59 | + final AtomicInteger totalInFlight = new AtomicInteger(); | |
| 60 | 60 | /** The maximum value of {@link #totalInFlight} since the last call to {@link #cleanupIdleConnections(long)}*/ |
| 61 | 61 | private final AtomicInteger maxTotalInFlight = new AtomicInteger(); |
| 62 | 62 | final Set<PooledConnection> trash = new CopyOnWriteArraySet<PooledConnection>(); |
| @@ -533,6 +533,10 @@ | ||
| 533 | 533 | return open.get(); |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | + public int trashed() { | |
| 537 | + return trash.size(); | |
| 538 | + } | |
| 539 | + | |
| 536 | 540 | private List<CloseFuture> discardAvailableConnections() { |
| 537 | 541 | // This can happen if creating the connections in the constructor fails |
| 538 | 542 | if (connections == null) |
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +24 −0
| @@ -69,6 +69,16 @@ | ||
| 69 | 69 | return value; |
| 70 | 70 | } |
| 71 | 71 | }); |
| 72 | + private final Gauge<Integer> trashedConnections = registry.register("trashed-connections", new Gauge<Integer>() { | |
| 73 | + @Override | |
| 74 | + public Integer getValue() { | |
| 75 | + int value = 0; | |
| 76 | + for (SessionManager session : manager.sessions) | |
| 77 | + for (HostConnectionPool pool : session.pools.values()) | |
| 78 | + value += pool.trashed(); | |
| 79 | + return value; | |
| 80 | + } | |
| 81 | + }); | |
| 72 | 82 | |
| 73 | 83 | private final Gauge<Integer> executorQueueDepth = registry.register("executor-queue-depth", new Gauge<Integer>() { |
| 74 | 84 | @Override |
| @@ -205,6 +215,20 @@ | ||
| 205 | 215 | return openConnections; |
| 206 | 216 | } |
| 207 | 217 | |
| 218 | + /** | |
| 219 | + * Returns the total number of currently "trashed" connections to Cassandra hosts. | |
| 220 | + * <p> | |
| 221 | + * When the load to a host decreases, the driver will reclaim some connections in order to save | |
| 222 | + * resources. No requests are sent to these connections anymore, but they are kept open for an | |
| 223 | + * additional amount of time ({@link PoolingOptions#getIdleTimeoutSeconds()}), in case the load | |
| 224 | + * goes up again. This metric counts connections in that state. | |
| 225 | + * | |
| 226 | + * @return The total number of currently trashed connections to Cassandra hosts. | |
| 227 | + */ | |
| 228 | + public Gauge<Integer> getTrashedConnections() { | |
| 229 | + return trashedConnections; | |
| 230 | + } | |
| 231 | + | |
| 208 | 232 | /** |
| 209 | 233 | * @return The number of queued up tasks in the non-blocking executor (Cassandra Java Driver workers). |
| 210 | 234 | */ |
modified driver-core/src/main/java/com/datastax/driver/core/Session.java +17 −1
| @@ -17,7 +17,6 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.io.Closeable; |
| 19 | 19 | import java.util.Collection; |
| 20 | -import java.util.Map; | |
| 21 | 20 | |
| 22 | 21 | import com.google.common.util.concurrent.ListenableFuture; |
| 23 | 22 | |
| @@ -376,6 +375,9 @@ | ||
| 376 | 375 | |
| 377 | 376 | /** |
| 378 | 377 | * The number of open connections to a given host. |
| 378 | + * <p> | |
| 379 | + * Note that this refers to <em>active</em> connections. The actual number of connections also | |
| 380 | + * includes {@link #getTrashedConnections(Host)}. | |
| 379 | 381 | * |
| 380 | 382 | * @param host the host to get open connections for. |
| 381 | 383 | * @return The number of open connections to {@code host}. If the session |
| @@ -383,6 +385,20 @@ | ||
| 383 | 385 | */ |
| 384 | 386 | public int getOpenConnections(Host host); |
| 385 | 387 | |
| 388 | + /** | |
| 389 | + * The number of "trashed" connections to a given host. | |
| 390 | + * <p> | |
| 391 | + * When the load to a host decreases, the driver will reclaim some connections in order to save | |
| 392 | + * resources. No requests are sent to these connections anymore, but they are kept open for an | |
| 393 | + * additional amount of time ({@link PoolingOptions#getIdleTimeoutSeconds()}), in case the load | |
| 394 | + * goes up again. This method counts connections in that state. | |
| 395 | + * | |
| 396 | + * @param host the host to get trashed connections for. | |
| 397 | + * @return The number of trashed connections to {@code host}. If the session | |
| 398 | + * is not connected to that host, 0 is returned. | |
| 399 | + */ | |
| 400 | + public int getTrashedConnections(Host host); | |
| 401 | + | |
| 386 | 402 | /** |
| 387 | 403 | * The number of queries that are currently being executed though a given host. |
| 388 | 404 | * <p> |
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +10 −3
| @@ -567,6 +567,7 @@ | ||
| 567 | 567 | private final SessionManager session; |
| 568 | 568 | private final List<Host> connectedHosts; |
| 569 | 569 | private final int[] openConnections; |
| 570 | + private final int[] trashedConnections; | |
| 570 | 571 | private final int[] inFlightQueries; |
| 571 | 572 | |
| 572 | 573 | private State(SessionManager session) { |
| @@ -574,6 +575,7 @@ | ||
| 574 | 575 | this.connectedHosts = ImmutableList.copyOf(session.pools.keySet()); |
| 575 | 576 | |
| 576 | 577 | this.openConnections = new int[connectedHosts.size()]; |
| 578 | + this.trashedConnections = new int[connectedHosts.size()]; | |
| 577 | 579 | this.inFlightQueries = new int[connectedHosts.size()]; |
| 578 | 580 | |
| 579 | 581 | int i = 0; |
| @@ -584,14 +586,14 @@ | ||
| 584 | 586 | // connections will be slightly weird, but it's unlikely enough that we don't bother avoiding. |
| 585 | 587 | if (p == null) { |
| 586 | 588 | openConnections[i] = 0; |
| 589 | + trashedConnections[i] = 0; | |
| 587 | 590 | inFlightQueries[i] = 0; |
| 588 | 591 | continue; |
| 589 | 592 | } |
| 590 | 593 | |
| 591 | 594 | openConnections[i] = p.connections.size(); |
| 592 | - for (Connection c : p.connections) { | |
| 593 | - inFlightQueries[i] += c.inFlight.get(); | |
| 594 | - } | |
| 595 | + trashedConnections[i] = p.trash.size(); | |
| 596 | + inFlightQueries[i] = p.totalInFlight.get(); | |
| 595 | 597 | i++; |
| 596 | 598 | } |
| 597 | 599 | } |
| @@ -622,6 +624,11 @@ | ||
| 622 | 624 | return i < 0 ? 0 : openConnections[i]; |
| 623 | 625 | } |
| 624 | 626 | |
| 627 | + public int getTrashedConnections(Host host) { | |
| 628 | + int i = getIdx(host); | |
| 629 | + return i < 0 ? 0 : trashedConnections[i]; | |
| 630 | + } | |
| 631 | + | |
| 625 | 632 | public int getInFlightQueries(Host host) { |
| 626 | 633 | int i = getIdx(host); |
| 627 | 634 | return i < 0 ? 0 : inFlightQueries[i]; |