You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/11/25 03:31:00 UTC
[shardingsphere] branch master updated: Improve DistSQL DROP SHADOW RULE (#22397)
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 b5d297cba41 Improve DistSQL DROP SHADOW RULE (#22397)
b5d297cba41 is described below
commit b5d297cba41c8b4f809206a1fedbd6db1f31566d
Author: ChenJiaHao <37...@users.noreply.github.com>
AuthorDate: Fri Nov 25 11:30:53 2022 +0800
Improve DistSQL DROP SHADOW RULE (#22397)
* Remove unused algorithms while dropping a shadow rule
* Add related test cases
* Update document
* Format style
---
.../syntax/rdl/rule-definition/shadow.cn.md | 4 +--
.../syntax/rdl/rule-definition/shadow.en.md | 4 +--
.../update/DropShadowRuleStatementUpdater.java | 10 ++++++
.../update/DropShadowRuleStatementUpdaterTest.java | 41 +++++++++++++++++++---
4 files changed, 49 insertions(+), 10 deletions(-)
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.cn.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.cn.md
index 3a72c96f3ef..540b7314eca 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.cn.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.cn.md
@@ -51,10 +51,8 @@ algorithmProperty: key=value
- 重复的`ruleName`无法被创建;
- `storageUnitMapping` 指定源数据库和影子库的映射关系,需使用 RDL 管理的 `storage unit` ,请参考 [存储单元](/cn/user-manual/shardingsphere-proxy/distsql/syntax/rdl/storage-unit-definition/);
-- `shadowAlgorithm` 可同时作用于多个 `shadowTableRule`;
- `shadowAlgorithmType` 目前支持 `VALUE_MATCH`、`REGEX_MATCH` 和 `SIMPLE_HINT`;
-- `shadowTableRule` 能够被不同的 `shadowRuleDefinition` 复用,因此在执行 `DROP SHADOW RULE` 时,对应的 `shadowTableRule` 不会被移除;
-- `shadowAlgorithm` 能够被不同的 `shadowTableRule` 复用,因此在执行 `ALTER SHADOW RULE` 时,对应的 `shadowAlgorithm` 不会被移除。
+- 在执行 `ALTER SHADOW RULE` 时,对应的 `shadowAlgorithm` 不会被移除。
- 创建规则时,会根据 `ruleName`、`tableName` 和 `shadowAlgorithmType` 和算法集合下标自动生成 `algorithmName` 。默认算法名称为 `default_shadow_algorithm`。
## 示例
diff --git a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.en.md b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.en.md
index ee11baaef6f..3064375bf37 100644
--- a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.en.md
+++ b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/shadow.en.md
@@ -50,10 +50,8 @@ algorithmProperty: key=value
- Duplicate `ruleName` cannot be created
- `storageUnitMapping` specifies the mapping relationship between the source database and the shadow library. You need to use the `storage unit` managed by RDL, please refer to [storage unit](/en/user-manual/shardingsphere-proxy/distsql/syntax/rdl/storage-unit-definition/)
-- `shadowAlgorithm` can act on multiple `shadowTableRule` at the same time
- `shadowAlgorithmType` currently supports `VALUE_MATCH`, `REGEX_MATCH` and `SIMPLE_HINT`
-- `shadowTableRule` can be reused by different `shadowRuleDefinition`, so when executing `DROP SHADOW RULE`, the corresponding `shadowTableRule` will not be removed
-- `shadowAlgorithm` can be reused by different `shadowTableRule`, so when executing `ALTER SHADOW RULE`, the corresponding `shadowAlgorithm` will not be removed
+- When executing `ALTER SHADOW RULE`, the corresponding `shadowAlgorithm` will not be removed
- When creating shadow rule, `algorithmName` will be automatically generated according to `ruleName`, `tableName`, `shadowAlgorithmType` and algorithm collection index. The default name is `default_shadow_algorithm`.
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
index 69765dc3412..d4f314f6681 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java
@@ -68,9 +68,19 @@ public final class DropShadowRuleStatementUpdater implements RuleDefinitionDropU
currentRuleConfig.getDataSources().removeIf(each -> sqlStatement.getNames().contains(each.getName()));
currentRuleConfig.getTables().forEach((key, value) -> value.getDataSourceNames().removeIf(sqlStatement.getNames()::contains));
currentRuleConfig.getTables().entrySet().removeIf(entry -> entry.getValue().getDataSourceNames().isEmpty());
+ removeUnusedAlgorithm(currentRuleConfig);
return currentRuleConfig.getDataSources().isEmpty() || currentRuleConfig.getTables().isEmpty();
}
+ private void removeUnusedAlgorithm(final ShadowRuleConfiguration currentRuleConfig) {
+ Collection<String> inUsedAlgorithms = currentRuleConfig.getTables().entrySet().stream().flatMap(entry -> entry.getValue().getShadowAlgorithmNames().stream()).collect(Collectors.toSet());
+ if (null != currentRuleConfig.getDefaultShadowAlgorithmName()) {
+ inUsedAlgorithms.add(currentRuleConfig.getDefaultShadowAlgorithmName());
+ }
+ Collection<String> unusedAlgorithms = currentRuleConfig.getShadowAlgorithms().keySet().stream().filter(each -> !inUsedAlgorithms.contains(each)).collect(Collectors.toSet());
+ unusedAlgorithms.forEach(each -> currentRuleConfig.getShadowAlgorithms().remove(each));
+ }
+
@Override
public Class<ShadowRuleConfiguration> getRuleConfigurationClass() {
return ShadowRuleConfiguration.class;
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
index 43320c0e54d..500623caea3 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/DropShadowRuleStatementUpdaterTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.shadow.distsql.update;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.distsql.exception.rule.MissingRequiredRuleException;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
@@ -33,7 +34,10 @@ import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
+import java.util.Properties;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
@@ -59,16 +63,27 @@ public final class DropShadowRuleStatementUpdaterTest {
}
@Test
- public void assertUpdate() {
+ public void assertUpdateCurrentRuleConfigurationWithUnusedAlgorithms() {
DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group");
- ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration();
- ruleConfig.getDataSources().add(createShadowDataSourceConfiguration("shadow_group"));
- ruleConfig.getTables().put("t_order", new ShadowTableConfiguration(new ArrayList<>(Collections.singleton("shadow_group")), Collections.emptyList()));
+ ShadowRuleConfiguration ruleConfig = createCurrentRuleConfiguration();
+ DropShadowRuleStatementUpdater updater = new DropShadowRuleStatementUpdater();
+ updater.checkSQLStatement(database, sqlStatement, ruleConfig);
+ assertTrue(updater.updateCurrentRuleConfiguration(sqlStatement, ruleConfig));
+ assertTrue(ruleConfig.getDataSources().isEmpty());
+ assertTrue(ruleConfig.getTables().isEmpty());
+ assertTrue(ruleConfig.getShadowAlgorithms().isEmpty());
+ }
+
+ @Test
+ public void assertUpdateMultipleCurrentRuleConfigurationWithInUsedAlgorithms() {
+ DropShadowRuleStatement sqlStatement = createSQLStatement("shadow_group");
+ ShadowRuleConfiguration ruleConfig = createMultipleCurrentRuleConfiguration();
DropShadowRuleStatementUpdater updater = new DropShadowRuleStatementUpdater();
updater.checkSQLStatement(database, sqlStatement, ruleConfig);
assertTrue(updater.updateCurrentRuleConfiguration(sqlStatement, ruleConfig));
assertTrue(ruleConfig.getDataSources().isEmpty());
assertTrue(ruleConfig.getTables().isEmpty());
+ assertThat(ruleConfig.getShadowAlgorithms().size(), is(1));
}
private DropShadowRuleStatement createSQLStatement(final String... ruleName) {
@@ -79,6 +94,24 @@ public final class DropShadowRuleStatementUpdaterTest {
return new DropShadowRuleStatement(ifExists, Arrays.asList(ruleName));
}
+ private ShadowRuleConfiguration createCurrentRuleConfiguration() {
+ ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration();
+ ruleConfig.getDataSources().add(createShadowDataSourceConfiguration("shadow_group"));
+ ruleConfig.getTables().put("t_order", new ShadowTableConfiguration(new ArrayList<>(Collections.singleton("shadow_group")), Collections.emptyList()));
+ ruleConfig.getShadowAlgorithms().put("t_order_algorithm", new AlgorithmConfiguration("SHADOW", new Properties()));
+ return ruleConfig;
+ }
+
+ private ShadowRuleConfiguration createMultipleCurrentRuleConfiguration() {
+ ShadowRuleConfiguration ruleConfig = new ShadowRuleConfiguration();
+ ruleConfig.getDataSources().add(createShadowDataSourceConfiguration("shadow_group"));
+ ruleConfig.getTables().put("t_order", new ShadowTableConfiguration(new ArrayList<>(Collections.singleton("shadow_group")), Collections.emptyList()));
+ ruleConfig.getShadowAlgorithms().put("t_order_algorithm_inUsed", new AlgorithmConfiguration("SHADOW", new Properties()));
+ ruleConfig.getShadowAlgorithms().put("t_order_algorithm_unused", new AlgorithmConfiguration("SHADOW", new Properties()));
+ ruleConfig.setDefaultShadowAlgorithmName("t_order_algorithm_inUsed");
+ return ruleConfig;
+ }
+
private ShadowDataSourceConfiguration createShadowDataSourceConfiguration(final String ruleName) {
return new ShadowDataSourceConfiguration(ruleName, "production", "shadow");
}