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/04/24 15:42:14 UTC
[shardingsphere] branch master updated: Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() to isPrimaryInstance() (#17070)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 018d39b5a5a Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() to isPrimaryInstance() (#17070)
018d39b5a5a is described below
commit 018d39b5a5a73f054b3ad736ea6a134211a047bf
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Apr 24 23:42:08 2022 +0800
Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() to isPrimaryInstance() (#17070)
* Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() to isPrimaryInstance()
* For file format
* Fix test cases
* Fix test cases
---
.../spi/DatabaseDiscoveryProviderAlgorithm.java | 13 ++++------
.../algorithm/DatabaseDiscoveryEngine.java | 30 +++-------------------
...eFixtureDatabaseDiscoveryProviderAlgorithm.java | 7 ++---
.../route/DatabaseDiscoverySQLRouterTest.java | 2 +-
.../rule/DatabaseDiscoveryRuleTest.java | 2 +-
...LFixtureDatabaseDiscoveryProviderAlgorithm.java | 6 ++---
...MGRMySQLDatabaseDiscoveryProviderAlgorithm.java | 19 ++++++--------
...ionMySQLDatabaseDiscoveryProviderAlgorithm.java | 13 +++++++---
.../MGRDatabaseDiscoveryProviderAlgorithmTest.java | 13 ++++------
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 8 ++----
...licationDatabaseDiscoveryProviderAlgorithm.java | 10 +++-----
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 11 ++------
12 files changed, 43 insertions(+), 91 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryProviderAlgorithm.java
index 8c2a56c9acf..2b3833bf92c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/DatabaseDiscoveryProviderAlgorithm.java
@@ -17,14 +17,12 @@
package org.apache.shardingsphere.dbdiscovery.spi;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithm;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
-import java.util.Optional;
/**
* Database discovery provider algorithm.
@@ -41,13 +39,12 @@ public interface DatabaseDiscoveryProviderAlgorithm extends ShardingSphereAlgori
HighlyAvailableStatus loadHighlyAvailableStatus(DataSource dataSource) throws SQLException;
/**
- * Find primary database instance.
- *
- * @param dataSourceName data source name
- * @param dataSource data source
- * @return found primary database instance
+ * Judge whether database instance is primary.
+ *
+ * @param dataSource data source to be judged
+ * @return is primary database instance or not
*/
- Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(String dataSourceName, DataSource dataSource);
+ boolean isPrimaryInstance(DataSource dataSource);
/**
* Get storage node data source.
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 8f0ab887707..c380050783a 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
@@ -21,9 +21,6 @@ import com.google.common.base.Preconditions;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatabaseInstance;
import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
import org.apache.shardingsphere.dbdiscovery.spi.status.type.GlobalHighlyAvailableStatus;
import org.apache.shardingsphere.dbdiscovery.spi.status.type.RoleSeparatedHighlyAvailableStatus;
@@ -33,7 +30,6 @@ import org.apache.shardingsphere.infra.rule.event.impl.DataSourceDisabledEvent;
import org.apache.shardingsphere.infra.rule.event.impl.PrimaryDataSourceChangedEvent;
import javax.sql.DataSource;
-import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
@@ -113,29 +109,9 @@ public final class DatabaseDiscoveryEngine {
private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
for (Entry<String, DataSource> entry : getActiveDataSourceMap(dataSourceMap, disabledDataSourceNames).entrySet()) {
- Optional<? extends PrimaryDatabaseInstance> newPrimaryInstance = databaseDiscoveryProviderAlgorithm.findPrimaryInstance(entry.getKey(), entry.getValue());
- if (newPrimaryInstance.isPresent()) {
- return findPrimaryDataSourceName(dataSourceMap, newPrimaryInstance.get());
- }
- }
- return Optional.empty();
- }
-
- private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap, final PrimaryDatabaseInstance newPrimaryInstance) {
- return newPrimaryInstance instanceof IPPortPrimaryDatabaseInstance
- ? findPrimaryDataSourceName(dataSourceMap, (IPPortPrimaryDatabaseInstance) newPrimaryInstance)
- : Optional.of(((NamedPrimaryDatabaseInstance) newPrimaryInstance).getDataSourceName());
- }
-
- private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap, final IPPortPrimaryDatabaseInstance databaseInstance) {
- for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
- try (Connection connection = entry.getValue().getConnection()) {
- String url = connection.getMetaData().getURL();
- if (null != url && url.contains(databaseInstance.toString())) {
- return Optional.of(entry.getKey());
- }
- } catch (final SQLException ex) {
- log.error("An exception occurred while find primary data source name", ex);
+ boolean isPrimaryInstance = databaseDiscoveryProviderAlgorithm.isPrimaryInstance(entry.getValue());
+ if (isPrimaryInstance) {
+ return Optional.of(entry.getKey());
}
}
return Optional.empty();
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryProviderAlgorithm.java
index f7c317e499c..5ac180254be 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/fixture/CoreFixtureDatabaseDiscoveryProviderAlgorithm.java
@@ -18,15 +18,12 @@
package org.apache.shardingsphere.dbdiscovery.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatabaseInstance;
import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
import javax.sql.DataSource;
-import java.util.Optional;
import static org.mockito.Mockito.mock;
@@ -38,8 +35,8 @@ public final class CoreFixtureDatabaseDiscoveryProviderAlgorithm implements Data
}
@Override
- public Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
- return Optional.of(new NamedPrimaryDatabaseInstance("primary"));
+ public boolean isPrimaryInstance(final DataSource dataSource) {
+ return true;
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
index 34634263016..1fec8265149 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/route/DatabaseDiscoverySQLRouterTest.java
@@ -79,7 +79,7 @@ public final class DatabaseDiscoverySQLRouterTest {
DatabaseDiscoveryRuleConfiguration config = new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceConfig),
Collections.singletonMap("ha_heartbeat", new DatabaseDiscoveryHeartBeatConfiguration(new Properties())),
Collections.singletonMap("CORE.FIXTURE", algorithmConfig));
- rule = new DatabaseDiscoveryRule("CORE.FIXTURE", Collections.singletonMap("ds", mock(DataSource.class)), config);
+ rule = new DatabaseDiscoveryRule(DATA_SOURCE_NAME, Collections.singletonMap(PRIMARY_DATA_SOURCE, mock(DataSource.class)), config);
sqlRouter = (DatabaseDiscoverySQLRouter) OrderedSPIRegistry.getRegisteredServices(SQLRouter.class, Collections.singleton(rule)).get(rule);
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
index 80b7b44308c..c869c23a4c4 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-core/src/test/java/org/apache/shardingsphere/dbdiscovery/rule/DatabaseDiscoveryRuleTest.java
@@ -41,7 +41,7 @@ import static org.mockito.Mockito.mock;
public final class DatabaseDiscoveryRuleTest {
- private final Map<String, DataSource> dataSourceMap = Collections.singletonMap("ds", mock(DataSource.class));
+ private final Map<String, DataSource> dataSourceMap = Collections.singletonMap("primary", mock(DataSource.class));
@Test
public void assertNewWithEmptyDataSourceRule() {
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/DistSQLFixtureDatabaseDiscoveryProviderAlgorithm.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/DistSQLF [...]
index 02a5f4152ee..3ec318bab7e 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/DistSQLFixtureDatabaseDiscoveryProviderAlgorithm.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/DistSQLFixtureDatabaseDiscoveryProviderAlgorithm.java
@@ -18,14 +18,12 @@
package org.apache.shardingsphere.dbdiscovery.distsql.handler.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
import javax.sql.DataSource;
-import java.util.Optional;
import static org.mockito.Mockito.mock;
@@ -37,8 +35,8 @@ public final class DistSQLFixtureDatabaseDiscoveryProviderAlgorithm implements D
}
@Override
- public Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
- return Optional.empty();
+ public boolean isPrimaryInstance(final DataSource dataSource) {
+ return false;
}
@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/mgr/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
index cf7bf2f0d2e..cac485cb83a 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRMySQLDatabaseDiscoveryProviderAlgorithm.java
@@ -35,7 +35,6 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
-import java.util.Optional;
import java.util.Properties;
/**
@@ -99,21 +98,19 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements Databas
}
@Override
- public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
+ public boolean isPrimaryInstance(final DataSource dataSource) {
try (
Connection connection = dataSource.getConnection();
- Statement statement = connection.createStatement()) {
- return loadPrimaryDatabaseInstance(statement);
+ Statement statement = connection.createStatement();
+ ResultSet resultSet = statement.executeQuery(QUERY_PRIMARY_DATA_SOURCE)) {
+ if (resultSet.next()) {
+ MySQLDataSourceMetaData metaData = new MySQLDataSourceMetaData(connection.getMetaData().getURL());
+ return metaData.getHostname().equals(resultSet.getString("MEMBER_HOST")) && Integer.toString(metaData.getPort()).equals(resultSet.getString("MEMBER_PORT"));
+ }
} catch (final SQLException ex) {
log.error("An exception occurred while find primary data source name", ex);
}
- return Optional.empty();
- }
-
- private Optional<IPPortPrimaryDatabaseInstance> loadPrimaryDatabaseInstance(final Statement statement) throws SQLException {
- try (ResultSet resultSet = statement.executeQuery(QUERY_PRIMARY_DATA_SOURCE)) {
- return resultSet.next() ? Optional.of(new IPPortPrimaryDatabaseInstance(resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT"))) : Optional.empty();
- }
+ return false;
}
@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/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalRepl [...]
index 4b43f83aaae..5a8469872a9 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm.java
@@ -22,6 +22,7 @@ import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
+import org.apache.shardingsphere.infra.database.metadata.dialect.MySQLDataSourceMetaData;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
@@ -56,15 +57,19 @@ public final class MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm
}
@Override
- public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
+ public boolean isPrimaryInstance(final DataSource dataSource) {
try (
Connection connection = dataSource.getConnection();
- Statement statement = connection.createStatement()) {
- return loadPrimaryDatabaseInstance(statement);
+ Statement statement = connection.createStatement();
+ ResultSet resultSet = statement.executeQuery(SHOW_SLAVE_STATUS)) {
+ if (resultSet.next()) {
+ MySQLDataSourceMetaData metaData = new MySQLDataSourceMetaData(connection.getMetaData().getURL());
+ return metaData.getHostname().equals(resultSet.getString("Master_Host")) && Integer.toString(metaData.getPort()).equals(resultSet.getString("Master_Port"));
+ }
} catch (final SQLException ex) {
log.error("An exception occurred while find primary data source name", ex);
}
- return Optional.empty();
+ return false;
}
private Optional<IPPortPrimaryDatabaseInstance> loadPrimaryDatabaseInstance(final Statement statement) throws SQLException {
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/mgr/MGRDatabaseDiscoveryProviderAlgorithmTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRDatabaseDiscoveryProviderAlgorithmTest.java
index b4143b39301..2fc1fc30916 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRDatabaseDiscoveryProviderAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/mgr/MGRDatabaseDiscoveryProviderAlgorithmTest.java
@@ -25,7 +25,6 @@ import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
-import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
@@ -65,15 +64,13 @@ public final class MGRDatabaseDiscoveryProviderAlgorithmTest {
}
@Test
- public void assertFindPrimaryDataSource() throws SQLException {
- String sql = "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')";
- Optional<IPPortPrimaryDatabaseInstance> actual = new MGRMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("foo_ds", mockToBeFoundPrimaryDataSource(sql));
- assertTrue(actual.isPresent());
- assertThat(actual.get().toString(), is("127.0.0.1:3306"));
+ public void assertIsPrimaryInstance() throws SQLException {
+ assertTrue(new MGRMySQLDatabaseDiscoveryProviderAlgorithm().isPrimaryInstance(mockPrimaryDataSource()));
}
- private DataSource mockToBeFoundPrimaryDataSource(final String sql) throws SQLException {
+ private DataSource mockPrimaryDataSource() throws SQLException {
+ String sql = "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')";
DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
ResultSet resultSet = mock(ResultSet.class);
when(result.getConnection().createStatement().executeQuery(sql)).thenReturn(resultSet);
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/replication/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalRepli [...]
index 1540d3b5b3b..68a61470142 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/test/java/org/apache/shardingsphere/dbdiscovery/mysql/type/replication/MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
@@ -17,13 +17,11 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type.replication;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@@ -41,10 +39,8 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
}
@Test
- public void assertFindPrimaryDataSource() throws SQLException {
- Optional<IPPortPrimaryDatabaseInstance> actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("ds_0", mockDataSource());
- assertTrue(actual.isPresent());
- assertThat(actual.get().toString(), is("127.0.0.1:3306"));
+ public void assertIsPrimaryInstance() throws SQLException {
+ assertTrue(new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().isPrimaryInstance(mockDataSource()));
}
private DataSource mockDataSource() 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/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussN [...]
index 96e03898343..c8f604d0072 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm.java
@@ -21,7 +21,6 @@ import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatabaseInstance;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
@@ -31,7 +30,6 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
-import java.util.Optional;
import java.util.Properties;
/**
@@ -57,18 +55,16 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm
}
@Override
- public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
+ public boolean isPrimaryInstance(final DataSource dataSource) {
try (
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(QUERY_DB_ROLE)) {
- if (resultSet.next() && "Primary".equals(resultSet.getString("local_role")) && "Normal".equals(resultSet.getString("db_state"))) {
- return Optional.of(new NamedPrimaryDatabaseInstance(dataSourceName));
- }
+ return resultSet.next() && "Primary".equals(resultSet.getString("local_role")) && "Normal".equals(resultSet.getString("db_state"));
} catch (final SQLException ex) {
log.error("An exception occurred while find primary data source url", ex);
}
- return Optional.empty();
+ return false;
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGa [...]
index 0a03b4f9c77..81c62adca81 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/test/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmTest.java
@@ -17,16 +17,12 @@
package org.apache.shardingsphere.dbdiscovery.opengauss.replication;
-import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatabaseInstance;
import org.junit.Test;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
-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.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
@@ -35,11 +31,8 @@ import static org.mockito.Mockito.when;
public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmTest {
@Test
- public void assertFindPrimaryDataSource() throws SQLException {
- DataSource dataSource = mockDatSource();
- Optional<NamedPrimaryDatabaseInstance> actual = new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("foo_ds", dataSource);
- assertTrue(actual.isPresent());
- assertThat(actual.get().getDataSourceName(), is("foo_ds"));
+ public void assertIsPrimaryInstance() throws SQLException {
+ assertTrue(new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().isPrimaryInstance(mockDatSource()));
}
private DataSource mockDatSource() throws SQLException {