You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by bl...@apache.org on 2020/10/20 08:30:35 UTC

[cassandra] branch trunk updated: Improve ClientRequestSizeMetrics test

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

blerer pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/cassandra.git


The following commit(s) were added to refs/heads/trunk by this push:
     new c87bc23  Improve ClientRequestSizeMetrics test
c87bc23 is described below

commit c87bc233c901d3f81649a0172cbc6b078b287ad0
Author: Benjamin Lerer <b....@gmail.com>
AuthorDate: Wed Oct 7 17:52:37 2020 +0200

    Improve ClientRequestSizeMetrics test
    
    patch by Benjamin Lerer; reviewed by Berenguer Blasi for CASSANDRA-16184
    
    The test was only checking that the metrics where updated but was not
    testing if the updates were correct.
    
    This patch also fix the ClearableHistogram.clear method (through an ugly
    hack) that was not clearing the histogram count.
---
 .../cassandra/metrics/ClearableHistogram.java      |  34 ++++++
 .../metrics/ClientRequestSizeMetrics.java          |   2 +-
 src/java/org/apache/cassandra/transport/Frame.java |   2 +-
 .../metrics/ClientRequestSizeMetricsTest.java      | 124 +++++++++++++++++++++
 .../cassandra/transport/ServerMetricsTest.java     |  65 -----------
 5 files changed, 160 insertions(+), 67 deletions(-)

diff --git a/src/java/org/apache/cassandra/metrics/ClearableHistogram.java b/src/java/org/apache/cassandra/metrics/ClearableHistogram.java
index 4a081d8..f83ef82 100644
--- a/src/java/org/apache/cassandra/metrics/ClearableHistogram.java
+++ b/src/java/org/apache/cassandra/metrics/ClearableHistogram.java
@@ -17,6 +17,10 @@
  */
 package org.apache.cassandra.metrics;
 
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.LongAdder;
+
 import com.google.common.annotations.VisibleForTesting;
 
 import com.codahale.metrics.Histogram;
@@ -43,6 +47,36 @@ public class ClearableHistogram extends Histogram
     @VisibleForTesting
     public void clear()
     {
+        clearCount();
         reservoirRef.clear();
     }
+
+    private void clearCount()
+    {
+        // We have unfortunately no access to the count field so we need to use reflection to ensure that it is cleared.
+        // I hate that as it is fragile and pretty ugly but we only use that method for tests and it should fail pretty
+        // clearly if we start using an incompatible version of the metrics.
+        try
+        {
+            Field countField = Histogram.class.getDeclaredField("count");
+            countField.setAccessible(true);
+            // in 3.1 the counter object is a LongAdderAdapter which is a package private interface
+            // from com.codahale.metrics. In 4.0, it is a java LongAdder so the code will be simpler.
+            Object counter = countField.get(this);
+            if (counter instanceof LongAdder) // For com.codahale.metrics version >= 4.0
+            {
+                ((LongAdder) counter).reset();
+            }
+            else // 3.1 and 3.2
+            {
+                Method sumThenReset = counter.getClass().getDeclaredMethod("sumThenReset");
+                sumThenReset.setAccessible(true);
+                sumThenReset.invoke(counter);
+            }
+        }
+        catch (Exception e)
+        {
+            throw new IllegalStateException("Cannot reset the com.codahale.metrics.Histogram count. This might be due to a change of version of the metric library", e);
+        }
+    }
 }
diff --git a/src/java/org/apache/cassandra/metrics/ClientRequestSizeMetrics.java b/src/java/org/apache/cassandra/metrics/ClientRequestSizeMetrics.java
index 41fb162..d86515e 100644
--- a/src/java/org/apache/cassandra/metrics/ClientRequestSizeMetrics.java
+++ b/src/java/org/apache/cassandra/metrics/ClientRequestSizeMetrics.java
@@ -31,6 +31,6 @@ public class ClientRequestSizeMetrics
     private static final String TYPE = "ClientRequestSize";
     public static final Counter totalBytesRead = Metrics.counter(DefaultNameFactory.createMetricName(TYPE, "IncomingBytes", null));
     public static final Counter totalBytesWritten = Metrics.counter(DefaultNameFactory.createMetricName(TYPE, "OutgoingBytes", null));
