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 ad3c6abdae3 Improve DistSQL DROP SHARDING TABLE RULE (#22361)
ad3c6abdae3 is described below
commit ad3c6abdae3c5d8aabcdb33c0167793e97e0415f
Author: ChenJiaHao <[email protected]>
AuthorDate: Wed Nov 23 18:48:16 2022 +0800
Improve DistSQL DROP SHARDING TABLE RULE (#22361)
* Improve DROP SHARDING TABLE RULE, removing unused algorithm by default
* Format code
* Fix remove unused auditor
* Supplementary test cases
* Update document
---
.../syntax/rdl/rule-definition/sharding.cn.md | 1 -
.../syntax/rdl/rule-definition/sharding.en.md | 1 -
.../DropShardingTableRuleStatementUpdater.java | 33 ++++++++++++++++++----
.../DropShardingTableRuleStatementUpdaterTest.java | 24 +++++++++-------
.../main/antlr4/imports/sharding/RDLStatement.g4 | 6 +---
.../core/ShardingDistSQLStatementVisitor.java | 2 +-
.../statement/DropShardingTableRuleStatement.java | 5 +---
7 files changed, 44 insertions(+), 28 deletions(-)
diff --git
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.cn.md
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.cn.md
index a9c64b2ef48..f2aba23def1 100644
---
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.cn.md
+++
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.cn.md
@@ -113,7 +113,6 @@ auditorAlgorithmDefinition:
- `keyGenerateStrategyType` 指定分布式主键生成策略,请参考
[分布式主键](/cn/user-manual/common-config/builtin-algorithm/keygen/);
- `auditorAlgorithmType` 指定分片审计策略,请参考
[分片审计](/cn/user-manual/common-config/builtin-algorithm/audit/);
- 重复的 `tableName` 将无法被创建;
-- `shardingAlgorithm` 能够被不同的 `Sharding Table Rule` 复用,因此在执行 `DROP SHARDING
TABLE RULE` 时,对应的 `shardingAlgorithm` 不会被移除;
- 如需移除 `shardingAlgorithm`,请执行 `DROP SHARDING ALGORITHM`;
- `strategyType`
指定分片策略,请参考[分片策略](/cn/features/sharding/concept/sharding/#%E5%88%86%E7%89%87%E7%AD%96%E7%95%A5);
- `Sharding Table Rule` 同时支持 `Auto Table` 和 `Table` 两种类型,两者在语法上有所差异,对应配置文件请参考
[数据分片](/cn/user-manual/shardingsphere-jdbc/yaml-config/rules/sharding/) ;
diff --git
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.en.md
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.en.md
index 000034b664f..49d259a6a2e 100644
---
a/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.en.md
+++
b/docs/document/content/user-manual/shardingsphere-proxy/distsql/syntax/rdl/rule-definition/sharding.en.md
@@ -113,7 +113,6 @@ auditorAlgorithmDefinition:
- `keyGenerateStrategyType` specifies the distributed primary key generation
strategy, please refer to [Key Generate
Algorithm](/en/user-manual/common-config/builtin-algorithm/keygen/)
- `auditorAlgorithmType` specifies the sharding audit strategy, please refer
to [Sharding Audit
Algorithm](/cn/user-manual/common-config/builtin-algorithm/audit/);
- Duplicate `tableName` will not be created
-- `shardingAlgorithm` can be reused by different `Sharding Table Rule`, so
when executing `DROP SHARDING TABLE RULE`, the corresponding
`shardingAlgorithm` will not be removed
- To remove `shardingAlgorithm`, please execute `DROP SHARDING ALGORITHM`
- `strategyType` specifies the sharding strategy, please refer to[Sharding
Strategy](/en/features/sharding/concept/sharding/#sharding-strategy)
- `Sharding Table Rule` supports both `Auto Table` and `Table` at the same
time. The two types are different in syntax. For the corresponding
configuration file, please refer to
[Sharding](/en/user-manual/shardingsphere-jdbc/yaml-config/rules/sharding/)
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
index bea063e7581..5d5ef03df53 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingTableRuleStatementUpdater.java
@@ -27,6 +27,7 @@ import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePrecondition
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingTableRuleStatement;
@@ -113,18 +114,16 @@ public final class DropShardingTableRuleStatementUpdater
implements RuleDefiniti
@Override
public boolean updateCurrentRuleConfiguration(final
DropShardingTableRuleStatement sqlStatement, final ShardingRuleConfiguration
currentRuleConfig) {
- boolean dropUnusedAlgorithms = sqlStatement.isDropUnusedAlgorithms();
Collection<String> toBeDroppedShardingTableNames =
getToBeDroppedShardingTableNames(sqlStatement);
toBeDroppedShardingTableNames.forEach(each ->
dropShardingTable(currentRuleConfig, each));
- if (dropUnusedAlgorithms) {
- toBeDroppedShardingTableNames.forEach(each ->
dropUnusedAlgorithms(currentRuleConfig));
- }
+ dropUnusedAlgorithms(currentRuleConfig);
+ dropUnusedKeyGenerator(currentRuleConfig);
+ dropUnusedAuditor(currentRuleConfig);
return false;
}
private void dropUnusedAlgorithms(final ShardingRuleConfiguration
currentRuleConfig) {
- Collection<String> inUsedAlgorithms =
currentRuleConfig.getTables().stream()
- .map(each -> Arrays.asList(each.getTableShardingStrategy(),
each.getDatabaseShardingStrategy()))
+ Collection<String> inUsedAlgorithms =
currentRuleConfig.getTables().stream().map(each ->
Arrays.asList(each.getTableShardingStrategy(),
each.getDatabaseShardingStrategy()))
.flatMap(Collection::stream).filter(Objects::nonNull).map(ShardingStrategyConfiguration::getShardingAlgorithmName).collect(Collectors.toSet());
inUsedAlgorithms.addAll(currentRuleConfig.getAutoTables().stream().map(each ->
each.getShardingStrategy().getShardingAlgorithmName()).collect(Collectors.toSet()));
if (null != currentRuleConfig.getDefaultTableShardingStrategy()) {
@@ -137,6 +136,28 @@ public final class DropShardingTableRuleStatementUpdater
implements RuleDefiniti
unusedAlgorithms.forEach(each ->
currentRuleConfig.getShardingAlgorithms().remove(each));
}
+ private void dropUnusedKeyGenerator(final ShardingRuleConfiguration
currentRuleConfig) {
+ Collection<String> inUsedKeyGenerators =
currentRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getKeyGenerateStrategy).filter(Objects::nonNull)
+
.map(KeyGenerateStrategyConfiguration::getKeyGeneratorName).collect(Collectors.toSet());
+
inUsedKeyGenerators.addAll(currentRuleConfig.getAutoTables().stream().map(each
->
each.getKeyGenerateStrategy().getKeyGeneratorName()).collect(Collectors.toSet()));
+ if (null != currentRuleConfig.getDefaultKeyGenerateStrategy()) {
+
inUsedKeyGenerators.add(currentRuleConfig.getDefaultKeyGenerateStrategy().getKeyGeneratorName());
+ }
+ Collection<String> unusedKeyGenerators =
currentRuleConfig.getKeyGenerators().keySet().stream().filter(each ->
!inUsedKeyGenerators.contains(each)).collect(Collectors.toSet());
+ unusedKeyGenerators.forEach(each ->
currentRuleConfig.getKeyGenerators().remove(each));
+ }
+
+ private void dropUnusedAuditor(final ShardingRuleConfiguration
currentRuleConfig) {
+ Collection<String> inUsedAuditors =
currentRuleConfig.getTables().stream().map(ShardingTableRuleConfiguration::getAuditStrategy).filter(Objects::nonNull)
+ .flatMap(each ->
each.getAuditorNames().stream()).collect(Collectors.toSet());
+
inUsedAuditors.addAll(currentRuleConfig.getAutoTables().stream().flatMap(each
->
each.getAuditStrategy().getAuditorNames().stream()).collect(Collectors.toSet()));
+ if (null != currentRuleConfig.getDefaultAuditStrategy()) {
+
inUsedAuditors.addAll(currentRuleConfig.getDefaultAuditStrategy().getAuditorNames());
+ }
+ Collection<String> unusedAuditors =
currentRuleConfig.getAuditors().keySet().stream().filter(each ->
!inUsedAuditors.contains(each)).collect(Collectors.toSet());
+ unusedAuditors.forEach(each ->
currentRuleConfig.getAuditors().remove(each));
+ }
+
private void dropShardingTable(final ShardingRuleConfiguration
currentRuleConfig, final String tableName) {
currentRuleConfig.getTables().removeAll(currentRuleConfig.getTables().stream().filter(each
->
tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()));
currentRuleConfig.getAutoTables().removeAll(currentRuleConfig.getAutoTables().stream().filter(each
->
tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()));
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
index 7be30377f5b..3abe646521f 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingTableRuleStatementUpdaterTest.java
@@ -50,37 +50,37 @@ public final class
DropShardingTableRuleStatementUpdaterTest {
@Test(expected = MissingRequiredRuleException.class)
public void assertCheckSQLStatementWithoutCurrentRule() throws
RuleDefinitionViolationException {
new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), new DropShardingTableRuleStatement(false,
Collections.emptyList(), false), null);
+ mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), new DropShardingTableRuleStatement(false,
Collections.emptyList()), null);
}
@Test(expected = MissingRequiredRuleException.class)
public void assertCheckSQLStatementWithoutExistedTableRule() throws
RuleDefinitionViolationException {
new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order", false), new
ShardingRuleConfiguration());
+ mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order"), new
ShardingRuleConfiguration());
}
@Test
public void
assertCheckSQLStatementIfExistsWithNullCurrentRuleConfiguration() throws
RuleDefinitionViolationException {
- DropShardingTableRuleStatement statement = new
DropShardingTableRuleStatement(true, Collections.singleton(new
TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists"))), false);
+ DropShardingTableRuleStatement statement = new
DropShardingTableRuleStatement(true, Collections.singleton(new
TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists"))));
new
DropShardingTableRuleStatementUpdater().checkSQLStatement(mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), statement, null);
}
@Test
public void assertCheckSQLStatementIfExists() throws
RuleDefinitionViolationException {
- DropShardingTableRuleStatement statement = new
DropShardingTableRuleStatement(true, Collections.singleton(new
TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists"))), false);
+ DropShardingTableRuleStatement statement = new
DropShardingTableRuleStatement(true, Collections.singleton(new
TableNameSegment(0, 3, new IdentifierValue("t_order_if_exists"))));
new
DropShardingTableRuleStatementUpdater().checkSQLStatement(mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), statement, new ShardingRuleConfiguration());
}
@Test(expected = RuleInUsedException.class)
public void assertCheckSQLStatementWithBindingTableRule() throws
RuleDefinitionViolationException {
new DropShardingTableRuleStatementUpdater().checkSQLStatement(
- mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order_item", false),
createCurrentRuleConfiguration());
+ mock(ShardingSphereDatabase.class,
Answers.RETURNS_DEEP_STUBS), createSQLStatement("t_order_item"),
createCurrentRuleConfiguration());
}
@Test
public void assertUpdate() {
ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
- new
DropShardingTableRuleStatementUpdater().updateCurrentRuleConfiguration(createSQLStatement("t_order",
false), currentRuleConfig);
+ new
DropShardingTableRuleStatementUpdater().updateCurrentRuleConfiguration(createSQLStatement("t_order"),
currentRuleConfig);
assertFalse(getShardingTables(currentRuleConfig).contains("t_order"));
assertTrue(getBindingTables(currentRuleConfig).contains("t_order_item"));
}
@@ -88,7 +88,7 @@ public final class DropShardingTableRuleStatementUpdaterTest {
@Test
public void assertUpdateWithDifferentCase() {
ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
- new
DropShardingTableRuleStatementUpdater().updateCurrentRuleConfiguration(createSQLStatement("T_ORDER",
false), currentRuleConfig);
+ new
DropShardingTableRuleStatementUpdater().updateCurrentRuleConfiguration(createSQLStatement("T_ORDER"),
currentRuleConfig);
assertFalse(getShardingTables(currentRuleConfig).contains("t_order"));
assertTrue(getBindingTables(currentRuleConfig).contains("t_order_item"));
}
@@ -96,15 +96,17 @@ public final class
DropShardingTableRuleStatementUpdaterTest {
@Test
public void assertDropRuleAndUnusedAlgorithm() {
ShardingRuleConfiguration currentRuleConfig =
createCurrentRuleConfiguration();
- DropShardingTableRuleStatement sqlStatement =
createSQLStatement("t_order", true);
+ DropShardingTableRuleStatement sqlStatement =
createSQLStatement("t_order");
new
DropShardingTableRuleStatementUpdater().updateCurrentRuleConfiguration(sqlStatement,
currentRuleConfig);
assertFalse(getShardingTables(currentRuleConfig).contains("t_order"));
assertTrue(getBindingTables(currentRuleConfig).contains("t_order_item"));
assertThat(currentRuleConfig.getShardingAlgorithms().size(), is(2));
+ assertThat(currentRuleConfig.getKeyGenerators().size(), is(0));
+ assertThat(currentRuleConfig.getAuditors().size(), is(0));
}
- private DropShardingTableRuleStatement createSQLStatement(final String
tableName, final boolean dropUnusedAlgorithms) {
- return new DropShardingTableRuleStatement(false,
Collections.singleton(new TableNameSegment(0, 3, new
IdentifierValue(tableName))), dropUnusedAlgorithms);
+ private DropShardingTableRuleStatement createSQLStatement(final String
tableName) {
+ return new DropShardingTableRuleStatement(false,
Collections.singleton(new TableNameSegment(0, 3, new
IdentifierValue(tableName))));
}
private ShardingRuleConfiguration createCurrentRuleConfiguration() {
@@ -118,6 +120,8 @@ public final class
DropShardingTableRuleStatementUpdaterTest {
result.getShardingAlgorithms().put("unused_algorithm", null);
result.getShardingAlgorithms().put("t_order_item_algorithm", null);
result.getShardingAlgorithms().put("default_table_strategy", null);
+ result.getKeyGenerators().put("unused_key_generator", null);
+ result.getAuditors().put("unused_auditor", null);
return result;
}
diff --git
a/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
b/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
index 4d84eb13276..dd313fb5f17 100644
---
a/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
+++
b/features/sharding/distsql/parser/src/main/antlr4/imports/sharding/RDLStatement.g4
@@ -28,7 +28,7 @@ alterShardingTableRule
;
dropShardingTableRule
- : DROP SHARDING TABLE RULE ifExists? tableName (COMMA tableName)*
withUnusedAlgorithmsClause?
+ : DROP SHARDING TABLE RULE ifExists? tableName (COMMA tableName)*
;
createShardingTableReferenceRule
@@ -198,7 +198,3 @@ strategyType
ifExists
: IF EXISTS
;
-
-withUnusedAlgorithmsClause
- : WITH UNUSED ALGORITHMS
- ;
diff --git
a/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
b/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
index 13adfe8cf62..eb5c53be931 100644
---
a/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
+++
b/features/sharding/distsql/parser/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/core/ShardingDistSQLStatementVisitor.java
@@ -184,7 +184,7 @@ public final class ShardingDistSQLStatementVisitor extends
ShardingDistSQLStatem
@Override
public ASTNode visitDropShardingTableRule(final
DropShardingTableRuleContext ctx) {
return new DropShardingTableRuleStatement(null != ctx.ifExists(),
- ctx.tableName().stream().map(each -> (TableNameSegment)
visit(each)).collect(Collectors.toList()), null !=
ctx.withUnusedAlgorithmsClause());
+ ctx.tableName().stream().map(each -> (TableNameSegment)
visit(each)).collect(Collectors.toList()));
}
@Override
diff --git
a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
index 918d2030db3..e3bd34ea9c0 100644
---
a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
+++
b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/statement/DropShardingTableRuleStatement.java
@@ -31,11 +31,8 @@ public final class DropShardingTableRuleStatement extends
DropRuleStatement {
private final Collection<TableNameSegment> tableNames;
- private final boolean dropUnusedAlgorithms;
-
- public DropShardingTableRuleStatement(final boolean ifExists, final
Collection<TableNameSegment> tableNames, final boolean dropUnusedAlgorithms) {
+ public DropShardingTableRuleStatement(final boolean ifExists, final
Collection<TableNameSegment> tableNames) {
super(ifExists);
this.tableNames = tableNames;
- this.dropUnusedAlgorithms = dropUnusedAlgorithms;
}
}