PHOENIX-4853 Add sql statement to PhoenixMetricsLog interface for query level metrics logging
Project: http://git-wip-us.apache.org/repos/asf/phoenix/repo Commit: http://git-wip-us.apache.org/repos/asf/phoenix/commit/e979241a Tree: http://git-wip-us.apache.org/repos/asf/phoenix/tree/e979241a Diff: http://git-wip-us.apache.org/repos/asf/phoenix/diff/e979241a Branch: refs/heads/omid2 Commit: e979241ac9e7fb938c56a9e41b38f7d5aa3a56cd Parents: eb79c5b Author: Karan Mehta <karanmeht...@gmail.com> Authored: Fri Aug 17 13:02:08 2018 -0700 Committer: Karan Mehta <karanmeht...@gmail.com> Committed: Fri Aug 17 17:36:28 2018 -0700 ---------------------------------------------------------------------- .../monitoring/PhoenixLoggingMetricsIT.java | 15 +++++++++--- .../phoenix/jdbc/LoggingPhoenixConnection.java | 16 ++++++------- .../jdbc/LoggingPhoenixPreparedStatement.java | 13 +++++++---- .../phoenix/jdbc/LoggingPhoenixResultSet.java | 10 ++++---- .../phoenix/jdbc/LoggingPhoenixStatement.java | 24 ++++++++++++++++---- .../apache/phoenix/jdbc/PhoenixMetricsLog.java | 6 ++--- 6 files changed, 56 insertions(+), 28 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixLoggingMetricsIT.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixLoggingMetricsIT.java b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixLoggingMetricsIT.java index 02640e7..97b2c5d 100644 --- a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixLoggingMetricsIT.java +++ b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/PhoenixLoggingMetricsIT.java @@ -43,6 +43,7 @@ public class PhoenixLoggingMetricsIT extends BasePhoenixMetricsIT { private String tableName1; private String tableName2; private LoggingPhoenixConnection loggedConn; + private String loggedSql; @Before public void beforeTest() throws Exception { @@ -75,7 +76,10 @@ public class PhoenixLoggingMetricsIT extends BasePhoenixMetricsIT { rs.close(); assertTrue("Read metrics for not found for " + tableName1, requestReadMetricsMap.get(tableName1).size() > 0); + assertTrue("Logged query doesn't match actual query", loggedSql.equals(query)); + assertTrue("Overall read metrics for not found ", overAllQueryMetricsMap.size() > 0); + assertTrue("Logged query doesn't match actual query", loggedSql.equals(query)); // run UPSERT SELECT to verify mutation metrics are logged String upsertSelect = "UPSERT INTO " + tableName2 + " SELECT * FROM " + tableName1; @@ -120,7 +124,10 @@ public class PhoenixLoggingMetricsIT extends BasePhoenixMetricsIT { rs.close(); assertTrue("Read metrics for not found for " + tableName1, requestReadMetricsMap.get(tableName1).size() > 0); + assertTrue("Logged query doesn't match actual query", loggedSql.equals(query)); + assertTrue("Overall read metrics for not found ", overAllQueryMetricsMap.size() > 0); + assertTrue("Logged query doesn't match actual query", loggedSql.equals(query)); // run UPSERT SELECT to verify mutation metrics are logged String upsertSelect = "UPSERT INTO " + tableName2 + " SELECT * FROM " + tableName1; @@ -155,18 +162,20 @@ public class PhoenixLoggingMetricsIT extends BasePhoenixMetricsIT { return new LoggingPhoenixConnection(conn, new PhoenixMetricsLog() { @Override public void logOverAllReadRequestMetrics( - Map<MetricType, Long> overAllQueryMetrics) { + Map<MetricType, Long> overAllQueryMetrics, String sql) { overAllQueryMetricsMap.putAll(overAllQueryMetrics); + loggedSql = sql; } @Override public void logRequestReadMetrics( - Map<String, Map<MetricType, Long>> requestReadMetrics) { + Map<String, Map<MetricType, Long>> requestReadMetrics, String sql) { requestReadMetricsMap.putAll(requestReadMetrics); + loggedSql = sql; } @Override - public void logWriteMetricsfoForMutations( + public void logWriteMetricsfoForMutationsSinceLastReset( Map<String, Map<MetricType, Long>> mutationWriteMetrics) { mutationWriteMetricsMap.putAll(mutationWriteMetrics); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixConnection.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixConnection.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixConnection.java index 9a2e00f..37917e2 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixConnection.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixConnection.java @@ -61,7 +61,7 @@ public class LoggingPhoenixConnection extends DelegateConnection { @Override public PreparedStatement prepareStatement(String sql) throws SQLException { return new LoggingPhoenixPreparedStatement(super.prepareStatement(sql), - phoenixMetricsLog); + phoenixMetricsLog, sql); } @Override @@ -69,40 +69,40 @@ public class LoggingPhoenixConnection extends DelegateConnection { int resultSetConcurrency) throws SQLException { return new LoggingPhoenixPreparedStatement( super.prepareStatement(sql, resultSetType, resultSetConcurrency), - phoenixMetricsLog); + phoenixMetricsLog, sql); } @Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { return new LoggingPhoenixPreparedStatement(super.prepareStatement(sql, resultSetType, - resultSetConcurrency, resultSetHoldability), phoenixMetricsLog); + resultSetConcurrency, resultSetHoldability), phoenixMetricsLog, sql); } @Override public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { return new LoggingPhoenixPreparedStatement(super.prepareStatement(sql, autoGeneratedKeys), - phoenixMetricsLog); + phoenixMetricsLog, sql); } @Override public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { return new LoggingPhoenixPreparedStatement(super.prepareStatement(sql, columnIndexes), - phoenixMetricsLog); + phoenixMetricsLog, sql); } @Override public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return new LoggingPhoenixPreparedStatement(super.prepareStatement(sql, columnNames), - phoenixMetricsLog); + phoenixMetricsLog, sql); } @Override public void commit() throws SQLException { super.commit(); - phoenixMetricsLog.logWriteMetricsfoForMutations(PhoenixRuntime.getWriteMetricInfoForMutationsSinceLastReset(conn)); + phoenixMetricsLog.logWriteMetricsfoForMutationsSinceLastReset(PhoenixRuntime.getWriteMetricInfoForMutationsSinceLastReset(conn)); phoenixMetricsLog.logReadMetricInfoForMutationsSinceLastReset(PhoenixRuntime.getReadMetricInfoForMutationsSinceLastReset(conn)); PhoenixRuntime.resetMetrics(conn); } @@ -110,7 +110,7 @@ public class LoggingPhoenixConnection extends DelegateConnection { @Override public void close() throws SQLException { try { - phoenixMetricsLog.logWriteMetricsfoForMutations(PhoenixRuntime.getWriteMetricInfoForMutationsSinceLastReset(conn)); + phoenixMetricsLog.logWriteMetricsfoForMutationsSinceLastReset(PhoenixRuntime.getWriteMetricInfoForMutationsSinceLastReset(conn)); phoenixMetricsLog.logReadMetricInfoForMutationsSinceLastReset(PhoenixRuntime.getReadMetricInfoForMutationsSinceLastReset(conn)); PhoenixRuntime.resetMetrics(conn); } finally { http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixPreparedStatement.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixPreparedStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixPreparedStatement.java index cc8ec1f..952e3fd 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixPreparedStatement.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixPreparedStatement.java @@ -20,34 +20,37 @@ package org.apache.phoenix.jdbc; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.SQLFeatureNotSupportedException; public class LoggingPhoenixPreparedStatement extends DelegatePreparedStatement { private PhoenixMetricsLog phoenixMetricsLog; + private String sql; - public LoggingPhoenixPreparedStatement(PreparedStatement stmt, PhoenixMetricsLog phoenixMetricsLog) { + public LoggingPhoenixPreparedStatement(PreparedStatement stmt, PhoenixMetricsLog phoenixMetricsLog, String sql) { super(stmt); this.phoenixMetricsLog = phoenixMetricsLog; + this.sql = sql; } @Override public ResultSet executeQuery(String sql) throws SQLException { - return new LoggingPhoenixResultSet(super.executeQuery(sql), phoenixMetricsLog); + throw new SQLFeatureNotSupportedException(); } @Override public ResultSet executeQuery() throws SQLException { - return new LoggingPhoenixResultSet(super.executeQuery(), phoenixMetricsLog); + return new LoggingPhoenixResultSet(super.executeQuery(), phoenixMetricsLog, sql); } @Override public ResultSet getResultSet() throws SQLException { - return new LoggingPhoenixResultSet(super.getResultSet(), phoenixMetricsLog); + return new LoggingPhoenixResultSet(super.getResultSet(), phoenixMetricsLog, sql); } @Override public ResultSet getGeneratedKeys() throws SQLException { - return new LoggingPhoenixResultSet(super.getGeneratedKeys(), phoenixMetricsLog); + return new LoggingPhoenixResultSet(super.getGeneratedKeys(), phoenixMetricsLog, sql); } } http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixResultSet.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixResultSet.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixResultSet.java index fbde499..53f5cb4 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixResultSet.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixResultSet.java @@ -25,16 +25,18 @@ import org.apache.phoenix.util.PhoenixRuntime; public class LoggingPhoenixResultSet extends DelegateResultSet { private PhoenixMetricsLog phoenixMetricsLog; - - public LoggingPhoenixResultSet(ResultSet rs, PhoenixMetricsLog phoenixMetricsLog) { + private String sql; + + public LoggingPhoenixResultSet(ResultSet rs, PhoenixMetricsLog phoenixMetricsLog, String sql) { super(rs); this.phoenixMetricsLog = phoenixMetricsLog; + this.sql = sql; } @Override public void close() throws SQLException { - phoenixMetricsLog.logOverAllReadRequestMetrics(PhoenixRuntime.getOverAllReadRequestMetricInfo(rs)); - phoenixMetricsLog.logRequestReadMetrics(PhoenixRuntime.getRequestReadMetricInfo(rs)); + phoenixMetricsLog.logOverAllReadRequestMetrics(PhoenixRuntime.getOverAllReadRequestMetricInfo(rs), sql); + phoenixMetricsLog.logRequestReadMetrics(PhoenixRuntime.getRequestReadMetricInfo(rs), sql); PhoenixRuntime.resetMetrics(rs); super.close(); } http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixStatement.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixStatement.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixStatement.java index ea919ed..f9abe6a 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixStatement.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/LoggingPhoenixStatement.java @@ -24,6 +24,7 @@ import java.sql.Statement; public class LoggingPhoenixStatement extends DelegateStatement { private PhoenixMetricsLog phoenixMetricsLog; + private String sql; public LoggingPhoenixStatement(Statement stmt, PhoenixMetricsLog phoenixMetricsLog) { super(stmt); @@ -31,18 +32,31 @@ public class LoggingPhoenixStatement extends DelegateStatement { } @Override + public boolean execute(String sql) throws SQLException { + this.sql = sql; + return super.execute(sql); + } + + @Override public ResultSet executeQuery(String sql) throws SQLException { - return new LoggingPhoenixResultSet(super.executeQuery(sql), phoenixMetricsLog); + this.sql = sql; + return new LoggingPhoenixResultSet(super.executeQuery(sql), phoenixMetricsLog, this.sql); } - + + @Override + public int executeUpdate(String sql) throws SQLException { + this.sql = sql; + return super.executeUpdate(sql); + } + @Override public ResultSet getResultSet() throws SQLException { - return new LoggingPhoenixResultSet(super.getResultSet(), phoenixMetricsLog); + return new LoggingPhoenixResultSet(super.getResultSet(), phoenixMetricsLog, this.sql); } @Override public ResultSet getGeneratedKeys() throws SQLException { - return new LoggingPhoenixResultSet(super.getGeneratedKeys(), phoenixMetricsLog); + return new LoggingPhoenixResultSet(super.getGeneratedKeys(), phoenixMetricsLog, this.sql); } - + } http://git-wip-us.apache.org/repos/asf/phoenix/blob/e979241a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixMetricsLog.java ---------------------------------------------------------------------- diff --git a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixMetricsLog.java b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixMetricsLog.java index be08e52..96556ad 100644 --- a/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixMetricsLog.java +++ b/phoenix-core/src/main/java/org/apache/phoenix/jdbc/PhoenixMetricsLog.java @@ -23,11 +23,11 @@ import org.apache.phoenix.monitoring.MetricType; public interface PhoenixMetricsLog { - void logOverAllReadRequestMetrics(Map<MetricType, Long> overAllQueryMetrics); + void logOverAllReadRequestMetrics(Map<MetricType, Long> overAllQueryMetrics, String sql); - void logRequestReadMetrics(Map<String, Map<MetricType, Long>> requestReadMetrics); + void logRequestReadMetrics(Map<String, Map<MetricType, Long>> requestReadMetrics, String sql); - void logWriteMetricsfoForMutations(Map<String, Map<MetricType, Long>> mutationWriteMetrics); + void logWriteMetricsfoForMutationsSinceLastReset(Map<String, Map<MetricType, Long>> mutationWriteMetrics); void logReadMetricInfoForMutationsSinceLastReset(Map<String, Map<MetricType, Long>> mutationReadMetrics);