You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2022/04/24 13:57:29 UTC
[shardingsphere] branch master updated: Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17065)
This is an automated email from the ASF dual-hosted git repository.
panjuan 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 48556095a22 Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17065)
48556095a22 is described below
commit 48556095a2298ecacab1a06aaef94f764e5346ee
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Apr 24 21:57:20 2022 +0800
Refactor DatabaseDiscoveryProviderAlgorithm.findPrimaryInstance() (#17065)
---
docs/document/content/dev-manual/ha.cn.md | 18 ++---
docs/document/content/dev-manual/ha.en.md | 18 ++---
.../spi/DatabaseDiscoveryProviderAlgorithm.java | 12 ++--
.../algorithm/DatabaseDiscoveryEngine.java | 9 ++-
...eFixtureDatabaseDiscoveryProviderAlgorithm.java | 4 +-
.../DatabaseDiscoveryRuleStatementConverter.java | 12 ++--
...AlterDatabaseDiscoveryTypeStatementUpdater.java | 10 +--
...reateDatabaseDiscoveryTypeStatementUpdater.java | 11 +--
...LFixtureDatabaseDiscoveryProviderAlgorithm.java | 5 +-
...overyProviderAlgorithmStatementUpdaterTest.java | 35 +++++-----
...overyProviderAlgorithmStatementUpdaterTest.java | 30 ++++----
.../DatabaseDiscoveryDistSQLStatementVisitor.java | 8 +--
...DatabaseDiscoveryProviderAlgorithmSegment.java} | 8 +--
.../AlterDatabaseDiscoveryTypeStatement.java | 4 +-
.../CreateDatabaseDiscoveryTypeStatement.java | 4 +-
...MGRMySQLDatabaseDiscoveryProviderAlgorithm.java | 18 ++---
...ionMySQLDatabaseDiscoveryProviderAlgorithm.java | 18 ++---
.../MGRDatabaseDiscoveryProviderAlgorithmTest.java | 80 ++++++++--------------
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 13 ++--
...licationDatabaseDiscoveryProviderAlgorithm.java | 22 +++---
...tionDatabaseDiscoveryProviderAlgorithmTest.java | 52 ++++----------
.../AlterDatabaseDiscoveryTypeStatementAssert.java | 11 +--
...CreateDatabaseDiscoveryTypeStatementAssert.java | 11 +--
23 files changed, 181 insertions(+), 232 deletions(-)
diff --git a/docs/document/content/dev-manual/ha.cn.md b/docs/document/content/dev-manual/ha.cn.md
index 53c0235f475..24f844060db 100644
--- a/docs/document/content/dev-manual/ha.cn.md
+++ b/docs/document/content/dev-manual/ha.cn.md
@@ -5,14 +5,14 @@ weight = 9
chapter = true
+++
-## DatabaseDiscoveryType
+## DatabaseDiscoveryProviderAlgorithm
-| *SPI 名称* | *详细说明* |
-| ----------------------------------------------- | ----------------------------- |
-| DatabaseDiscoveryType | 数据库发现类型 |
+| *SPI 名称* | *详细说明* |
+| ------------------------------------------------------------ | -------------------------------- |
+| DatabaseDiscoveryProviderAlgorithm | 数据库发现算法 |
-| *已知实现类* | *详细说明* |
-| ----------------------------------------------- | ------------------------------ |
-| MGRDatabaseDiscoveryType | 基于 MySQL MGR 的数据库发现 |
-| MySQLNormalReplicationDatabaseDiscoveryType | 基于 MySQL 主从同步的数据库发现 |
-| OpenGaussNormalReplicationDatabaseDiscoveryType | 基于 openGauss 主从同步的数据库发现 |
+| *已知实现类* | *详细说明* |
+| ------------------------------------------------------------ | --------------------------------- |
+| MGRDatabaseDiscoveryProviderAlgorithm | 基于 MySQL MGR 的数据库发现算法 |
+| MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm | 基于 MySQL 主从同步的数据库发现算法 |
+| OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm | 基于 openGauss 主从同步的数据库发现算法 |
diff --git a/docs/document/content/dev-manual/ha.en.md b/docs/document/content/dev-manual/ha.en.md
index 9b9fae23227..ba9fba68434 100644
--- a/docs/document/content/dev-manual/ha.en.md
+++ b/docs/document/content/dev-manual/ha.en.md
@@ -5,14 +5,14 @@ weight = 9
chapter = true
+++
-## DatabaseDiscoveryType
+## DatabaseDiscoveryProviderAlgorithm
-| *SPI Name* | *Description* |
-| ----------------------------------------------- | --------------------------------------------- |
-| DatabaseDiscoveryType | Database discovery type |
+| *SPI Name* | *Description* |
+| ------------------------------------------------------------ | ---------------------------------------------------------------- |
+| DatabaseDiscoveryProviderAlgorithm | Database discovery provider algorithm |
-| *Implementation Class* | *Description* |
-| ----------------------------------------------- | --------------------------------------------- |
-| MGRDatabaseDiscoveryType | Database discovery of MySQL's MGR |
-| MySQLNormalReplicationDatabaseDiscoveryType | Database discovery of MySQL's replication |
-| OpenGaussNormalReplicationDatabaseDiscoveryType | Database discovery of openGauss's replication |
+| *Implementation Class* | *Description* |
+| ------------------------------------------------------------ | ---------------------------------------------------------------- |
+| MGRDatabaseDiscoveryProviderAlgorithm | Database discovery provider algorithm of MySQL's MGR |
+| MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithm | Database discovery provider algorithm of MySQL's replication |
+| OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm | Database discovery provider algorithm of openGauss's replication |
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 1c0cf7f4722..8c2a56c9acf 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
@@ -24,7 +24,6 @@ import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import javax.sql.DataSource;
import java.sql.SQLException;
-import java.util.Map;
import java.util.Optional;
/**
@@ -42,12 +41,13 @@ public interface DatabaseDiscoveryProviderAlgorithm extends ShardingSphereAlgori
HighlyAvailableStatus loadHighlyAvailableStatus(DataSource dataSource) throws SQLException;
/**
- * Find primary instance.
- *
- * @param dataSourceMap data source map
- * @return found primary instance
+ * Find primary database instance.
+ *
+ * @param dataSourceName data source name
+ * @param dataSource data source
+ * @return found primary database instance
*/
- Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(Map<String, DataSource> dataSourceMap);
+ Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(String dataSourceName, 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 f953d2fae95..8f0ab887707 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
@@ -112,8 +112,13 @@ public final class DatabaseDiscoveryEngine {
}
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();
+ 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) {
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 d86b2a2c7ce..f7c317e499c 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.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;
@@ -25,7 +26,6 @@ import org.apache.shardingsphere.infra.storage.StorageNodeRole;
import org.apache.shardingsphere.infra.storage.StorageNodeStatus;
import javax.sql.DataSource;
-import java.util.Map;
import java.util.Optional;
import static org.mockito.Mockito.mock;
@@ -38,7 +38,7 @@ public final class CoreFixtureDatabaseDiscoveryProviderAlgorithm implements Data
}
@Override
- public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
+ public Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
return Optional.of(new NamedPrimaryDatabaseInstance("primary"));
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/converter/DatabaseDiscoveryRuleStatementConverter.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/converter/DatabaseDisco [...]
index fe024b6bded..d5cbf3fde18 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/converter/DatabaseDiscoveryRuleStatementConverter.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/converter/DatabaseDiscoveryRuleStatementConverter.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.AbstractData
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryConstructionSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryDefinitionSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryHeartbeatSegment;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import java.util.Collection;
@@ -94,15 +94,15 @@ public final class DatabaseDiscoveryRuleStatementConverter {
}
/**
- * Convert database discovery type segment to database discovery heartbeat configuration.
+ * Convert database discovery provider algorithm segment to database discovery heartbeat configuration.
*
- * @param typeSegment database discovery type segments
+ * @param algorithmSegments database discovery provider algorithm segments
* @return database discovery type configuration
*/
- public static DatabaseDiscoveryRuleConfiguration convertDiscoveryType(final Collection<DatabaseDiscoveryTypeSegment> typeSegment) {
+ public static DatabaseDiscoveryRuleConfiguration convertDiscoveryProviderAlgorithm(final Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments) {
final DatabaseDiscoveryRuleConfiguration result = new DatabaseDiscoveryRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>(), new LinkedHashMap<>());
- typeSegment.forEach(each -> result.getDiscoveryTypes().put(each.getDiscoveryTypeName(),
- new ShardingSphereAlgorithmConfiguration(each.getAlgorithmSegment().getName(), each.getAlgorithmSegment().getProps())));
+ algorithmSegments.forEach(each -> result.getDiscoveryTypes().put(each.getDiscoveryProviderName(),
+ new ShardingSphereAlgorithmConfiguration(each.getAlgorithm().getName(), each.getAlgorithm().getProps())));
return result;
}
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryTypeStatementUpdater.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDis [...]
index 61e64510e79..8110b157af7 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryTypeStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/AlterDatabaseDiscoveryTypeStatementUpdater.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.distsql.handler.converter.DatabaseDiscoveryRuleStatementConverter;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
@@ -62,7 +62,7 @@ public final class AlterDatabaseDiscoveryTypeStatementUpdater implements RuleDef
private void checkNotExistDiscoveryType(final String databaseName, final AlterDatabaseDiscoveryTypeStatement sqlStatement,
final DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
Collection<String> existTypes = currentRuleConfig.getDiscoveryTypes().keySet();
- Collection<String> notExistTypes = sqlStatement.getTypes().stream().map(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName)
+ Collection<String> notExistTypes = sqlStatement.getProviders().stream().map(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName)
.filter(each -> !existTypes.contains(each)).collect(Collectors.toSet());
DistSQLException.predictionThrow(notExistTypes.isEmpty(), () -> new RequiredRuleMissedException(RULE_TYPE, databaseName));
@@ -78,19 +78,19 @@ public final class AlterDatabaseDiscoveryTypeStatementUpdater implements RuleDef
}
private Collection<String> getToBeAlteredDuplicateTypeNames(final AlterDatabaseDiscoveryTypeStatement sqlStatement) {
- return sqlStatement.getTypes().stream().collect(Collectors.toMap(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName, e -> 1, Integer::sum))
+ return sqlStatement.getProviders().stream().collect(Collectors.toMap(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName, each -> 1, Integer::sum))
.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet());
}
private void checkInvalidDiscoverType(final AlterDatabaseDiscoveryTypeStatement sqlStatement) throws DistSQLException {
- Collection<String> invalidType = sqlStatement.getTypes().stream().map(each -> each.getAlgorithmSegment().getName()).distinct()
+ Collection<String> invalidType = sqlStatement.getProviders().stream().map(each -> each.getAlgorithm().getName()).distinct()
.filter(each -> !TypedSPIRegistry.findRegisteredService(DatabaseDiscoveryProviderAlgorithm.class, each, new Properties()).isPresent()).collect(Collectors.toList());
DistSQLException.predictionThrow(invalidType.isEmpty(), () -> new InvalidAlgorithmConfigurationException(RULE_TYPE, invalidType));
}
@Override
public RuleConfiguration buildToBeAlteredRuleConfiguration(final AlterDatabaseDiscoveryTypeStatement sqlStatement) {
- return DatabaseDiscoveryRuleStatementConverter.convertDiscoveryType(sqlStatement.getTypes());
+ return DatabaseDiscoveryRuleStatementConverter.convertDiscoveryProviderAlgorithm(sqlStatement.getProviders());
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/CreateDatabaseDiscoveryTypeStatementUpdater.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/CreateDatabaseD [...]
index 18fdbebf852..3a8909002b8 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/CreateDatabaseDiscoveryTypeStatementUpdater.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/CreateDatabaseDiscoveryTypeStatementUpdater.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.distsql.handler.converter.DatabaseDiscoveryRuleStatementConverter;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CreateDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.infra.config.RuleConfiguration;
@@ -62,25 +62,26 @@ public final class CreateDatabaseDiscoveryTypeStatementUpdater implements RuleDe
return;
}
Collection<String> existRuleNames = currentRuleConfig.getDiscoveryTypes().keySet();
- Collection<String> duplicateRuleNames = sqlStatement.getTypes().stream().map(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName).filter(existRuleNames::contains).collect(Collectors.toSet());
+ Collection<String> duplicateRuleNames = sqlStatement.getProviders()
+ .stream().map(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName).filter(existRuleNames::contains).collect(Collectors.toSet());
duplicateRuleNames.addAll(getToBeCreatedDuplicateRuleNames(sqlStatement));
DistSQLException.predictionThrow(duplicateRuleNames.isEmpty(), () -> new DuplicateRuleException(RULE_TYPE, databaseName, duplicateRuleNames));
}
private Collection<String> getToBeCreatedDuplicateRuleNames(final CreateDatabaseDiscoveryTypeStatement sqlStatement) {
- return sqlStatement.getTypes().stream().collect(Collectors.toMap(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName, e -> 1, Integer::sum))
+ return sqlStatement.getProviders().stream().collect(Collectors.toMap(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName, each -> 1, Integer::sum))
.entrySet().stream().filter(entry -> entry.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet());
}
private void checkInvalidDiscoverType(final CreateDatabaseDiscoveryTypeStatement sqlStatement) throws DistSQLException {
- Collection<String> invalidType = sqlStatement.getTypes().stream().map(each -> each.getAlgorithmSegment().getName()).distinct()
+ Collection<String> invalidType = sqlStatement.getProviders().stream().map(each -> each.getAlgorithm().getName()).distinct()
.filter(each -> !TypedSPIRegistry.findRegisteredService(DatabaseDiscoveryProviderAlgorithm.class, each, new Properties()).isPresent()).collect(Collectors.toList());
DistSQLException.predictionThrow(invalidType.isEmpty(), () -> new InvalidAlgorithmConfigurationException(RULE_TYPE, invalidType));
}
@Override
public RuleConfiguration buildToBeCreatedRuleConfiguration(final CreateDatabaseDiscoveryTypeStatement sqlStatement) {
- return DatabaseDiscoveryRuleStatementConverter.convertDiscoveryType(sqlStatement.getTypes());
+ return DatabaseDiscoveryRuleStatementConverter.convertDiscoveryProviderAlgorithm(sqlStatement.getProviders());
}
@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 3d5d96c4110..02a5f4152ee 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,13 @@
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.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.Map;
import java.util.Optional;
import static org.mockito.Mockito.mock;
@@ -38,7 +37,7 @@ public final class DistSQLFixtureDatabaseDiscoveryProviderAlgorithm implements D
}
@Override
- public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final Map<String, DataSource> dataSourceMap) {
+ public Optional<? extends PrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
return Optional.empty();
}
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/update/AlterDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.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/update [...]
index ccd00d8b250..069fd693cdd 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/update/AlterDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.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/update/AlterDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
@@ -36,6 +36,7 @@ import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
@@ -61,26 +62,27 @@ public final class AlterDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest {
@Test(expected = DuplicateRuleException.class)
public void assertCheckSQLStatementWithDuplicate() throws DistSQLException {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = new DatabaseDiscoveryDataSourceRuleConfiguration("readwrite_ds", Collections.emptyList(), "ha-heartbeat", "test");
- List<DatabaseDiscoveryTypeSegment> databaseDiscoveryTypeSegments = Arrays.asList(
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())),
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(databaseDiscoveryTypeSegments),
+ List<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Arrays.asList(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())),
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(algorithmSegments),
new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceRuleConfig), Collections.emptyMap(), Collections.emptyMap()));
}
@Test(expected = RequiredRuleMissedException.class)
public void assertCheckSQLStatementWithNotExist() throws DistSQLException {
- List<DatabaseDiscoveryTypeSegment> databaseDiscoveryTypeSegments =
- Collections.singletonList(new DatabaseDiscoveryTypeSegment("discovery_type_1", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(databaseDiscoveryTypeSegments),
+ List<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments =
+ Collections.singletonList(new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type_1", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(algorithmSegments),
new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap(),
Collections.singletonMap("discovery_type", new ShardingSphereAlgorithmConfiguration("DISTSQL.FIXTURE", new Properties()))));
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertCheckSQLStatementWithDatabaseDiscoveryType() throws DistSQLException {
- Set<DatabaseDiscoveryTypeSegment> discoveryTypeSegments = Collections.singleton(new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("INVALID_TYPE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(discoveryTypeSegments),
+ Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Collections.singleton(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("INVALID_TYPE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(algorithmSegments),
new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap(),
Collections.singletonMap("discovery_type", new ShardingSphereAlgorithmConfiguration("DISTSQL.FIXTURE", new Properties()))));
}
@@ -89,16 +91,17 @@ public final class AlterDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest {
public void assertBuildAndUpdate() throws DistSQLException {
Properties currentProperties = new Properties();
currentProperties.put("key", "value");
- DatabaseDiscoveryRuleConfiguration currentRuleConfiguration = new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap(),
+ DatabaseDiscoveryRuleConfiguration currentRuleConfig = new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), Collections.emptyMap(),
Collections.singletonMap("discovery_type", new ShardingSphereAlgorithmConfiguration("DISTSQL.FIXTURE", currentProperties)));
Properties properties = new Properties();
properties.put("key", "value_1");
- Set<DatabaseDiscoveryTypeSegment> discoveryTypeSegments = Collections.singleton(new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", properties)));
- updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(discoveryTypeSegments), currentRuleConfiguration);
- DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration =
- (DatabaseDiscoveryRuleConfiguration) updater.buildToBeAlteredRuleConfiguration(new AlterDatabaseDiscoveryTypeStatement(discoveryTypeSegments));
+ Set<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Collections.singleton(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", properties)));
+ updater.checkSQLStatement(shardingSphereMetaData, new AlterDatabaseDiscoveryTypeStatement(algorithmSegments), currentRuleConfig);
+ DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfig =
+ (DatabaseDiscoveryRuleConfiguration) updater.buildToBeAlteredRuleConfiguration(new AlterDatabaseDiscoveryTypeStatement(algorithmSegments));
DatabaseDiscoveryRuleConfiguration currentConfiguration = new DatabaseDiscoveryRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>(), new LinkedHashMap<>());
- updater.updateCurrentRuleConfiguration(currentConfiguration, databaseDiscoveryRuleConfiguration);
+ updater.updateCurrentRuleConfiguration(currentConfiguration, databaseDiscoveryRuleConfig);
assertThat(currentConfiguration.getDiscoveryTypes().size(), is(1));
assertThat(currentConfiguration.getDiscoveryTypes().get("discovery_type").getType(), is("DISTSQL.FIXTURE"));
assertThat(currentConfiguration.getDiscoveryTypes().get("discovery_type").getProps().get("key"), is("value_1"));
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/update/CreateDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.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/updat [...]
index 6d3f664bf0f..8e941416038 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/update/CreateDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.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/update/CreateDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
import org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CreateDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.dbdiscovery.spi.DatabaseDiscoveryProviderAlgorithm;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
@@ -59,37 +59,37 @@ public final class CreateDatabaseDiscoveryProviderAlgorithmStatementUpdaterTest
@Test(expected = DuplicateRuleException.class)
public void assertCheckSQLStatementWithDuplicate() throws DistSQLException {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = new DatabaseDiscoveryDataSourceRuleConfiguration("readwrite_ds", Collections.emptyList(), "ha-heartbeat", "test");
- Collection<DatabaseDiscoveryTypeSegment> databaseDiscoveryTypeSegments = Arrays.asList(
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())),
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(databaseDiscoveryTypeSegments),
+ Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Arrays.asList(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())),
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(algorithmSegments),
new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceRuleConfig), Collections.emptyMap(), Collections.emptyMap()));
}
@Test(expected = DuplicateRuleException.class)
public void assertCheckSQLStatementWithExist() throws DistSQLException {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = new DatabaseDiscoveryDataSourceRuleConfiguration("readwrite_ds", Collections.emptyList(), "ha-heartbeat", "test");
- Collection<DatabaseDiscoveryTypeSegment> databaseDiscoveryTypeSegments = Collections.singletonList(
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(databaseDiscoveryTypeSegments),
+ Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Collections.singletonList(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(algorithmSegments),
new DatabaseDiscoveryRuleConfiguration(Collections.singleton(dataSourceRuleConfig), Collections.emptyMap(),
Collections.singletonMap("discovery_type", new ShardingSphereAlgorithmConfiguration("DISTSQL.FIXTURE", new Properties()))));
}
@Test(expected = InvalidAlgorithmConfigurationException.class)
public void assertCheckSQLStatementWithDatabaseDiscoveryType() throws DistSQLException {
- Collection<DatabaseDiscoveryTypeSegment> discoveryTypeSegments = Collections.singleton(
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("INVALID_TYPE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(discoveryTypeSegments), null);
+ Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Collections.singleton(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("INVALID_TYPE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(algorithmSegments), null);
}
@Test
public void assertBuildAndUpdate() throws DistSQLException {
- Collection<DatabaseDiscoveryTypeSegment> discoveryTypeSegments = Collections.singleton(
- new DatabaseDiscoveryTypeSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
- updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(discoveryTypeSegments), null);
+ Collection<DatabaseDiscoveryProviderAlgorithmSegment> algorithmSegments = Collections.singleton(
+ new DatabaseDiscoveryProviderAlgorithmSegment("discovery_type", new AlgorithmSegment("DISTSQL.FIXTURE", new Properties())));
+ updater.checkSQLStatement(shardingSphereMetaData, new CreateDatabaseDiscoveryTypeStatement(algorithmSegments), null);
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfig =
- (DatabaseDiscoveryRuleConfiguration) updater.buildToBeCreatedRuleConfiguration(new CreateDatabaseDiscoveryTypeStatement(discoveryTypeSegments));
+ (DatabaseDiscoveryRuleConfiguration) updater.buildToBeCreatedRuleConfiguration(new CreateDatabaseDiscoveryTypeStatement(algorithmSegments));
DatabaseDiscoveryRuleConfiguration currentConfig = new DatabaseDiscoveryRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>(), new LinkedHashMap<>());
updater.updateCurrentRuleConfiguration(currentConfig, databaseDiscoveryRuleConfig);
assertThat(currentConfig.getDiscoveryTypes().size(), is(1));
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLSt [...]
index f5d93d0bcb4..2471a0299a7 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.AbstractData
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryConstructionSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryDefinitionSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryHeartbeatSegment;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryHeartbeatStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryTypeStatement;
@@ -141,13 +141,13 @@ public final class DatabaseDiscoveryDistSQLStatementVisitor extends DatabaseDisc
return new AlterDatabaseDiscoveryTypeStatement(buildAlgorithmEntry(ctx.databaseDiscoveryTypeDefinition()));
}
- private Collection<DatabaseDiscoveryTypeSegment> buildAlgorithmEntry(final List<DatabaseDiscoveryTypeDefinitionContext> ctx) {
- return ctx.stream().map(each -> (DatabaseDiscoveryTypeSegment) visit(each)).collect(Collectors.toList());
+ private Collection<DatabaseDiscoveryProviderAlgorithmSegment> buildAlgorithmEntry(final List<DatabaseDiscoveryTypeDefinitionContext> ctx) {
+ return ctx.stream().map(each -> (DatabaseDiscoveryProviderAlgorithmSegment) visit(each)).collect(Collectors.toList());
}
@Override
public ASTNode visitDatabaseDiscoveryTypeDefinition(final DatabaseDiscoveryTypeDefinitionContext ctx) {
- return new DatabaseDiscoveryTypeSegment(getIdentifierValue(ctx.discoveryTypeName()), (AlgorithmSegment) visit(ctx.typeDefinition()));
+ return new DatabaseDiscoveryProviderAlgorithmSegment(getIdentifierValue(ctx.discoveryTypeName()), (AlgorithmSegment) visit(ctx.typeDefinition()));
}
@Override
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryTypeSegment.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryProviderA [...]
similarity index 83%
rename from shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryTypeSegment.java
rename to shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryProviderAlgorithmSegment.java
index 917654d85b4..8db8aaefb8d 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryTypeSegment.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/segment/DatabaseDiscoveryProviderAlgorithmSegment.java
@@ -23,13 +23,13 @@ import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
/**
- * Database discovery type segment.
+ * Database discovery provider algorithm segment.
*/
@RequiredArgsConstructor
@Getter
-public final class DatabaseDiscoveryTypeSegment implements ASTNode {
+public final class DatabaseDiscoveryProviderAlgorithmSegment implements ASTNode {
- private final String discoveryTypeName;
+ private final String discoveryProviderName;
- private final AlgorithmSegment algorithmSegment;
+ private final AlgorithmSegment algorithm;
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/AlterDatabaseDiscoveryTypeStatement.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/AlterDatabaseDi [...]
index 0d181c911fe..2418fd358ea 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/AlterDatabaseDiscoveryTypeStatement.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/AlterDatabaseDiscoveryTypeStatement.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.parser.statement;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.parser.subject.impl.DatabaseDiscoverySubjectSupplier;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.alter.AlterRuleStatement;
import java.util.Collection;
@@ -32,5 +32,5 @@ import java.util.Collection;
@Getter
public final class AlterDatabaseDiscoveryTypeStatement extends AlterRuleStatement implements DatabaseDiscoverySubjectSupplier {
- private final Collection<DatabaseDiscoveryTypeSegment> types;
+ private final Collection<DatabaseDiscoveryProviderAlgorithmSegment> providers;
}
diff --git a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/CreateDatabaseDiscoveryTypeStatement.java b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/CreateDatabase [...]
index 09b667fd73f..4f105204108 100644
--- a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/CreateDatabaseDiscoveryTypeStatement.java
+++ b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/CreateDatabaseDiscoveryTypeStatement.java
@@ -20,7 +20,7 @@ package org.apache.shardingsphere.dbdiscovery.distsql.parser.statement;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.parser.subject.impl.DatabaseDiscoverySubjectSupplier;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.distsql.parser.statement.rdl.create.CreateRuleStatement;
import java.util.Collection;
@@ -32,5 +32,5 @@ import java.util.Collection;
@Getter
public final class CreateDatabaseDiscoveryTypeStatement extends CreateRuleStatement implements DatabaseDiscoverySubjectSupplier {
- private final Collection<DatabaseDiscoveryTypeSegment> types;
+ private final Collection<DatabaseDiscoveryProviderAlgorithmSegment> providers;
}
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 e2a77ad6b8e..cf7bf2f0d2e 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,8 +35,6 @@ 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;
@@ -101,15 +99,13 @@ public final class MGRMySQLDatabaseDiscoveryProviderAlgorithm implements Databas
}
@Override
- 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);
- }
+ public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
+ try (
+ Connection connection = dataSource.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();
}
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 cf3a19d10df..4b43f83aaae 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
@@ -31,8 +31,6 @@ 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;
@@ -58,15 +56,13 @@ public final class MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm
}
@Override
- 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);
- }
+ public Optional<IPPortPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, final DataSource dataSource) {
+ try (
+ Connection connection = dataSource.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();
}
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 59281c78574..b4143b39301 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
@@ -22,16 +22,9 @@ import org.apache.shardingsphere.infra.storage.StorageNodeDataSource;
import org.junit.Test;
import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.HashMap;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -45,22 +38,9 @@ import static org.mockito.Mockito.when;
public final class MGRDatabaseDiscoveryProviderAlgorithmTest {
- private final MGRMySQLDatabaseDiscoveryProviderAlgorithm databaseDiscoveryType = new MGRMySQLDatabaseDiscoveryProviderAlgorithm();
-
@Test
public void assertLoadHighlyAvailableStatus() throws SQLException {
- DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
- ResultSet resultSet = mock(ResultSet.class);
- 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.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(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);
+ MGRHighlyAvailableStatus actual = new MGRMySQLDatabaseDiscoveryProviderAlgorithm().loadHighlyAvailableStatus(mockToBeLoadedHighlyAvailableStatusDataSource());
assertTrue(actual.isPluginActive());
assertTrue(actual.isSinglePrimaryMode());
assertThat(actual.getGroupName(), is("group_name"));
@@ -70,47 +50,45 @@ public final class MGRDatabaseDiscoveryProviderAlgorithmTest {
assertFalse(databaseInstances.hasNext());
}
+ private DataSource mockToBeLoadedHighlyAvailableStatusDataSource() throws SQLException {
+ DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+ ResultSet resultSet = mock(ResultSet.class);
+ when(result.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.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(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
+ return result;
+ }
+
@Test
public void assertFindPrimaryDataSource() throws SQLException {
- List<DataSource> dataSources = new LinkedList<>();
- List<Connection> connections = new LinkedList<>();
- List<Statement> statements = new LinkedList<>();
- List<ResultSet> resultSets = new LinkedList<>();
- List<DatabaseMetaData> databaseMetaData = new LinkedList<>();
- for (int i = 0; i < 3; i++) {
- dataSources.add(mock(DataSource.class));
- connections.add(mock(Connection.class));
- statements.add(mock(Statement.class));
- resultSets.add(mock(ResultSet.class));
- databaseMetaData.add(mock(DatabaseMetaData.class));
- }
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')";
- for (int i = 0; i < 3; i++) {
- when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
- when(connections.get(i).createStatement()).thenReturn(statements.get(i));
- when(statements.get(i).executeQuery(sql)).thenReturn(resultSets.get(i));
- when(resultSets.get(i).next()).thenReturn(true, false);
- when(resultSets.get(i).getString("MEMBER_HOST")).thenReturn("127.0.0.1");
- when(resultSets.get(i).getString("MEMBER_PORT")).thenReturn(Integer.toString(3306 + i));
- when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
- when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:mysql://127.0.0.1:" + (3306 + i) + "/ds_0?serverTimezone=UTC&useSSL=false");
- }
- Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
- for (int i = 0; i < 3; i++) {
- dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
- }
- databaseDiscoveryType.getProps().setProperty("group-name", "group_name");
- Optional<IPPortPrimaryDatabaseInstance> actual = databaseDiscoveryType.findPrimaryInstance(dataSourceMap);
+ Optional<IPPortPrimaryDatabaseInstance> actual = new MGRMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("foo_ds", mockToBeFoundPrimaryDataSource(sql));
assertTrue(actual.isPresent());
- assertThat(actual.get().toString(), is("127.0.0.1:3308"));
+ assertThat(actual.get().toString(), is("127.0.0.1:3306"));
+ }
+
+ private DataSource mockToBeFoundPrimaryDataSource(final String sql) throws SQLException {
+ DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+ ResultSet resultSet = mock(ResultSet.class);
+ when(result.getConnection().createStatement().executeQuery(sql)).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false);
+ when(resultSet.getString("MEMBER_HOST")).thenReturn("127.0.0.1");
+ when(resultSet.getString("MEMBER_PORT")).thenReturn("3306");
+ when(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
+ return result;
}
@Test
public void assertGetDisabledStorageNodeDataSource() throws SQLException {
DataSource dataSource = mock(DataSource.class, RETURNS_DEEP_STUBS);
when(dataSource.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
- StorageNodeDataSource actual = databaseDiscoveryType.getStorageNodeDataSource(dataSource);
+ StorageNodeDataSource actual = new MGRMySQLDatabaseDiscoveryProviderAlgorithm().getStorageNodeDataSource(dataSource);
assertThat(actual.getRole(), is("member"));
assertThat(actual.getStatus(), is("disabled"));
assertThat(actual.getReplicationDelayMilliseconds(), is(0L));
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 637db84838a..1540d3b5b3b 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
@@ -23,8 +23,6 @@ import org.junit.Test;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.util.HashMap;
-import java.util.Map;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
@@ -38,28 +36,25 @@ public final class MySQLNormalReplicationDatabaseDiscoveryProviderAlgorithmTest
@Test
public void assertLoadHighlyAvailableStatus() throws SQLException {
- MySQLNormalReplicationHighlyAvailableStatus actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().loadHighlyAvailableStatus(mockDataSource(3306));
+ MySQLNormalReplicationHighlyAvailableStatus actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().loadHighlyAvailableStatus(mockDataSource());
assertThat(actual.getDatabaseInstance().toString(), is("127.0.0.1:3306"));
}
@Test
public void assertFindPrimaryDataSource() throws SQLException {
- Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
- dataSourceMap.put("ds_0", mockDataSource(3306));
- dataSourceMap.put("ds_1", mockDataSource(3307));
- Optional<IPPortPrimaryDatabaseInstance> actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance(dataSourceMap);
+ Optional<IPPortPrimaryDatabaseInstance> actual = new MySQLNormalReplicationMySQLDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("ds_0", mockDataSource());
assertTrue(actual.isPresent());
assertThat(actual.get().toString(), is("127.0.0.1:3306"));
}
- private DataSource mockDataSource(final int port) throws SQLException {
+ private DataSource mockDataSource() throws SQLException {
DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
ResultSet resultSet = mock(ResultSet.class);
when(result.getConnection().createStatement().executeQuery("SHOW SLAVE STATUS")).thenReturn(resultSet);
when(resultSet.next()).thenReturn(true, false);
when(resultSet.getString("Master_Host")).thenReturn("127.0.0.1");
when(resultSet.getString("Master_Port")).thenReturn("3306");
- when(result.getConnection().getMetaData().getURL()).thenReturn(String.format("jdbc:mysql://127.0.0.1:%s/test?serverTimezone=UTC&useSSL=false", port));
+ when(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:mysql://127.0.0.1:3306/foo_ds");
return result;
}
}
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 65b120645fd..96e03898343 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
@@ -31,8 +31,6 @@ 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;
@@ -59,18 +57,16 @@ public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm
}
@Override
- 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(new NamedPrimaryDatabaseInstance(entry.getKey()));
- }
- } catch (final SQLException ex) {
- log.error("An exception occurred while find primary data source url", ex);
+ public Optional<NamedPrimaryDatabaseInstance> findPrimaryInstance(final String dataSourceName, 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));
}
+ } catch (final SQLException ex) {
+ log.error("An exception occurred while find primary data source url", ex);
}
return Optional.empty();
}
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 a9283a8f955..0a03b4f9c77 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
@@ -21,57 +21,35 @@ import org.apache.shardingsphere.dbdiscovery.spi.instance.type.NamedPrimaryDatab
import org.junit.Test;
import javax.sql.DataSource;
-import java.sql.Connection;
-import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
-import java.sql.Statement;
-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.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public final class OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithmTest {
- private static final String DB_ROLE = "SELECT local_role,db_state FROM pg_stat_get_stream_replications()";
-
@Test
public void assertFindPrimaryDataSource() throws SQLException {
- List<DataSource> dataSources = new LinkedList<>();
- List<Connection> connections = new LinkedList<>();
- List<Statement> statements = new LinkedList<>();
- List<ResultSet> resultSets = new LinkedList<>();
- List<DatabaseMetaData> databaseMetaData = new LinkedList<>();
- for (int i = 0; i < 3; i++) {
- dataSources.add(mock(DataSource.class));
- connections.add(mock(Connection.class));
- statements.add(mock(Statement.class));
- resultSets.add(mock(ResultSet.class));
- databaseMetaData.add(mock(DatabaseMetaData.class));
- }
- for (int i = 0; i < 3; i++) {
- when(dataSources.get(i).getConnection()).thenReturn(connections.get(i));
- when(connections.get(i).createStatement()).thenReturn(statements.get(i));
- when(statements.get(i).executeQuery(DB_ROLE)).thenReturn(resultSets.get(i));
- when(resultSets.get(i).next()).thenReturn(true, false);
- when(resultSets.get(i).getString("local_role")).thenReturn("Primary");
- when(resultSets.get(i).getString("db_state")).thenReturn("Normal");
- when(connections.get(i).getMetaData()).thenReturn(databaseMetaData.get(i));
- when(databaseMetaData.get(i).getURL()).thenReturn("jdbc:postgres://127.0.0.1:" + (3306 + i) + "/ds_0");
- }
- Map<String, DataSource> dataSourceMap = new HashMap<>(3, 1);
- for (int i = 0; i < 3; i++) {
- dataSourceMap.put(String.format("ds_%s", i), dataSources.get(i));
- }
- Optional<NamedPrimaryDatabaseInstance> actual = new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance(dataSourceMap);
+ DataSource dataSource = mockDatSource();
+ Optional<NamedPrimaryDatabaseInstance> actual = new OpenGaussNormalReplicationDatabaseDiscoveryProviderAlgorithm().findPrimaryInstance("foo_ds", dataSource);
assertTrue(actual.isPresent());
- assertThat(actual.get().getDataSourceName(), is("ds_2"));
+ assertThat(actual.get().getDataSourceName(), is("foo_ds"));
+ }
+
+ private DataSource mockDatSource() throws SQLException {
+ DataSource result = mock(DataSource.class, RETURNS_DEEP_STUBS);
+ ResultSet resultSet = mock(ResultSet.class);
+ when(result.getConnection().createStatement().executeQuery("SELECT local_role,db_state FROM pg_stat_get_stream_replications()")).thenReturn(resultSet);
+ when(resultSet.next()).thenReturn(true, false);
+ when(resultSet.getString("local_role")).thenReturn("Primary");
+ when(resultSet.getString("db_state")).thenReturn("Normal");
+ when(result.getConnection().getMetaData().getURL()).thenReturn("jdbc:postgres://127.0.0.1:3306/foo_ds");
+ return result;
}
}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/alter/impl/AlterDatabaseDiscoveryTypeStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/alter/impl/AlterDatabaseDiscoveryTypeStatementAssert.java
index 8175e26cdef..cf3ae25b3a4 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/alter/impl/AlterDatabaseDiscoveryTypeStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/alter/impl/AlterDatabaseDiscoveryTypeStatementAssert.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statemen
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.AlterDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.distsql.AlgorithmAssert;
@@ -51,12 +51,13 @@ public final class AlterDatabaseDiscoveryTypeStatementAssert {
assertNull(assertContext.getText("Actual statement should not exist."), actual);
} else {
assertNotNull(assertContext.getText("Actual statement should exist."), actual);
- Map<String, DatabaseDiscoveryTypeSegment> actualMap = actual.getTypes().stream().collect(Collectors.toMap(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName, each -> each));
+ Map<String, DatabaseDiscoveryProviderAlgorithmSegment> actualMap = actual.getProviders()
+ .stream().collect(Collectors.toMap(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName, each -> each));
expected.getTypes().forEach(each -> {
- DatabaseDiscoveryTypeSegment actualSegment = actualMap.get(each.getDiscoveryTypeName());
+ DatabaseDiscoveryProviderAlgorithmSegment actualSegment = actualMap.get(each.getDiscoveryTypeName());
assertNotNull(actualSegment);
- assertThat(actualSegment.getDiscoveryTypeName(), is(each.getDiscoveryTypeName()));
- AlgorithmAssert.assertIs(assertContext, actualSegment.getAlgorithmSegment(), each.getAlgorithmSegment());
+ assertThat(actualSegment.getDiscoveryProviderName(), is(each.getDiscoveryTypeName()));
+ AlgorithmAssert.assertIs(assertContext, actualSegment.getAlgorithm(), each.getAlgorithmSegment());
});
}
}
diff --git a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/create/impl/CreateDatabaseDiscoveryTypeStatementAssert.java b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/create/impl/CreateDatabaseDiscoveryTypeStatementAssert.java
index 1480de688d0..6a1cafb378b 100644
--- a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/create/impl/CreateDatabaseDiscoveryTypeStatementAssert.java
+++ b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/create/impl/CreateDatabaseDiscoveryTypeStatementAssert.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.test.sql.parser.parameterized.asserts.statemen
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryTypeSegment;
+import org.apache.shardingsphere.dbdiscovery.distsql.parser.segment.DatabaseDiscoveryProviderAlgorithmSegment;
import org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.CreateDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.SQLCaseAssertContext;
import org.apache.shardingsphere.test.sql.parser.parameterized.asserts.segment.distsql.AlgorithmAssert;
@@ -51,12 +51,13 @@ public final class CreateDatabaseDiscoveryTypeStatementAssert {
assertNull(assertContext.getText("Actual statement should not exist."), actual);
} else {
assertNotNull(assertContext.getText("Actual statement should exist."), actual);
- Map<String, DatabaseDiscoveryTypeSegment> actualMap = actual.getTypes().stream().collect(Collectors.toMap(DatabaseDiscoveryTypeSegment::getDiscoveryTypeName, each -> each));
+ Map<String, DatabaseDiscoveryProviderAlgorithmSegment> actualMap = actual.getProviders()
+ .stream().collect(Collectors.toMap(DatabaseDiscoveryProviderAlgorithmSegment::getDiscoveryProviderName, each -> each));
expected.getTypes().forEach(each -> {
- DatabaseDiscoveryTypeSegment actualSegment = actualMap.get(each.getDiscoveryTypeName());
+ DatabaseDiscoveryProviderAlgorithmSegment actualSegment = actualMap.get(each.getDiscoveryTypeName());
assertNotNull(actualSegment);
- assertThat(actualSegment.getDiscoveryTypeName(), is(each.getDiscoveryTypeName()));
- AlgorithmAssert.assertIs(assertContext, actualSegment.getAlgorithmSegment(), each.getAlgorithmSegment());
+ assertThat(actualSegment.getDiscoveryProviderName(), is(each.getDiscoveryTypeName()));
+ AlgorithmAssert.assertIs(assertContext, actualSegment.getAlgorithm(), each.getAlgorithmSegment());
});
}
}