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 2022/02/10 08:49:54 UTC

[cassandra] branch trunk updated: Add a virtual table for exposing batch metrics

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 ce7502a  Add a virtual table for exposing batch metrics
ce7502a is described below

commit ce7502a11067ef903cba24ad588cf0a5b9da9257
Author: Michael Burman <ya...@iki.fi>
AuthorDate: Thu Dec 23 21:01:09 2021 +0200

    Add a virtual table for exposing batch metrics
    
    Patch by Michael Burman; review by Aleksei Zotov, Benjamin Lerer and Ekaterina Dimitrova for CASSANDRA-17225
---
 CHANGES.txt                                        |  1 +
 .../cassandra/db/virtual/BatchMetricsTable.java    | 75 ++++++++++++++++++
 .../cassandra/db/virtual/SystemViewsKeyspace.java  |  1 +
 .../db/virtual/BatchMetricsTableTest.java          | 90 ++++++++++++++++++++++
 4 files changed, 167 insertions(+)

diff --git a/CHANGES.txt b/CHANGES.txt
index d3808db..74524cf 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 4.1
+ * Add a virtual table for exposing batch metrics (CASSANDRA-17225)
  * Flatten guardrails config (CASSANDRA-17353)
  * Instance failed to start up due to NPE in StartupClusterConnectivityChecker (CASSANDRA-17347)
  * add the shorter version of version flag (-v) in cqlsh (CASSANDRA-17236)
diff --git a/src/java/org/apache/cassandra/db/virtual/BatchMetricsTable.java b/src/java/org/apache/cassandra/db/virtual/BatchMetricsTable.java
new file mode 100644
index 0000000..948f2a1
--- /dev/null
+++ b/src/java/org/apache/cassandra/db/virtual/BatchMetricsTable.java
@@ -0,0 +1,75 @@
+/*
+ * 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.db.virtual;
+
+import com.codahale.metrics.Snapshot;
+import org.apache.cassandra.cql3.statements.BatchStatement;
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.LongType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.BatchMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+
+public class BatchMetricsTable extends AbstractVirtualTable
+{
+
+    private static final String PARTITIONS_PER_LOGGED_BATCH = "partitions_per_logged_batch";
+    private static final String PARTITIONS_PER_UNLOGGED_BATCH = "partitions_per_unlogged_batch";
+    private static final String PARTITIONS_PER_COUNTER_BATCH = "partitions_per_counter_batch";
+    private final static String P50 = "p50th";
+    private final static String P99 = "p99th";
+    private final static String P999 = "p999th";
+    private final static String MAX = "max";
+
+    BatchMetricsTable(String keyspace)
+    {
+        super(TableMetadata.builder(keyspace, "batch_metrics")
+                           .comment("Metrics specific to batch statements")
+                           .kind(TableMetadata.Kind.VIRTUAL)
+                           .partitioner(new LocalPartitioner(UTF8Type.instance))
+                           .addPartitionKeyColumn("name", UTF8Type.instance)
+                           .addRegularColumn(P50, DoubleType.instance)
+                           .addRegularColumn(P99, DoubleType.instance)
+                           .addRegularColumn(P999, DoubleType.instance)
+                           .addRegularColumn(MAX, LongType.instance)
+                           .build());
+    }
+
+    @Override
+    public DataSet data()
+    {
+        SimpleDataSet result = new SimpleDataSet(metadata());
+        BatchMetrics metrics = BatchStatement.metrics;
+        addRow(result, PARTITIONS_PER_LOGGED_BATCH, metrics.partitionsPerLoggedBatch.getSnapshot());
+        addRow(result, PARTITIONS_PER_UNLOGGED_BATCH, metrics.partitionsPerUnloggedBatch.getSnapshot());
+        addRow(result, PARTITIONS_PER_COUNTER_BATCH, metrics.partitionsPerCounterBatch.getSnapshot());
+
+        return result;
+    }
+
+    private void addRow(SimpleDataSet dataSet, String name, Snapshot snapshot)
+    {
+        dataSet.row(name)
+               .column(P50, snapshot.getMedian())
+               .column(P99, snapshot.get99thPercentile())
+               .column(P999, snapshot.get999thPercentile())
+               .column(MAX, snapshot.getMax());
+    }
+}
diff --git a/src/java/org/apache/cassandra/db/virtual/SystemViewsKeyspace.java b/src/java/org/apache/cassandra/db/virtual/SystemViewsKeyspace.java
index 6d5582e..6fe189e 100644
--- a/src/java/org/apache/cassandra/db/virtual/SystemViewsKeyspace.java
+++ b/src/java/org/apache/cassandra/db/virtual/SystemViewsKeyspace.java
@@ -44,6 +44,7 @@ public final class SystemViewsKeyspace extends VirtualKeyspace
                     .add(new PermissionsCacheKeysTable(VIRTUAL_VIEWS))
                     .add(new RolesCacheKeysTable(VIRTUAL_VIEWS))
                     .add(new CQLMetricsTable(VIRTUAL_VIEWS))
+                    .add(new BatchMetricsTable(VIRTUAL_VIEWS))
                     .build());
     }
 }
diff --git a/test/unit/org/apache/cassandra/db/virtual/BatchMetricsTableTest.java b/test/unit/org/apache/cassandra/db/virtual/BatchMetricsTableTest.java
new file mode 100644
index 0000000..8c34759
--- /dev/null
+++ b/test/unit/org/apache/cassandra/db/virtual/BatchMetricsTableTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.db.virtual;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import com.google.common.collect.ImmutableList;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Snapshot;
+import com.datastax.driver.core.ResultSet;
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.statements.BatchStatement;
+import org.apache.cassandra.metrics.BatchMetrics;
+
+import static java.lang.String.format;
+import static org.junit.Assert.assertEquals;
+
+public class BatchMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    @Before
+    public void config()
+    {
+        BatchMetricsTable table = new BatchMetricsTable(KS_NAME);
+        VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+    }
+
+    @Test
+    public void testSelectAll() throws Throwable
+    {
+        BatchMetrics metrics = BatchStatement.metrics;
+
+        for (int i = 0; i < 10; i++)
+        {
+            metrics.partitionsPerLoggedBatch.update(i);
+            metrics.partitionsPerUnloggedBatch.update(i + 10);
+            metrics.partitionsPerCounterBatch.update(i * 10);
+        }
+
+        ResultSet result = executeNet(format("SELECT * FROM %s.batch_metrics", KS_NAME));
+        assertEquals(5, result.getColumnDefinitions().size());
+        AtomicInteger rowCount = new AtomicInteger(0);
+        result.forEach(r -> {
+            Snapshot snapshot = getExpectedHistogram(metrics, r.getString("name")).getSnapshot();
+            assertEquals(snapshot.getMedian(), r.getDouble("p50th"), 0.0);
+            assertEquals(snapshot.get99thPercentile(), r.getDouble("p99th"), 0.0);
+            rowCount.addAndGet(1);
+        });
+
+        assertEquals(3, rowCount.get());
+    }
+
+    private Histogram getExpectedHistogram(BatchMetrics metrics, String name)
+    {
+        if ("partitions_per_logged_batch".equals(name))
+            return metrics.partitionsPerLoggedBatch;
+
+        if ("partitions_per_unlogged_batch".equals(name))
+            return metrics.partitionsPerUnloggedBatch;
+
+        return metrics.partitionsPerCounterBatch;
+    }
+}

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