From 0975809e43c434ffeb09b9203165c578520f7a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E4=BC=9F=E6=9D=B0?= Date: Wed, 30 Jun 2021 21:00:20 +0800 Subject: [PATCH] Add an agent plugin for ShardingSphere 5.0.0-beta (#7171) --- .github/workflows/plugins-test.2.yaml | 1 + CHANGES.md | 1 + apm-sniffer/apm-sdk-plugin/pom.xml | 1 + .../shardingsphere-5.0.0-beta-plugin/pom.xml | 73 ++++++ .../shardingsphere/v500beta/Constant.java | 24 ++ .../v500beta/ExecuteInterceptor.java | 59 +++++ .../v500beta/JDBCRootInvokeInterceptor.java | 52 ++++ .../v500beta/ParseInterceptor.java | 53 ++++ .../v500beta/ProxyRootInvokeInterceptor.java | 52 ++++ .../v500beta/RewriteInterceptor.java | 50 ++++ .../v500beta/RouteInterceptor.java | 50 ++++ ...ShardingSphereV500BetaInstrumentation.java | 28 +++ .../define/ExecuteInstrumentation.java | 71 ++++++ .../define/JDBCRootInvokeInstrumentation.java | 87 +++++++ .../v500beta/define/ParseInstrumentation.java | 69 ++++++ .../ProxyRootInvokeInstrumentation.java | 69 ++++++ .../define/RewriteInstrumentation.java | 69 ++++++ .../v500beta/define/RouteInstrumentation.java | 69 ++++++ .../src/main/resources/skywalking-plugin.def | 21 ++ .../v500beta/InterceptorTest.java | 168 +++++++++++++ .../service-agent/java-agent/Plugin-list.md | 1 + .../java-agent/Supported-list.md | 2 +- .../bin/startup.sh | 21 ++ .../config/expectedData.yaml | 231 ++++++++++++++++++ .../configuration.yml | 20 ++ .../pom.xml | 122 +++++++++ .../src/main/assembly/assembly.xml | 41 ++++ .../testcase/shardingsphere/Application.java | 35 +++ .../controller/CaseController.java | 60 +++++ .../service/api/entity/Order.java | 61 +++++ .../service/api/entity/OrderItem.java | 71 ++++++ .../api/repository/CommonRepository.java | 38 +++ .../api/repository/OrderItemRepository.java | 24 ++ .../api/repository/OrderRepository.java | 24 ++ .../service/api/service/CommonService.java | 32 +++ .../api/service/CommonServiceImpl.java | 108 ++++++++ ...atabasesAndTablesConfigurationPrecise.java | 94 +++++++ .../jdbc/JDBCOrderItemRepositoryImpl.java | 126 ++++++++++ .../jdbc/JDBCOrderRepositoryImpl.java | 124 ++++++++++ .../repository/service/RawPojoService.java | 60 +++++ .../utility/config/DataSourceUtil.java | 54 ++++ .../utility/config/ExampleConfiguration.java | 27 ++ .../src/main/resources/application.properties | 17 ++ .../support-version.list | 17 ++ 44 files changed, 2476 insertions(+), 1 deletion(-) create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/pom.xml create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/Constant.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ExecuteInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/JDBCRootInvokeInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ParseInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ProxyRootInvokeInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RewriteInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RouteInterceptor.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/AbstractShardingSphereV500BetaInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ExecuteInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/JDBCRootInvokeInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ParseInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ProxyRootInvokeInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RewriteInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RouteInstrumentation.java create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/resources/skywalking-plugin.def create mode 100644 apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/InterceptorTest.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/bin/startup.sh create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/config/expectedData.yaml create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/configuration.yml create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/pom.xml create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/assembly/assembly.xml create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/Application.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/controller/CaseController.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/Order.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/OrderItem.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/CommonRepository.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderItemRepository.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderRepository.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonService.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonServiceImpl.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/config/ShardingDatabasesAndTablesConfigurationPrecise.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderItemRepositoryImpl.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderRepositoryImpl.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/service/RawPojoService.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/DataSourceUtil.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/ExampleConfiguration.java create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/resources/application.properties create mode 100644 test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/support-version.list diff --git a/.github/workflows/plugins-test.2.yaml b/.github/workflows/plugins-test.2.yaml index 4cc71284bf..a1191ddeba 100644 --- a/.github/workflows/plugins-test.2.yaml +++ b/.github/workflows/plugins-test.2.yaml @@ -60,6 +60,7 @@ jobs: - shardingsphere-4.x-RC3-scenario - shardingsphere-4.0.x-scenario - shardingsphere-4.x-scenario + - shardingsphere-5.0.0-beta-scenario - sofarpc-scenario - solrj-7.x-scenario - spring-3.0.x-scenario diff --git a/CHANGES.md b/CHANGES.md index 9323e9942b..a6a4609145 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -23,6 +23,7 @@ Release Notes. * Add `Neo4j-4.x` plugin. * Correct `profile.duration` to `profile.max_duration` in the default `agent.config` file. * Fix the response time of gRPC. +* Add `ShardingSphere-5.0.0-beta` plugin. #### OAP-Backend diff --git a/apm-sniffer/apm-sdk-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/pom.xml index a6cff87c82..8eb9bdf21c 100644 --- a/apm-sniffer/apm-sdk-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/pom.xml @@ -52,6 +52,7 @@ sharding-sphere-4.x-RC3-plugin sharding-sphere-4.0.x-plugin sharding-sphere-4.1.0-plugin + shardingsphere-5.0.0-beta-plugin xmemcached-2.x-plugin grpc-1.x-plugin mysql-5.x-plugin diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/pom.xml new file mode 100644 index 0000000000..8c3609e6ca --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/pom.xml @@ -0,0 +1,73 @@ + + + + + + apm-sdk-plugin + org.apache.skywalking + 8.7.0-SNAPSHOT + + 4.0.0 + + apm-shardingsphere-5.0.0-beta-plugin + jar + + shardingsphere-5.0.0-beta-plugin + http://maven.apache.org + + + UTF-8 + 5.0.0-beta + + + + + mysql + mysql-connector-java + [2.0.14,6.0.6] + test + + + org.apache.shardingsphere + shardingsphere-infra-executor + ${shardingsphere.version} + provided + + + org.apache.shardingsphere + shardingsphere-jdbc-core + ${shardingsphere.version} + provided + + + org.apache.shardingsphere + shardingsphere-sql-parser-engine + ${shardingsphere.version} + provided + + + + + + + maven-deploy-plugin + + + + diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/Constant.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/Constant.java new file mode 100644 index 0000000000..6848fcf5f1 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/Constant.java @@ -0,0 +1,24 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +public final class Constant { + + public static final String CONTEXT_SNAPSHOT = "CONTEXT_SNAPSHOT"; +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ExecuteInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ExecuteInterceptor.java new file mode 100644 index 0000000000..c68abb32e1 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ExecuteInterceptor.java @@ -0,0 +1,59 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.shardingsphere.infra.executor.kernel.model.ExecutorDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.ContextSnapshot; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; +import java.util.Map; + +public class ExecuteInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/executeSQL/").setComponent(ComponentsDefine.SHARDING_SPHERE); + ContextSnapshot contextSnapshot = (ContextSnapshot) ExecutorDataMap.getValue().get(Constant.CONTEXT_SNAPSHOT); + if (null == contextSnapshot) { + contextSnapshot = (ContextSnapshot) ((Map) allArguments[2]).get(Constant.CONTEXT_SNAPSHOT); + } + if (null != contextSnapshot) { + ContextManager.continued(contextSnapshot); + } + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/JDBCRootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/JDBCRootInvokeInterceptor.java new file mode 100644 index 0000000000..c482fb5d27 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/JDBCRootInvokeInterceptor.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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.shardingsphere.infra.executor.kernel.model.ExecutorDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +public class JDBCRootInvokeInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/JDBCRootInvoke/") + .setComponent(ComponentsDefine.SHARDING_SPHERE); + ExecutorDataMap.getValue().put(Constant.CONTEXT_SNAPSHOT, ContextManager.capture()); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ParseInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ParseInterceptor.java new file mode 100644 index 0000000000..27188e7d1b --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ParseInterceptor.java @@ -0,0 +1,53 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.context.tag.Tags; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +public class ParseInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + AbstractSpan span = ContextManager.createLocalSpan("/ShardingSphere/parseSQL/") + .setComponent(ComponentsDefine.SHARDING_SPHERE); + Tags.DB_STATEMENT.set(span, (String) allArguments[0]); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ProxyRootInvokeInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ProxyRootInvokeInterceptor.java new file mode 100644 index 0000000000..ea2b24c627 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/ProxyRootInvokeInterceptor.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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.shardingsphere.infra.executor.kernel.model.ExecutorDataMap; +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +public class ProxyRootInvokeInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/ProxyRootInvoke/") + .setComponent(ComponentsDefine.SHARDING_SPHERE); + ExecutorDataMap.getValue().put(Constant.CONTEXT_SNAPSHOT, ContextManager.capture()); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RewriteInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RewriteInterceptor.java new file mode 100644 index 0000000000..8fc2aef619 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RewriteInterceptor.java @@ -0,0 +1,50 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +public class RewriteInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/rewriteSQL/") + .setComponent(ComponentsDefine.SHARDING_SPHERE); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RouteInterceptor.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RouteInterceptor.java new file mode 100644 index 0000000000..33e5b0b9ab --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/RouteInterceptor.java @@ -0,0 +1,50 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.skywalking.apm.agent.core.context.ContextManager; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult; +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; + +import java.lang.reflect.Method; + +public class RouteInterceptor implements InstanceMethodsAroundInterceptor { + + @Override + public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + MethodInterceptResult result) { + ContextManager.createLocalSpan("/ShardingSphere/routeSQL/") + .setComponent(ComponentsDefine.SHARDING_SPHERE); + } + + @Override + public Object afterMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class[] argumentsTypes, + Object ret) { + ContextManager.stopSpan(); + return ret; + } + + @Override + public void handleMethodException(EnhancedInstance objInst, Method method, Object[] allArguments, + Class[] argumentsTypes, Throwable t) { + ContextManager.activeSpan().log(t); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/AbstractShardingSphereV500BetaInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/AbstractShardingSphereV500BetaInstrumentation.java new file mode 100644 index 0000000000..48aeca9ccd --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/AbstractShardingSphereV500BetaInstrumentation.java @@ -0,0 +1,28 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; + +public abstract class AbstractShardingSphereV500BetaInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { + + @Override + protected final String[] witnessClasses() { + return new String[]{"org.apache.shardingsphere.infra.context.kernel.KernelProcessor"}; + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ExecuteInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ExecuteInstrumentation.java new file mode 100644 index 0000000000..4570a39f88 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ExecuteInstrumentation.java @@ -0,0 +1,71 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.ExecuteInterceptor; + +import static net.bytebuddy.matcher.ElementMatchers.isPrivate; +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ExecuteInstrumentation} presents that skywalking intercepts {@link + * org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback}. + */ +public class ExecuteInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String ENHANCE_CLASS = "org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutorCallback"; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("execute").and(isPrivate()); + } + + @Override + public String getMethodsInterceptor() { + return ExecuteInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/JDBCRootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/JDBCRootInvokeInstrumentation.java new file mode 100644 index 0000000000..fcc9c6d770 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/JDBCRootInvokeInstrumentation.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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.JDBCRootInvokeInterceptor; + +import static net.bytebuddy.matcher.ElementMatchers.named; +import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; +import static org.apache.skywalking.apm.agent.core.plugin.match.MultiClassNameMatch.byMultiClassMatch; + +/** + * {@link JDBCRootInvokeInstrumentation} presents that skywalking intercepts + * + */ +public class JDBCRootInvokeInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String[] ENHANCE_CLASSES = { + "org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSphereStatement", + "org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement" + }; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + + @Override + public ElementMatcher getMethodsMatcher() { + return named("executeQuery") + .or(named("executeUpdate")) + .or(named("execute0")) + .or(named("execute").and(takesNoArguments())) + .or(named("executeBatch")); + } + + @Override + public String getMethodsInterceptor() { + return JDBCRootInvokeInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return byMultiClassMatch(ENHANCE_CLASSES); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ParseInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ParseInstrumentation.java new file mode 100644 index 0000000000..4feba63ea2 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ParseInstrumentation.java @@ -0,0 +1,69 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.ParseInterceptor; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * {@link ParseInstrumentation} presents that skywalking intercepts {@link org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine}. + */ +public class ParseInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String ENHANCE_CLASS = "org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine"; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("parse0"); + } + + @Override + public String getMethodsInterceptor() { + return ParseInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ProxyRootInvokeInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ProxyRootInvokeInstrumentation.java new file mode 100644 index 0000000000..ddf194c4d8 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/ProxyRootInvokeInstrumentation.java @@ -0,0 +1,69 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.ProxyRootInvokeInterceptor; + +import static net.bytebuddy.matcher.ElementMatchers.named; + +/** + * ProxyRootInvokeInstrumentation presents that skywalking intercepts org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask. + */ +public class ProxyRootInvokeInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String ENHANCE_CLASS = "org.apache.shardingsphere.proxy.frontend.command.CommandExecutorTask"; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[] { + new InstanceMethodsInterceptPoint() { + @Override + public ElementMatcher getMethodsMatcher() { + return named("run"); + } + + @Override + public String getMethodsInterceptor() { + return ProxyRootInvokeInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RewriteInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RewriteInstrumentation.java new file mode 100644 index 0000000000..b780633736 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RewriteInstrumentation.java @@ -0,0 +1,69 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import net.bytebuddy.matcher.ElementMatchers; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.RewriteInterceptor; + +/** + * {@link RewriteInstrumentation} presents that skywalking intercepts {@link org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry}. + */ +public class RewriteInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String ENHANCE_CLASS = "org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry"; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + + @Override + public ElementMatcher getMethodsMatcher() { + return ElementMatchers.named("rewrite"); + } + + @Override + public String getMethodsInterceptor() { + return RewriteInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RouteInstrumentation.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RouteInstrumentation.java new file mode 100644 index 0000000000..f31dc03896 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/define/RouteInstrumentation.java @@ -0,0 +1,69 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta.define; + +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; +import net.bytebuddy.matcher.ElementMatchers; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch; +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch; +import org.apache.skywalking.apm.plugin.shardingsphere.v500beta.RouteInterceptor; + +/** + * {@link RouteInstrumentation} presents that skywalking intercepts {@link org.apache.shardingsphere.infra.route.engine.SQLRouteEngine}. + */ +public class RouteInstrumentation extends AbstractShardingSphereV500BetaInstrumentation { + + private static final String ENHANCE_CLASS = "org.apache.shardingsphere.infra.route.engine.SQLRouteEngine"; + + @Override + public InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { + return new InstanceMethodsInterceptPoint[]{ + new InstanceMethodsInterceptPoint() { + + @Override + public ElementMatcher getMethodsMatcher() { + return ElementMatchers.named("route"); + } + + @Override + public String getMethodsInterceptor() { + return RouteInterceptor.class.getName(); + } + + @Override + public boolean isOverrideArgs() { + return false; + } + } + }; + } + + @Override + public ConstructorInterceptPoint[] getConstructorsInterceptPoints() { + return new ConstructorInterceptPoint[0]; + } + + @Override + protected ClassMatch enhanceClass() { + return NameMatch.byName(ENHANCE_CLASS); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/resources/skywalking-plugin.def new file mode 100644 index 0000000000..25a0f05781 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/main/resources/skywalking-plugin.def @@ -0,0 +1,21 @@ +# 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. +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.ProxyRootInvokeInstrumentation +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.JDBCRootInvokeInstrumentation +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.ParseInstrumentation +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.RouteInstrumentation +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.RewriteInstrumentation +shardingsphere-5.0.0-beta=org.apache.skywalking.apm.plugin.shardingsphere.v500beta.define.ExecuteInstrumentation diff --git a/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/InterceptorTest.java b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/InterceptorTest.java new file mode 100644 index 0000000000..149fa07a08 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/shardingsphere-5.0.0-beta-plugin/src/test/java/org/apache/skywalking/apm/plugin/shardingsphere/v500beta/InterceptorTest.java @@ -0,0 +1,168 @@ +/* + * 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.skywalking.apm.plugin.shardingsphere.v500beta; + +import org.apache.shardingsphere.infra.binder.LogicSQL; +import org.apache.skywalking.apm.agent.core.context.trace.AbstractTracingSpan; +import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment; +import org.apache.skywalking.apm.agent.test.helper.SegmentHelper; +import org.apache.skywalking.apm.agent.test.tools.AgentServiceRule; +import org.apache.skywalking.apm.agent.test.tools.SegmentStorage; +import org.apache.skywalking.apm.agent.test.tools.SegmentStoragePoint; +import org.apache.skywalking.apm.agent.test.tools.SpanAssert; +import org.apache.skywalking.apm.agent.test.tools.TracingSegmentRunner; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.modules.junit4.PowerMockRunnerDelegate; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +@RunWith(PowerMockRunner.class) +@PowerMockRunnerDelegate(TracingSegmentRunner.class) +public class InterceptorTest { + + @SegmentStoragePoint + private SegmentStorage segmentStorage; + + @Rule + public AgentServiceRule serviceRule = new AgentServiceRule(); + + private ProxyRootInvokeInterceptor proxyRootInvokeInterceptor; + + private JDBCRootInvokeInterceptor jdbcRootInvokeInterceptor; + + private ParseInterceptor parseInterceptor; + + private RouteInterceptor routeInterceptor; + + private RewriteInterceptor rewriteInterceptor; + + private ExecuteInterceptor executeInterceptor; + + @Before + public void setUp() { + proxyRootInvokeInterceptor = new ProxyRootInvokeInterceptor(); + jdbcRootInvokeInterceptor = new JDBCRootInvokeInterceptor(); + parseInterceptor = new ParseInterceptor(); + routeInterceptor = new RouteInterceptor(); + rewriteInterceptor = new RewriteInterceptor(); + executeInterceptor = new ExecuteInterceptor(); + } + + @Test + public void assertProxyRootInvoke() { + proxyRootInvokeInterceptor.beforeMethod(null, null, null, null, null); + proxyRootInvokeInterceptor.afterMethod(null, null, null, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/ProxyRootInvoke/")); + } + + @Test + public void assertJDBCRootInvoke() { + jdbcRootInvokeInterceptor.beforeMethod(null, null, null, null, null); + jdbcRootInvokeInterceptor.afterMethod(null, null, null, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/JDBCRootInvoke/")); + } + + @Test + public void assertParse() { + Object[] allArguments = new Object[] { + "SELECT * FROM t_order", + false + }; + parseInterceptor.beforeMethod(null, null, allArguments, null, null); + parseInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/parseSQL/")); + SpanAssert.assertTag(spans.get(0), 0, "SELECT * FROM t_order"); + } + + @Test + public void assertRoute() { + Object[] allArguments = new Object[]{ + new LogicSQL(null, "SELECT * FROM t_order", Collections.emptyList()), + null + }; + routeInterceptor.beforeMethod(null, null, allArguments, null, null); + routeInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/routeSQL/")); + } + + @Test + public void assertRewrite() { + Object[] allArguments = new Object[]{ + "SELECT * FROM t_order", + Collections.emptyList(), + null, + null + }; + rewriteInterceptor.beforeMethod(null, null, allArguments, null, null); + rewriteInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/rewriteSQL/")); + } + + @Test + public void assertExecute() { + Object[] allArguments = new Object[]{ + null, + null, + new HashMap<>() + }; + executeInterceptor.beforeMethod(null, null, allArguments, null, null); + executeInterceptor.afterMethod(null, null, allArguments, null, null); + assertThat(segmentStorage.getTraceSegments().size(), is(1)); + TraceSegment segment = segmentStorage.getTraceSegments().get(0); + List spans = SegmentHelper.getSpans(segment); + assertNotNull(spans); + assertThat(spans.size(), is(1)); + assertThat(spans.get(0).getOperationName(), is("/ShardingSphere/executeSQL/")); + } +} diff --git a/docs/en/setup/service-agent/java-agent/Plugin-list.md b/docs/en/setup/service-agent/java-agent/Plugin-list.md index 54a31e6f63..6acd1687c2 100644 --- a/docs/en/setup/service-agent/java-agent/Plugin-list.md +++ b/docs/en/setup/service-agent/java-agent/Plugin-list.md @@ -73,6 +73,7 @@ - sharding-sphere-4.1.0 - sharding-sphere-4.x - sharding-sphere-4.x-rc3 +- shardingsphere-5.0.0-beta - sofarpc - solrj-7.x - spring-annotation diff --git a/docs/en/setup/service-agent/java-agent/Supported-list.md b/docs/en/setup/service-agent/java-agent/Supported-list.md index 8f295c4af3..0ec7b25ec6 100644 --- a/docs/en/setup/service-agent/java-agent/Supported-list.md +++ b/docs/en/setup/service-agent/java-agent/Supported-list.md @@ -36,7 +36,7 @@ metrics based on the tracing data. * Oracle Driver (Optional¹) * H2 Driver 1.3.x -> 1.4.x * [Sharding-JDBC](https://github.com/shardingjdbc/sharding-jdbc) 1.5.x - * [ShardingSphere](https://github.com/apache/shardingsphere) 3.0.0, 4.0.0-RC1, 4.0.0, 4.0.1, 4.1.0, 4.1.1 + * [ShardingSphere](https://github.com/apache/shardingsphere) 3.0.0, 4.0.0-RC1, 4.0.0, 4.0.1, 4.1.0, 4.1.1, 5.0.0-beta * PostgreSQL Driver 8.x, 9.x, 42.x * Mariadb Driver 2.x, 1.8 * [InfluxDB](https://github.com/influxdata/influxdb-java) 2.5 -> 2.17 diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/bin/startup.sh b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/bin/startup.sh new file mode 100644 index 0000000000..b78ea920e0 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/bin/startup.sh @@ -0,0 +1,21 @@ +#!/bin/bash +# +# 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. + +home="$(cd "$(dirname $0)"; pwd)" + +java -jar ${agent_opts} ${home}/../libs/shardingsphere-5.0.0-beta-scenario.jar & diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/config/expectedData.yaml b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/config/expectedData.yaml new file mode 100644 index 0000000000..a2ced41053 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/config/expectedData.yaml @@ -0,0 +1,231 @@ +# 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. +segmentItems: +- serviceName: shardingsphere-5.0.0-beta-scenario + segmentSize: gt 0 + segments: + - segmentId: not null + spans: + - operationName: H2/JDBI/PreparedStatement/executeQuery + operationId: 0 + parentSpanId: 0 + spanId: 1 + spanLayer: Database + startTime: not null + endTime: not null + componentId: 32 + isError: false + spanType: Exit + peer: localhost:-1 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: demo_ds_1} + - {key: db.statement, value: SELECT * FROM t_order_0} + - operationName: /ShardingSphere/executeSQL/ + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + refs: + - parentEndpoint: /shardingsphere-5.0.0-beta-scenario/case/execute + networkAddress: '' + refType: CrossThread + parentSpanId: 2 + parentTraceSegmentId: not null + parentServiceInstance: not null + parentService: shardingsphere-5.0.0-beta-scenario + traceId: not null + - segmentId: not null + spans: + - operationName: H2/JDBI/PreparedStatement/executeQuery + operationId: 0 + parentSpanId: 0 + spanId: 1 + spanLayer: Database + startTime: not null + endTime: not null + componentId: 32 + isError: false + spanType: Exit + peer: localhost:-1 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: demo_ds_0} + - {key: db.statement, value: SELECT * FROM t_order_1} + - operationName: /ShardingSphere/executeSQL/ + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + refs: + - parentEndpoint: /shardingsphere-5.0.0-beta-scenario/case/execute + networkAddress: '' + refType: CrossThread + parentSpanId: 2 + parentTraceSegmentId: not null + parentServiceInstance: not null + parentService: shardingsphere-5.0.0-beta-scenario + traceId: not null + - segmentId: not null + spans: + - operationName: H2/JDBI/PreparedStatement/executeQuery + operationId: 0 + parentSpanId: 0 + spanId: 1 + spanLayer: Database + startTime: not null + endTime: not null + componentId: 32 + isError: false + spanType: Exit + peer: localhost:-1 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: demo_ds_1} + - {key: db.statement, value: SELECT * FROM t_order_1} + - operationName: /ShardingSphere/executeSQL/ + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + refs: + - parentEndpoint: /shardingsphere-5.0.0-beta-scenario/case/execute + networkAddress: '' + refType: CrossThread + parentSpanId: 2 + parentTraceSegmentId: not null + parentServiceInstance: not null + parentService: shardingsphere-5.0.0-beta-scenario + traceId: not null + - segmentId: not null + spans: + - operationName: /ShardingSphere/parseSQL/ + operationId: 0 + parentSpanId: 0 + spanId: 1 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + tags: + - {key: db.statement, value: SELECT * FROM t_order} + - operationName: /ShardingSphere/routeSQL/ + operationId: 0 + parentSpanId: 2 + spanId: 3 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: /ShardingSphere/rewriteSQL/ + operationId: 0 + parentSpanId: 2 + spanId: 4 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: H2/JDBI/PreparedStatement/executeQuery + operationId: 0 + parentSpanId: 5 + spanId: 6 + spanLayer: Database + startTime: not null + endTime: not null + componentId: 32 + isError: false + spanType: Exit + peer: localhost:-1 + skipAnalysis: false + tags: + - {key: db.type, value: sql} + - {key: db.instance, value: demo_ds_0} + - {key: db.statement, value: SELECT * FROM t_order_0} + - operationName: /ShardingSphere/executeSQL/ + operationId: 0 + parentSpanId: 2 + spanId: 5 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: /ShardingSphere/JDBCRootInvoke/ + operationId: 0 + parentSpanId: 0 + spanId: 2 + spanLayer: Unknown + startTime: not null + endTime: not null + componentId: 60 + isError: false + spanType: Local + peer: '' + skipAnalysis: false + - operationName: /shardingsphere-5.0.0-beta-scenario/case/execute + operationId: 0 + parentSpanId: -1 + spanId: 0 + spanLayer: Http + startTime: not null + endTime: not null + componentId: 1 + isError: false + spanType: Entry + peer: '' + skipAnalysis: false + tags: + - {key: url, value: 'http://localhost:8080/shardingsphere-5.0.0-beta-scenario/case/execute'} + - {key: http.method, value: GET} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/configuration.yml b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/configuration.yml new file mode 100644 index 0000000000..581097b499 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/configuration.yml @@ -0,0 +1,20 @@ +# 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. + +type: jvm +entryService: http://localhost:8080/shardingsphere-5.0.0-beta-scenario/case/execute +healthCheck: http://localhost:8080/shardingsphere-5.0.0-beta-scenario/case/healthCheck +startScript: ./bin/startup.sh diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/pom.xml b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/pom.xml new file mode 100644 index 0000000000..74225f445d --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/pom.xml @@ -0,0 +1,122 @@ + + + + 4.0.0 + + org.example + shardingsphere-5.0.0-beta-scenario + 5.0.0-beta + + skywalking-shardingsphere-5.0.0-beta-scenario + + UTF-8 + + 5.0.0-beta + + 4.3.8.RELEASE + 1.5.2.RELEASE + + + + + org.apache.shardingsphere + shardingsphere-infra-executor + ${test.framework.version} + + + org.apache.shardingsphere + shardingsphere-jdbc-core + ${test.framework.version} + + + org.springframework.boot + spring-boot-starter-tomcat + ${spring-boot-version} + + + org.springframework.boot + spring-boot-starter-web + ${spring-boot-version} + + + com.h2database + h2 + 1.4.196 + + + + shardingsphere-5.0.0-beta-scenario + + + org.apache.maven.plugins + maven-compiler-plugin + 3.6.0 + + 1.8 + 1.8 + ${project.build.sourceEncoding} + + + + org.springframework.boot + spring-boot-maven-plugin + 1.5.9.RELEASE + + + + repackage + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + assemble + package + + single + + + + src/main/assembly/assembly.xml + + ./target/ + + + + + + + + + + spring-snapshots + https://repo.spring.io/snapshot + + + spring-milestones + https://repo.spring.io/milestone + + + diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/assembly/assembly.xml b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/assembly/assembly.xml new file mode 100644 index 0000000000..b34d8814a1 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/assembly/assembly.xml @@ -0,0 +1,41 @@ + + + + + zip + + + + + ./bin + 0775 + + + + + + ${project.build.directory}/shardingsphere-5.0.0-beta-scenario.jar + ./libs + 0775 + + + diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/Application.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/Application.java new file mode 100644 index 0000000000..b912d27c27 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/Application.java @@ -0,0 +1,35 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.support.SpringBootServletInitializer; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(String[] args) { + try { + SpringApplication.run(Application.class, args); + } catch (Exception e) { + // Never do this + } + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/controller/CaseController.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/controller/CaseController.java new file mode 100644 index 0000000000..248058c0e9 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/controller/CaseController.java @@ -0,0 +1,60 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.controller; + +import javax.sql.DataSource; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.service.CommonService; +import org.apache.skywalking.apm.testcase.shardingsphere.service.config.ShardingDatabasesAndTablesConfigurationPrecise; +import org.apache.skywalking.apm.testcase.shardingsphere.service.repository.jdbc.JDBCOrderItemRepositoryImpl; +import org.apache.skywalking.apm.testcase.shardingsphere.service.repository.jdbc.JDBCOrderRepositoryImpl; +import org.apache.skywalking.apm.testcase.shardingsphere.service.repository.service.RawPojoService; +import org.apache.skywalking.apm.testcase.shardingsphere.service.utility.config.DataSourceUtil; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import java.sql.SQLException; + +@RestController +@RequestMapping("/case") +public class CaseController { + + private CommonService commonService = null; + + @RequestMapping("/healthCheck") + @ResponseBody + public String healthCheck() throws SQLException { + DataSourceUtil.createDataSource(""); + DataSourceUtil.createSchema("demo_ds_0"); + DataSourceUtil.createSchema("demo_ds_1"); + DataSourceUtil.createDataSource("demo_ds_0"); + DataSourceUtil.createDataSource("demo_ds_1"); + DataSource dataSource = new ShardingDatabasesAndTablesConfigurationPrecise().createDataSource(); + commonService = new RawPojoService(new JDBCOrderRepositoryImpl(dataSource), new JDBCOrderItemRepositoryImpl(dataSource)); + commonService.initEnvironment(); + return "Success"; + } + + @RequestMapping("/execute") + @ResponseBody + public String execute() { + commonService.processSuccess(false); + return "Success"; + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/Order.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/Order.java new file mode 100644 index 0000000000..5d00a352ff --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/Order.java @@ -0,0 +1,61 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.entity; + +import java.io.Serializable; + +public class Order implements Serializable { + + private static final long serialVersionUID = 661434701950670670L; + + private long orderId; + + private int userId; + + private String status; + + public long getOrderId() { + return orderId; + } + + public void setOrderId(final long orderId) { + this.orderId = orderId; + } + + public int getUserId() { + return userId; + } + + public void setUserId(final int userId) { + this.userId = userId; + } + + public String getStatus() { + return status; + } + + public void setStatus(final String status) { + this.status = status; + } + + @Override + public String toString() { + return String.format("order_id: %s, user_id: %s, status: %s", orderId, userId, status); + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/OrderItem.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/OrderItem.java new file mode 100644 index 0000000000..8ffa5b9fa6 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/entity/OrderItem.java @@ -0,0 +1,71 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.entity; + +import java.io.Serializable; + +public class OrderItem implements Serializable { + + private static final long serialVersionUID = 263434701950670170L; + + private long orderItemId; + + private long orderId; + + private int userId; + + private String status; + + public long getOrderItemId() { + return orderItemId; + } + + public void setOrderItemId(final long orderItemId) { + this.orderItemId = orderItemId; + } + + public long getOrderId() { + return orderId; + } + + public void setOrderId(final long orderId) { + this.orderId = orderId; + } + + public int getUserId() { + return userId; + } + + public void setUserId(final int userId) { + this.userId = userId; + } + + public String getStatus() { + return status; + } + + public void setStatus(final String status) { + this.status = status; + } + + @Override + public String toString() { + return String.format("order_item_id:%s, order_id: %s, user_id: %s, status: %s", orderItemId, orderId, userId, status); + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/CommonRepository.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/CommonRepository.java new file mode 100644 index 0000000000..51da35ec94 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/CommonRepository.java @@ -0,0 +1,38 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.repository; + +import java.util.List; + +public interface CommonRepository { + + void createTableIfNotExists(); + + void dropTable(); + + void truncateTable(); + + Long insert(T entity); + + void delete(Long id); + + List selectAll(); + + List selectRange(); +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderItemRepository.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderItemRepository.java new file mode 100644 index 0000000000..ded0048075 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderItemRepository.java @@ -0,0 +1,24 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.repository; + +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.OrderItem; + +public interface OrderItemRepository extends CommonRepository { +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderRepository.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderRepository.java new file mode 100644 index 0000000000..95e55a72cc --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/repository/OrderRepository.java @@ -0,0 +1,24 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.repository; + +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.Order; + +public interface OrderRepository extends CommonRepository { +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonService.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonService.java new file mode 100644 index 0000000000..066f122cde --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonService.java @@ -0,0 +1,32 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.service; + +public interface CommonService { + + void initEnvironment(); + + void cleanEnvironment(); + + void processSuccess(boolean isRangeSharding); + + void processFailure(); + + void printData(boolean isRangeSharding); +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonServiceImpl.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonServiceImpl.java new file mode 100644 index 0000000000..a1d358bbf4 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/api/service/CommonServiceImpl.java @@ -0,0 +1,108 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.api.service; + +import java.util.ArrayList; +import java.util.List; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.Order; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.OrderItem; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderItemRepository; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderRepository; + +public abstract class CommonServiceImpl implements CommonService { + + @Override + public void initEnvironment() { + getOrderRepository().createTableIfNotExists(); + getOrderItemRepository().createTableIfNotExists(); + getOrderRepository().truncateTable(); + getOrderItemRepository().truncateTable(); + insertData(); + } + + @Override + public void cleanEnvironment() { + getOrderRepository().dropTable(); + getOrderItemRepository().dropTable(); + } + + @Override + public void processSuccess(final boolean isRangeSharding) { + printData(isRangeSharding); + } + + @Override + public void processFailure() { + insertData(); + throw new RuntimeException("Exception occur for transaction test."); + } + + private List insertData() { + List result = new ArrayList<>(10); + for (int i = 1; i <= 10; i++) { + Order order = newOrder(); + order.setUserId(i); + order.setStatus("INSERT_TEST"); + getOrderRepository().insert(order); + OrderItem item = newOrderItem(); + item.setOrderId(order.getOrderId()); + item.setUserId(i); + item.setStatus("INSERT_TEST"); + getOrderItemRepository().insert(item); + result.add(order.getOrderId()); + } + return result; + } + + private void deleteData(final List orderIds) { + for (Long each : orderIds) { + getOrderRepository().delete(each); + getOrderItemRepository().delete(each); + } + } + + @Override + public void printData(final boolean isRangeSharding) { + if (isRangeSharding) { + printDataRange(); + } else { + printDataAll(); + } + } + + private void printDataRange() { + for (Object each : getOrderRepository().selectRange()) { + } + for (Object each : getOrderItemRepository().selectRange()) { + } + } + + private void printDataAll() { + for (Object each : getOrderRepository().selectAll()) { + } + } + + protected abstract OrderRepository getOrderRepository(); + + protected abstract OrderItemRepository getOrderItemRepository(); + + protected abstract Order newOrder(); + + protected abstract OrderItem newOrderItem(); +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/config/ShardingDatabasesAndTablesConfigurationPrecise.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/config/ShardingDatabasesAndTablesConfigurationPrecise.java new file mode 100644 index 0000000000..aa086cce1a --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/config/ShardingDatabasesAndTablesConfigurationPrecise.java @@ -0,0 +1,94 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.config; + +import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory; +import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration; +import org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration; +import org.apache.skywalking.apm.testcase.shardingsphere.service.utility.config.DataSourceUtil; +import org.apache.skywalking.apm.testcase.shardingsphere.service.utility.config.ExampleConfiguration; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public final class ShardingDatabasesAndTablesConfigurationPrecise implements ExampleConfiguration { + + private static DataSource DATA_SOURCE; + + @Override + public synchronized DataSource createDataSource() throws SQLException { + if (null == DATA_SOURCE) { + initDataSource(); + } + return DATA_SOURCE; + } + + private void initDataSource() throws SQLException { + ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); + shardingRuleConfig.getTables().add(getOrderTableRuleConfiguration()); + shardingRuleConfig.getTables().add(getOrderItemTableRuleConfiguration()); + shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); + shardingRuleConfig.getShardingAlgorithms().put("demo_ds_inline", createDbShardingAlgorithmConfiguration()); + shardingRuleConfig.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "demo_ds_inline")); + Properties tableOrderInlineProperties = new Properties(); + tableOrderInlineProperties.setProperty("algorithm-expression", "t_order_${order_id % 2}"); + shardingRuleConfig.getShardingAlgorithms().put("t_order_inline", new ShardingSphereAlgorithmConfiguration("INLINE", tableOrderInlineProperties)); + Properties tableOrderItemInlineProperties = new Properties(); + tableOrderItemInlineProperties.setProperty("algorithm-expression", "t_order_item_${order_id % 2}"); + shardingRuleConfig.getShardingAlgorithms().put("t_order_item_inline", new ShardingSphereAlgorithmConfiguration("INLINE", tableOrderItemInlineProperties)); + Properties properties = new Properties(); + properties.setProperty(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY.getKey(), "16"); + properties.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true"); + DATA_SOURCE = ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singletonList(shardingRuleConfig), properties); + } + + private static ShardingTableRuleConfiguration getOrderTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order", "demo_ds_${0..1}.t_order_${[0, 1]}"); + result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "SNOWFLAKE")); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_inline")); + return result; + } + + private static ShardingTableRuleConfiguration getOrderItemTableRuleConfiguration() { + ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order_item", "demo_ds_${0..1}.t_order_item_${[0, 1]}"); + result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_item_inline")); + return result; + } + + private ShardingSphereAlgorithmConfiguration createDbShardingAlgorithmConfiguration() { + Properties dbShardingAlgorithmrProps = new Properties(); + dbShardingAlgorithmrProps.setProperty("algorithm-expression", "demo_ds_${user_id % 2}"); + return new ShardingSphereAlgorithmConfiguration("INLINE", dbShardingAlgorithmrProps); + } + + private static Map createDataSourceMap() { + Map result = new HashMap<>(); + result.put("demo_ds_0", DataSourceUtil.getDataSource("demo_ds_0")); + result.put("demo_ds_1", DataSourceUtil.getDataSource("demo_ds_1")); + return result; + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderItemRepositoryImpl.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderItemRepositoryImpl.java new file mode 100644 index 0000000000..b99d440576 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderItemRepositoryImpl.java @@ -0,0 +1,126 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.repository.jdbc; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import javax.sql.DataSource; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.OrderItem; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderItemRepository; + +public final class JDBCOrderItemRepositoryImpl implements OrderItemRepository { + + private final DataSource dataSource; + + private final Connection connection; + + public JDBCOrderItemRepositoryImpl(final DataSource dataSource) throws SQLException { + this.dataSource = dataSource; + this.connection = dataSource.getConnection(); + } + + @Override + public void createTableIfNotExists() { + String sql = "CREATE TABLE IF NOT EXISTS t_order_item " + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_item_id))"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public void dropTable() { + String sql = "DROP TABLE t_order_item"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public void truncateTable() { + String sql = "TRUNCATE TABLE t_order_item"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public Long insert(final OrderItem orderItem) { + String sql = "INSERT INTO t_order_item (order_id, user_id, status) VALUES (?, ?, ?)"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setLong(1, orderItem.getOrderId()); + preparedStatement.setInt(2, orderItem.getUserId()); + preparedStatement.setString(3, orderItem.getStatus()); + preparedStatement.executeUpdate(); + try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { + if (resultSet.next()) { + orderItem.setOrderItemId(resultSet.getLong(1)); + } + } + } catch (final SQLException ignored) { + } + return orderItem.getOrderItemId(); + } + + @Override + public void delete(final Long orderItemId) { + String sql = "DELETE FROM t_order_item WHERE order_item_id=?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, orderItemId); + preparedStatement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public List selectAll() { + String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id"; + return getOrderItems(sql); + } + + @Override + public List selectRange() { + String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE o.order_id = i.order_id AND o.user_id BETWEEN 1 AND 5"; + return getOrderItems(sql); + } + + private List getOrderItems(final String sql) { + List result = new LinkedList<>(); + try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement + .executeQuery()) { + while (resultSet.next()) { + OrderItem orderItem = new OrderItem(); + orderItem.setOrderItemId(resultSet.getLong(1)); + orderItem.setOrderId(resultSet.getLong(2)); + orderItem.setUserId(resultSet.getInt(3)); + orderItem.setStatus(resultSet.getString(4)); + result.add(orderItem); + } + } catch (final SQLException ignored) { + } + return result; + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderRepositoryImpl.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderRepositoryImpl.java new file mode 100644 index 0000000000..dc4c80dd97 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/jdbc/JDBCOrderRepositoryImpl.java @@ -0,0 +1,124 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.repository.jdbc; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.LinkedList; +import java.util.List; +import javax.sql.DataSource; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.Order; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderRepository; + +public final class JDBCOrderRepositoryImpl implements OrderRepository { + + private final DataSource dataSource; + + private final Connection connection; + + public JDBCOrderRepositoryImpl(final DataSource dataSource) throws SQLException { + this.dataSource = dataSource; + this.connection = dataSource.getConnection(); + } + + @Override + public void createTableIfNotExists() { + String sql = "CREATE TABLE IF NOT EXISTS t_order (order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public void dropTable() { + String sql = "DROP TABLE t_order"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public void truncateTable() { + String sql = "TRUNCATE TABLE t_order"; + try (Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (final SQLException ignored) { + } + } + + @Override + public Long insert(final Order order) { + String sql = "INSERT INTO t_order (user_id, status) VALUES (?, ?)"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setInt(1, order.getUserId()); + preparedStatement.setString(2, order.getStatus()); + preparedStatement.executeUpdate(); + try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { + if (resultSet.next()) { + order.setOrderId(resultSet.getLong(1)); + } + } + } catch (final SQLException ignored) { + } + return order.getOrderId(); + } + + @Override + public void delete(final Long orderId) { + String sql = "DELETE FROM t_order WHERE order_id=?"; + try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, orderId); + preparedStatement.executeUpdate(); + } catch (final SQLException ignored) { + } + } + + @Override + public List selectAll() { + String sql = "SELECT * FROM t_order"; + return getOrders(sql); + } + + @Override + public List selectRange() { + String sql = "SELECT * FROM t_order WHERE order_id BETWEEN 200000000000000000 AND 400000000000000000"; + return getOrders(sql); + } + + private List getOrders(final String sql) { + List result = new LinkedList<>(); + try (PreparedStatement preparedStatement = connection.prepareStatement(sql); ResultSet resultSet = preparedStatement + .executeQuery()) { + while (resultSet.next()) { + Order order = new Order(); + order.setOrderId(resultSet.getLong(1)); + order.setUserId(resultSet.getInt(2)); + order.setStatus(resultSet.getString(3)); + result.add(order); + } + } catch (final SQLException ignored) { + } + return result; + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/service/RawPojoService.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/service/RawPojoService.java new file mode 100644 index 0000000000..796856a43a --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/repository/service/RawPojoService.java @@ -0,0 +1,60 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.repository.service; + +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.Order; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.entity.OrderItem; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderItemRepository; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.repository.OrderRepository; +import org.apache.skywalking.apm.testcase.shardingsphere.service.api.service.CommonServiceImpl; +import org.apache.skywalking.apm.testcase.shardingsphere.service.repository.jdbc.JDBCOrderItemRepositoryImpl; +import org.apache.skywalking.apm.testcase.shardingsphere.service.repository.jdbc.JDBCOrderRepositoryImpl; + +public class RawPojoService extends CommonServiceImpl { + + private final OrderRepository orderRepository; + + private final OrderItemRepository orderItemRepository; + + public RawPojoService(final JDBCOrderRepositoryImpl orderRepository, + final JDBCOrderItemRepositoryImpl orderItemRepository) { + this.orderRepository = orderRepository; + this.orderItemRepository = orderItemRepository; + } + + @Override + protected OrderRepository getOrderRepository() { + return orderRepository; + } + + @Override + protected OrderItemRepository getOrderItemRepository() { + return orderItemRepository; + } + + @Override + protected Order newOrder() { + return new Order(); + } + + @Override + protected OrderItem newOrderItem() { + return new OrderItem(); + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/DataSourceUtil.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/DataSourceUtil.java new file mode 100644 index 0000000000..5a2bfa57ec --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/DataSourceUtil.java @@ -0,0 +1,54 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.utility.config; + +import java.sql.Connection; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.HashMap; +import java.util.Map; +import javax.sql.DataSource; +import org.h2.jdbcx.JdbcDataSource; + +public class DataSourceUtil { + + private static final String DEFAULT_SCHEMA = ""; + + private static final Map DATA_SOURCE_MAP = new HashMap<>(); + + public static void createDataSource(final String dataSourceName) { + JdbcDataSource result = new JdbcDataSource(); + result.setUrl("jdbc:h2:mem:" + dataSourceName + ";DB_CLOSE_DELAY=-1"); + result.setUser("sa"); + result.setPassword(""); + DATA_SOURCE_MAP.put(dataSourceName, result); + } + + public static DataSource getDataSource(final String dataSourceName) { + return DATA_SOURCE_MAP.get(dataSourceName); + } + + public static void createSchema(final String dataSourceName) { + String sql = "CREATE SCHEMA " + dataSourceName; + try (Connection connection = getDataSource(DEFAULT_SCHEMA).getConnection(); Statement statement = connection.createStatement()) { + statement.execute(sql); + } catch (final SQLException ignored) { + } + } +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/ExampleConfiguration.java b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/ExampleConfiguration.java new file mode 100644 index 0000000000..c001502d0e --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/java/org/apache/skywalking/apm/testcase/shardingsphere/service/utility/config/ExampleConfiguration.java @@ -0,0 +1,27 @@ +/* + * 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.skywalking.apm.testcase.shardingsphere.service.utility.config; + +import java.sql.SQLException; +import javax.sql.DataSource; + +public interface ExampleConfiguration { + + DataSource createDataSource() throws SQLException; +} diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/resources/application.properties b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/resources/application.properties new file mode 100644 index 0000000000..67712019d8 --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/src/main/resources/application.properties @@ -0,0 +1,17 @@ +# 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. +server.port=8080 +server.contextPath=/shardingsphere-5.0.0-beta-scenario diff --git a/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/support-version.list b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/support-version.list new file mode 100644 index 0000000000..8da735b4fd --- /dev/null +++ b/test/plugin/scenarios/shardingsphere-5.0.0-beta-scenario/support-version.list @@ -0,0 +1,17 @@ +# 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. + +5.0.0-beta -- GitLab