This is an automated email from the ASF dual-hosted git repository.
zhonghongsheng 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 329e77e35db Rename RouteCountAdvice and SQLParserEngineAdvice (#23555)
329e77e35db is described below
commit 329e77e35dbd4e8605dea601d52d318c8a913096
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jan 14 21:35:30 2023 +0800
Rename RouteCountAdvice and SQLParserEngineAdvice (#23555)
---
...ountAdvice.java => RouteResultCountAdvice.java} | 30 +------------
...teCountAdvice.java => SQLRouteCountAdvice.java} | 21 +--------
.../metrics/core/advice/MetricsAdviceBaseTest.java | 17 +++++++
.../core/advice/RouteResultCountAdviceTest.java | 52 ++++++++++++++++++++++
...dviceTest.java => SQLRouteCountAdviceTest.java} | 25 ++---------
.../META-INF/conf/prometheus-proxy-advisors.yaml | 7 ++-
6 files changed, 82 insertions(+), 70 deletions(-)
diff --git
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdvice.java
similarity index 55%
copy from
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
copy to
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdvice.java
index 8ff98a91aac..91e9ec2d0bd 100644
---
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
+++
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdvice.java
@@ -20,49 +20,23 @@ package
org.apache.shardingsphere.agent.plugin.metrics.core.advice;
import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsPool;
-import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsWrapper;
import org.apache.shardingsphere.agent.plugin.metrics.core.constant.MetricIds;
-import org.apache.shardingsphere.infra.binder.QueryContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.SelectStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
import java.lang.reflect.Method;
/**
- * Route count advice.
+ * Route result count advice.
*/
-public final class RouteCountAdvice implements InstanceMethodAdvice {
+public final class RouteResultCountAdvice implements InstanceMethodAdvice {
static {
- MetricsPool.create(MetricIds.ROUTED_INSERT_SQL);
- MetricsPool.create(MetricIds.ROUTED_DELETE_SQL);
- MetricsPool.create(MetricIds.ROUTED_UPDATE_SQL);
- MetricsPool.create(MetricIds.ROUTED_SELECT_SQL);
MetricsPool.create(MetricIds.ROUTED_DATA_SOURCES);
MetricsPool.create(MetricIds.ROUTED_TABLES);
}
- @Override
- public void beforeMethod(final TargetAdviceObject target, final Method
method, final Object[] args) {
- QueryContext queryContext = (QueryContext) args[1];
- SQLStatement sqlStatement =
queryContext.getSqlStatementContext().getSqlStatement();
- if (sqlStatement instanceof InsertStatement) {
-
MetricsPool.get(MetricIds.ROUTED_INSERT_SQL).ifPresent(MetricsWrapper::inc);
- } else if (sqlStatement instanceof DeleteStatement) {
-
MetricsPool.get(MetricIds.ROUTED_DELETE_SQL).ifPresent(MetricsWrapper::inc);
- } else if (sqlStatement instanceof UpdateStatement) {
-
MetricsPool.get(MetricIds.ROUTED_UPDATE_SQL).ifPresent(MetricsWrapper::inc);
- } else if (sqlStatement instanceof SelectStatement) {
-
MetricsPool.get(MetricIds.ROUTED_SELECT_SQL).ifPresent(MetricsWrapper::inc);
- }
- }
-
@Override
public void afterMethod(final TargetAdviceObject target, final Method
method, final Object[] args, final Object result) {
if (null == result) {
diff --git
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
similarity index 73%
rename from
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
rename to
agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
index 8ff98a91aac..d3e04b26cfb 100644
---
a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdvice.java
+++
b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdvice.java
@@ -23,9 +23,6 @@ import
org.apache.shardingsphere.agent.plugin.metrics.core.MetricsPool;
import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsWrapper;
import org.apache.shardingsphere.agent.plugin.metrics.core.constant.MetricIds;
import org.apache.shardingsphere.infra.binder.QueryContext;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
@@ -35,17 +32,15 @@ import
org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateState
import java.lang.reflect.Method;
/**
- * Route count advice.
+ * SQL route count advice.
*/
-public final class RouteCountAdvice implements InstanceMethodAdvice {
+public final class SQLRouteCountAdvice implements InstanceMethodAdvice {
static {
MetricsPool.create(MetricIds.ROUTED_INSERT_SQL);
MetricsPool.create(MetricIds.ROUTED_DELETE_SQL);
MetricsPool.create(MetricIds.ROUTED_UPDATE_SQL);
MetricsPool.create(MetricIds.ROUTED_SELECT_SQL);
- MetricsPool.create(MetricIds.ROUTED_DATA_SOURCES);
- MetricsPool.create(MetricIds.ROUTED_TABLES);
}
@Override
@@ -62,16 +57,4 @@ public final class RouteCountAdvice implements
InstanceMethodAdvice {
MetricsPool.get(MetricIds.ROUTED_SELECT_SQL).ifPresent(MetricsWrapper::inc);
}
}
-
- @Override
- public void afterMethod(final TargetAdviceObject target, final Method
method, final Object[] args, final Object result) {
- if (null == result) {
- return;
- }
- for (RouteUnit each : ((RouteContext) result).getRouteUnits()) {
- RouteMapper dataSourceMapper = each.getDataSourceMapper();
- MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).ifPresent(optional
-> optional.inc(dataSourceMapper.getActualName()));
- each.getTableMappers().forEach(table ->
MetricsPool.get(MetricIds.ROUTED_TABLES).ifPresent(optional ->
optional.inc(table.getActualName())));
- }
- }
}
diff --git
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/MetricsAdviceBaseTest.java
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/MetricsAdviceBaseTest.java
index 476c6794de4..47869fe51e4 100644
---
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/MetricsAdviceBaseTest.java
+++
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/MetricsAdviceBaseTest.java
@@ -33,6 +33,23 @@ public abstract class MetricsAdviceBaseTest {
@After
public void reset() {
+ MetricsPool.get(MetricIds.PARSED_INSERT_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_UPDATE_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_DELETE_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_SELECT_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_DDL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_DCL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_DAL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_TCL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_RQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_RDL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.PARSED_RAL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_INSERT_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_UPDATE_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_DELETE_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_SELECT_SQL).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).ifPresent(optional ->
((FixtureWrapper) optional).reset());
+ MetricsPool.get(MetricIds.ROUTED_TABLES).ifPresent(optional ->
((FixtureWrapper) optional).reset());
MetricsPool.get(MetricIds.PROXY_EXECUTE_LATENCY_MILLIS).ifPresent(optional ->
((FixtureWrapper) optional).reset());
MetricsPool.get(MetricIds.PROXY_EXECUTE_ERRORS).ifPresent(optional ->
((FixtureWrapper) optional).reset());
MetricsPool.get(MetricIds.PROXY_CURRENT_CONNECTIONS).ifPresent(optional ->
((FixtureWrapper) optional).reset());
diff --git
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
new file mode 100644
index 00000000000..73e8e8cf6fe
--- /dev/null
+++
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteResultCountAdviceTest.java
@@ -0,0 +1,52 @@
+/*
+ * 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.shardingsphere.agent.plugin.metrics.core.advice;
+
+import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsPool;
+import org.apache.shardingsphere.agent.plugin.metrics.core.constant.MetricIds;
+import
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.FixtureWrapper;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.junit.Test;
+
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class RouteResultCountAdviceTest extends MetricsAdviceBaseTest {
+
+ @Test
+ public void assertCountRouteResult() {
+ RouteContext routeContext = new RouteContext();
+ RouteMapper dataSourceMapper = new RouteMapper("logic_db", "ds_0");
+ RouteMapper tableMapper = new RouteMapper("t_order", "t_order_0");
+ routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper,
Collections.singleton(tableMapper)));
+ new RouteResultCountAdvice().afterMethod(new MockTargetAdviceObject(),
mock(Method.class), new Object[]{}, routeContext);
+ FixtureWrapper wrapper = (FixtureWrapper)
MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).get();
+ assertTrue(MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).isPresent());
+ assertThat(wrapper.getFixtureValue(), is(1d));
+ wrapper = (FixtureWrapper)
MetricsPool.get(MetricIds.ROUTED_TABLES).get();
+ assertTrue(MetricsPool.get(MetricIds.ROUTED_TABLES).isPresent());
+ assertThat(wrapper.getFixtureValue(), is(1d));
+ }
+}
diff --git
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdviceTest.java
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
similarity index 69%
rename from
agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdviceTest.java
rename to
agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
index b203b232626..6b32415e713 100644
---
a/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/RouteCountAdviceTest.java
+++
b/agent/plugins/metrics/core/src/test/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/SQLRouteCountAdviceTest.java
@@ -23,9 +23,6 @@ import
org.apache.shardingsphere.agent.plugin.metrics.core.fixture.FixtureWrappe
import org.apache.shardingsphere.infra.binder.QueryContext;
import
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
import org.apache.shardingsphere.infra.context.ConnectionContext;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
-import org.apache.shardingsphere.infra.route.context.RouteUnit;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLDeleteStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLInsertStatement;
import
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQLSelectStatement;
@@ -40,9 +37,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
-public final class RouteCountAdviceTest extends MetricsAdviceBaseTest {
+public final class SQLRouteCountAdviceTest extends MetricsAdviceBaseTest {
- private final RouteCountAdvice advice = new RouteCountAdvice();
+ private final SQLRouteCountAdvice advice = new SQLRouteCountAdvice();
@Test
public void assertInsertRoute() {
@@ -69,24 +66,8 @@ public final class RouteCountAdviceTest extends
MetricsAdviceBaseTest {
}
public void assertRoute(final String metricIds, final QueryContext
queryContext) {
- MockTargetAdviceObject targetObject = new MockTargetAdviceObject();
- advice.beforeMethod(targetObject, mock(Method.class), new Object[]{new
ConnectionContext(), queryContext});
+ advice.beforeMethod(new MockTargetAdviceObject(), mock(Method.class),
new Object[]{new ConnectionContext(), queryContext});
assertTrue(MetricsPool.get(metricIds).isPresent());
assertThat(((FixtureWrapper)
MetricsPool.get(metricIds).get()).getFixtureValue(), is(1d));
}
-
- @Test
- public void assertRouteDataSourceAndTable() {
- RouteContext routeContext = new RouteContext();
- RouteMapper dataSourceMapper = new RouteMapper("logic_db", "ds_0");
- RouteMapper tableMapper = new RouteMapper("t_order", "t_order_0");
- routeContext.getRouteUnits().add(new RouteUnit(dataSourceMapper,
Collections.singleton(tableMapper)));
- advice.afterMethod(new MockTargetAdviceObject(), mock(Method.class),
new Object[]{}, routeContext);
- FixtureWrapper wrapper = (FixtureWrapper)
MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).get();
- assertTrue(MetricsPool.get(MetricIds.ROUTED_DATA_SOURCES).isPresent());
- assertThat(wrapper.getFixtureValue(), is(1d));
- wrapper = (FixtureWrapper)
MetricsPool.get(MetricIds.ROUTED_TABLES).get();
- assertTrue(MetricsPool.get(MetricIds.ROUTED_TABLES).isPresent());
- assertThat(wrapper.getFixtureValue(), is(1d));
- }
}
diff --git
a/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-proxy-advisors.yaml
b/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-proxy-advisors.yaml
index 390e2300159..71ef799ecd0 100644
---
a/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-proxy-advisors.yaml
+++
b/agent/plugins/metrics/type/prometheus/src/main/resources/META-INF/conf/prometheus-proxy-advisors.yaml
@@ -49,7 +49,12 @@ advisors:
- name: rollback
type: method
- target: org.apache.shardingsphere.infra.route.engine.SQLRouteEngine
- advice:
org.apache.shardingsphere.agent.plugin.metrics.core.advice.RouteCountAdvice
+ advice:
org.apache.shardingsphere.agent.plugin.metrics.core.advice.SQLRouteCountAdvice
+ pointcuts:
+ - name: route
+ type: method
+ - target: org.apache.shardingsphere.infra.route.engine.SQLRouteEngine
+ advice:
org.apache.shardingsphere.agent.plugin.metrics.core.advice.RouteResultCountAdvice
pointcuts:
- name: route
type: method