You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2022/01/25 00:04:13 UTC

[GitHub] [cassandra] ekaterinadimitrova2 commented on a change in pull request #1421: CASSANDRA-17224: Virtual table for exposing CQL metrics

ekaterinadimitrova2 commented on a change in pull request #1421:
URL: https://github.com/apache/cassandra/pull/1421#discussion_r791214141



##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics

Review comment:
       I think we can skip this comment as the package and the class are already self-explanatory

##########
File path: test/unit/org/apache/cassandra/db/virtual/CQLMetricsTableTest.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.metrics.CQLMetrics;
+
+public class CQLMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    private void queryAndValidateMetrics(CQLMetrics expectedMetrics) throws Throwable
+    {
+        String getMetricsQuery = "SELECT * FROM " + KS_NAME + "." + CQLMetricsTable.TABLE_NAME;
+        ResultSet vtsRows = executeNet(getMetricsQuery);
+
+        // validate - number of columns
+        assertEquals(6, vtsRows.getColumnDefinitions().size());
+
+        AtomicInteger rowCount = new AtomicInteger(0);
+        vtsRows.forEach(r -> {
+            assertEquals(expectedMetrics.preparedStatementsCount.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_COUNT_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsEvicted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EVICTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EXECUTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsRatio.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_RATIO_COL), 0.01);
+            assertEquals(expectedMetrics.regularStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.REGULAR_STATEMENTS_EXECUTED_COL), 0);
+            rowCount.getAndIncrement();
+        });
+
+        // validate - number of rows
+        assertEquals(1, rowCount.get());
+    }
+
+    @Test
+    public void testUsingPrepareStmts() throws Throwable
+    {
+        CQLMetricsTable table = new CQLMetricsTable(KS_NAME);
+        VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+
+        String ks = createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
+        String tbl = createTable(ks, "CREATE TABLE %s (id int PRIMARY KEY, cid int, val text)");
+        Session session = sessionNet();
+
+        // prepare statements
+        String insertCQL = "INSERT INTO " + ks + "." + tbl + " (id, cid, val) VALUES (?, ?, ?)";
+        PreparedStatement preparedInsert = session.prepare(insertCQL);
+
+        String selectCQL = "Select * from " + ks + "." + tbl + " where id = ?";
+        PreparedStatement preparedSelect = session.prepare(selectCQL);
+
+        for (int i = 0; i < 10; i++)
+        {
+            // execute prepared statements

Review comment:
       I think we can skip this comment

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";
+
+    private final CQLMetrics cqlMetrics;
+
+    // Default constructor references query processor metrics
+    CQLMetricsTable(String keyspace)
+    {
+        this(keyspace, QueryProcessor.metrics);
+    }
+
+    // For dependency injection
+    @VisibleForTesting
+    CQLMetricsTable(String keyspace, CQLMetrics cqlMetrics)
+    {
+        // create virtual table with this name
+        super(TableMetadata.builder(keyspace, TABLE_NAME)
+                           .kind(TableMetadata.Kind.VIRTUAL)
+                           .partitioner(new LocalPartitioner(UTF8Type.instance))
+                           .addPartitionKeyColumn(NAME_COL, UTF8Type.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_COUNT_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_EVICTED_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_EXECUTED_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_RATIO_COL, DoubleType.instance)
+                           .addRegularColumn(REGULAR_STATEMENTS_EXECUTED_COL, DoubleType.instance)
+                           .build());
+        this.cqlMetrics = cqlMetrics;
+    }
+
+    @Override
+    public DataSet data()
+    {
+        // populate metrics in the virtual table

Review comment:
       I think we can skip this comment

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";
+
+    private final CQLMetrics cqlMetrics;
+
+    // Default constructor references query processor metrics

Review comment:
       I think we can skip this comment 

##########
File path: test/unit/org/apache/cassandra/db/virtual/CQLMetricsTableTest.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.metrics.CQLMetrics;
+
+public class CQLMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    private void queryAndValidateMetrics(CQLMetrics expectedMetrics) throws Throwable
+    {
+        String getMetricsQuery = "SELECT * FROM " + KS_NAME + "." + CQLMetricsTable.TABLE_NAME;
+        ResultSet vtsRows = executeNet(getMetricsQuery);
+
+        // validate - number of columns
+        assertEquals(6, vtsRows.getColumnDefinitions().size());
+
+        AtomicInteger rowCount = new AtomicInteger(0);
+        vtsRows.forEach(r -> {
+            assertEquals(expectedMetrics.preparedStatementsCount.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_COUNT_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsEvicted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EVICTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EXECUTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsRatio.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_RATIO_COL), 0.01);
+            assertEquals(expectedMetrics.regularStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.REGULAR_STATEMENTS_EXECUTED_COL), 0);
+            rowCount.getAndIncrement();
+        });
+
+        // validate - number of rows
+        assertEquals(1, rowCount.get());
+    }
+
+    @Test
+    public void testUsingPrepareStmts() throws Throwable
+    {
+        CQLMetricsTable table = new CQLMetricsTable(KS_NAME);
+        VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));

