This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d250408c483 Refactor SQLRouteCountAdvice and SQLParseCountAdvice
(#23636)
d250408c483 is described below
commit d250408c483fe90bd491fff8c011eb41d7a15b25
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Jan 18 19:27:27 2023 +0800
Refactor SQLRouteCountAdvice and SQLParseCountAdvice (#23636)
* Refactor SQLRouteCountAdvice and SQLParseCountAdvice
* Refactor MetricsCollectorRegistry
---
.../metrics/core/advice/SQLParseCountAdvice.java | 68 ++++++++++++----------
.../metrics/core/advice/SQLRouteCountAdvice.java | 28 ++++-----
.../core/collector/MetricsCollectorRegistry.java | 6 +-
3 files changed, 55 insertions(+), 47 deletions(-)
diff --git
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdvice.java
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdvice.java
index f3f1ce7228b..bba78ee98ac 100644
---
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdvice.java
+++
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLParseCountAdvice.java
@@ -36,6 +36,7 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateState
import
org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
import java.lang.reflect.Method;
+import java.util.Optional;
/**
* SQL parse count advice.
@@ -46,39 +47,46 @@ public final class SQLParseCountAdvice implements
InstanceMethodAdvice {
@Override
public void afterMethod(final TargetAdviceObject target, final Method
method, final Object[] args, final Object result, final String pluginType) {
- String sqlType = getSQLType((SQLStatement) result);
- if (null != sqlType) {
-
MetricsCollectorRegistry.<CounterMetricsCollector>get(PARSED_SQL_METRIC_KEY,
pluginType).inc(sqlType);
- }
+ getSQLType((SQLStatement) result).ifPresent(optional ->
MetricsCollectorRegistry.<CounterMetricsCollector>get(PARSED_SQL_METRIC_KEY,
pluginType).inc(optional));
}
- private String getSQLType(final SQLStatement sqlStatement) {
- String result = null;
+ private Optional<String> getSQLType(final SQLStatement sqlStatement) {
if (sqlStatement instanceof InsertStatement) {
- result = "INSERT";
- } else if (sqlStatement instanceof UpdateStatement) {
- result = "UPDATE";
- } else if (sqlStatement instanceof DeleteStatement) {
- result = "DELETE";
- } else if (sqlStatement instanceof SelectStatement) {
- result = "SELECT";
- } else if (sqlStatement instanceof DDLStatement) {
- result = "DDL";
- } else if (sqlStatement instanceof DCLStatement) {
- result = "DCL";
- } else if (sqlStatement instanceof DALStatement) {
- result = "DAL";
- } else if (sqlStatement instanceof TCLStatement) {
- result = "TCL";
- } else if (sqlStatement instanceof RQLStatement) {
- result = "RQL";
- } else if (sqlStatement instanceof RDLStatement) {
- result = "RDL";
- } else if (sqlStatement instanceof RALStatement) {
- result = "RAL";
- } else if (sqlStatement instanceof RULStatement) {
- result = "RUL";
+ return Optional.of("INSERT");
+ }
+ if (sqlStatement instanceof UpdateStatement) {
+ return Optional.of("UPDATE");
+ }
+ if (sqlStatement instanceof DeleteStatement) {
+ return Optional.of("DELETE");
+ }
+ if (sqlStatement instanceof SelectStatement) {
+ return Optional.of("SELECT");
+ }
+ if (sqlStatement instanceof DDLStatement) {
+ return Optional.of("DDL");
+ }
+ if (sqlStatement instanceof DCLStatement) {
+ return Optional.of("DCL");
+ }
+ if (sqlStatement instanceof DALStatement) {
+ return Optional.of("DAL");
+ }
+ if (sqlStatement instanceof TCLStatement) {
+ return Optional.of("TCL");
+ }
+ if (sqlStatement instanceof RQLStatement) {
+ return Optional.of("RQL");
+ }
+ if (sqlStatement instanceof RDLStatement) {
+ return Optional.of("RDL");
+ }
+ if (sqlStatement instanceof RALStatement) {
+ return Optional.of("RAL");
+ }
+ if (sqlStatement instanceof RULStatement) {
+ return Optional.of("RUL");
}
- return result;
+ return Optional.empty();
}
}
diff --git
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
index 9650e11e13d..f146628d6e2 100644
---
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
+++
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
@@ -29,6 +29,7 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectState
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
import java.lang.reflect.Method;
+import java.util.Optional;
/**
* SQL route count advice.
@@ -41,23 +42,22 @@ public final class SQLRouteCountAdvice implements
InstanceMethodAdvice {
public void beforeMethod(final TargetAdviceObject target, final Method
method, final Object[] args, final String pluginType) {
QueryContext queryContext = (QueryContext) args[1];
SQLStatement sqlStatement =
queryContext.getSqlStatementContext().getSqlStatement();
- String sqlType = getSQLType(sqlStatement);
- if (null != sqlType) {
-
MetricsCollectorRegistry.<CounterMetricsCollector>get(ROUTED_SQL_METRIC_KEY,
pluginType).inc(sqlType);
- }
+ getSQLType(sqlStatement).ifPresent(optional ->
MetricsCollectorRegistry.<CounterMetricsCollector>get(ROUTED_SQL_METRIC_KEY,
pluginType).inc(optional));
}
- private String getSQLType(final SQLStatement sqlStatement) {
- String result = null;
+ private Optional<String> getSQLType(final SQLStatement sqlStatement) {
if (sqlStatement instanceof InsertStatement) {
- result = "INSERT";
- } else if (sqlStatement instanceof UpdateStatement) {
- result = "UPDATE";
- } else if (sqlStatement instanceof DeleteStatement) {
- result = "DELETE";
- } else if (sqlStatement instanceof SelectStatement) {
- result = "SELECT";
+ return Optional.of("INSERT");
+ }
+ if (sqlStatement instanceof UpdateStatement) {
+ return Optional.of("UPDATE");
+ }
+ if (sqlStatement instanceof DeleteStatement) {
+ return Optional.of("DELETE");
+ }
+ if (sqlStatement instanceof SelectStatement) {
+ return Optional.of("SELECT");
}
- return result;
+ return Optional.empty();
}
}
diff --git
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistry.java
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistry.java
index 4ed495848d3..be81c69f3c4 100644
---
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistry.java
+++
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/collector/MetricsCollectorRegistry.java
@@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public final class MetricsCollectorRegistry {
- private static final Map<String, MetricsCollector> METRICS_WRAPPERS = new
ConcurrentHashMap<>();
+ private static final Map<String, MetricsCollector> METRICS_COLLECTORS =
new ConcurrentHashMap<>();
/**
* Get metrics collector.
@@ -40,7 +40,7 @@ public final class MetricsCollectorRegistry {
*/
@SuppressWarnings("unchecked")
public static <T extends MetricsCollector> T get(final String id, final
String pluginType) {
- T result = (T) METRICS_WRAPPERS.get(id);
- return (T) (null == result ? METRICS_WRAPPERS.computeIfAbsent(id,
PluginServiceLoader.getServiceLoader(MetricsCollectorFactory.class).getService(pluginType)::create)
: result);
+ T result = (T) METRICS_COLLECTORS.get(id);
+ return (T) (null == result ? METRICS_COLLECTORS.computeIfAbsent(id,
PluginServiceLoader.getServiceLoader(MetricsCollectorFactory.class).getService(pluginType)::create)
: result);
}
}