Repository: cassandra Updated Branches: refs/heads/trunk d080a7372 -> 9a47974d6
Expose recent histograms in JmxHistograms Closes #126 Patch by CHris Lohfink; Reviewed by Jeremiah Jordan for CASSANDRA-13642 Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/9a47974d Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/9a47974d Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/9a47974d Branch: refs/heads/trunk Commit: 9a47974d6378b62dd2cdb2d3e509374b002caa2c Parents: d080a73 Author: Chris Lohfink <[email protected]> Authored: Tue Jun 27 14:10:50 2017 -0500 Committer: Jeff Jirsa <[email protected]> Committed: Wed Oct 4 10:28:39 2017 -0700 ---------------------------------------------------------------------- CHANGES.txt | 1 + .../metrics/CassandraMetricsRegistry.java | 48 ++++++++++++++++++++ .../metrics/CassandraMetricsRegistryTest.java | 29 ++++++++++-- 3 files changed, 74 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/9a47974d/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 635c2f4..e006db3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 4.0 + * Expose recent histograms in JmxHistograms (CASSANDRA-13642) * Fix buffer length comparison when decompressing in netty-based streaming (CASSANDRA-13899) * Properly close StreamCompressionInputStream to release any ByteBuf (CASSANDRA-13906) * Add SERIAL and LOCAL_SERIAL support for cassandra-stress (CASSANDRA-13925) http://git-wip-us.apache.org/repos/asf/cassandra/blob/9a47974d/src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java b/src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java index e1a5703..97a932b 100644 --- a/src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java +++ b/src/java/org/apache/cassandra/metrics/CassandraMetricsRegistry.java @@ -23,6 +23,8 @@ import java.util.Locale; import java.util.concurrent.TimeUnit; import com.codahale.metrics.*; +import com.google.common.annotations.VisibleForTesting; + import javax.management.*; /** @@ -273,11 +275,14 @@ public class CassandraMetricsRegistry extends MetricRegistry double get999thPercentile(); long[] values(); + + long[] getRecentValues(); } private static class JmxHistogram extends AbstractBean implements JmxHistogramMBean { private final Histogram metric; + private long[] last = null; private JmxHistogram(Histogram metric, ObjectName objectName) { @@ -356,6 +361,15 @@ public class CassandraMetricsRegistry extends MetricRegistry { return metric.getSnapshot().getValues(); } + + @Override + public long[] getRecentValues() + { + long[] now = metric.getSnapshot().getValues(); + long[] delta = delta(now, last); + last = now; + return delta; + } } public interface JmxCounterMBean extends MetricMBean @@ -476,6 +490,8 @@ public class CassandraMetricsRegistry extends MetricRegistry long[] values(); + long[] getRecentValues(); + String getDurationUnit(); } @@ -484,6 +500,7 @@ public class CassandraMetricsRegistry extends MetricRegistry private final Timer metric; private final double durationFactor; private final String durationUnit; + private long[] last = null; private JmxTimer(Timer metric, ObjectName objectName, @@ -563,6 +580,15 @@ public class CassandraMetricsRegistry extends MetricRegistry } @Override + public long[] getRecentValues() + { + long[] now = metric.getSnapshot().getValues(); + long[] delta = delta(now, last); + last = now; + return delta; + } + + @Override public String getDurationUnit() { return durationUnit; @@ -570,6 +596,28 @@ public class CassandraMetricsRegistry extends MetricRegistry } /** + * Used to determine the changes in a histogram since the last time checked. + * + * @param now The current histogram + * @param last The previous value of the histogram + * @return the difference between <i>now</> and <i>last</i> + */ + @VisibleForTesting + static long[] delta(long[] now, long[] last) + { + long[] delta = new long[now.length]; + if (last == null) + { + last = new long[now.length]; + } + for(int i = 0; i< now.length; i++) + { + delta[i] = now[i] - (i < last.length? last[i] : 0); + } + return delta; + } + + /** * A value class encapsulating a metric's owning class and name. */ public static class MetricName implements Comparable<MetricName> http://git-wip-us.apache.org/repos/asf/cassandra/blob/9a47974d/test/unit/org/apache/cassandra/metrics/CassandraMetricsRegistryTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/metrics/CassandraMetricsRegistryTest.java b/test/unit/org/apache/cassandra/metrics/CassandraMetricsRegistryTest.java index 4a9b874..cd9866c 100644 --- a/test/unit/org/apache/cassandra/metrics/CassandraMetricsRegistryTest.java +++ b/test/unit/org/apache/cassandra/metrics/CassandraMetricsRegistryTest.java @@ -20,17 +20,17 @@ */ package org.apache.cassandra.metrics; +import static org.junit.Assert.*; + import java.lang.management.ManagementFactory; import java.util.Collection; +import org.apache.cassandra.metrics.CassandraMetricsRegistry.MetricName; import org.junit.Test; import com.codahale.metrics.jvm.BufferPoolMetricSet; import com.codahale.metrics.jvm.GarbageCollectorMetricSet; import com.codahale.metrics.jvm.MemoryUsageGaugeSet; -import org.apache.cassandra.metrics.CassandraMetricsRegistry.MetricName; - -import static org.junit.Assert.*; public class CassandraMetricsRegistryTest @@ -86,4 +86,25 @@ public class CassandraMetricsRegistryTest } } -} \ No newline at end of file + @Test + public void testDeltaBaseCase() + { + long[] last = new long[10]; + long[] now = new long[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + // difference between all zeros and a value should be the value + assertArrayEquals(now, CassandraMetricsRegistry.delta(now, last)); + // the difference between itself should be all 0s + assertArrayEquals(last, CassandraMetricsRegistry.delta(now, now)); + // verifying each value is calculated + assertArrayEquals(new long[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + CassandraMetricsRegistry.delta(new long[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, now)); + } + + @Test + public void testDeltaHistogramSizeChange() + { + long[] count = new long[]{0, 1, 2, 3, 4, 5}; + assertArrayEquals(count, CassandraMetricsRegistry.delta(count, new long[3])); + assertArrayEquals(new long[6], CassandraMetricsRegistry.delta(count, new long[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
