You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/04/22 17:37:51 UTC
[shardingsphere] branch master updated: Add HighlyAvailableStatus (#17022)
This is an automated email from the ASF dual-hosted git repository.
totalo 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 e2986888ee8 Add HighlyAvailableStatus (#17022)
e2986888ee8 is described below
commit e2986888ee8fef8f2e935ed5fd351322d96bee64
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Apr 23 01:37:35 2022 +0800
Add HighlyAvailableStatus (#17022)
* Refactor DatabaseDiscoveryType
* Add HighlyAvailableStatus
* Add HighlyAvailableStatus
* Fix checkstyle
* Fix test case
---
.../dbdiscovery/spi/DatabaseDiscoveryType.java | 10 +--
.../dbdiscovery/spi/HighlyAvailableStatus.java | 39 +++++++++
.../algorithm/DatabaseDiscoveryEngine.java | 16 +++-
.../dbdiscovery/rule/DatabaseDiscoveryRule.java | 2 +-
.../fixture/CoreFixtureDatabaseDiscoveryType.java | 6 +-
.../DistSQLFixtureDatabaseDiscoveryType.java | 6 +-
.../mysql/AbstractDatabaseDiscoveryType.java | 2 +-
.../mysql/type/MGRDatabaseDiscoveryType.java | 94 ++++++----------------
.../mysql/type/MGRHighlyAvailableStatus.java | 73 +++++++++++++++++
.../type/ShowSlaveStatusDatabaseDiscoveryType.java | 24 +-----
.../type/ShowSlaveStatusHighlyAvailableStatus.java | 44 ++++++++++
.../mysql/type/MGRDatabaseDiscoveryTypeTest.java | 64 ++++++---------
.../ShowSlaveStatusDatabaseDiscoveryTypeTest.java | 16 ++--
.../opengauss/OpenGaussDatabaseDiscoveryType.java | 7 +-
.../opengauss/OpenGaussHighlyAvailableStatus.java | 36 +++++++++
.../OpenGaussDatabaseDiscoveryTypeTest.java | 29 ++-----
16 files changed, 292 insertions(+), 176 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
index b704cc285cc..3f0cb68dcbd 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryType.java
@@ -30,13 +30,13 @@ import java.util.Optional;
public interface DatabaseDiscoveryType extends ShardingSphereAlgorithm {
/**
- * Check database discovery configuration.
- *
- * @param databaseName database name
- * @param dataSourceMap data source map
+ * Load highly available status.
+ *
+ * @param dataSource data source
+ * @return loaded highly available status
* @throws SQLException SQL exception
*/
- void checkDatabaseDiscoveryConfiguration(String databaseName, Map<String, DataSource> dataSourceMap) throws SQLException;
+ HighlyAvailableStatus loadHighlyAvailableStatus(DataSource dataSource) throws SQLException;
/**
* Determine primary data source.
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/HighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/HighlyAvailableStatus.java
new file mode 100644
index 00000000000..58755a0f4df
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/HighlyAvailableStatus.java
@@ -0,0 +1,39 @@
+/*
+ * 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.dbdiscovery.spi;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Highly available status for database cluster.
+ */
+public interface HighlyAvailableStatus {
+
+ /**
+ * Validate highly available status.
+ *
+ * @param databaseName database name
+ * @param dataSourceMap data source map
+ * @param props properties of database discovery type
+ * @throws SQLException SQL exception
+ */
+ void validate(String databaseName, Map<String, DataSource> dataSourceMap, Properties props) throws SQLException;
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
index d9996417e3d..a1904fe4d86 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/algorithm/DatabaseDiscoveryEngine.java
@@ -17,8 +17,10 @@
package org.apache.shardingsphere.dbdiscovery.algorithm;
+import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.QualifiedDatabase;
import org.apache.shardingsphere.infra.rule.event.impl.PrimaryDataSourceChangedEvent;
@@ -27,7 +29,9 @@ import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
/**
@@ -39,14 +43,20 @@ public final class DatabaseDiscoveryEngine {
private final DatabaseDiscoveryType databaseDiscoveryType;
/**
- * Check database discovery configuration.
+ * Check highly available status of database cluster.
*
* @param databaseName database name
* @param dataSourceMap data source map
* @throws SQLException SQL exception
*/
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) throws SQLException {
- databaseDiscoveryType.checkDatabaseDiscoveryConfiguration(databaseName, dataSourceMap);
+ public void checkHighlyAvailableStatus(final String databaseName, final Map<String, DataSource> dataSourceMap) throws SQLException {
+ Collection<HighlyAvailableStatus> statuses = new HashSet<>();
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ // TODO query with multiple threads
+ statuses.add(databaseDiscoveryType.loadHighlyAvailableStatus(entry.getValue()));
+ }
+ Preconditions.checkState(1 == statuses.size(), "Different status in highly available cluster in database `%s`.", databaseName);
+ statuses.iterator().next().validate(databaseName, dataSourceMap, databaseDiscoveryType.getProps());
}
/**
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
index 56ab36c2062..3539328f05b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/main/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRule.java
@@ -112,7 +112,7 @@ public final class DatabaseDiscoveryRule implements SchemaRule, DataSourceContai
Map<String, DataSource> originalDataSourceMap = new HashMap<>(dataSourceMap);
Collection<String> disabledDataSourceNames = dataSourceRule.getDisabledDataSourceNames();
try {
- engine.checkDatabaseDiscoveryConfiguration(databaseName, originalDataSourceMap);
+ engine.checkHighlyAvailableStatus(databaseName, originalDataSourceMap);
} catch (final SQLException ex) {
throw new ShardingSphereException(ex);
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
index d1367c73fc0..20ee5ae091c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryType.java
@@ -18,15 +18,19 @@
package org.apache.shardingsphere.dbdiscovery.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
+import static org.mockito.Mockito.mock;
+
public final class CoreFixtureDatabaseDiscoveryType implements DatabaseDiscoveryType {
@Override
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) {
+ public HighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) {
+ return mock(HighlyAvailableStatus.class);
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DistSQLFixtureDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DistSQLFixtureDatabas [...]
index 947baace8f9..fe9a00ce9ae 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DistSQLFixtureDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/fixture/DistSQLFixtureDatabaseDiscoveryType.java
@@ -18,15 +18,19 @@
package org.apache.shardingsphere.dbdiscovery.distsql.handler.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryType;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
import javax.sql.DataSource;
import java.util.Map;
import java.util.Optional;
+import static org.mockito.Mockito.mock;
+
public final class DistSQLFixtureDatabaseDiscoveryType implements DatabaseDiscoveryType {
@Override
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) {
+ public HighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) {
+ return mock(HighlyAvailableStatus.class);
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractDatabaseDiscoveryType.java
index c827cdcb54f..6a546b3f697 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractDatabaseDiscoveryType.java
@@ -62,7 +62,7 @@ public abstract class AbstractDatabaseDiscoveryType implements DatabaseDiscovery
protected abstract Optional<String> loadPrimaryDataSourceURL(Statement statement) throws SQLException;
- private Optional<String> findPrimaryDataSourceName(final String primaryDataSourceURL, final Map<String, DataSource> dataSourceMap) {
+ private Optional<String> findPrimaryDataSourceName(final String primaryDataSourceURL, final Map<String, DataSource> dataSourceMap) {
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
try (Connection connection = entry.getValue().getConnection()) {
String url = connection.getMetaData().getURL();
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryType.java
index f950d08f8b0..31e9814e76c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryType.java
@@ -21,7 +21,6 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.mysql.AbstractDatabaseDiscoveryType;
-import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
@@ -47,17 +46,15 @@ import java.util.Properties;
@Slf4j
public final class MGRDatabaseDiscoveryType extends AbstractDatabaseDiscoveryType {
- private static final String PLUGIN_STATUS = "SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
+ private static final String QUERY_PLUGIN_STATUS = "SELECT PLUGIN_STATUS FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
- private static final String MEMBER_COUNT = "SELECT count(*) FROM performance_schema.replication_group_members";
+ private static final String QUERY_GROUP_NAME = "SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_group_name'";
- private static final String GROUP_NAME = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_group_name'";
+ private static final String QUERY_SINGLE_PRIMARY_MODE = "SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_single_primary_mode'";
- private static final String SINGLE_PRIMARY = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_single_primary_mode'";
+ private static final String QUERY_MEMBER_LIST = "SELECT MEMBER_HOST, MEMBER_PORT, MEMBER_STATE FROM performance_schema.replication_group_members";
- private static final String MEMBER_LIST = "SELECT MEMBER_HOST, MEMBER_PORT, MEMBER_STATE FROM performance_schema.replication_group_members";
-
- private static final String PRIMARY_DATA_SOURCE = "SELECT MEMBER_HOST, MEMBER_PORT FROM performance_schema.replication_group_members WHERE MEMBER_ID = "
+ private static final String QUERY_PRIMARY_DATA_SOURCE = "SELECT MEMBER_HOST, MEMBER_PORT FROM performance_schema.replication_group_members WHERE MEMBER_ID = "
+ "(SELECT VARIABLE_VALUE FROM performance_schema.global_status WHERE VARIABLE_NAME = 'group_replication_primary_member')";
@Getter
@@ -65,87 +62,44 @@ public final class MGRDatabaseDiscoveryType extends AbstractDatabaseDiscoveryTyp
private Properties props = new Properties();
@Override
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) throws SQLException {
- try (Connection connection = dataSourceMap.values().iterator().next().getConnection();
+ public MGRHighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) throws SQLException {
+ try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
- checkPluginIsActive(statement);
- checkMemberCount(statement);
- checkServerGroupName(statement);
- checkIsSinglePrimaryMode(statement);
- checkDataSourceInReplicationGroup(statement, dataSourceMap);
+ return new MGRHighlyAvailableStatus(queryIsPluginActive(statement), queryIsSinglePrimaryMode(statement), queryGroupName(statement), queryMemberInstanceURLs(statement));
}
}
- private void checkPluginIsActive(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(PLUGIN_STATUS)) {
- while (resultSet.next()) {
- if (!"ACTIVE".equals(resultSet.getString("PLUGIN_STATUS"))) {
- throw new ShardingSphereConfigurationException("MGR plugin is not active.");
- }
- }
+ private boolean queryIsPluginActive(final Statement statement) throws SQLException {
+ try (ResultSet resultSet = statement.executeQuery(QUERY_PLUGIN_STATUS)) {
+ return resultSet.next() && "ACTIVE".equals(resultSet.getString("PLUGIN_STATUS"));
}
}
- private void checkMemberCount(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(MEMBER_COUNT)) {
- while (resultSet.next()) {
- if (resultSet.getInt(1) < 1) {
- throw new ShardingSphereConfigurationException("MGR member count < 1");
- }
- }
+ private boolean queryIsSinglePrimaryMode(final Statement statement) throws SQLException {
+ try (ResultSet resultSet = statement.executeQuery(QUERY_SINGLE_PRIMARY_MODE)) {
+ return resultSet.next() && "ON".equals(resultSet.getString("VARIABLE_VALUE"));
}
}
- private void checkServerGroupName(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(GROUP_NAME)) {
- while (resultSet.next()) {
- String serverGroupName = resultSet.getString("VARIABLE_VALUE");
- String ruleGroupName = props.getProperty("group-name");
- if (!serverGroupName.equals(ruleGroupName)) {
- throw new ShardingSphereConfigurationException("MGR group name is not consistent\n" + "serverGroupName: %s\nruleGroupName: %s", serverGroupName, ruleGroupName);
- }
- }
+ private String queryGroupName(final Statement statement) throws SQLException {
+ try (ResultSet resultSet = statement.executeQuery(QUERY_GROUP_NAME)) {
+ return resultSet.next() ? resultSet.getString("VARIABLE_VALUE") : "";
}
}
- private void checkIsSinglePrimaryMode(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(SINGLE_PRIMARY)) {
+ private Collection<String> queryMemberInstanceURLs(final Statement statement) throws SQLException {
+ Collection<String> result = new LinkedList<>();
+ try (ResultSet resultSet = statement.executeQuery(QUERY_MEMBER_LIST)) {
while (resultSet.next()) {
- if (!"ON".equals(resultSet.getString("VARIABLE_VALUE"))) {
- throw new ShardingSphereConfigurationException("MGR is not in single primary mode");
- }
- }
- }
- }
-
- private void checkDataSourceInReplicationGroup(final Statement statement, final Map<String, DataSource> dataSourceMap) throws SQLException {
- Collection<String> memberDataSourceURLs = new LinkedList<>();
- try (ResultSet resultSet = statement.executeQuery(MEMBER_LIST)) {
- while (resultSet.next()) {
- memberDataSourceURLs.add(String.format("%s:%s", resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT")));
- }
- }
- for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
- checkDataSourceExistedWithGroupMember(entry.getKey(), entry.getValue(), memberDataSourceURLs);
- }
- }
-
- private void checkDataSourceExistedWithGroupMember(final String datasourceName, final DataSource dataSource, final Collection<String> memberDataSourceURLs) throws SQLException {
- boolean isExisted = false;
- for (String each : memberDataSourceURLs) {
- if (dataSource.getConnection().getMetaData().getURL().contains(each)) {
- isExisted = true;
- break;
+ result.add(String.format("%s:%s", resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT")));
}
}
- if (!isExisted) {
- throw new ShardingSphereConfigurationException("%s is not MGR replication group member", datasourceName);
- }
+ return result;
}
@Override
protected Optional<String> loadPrimaryDataSourceURL(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(PRIMARY_DATA_SOURCE)) {
+ try (ResultSet resultSet = statement.executeQuery(QUERY_PRIMARY_DATA_SOURCE)) {
if (resultSet.next()) {
return Optional.of(String.format("%s:%s", resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT")));
}
@@ -166,7 +120,7 @@ public final class MGRDatabaseDiscoveryType extends AbstractDatabaseDiscoveryTyp
List<String> result = new LinkedList<>();
try (Connection connection = dataSourceMap.get(getPrimaryDataSource()).getConnection();
Statement statement = connection.createStatement()) {
- ResultSet resultSet = statement.executeQuery(MEMBER_LIST);
+ ResultSet resultSet = statement.executeQuery(QUERY_MEMBER_LIST);
while (resultSet.next()) {
if (!"ONLINE".equals(resultSet.getString("MEMBER_STATE"))) {
continue;
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRHighlyAvailableStatus.java
new file mode 100644
index 00000000000..5fdfaec259a
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRHighlyAvailableStatus.java
@@ -0,0 +1,73 @@
+/*
+ * 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.dbdiscovery.mysql.type;
+
+import com.google.common.base.Preconditions;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
+import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Properties;
+
+/**
+ * Highly available status of MySQL MGR cluster.
+ */
+@RequiredArgsConstructor
+@Getter
+@EqualsAndHashCode
+public final class MGRHighlyAvailableStatus implements HighlyAvailableStatus {
+
+ private final boolean pluginActive;
+
+ private final boolean singlePrimaryMode;
+
+ private final String groupName;
+
+ private final Collection<String> memberInstanceURLs;
+
+ @Override
+ public void validate(final String databaseName, final Map<String, DataSource> dataSourceMap, final Properties props) throws SQLException {
+ Preconditions.checkState(pluginActive, "MGR plugin is not active in database `%s`.", databaseName);
+ Preconditions.checkState(singlePrimaryMode, "MGR is not in single primary mode in database `%s`.", databaseName);
+ Preconditions.checkState(props.getProperty("group-name", "").equals(groupName),
+ "Group name `%s` in MGR is not same with configured one `%s` in database `%s`.", groupName, props.getProperty("group-name"), databaseName);
+ Preconditions.checkState(!memberInstanceURLs.isEmpty(), "MGR member is empty in database `%s`.", databaseName);
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ checkDataSourceInReplicationGroup(databaseName, entry.getKey(), entry.getValue());
+ }
+ }
+
+ private void checkDataSourceInReplicationGroup(final String databaseName, final String dataSourceName, final DataSource dataSource) throws SQLException {
+ for (String each : memberInstanceURLs) {
+ try (Connection connection = dataSource.getConnection()) {
+ if (connection.getMetaData().getURL().contains(each)) {
+ return;
+ }
+ }
+ }
+ throw new ShardingSphereConfigurationException("%s is not in MGR replication group member in database `%s`.", dataSourceName, databaseName);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
index 933fb22ac3f..b9a5d79e54b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryType.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type;
-import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@@ -33,8 +32,6 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
@@ -53,24 +50,11 @@ public final class ShowSlaveStatusDatabaseDiscoveryType extends AbstractDatabase
private Properties props = new Properties();
@Override
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) throws SQLException {
- Collection<String> result = getPrimaryDataSourceURLS(dataSourceMap);
- Preconditions.checkState(!result.isEmpty(), "Not found primary data source for databaseName `%s`", databaseName);
- Preconditions.checkState(1 == result.size(), "More than one primary data source for databaseName `%s`", databaseName);
- }
-
- private Collection<String> getPrimaryDataSourceURLS(final Map<String, DataSource> dataSourceMap) throws SQLException {
- Collection<String> result = new ArrayList<>();
- for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
- try (Connection connection = entry.getValue().getConnection();
- Statement statement = connection.createStatement()) {
- Optional<String> url = loadPrimaryDataSourceURL(statement);
- if (url.isPresent() && !result.contains(url.get())) {
- result.add(url.get());
- }
- }
+ public ShowSlaveStatusHighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) throws SQLException {
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ return new ShowSlaveStatusHighlyAvailableStatus(loadPrimaryDataSourceURL(statement).orElse(null));
}
- return result;
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusHighlyAvailableStatus.java
new file mode 100644
index 00000000000..b250ff14141
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusHighlyAvailableStatus.java
@@ -0,0 +1,44 @@
+/*
+ * 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.dbdiscovery.mysql.type;
+
+import com.google.common.base.Preconditions;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
+
+import javax.sql.DataSource;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Highly available status of MySQL show slave status cluster.
+ */
+@RequiredArgsConstructor
+@Getter
+@EqualsAndHashCode
+public final class ShowSlaveStatusHighlyAvailableStatus implements HighlyAvailableStatus {
+
+ private final String primaryInstanceURL;
+
+ @Override
+ public void validate(final String databaseName, final Map<String, DataSource> dataSourceMap, final Properties props) {
+ Preconditions.checkState(null != primaryInstanceURL, "Can not load primary data source URL in database `%s`.", databaseName);
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryTypeTest.java
index a9b1967fe1d..91be6586f0b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/MGRDatabaseDiscoveryTypeTest.java
@@ -35,13 +35,18 @@ import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.verify;
@@ -49,45 +54,26 @@ import static org.mockito.Mockito.when;
public final class MGRDatabaseDiscoveryTypeTest {
- private static final String PLUGIN_STATUS = "SELECT * FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
-
- private static final String MEMBER_COUNT = "SELECT count(*) FROM performance_schema.replication_group_members";
-
- private static final String GROUP_NAME = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_group_name'";
-
- private static final String SINGLE_PRIMARY = "SELECT * FROM performance_schema.global_variables WHERE VARIABLE_NAME='group_replication_single_primary_mode'";
-
- private static final String GROUP_MEMBER = "SELECT MEMBER_HOST, MEMBER_PORT, MEMBER_STATE FROM performance_schema.replication_group_members";
-
- private final MGRDatabaseDiscoveryType mgrDatabaseDiscoveryType = new MGRDatabaseDiscoveryType();
+ private final MGRDatabaseDiscoveryType databaseDiscoveryType = new MGRDatabaseDiscoveryType();
@Test
- public void assertCheckMGRConfig() throws SQLException {
- DataSource dataSource = mock(DataSource.class);
- Connection connection = mock(Connection.class);
- Statement statement = mock(Statement.class);
+ public void assertLoadHighlyAvailableStatus() throws SQLException {
+ DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
ResultSet resultSet = mock(ResultSet.class);
- DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.createStatement()).thenReturn(statement);
- when(statement.executeQuery(PLUGIN_STATUS)).thenReturn(resultSet);
- when(statement.executeQuery(MEMBER_COUNT)).thenReturn(resultSet);
- when(statement.executeQuery(GROUP_NAME)).thenReturn(resultSet);
- when(statement.executeQuery(SINGLE_PRIMARY)).thenReturn(resultSet);
- when(statement.executeQuery(GROUP_MEMBER)).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(true, false, true, false, true, false, true, false, true, false);
+ when(dataSource.getConnection().createStatement().executeQuery(any())).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, true, true, true, true, false);
when(resultSet.getString("PLUGIN_STATUS")).thenReturn("ACTIVE");
- when(resultSet.getInt(1)).thenReturn(3);
- when(resultSet.getString("VARIABLE_VALUE")).thenReturn("group_name", "ON");
- when(resultSet.getString("MEMBER_HOST")).thenReturn("127.0.0.1");
- when(resultSet.getString("MEMBER_PORT")).thenReturn(Integer.toString(3306));
+ when(resultSet.getString("VARIABLE_VALUE")).thenReturn("ON", "group_name");
+ when(resultSet.getString("MEMBER_HOST")).thenReturn("127.0.0.1", "127.0.0.1");
+ when(resultSet.getString("MEMBER_PORT")).thenReturn("3306", "3307");
when(resultSet.getString("MEMBER_STATE")).thenReturn("ONLINE");
- when(connection.getMetaData()).thenReturn(databaseMetaData);
- when(databaseMetaData.getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false");
- mgrDatabaseDiscoveryType.getProps().setProperty("group-name", "group_name");
- Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
- dataSourceMap.put("ds_0", dataSource);
- mgrDatabaseDiscoveryType.checkDatabaseDiscoveryConfiguration("discovery_db", dataSourceMap);
+ when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/ds_0?serverTimezone=UTC&useSSL=false");
+ databaseDiscoveryType.getProps().setProperty("group-name", "group_name");
+ MGRHighlyAvailableStatus actual = databaseDiscoveryType.loadHighlyAvailableStatus(dataSource);
+ assertTrue(actual.isPluginActive());
+ assertTrue(actual.isSinglePrimaryMode());
+ assertThat(actual.getGroupName(), is("group_name"));
+ assertThat(actual.getMemberInstanceURLs(), is(Arrays.asList("127.0.0.1:3306", "127.0.0.1:3307")));
}
@Test
@@ -120,8 +106,10 @@ public final class MGRDatabaseDiscoveryTypeTest {
for (int i = 0; i < 3; i++) {
dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
}
- mgrDatabaseDiscoveryType.getProps().setProperty("group-name", "group_name");
- assertThat(mgrDatabaseDiscoveryType.determinePrimaryDataSource(dataSourceMap), is("ds_2"));
+ databaseDiscoveryType.getProps().setProperty("group-name", "group_name");
+ Optional<String> actual = databaseDiscoveryType.determinePrimaryDataSource(dataSourceMap);
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("ds_2"));
}
// TODO Fix me
@@ -130,7 +118,7 @@ public final class MGRDatabaseDiscoveryTypeTest {
public void assertUpdateMemberState() throws SQLException, IllegalAccessException, NoSuchFieldException {
Field declaredField = AbstractDatabaseDiscoveryType.class.getDeclaredField("oldPrimaryDataSource");
declaredField.setAccessible(true);
- declaredField.set(mgrDatabaseDiscoveryType, "ds_0");
+ declaredField.set(databaseDiscoveryType, "ds_0");
EventBus eventBus = mock(EventBus.class);
mockStatic(ShardingSphereEventBus.class);
when(ShardingSphereEventBus.getInstance()).thenReturn(eventBus);
@@ -162,7 +150,7 @@ public final class MGRDatabaseDiscoveryTypeTest {
for (int i = 0; i < 3; i++) {
dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
}
- mgrDatabaseDiscoveryType.updateMemberState("discovery_db", dataSourceMap, "readwrite_ds");
+ databaseDiscoveryType.updateMemberState("discovery_db", dataSourceMap, "readwrite_ds");
verify(eventBus).post(Mockito.refEq(new DataSourceDisabledEvent("discovery_db", "readwrite_ds", "ds_2",
new StorageNodeDataSource(StorageNodeRole.MEMBER, StorageNodeStatus.DISABLED))));
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
index 3ae250027ef..c67bf7df960 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/ShowSlaveStatusDatabaseDiscoveryTypeTest.java
@@ -27,22 +27,20 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class ShowSlaveStatusDatabaseDiscoveryTypeTest {
- private final ShowSlaveStatusDatabaseDiscoveryType showSlaveStatusDatabaseDiscoveryType = new ShowSlaveStatusDatabaseDiscoveryType();
-
@Test
- public void assertCheckShowSlaveStatusConfig() throws SQLException {
- Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
- dataSourceMap.put("ds_0", getDataSource(false, 3306));
- dataSourceMap.put("ds_1", getDataSource(true, 3307));
- showSlaveStatusDatabaseDiscoveryType.checkDatabaseDiscoveryConfiguration("discovery_db", dataSourceMap);
+ public void assertLoadHighlyAvailableStatus() throws SQLException {
+ ShowSlaveStatusHighlyAvailableStatus actual = new ShowSlaveStatusDatabaseDiscoveryType().loadHighlyAvailableStatus(getDataSource(true, 3306));
+ assertThat(actual.getPrimaryInstanceURL(), is("127.0.0.1:3306"));
}
@Test
@@ -50,7 +48,9 @@ public final class ShowSlaveStatusDatabaseDiscoveryTypeTest {
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("ds_0", getDataSource(false, 3306));
dataSourceMap.put("ds_1", getDataSource(true, 3307));
- assertThat(showSlaveStatusDatabaseDiscoveryType.determinePrimaryDataSource(dataSourceMap), is("ds_0"));
+ Optional<String> actual = new ShowSlaveStatusDatabaseDiscoveryType().determinePrimaryDataSource(dataSourceMap);
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("ds_0"));
}
private DataSource getDataSource(final boolean slave, final int port) throws SQLException {
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
index 7462394cb7c..2d95c1acc1e 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryType.java
@@ -49,13 +49,12 @@ public final class OpenGaussDatabaseDiscoveryType implements DatabaseDiscoveryTy
private String oldPrimaryDataSource;
- @Getter
- @Setter
private Properties props = new Properties();
@Override
- public void checkDatabaseDiscoveryConfiguration(final String databaseName, final Map<String, DataSource> dataSourceMap) {
- // TODO Check openGauss configuration
+ public OpenGaussHighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) {
+ // TODO Load OpenGaussHighlyAvailableStatus
+ return new OpenGaussHighlyAvailableStatus();
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHighlyAvailableStatus.java
new file mode 100644
index 00000000000..70f8256d2ef
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussHighlyAvailableStatus.java
@@ -0,0 +1,36 @@
+/*
+ * 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.dbdiscovery.opengauss;
+
+import lombok.EqualsAndHashCode;
+import org.apache.shardingsphere.dbdiscovery.spi.HighlyAvailableStatus;
+
+import javax.sql.DataSource;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * Highly available status of openGauss cluster.
+ */
+@EqualsAndHashCode
+public final class OpenGaussHighlyAvailableStatus implements HighlyAvailableStatus {
+
+ @Override
+ public void validate(final String databaseName, final Map<String, DataSource> dataSourceMap, final Properties props) {
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
index 488ac0f1483..b3c2f88ed77 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/OpenGaussDatabaseDiscoveryTypeTest.java
@@ -29,9 +29,11 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -39,29 +41,6 @@ public final class OpenGaussDatabaseDiscoveryTypeTest {
private static final String DB_ROLE = "SELECT local_role,db_state FROM pg_stat_get_stream_replications()";
- private static final String STANDBYS = "SELECT client_addr,sync_state FROM pg_stat_replication";
-
- private final OpenGaussDatabaseDiscoveryType ogHaType = new OpenGaussDatabaseDiscoveryType();
-
- @Test
- public void assertCheckHAConfig() throws SQLException {
- DataSource dataSource = mock(DataSource.class);
- Connection connection = mock(Connection.class);
- Statement statement = mock(Statement.class);
- ResultSet resultSet = mock(ResultSet.class);
- when(dataSource.getConnection()).thenReturn(connection);
- when(connection.createStatement()).thenReturn(statement);
- when(statement.executeQuery(DB_ROLE)).thenReturn(resultSet);
- when(statement.executeQuery(STANDBYS)).thenReturn(resultSet);
- when(resultSet.next()).thenReturn(true, false, true, false, true, false, true, false);
- when(resultSet.getString("local_role")).thenReturn("Primary");
- when(resultSet.getString("db_state")).thenReturn("Normal");
- when(resultSet.getString("db_state")).thenReturn("Sync");
- Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
- dataSourceMap.put("ds_0", dataSource);
- ogHaType.checkDatabaseDiscoveryConfiguration("discovery_db", dataSourceMap);
- }
-
@Test
public void assertDeterminePrimaryDataSource() throws SQLException {
List<DataSource> dataSources = new LinkedList<>();
@@ -90,6 +69,8 @@ public final class OpenGaussDatabaseDiscoveryTypeTest {
for (int i = 0; i < 3; i++) {
dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
}
- assertThat(ogHaType.determinePrimaryDataSource(dataSourceMap), is("ds_2"));
+ Optional<String> actual = new OpenGaussDatabaseDiscoveryType().determinePrimaryDataSource(dataSourceMap);
+ assertTrue(actual.isPresent());
+ assertThat(actual.get(), is("ds_2"));
}
}