Review comment:
       I think we can add something like that for clarity and to reduce the repetition of code below, WDYT?
   ```
       @Before
       public void config()
       {
           CQLMetricsTable table = new CQLMetricsTable(KS_NAME);
           VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
       }
   ```

##########
File path: test/unit/org/apache/cassandra/db/virtual/CQLMetricsTableTest.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.metrics.CQLMetrics;
+
+public class CQLMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    private void queryAndValidateMetrics(CQLMetrics expectedMetrics) throws Throwable
+    {
+        String getMetricsQuery = "SELECT * FROM " + KS_NAME + "." + CQLMetricsTable.TABLE_NAME;
+        ResultSet vtsRows = executeNet(getMetricsQuery);
+
+        // validate - number of columns
+        assertEquals(6, vtsRows.getColumnDefinitions().size());
+
+        AtomicInteger rowCount = new AtomicInteger(0);
+        vtsRows.forEach(r -> {
+            assertEquals(expectedMetrics.preparedStatementsCount.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_COUNT_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsEvicted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EVICTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EXECUTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsRatio.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_RATIO_COL), 0.01);
+            assertEquals(expectedMetrics.regularStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.REGULAR_STATEMENTS_EXECUTED_COL), 0);
+            rowCount.getAndIncrement();
+        });
+
+        // validate - number of rows
+        assertEquals(1, rowCount.get());
+    }
+
+    @Test
+    public void testUsingPrepareStmts() throws Throwable
+    {
+        CQLMetricsTable table = new CQLMetricsTable(KS_NAME);
+        VirtualKeyspaceRegistry.instance.register(new VirtualKeyspace(KS_NAME, ImmutableList.of(table)));
+
+        String ks = createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
+        String tbl = createTable(ks, "CREATE TABLE %s (id int PRIMARY KEY, cid int, val text)");
+        Session session = sessionNet();
+
+        // prepare statements

Review comment:
       I think we can skip this comment

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";
+
+    private final CQLMetrics cqlMetrics;

Review comment:
       I don't think we really need `private final CQLMetrics cqlMetrics;`
   I would suggest 
   ```
       @Override
       public DataSet data()
       {
           SimpleDataSet result = new SimpleDataSet(metadata());
           CQLMetrics cqlMetrics = QueryProcessor.metrics;
           addRow(result, cqlMetrics);
   
           return result;
       }
   ```
   
   Then I will make another suggestion in the tests, please follow there :-) 

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";
+
+    private final CQLMetrics cqlMetrics;
+
+    // Default constructor references query processor metrics
+    CQLMetricsTable(String keyspace)
+    {
+        this(keyspace, QueryProcessor.metrics);
+    }
+
+    // For dependency injection
+    @VisibleForTesting
+    CQLMetricsTable(String keyspace, CQLMetrics cqlMetrics)
+    {
+        // create virtual table with this name

Review comment:
       I think we can skip this comment

##########
File path: test/unit/org/apache/cassandra/db/virtual/CQLMetricsTableTest.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.metrics.CQLMetrics;
+
+public class CQLMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    private void queryAndValidateMetrics(CQLMetrics expectedMetrics) throws Throwable
+    {
+        String getMetricsQuery = "SELECT * FROM " + KS_NAME + "." + CQLMetricsTable.TABLE_NAME;
+        ResultSet vtsRows = executeNet(getMetricsQuery);
+
+        // validate - number of columns
+        assertEquals(6, vtsRows.getColumnDefinitions().size());
+
+        AtomicInteger rowCount = new AtomicInteger(0);
+        vtsRows.forEach(r -> {
+            assertEquals(expectedMetrics.preparedStatementsCount.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_COUNT_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsEvicted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EVICTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_EXECUTED_COL), 0);
+            assertEquals(expectedMetrics.preparedStatementsRatio.getValue(), r.getDouble(CQLMetricsTable.PREPARED_STATEMENTS_RATIO_COL), 0.01);
+            assertEquals(expectedMetrics.regularStatementsExecuted.getCount(), r.getDouble(CQLMetricsTable.REGULAR_STATEMENTS_EXECUTED_COL), 0);
+            rowCount.getAndIncrement();
+        });
+
+        // validate - number of rows

Review comment:
       I think we can skip this comment

##########
File path: test/unit/org/apache/cassandra/db/virtual/CQLMetricsTableTest.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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 com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Session;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+import org.junit.BeforeClass;
+
+import org.apache.cassandra.cql3.CQLTester;
+import org.apache.cassandra.cql3.QueryProcessor;
+import org.apache.cassandra.metrics.CQLMetrics;
+
+public class CQLMetricsTableTest extends CQLTester
+{
+    private static final String KS_NAME = "vts";
+
+    @BeforeClass
+    public static void setUpClass()
+    {
+        CQLTester.setUpClass();
+    }
+
+    private void queryAndValidateMetrics(CQLMetrics expectedMetrics) throws Throwable
+    {
+        String getMetricsQuery = "SELECT * FROM " + KS_NAME + "." + CQLMetricsTable.TABLE_NAME;
+        ResultSet vtsRows = executeNet(getMetricsQuery);
+
+        // validate - number of columns

Review comment:
       I think we can skip this comment

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";

Review comment:
       I am thinking maybe of `Metric`? (I have to warn you I am bad with names :-) )

##########
File path: src/java/org/apache/cassandra/db/virtual/CQLMetricsTable.java
##########
@@ -0,0 +1,87 @@
+/*
+ * 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.google.common.annotations.VisibleForTesting;
+
+import org.apache.cassandra.db.marshal.DoubleType;
+import org.apache.cassandra.db.marshal.UTF8Type;
+import org.apache.cassandra.dht.LocalPartitioner;
+import org.apache.cassandra.metrics.CQLMetrics;
+import org.apache.cassandra.schema.TableMetadata;
+import org.apache.cassandra.cql3.QueryProcessor;
+
+// Virtual table for CQL metrics
+public class CQLMetricsTable extends AbstractVirtualTable
+{
+    public static final String TABLE_NAME = "cql_metrics";
+    public static final String PREPARED_STATEMENTS_COUNT_COL = "prepared_statements_count";
+    public static final String PREPARED_STATEMENTS_EVICTED_COL = "prepared_statements_evicted";
+    public static final String PREPARED_STATEMENTS_EXECUTED_COL = "prepared_statements_executed";
+    public static final String PREPARED_STATEMENTS_RATIO_COL = "prepared_statements_ratio";
+    public static final String REGULAR_STATEMENTS_EXECUTED_COL = "regular_statements_executed";
+    public static final String NAME_COL = "name";
+    public static final String VALUE_ROW = "value";
+
+    private final CQLMetrics cqlMetrics;
+
+    // Default constructor references query processor metrics
+    CQLMetricsTable(String keyspace)
+    {
+        this(keyspace, QueryProcessor.metrics);
+    }
+
+    // For dependency injection
+    @VisibleForTesting
+    CQLMetricsTable(String keyspace, CQLMetrics cqlMetrics)
+    {
+        // create virtual table with this name
+        super(TableMetadata.builder(keyspace, TABLE_NAME)
+                           .kind(TableMetadata.Kind.VIRTUAL)
+                           .partitioner(new LocalPartitioner(UTF8Type.instance))
+                           .addPartitionKeyColumn(NAME_COL, UTF8Type.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_COUNT_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_EVICTED_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_EXECUTED_COL, DoubleType.instance)
+                           .addRegularColumn(PREPARED_STATEMENTS_RATIO_COL, DoubleType.instance)
+                           .addRegularColumn(REGULAR_STATEMENTS_EXECUTED_COL, DoubleType.instance)
+                           .build());
+        this.cqlMetrics = cqlMetrics;
+    }
+
+    @Override
+    public DataSet data()
+    {
+        // populate metrics in the virtual table
+        SimpleDataSet result = new SimpleDataSet(metadata());
+        addRow(result, VALUE_ROW, cqlMetrics);
+
+        return result;
+    }
+
+    private void addRow(SimpleDataSet dataSet, String name, CQLMetrics cqlMetrics)
+    {
+        dataSet.row(name)
+               .column(PREPARED_STATEMENTS_COUNT_COL, Double.valueOf(cqlMetrics.preparedStatementsCount.getValue()))

Review comment:
       We can also invert to table with two columns and 5 rows. I think this would be the format most tables follow actually




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



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