You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@phoenix.apache.org by tk...@apache.org on 2023/12/13 00:12:48 UTC

(phoenix) branch master updated: PHOENIX-7144 Fixing tableNames for Index Tables for phoenix client Metrics (#1755)

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

tkhurana pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/master by this push:
     new 087a88ffc7 PHOENIX-7144 Fixing tableNames for Index Tables for phoenix client Metrics (#1755)
087a88ffc7 is described below

commit 087a88ffc7c79ffc726567a512326f246bd0026e
Author: vikas meka <vm...@salesforce.com>
AuthorDate: Tue Dec 12 16:12:43 2023 -0800

    PHOENIX-7144 Fixing tableNames for Index Tables for phoenix client Metrics (#1755)
    
    Fixing tableNames for Index Tables for phoenix client Metrics
---
 .../org/apache/phoenix/jdbc/PhoenixStatement.java  | 17 +++---
 .../monitoring/PhoenixTableLevelMetricsIT.java     | 71 +++++++++++++++++++++-
 2 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
index 4cc5abdb39..2b47e7d50b 100644
--- a/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
+++ b/phoenix-core-client/src/main/java/org/apache/phoenix/jdbc/PhoenixStatement.java
@@ -363,14 +363,6 @@ public class PhoenixStatement implements PhoenixMonitoredStatement, SQLCloseable
                                 // Use original plan for data table so that data and immutable indexes will be sent
                                 // TODO: for joins, we need to iterate through all tables, but we need the original table,
                                 // not the projected table, so plan.getContext().getResolver().getTables() won't work.
-                                if (plan.getTableRef() != null
-                                        && plan.getTableRef().getTable() != null && !Strings
-                                        .isNullOrEmpty(
-                                                plan.getTableRef().getTable().getPhysicalName()
-                                                        .toString())) {
-                                    tableName = plan.getTableRef().getTable().getPhysicalName()
-                                            .toString();
-                                }
                                 if (plan.getContext().getScanRanges().isPointLookup()) {
                                     pointLookup = true;
                                 }
@@ -379,6 +371,15 @@ public class PhoenixStatement implements PhoenixMonitoredStatement, SQLCloseable
                                 plan =
                                         connection.getQueryServices().getOptimizer()
                                                 .optimize(PhoenixStatement.this, plan);
+
+                                if (plan.getTableRef() != null
+                                        && plan.getTableRef().getTable() != null && !Strings
+                                        .isNullOrEmpty(
+                                                plan.getTableRef().getTable().getPhysicalName()
+                                                        .toString())) {
+                                    tableName = plan.getTableRef().getTable().getPhysicalName()
+                                            .toString();
+                                }
                                 // this will create its own trace internally, so we don't wrap this
                                 // whole thing in tracing
                                 ResultIterator resultIterator = plan.iterator();
diff --git a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixTableLevelMetricsIT.java b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixTableLevelMetricsIT.java
index 441edcee7d..e070075a46 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixTableLevelMetricsIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixTableLevelMetricsIT.java
@@ -486,8 +486,6 @@ public class PhoenixTableLevelMetricsIT extends BaseTest {
     private static void assertMetricValue(Metric m, MetricType checkType, long compareValue,
             CompareOp op) {
         if (m.getMetricType().equals(checkType)) {
-            System.out.println("THe Type:"+m.getMetricType().toString() + "expected value:"+m.getValue() +
-                    "\t compare value:"+compareValue);
             switch (op) {
             case EQ:
                 assertEquals(compareValue, m.getValue());
@@ -1218,6 +1216,75 @@ public class PhoenixTableLevelMetricsIT extends BaseTest {
         }
     }
 
+    @Test
+    public void testMetricsWithIndexUsage() throws Exception {
+        // Generate unique names for the table and index
+        String dataTable = generateUniqueName();
+        String indexName = generateUniqueName() + "_IDX";
+
+
+        try (Connection conn = getConnFromTestDriver()) {
+            // Create a mutable table with one key and one column
+            String tableDdl = "CREATE TABLE "
+                    + dataTable
+                    + " (K VARCHAR NOT NULL, V INTEGER, CONSTRAINT PK PRIMARY KEY(K))" + " IMMUTABLE_ROWS = true";
+            conn.createStatement().execute(tableDdl);
+
+            // Create an index for the column 'V'
+            String indexDdl = "CREATE INDEX " + indexName + " ON " + dataTable + " (V)";
+            conn.createStatement().execute(indexDdl);
+        }
+
+        // Insert data into the table
+        String insertData = "UPSERT INTO " + dataTable + " VALUES (?, ?)";
+        try (Connection conn = getConnFromTestDriver();
+             PreparedStatement stmt = conn.prepareStatement(insertData)) {
+            for (int i = 1; i <= 10; i++) {
+                stmt.setString(1, "key" + i);
+                stmt.setInt(2, i);
+                stmt.executeUpdate();
+            }
+            conn.commit();
+        }
+
+        // Check if the index is being used
+        try (Connection conn = getConnFromTestDriver()) {
+            String query = "SELECT * FROM " + dataTable + " WHERE V = ?";
+            try (PreparedStatement stmt = conn.prepareStatement(query)) {
+                // Set a parameter value that corresponds to the data inserted
+                stmt.setInt(1, 5);
+                clearTableLevelMetrics();
+                // Execute the query
+                try (ResultSet resultSet = stmt.executeQuery()) {
+                    while (resultSet.next()) {
+
+                    }
+                }
+                assertTrue(!getPhoenixTableClientMetrics().get(indexName).isEmpty());
+                // Assert that the index is used
+                boolean metricExists = false;
+                for (PhoenixTableMetric metric : getPhoenixTableClientMetrics().get(indexName)) {
+                    if (metric.getMetricType().equals(SELECT_SQL_COUNTER)) {
+                        metricExists = true;
+                        assertMetricValue(metric, SELECT_SQL_COUNTER, 1, CompareOp.EQ);
+                        break;
+                    }
+                }
+                assertTrue(metricExists);
+                metricExists = false;
+                //assert BaseTable is not being queried
+                for (PhoenixTableMetric metric : getPhoenixTableClientMetrics().get(dataTable)) {
+                    if (metric.getMetricType().equals(SELECT_SQL_COUNTER)) {
+                        metricExists = true;
+                        assertMetricValue(metric, SELECT_SQL_COUNTER, 0, CompareOp.EQ);
+                        break;
+                    }
+                }
+                assertTrue(metricExists);
+            }
+        }
+    }
+
     @Test public void testDeleteCommitTimeSlowRS() throws Throwable {
         String tableName = generateUniqueName();
         int numRows = 15;