Elffuss BraidHub

kikocis/java-driver public

Add metrics for trashed connections.
Changeset2964aed846e2c984b9331e0b93e15e333e94895223ee71070caa683586278f0a
Authorolim7t <omichallat+github@gmail.com> · 2015-03-27 15:08 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2015-03-27 19:11 +01:00
Parents64a1208dbf6b

Browse the files at this changeset

4 files changed

modified driver-core/src/main/java/com/datastax/driver/core/HostConnectionPool.java +5 −1
@@ -56,7 +56,7 @@
5656 final List<PooledConnection> connections;
5757 private final AtomicInteger open;
5858 /** 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();
6060 /** The maximum value of {@link #totalInFlight} since the last call to {@link #cleanupIdleConnections(long)}*/
6161 private final AtomicInteger maxTotalInFlight = new AtomicInteger();
6262 final Set<PooledConnection> trash = new CopyOnWriteArraySet<PooledConnection>();
@@ -533,6 +533,10 @@
533533 return open.get();
534534 }
535535
536+ public int trashed() {
537+ return trash.size();
538+ }
539+
536540 private List<CloseFuture> discardAvailableConnections() {
537541 // This can happen if creating the connections in the constructor fails
538542 if (connections == null)
modified driver-core/src/main/java/com/datastax/driver/core/Metrics.java +24 −0
@@ -69,6 +69,16 @@
6969 return value;
7070 }
7171 });
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+ });
7282
7383 private final Gauge<Integer> executorQueueDepth = registry.register("executor-queue-depth", new Gauge<Integer>() {
7484 @Override
@@ -205,6 +215,20 @@
205215 return openConnections;
206216 }
207217
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+
208232 /**
209233 * @return The number of queued up tasks in the non-blocking executor (Cassandra Java Driver workers).
210234 */
modified driver-core/src/main/java/com/datastax/driver/core/Session.java +17 −1
@@ -17,7 +17,6 @@
1717
1818 import java.io.Closeable;
1919 import java.util.Collection;
20-import java.util.Map;
2120
2221 import com.google.common.util.concurrent.ListenableFuture;
2322
@@ -376,6 +375,9 @@
376375
377376 /**
378377 * 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)}.
379381 *
380382 * @param host the host to get open connections for.
381383 * @return The number of open connections to {@code host}. If the session
@@ -383,6 +385,20 @@
383385 */
384386 public int getOpenConnections(Host host);
385387
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+
386402 /**
387403 * The number of queries that are currently being executed though a given host.
388404 * <p>
modified driver-core/src/main/java/com/datastax/driver/core/SessionManager.java +10 −3
@@ -567,6 +567,7 @@
567567 private final SessionManager session;
568568 private final List<Host> connectedHosts;
569569 private final int[] openConnections;
570+ private final int[] trashedConnections;
570571 private final int[] inFlightQueries;
571572
572573 private State(SessionManager session) {
@@ -574,6 +575,7 @@
574575 this.connectedHosts = ImmutableList.copyOf(session.pools.keySet());
575576
576577 this.openConnections = new int[connectedHosts.size()];
578+ this.trashedConnections = new int[connectedHosts.size()];
577579 this.inFlightQueries = new int[connectedHosts.size()];
578580
579581 int i = 0;
@@ -584,14 +586,14 @@
584586 // connections will be slightly weird, but it's unlikely enough that we don't bother avoiding.
585587 if (p == null) {
586588 openConnections[i] = 0;
589+ trashedConnections[i] = 0;
587590 inFlightQueries[i] = 0;
588591 continue;
589592 }
590593
591594 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();
595597 i++;
596598 }
597599 }
@@ -622,6 +624,11 @@
622624 return i < 0 ? 0 : openConnections[i];
623625 }
624626
627+ public int getTrashedConnections(Host host) {
628+ int i = getIdx(host);
629+ return i < 0 ? 0 : trashedConnections[i];
630+ }
631+
625632 public int getInFlightQueries(Host host) {
626633 int i = getIdx(host);
627634 return i < 0 ? 0 : inFlightQueries[i];