You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/24 10:44:32 UTC
[shardingsphere] branch master updated: Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17058)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 0f0ddf8ffcc Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17058)
0f0ddf8ffcc is described below
commit 0f0ddf8ffcc1fa9b85f7065d6bf4c72ab13c90fe
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Apr 24 18:44:26 2022 +0800
Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17058)
* Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance()
* Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance()
---
.../spi/DatabaseDiscoveryProviderAlgorithm.java | 7 +-
.../PrimaryDatabaseInstance.java} | 6 +-
.../type/IPPortPrimaryDatabaseInstance.java} | 24 ++++---
.../type/NamedPrimaryDatabaseInstance.java} | 19 ++---
.../{ => type}/GlobalHighlyAvailableStatus.java | 4 +-
.../RoleSeparatedHighlyAvailableStatus.java | 4 +-
.../algorithm/DatabaseDiscoveryEngine.java | 49 ++++++++++---
...eFixtureDatabaseDiscoveryProviderAlgorithm.java | 5 +-
...LFixtureDatabaseDiscoveryProviderAlgorithm.java | 3 +-
...actMySQLDatabaseDiscoveryProviderAlgorithm.java | 81 ----------------------
.../mysql/type/mgr/MGRHighlyAvailableStatus.java | 2 +-
...MGRMySQLDatabaseDiscoveryProviderAlgorithm.java | 31 +++++++--
...ySQLNormalReplicationHighlyAvailableStatus.java | 7 +-
...ionMySQLDatabaseDiscoveryProviderAlgorithm.java | 33 +++++++--
.../MGRDatabaseDiscoveryProviderAlgorithmTest.java | 5 +-
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 7 +-
...licationDatabaseDiscoveryProviderAlgorithm.java | 5 +-
...aussNormalReplicationHighlyAvailableStatus.java | 2 +-
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 5 +-
.../DatabaseDiscoverySpringNamespaceTest.java | 3 -
20 files changed, 157 insertions(+), 145 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 8e5dd983421..1c0cf7f4722 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,6 +17,7 @@
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;
@@ -41,12 +42,12 @@ public interface DatabaseDiscoveryProviderAlgorithm extends ShardingSphereAlgori
HighlyAvailableStatus loadHighlyAvailableStatus(DataSource dataSource) throws SQLException;
/**
- * Find primary data source name.
+ * Find primary instance.
*
* @param dataSourceMap data source map
- * @return found name of primary data source
+ * @return found primary instance
*/
- Optional<String> findPrimaryDataSourceName(Map<String, DataSource> dataSourceMap);
+ Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(Map<String, DataSource> dataSourceMap);
/**
* Get storage node data source.
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/PrimaryDatabaseInstance.java
similarity index 80%
copy from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java
copy to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/PrimaryDatabaseInstance.java
index 1ef54ab621f..95ab85c6a8b 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/PrimaryDatabaseInstance.java
@@ -15,10 +15,10 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.dbdiscovery.spi.status;
+package org.apache.shardingsphere.dbdiscovery.spi.instance;
/**
- * Global highly available status for database cluster.
+ * Primary database instance.
*/
-public interface GlobalHighlyAvailableStatus extends HighlyAvailableStatus {
+public interface PrimaryDatabaseInstance {
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/IPPortPrimaryDatabaseInstance.java
similarity index 61%
copy from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
copy to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/IPPortPrimaryDatabaseInstance.java
index ed0f91d035c..090bf234462 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/IPPortPrimaryDatabaseInstance.java
@@ -15,17 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.dbdiscovery.spi.status;
+package org.apache.shardingsphere.dbdiscovery.spi.instance.type;
+
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
/**
- * Role separated highly available status for database cluster.
+ * Primary database instance by IP and port.
*/
-public interface RoleSeparatedHighlyAvailableStatus extends HighlyAvailableStatus {
+@RequiredArgsConstructor
+public final class IPPortPrimaryDatabaseInstance implements PrimaryDatabaseInstance {
+
+ private final String ip;
+
+ private final String port;
- /**
- * Is primary database instance.
- *
- * @return primary database instance or not
- */
- boolean isPrimary();
+ @Override
+ public String toString() {
+ return String.join(":", ip, port);
+ }
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/NamedPrimaryDatabaseInstance.java
similarity index 66%
copy from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
copy to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/NamedPrimaryDatabaseInstance.java
index ed0f91d035c..a064ea1bb58 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/instance/type/NamedPrimaryDatabaseInstance.java
@@ -15,17 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.dbdiscovery.spi.status;
+package org.apache.shardingsphere.dbdiscovery.spi.instance.type;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.PrimaryDatabaseInstance;
/**
- * Role separated highly available status for database cluster.
+ * Primary database instance by data source name.
*/
-public interface RoleSeparatedHighlyAvailableStatus extends HighlyAvailableStatus {
+@RequiredArgsConstructor
+@Getter
+public final class NamedPrimaryDatabaseInstance implements PrimaryDatabaseInstance {
- /**
- * Is primary database instance.
- *
- * @return primary database instance or not
- */
- boolean isPrimary();
+ private final String dataSourceName;
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/GlobalHighlyAvailableStatus.java
similarity index 86%
rename from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java
rename to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/GlobalHighlyAvailableStatus.java
index 1ef54ab621f..49f65c6eb51 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/GlobalHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/GlobalHighlyAvailableStatus.java
@@ -15,7 +15,9 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.dbdiscovery.spi.status;
+package org.apache.shardingsphere.dbdiscovery.spi.status.type;
+
+import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
/**
* Global highly available status for database cluster.
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/RoleSeparatedHighlyAvailableStatus.java
similarity index 88%
rename from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
rename to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/RoleSeparatedHighlyAvailableStatus.java
index ed0f91d035c..0addb05476c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/RoleSeparatedHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-api/src/main/java/org/apache/shardingsphere/dbdiscovery/spi/status/type/RoleSeparatedHighlyAvailableStatus.java
@@ -15,7 +15,9 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.dbdiscovery.spi.status;
+package org.apache.shardingsphere.dbdiscovery.spi.status.type;
+
+import org.apache.shardingsphere.dbdiscovery.spi.status.HighlyAvailableStatus;
/**
* Role separated highly available status for database cluster.
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 45d2047aba4..f953d2fae95 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
@@ -19,16 +19,21 @@ package org.apache.shardingsphere.dbdiscovery.algorithm;
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.status.GlobalHighlyAvailableStatus;
+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.RoleSeparatedHighlyAvailableStatus;
+import org.apache.shardingsphere.dbdiscovery.spi.status.type.GlobalHighlyAvailableStatus;
+import org.apache.shardingsphere.dbdiscovery.spi.status.type.RoleSeparatedHighlyAvailableStatus;
import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
import org.apache.shardingsphere.infra.metadata.schema.QualifiedDatabase;
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;
@@ -41,6 +46,7 @@ import java.util.Optional;
* Database discovery engine.
*/
@RequiredArgsConstructor
+@Slf4j
public final class DatabaseDiscoveryEngine {
private final DatabaseDiscoveryProviderAlgorithm databaseDiscoveryProviderAlgorithm;
@@ -77,8 +83,8 @@ public final class DatabaseDiscoveryEngine {
statuses.iterator().next().validate(databaseName, dataSourceMap, databaseDiscoveryProviderAlgorithm.getProps());
}
- private void checkRoleSeparatedHighlyAvailableStatus(
- final String databaseName, final Map<String, DataSource> dataSourceMap, final Collection<HighlyAvailableStatus> statuses) throws SQLException {
+ private void checkRoleSeparatedHighlyAvailableStatus(final String databaseName,
+ final Map<String, DataSource> dataSourceMap, final Collection<HighlyAvailableStatus> statuses) throws SQLException {
for (HighlyAvailableStatus each : statuses) {
each.validate(databaseName, dataSourceMap, databaseDiscoveryProviderAlgorithm.getProps());
}
@@ -96,7 +102,7 @@ public final class DatabaseDiscoveryEngine {
*/
public String changePrimaryDataSource(final String databaseName, final String groupName, final String originalPrimaryDataSourceName,
final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
- Optional<String> newPrimaryDataSourceName = databaseDiscoveryProviderAlgorithm.findPrimaryDataSourceName(getActiveDataSourceMap(dataSourceMap, disabledDataSourceNames));
+ Optional<String> newPrimaryDataSourceName = findPrimaryDataSourceName(dataSourceMap, disabledDataSourceNames);
if (newPrimaryDataSourceName.isPresent() && !newPrimaryDataSourceName.get().equals(originalPrimaryDataSourceName)) {
ShardingSphereEventBus.getInstance().post(new PrimaryDataSourceChangedEvent(new QualifiedDatabase(databaseName, groupName, newPrimaryDataSourceName.get())));
}
@@ -105,13 +111,29 @@ public final class DatabaseDiscoveryEngine {
return result;
}
- private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName, final Map<String, DataSource> dataSourceMap) {
+ private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
+ Optional<? extends PrimaryDatabaseInstance> newPrimaryInstance = databaseDiscoveryProviderAlgorithm.findPrimaryInstance(getActiveDataSourceMap(dataSourceMap, disabledDataSourceNames));
+ return newPrimaryInstance.isPresent() ? findPrimaryDataSourceName(dataSourceMap, newPrimaryInstance.get()) : 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()) {
- if (!entry.getKey().equals(primaryDataSourceName)) {
- ShardingSphereEventBus.getInstance().post(
- new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), databaseDiscoveryProviderAlgorithm.getStorageNodeDataSource(entry.getValue())));
+ 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);
}
}
+ return Optional.empty();
}
private Map<String, DataSource> getActiveDataSourceMap(final Map<String, DataSource> dataSourceMap, final Collection<String> disabledDataSourceNames) {
@@ -121,4 +143,13 @@ public final class DatabaseDiscoveryEngine {
}
return result;
}
+
+ private void postReplicaDataSourceDisabledEvent(final String databaseName, final String groupName, final String primaryDataSourceName, final Map<String, DataSource> dataSourceMap) {
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ if (!entry.getKey().equals(primaryDataSourceName)) {
+ ShardingSphereEventBus.getInstance().post(
+ new DataSourceDisabledEvent(databaseName, groupName, entry.getKey(), databaseDiscoveryProviderAlgorithm.getStorageNodeDataSource(entry.getValue())));
+ }
+ }
+ }
}
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 434d169da2b..d86b2a2c7ce 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,6 +18,7 @@
package org.apache.shardingsphere.dbdiscovery.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
+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;
@@ -37,8 +38,8 @@ public final class CoreFixtureDatabaseDiscoveryProviderAlgorithm implements Data
}
@Override
- public Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap) {
- return Optional.of("primary");
+ public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
+ return Optional.of(new NamedPrimaryDatabaseInstance("primary"));
}
@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/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 1a7ccff67c2..3d5d96c4110 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,6 +18,7 @@
package org.apache.shardingsphere.dbdiscovery.distsql.handler.fixture;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
+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;
@@ -37,7 +38,7 @@ public final class DistSQLFixtureDatabaseDiscoveryProviderAlgorithm implements D
}
@Override
- public Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap) {
+ public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
return Optional.empty();
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryProviderAlgorithm.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryProviderAlgorithm.java
deleted file mode 100644
index 32e2576c653..00000000000
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-mysql/src/main/java/org/apache/shardingsphere/dbdiscovery/mysql/AbstractMySQLDatabaseDiscoveryProviderAlgorithm.java
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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;
-
-import lombok.Getter;
-import lombok.Setter;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
-
-import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.Map;
-import java.util.Map.Entry;
-import java.util.Optional;
-import java.util.Properties;
-
-/**
- * Abstract MySQL database discovery provider algorithm.
- */
-@Getter
-@Setter
-@Slf4j
-public abstract class AbstractMySQLDatabaseDiscoveryProviderAlgorithm implements DatabaseDiscoveryProviderAlgorithm {
-
- private Properties props = new Properties();
-
- @Override
- public final Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap) {
- String primaryDatabaseInstanceURL = loadPrimaryDatabaseInstanceURL(dataSourceMap);
- return findPrimaryDataSourceName(dataSourceMap, primaryDatabaseInstanceURL);
- }
-
- private Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap, final String primaryDatabaseInstanceURL) {
- for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
- try (Connection connection = entry.getValue().getConnection()) {
- String url = connection.getMetaData().getURL();
- if (null != url && url.contains(primaryDatabaseInstanceURL)) {
- return Optional.of(entry.getKey());
- }
- } catch (final SQLException ex) {
- log.error("An exception occurred while find primary data source name", ex);
- }
- }
- return Optional.empty();
- }
-
- private String loadPrimaryDatabaseInstanceURL(final Map<String, DataSource> dataSourceMap) {
- for (DataSource each : dataSourceMap.values()) {
- try (
- Connection connection = each.getConnection();
- Statement statement = connection.createStatement()) {
- Optional<String> primaryDatabaseInstanceURL = loadPrimaryDatabaseInstanceURL(statement);
- if (primaryDatabaseInstanceURL.isPresent()) {
- return primaryDatabaseInstanceURL.get();
- }
- } catch (final SQLException ex) {
- log.error("An exception occurred while find primary data source url", ex);
- }
- }
- return "";
- }
-
- protected abstract Optional<String> loadPrimaryDatabaseInstanceURL(Statement statement) throws SQLException;
-}
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/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/mgr/MGRHighlyAvailableStatus.java
index 7c2a23f6e20..037dbf9f77b 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/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/mgr/MGRHighlyAvailableStatus.java
@@ -21,7 +21,7 @@ import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.spi.status.GlobalHighlyAvailableStatus;
+import org.apache.shardingsphere.dbdiscovery.spi.status.type.GlobalHighlyAvailableStatus;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import javax.sql.DataSource;
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 936980b4399..74de129b724 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
@@ -17,8 +17,11 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type.mgr;
+import lombok.Getter;
+import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.dbdiscovery.mysql.AbstractMySQLDatabaseDiscoveryProviderAlgorithm;
+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;
@@ -32,13 +35,18 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.LinkedList;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
+import java.util.Properties;
/**
* MGR database discovery provider algorithm for MySQL.
*/
+@Getter
+@Setter
@Slf4j
-public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm extends AbstractMySQLDatabaseDiscoveryProviderAlgorithm {
+public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements DatabaseDiscoveryProviderAlgorithm {
private static final String QUERY_PLUGIN_STATUS = "SELECT PLUGIN_STATUS FROM information_schema.PLUGINS WHERE PLUGIN_NAME='group_replication'";
@@ -53,6 +61,8 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm extends AbstractMy
private static final String QUERY_CURRENT_MEMBER_STATE = "SELECT MEMBER_STATE FROM performance_schema.replication_group_members WHERE MEMBER_HOST=? AND MEMBER_PORT=?";
+ private Properties props = new Properties();
+
@Override
public MGRHighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) throws SQLException {
try (
@@ -91,9 +101,22 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm extends AbstractMy
}
@Override
- protected Optional<String> loadPrimaryDatabaseInstanceURL(final Statement statement) throws SQLException {
+ public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ try (
+ Connection connection = entry.getValue().getConnection();
+ Statement statement = connection.createStatement()) {
+ return loadPrimaryDatabaseInstance(statement);
+ } 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(String.format("%s:%s", resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT"))) : Optional.empty();
+ return resultSet.next() ? Optional.of(new IPPortPrimaryDatabaseInstance(resultSet.getString("MEMBER_HOST"), resultSet.getString("MEMBER_PORT"))) : Optional.empty();
}
}
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/MySQLNormalReplicationHighlyAvailableStatus.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/MySQLNormalReplicationHighlyAvail [...]
index 5cec7809d81..d5be7412aca 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/MySQLNormalReplicationHighlyAvailableStatus.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/MySQLNormalReplicationHighlyAvailableStatus.java
@@ -21,7 +21,8 @@ import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.spi.status.GlobalHighlyAvailableStatus;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
+import org.apache.shardingsphere.dbdiscovery.spi.status.type.GlobalHighlyAvailableStatus;
import javax.sql.DataSource;
import java.util.Map;
@@ -35,10 +36,10 @@ import java.util.Properties;
@EqualsAndHashCode
public final class MySQLNormalReplicationHighlyAvailableStatus implements GlobalHighlyAvailableStatus {
- private final String primaryInstanceURL;
+ private final IPPortPrimaryDatabaseInstance databaseInstance;
@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);
+ Preconditions.checkState(null != databaseInstance, "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/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 bb6b9e08f17..cf3a19d10df 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
@@ -17,8 +17,11 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type.replication;
+import lombok.Getter;
+import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
-import org.apache.shardingsphere.dbdiscovery.mysql.AbstractMySQLDatabaseDiscoveryProviderAlgorithm;
+import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
@@ -28,33 +31,53 @@ import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Optional;
+import java.util.Properties;
/**
* Normal replication database discovery provider algorithm for MySQL.
*/
+@Getter
+@Setter
@Slf4j
-public final class MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm extends AbstractMySQLDatabaseDiscoveryProviderAlgorithm {
+public final class MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm implements DatabaseDiscoveryProviderAlgorithm {
private static final String SHOW_SLAVE_STATUS = "SHOW SLAVE STATUS";
+ private Properties props = new Properties();
+
@Override
public MySQLNormalReplicationHighlyAvailableStatus loadHighlyAvailableStatus(final DataSource dataSource) throws SQLException {
try (
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
- return new MySQLNormalReplicationHighlyAvailableStatus(loadPrimaryDatabaseInstanceURL(statement).orElse(null));
+ return new MySQLNormalReplicationHighlyAvailableStatus(loadPrimaryDatabaseInstance(statement).orElse(null));
}
}
@Override
- protected Optional<String> loadPrimaryDatabaseInstanceURL(final Statement statement) throws SQLException {
+ public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
+ for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
+ try (
+ Connection connection = entry.getValue().getConnection();
+ Statement statement = connection.createStatement()) {
+ return loadPrimaryDatabaseInstance(statement);
+ } 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(SHOW_SLAVE_STATUS)) {
if (resultSet.next()) {
String masterHost = resultSet.getString("Master_Host");
String masterPort = resultSet.getString("Master_Port");
if (null != masterHost && null != masterPort) {
- return Optional.of(String.format("%s:%s", masterHost, masterPort));
+ return Optional.of(new IPPortPrimaryDatabaseInstance(masterHost, masterPort));
}
}
return Optional.empty();
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 4fe9942295c..e020ad70842 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
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.dbdiscovery.mysql.type.mgr;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.type.IPPortPrimaryDatabaseInstance;
import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.junit.Test;
@@ -96,9 +97,9 @@ public final class MGRDatabaseDiscoveryProviderAlgorithmTest {
dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
}
databaseDiscoveryType.getProps().setProperty("group-name", "group_name");
- Optional<String> actual = databaseDiscoveryType.findPrimaryDataSourceName(dataSourceMap);
+ Optional<IPPortPrimaryDatabaseInstance> actual = databaseDiscoveryType.findPrimaryInstance(dataSourceMap);
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("ds_2"));
+ assertThat(actual.get().toString(), is("127.0.0.1:3308"));
}
@Test
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 51ec3c3f4ff..637db84838a 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,6 +17,7 @@
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;
@@ -38,7 +39,7 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
@Test
public void assertLoadHighlyAvailableStatus() throws SQLException {
MySQLNormalReplicationHighlyAvailableStatus actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().loadHighlyAvailableStatus(mockDataSource(3306));
- assertThat(actual.getPrimaryInstanceURL(), is("127.0.0.1:3306"));
+ assertThat(actual.getDatabaseInstance().toString(), is("127.0.0.1:3306"));
}
@Test
@@ -46,9 +47,9 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
dataSourceMap.put("ds_0", mockDataSource(3306));
dataSourceMap.put("ds_1", mockDataSource(3307));
- Optional<String> actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryDataSourceName(dataSourceMap);
+ Optional<IPPortPrimaryDatabaseInstance> actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance(dataSourceMap);
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("ds_0"));
+ assertThat(actual.get().toString(), is("127.0.0.1:3306"));
}
private DataSource mockDataSource(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/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 7cd3a09edcb..65b120645fd 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,6 +21,7 @@ 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;
@@ -58,14 +59,14 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm
}
@Override
- public Optional<String> findPrimaryDataSourceName(final Map<String, DataSource> dataSourceMap) {
+ public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
try (
Connection connection = entry.getValue().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(entry.getKey());
+ return Optional.of(new NamedPrimaryDatabaseInstance(entry.getKey()));
}
} catch (final SQLException ex) {
log.error("An exception occurred while find primary data source url", ex);
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/OpenGaussNormalReplicationHighlyAvailableStatus.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicat [...]
index 39726087df3..0698fd4ff26 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationHighlyAvailableStatus.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-provider/shardingsphere-db-discovery-opengauss/src/main/java/org/apache/shardingsphere/dbdiscovery/opengauss/replication/OpenGaussNormalReplicationHighlyAvailableStatus.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.dbdiscovery.opengauss.replication;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.spi.status.RoleSeparatedHighlyAvailableStatus;
+import org.apache.shardingsphere.dbdiscovery.spi.status.type.RoleSeparatedHighlyAvailableStatus;
import javax.sql.DataSource;
import java.util.Map;
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 07eb9852c9a..a9283a8f955 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,6 +17,7 @@
package org.apache.shardingsphere.dbdiscovery.opengauss.replication;
+import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatabaseInstance;
import org.junit.Test;
import javax.sql.DataSource;
@@ -69,8 +70,8 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmT
for (int i = 0; i < 3; i++) {
dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
}
- Optional<String> actual = new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().findPrimaryDataSourceName(dataSourceMap);
+ Optional<NamedPrimaryDatabaseInstance> actual = new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance(dataSourceMap);
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("ds_2"));
+ assertThat(actual.get().getDataSourceName(), is("ds_2"));
}
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/dbdiscovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/dbdiscovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java
index c86e6b09040..7d27fe4640c 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/dbdiscovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-spring/shardingsphere-db-discovery-spring-namespace/src/test/java/org/apache/shardingsphere/dbdiscovery/spring/namespace/DatabaseDiscoverySpringNamespaceTest.java
@@ -38,9 +38,6 @@ import static org.junit.Assert.assertThat;
@ContextConfiguration(locations = "classpath:META-INF/spring/database-discovery-application-context.xml")
public final class DatabaseDiscoverySpringNamespaceTest extends AbstractJUnit4SpringContextTests {
- @Resource
- private DatabaseDiscoveryProviderAlgorithm mgrDatabaseDiscoveryProviderAlgorithm;
-
@Resource
private AlgorithmProvidedDatabaseDiscoveryRuleConfiguration mgrDatabaseDiscoveryRule;