This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 d048e548dac Improve DistSQL DROP DB_DISCOVERY RULE (#22364)
d048e548dac is described below
commit d048e548dac75ef5a27f90fc03bc67ce1f000799
Author: ChenJiaHao <[email protected]>
AuthorDate: Thu Nov 24 13:08:07 2022 +0800
Improve DistSQL DROP DB_DISCOVERY RULE (#22364)
* Remove unused discovery type and discovery heartbeat while dropping DB
discovery rule
* Add related test cases
* Update document
* Fix test cases
---
.../syntax/rdl/rule-definition/db-discovery.cn.md | 1 -
.../syntax/rdl/rule-definition/db-discovery.en.md | 1 -
.../DropDatabaseDiscoveryRuleStatementUpdater.java | 22 +++++++++++++++++++---
...pDatabaseDiscoveryRuleStatementUpdaterTest.java | 12 ++++++++----
4 files changed, 27 insertions(+), 9 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 0e5c7c5745e..c73447044c2 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
@@ -51,7 +51,6 @@ property:
- 重复的 `ruleName` 将无法被创建;
- 正在被使用的 `discoveryType` 和 `discoveryHeartbeat` 无法被删除;
- 带有 `-` 的命名在改动时需要使用 `" "`;
-- 移除 `discoveryRule` 时不会移除被该 `discoveryRule` 使用的 `discoveryType` 和
`discoveryHeartbeat`。
## 示例
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 194fcec1e02..c73543f1165 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
@@ -51,7 +51,6 @@ property:
- 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
diff --git
a/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
b/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
index f29e7775aac..e33ef6821b9 100644
---
a/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
+++
b/features/db-discovery/distsql/handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
@@ -34,6 +34,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
+import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -80,9 +81,9 @@ public final class DropDatabaseDiscoveryRuleStatementUpdater
implements RuleDefi
@Override
public boolean updateCurrentRuleConfiguration(final
DropDatabaseDiscoveryRuleStatement sqlStatement, final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) {
- for (String each : sqlStatement.getNames()) {
- dropRule(currentRuleConfig, each);
- }
+ sqlStatement.getNames().forEach(each -> dropRule(currentRuleConfig,
each));
+ dropUnusedType(currentRuleConfig);
+ dropUnusedHeartbeat(currentRuleConfig);
return false;
}
@@ -91,6 +92,21 @@ public final class DropDatabaseDiscoveryRuleStatementUpdater
implements RuleDefi
dataSourceRuleConfig.ifPresent(optional ->
currentRuleConfig.getDataSources().remove(dataSourceRuleConfig.get()));
}
+ private void dropUnusedType(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) {
+ Collection<String> inUsedDiscoveryTypes =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getDiscoveryTypeName).filter(Objects::nonNull)
+ .collect(Collectors.toSet());
+ Collection<String> unusedDiscoveryTypes =
currentRuleConfig.getDiscoveryTypes().keySet().stream().filter(each ->
!inUsedDiscoveryTypes.contains(each)).collect(Collectors.toSet());
+ unusedDiscoveryTypes.forEach(each ->
currentRuleConfig.getDiscoveryTypes().remove(each));
+ }
+
+ private void dropUnusedHeartbeat(final DatabaseDiscoveryRuleConfiguration
currentRuleConfig) {
+ Collection<String> inUsedDiscoveryHeartbeats =
currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getDiscoveryHeartbeatName)
+ .filter(Objects::nonNull).collect(Collectors.toSet());
+ Collection<String> unusedDiscoveryHeartbeats =
currentRuleConfig.getDiscoveryHeartbeats().keySet().stream().filter(each ->
!inUsedDiscoveryHeartbeats.contains(each))
+ .collect(Collectors.toSet());
+ unusedDiscoveryHeartbeats.forEach(each ->
currentRuleConfig.getDiscoveryHeartbeats().remove(each));
+ }
+
@Override
public boolean hasAnyOneToBeDropped(final
DropDatabaseDiscoveryRuleStatement sqlStatement, final
DatabaseDiscoveryRuleConfiguration currentRuleConfig) {
return isExistRuleConfig(currentRuleConfig)
diff --git
a/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
b/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
index 495f817bab5..0fd2990f99a 100644
---
a/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
+++
b/features/db-discovery/distsql/handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
@@ -19,6 +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.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryRuleStatement;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.constant.ExportableConstants;
@@ -106,7 +107,8 @@ public final class
DropDatabaseDiscoveryRuleStatementUpdaterTest {
DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfig =
createCurrentRuleConfiguration();
updater.updateCurrentRuleConfiguration(createSQLStatement(),
databaseDiscoveryRuleConfig);
assertTrue(databaseDiscoveryRuleConfig.getDataSources().isEmpty());
- assertThat(databaseDiscoveryRuleConfig.getDiscoveryTypes().size(),
is(1));
+ assertTrue(databaseDiscoveryRuleConfig.getDiscoveryTypes().isEmpty());
+
assertTrue(databaseDiscoveryRuleConfig.getDiscoveryHeartbeats().isEmpty());
}
@Test
@@ -136,9 +138,11 @@ public final class
DropDatabaseDiscoveryRuleStatementUpdaterTest {
private DatabaseDiscoveryRuleConfiguration
createCurrentRuleConfiguration() {
DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig =
new DatabaseDiscoveryDataSourceRuleConfiguration("ha_group",
Collections.emptyList(), "ha_heartbeat", "readwrite_ds_MGR");
- Map<String, AlgorithmConfiguration> discoveryTypes =
Collections.singletonMap(
- "readwrite_ds_MGR", new
AlgorithmConfiguration("readwrite_ds_MGR", new Properties()));
- return new DatabaseDiscoveryRuleConfiguration(new
LinkedList<>(Collections.singleton(dataSourceRuleConfig)),
Collections.emptyMap(), discoveryTypes);
+ Map<String, AlgorithmConfiguration> discoveryTypes = new
LinkedHashMap<>(1, 1);
+ discoveryTypes.put("readwrite_ds_MGR", new
AlgorithmConfiguration("readwrite_ds_MGR", new Properties()));
+ Map<String, DatabaseDiscoveryHeartBeatConfiguration>
discoveryHeartbeats = new LinkedHashMap<>(1, 1);
+ discoveryHeartbeats.put("unused_heartbeat", new
DatabaseDiscoveryHeartBeatConfiguration(new Properties()));
+ return new DatabaseDiscoveryRuleConfiguration(new
LinkedList<>(Collections.singleton(dataSourceRuleConfig)), discoveryHeartbeats,
discoveryTypes);
}
private DatabaseDiscoveryRuleConfiguration
createMultipleCurrentRuleConfigurations() {