This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new c618972 [DistSQL]Add DB_Discovery document and optimize verification
logic (#14174)
c618972 is described below
commit c618972e85bec6981da0db8654d1e64cf7e833d4
Author: lanchengx <[email protected]>
AuthorDate: Mon Dec 20 22:53:47 2021 +0800
[DistSQL]Add DB_Discovery document and optimize verification logic (#14174)
* Add Discovery related documents.
* Fix failure when `Drop discovery type/heartbeat`.
* Reformat.
* Add document.
---
.../syntax/rdl/rule-definition/db-discovery.cn.md | 104 +++++++++++++++++---
.../syntax/rdl/rule-definition/db-discovery.en.md | 105 ++++++++++++++++++---
...AlterDatabaseDiscoveryRuleStatementUpdater.java | 2 -
...DatabaseDiscoveryHeartbeatStatementUpdater.java | 33 ++++---
.../DropDatabaseDiscoveryRuleStatementUpdater.java | 9 +-
.../DropDatabaseDiscoveryTypeStatementUpdater.java | 25 +++--
...here.infra.distsql.update.RuleDefinitionUpdater | 2 +
...baseDiscoveryHeartbeatStatementUpdaterTest.java | 9 ++
...pDatabaseDiscoveryRuleStatementUpdaterTest.java | 5 +-
...pDatabaseDiscoveryTypeStatementUpdaterTest.java | 9 ++
10 files changed, 246 insertions(+), 57 deletions(-)
diff --git
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.cn.md
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.cn.md
index 312aa41..164c721 100644
---
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.cn.md
+++
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.cn.md
@@ -6,42 +6,120 @@ weight = 4
## 语法说明
```sql
-CREATE DB_DISCOVERY RULE databaseDiscoveryRuleDefinition [,
databaseDiscoveryRuleDefinition] ...
+CREATE DB_DISCOVERY RULE ruleDefinition [, ruleDefinition] ...
-ALTER DB_DISCOVERY RULE databaseDiscoveryRuleDefinition [,
databaseDiscoveryRuleDefinition] ...
+ALTER DB_DISCOVERY RULE ruleDefinition [, ruleDefinition] ...
DROP DB_DISCOVERY RULE ruleName [, ruleName] ...
-databaseDiscoveryRuleDefinition:
- ruleName(resources, discoveryTypeDefinition)
+CREATE DB_DISCOVERY TYPE databaseDiscoveryTypeDefinition [,
databaseDiscoveryTypeDefinition] ...
+
+ALTER DB_DISCOVERY TYPE databaseDiscoveryTypeDefinition [,
databaseDiscoveryTypeDefinition] ...
+
+DROP DB_DISCOVERY TYPE discoveryTypeName [, discoveryTypeName] ...
+
+CREATE DB_DISCOVERY HEARTBEAT databaseDiscoveryHeartbaetDefinition [,
databaseDiscoveryHeartbaetDefinition] ...
+
+ALTER DB_DISCOVERY HEARTBEAT databaseDiscoveryHeartbaetDefinition [,
databaseDiscoveryHeartbaetDefinition] ...
+
+DROP DB_DISCOVERY HEARTBEAT discoveryHeartbeatName [, discoveryHeartbeatName]
...
+
+ruleDefinition:
+ (databaseDiscoveryRuleDefinition | databaseDiscoveryRuleConstruction)
+
+databaseDiscoveryRuleDefinition
+ ruleName (resources, typeDefinition, heartbeatDefinition)
+
+databaseDiscoveryRuleConstruction
+ ruleName (resources, TYPE = discoveryTypeName, HEARTBEAT =
discoveryHeartbeatName)
+
+databaseDiscoveryTypeDefinition
+ discoveryTypeName (typeDefinition)
+
+databaseDiscoveryHeartbaetDefinition
+ discoveryHeartbeatName (PROPERTIES (properties))
resources:
RESOURCES(resourceName [, resourceName] ...)
-discoveryTypeDefinition:
- TYPE(NAME=discoveryType [, PROPERTIES([algorithmProperties] )] )
+typeDefinition:
+ TYPE(NAME=typeName [, PROPERTIES([properties] )] )
+
+heartbeatDefinition
+ HEARTBEAT (PROPERTIES (properties))
-algorithmProperties:
- algorithmProperty [, algorithmProperty] ...
+properties:
+ property [, property] ...
-algorithmProperty:
+property:
key=value
```
+
- `discoveryType` 指定数据库发现服务类型,`ShardingSphere` 内置支持 `MGR`
- 重复的 `ruleName` 将无法被创建
+- 正在被使用的 `discoveryType` 和 `discoveryHeartbeat` 无法被删除
+- 带有 `-` 的命名在改动时需要使用 `" "`
+- 移除 `discoveryRule` 时不会移除被该 `discoveryRule` 使用的 `discoveryType` 和
`discoveryHeartbeat`
## 示例
+### 创建 `discoveryRule` 时同时创建 `discoveryType` 和 `discoveryHeartbeat`
+
```sql
CREATE DB_DISCOVERY RULE ha_group_0 (
-RESOURCES(resource_0,resource_1),
-TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec',keepAliveCron=''))
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE(NAME=mgr,PROPERTIES('groupName'='92504d5b-6dec')),
+HEARTBEAT(PROPERTIES('keepAliveCron'='0/5 * * * * ?'))
);
ALTER DB_DISCOVERY RULE ha_group_0 (
-RESOURCES(resource_0,resource_1,resource_2),
-TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec' ,keepAliveCron=''))
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE(NAME=mgr,PROPERTIES('groupName'='246e9612-aaf1')),
+HEARTBEAT(PROPERTIES('keepAliveCron'='0/5 * * * * ?'))
);
DROP DB_DISCOVERY RULE ha_group_0;
+
+DROP DB_DISCOVERY TYPE ha_group_0_mgr;
+
+DROP DB_DISCOVERY HEARTBEAT ha_group_0_heartbeat;
+
```
+
+### 使用已有的 `discoveryType` 和 `discoveryHeartbeat` 创建 `discoveryRule`
+
+```sql
+CREATE DB_DISCOVERY TYPE ha_group_1_mgr(
+ TYPE(NAME=mgr,PROPERTIES('groupName'='92504d5b-6dec'))
+);
+
+CREATE DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat(
+ PROPERTIES('keepAliveCron'='0/5 * * * * ?')
+);
+
+CREATE DB_DISCOVERY RULE ha_group_1 (
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE=ha_group_1_mgr,
+HEARTBEAT=ha_group_1_heartbeat
+);
+
+ALTER DB_DISCOVERY TYPE ha_group_1_mgr(
+ TYPE(NAME=mgr,PROPERTIES('groupName'='246e9612-aaf1'))
+);
+
+ALTER DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat(
+ PROPERTIES('keepAliveCron'='0/10 * * * * ?')
+);
+
+ALTER DB_DISCOVERY RULE ha_group_1 (
+RESOURCES(ds_0, ds_1),
+TYPE=ha_group_1_mgr,
+HEARTBEAT=ha_group_1_heartbeat
+);
+
+DROP DB_DISCOVERY RULE ha_group_1;
+
+DROP DB_DISCOVERY TYPE ha_group_1_mgr;
+
+DROP DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat;
+```
\ No newline at end of file
diff --git
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.en.md
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.en.md
index 9ab39a2..bd712d2 100644
---
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.en.md
+++
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/db-discovery.en.md
@@ -6,42 +6,121 @@ weight = 4
## Syntax
```sql
-CREATE DB_DISCOVERY RULE databaseDiscoveryRuleDefinition [,
databaseDiscoveryRuleDefinition] ...
+CREATE DB_DISCOVERY RULE ruleDefinition [, ruleDefinition] ...
-ALTER DB_DISCOVERY RULE databaseDiscoveryRuleDefinition [,
databaseDiscoveryRuleDefinition] ...
+ALTER DB_DISCOVERY RULE ruleDefinition [, ruleDefinition] ...
DROP DB_DISCOVERY RULE ruleName [, ruleName] ...
-databaseDiscoveryRuleDefinition:
- ruleName(resources, discoveryTypeDefinition)
+CREATE DB_DISCOVERY TYPE databaseDiscoveryTypeDefinition [,
databaseDiscoveryTypeDefinition] ...
+
+ALTER DB_DISCOVERY TYPE databaseDiscoveryTypeDefinition [,
databaseDiscoveryTypeDefinition] ...
+
+DROP DB_DISCOVERY TYPE discoveryTypeName [, discoveryTypeName] ...
+
+CREATE DB_DISCOVERY HEARTBEAT databaseDiscoveryHeartbaetDefinition [,
databaseDiscoveryHeartbaetDefinition] ...
+
+ALTER DB_DISCOVERY HEARTBEAT databaseDiscoveryHeartbaetDefinition [,
databaseDiscoveryHeartbaetDefinition] ...
+
+DROP DB_DISCOVERY HEARTBEAT discoveryHeartbeatName [, discoveryHeartbeatName]
...
+
+ruleDefinition:
+ (databaseDiscoveryRuleDefinition | databaseDiscoveryRuleConstruction)
+
+databaseDiscoveryRuleDefinition
+ ruleName (resources, typeDefinition, heartbeatDefinition)
+
+databaseDiscoveryRuleConstruction
+ ruleName (resources, TYPE = discoveryTypeName, HEARTBEAT =
discoveryHeartbeatName)
+
+databaseDiscoveryTypeDefinition
+ discoveryTypeName (typeDefinition)
+
+databaseDiscoveryHeartbaetDefinition
+ discoveryHeartbeatName (PROPERTIES (properties))
resources:
RESOURCES(resourceName [, resourceName] ...)
-discoveryTypeDefinition:
- TYPE(NAME=discoveryType [, PROPERTIES([algorithmProperties] )] )
+typeDefinition:
+ TYPE(NAME=typeName [, PROPERTIES([properties] )] )
+
+heartbeatDefinition
+ HEARTBEAT (PROPERTIES (properties))
-algorithmProperties:
- algorithmProperty [, algorithmProperty] ...
+properties:
+ property [, property] ...
-algorithmProperty:
+property:
key=value
```
+
- `discoveryType` specifies the database discovery service type,
`ShardingSphere` has built-in support for `MGR`
- Duplicate `ruleName` will not be created
+- The `discoveryType` and `discoveryHeartbeat` being used cannot be deleted
+- Names with `-` need to use `" "` when changing
+- When removing the `discoveryRule`, the `discoveryType` and
`discoveryHeartbeat` used by the `discoveryRule` will not be removed
+
## Example
+### When creating a `discoveryRule`, create both `discoveryType` and
`discoveryHeartbeat`
+
```sql
CREATE DB_DISCOVERY RULE ha_group_0 (
-RESOURCES(resource_0,resource_1),
-TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec',keepAliveCron=''))
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE(NAME=mgr,PROPERTIES('groupName'='92504d5b-6dec')),
+HEARTBEAT(PROPERTIES('keepAliveCron'='0/5 * * * * ?'))
);
ALTER DB_DISCOVERY RULE ha_group_0 (
-RESOURCES(resource_0,resource_1,resource_2),
-TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec' ,keepAliveCron=''))
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE(NAME=mgr,PROPERTIES('groupName'='246e9612-aaf1')),
+HEARTBEAT(PROPERTIES('keepAliveCron'='0/5 * * * * ?'))
);
DROP DB_DISCOVERY RULE ha_group_0;
+
+DROP DB_DISCOVERY TYPE ha_group_0_mgr;
+
+DROP DB_DISCOVERY HEARTBEAT ha_group_0_heartbeat;
+
```
+
+### Use the existing `discoveryType` and `discoveryHeartbeat` to create a
`discoveryRule`
+
+```sql
+CREATE DB_DISCOVERY TYPE ha_group_1_mgr(
+ TYPE(NAME=mgr,PROPERTIES('groupName'='92504d5b-6dec'))
+);
+
+CREATE DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat(
+ PROPERTIES('keepAliveCron'='0/5 * * * * ?')
+);
+
+CREATE DB_DISCOVERY RULE ha_group_1 (
+RESOURCES(ds_0, ds_1, ds_2),
+TYPE=ha_group_1_mgr,
+HEARTBEAT=ha_group_1_heartbeat
+);
+
+ALTER DB_DISCOVERY TYPE ha_group_1_mgr(
+ TYPE(NAME=mgr,PROPERTIES('groupName'='246e9612-aaf1'))
+);
+
+ALTER DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat(
+ PROPERTIES('keepAliveCron'='0/10 * * * * ?')
+);
+
+ALTER DB_DISCOVERY RULE ha_group_1 (
+RESOURCES(ds_0, ds_1),
+TYPE=ha_group_1_mgr,
+HEARTBEAT=ha_group_1_heartbeat
+);
+
+DROP DB_DISCOVERY RULE ha_group_1;
+
+DROP DB_DISCOVERY TYPE ha_group_1_mgr;
+
+DROP DB_DISCOVERY HEARTBEAT ha_group_1_heartbeat;
+```
\ No newline at end of file
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/AlterDatabaseDiscoveryRuleStatementUpdater.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 8a4255f..ef141df 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/AlterDatabaseDiscoveryRuleStatementUpdater.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/AlterDatabaseDiscoveryRuleStatementUpdater.java
@@ -134,8 +134,6 @@ public final class
AlterDatabaseDiscoveryRuleStatementUpdater implements RuleDef
.filter(each ->
each.getName().equals(toBeDroppedRuleNames)).findAny();
Preconditions.checkState(toBeDroppedDataSourceRuleConfig.isPresent());
currentRuleConfig.getDataSources().remove(toBeDroppedDataSourceRuleConfig.get());
-
currentRuleConfig.getDiscoveryTypes().remove(toBeDroppedDataSourceRuleConfig.get().getDiscoveryTypeName());
-
currentRuleConfig.getDiscoveryHeartbeats().remove(toBeDroppedDataSourceRuleConfig.get().getDiscoveryHeartbeatName());
}
private void addRuleConfiguration(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig, final DatabaseDiscoveryRuleConfiguration
toBeAlteredRuleConfig) {
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/DropDatabaseDiscoveryHeartbeatStatementUpdater.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/DropDatabase
[...]
index 096c2fd..f946ad1 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/DropDatabaseDiscoveryHeartbeatStatementUpdater.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/DropDatabaseDiscoveryHeartbeatStatementUpdater.java
@@ -22,10 +22,13 @@ import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDa
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryHeartbeatStatement;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import java.util.Collection;
+import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -33,23 +36,33 @@ import java.util.stream.Collectors;
*/
public final class DropDatabaseDiscoveryHeartbeatStatementUpdater implements
RuleDefinitionDropUpdater<DropDatabaseDiscoveryHeartbeatStatement,
DatabaseDiscoveryRuleConfiguration> {
+ private static final String RULE_TYPE = "database discovery";
+
@Override
public void checkSQLStatement(final ShardingSphereMetaData
shardingSphereMetaData, final DropDatabaseDiscoveryHeartbeatStatement
sqlStatement,
final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
String schemaName = shardingSphereMetaData.getName();
checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
- checkToBeDroppedHeartbeatNames(schemaName, sqlStatement,
currentRuleConfig);
+ checkIsExist(schemaName, sqlStatement, currentRuleConfig);
+ checkIsInUse(schemaName, sqlStatement, currentRuleConfig);
}
private void checkCurrentRuleConfiguration(final String schemaName, final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
- DistSQLException.predictionThrow(null != currentRuleConfig, new
RequiredRuleMissedException("Database discovery", schemaName));
+ DistSQLException.predictionThrow(null != currentRuleConfig, new
RequiredRuleMissedException(RULE_TYPE, schemaName));
}
- private void checkToBeDroppedHeartbeatNames(final String schemaName, final
DropDatabaseDiscoveryHeartbeatStatement sqlStatement,
- final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
- Collection<String> currentRuleNames =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getName).collect(Collectors.toList());
+ private void checkIsExist(final String schemaName, final
DropDatabaseDiscoveryHeartbeatStatement sqlStatement,
+ final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
+ Collection<String> currentRuleNames =
currentRuleConfig.getDiscoveryHeartbeats().keySet();
Collection<String> notExistedRuleNames =
sqlStatement.getHeartbeatNames().stream().filter(each ->
!currentRuleNames.contains(each)).collect(Collectors.toList());
- DistSQLException.predictionThrow(notExistedRuleNames.isEmpty(), new
RequiredRuleMissedException("Database discovery", schemaName,
notExistedRuleNames));
+ DistSQLException.predictionThrow(notExistedRuleNames.isEmpty(), new
RequiredRuleMissedException(RULE_TYPE, schemaName, notExistedRuleNames));
+ }
+
+ private void checkIsInUse(final String schemaName, final
DropDatabaseDiscoveryHeartbeatStatement sqlStatement,
+ final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
+ Set<String> heartbeatInUse =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getDiscoveryHeartbeatName).collect(Collectors.toSet());
+ List<String> invalid =
sqlStatement.getHeartbeatNames().stream().filter(heartbeatInUse::contains).collect(Collectors.toList());
+ DistSQLException.predictionThrow(invalid.isEmpty(), new
RuleInUsedException(RULE_TYPE, schemaName, invalid));
}
@Override
@@ -61,13 +74,7 @@ public final class
DropDatabaseDiscoveryHeartbeatStatementUpdater implements Rul
}
private void dropRule(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig, final String heartbeatName) {
- if (isNotInUse(currentRuleConfig, heartbeatName)) {
- currentRuleConfig.getDiscoveryHeartbeats().remove(heartbeatName);
- }
- }
-
- private boolean isNotInUse(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig, final String toBeDroppedDiscoveryTypeName) {
- return currentRuleConfig.getDataSources().stream().noneMatch(each ->
each.getDiscoveryHeartbeatName().equals(toBeDroppedDiscoveryTypeName));
+ currentRuleConfig.getDiscoveryHeartbeats().remove(heartbeatName);
}
@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/DropDatabaseDiscoveryRuleStatementUpdater.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/DropDatabaseDisco
[...]
index fa24534..7346427 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/DropDatabaseDiscoveryRuleStatementUpdater.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/DropDatabaseDiscoveryRuleStatementUpdater.java
@@ -63,20 +63,13 @@ public final class
DropDatabaseDiscoveryRuleStatementUpdater implements RuleDefi
for (String each : sqlStatement.getRuleNames()) {
dropRule(currentRuleConfig, each);
}
- return currentRuleConfig.getDataSources().isEmpty();
+ return false;
}
private void dropRule(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig, final String ruleName) {
Optional<DatabaseDiscoveryDataSourceRuleConfiguration>
dataSourceRuleConfig =
currentRuleConfig.getDataSources().stream().filter(dataSource ->
dataSource.getName().equals(ruleName)).findAny();
Preconditions.checkState(dataSourceRuleConfig.isPresent());
currentRuleConfig.getDataSources().remove(dataSourceRuleConfig.get());
- if (isDiscoveryTypeNotInUse(currentRuleConfig,
dataSourceRuleConfig.get().getDiscoveryTypeName())) {
-
currentRuleConfig.getDiscoveryTypes().remove(dataSourceRuleConfig.get().getDiscoveryTypeName());
- }
- }
-
- private boolean isDiscoveryTypeNotInUse(final
DatabaseDiscoveryRuleConfiguration currentRuleConfig, final String
toBeDroppedDiscoveryTypeName) {
- return !currentRuleConfig.getDataSources().stream().filter(each ->
each.getDiscoveryTypeName().equals(toBeDroppedDiscoveryTypeName)).findAny().isPresent();
}
@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/DropDatabaseDiscoveryTypeStatementUpdater.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/DropDatabaseDisco
[...]
index afdbf97..d4053d8 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/DropDatabaseDiscoveryTypeStatementUpdater.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/DropDatabaseDiscoveryTypeStatementUpdater.java
@@ -22,10 +22,13 @@ import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDa
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryTypeStatement;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import java.util.Collection;
+import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -33,23 +36,33 @@ import java.util.stream.Collectors;
*/
public final class DropDatabaseDiscoveryTypeStatementUpdater implements
RuleDefinitionDropUpdater<DropDatabaseDiscoveryTypeStatement,
DatabaseDiscoveryRuleConfiguration> {
+ private static final String RULE_TYPE = "database discovery";
+
@Override
public void checkSQLStatement(final ShardingSphereMetaData
shardingSphereMetaData, final DropDatabaseDiscoveryTypeStatement sqlStatement,
final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
String schemaName = shardingSphereMetaData.getName();
checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
- checkToBeDroppedTypeNames(schemaName, sqlStatement, currentRuleConfig);
+ checkIsExist(schemaName, sqlStatement, currentRuleConfig);
+ checkIsInUse(schemaName, sqlStatement, currentRuleConfig);
}
private void checkCurrentRuleConfiguration(final String schemaName, final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
- DistSQLException.predictionThrow(null != currentRuleConfig, new
RequiredRuleMissedException("Database discovery", schemaName));
+ DistSQLException.predictionThrow(null != currentRuleConfig, new
RequiredRuleMissedException(RULE_TYPE, schemaName));
}
- private void checkToBeDroppedTypeNames(final String schemaName, final
DropDatabaseDiscoveryTypeStatement sqlStatement,
- final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
- Collection<String> currentRuleNames =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getName).collect(Collectors.toList());
+ private void checkIsExist(final String schemaName, final
DropDatabaseDiscoveryTypeStatement sqlStatement,
+ final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
+ Collection<String> currentRuleNames =
currentRuleConfig.getDiscoveryTypes().keySet();
Collection<String> notExistedRuleNames =
sqlStatement.getTypes().stream().filter(each ->
!currentRuleNames.contains(each)).collect(Collectors.toList());
- DistSQLException.predictionThrow(notExistedRuleNames.isEmpty(), new
RequiredRuleMissedException("Database discovery", schemaName,
notExistedRuleNames));
+ DistSQLException.predictionThrow(notExistedRuleNames.isEmpty(), new
RequiredRuleMissedException(RULE_TYPE, schemaName, notExistedRuleNames));
+ }
+
+ private void checkIsInUse(final String schemaName, final
DropDatabaseDiscoveryTypeStatement sqlStatement,
+ final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) throws DistSQLException {
+ Set<String> heartbeatInUse =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getDiscoveryTypeName).collect(Collectors.toSet());
+ List<String> invalid =
sqlStatement.getTypes().stream().filter(heartbeatInUse::contains).collect(Collectors.toList());
+ DistSQLException.predictionThrow(invalid.isEmpty(), new
RuleInUsedException(RULE_TYPE, schemaName, invalid));
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.RuleDefinitionUpdater
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.RuleDefinitionUpdater
index bcd8500..8c07933 100644
---
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.RuleDefinitionUpdater
+++
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.distsql.update.RuleDefinitionUpdater
@@ -22,3 +22,5 @@
org.apache.shardingsphere.dbdiscovery.distsql.handler.update.CreateDatabaseDisco
org.apache.shardingsphere.dbdiscovery.distsql.handler.update.AlterDatabaseDiscoveryHeartbeatStatementUpdater
org.apache.shardingsphere.dbdiscovery.distsql.handler.update.CreateDatabaseDiscoveryHeartbeatStatementUpdater
org.apache.shardingsphere.dbdiscovery.distsql.handler.update.AlterDatabaseDiscoveryTypeStatementUpdater
+org.apache.shardingsphere.dbdiscovery.distsql.handler.update.DropDatabaseDiscoveryHeartbeatStatementUpdater
+org.apache.shardingsphere.dbdiscovery.distsql.handler.update.DropDatabaseDiscoveryTypeStatementUpdater
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/DropDatabaseDiscoveryHeartbeatStatementUpdaterTest.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/DropData
[...]
index a484a7e..a8e92da 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/DropDatabaseDiscoveryHeartbeatStatementUpdaterTest.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/DropDatabaseDiscoveryHeartbeatStatementUpdaterTest.java
@@ -18,11 +18,13 @@
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.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryHeartbeatStatement;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,6 +57,13 @@ public final class
DropDatabaseDiscoveryHeartbeatStatementUpdaterTest {
updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(), new
DatabaseDiscoveryRuleConfiguration(Collections.emptyList(),
Collections.emptyMap(), Collections.emptyMap()));
}
+ @Test(expected = RuleInUsedException.class)
+ public void assertCheckSQLStatementWithInUsed() throws DistSQLException {
+ DatabaseDiscoveryDataSourceRuleConfiguration configuration = new
DatabaseDiscoveryDataSourceRuleConfiguration("name", Collections.emptyList(),
"heartbeat_name", "");
+ updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(), new
DatabaseDiscoveryRuleConfiguration(Collections.singletonList(configuration),
+ Collections.singletonMap("heartbeat_name", null),
Collections.emptyMap()));
+ }
+
@Test
public void assertUpdateCurrentRuleConfiguration() {
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration
= createCurrentRuleConfiguration();
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/DropDatabaseDiscoveryRuleStatementUpdaterTest.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/DropDatabaseD
[...]
index d237fdd..705ab51 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/DropDatabaseDiscoveryRuleStatementUpdaterTest.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/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
@@ -62,8 +62,9 @@ public final class
DropDatabaseDiscoveryRuleStatementUpdaterTest {
@Test
public void assertUpdateCurrentRuleConfiguration() {
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration
= createCurrentRuleConfiguration();
-
assertTrue(updater.updateCurrentRuleConfiguration(createSQLStatement(),
databaseDiscoveryRuleConfiguration));
-
assertTrue(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().isEmpty());
+ updater.updateCurrentRuleConfiguration(createSQLStatement(),
databaseDiscoveryRuleConfiguration);
+
assertTrue(databaseDiscoveryRuleConfiguration.getDataSources().isEmpty());
+
assertThat(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().size(),
is(1));
}
@Test
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/DropDatabaseDiscoveryTypeStatementUpdaterTest.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/DropDatabaseD
[...]
index 6b43b0c..993c1ed 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/DropDatabaseDiscoveryTypeStatementUpdaterTest.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/DropDatabaseDiscoveryTypeStatementUpdaterTest.java
@@ -18,11 +18,13 @@
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.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryTypeStatement;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
+import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,6 +57,13 @@ public final class
DropDatabaseDiscoveryTypeStatementUpdaterTest {
updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(), new
DatabaseDiscoveryRuleConfiguration(Collections.emptyList(),
Collections.emptyMap(), Collections.emptyMap()));
}
+ @Test(expected = RuleInUsedException.class)
+ public void assertCheckSQLStatementWithInUsed() throws DistSQLException {
+ DatabaseDiscoveryDataSourceRuleConfiguration configuration = new
DatabaseDiscoveryDataSourceRuleConfiguration("name", Collections.emptyList(),
"", "type_name");
+ updater.checkSQLStatement(shardingSphereMetaData,
createSQLStatement(), new
DatabaseDiscoveryRuleConfiguration(Collections.singletonList(configuration),
+ Collections.emptyMap(), Collections.singletonMap("type_name",
null)));
+ }
+
@Test
public void assertUpdateCurrentRuleConfiguration() {
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration
= createCurrentRuleConfiguration();