This is an automated email from the ASF dual-hosted git repository. zhangliang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push: new 6a4e6d7 add agent it with proxy (#9108) 6a4e6d7 is described below commit 6a4e6d74eee604016c56226c4927e49358449e3b Author: xiaoyu <549477...@qq.com> AuthorDate: Thu Jan 21 19:45:21 2021 +0800 add agent it with proxy (#9108) * add it agent * remove empty line --- .github/workflows/it.yml | 21 +++ shardingsphere-test/pom.xml | 1 + .../pom.xml | 31 +++- .../Dockerfile | 27 ++++ .../pom.xml | 163 +++++++++++++++++++++ .../src/test/assembly/bin/start.sh | 71 +++++++++ .../src/test/assembly/bin/stop.sh | 51 +++++++ .../shardingsphere-proxy-agent-assembly.xml | 69 +++++++++ .../test/agent/plugins/BaseAgentIT.java | 94 ++++++++++++ .../test/agent/plugins/entity/OrderEntity.java | 33 +++++ .../agent/plugins/env/EnvironmentProperties.java | 51 +++++++ .../plugins/env/IntegrationTestEnvironment.java | 100 +++++++++++++ .../src/test/resources/docker/docker-compose.yml | 48 ++++++ .../test/resources/docker/proxy/conf/agent.yaml | 49 +++++++ .../resources/docker/proxy/conf/config-db.yaml | 74 ++++++++++ .../test/resources/docker/proxy/conf/logback.xml} | 33 ++--- .../test/resources/docker/proxy/conf/server.yaml | 32 ++++ .../src/test/resources/env/engine-env.properties | 18 +++ .../src/test/resources/env/mysql/init.sql | 34 +++++ 19 files changed, 977 insertions(+), 23 deletions(-) diff --git a/.github/workflows/it.yml b/.github/workflows/it.yml index 18b37e5..409457f 100644 --- a/.github/workflows/it.yml +++ b/.github/workflows/it.yml @@ -191,3 +191,24 @@ jobs: run: ./mvnw -B clean install -Dmaven.javadoc.skip=true -Drat.skip=true -Djacoco.skip=true -DskipITs - name: Run Integration Test run: ./mvnw -B clean install -f shardingsphere-test/shardingsphere-integration-test/pom.xml -Pit.env.docker -Dit.adapters=jdbc -Dit.scenarios=dbtbl_with_replica_query -Dit.databases=MySQL + + mysql-proxy-agent: + name: MySQL-Proxy with Agent + runs-on: ubuntu-latest + steps: + - name: Cache Maven Repos + uses: actions/cache@v2 + with: + path: ~/.m2/repository + key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }} + restore-keys: | + ${{ runner.os }}-maven- + - uses: actions/checkout@v2 + - name: Set up JDK 8 + uses: actions/setup-java@v1 + with: + java-version: 8 + - name: Build Project + run: ./mvnw -B clean install -Dmaven.javadoc.skip=true -Drat.skip=true -Djacoco.skip=true -DskipITs + - name: Run Integration Test + run: ./mvnw -B clean install -f shardingsphere-test/shardingsphere-integration-agent-test/pom.xml -Pit.env.agent diff --git a/shardingsphere-test/pom.xml b/shardingsphere-test/pom.xml index a77ef38..645418b 100644 --- a/shardingsphere-test/pom.xml +++ b/shardingsphere-test/pom.xml @@ -32,5 +32,6 @@ <modules> <module>shardingsphere-test-common</module> <module>shardingsphere-integration-test</module> + <module>shardingsphere-integration-agent-test</module> </modules> </project> diff --git a/shardingsphere-test/pom.xml b/shardingsphere-test/shardingsphere-integration-agent-test/pom.xml similarity index 61% copy from shardingsphere-test/pom.xml copy to shardingsphere-test/shardingsphere-integration-agent-test/pom.xml index a77ef38..a2bf93b 100644 --- a/shardingsphere-test/pom.xml +++ b/shardingsphere-test/shardingsphere-integration-agent-test/pom.xml @@ -21,16 +21,37 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> + <artifactId>shardingsphere-test</artifactId> <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere</artifactId> <version>5.0.0-RC1-SNAPSHOT</version> </parent> - <artifactId>shardingsphere-test</artifactId> + <artifactId>shardingsphere-integration-agent-test</artifactId> <packaging>pom</packaging> <name>${project.artifactId}</name> - + <modules> - <module>shardingsphere-test-common</module> - <module>shardingsphere-integration-test</module> + <module>shardingsphere-integration-agent-test-plugins</module> </modules> + + <properties> + <maven.deploy.skip>true</maven.deploy.skip> + </properties> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-failsafe-plugin</artifactId> + <executions> + <execution> + <id>integration-tests</id> + <goals> + <goal>integration-test</goal> + <goal>verify</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + </build> </project> diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/Dockerfile b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/Dockerfile new file mode 100644 index 0000000..d7a763a --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/Dockerfile @@ -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. +# + +FROM openjdk:8-jdk-alpine + +ARG APP_NAME +ENV WAIT_VERSION 2.7.2 + +ADD target/${APP_NAME}.tar.gz /opt +ADD https://github.com/ufoscout/docker-compose-wait/releases/download/$WAIT_VERSION/wait /wait +RUN chmod +x /wait +RUN mv /opt/${APP_NAME} /opt/shardingsphere-proxy +ENTRYPOINT /wait && /opt/shardingsphere-proxy/bin/start.sh && tail -f /opt/shardingsphere-proxy/logs/stdout.log diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/pom.xml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/pom.xml new file mode 100644 index 0000000..95dd367 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/pom.xml @@ -0,0 +1,163 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ~ 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. + --> + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + <parent> + <groupId>org.apache.shardingsphere</groupId> + <artifactId>shardingsphere-integration-agent-test</artifactId> + <version>5.0.0-RC1-SNAPSHOT</version> + </parent> + <artifactId>shardingsphere-integration-agent-test-plugins</artifactId> + <name>${project.artifactId}</name> + + <dependencies> + <dependency> + <groupId>org.apache.shardingsphere</groupId> + <artifactId>shardingsphere-jdbc-core</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>org.apache.shardingsphere</groupId> + <artifactId>shardingsphere-proxy-bootstrap</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>mysql</groupId> + <artifactId>mysql-connector-java</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>ch.qos.logback</groupId> + <artifactId>logback-classic</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-dbcp2</artifactId> + <scope>test</scope> + </dependency> + </dependencies> + + <profiles> + <profile> + <id>it.env.agent</id> + <properties> + <it.env>agent</it.env> + </properties> + <build> + <plugins> + <plugin> + <artifactId>maven-assembly-plugin</artifactId> + <configuration> + <appendAssemblyId>false</appendAssemblyId> + <descriptors> + <descriptor>src/test/assembly/shardingsphere-proxy-agent-assembly.xml</descriptor> + </descriptors> + </configuration> + <executions> + <execution> + <id>assembly</id> + <phase>package</phase> + <goals> + <goal>single</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <groupId>com.spotify</groupId> + <artifactId>dockerfile-maven-plugin</artifactId> + <executions> + <execution> + <id>shardingsphere-proxy-bin</id> + <goals> + <goal>build</goal> + </goals> + </execution> + </executions> + <configuration> + <repository>apache/shardingsphere-proxy-agent-test</repository> + <tag>${project.version}</tag> + <tag>latest</tag> + <buildArgs> + <APP_NAME>${project.build.finalName}</APP_NAME> + </buildArgs> + </configuration> + </plugin> + <plugin> + <groupId>com.dkanejs.maven.plugins</groupId> + <artifactId>docker-compose-maven-plugin</artifactId> + <executions> + <execution> + <id>agent-up</id> + <phase>pre-integration-test</phase> + <goals> + <goal>up</goal> + </goals> + <configuration> + <composeFile>${project.basedir}/src/test/resources/docker/docker-compose.yml</composeFile> + <detachedMode>true</detachedMode> + </configuration> + </execution> + <execution> + <id>agent-down</id> + <phase>post-integration-test</phase> + <goals> + <goal>down</goal> + </goals> + <configuration> + <composeFile>${project.basedir}/src/test/resources/docker/docker-compose.yml</composeFile> + <removeVolumes>true</removeVolumes> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> + </profile> + </profiles> + + <build> + <plugins> + <plugin> + <artifactId>maven-resources-plugin</artifactId> + <executions> + <execution> + <id>copy-resources</id> + <phase>validate</phase> + <goals> + <goal>copy-resources</goal> + </goals> + <configuration> + <outputDirectory>target/test-classes</outputDirectory> + <resources> + <resource> + <directory>src/test/resources</directory> + <filtering>true</filtering> + </resource> + </resources> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> +</project> diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/start.sh b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/start.sh new file mode 100644 index 0000000..4c292ae --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/start.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# +# 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_NAME=ShardingSphere-Proxy + +cd `dirname $0` +cd .. +DEPLOY_DIR=`pwd` + +LOGS_DIR=${DEPLOY_DIR}/logs +if [ ! -d ${LOGS_DIR} ]; then + mkdir ${LOGS_DIR} +fi + +STDOUT_FILE=${LOGS_DIR}/stdout.log +EXT_LIB=${DEPLOY_DIR}/ext-lib + +CLASS_PATH=.:${DEPLOY_DIR}/lib/*:${EXT_LIB}/* + +JAVA_OPTS=" -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=3308" + +JAVA_MEM_OPTS=" -server -Xmx2g -Xms2g -Xmn1g -Xss256k -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70 " + +MAIN_CLASS=org.apache.shardingsphere.proxy.Bootstrap + +print_usage() { + echo "usage: start.sh [port] [config_dir]" + echo " port: proxy listen port, default is 3307" + echo " config_dir: proxy config directory, default is conf" + exit 0 +} + +if [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then + print_usage +fi + +echo "Starting the $SERVER_NAME ..." + +if [ $# == 1 ]; then + MAIN_CLASS=${MAIN_CLASS}" "$1 + echo "The port is $1" + CLASS_PATH=../conf:${CLASS_PATH} +fi + +if [ $# == 2 ]; then + MAIN_CLASS=${MAIN_CLASS}" "$1" "$2 + echo "The port is $1" + echo "The configuration path is $DEPLOY_DIR/$2" + CLASS_PATH=${DEPLOY_DIR}/$2:${CLASS_PATH} +fi + +echo "The classpath is ${CLASS_PATH}" + +nohup java ${JAVA_OPTS} ${JAVA_MEM_OPTS} -javaagent:/opt/shardingsphere-proxy/shardingsphere-agent.jar -classpath ${CLASS_PATH} ${MAIN_CLASS} >> ${STDOUT_FILE} 2>&1 & +sleep 1 +echo "Please check the STDOUT file: $STDOUT_FILE" diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/stop.sh b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/stop.sh new file mode 100644 index 0000000..da8909e --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/bin/stop.sh @@ -0,0 +1,51 @@ +#!/bin/sh +# +# 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_NAME=ShardingSphere-Proxy + +cd `dirname $0` +cd .. +DEPLOY_DIR=`pwd` + +PIDS=`ps -ef | grep java | grep "$DEPLOY_DIR" | grep -v grep |awk '{print $2}'` +if [ -z "$PIDS" ]; then + echo "ERROR: The $SERVER_NAME does not started!" + exit 1 +fi + +echo -e "Stopping the $SERVER_NAME ...\c" +for PID in ${PIDS} ; do + kill ${PID} > /dev/null 2>&1 +done + +COUNT=0 +while [ ${COUNT} -lt 1 ]; do + echo -e ".\c" + sleep 1 + COUNT=1 + for PID in ${PIDS} ; do + PID_EXIST=`ps -f -p ${PID} | grep java` + if [ -n "$PID_EXIST" ]; then + COUNT=0 + break + fi + done +done + +echo "OK!" +echo "PID: $PIDS" diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/shardingsphere-proxy-agent-assembly.xml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/shardingsphere-proxy-agent-assembly.xml new file mode 100644 index 0000000..b3ed9b7 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/assembly/shardingsphere-proxy-agent-assembly.xml @@ -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. + --> + +<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> + <id>shardingsphere-proxy-bin</id> + <formats> + <format>tar.gz</format> + </formats> + <includeBaseDirectory>true</includeBaseDirectory> + + <fileSets> + <fileSet> + <directory>${project.basedir}/../../../shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-logging/target/plugins</directory> + <outputDirectory>./plugins</outputDirectory> + <includes> + <include>**.jar</include> + </includes> + </fileSet> + + <fileSet> + <directory>${project.basedir}/../../../shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-tracing/target/plugins</directory> + <outputDirectory>./plugins</outputDirectory> + <includes> + <include>**.jar</include> + </includes> + </fileSet> + <fileSet> + <directory>${project.basedir}/../../../shardingsphere-agent/shardingsphere-agent-plugins/shardingsphere-agent-plugin-metrics/target/plugins</directory> + <outputDirectory>./plugins</outputDirectory> + <includes> + <include>**.jar</include> + </includes> + </fileSet> + <fileSet> + <directory>${project.basedir}/../../../shardingsphere-agent/shardingsphere-agent-bootstrap/target</directory> + <outputDirectory>./</outputDirectory> + <includes> + <include>shardingsphere-agent.jar</include> + </includes> + </fileSet> + <fileSet> + <directory>src/test/assembly/bin</directory> + <outputDirectory>bin</outputDirectory> + <fileMode>0755</fileMode> + </fileSet> + </fileSets> + + <dependencySets> + <dependencySet> + <outputDirectory>lib</outputDirectory> + <fileMode>0644</fileMode> + </dependencySet> + </dependencySets> +</assembly> diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/BaseAgentIT.java b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/BaseAgentIT.java new file mode 100644 index 0000000..d88baa9 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/BaseAgentIT.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.shardingsphere.test.agent.plugins; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import javax.sql.DataSource; +import org.apache.shardingsphere.test.agent.plugins.entity.OrderEntity; +import org.apache.shardingsphere.test.agent.plugins.env.IntegrationTestEnvironment; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +public final class BaseAgentIT { + + @Test + public void assertProxyWithAgent() { + if (IntegrationTestEnvironment.getInstance().isEnvironmentPrepared()) { + DataSource dataSource = IntegrationTestEnvironment.getInstance().getDataSource(); + List<Long> results = new ArrayList<>(10); + for (int i = 1; i <= 10; i++) { + OrderEntity orderEntity = new OrderEntity(i, i, "INSERT_TEST"); + insert(orderEntity, dataSource); + results.add(orderEntity.getOrderId()); + } + Collection<OrderEntity> orderEntities = selectAll(dataSource); + assertThat(orderEntities.size(), is(10)); + for (Long each : results) { + delete(each, dataSource); + } + } + } + + private void insert(final OrderEntity orderEntity, final DataSource dataSource) { + String sql = "INSERT INTO t_order (order_id,user_id, status) VALUES (?, ?,?)"; + try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { + preparedStatement.setLong(1, orderEntity.getOrderId()); + preparedStatement.setInt(2, orderEntity.getUserId()); + preparedStatement.setString(3, orderEntity.getStatus()); + preparedStatement.executeUpdate(); + } catch (final SQLException ignored) { + } + } + + private void delete(final Long orderId, final DataSource dataSource) { + String sql = "DELETE FROM t_order WHERE order_id=?"; + try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + preparedStatement.setLong(1, orderId); + preparedStatement.executeUpdate(); + } catch (final SQLException ignored) { + } + } + + private Collection<OrderEntity> selectAll(final DataSource dataSource) { + String sql = "SELECT * FROM t_order"; + return getOrders(sql, dataSource); + } + + private Collection<OrderEntity> getOrders(final String sql, final DataSource dataSource) { + Collection<OrderEntity> result = new LinkedList<>(); + try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + ResultSet resultSet = preparedStatement.executeQuery(); + while (resultSet.next()) { + OrderEntity orderEntity = new OrderEntity(resultSet.getLong(1), resultSet.getInt(2), resultSet.getString(3)); + result.add(orderEntity); + } + } catch (final SQLException ignored) { + } + return result; + } +} diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/entity/OrderEntity.java b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/entity/OrderEntity.java new file mode 100644 index 0000000..43eb539 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/entity/OrderEntity.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.shardingsphere.test.agent.plugins.entity; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Getter +public final class OrderEntity { + + private final long orderId; + + private final int userId; + + private final String status; +} diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/EnvironmentProperties.java b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/EnvironmentProperties.java new file mode 100644 index 0000000..8ecac54 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/EnvironmentProperties.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.agent.plugins.env; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +/** + * Environment properties. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class EnvironmentProperties { + + /** + * Load properties. + * + * @param propsFileName properties file name + * @return properties + */ + @SuppressWarnings("AccessOfSystemProperties") + public static Properties loadProperties(final String propsFileName) { + Properties result = new Properties(); + try (InputStream inputStream = EnvironmentProperties.class.getClassLoader().getResourceAsStream(propsFileName)) { + result.load(inputStream); + } catch (final IOException ex) { + throw new RuntimeException(ex); + } + for (String each : System.getProperties().stringPropertyNames()) { + result.setProperty(each, System.getProperty(each)); + } + return result; + } +} diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/IntegrationTestEnvironment.java b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/IntegrationTestEnvironment.java new file mode 100644 index 0000000..975295a --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/java/org/apache/shardingsphere/test/agent/plugins/env/IntegrationTestEnvironment.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.test.agent.plugins.env; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; +import javax.sql.DataSource; +import lombok.Getter; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; + +@Getter +@Slf4j +public final class IntegrationTestEnvironment { + + private static final IntegrationTestEnvironment INSTANCE = new IntegrationTestEnvironment(); + + private static final String URL = "jdbc:mysql://127.0.0.1:43070?serverTimezone=UTC&useSSL=false&useLocalSessionState=true&characterEncoding=utf-8"; + + private static final String USER_NAME = "root"; + + private static final String PASSWORD = "root"; + + private final boolean isEnvironmentPrepared; + + private DataSource dataSource; + + @SneakyThrows + private IntegrationTestEnvironment() { + Properties engineEnvProps = EnvironmentProperties.loadProperties("env/engine-env.properties"); + isEnvironmentPrepared = "agent".equals(engineEnvProps.getProperty("it.env.type")); + if (isEnvironmentPrepared) { + waitForEnvironmentReady(); + dataSource = createHikariCP(); + } + } + + private static DataSource createHikariCP() { + HikariConfig result = new HikariConfig(); + result.setDriverClassName("com.mysql.jdbc.Driver"); + result.setJdbcUrl(URL); + result.setUsername(USER_NAME); + result.setPassword(PASSWORD); + result.setMaximumPoolSize(5); + result.setTransactionIsolation("TRANSACTION_READ_COMMITTED"); + return new HikariDataSource(result); + } + + private void waitForEnvironmentReady() { + log.info("wait begin proxy environment"); + int retryCount = 0; + while (!isProxyReady() && retryCount < 30) { + try { + Thread.sleep(1000L); + } catch (final InterruptedException ignore) { + } + retryCount++; + } + } + + private boolean isProxyReady() { + try (Connection connection = DriverManager.getConnection(URL, USER_NAME, PASSWORD); + Statement statement = connection.createStatement()) { + statement.execute("SELECT 1"); + } catch (final SQLException ignore) { + return false; + } + log.info(" it proxy environment success"); + return true; + } + + /** + * Get instance. + * + * @return singleton instance + */ + public static IntegrationTestEnvironment getInstance() { + return INSTANCE; + } +} diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/docker-compose.yml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/docker-compose.yml new file mode 100644 index 0000000..f3f7ba9 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/docker-compose.yml @@ -0,0 +1,48 @@ +# +# 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. +# + +version: "2.1" + +services: + mysql: + image: "mysql/mysql-server:5.7" + container_name: agent-mysql + command: ['--sql_mode=', '--default-authentication-plugin=mysql_native_password'] + volumes: + - ../env/mysql:/docker-entrypoint-initdb.d/ + ports: + - "43060:3306" + environment: + - LANG=C.UTF-8 + + shardingsphere-proxy: + image: apache/shardingsphere-proxy-agent-test + container_name: shardingsphere-proxy-agent + ports: + - "43070:3307" + - "43080:3308" + links: + - "mysql:mysql.agent.host" + volumes: + - ./proxy/conf:/opt/shardingsphere-proxy/conf + depends_on: + - mysql + environment: + - WAIT_HOSTS=mysql:3306 + - WAIT_HOSTS_TIMEOUT=300 + - WAIT_SLEEP_INTERVAL=5 + - WAIT_HOST_CONNECT_TIMEOUT=30 diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/agent.yaml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/agent.yaml new file mode 100644 index 0000000..67f662a --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/agent.yaml @@ -0,0 +1,49 @@ +# +# 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. +# + +applicationName: shardingsphere-agent +ignoredPluginNames: + - Jaeger + - Opentracing + +plugins: + prometheus: + host: "localhost" + port: 9090 + props: + JVM_INFORMATION_COLLECTOR_ENABLED : "true" + Jaeger: + host: "localhost" + port: 5775 + props: + SERVICE_NAME: "shardingsphere-agent" + JAEGER_SAMPLER_TYPE: "const" + JAEGER_SAMPLER_PARAM: "1" + JAEGER_REPORTER_LOG_SPANS: "true" + JAEGER_REPORTER_FLUSH_INTERVAL: "1" + Zipkin: + host: "localhost" + port: 9411 + props: + SERVICE_NAME: "shardingsphere-agent" + URL_VERSION: "/api/v2/spans" + Opentracing: + props: + OPENTRACING_TRACER_CLASS_NAME: "org.apache.skywalking.apm.toolkit.opentracing.SkywalkingTracer" + Logging: + props: + LEVEL: "INFO" diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/config-db.yaml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/config-db.yaml new file mode 100644 index 0000000..044f9c1 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/config-db.yaml @@ -0,0 +1,74 @@ +# +# 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. +# + +schemaName: agent-db + +dataSources: + ds_0: + url: jdbc:mysql://mysql.agent.host:3306/agent_db_0?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8 + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 10 + minPoolSize: 2 + maintenanceIntervalMilliseconds: 30000 + ds_1: + url: jdbc:mysql://mysql.agent.host:3306/agent_db_1?serverTimezone=UTC&useSSL=false&characterEncoding=utf-8 + username: root + password: + connectionTimeoutMilliseconds: 30000 + idleTimeoutMilliseconds: 60000 + maxLifetimeMilliseconds: 1800000 + maxPoolSize: 10 + minPoolSize: 2 + maintenanceIntervalMilliseconds: 30000 + +rules: + - !SHARDING + tables: + t_order: + actualDataNodes: ds_${0..1}.t_order_${0..1} + tableStrategy: + standard: + shardingColumn: order_id + shardingAlgorithmName: t_order_inline + keyGenerateStrategy: + column: order_id + keyGeneratorName: snowflake + defaultDatabaseStrategy: + standard: + shardingColumn: user_id + shardingAlgorithmName: database_inline + defaultTableStrategy: + none: + shardingAlgorithms: + database_inline: + type: INLINE + props: + algorithm-expression: ds_${user_id % 2} + t_order_inline: + type: INLINE + props: + algorithm-expression: t_order_${order_id % 2} + + keyGenerators: + snowflake: + type: SNOWFLAKE + props: + worker-id: 123 diff --git a/shardingsphere-test/pom.xml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/logback.xml similarity index 52% copy from shardingsphere-test/pom.xml copy to shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/logback.xml index a77ef38..1494746 100644 --- a/shardingsphere-test/pom.xml +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/logback.xml @@ -1,4 +1,4 @@ -<?xml version="1.0" encoding="UTF-8"?> +<?xml version="1.0"?> <!-- ~ Licensed to the Apache Software Foundation (ASF) under one or more ~ contributor license agreements. See the NOTICE file distributed with @@ -16,21 +16,18 @@ ~ limitations under the License. --> -<project xmlns="http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.apache.shardingsphere</groupId> - <artifactId>shardingsphere</artifactId> - <version>5.0.0-RC1-SNAPSHOT</version> - </parent> - <artifactId>shardingsphere-test</artifactId> - <packaging>pom</packaging> - <name>${project.artifactId}</name> +<configuration> + <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %logger{36} - %msg%n</pattern> + </encoder> + </appender> + <logger name="org.apache.shardingsphere" level="info" additivity="false"> + <appender-ref ref="console" /> + </logger> - <modules> - <module>shardingsphere-test-common</module> - <module>shardingsphere-integration-test</module> - </modules> -</project> + <root> + <level value="info" /> + <appender-ref ref="console" /> + </root> +</configuration> diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/server.yaml b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/server.yaml new file mode 100644 index 0000000..c8db741 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/docker/proxy/conf/server.yaml @@ -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. +# + +authentication: + users: + root: + password: root + +props: + max-connections-size-per-query: 1 + acceptor-size: 16 # The default value is available processors count * 2. + executor-size: 16 # Infinite by default. + proxy-frontend-flush-threshold: 128 # The default value is 128. + proxy-transaction-type: LOCAL + proxy-opentracing-enabled: false + proxy-hint-enabled: false + query-with-cipher-column: true + sql-show: true diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/engine-env.properties b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/engine-env.properties new file mode 100644 index 0000000..e9e7be6 --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/engine-env.properties @@ -0,0 +1,18 @@ +# +# 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. +# + +it.env.type=${it.env} diff --git a/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/mysql/init.sql b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/mysql/init.sql new file mode 100644 index 0000000..d82e10e --- /dev/null +++ b/shardingsphere-test/shardingsphere-integration-agent-test/shardingsphere-integration-agent-test-plugins/src/test/resources/env/mysql/init.sql @@ -0,0 +1,34 @@ +-- +-- 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. +-- + +CREATE USER IF NOT EXISTS 'root'@'%' IDENTIFIED BY ''; +GRANT All privileges ON *.* TO 'root'@'%'; + +SET character_set_database='utf8'; +SET character_set_server='utf8'; + +DROP DATABASE IF EXISTS agent_db_0; +DROP DATABASE IF EXISTS agent_db_1; + +CREATE DATABASE agent_db_0; +CREATE DATABASE agent_db_1; + +CREATE TABLE agent_db_0.t_order_0 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)); +CREATE TABLE agent_db_0.t_order_1 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)); + +CREATE TABLE agent_db_1.t_order_0 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id)); +CREATE TABLE agent_db_1.t_order_1 (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id));