-    public static final Histogram bytesRecievedPerFrame = Metrics.histogram(DefaultNameFactory.createMetricName(TYPE, "BytesRecievedPerFrame", null), true);
+    public static final Histogram bytesReceivedPerFrame = Metrics.histogram(DefaultNameFactory.createMetricName(TYPE, "BytesRecievedPerFrame", null), true);
     public static final Histogram bytesTransmittedPerFrame = Metrics.histogram(DefaultNameFactory.createMetricName(TYPE, "BytesTransmittedPerFrame", null), true);
 }
diff --git a/src/java/org/apache/cassandra/transport/Frame.java b/src/java/org/apache/cassandra/transport/Frame.java
index b597cc2..ec4317a 100644
--- a/src/java/org/apache/cassandra/transport/Frame.java
+++ b/src/java/org/apache/cassandra/transport/Frame.java
@@ -224,7 +224,7 @@ public class Frame
                 return null;
 
             ClientRequestSizeMetrics.totalBytesRead.inc(frameLength);
-            ClientRequestSizeMetrics.bytesRecievedPerFrame.update(frameLength);
+            ClientRequestSizeMetrics.bytesReceivedPerFrame.update(frameLength);
 
             // extract body
             ByteBuf body = buffer.slice(idx, (int) bodyLength);
