You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by rs...@apache.org on 2018/08/12 17:44:54 UTC

[kafka] branch 1.1 updated: KAFKA-7261: Record 1.0 for total metric when Count stat is used for rate (#5484)

This is an automated email from the ASF dual-hosted git repository.

rsivaram pushed a commit to branch 1.1
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/1.1 by this push:
     new 37e36aa  KAFKA-7261: Record 1.0 for total metric when Count stat is used for rate (#5484)
37e36aa is described below

commit 37e36aa28fa7886c26116e5ea4e77d85cf0061c4
Author: Rajini Sivaram <ra...@googlemail.com>
AuthorDate: Sun Aug 12 18:38:22 2018 +0100

    KAFKA-7261: Record 1.0 for total metric when Count stat is used for rate (#5484)
    
    Reviewers: Jun Rao <ju...@gmail.com>, John Roesler <jo...@confluent.io>, Ismael Juma <is...@juma.me.uk>
---
 .../apache/kafka/common/metrics/stats/Meter.java    |  7 ++++++-
 .../apache/kafka/common/network/NetworkReceive.java |  8 ++++++++
 .../org/apache/kafka/common/network/Selector.java   |  2 +-
 .../apache/kafka/common/metrics/MetricsTest.java    | 18 ++++++++++++++++--
 .../apache/kafka/common/network/NioEchoServer.java  |  2 +-
 .../kafka/common/network/SslTransportLayerTest.java | 21 +++++++++++++++++++++
 6 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java
index 09263ce..91d4461 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Meter.java
@@ -61,6 +61,9 @@ public class Meter implements CompoundStat {
      * Construct a Meter with provided time unit and provided {@link SampledStat} stats for Rate
      */
     public Meter(TimeUnit unit, SampledStat rateStat, MetricName rateMetricName, MetricName totalMetricName) {
+        if (!(rateStat instanceof SampledTotal) && !(rateStat instanceof Count)) {
+            throw new IllegalArgumentException("Meter is supported only for SampledTotal and Count");
+        }
         this.total = new Total();
         this.rate = new Rate(unit, rateStat);
         this.rateMetricName = rateMetricName;
@@ -77,6 +80,8 @@ public class Meter implements CompoundStat {
     @Override
     public void record(MetricConfig config, double value, long timeMs) {
         rate.record(config, value, timeMs);
-        total.record(config, value, timeMs);
+        // Total metrics with Count stat should record 1.0 (as recorded in the count)
+        double totalValue = (rate.stat instanceof Count) ? 1.0 : value;
+        total.record(config, totalValue, timeMs);
     }
 }
diff --git a/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java b/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java
index 564fbcd..c112fb6 100644
--- a/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java
+++ b/clients/src/main/java/org/apache/kafka/common/network/NetworkReceive.java
@@ -155,4 +155,12 @@ public class NetworkReceive implements Receive {
         return this.buffer;
     }
 
+    /**
+     * Returns the total size of the receive including payload and size buffer
+     * for use in metrics. This is consistent with {@link NetworkSend#size()}
+     */
+    public int size() {
+        return payload().limit() + size.limit();
+    }
+
 }
diff --git a/clients/src/main/java/org/apache/kafka/common/network/Selector.java b/clients/src/main/java/org/apache/kafka/common/network/Selector.java
index be02b5e..84b48f0 100644
--- a/clients/src/main/java/org/apache/kafka/common/network/Selector.java
+++ b/clients/src/main/java/org/apache/kafka/common/network/Selector.java
@@ -859,7 +859,7 @@ public class Selector implements Selectable, AutoCloseable {
     private void addToCompletedReceives(KafkaChannel channel, Deque<NetworkReceive> stagedDeque) {
         NetworkReceive networkReceive = stagedDeque.poll();
         this.completedReceives.add(networkReceive);
-        this.sensors.recordBytesReceived(channel.id(), networkReceive.payload().limit());
+        this.sensors.recordBytesReceived(channel.id(), networkReceive.size());
     }
 
     // only for testing
diff --git a/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java b/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
index 32669ff..7a973da 100644
--- a/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/metrics/MetricsTest.java
@@ -465,8 +465,12 @@ public class MetricsTest {
         Sensor s = metrics.sensor("test.sensor", cfg);
         MetricName rateMetricName = metrics.metricName("test.rate", "grp1");
         MetricName totalMetricName = metrics.metricName("test.total", "grp1");
+        MetricName countRateMetricName = metrics.metricName("test.count.rate", "grp1");
+        MetricName countTotalMetricName = metrics.metricName("test.count.total", "grp1");
         s.add(new Meter(TimeUnit.SECONDS, rateMetricName, totalMetricName));
-        KafkaMetric totalMetric = metrics.metrics().get(metrics.metricName("test.total", "grp1"));
+        s.add(new Meter(TimeUnit.SECONDS, new Count(), countRateMetricName, countTotalMetricName));
+        KafkaMetric totalMetric = metrics.metrics().get(totalMetricName);
+        KafkaMetric countTotalMetric = metrics.metrics().get(countTotalMetricName);
 
         int sum = 0;
         int count = cfg.samples() - 1;
@@ -484,11 +488,21 @@ public class MetricsTest {
         // prior to any time passing
         double elapsedSecs = (cfg.timeWindowMs() * (cfg.samples() - 1) + cfg.timeWindowMs() / 2) / 1000.0;
 
-        KafkaMetric rateMetric = metrics.metrics().get(metrics.metricName("test.rate", "grp1"));
+        KafkaMetric rateMetric = metrics.metrics().get(rateMetricName);
+        KafkaMetric countRateMetric = metrics.metrics().get(countRateMetricName);
         assertEquals("Rate(0...2) = 2.666", sum / elapsedSecs, rateMetric.value(), EPS);
+        assertEquals("Count rate(0...2) = 0.02666", count / elapsedSecs, countRateMetric.value(), EPS);
         assertEquals("Elapsed Time = 75 seconds", elapsedSecs,
                 ((Rate) rateMetric.measurable()).windowSize(cfg, time.milliseconds()) / 1000, EPS);
         assertEquals(sum, totalMetric.value(), EPS);
+        assertEquals(count, countTotalMetric.value(), EPS);
+
+        // Verify that rates are expired, but total is cumulative
+        time.sleep(cfg.timeWindowMs() * cfg.samples());
+        assertEquals(0, rateMetric.value(), EPS);
+        assertEquals(0, countRateMetric.value(), EPS);
+        assertEquals(sum, totalMetric.value(), EPS);
+        assertEquals(count, countTotalMetric.value(), EPS);
     }
 
     public static class ConstantMeasurable implements Measurable {
diff --git a/clients/src/test/java/org/apache/kafka/common/network/NioEchoServer.java b/clients/src/test/java/org/apache/kafka/common/network/NioEchoServer.java
index 0352ade..1c3bcf0 100644
--- a/clients/src/test/java/org/apache/kafka/common/network/NioEchoServer.java
+++ b/clients/src/test/java/org/apache/kafka/common/network/NioEchoServer.java
@@ -115,7 +115,7 @@ public class NioEchoServer extends Thread {
         waitForMetric("failed-authentication", failedAuthentications);
     }
 
-    private void waitForMetric(String name, final double expectedValue) throws InterruptedException {
+    public void waitForMetric(String name, final double expectedValue) throws InterruptedException {
         final String totalName = name + "-total";
         final String rateName = name + "-rate";
         if (expectedValue == 0.0) {
diff --git a/clients/src/test/java/org/apache/kafka/common/network/SslTransportLayerTest.java b/clients/src/test/java/org/apache/kafka/common/network/SslTransportLayerTest.java
index 5cc66e5..81fd1b7 100644
--- a/clients/src/test/java/org/apache/kafka/common/network/SslTransportLayerTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/network/SslTransportLayerTest.java
@@ -541,6 +541,27 @@ public class SslTransportLayerTest {
         server.verifyAuthenticationMetrics(0, 1);
     }
 
+    @Test
+    public void testServerRequestMetrics() throws Exception {
+        String node = "0";
+        server = createEchoServer(SecurityProtocol.SSL);
+        createSelector(sslClientConfigs, 16384, 16384, 16384);
+        InetSocketAddress addr = new InetSocketAddress("localhost", server.port());
+        selector.connect(node, addr, 102400, 102400);
+        NetworkTestUtils.waitForChannelReady(selector, node);
+        int messageSize = 1024 * 1024;
+        String message = TestUtils.randomString(messageSize);
+        selector.send(new NetworkSend(node, ByteBuffer.wrap(message.getBytes())));
+        while (selector.completedReceives().isEmpty()) {
+            selector.poll(100L);
+        }
+        int totalBytes = messageSize + 4; // including 4-byte size
+        server.waitForMetric("incoming-byte", totalBytes);
+        server.waitForMetric("outgoing-byte", totalBytes);
+        server.waitForMetric("request", 1);
+        server.waitForMetric("response", 1);
+    }
+
     /**
      * selector.poll() should be able to fetch more data than netReadBuffer from the socket.
      */