You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/11/26 15:20:54 UTC

[shardingsphere] branch master updated: Completed switch primary data source IT case (#22448)

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 7bef6ce7b1a Completed switch primary data source IT case (#22448)
7bef6ce7b1a is described below

commit 7bef6ce7b1abcc80c3e5fea4415905d0db02e685
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Sat Nov 26 23:20:42 2022 +0800

    Completed switch primary data source IT case (#22448)
    
    * Add assert for discovery
    
    * Completed switch primary data source IT
    
    * Fix ci
---
 .../optimizer/SQLOptimizeEngineTest.java           |  4 +-
 .../discovery/build/DiscoveryRuleBuilder.java      |  8 +-
 .../cases/DatabaseClusterEnvironment.java          | 41 ++++++++++
 .../cases/DatabaseClusterEnvironmentFactory.java   | 48 ++++++++++++
 .../discovery/cases/base/BaseITCase.java           | 88 ++++++++++++++++++++++
 ...ava => MySQLMGRPrimaryDataSourceChangedIT.java} | 25 +++---
 .../mysql/env/MySQLMGREnvironment.java}            | 34 ++++-----
 .../resources/env/common/discovery-command.xml     |  9 ++-
 8 files changed, 220 insertions(+), 37 deletions(-)

diff --git a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/SQLOptimizeEngineTest.java b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/SQLOptimizeEngineTest.java
index 42a09b37bd5..16a11ceaa3b 100644
--- a/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/SQLOptimizeEngineTest.java
+++ b/kernel/sql-federation/optimizer/src/test/java/org/apache/shardingsphere/sqlfederation/optimizer/SQLOptimizeEngineTest.java
@@ -69,9 +69,9 @@ public final class SQLOptimizeEngineTest {
     
     private SQLOptimizeEngine optimizeEngine;
     
-    private String sql;
+    private final String sql;
     
-    private String expectedResult;
+    private final String expectedResult;
     
     public SQLOptimizeEngineTest(final TestCase testcase) {
         sql = testcase.getSql();
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/DiscoveryRuleBuilder.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/DiscoveryRuleBuilder.java
index a696398486b..454999f2cef 100644
--- a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/DiscoveryRuleBuilder.java
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/DiscoveryRuleBuilder.java
@@ -41,10 +41,14 @@ public final class DiscoveryRuleBuilder {
     public DiscoveryRuleBuilder(final DataSource proxyDataSource) throws SQLException {
         this.proxyDataSource = proxyDataSource;
         discoveryDistSQLCommand = JAXB.unmarshal(Objects.requireNonNull(BaseITCase.class.getClassLoader().getResource("env/common/discovery-command.xml")), DiscoveryDistSQLCommand.class);
-        buildDiscoveryEnvironment();
     }
     
-    private void buildDiscoveryEnvironment() throws SQLException {
+    /**
+     *  build Discovery Environment.
+     *
+     * @throws SQLException SQL exception
+     */
+    public void buildDiscoveryEnvironment() throws SQLException {
         try (
                 Connection connection = proxyDataSource.getConnection();
                 Statement statement = connection.createStatement()) {
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironment.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironment.java
new file mode 100644
index 00000000000..b01d1971b5a
--- /dev/null
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironment.java
@@ -0,0 +1,41 @@
+/*
+ * 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.integration.discovery.cases;
+
+import javax.sql.DataSource;
+import java.util.List;
+
+/**
+ * Database cluster environment.
+ */
+public interface DatabaseClusterEnvironment {
+    
+    /**
+     * Get primary data source.
+     *
+     * @return primary data source
+     */
+    DataSource getPrimaryDataSource();
+    
+    /**
+     * Get replication data sources.
+     *
+     * @return replication data sources
+     */
+    List<DataSource> getReplicationDataSources();
+}
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironmentFactory.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironmentFactory.java
new file mode 100644
index 00000000000..d02b7f350ea
--- /dev/null
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/DatabaseClusterEnvironmentFactory.java
@@ -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.
+ */
+
+package org.apache.shardingsphere.test.integration.discovery.cases;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.test.integration.discovery.cases.mysql.env.MySQLMGREnvironment;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.List;
+
+/**
+ * Database cluster environment factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class DatabaseClusterEnvironmentFactory {
+    
+    /**
+     * Create new instance of Database cluster environment.
+     *
+     * @param environment environment
+     * @param dataSources storage data sources
+     * @return Database cluster environment instance
+     * @throws SQLException SQL exception
+     */
+    public static DatabaseClusterEnvironment newInstance(final String environment, final List<DataSource> dataSources) throws SQLException {
+        if ("MySQL.MGR".equals(environment)) {
+            return new MySQLMGREnvironment(dataSources);
+        }
+        throw new UnsupportedOperationException("Unsupported database environment : " + environment);
+    }
+}
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/base/BaseITCase.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/base/BaseITCase.java
index 5809661b14e..1bbce5b49f6 100644
--- a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/base/BaseITCase.java
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/base/BaseITCase.java
@@ -17,17 +17,31 @@
 
 package org.apache.shardingsphere.test.integration.discovery.cases.base;
 
+import com.google.common.base.Preconditions;
 import lombok.AccessLevel;
 import lombok.Getter;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
+import org.apache.shardingsphere.data.pipeline.core.util.ThreadUtil;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
+import org.apache.shardingsphere.test.integration.discovery.build.DiscoveryRuleBuilder;
 import org.apache.shardingsphere.test.integration.discovery.env.IntegrationTestEnvironment;
 import org.apache.shardingsphere.test.integration.discovery.framework.container.compose.BaseContainerComposer;
 import org.apache.shardingsphere.test.integration.discovery.framework.container.compose.DockerContainerComposer;
 import org.apache.shardingsphere.test.integration.discovery.framework.parameter.DiscoveryParameterized;
 
 import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertNotEquals;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
 
 /**
  * Base integration test.
@@ -53,4 +67,78 @@ public abstract class BaseITCase {
         mappedDataSources = containerComposer.getMappedDatasource();
         proxyDataSource = containerComposer.getProxyDatasource();
     }
+    
+    /**
+     * Initialization discovery environment.
+     *
+     * @throws SQLException SQL exception
+     */
+    public void initDiscoveryEnvironment() throws SQLException {
+        new DiscoveryRuleBuilder(proxyDataSource).buildDiscoveryEnvironment();
+    }
+    
+    /**
+     * Get primary data source name.
+     *
+     * @return primary data source name
+     * @throws SQLException SQL exception
+     */
+    public String getPrimaryDataSourceName() throws SQLException {
+        try (
+                Connection connection = proxyDataSource.getConnection();
+                Statement statement = connection.createStatement()) {
+            String expectedPrimaryDataSourceName = getDiscoveryRulePrimaryDataSourceName(statement);
+            String actualPrimaryDataSourceName = getReadwriteSplittingRulePrimaryDataSourceName(statement);
+            assertPrimaryDataSource(actualPrimaryDataSourceName, expectedPrimaryDataSourceName);
+            return actualPrimaryDataSourceName;
+        }
+    }
+    
+    private String getReadwriteSplittingRulePrimaryDataSourceName(final Statement statement) throws SQLException {
+        try (ResultSet resultSet = statement.executeQuery("SHOW READWRITE_SPLITTING RULES")) {
+            return resultSet.next() ? resultSet.getString("write_storage_unit_name") : "";
+        }
+    }
+    
+    private String getDiscoveryRulePrimaryDataSourceName(final Statement statement) throws SQLException {
+        try (ResultSet resultSet = statement.executeQuery("SHOW DB_DISCOVERY RULES")) {
+            return resultSet.next() ? resultSet.getString("primary_data_source_name") : "";
+        }
+    }
+    
+    private void assertPrimaryDataSource(final String actualPrimaryDataSourceName, final String expectedPrimaryDataSourceName) {
+        Preconditions.checkState(StringUtils.isNotBlank(expectedPrimaryDataSourceName) && StringUtils.isNotBlank(actualPrimaryDataSourceName));
+        assertThat(actualPrimaryDataSourceName, is(expectedPrimaryDataSourceName));
+    }
+    
+    /**
+     * Close data sources.
+     *
+     * @param dataSources data sources
+     * @throws SQLException SQL exception
+     */
+    public void closeDataSources(final List<DataSource> dataSources) throws SQLException {
+        for (DataSource each : dataSources) {
+            close(each);
+        }
+        ThreadUtil.sleep(20, TimeUnit.SECONDS);
+    }
+    
+    private void close(final DataSource dataSource) throws SQLException {
+        try (
+                Connection connection = dataSource.getConnection();
+                Statement statement = connection.createStatement()) {
+            statement.execute("SHUTDOWN");
+        }
+    }
+    
+    /**
+     * Assert primary data source changed.
+     *
+     * @param oldPrimaryDataSourceName old primary data source name
+     * @param newPrimaryDataSourceName new primary data source name
+     */
+    public void assertPrimaryDataSourceChanged(final String oldPrimaryDataSourceName, final String newPrimaryDataSourceName) {
+        assertNotEquals(oldPrimaryDataSourceName, newPrimaryDataSourceName);
+    }
 }
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLDiscoveryGeneralIT.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLMGRPrimaryDataSourceChangedIT.java
similarity index 67%
rename from test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLDiscoveryGeneralIT.java
rename to test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLMGRPrimaryDataSourceChangedIT.java
index adea2fa4f03..dbe34be97aa 100644
--- a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLDiscoveryGeneralIT.java
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/MySQLMGRPrimaryDataSourceChangedIT.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.test.integration.discovery.cases.mysql;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
-import org.apache.shardingsphere.test.integration.discovery.build.DiscoveryRuleBuilder;
-import org.apache.shardingsphere.test.integration.discovery.build.MySQLMGRBuilder;
+import org.apache.shardingsphere.test.integration.discovery.cases.DatabaseClusterEnvironment;
+import org.apache.shardingsphere.test.integration.discovery.cases.DatabaseClusterEnvironmentFactory;
 import org.apache.shardingsphere.test.integration.discovery.cases.base.BaseITCase;
 import org.apache.shardingsphere.test.integration.discovery.framework.parameter.DiscoveryParameterized;
 import org.junit.Test;
@@ -30,20 +30,19 @@ import org.junit.runners.Parameterized.Parameters;
 
 import java.sql.SQLException;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.LinkedList;
 
+
 /**
- * MySQL Discovery Integration Test.
+ * MySQL MGR primary data source changed Integration Test.
  */
 @Slf4j
 @RunWith(Parameterized.class)
-public final class MySQLDiscoveryGeneralIT extends BaseITCase {
-    
-    private final DiscoveryParameterized discoveryParameterized;
+public final class MySQLMGRPrimaryDataSourceChangedIT extends BaseITCase {
     
-    public MySQLDiscoveryGeneralIT(final DiscoveryParameterized discoveryParameterized) {
+    public MySQLMGRPrimaryDataSourceChangedIT(final DiscoveryParameterized discoveryParameterized) {
         super(discoveryParameterized);
-        this.discoveryParameterized = discoveryParameterized;
     }
     
     @Parameters(name = "{0}")
@@ -57,9 +56,11 @@ public final class MySQLDiscoveryGeneralIT extends BaseITCase {
     }
     
     @Test
-    public void assertMySQLMGRDiscovery() throws SQLException {
-        new MySQLMGRBuilder(getMappedDataSources()).createDatabase();
-        new DiscoveryRuleBuilder(getProxyDataSource());
-        // TODO Add some assert
+    public void assertMySQLMGRPrimaryDataSourceChanged() throws SQLException {
+        DatabaseClusterEnvironment mgrEnvironment = DatabaseClusterEnvironmentFactory.newInstance("MySQL.MGR", getMappedDataSources());
+        initDiscoveryEnvironment();
+        String oldPrimaryDataSourceName = getPrimaryDataSourceName();
+        closeDataSources(Collections.singletonList(mgrEnvironment.getPrimaryDataSource()));
+        assertPrimaryDataSourceChanged(oldPrimaryDataSourceName, getPrimaryDataSourceName());
     }
 }
diff --git a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/MySQLMGRBuilder.java b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/env/MySQLMGREnvironment.java
similarity index 75%
rename from test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/MySQLMGRBuilder.java
rename to test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/env/MySQLMGREnvironment.java
index 723e684ac4d..99ab39eb758 100644
--- a/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/build/MySQLMGRBuilder.java
+++ b/test/integration-test/discovery/src/test/java/org/apache/shardingsphere/test/integration/discovery/cases/mysql/env/MySQLMGREnvironment.java
@@ -15,11 +15,12 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.test.integration.discovery.build;
+package org.apache.shardingsphere.test.integration.discovery.cases.mysql.env;
 
 import com.google.common.base.Splitter;
 import lombok.Getter;
 import org.apache.shardingsphere.test.integration.discovery.cases.base.BaseITCase;
+import org.apache.shardingsphere.test.integration.discovery.cases.DatabaseClusterEnvironment;
 import org.apache.shardingsphere.test.integration.discovery.command.MGRPrimaryReplicaCommand;
 
 import javax.sql.DataSource;
@@ -31,9 +32,9 @@ import java.util.List;
 import java.util.Objects;
 
 /**
- * Build MySQL MGR.
+ * Build MySQL MGR Environment.
  */
-public final class MySQLMGRBuilder {
+public final class MySQLMGREnvironment implements DatabaseClusterEnvironment {
     
     private final MGRPrimaryReplicaCommand mgrPrimaryReplicaCommand;
     
@@ -41,20 +42,21 @@ public final class MySQLMGRBuilder {
     private final DataSource primaryDataSource;
     
     @Getter
-    private final List<DataSource> replicaDataSources;
+    private final List<DataSource> replicationDataSources;
     
-    public MySQLMGRBuilder(final List<DataSource> dataSources) throws SQLException {
-        this.primaryDataSource = dataSources.get(0);
-        this.replicaDataSources = dataSources.subList(1, dataSources.size());
+    public MySQLMGREnvironment(final List<DataSource> dataSources) throws SQLException {
+        primaryDataSource = dataSources.get(0);
+        replicationDataSources = dataSources.subList(1, dataSources.size());
         mgrPrimaryReplicaCommand = JAXB.unmarshal(Objects.requireNonNull(BaseITCase.class.getClassLoader().getResource("env/common/mgr-primary-replica-command.xml")),
                 MGRPrimaryReplicaCommand.class);
-        buildMGRPrimaryDataSource(primaryDataSource);
-        buildMGRReplicaDataSources(dataSources.subList(1, dataSources.size()));
+        buildMGRPrimaryDataSource();
+        buildMGRReplicaDataSources();
+        createDatabase();
     }
     
-    private void buildMGRPrimaryDataSource(final DataSource dataSource) throws SQLException {
+    private void buildMGRPrimaryDataSource() throws SQLException {
         try (
-                Connection connection = dataSource.getConnection();
+                Connection connection = primaryDataSource.getConnection();
                 Statement statement = connection.createStatement()) {
             buildMGRPrimaryDataSource(statement);
         }
@@ -66,8 +68,8 @@ public final class MySQLMGRBuilder {
         }
     }
     
-    private void buildMGRReplicaDataSources(final List<DataSource> dataSources) throws SQLException {
-        for (DataSource each : dataSources) {
+    private void buildMGRReplicaDataSources() throws SQLException {
+        for (DataSource each : replicationDataSources) {
             buildMGRReplicaDataSource(each);
         }
     }
@@ -86,11 +88,7 @@ public final class MySQLMGRBuilder {
         }
     }
     
-    /**
-     * Create database.
-     * @throws SQLException SQL exception
-     */
-    public void createDatabase() throws SQLException {
+    private void createDatabase() throws SQLException {
         try (
                 Connection connection = primaryDataSource.getConnection();
                 Statement statement = connection.createStatement()) {
diff --git a/test/integration-test/discovery/src/test/resources/env/common/discovery-command.xml b/test/integration-test/discovery/src/test/resources/env/common/discovery-command.xml
index c8a213494da..90f9ed50849 100644
--- a/test/integration-test/discovery/src/test/resources/env/common/discovery-command.xml
+++ b/test/integration-test/discovery/src/test/resources/env/common/discovery-command.xml
@@ -22,19 +22,22 @@
         PORT=3306,
         DB="it_discovery_test",
         USER="test_user",
-        PASSWORD="Test@123"
+        PASSWORD="Test@123",
+        PROPERTIES("connectionTimeout"="8000")
         ),ds_1 (
         HOST="mysql_2",
         PORT=3306,
         DB="it_discovery_test",
         USER="test_user",
-        PASSWORD="Test@123"
+        PASSWORD="Test@123",
+        PROPERTIES("connectionTimeout"="8000")
         ),ds_2 (
         HOST="mysql_3",
         PORT=3306,
         DB="it_discovery_test",
         USER="test_user",
-        PASSWORD="Test@123"
+        PASSWORD="Test@123",
+        PROPERTIES("connectionTimeout"="8000")
         );
     </register-storage-unit>