diff --git a/test/unit/org/apache/cassandra/metrics/ClientRequestSizeMetricsTest.java b/test/unit/org/apache/cassandra/metrics/ClientRequestSizeMetricsTest.java
new file mode 100644
index 0000000..f081743
--- /dev/null
+++ b/test/unit/org/apache/cassandra/metrics/ClientRequestSizeMetricsTest.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.metrics;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Snapshot;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.metrics.ClearableHistogram;
+import org.apache.cassandra.metrics.ClientRequestSizeMetrics;
+import org.apache.cassandra.metrics.DecayingEstimatedHistogramReservoir.EstimatedHistogramReservoirSnapshot;
+import org.apache.cassandra.metrics.DecayingEstimatedHistogramReservoir.Range;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Ensures we properly account for metrics tracked in the native protocol
+ */
+public class ClientRequestSizeMetricsTest extends CQLTester
+{
+    @BeforeClass
+    public static void setUp()
+    {
+        requireNetwork();
+    }
+
+    @Test
+    public void testReadAndWriteMetricsAreRecordedDuringNativeRequests() throws Throwable
+    {
+        // We want to ignore all the messages sent by the driver upon connection as well as
+        // the event sent upon schema updates
+        clearMetrics();
+
+        executeNet("SELECT * from system.peers");
+
+        long requestLength = ClientRequestSizeMetrics.totalBytesRead.getCount();
+        long responseLength = ClientRequestSizeMetrics.totalBytesWritten.getCount();
+
+        assertThat(requestLength).isGreaterThan(0);
+        assertThat(responseLength).isGreaterThan(0);
+
+        checkMetrics(1, requestLength, responseLength);
+
+        // Let's fire the same request again and test that the changes are the same that previously
+        executeNet("SELECT * from system.peers");
+
+        checkMetrics(2, requestLength, responseLength);
+    }
+
+    private void checkMetrics(int numberOfRequests, long requestLength, long responseLength)
+    {
+        Snapshot snapshot;
+        long expectedTotalBytesRead = numberOfRequests * requestLength;
+        assertEquals(expectedTotalBytesRead, ClientRequestSizeMetrics.totalBytesRead.getCount());
+
+        long expectedTotalBytesWritten = numberOfRequests * responseLength;
+        assertEquals(expectedTotalBytesWritten, ClientRequestSizeMetrics.totalBytesWritten.getCount());
+
+        // The request fit in one single frame so we know the new number of received frames
+        assertEquals(numberOfRequests, ClientRequestSizeMetrics.bytesReceivedPerFrame.getCount());
+
+        snapshot = ClientRequestSizeMetrics.bytesReceivedPerFrame.getSnapshot();
+        assertMin(snapshot, requestLength);
+        assertMax(snapshot, requestLength);
+
+        // The response fit in one single frame so we know the new number of received frames
+        assertEquals(numberOfRequests, ClientRequestSizeMetrics.bytesTransmittedPerFrame.getCount());
+
+        snapshot = ClientRequestSizeMetrics.bytesTransmittedPerFrame.getSnapshot();
+        assertMin(snapshot, responseLength);
+        assertMax(snapshot, responseLength);
+    }
+
+    private void assertMin(Snapshot snapshot, long value)
+    {
+        Range range = ((EstimatedHistogramReservoirSnapshot) snapshot).getBucketingRangeForValue(value);
+        assertEquals(snapshot.getMin(), range.min);
+    }
+
+    private void assertMax(Snapshot snapshot, long value)
+    {
+        Range range = ((EstimatedHistogramReservoirSnapshot) snapshot).getBucketingRangeForValue(value);
+        assertEquals(snapshot.getMax(), range.max);
+    }
+
+    private void clearMetrics()
+    {
+        clearCounter(ClientRequestSizeMetrics.totalBytesRead);
+        clearCounter(ClientRequestSizeMetrics.totalBytesWritten);
+        clearHistogram(ClientRequestSizeMetrics.bytesReceivedPerFrame);
+        clearHistogram(ClientRequestSizeMetrics.bytesTransmittedPerFrame);
+    }
+
+    private void clearCounter(Counter counter)
+    {
+        counter.dec(counter.getCount());
+    }
+
+    private void clearHistogram(Histogram histogram)
+    {
+        ((ClearableHistogram) histogram).clear();
+    }
+}
diff --git a/test/unit/org/apache/cassandra/transport/ServerMetricsTest.java b/test/unit/org/apache/cassandra/transport/ServerMetricsTest.java
deleted file mode 100644
index 081da00..0000000
--- a/test/unit/org/apache/cassandra/transport/ServerMetricsTest.java
+++ /dev/null
@@ -1,65 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.cassandra.transport;
-
-import java.util.List;
-
-import org.junit.Before;
-import org.junit.Test;
-
-import io.netty.buffer.ByteBuf;
-import io.netty.buffer.ByteBufAllocator;
-import org.apache.cassandra.cql3.CQLTester;
-import org.apache.cassandra.metrics.ClientRequestSizeMetrics;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-/**
- * Ensures we properly account for metrics tracked in the native protocol
- */
-public class ServerMetricsTest extends CQLTester
-{
-    private long totalBytesReadStart;
-    private long totalBytesWrittenStart;
-
-    private long totalBytesReadHistoCount;
-    private long totalBytesWrittenHistoCount;
-
-    @Before
-    public void setUp()
-    {
-        totalBytesReadStart = ClientRequestSizeMetrics.totalBytesRead.getCount();
-        totalBytesWrittenStart = ClientRequestSizeMetrics.totalBytesWritten.getCount();
-
-        totalBytesReadHistoCount = ClientRequestSizeMetrics.bytesRecievedPerFrame.getCount();
-        totalBytesWrittenHistoCount = ClientRequestSizeMetrics.bytesTransmittedPerFrame.getCount();
-    }
-
-    @Test
-    public void testReadAndWriteMetricsAreRecordedDuringNativeRequests() throws Throwable
-    {
-        executeNet("SELECT * from system.peers");
-
-        assertThat(ClientRequestSizeMetrics.totalBytesRead.getCount()).isGreaterThan(totalBytesReadStart);
-        assertThat(ClientRequestSizeMetrics.totalBytesWritten.getCount()).isGreaterThan(totalBytesWrittenStart);
-        assertThat(ClientRequestSizeMetrics.bytesRecievedPerFrame.getCount()).isGreaterThan(totalBytesReadStart);
-        assertThat(ClientRequestSizeMetrics.bytesTransmittedPerFrame.getCount()).isGreaterThan(totalBytesWrittenStart);
-    }
-
-}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cassandra.apache.org
For additional commands, e-mail: commits-help@cassandra.apache.org