This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 e2a30e77afe Optimize ShardingTableRuleStatementChecker, remove peek in
stream. (#25800)
e2a30e77afe is described below
commit e2a30e77afe9e9235d62818ef89c0af7f46d2dcf
Author: Raigor <[email protected]>
AuthorDate: Sat May 20 08:55:56 2023 +0800
Optimize ShardingTableRuleStatementChecker, remove peek in stream. (#25800)
---
.../checker/ShardingTableRuleStatementChecker.java | 63 +++++++---------------
.../ShardingTableRuleStatementConverter.java | 13 ++---
.../checker/ShardingRuleStatementCheckerTest.java | 13 +++--
.../ShardingRuleStatementConverterTest.java | 6 +--
.../strategy/KeyGenerateStrategySegment.java | 12 +----
5 files changed, 37 insertions(+), 70 deletions(-)
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index 7371b178474..f54ec7c8e3a 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -21,18 +21,16 @@ import com.google.common.base.Splitter;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
-import
org.apache.shardingsphere.sharding.exception.strategy.InvalidShardingStrategyConfigurationException;
-import
org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException;
import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import
org.apache.shardingsphere.distsql.handler.exception.rule.InvalidRuleConfigurationException;
import
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
import
org.apache.shardingsphere.distsql.handler.exception.storageunit.MissingRequiredStorageUnitsException;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.infra.datanode.DataNode;
+import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
@@ -47,6 +45,7 @@ import
org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorith
import
org.apache.shardingsphere.sharding.distsql.handler.converter.ShardingTableRuleStatementConverter;
import
org.apache.shardingsphere.sharding.distsql.handler.enums.ShardingStrategyType;
import
org.apache.shardingsphere.sharding.distsql.parser.segment.strategy.AuditStrategySegment;
+import
org.apache.shardingsphere.sharding.distsql.parser.segment.strategy.KeyGenerateStrategySegment;
import
org.apache.shardingsphere.sharding.distsql.parser.segment.strategy.ShardingAuditorSegment;
import
org.apache.shardingsphere.sharding.distsql.parser.segment.strategy.ShardingStrategySegment;
import
org.apache.shardingsphere.sharding.distsql.parser.segment.table.AbstractTableRuleSegment;
@@ -54,13 +53,13 @@ import
org.apache.shardingsphere.sharding.distsql.parser.segment.table.AutoTable
import
org.apache.shardingsphere.sharding.distsql.parser.segment.table.TableRuleSegment;
import
org.apache.shardingsphere.sharding.exception.algorithm.sharding.ShardingAlgorithmClassImplementationException;
import
org.apache.shardingsphere.sharding.exception.metadata.ShardingTableRuleNotFoundException;
+import
org.apache.shardingsphere.sharding.exception.strategy.InvalidShardingStrategyConfigurationException;
import
org.apache.shardingsphere.sharding.rule.BindingTableCheckedConfiguration;
import org.apache.shardingsphere.sharding.rule.TableRule;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAuditAlgorithm;
-import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
@@ -69,7 +68,6 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
@@ -147,11 +145,10 @@ public final class ShardingTableRuleStatementChecker {
String databaseName = database.getName();
checkTables(databaseName, rules, currentRuleConfig, isCreated,
ifNotExists);
checkDataSources(databaseName, rules, database);
- checkKeyGenerators(rules, currentRuleConfig);
+ checkKeyGenerators(rules);
checkAuditors(rules);
- Map<String, List<AbstractTableRuleSegment>> groupedTableRule =
groupingByClassType(rules);
-
checkAutoTableRule(groupedTableRule.getOrDefault(AutoTableRuleSegment.class.getSimpleName(),
Collections.emptyList()));
- checkTableRule(databaseName,
groupedTableRule.getOrDefault(TableRuleSegment.class.getSimpleName(),
Collections.emptyList()));
+ checkAutoTableRule(rules.stream().filter(each -> each instanceof
AutoTableRuleSegment).map(AutoTableRuleSegment.class::cast).collect(Collectors.toList()));
+ checkTableRule(databaseName, rules.stream().filter(each -> each
instanceof
TableRuleSegment).map(TableRuleSegment.class::cast).collect(Collectors.toList()));
if (!isCreated) {
checkBindingTableRules(rules, currentRuleConfig);
}
@@ -175,10 +172,6 @@ public final class ShardingTableRuleStatementChecker {
defaultDatabaseShardingStrategyConfig,
defaultTableShardingStrategyConfig, checkedConfig.getDefaultShardingColumn()));
}
- private static Map<String, List<AbstractTableRuleSegment>>
groupingByClassType(final Collection<AbstractTableRuleSegment> rules) {
- return rules.stream().collect(Collectors.groupingBy(each ->
each.getClass().getSimpleName()));
- }
-
private static Collection<String> getDataSourceNames(final
Collection<ShardingTableRuleConfiguration> tableRuleConfigs,
final
Collection<ShardingAutoTableRuleConfiguration> autoTableRuleConfigs, final
Collection<String> dataSourceNames) {
if (tableRuleConfigs.isEmpty() && autoTableRuleConfigs.isEmpty()) {
@@ -199,7 +192,7 @@ public final class ShardingTableRuleStatementChecker {
}
private static Collection<String> getDataSourceNames(final
Collection<String> actualDataNodes) {
- Collection<String> result = new HashSet<>();
+ Collection<String> result = new LinkedHashSet<>();
for (String each : actualDataNodes) {
result.add(isValidDataNode(each) ? new
DataNode(each).getDataSourceName() : each);
}
@@ -396,38 +389,27 @@ public final class ShardingTableRuleStatementChecker {
return result;
}
- private static void checkKeyGenerators(final
Collection<AbstractTableRuleSegment> rules, final ShardingRuleConfiguration
currentRuleConfig) {
- Set<String> notExistKeyGenerator = new LinkedHashSet<>(rules.size());
- Set<AlgorithmSegment> requiredKeyGenerators = new
LinkedHashSet<>(rules.size());
+ private static void checkKeyGenerators(final
Collection<AbstractTableRuleSegment> rules) {
rules.stream().map(AbstractTableRuleSegment::getKeyGenerateStrategySegment).filter(Objects::nonNull)
- .peek(each -> each.getKeyGenerateAlgorithmName()
- .filter(optional -> null == currentRuleConfig ||
!currentRuleConfig.getKeyGenerators().containsKey(optional)).ifPresent(notExistKeyGenerator::add))
- .filter(each ->
!each.getKeyGenerateAlgorithmName().isPresent()).filter(each -> null !=
each.getKeyGenerateAlgorithmSegment())
- .forEach(each ->
requiredKeyGenerators.add(each.getKeyGenerateAlgorithmSegment()));
- ShardingSpherePreconditions.checkState(notExistKeyGenerator.isEmpty(),
() -> new MissingRequiredAlgorithmException("key generator",
notExistKeyGenerator));
- requiredKeyGenerators.forEach(each ->
TypedSPILoader.checkService(KeyGenerateAlgorithm.class, each.getName(),
each.getProps()));
+
.map(KeyGenerateStrategySegment::getKeyGenerateAlgorithmSegment)
+ .forEach(each ->
TypedSPILoader.checkService(KeyGenerateAlgorithm.class, each.getName(),
each.getProps()));
}
private static void checkAuditors(final
Collection<AbstractTableRuleSegment> rules) {
Collection<AuditStrategySegment> auditStrategySegments =
rules.stream().map(AbstractTableRuleSegment::getAuditStrategySegment).filter(Objects::nonNull).collect(Collectors.toList());
- Set<AlgorithmSegment> requiredAuditors = new LinkedHashSet<>();
+ Collection<AlgorithmSegment> requiredAuditors = new LinkedHashSet<>();
for (AuditStrategySegment each : auditStrategySegments) {
- requiredAuditors.addAll(each.getAuditorSegments().stream()
-
.map(ShardingAuditorSegment::getAlgorithmSegment).collect(Collectors.toList()));
+
requiredAuditors.addAll(each.getAuditorSegments().stream().map(ShardingAuditorSegment::getAlgorithmSegment).collect(Collectors.toList()));
}
requiredAuditors.forEach(each ->
TypedSPILoader.checkService(ShardingAuditAlgorithm.class, each.getName(),
each.getProps()));
}
- private static void checkAutoTableRule(final
Collection<AbstractTableRuleSegment> rules) {
- Collection<AutoTableRuleSegment> autoTableRules =
rules.stream().map(AutoTableRuleSegment.class::cast).collect(Collectors.toList());
- Optional<AlgorithmSegment> anyAutoTableRule =
autoTableRules.stream().map(AutoTableRuleSegment::getShardingAlgorithmSegment).filter(Objects::nonNull).findAny();
- if (anyAutoTableRule.isPresent()) {
- checkAutoTableShardingAlgorithms(autoTableRules);
- }
+ private static void checkAutoTableRule(final
Collection<AutoTableRuleSegment> autoTableRules) {
+ checkAutoTableShardingAlgorithms(autoTableRules);
}
- private static void checkAutoTableShardingAlgorithms(final
Collection<AutoTableRuleSegment> rules) {
- rules.forEach(each -> {
+ private static void checkAutoTableShardingAlgorithms(final
Collection<AutoTableRuleSegment> autoTableRules) {
+ autoTableRules.forEach(each -> {
ShardingSpherePreconditions.checkState(TypedSPILoader.findService(
ShardingAlgorithm.class,
each.getShardingAlgorithmSegment().getName(),
each.getShardingAlgorithmSegment().getProps()).isPresent(),
() -> new
InvalidAlgorithmConfigurationException("sharding",
each.getShardingAlgorithmSegment().getName()));
@@ -437,13 +419,8 @@ public final class ShardingTableRuleStatementChecker {
});
}
- private static void checkTableRule(final String databaseName, final
Collection<AbstractTableRuleSegment> rules) {
- Collection<TableRuleSegment> tableRules =
rules.stream().map(TableRuleSegment.class::cast).collect(Collectors.toList());
- Optional<ShardingStrategySegment> anyTableRule =
tableRules.stream().map(each ->
Arrays.asList(each.getDatabaseStrategySegment(),
each.getTableStrategySegment()))
-
.flatMap(Collection::stream).filter(Objects::nonNull).findAny();
- if (anyTableRule.isPresent()) {
- checkStrategy(databaseName, tableRules);
- }
+ private static void checkTableRule(final String databaseName, final
Collection<TableRuleSegment> tableRules) {
+ checkStrategy(databaseName, tableRules);
}
private static void checkStrategy(final String databaseName, final
Collection<TableRuleSegment> rules) {
@@ -499,8 +476,8 @@ public final class ShardingTableRuleStatementChecker {
return
ShardingStrategyType.getValueOf(shardingStrategySegment.getType()).isValid(shardingStrategySegment.getShardingColumn())
&& null != shardingStrategySegment.getShardingAlgorithm();
}
- private static <T extends AbstractTableRuleSegment> Collection<String>
getRequiredTables(final Collection<T> rules) {
- return
rules.stream().map(AbstractTableRuleSegment::getDataSourceNodes).flatMap(Collection::stream)
+ private static Collection<String> getRequiredTables(final
Collection<TableRuleSegment> rules) {
+ return
rules.stream().map(TableRuleSegment::getDataSourceNodes).flatMap(Collection::stream)
.map(ShardingTableRuleStatementChecker::parseDateSource).map(ShardingTableRuleStatementChecker::getTableNames).flatMap(Collection::stream).collect(Collectors.toList());
}
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
index 889b7261e81..d0f4b27959c 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/converter/ShardingTableRuleStatementConverter.java
@@ -40,7 +40,6 @@ import
org.apache.shardingsphere.sharding.distsql.parser.segment.table.TableRule
import java.util.Collection;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -76,11 +75,8 @@ public final class ShardingTableRuleStatementConverter {
private static Map<String, AlgorithmConfiguration>
createKeyGeneratorConfiguration(final AbstractTableRuleSegment rule) {
Map<String, AlgorithmConfiguration> result = new HashMap<>();
-
Optional.ofNullable(rule.getKeyGenerateStrategySegment()).ifPresent(optional ->
{
- if (!optional.getKeyGenerateAlgorithmName().isPresent()) {
- result.put(getKeyGeneratorName(rule.getLogicTable(),
optional.getKeyGenerateAlgorithmSegment().getName()),
createAlgorithmConfiguration(optional.getKeyGenerateAlgorithmSegment()));
- }
- });
+
Optional.ofNullable(rule.getKeyGenerateStrategySegment()).ifPresent(optional ->
result.put(getKeyGeneratorName(rule.getLogicTable(),
optional.getKeyGenerateAlgorithmSegment().getName()),
+
createAlgorithmConfiguration(optional.getKeyGenerateAlgorithmSegment())));
return result;
}
@@ -167,14 +163,11 @@ public final class ShardingTableRuleStatementConverter {
}
private static KeyGenerateStrategyConfiguration
createKeyGenerateStrategyConfiguration(final String logicTable, final
KeyGenerateStrategySegment segment) {
- if (segment.getKeyGenerateAlgorithmName().isPresent()) {
- return new
KeyGenerateStrategyConfiguration(segment.getKeyGenerateColumn(),
segment.getKeyGenerateAlgorithmName().get());
- }
return new
KeyGenerateStrategyConfiguration(segment.getKeyGenerateColumn(),
getKeyGeneratorName(logicTable,
segment.getKeyGenerateAlgorithmSegment().getName()));
}
private static ShardingAuditStrategyConfiguration
createShardingAuditStrategyConfiguration(final AuditStrategySegment segment) {
- List<String> auditorNames =
segment.getAuditorSegments().stream().map(ShardingAuditorSegment::getAuditorName).collect(Collectors.toList());
+ Collection<String> auditorNames =
segment.getAuditorSegments().stream().map(ShardingAuditorSegment::getAuditorName).collect(Collectors.toList());
return new ShardingAuditStrategyConfiguration(auditorNames,
segment.isAllowHintDisable());
}
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
index 2560516f76d..7dabfefae13 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/checker/ShardingRuleStatementCheckerTest.java
@@ -84,13 +84,20 @@ class ShardingRuleStatementCheckerTest {
}
@Test
- void assertCheckerAutoTableSuccess() {
+ void assertCheckCreatSuccess() {
Collection<AbstractTableRuleSegment> rules = new LinkedList<>();
rules.add(createCompleteAutoTableRule());
rules.add(createCompleteTableRule());
ShardingTableRuleStatementChecker.checkCreation(database, rules,
false, shardingRuleConfig);
- rules.clear();
- rules.add(new AutoTableRuleSegment("t_order", Arrays.asList("ds_0",
"ds_1")));
+ }
+
+ @Test
+ void assertCheckAlterSuccess() {
+ Collection<AbstractTableRuleSegment> rules = new LinkedList<>();
+ AutoTableRuleSegment autoTableRuleSegment = new
AutoTableRuleSegment("t_order", Arrays.asList("ds_0", "ds_1"));
+ autoTableRuleSegment.setShardingColumn("order_id");
+ autoTableRuleSegment.setShardingAlgorithmSegment(new
AlgorithmSegment("CORE.AUTO.FIXTURE", PropertiesBuilder.build(new
Property("sharding-count", "2"))));
+ rules.add(autoTableRuleSegment);
ShardingTableRuleStatementChecker.checkAlteration(database, rules,
shardingRuleConfig);
}
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/converter/ShardingRuleStatementConverterTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/converter/ShardingRuleStatementConverterTest.java
index ba5263be8fc..fc9bb167544 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/converter/ShardingRuleStatementConverterTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/converter/ShardingRuleStatementConverterTest.java
@@ -65,11 +65,11 @@ class ShardingRuleStatementConverterTest {
assertThat(tableRule.getKeyGenerateStrategy().getKeyGeneratorName(),
is("t_order_snowflake"));
assertThat(tableRule.getKeyGenerateStrategy().getColumn(),
is("order_id"));
autoTableRule = autoTableConfigs.next();
-
assertThat(autoTableRule.getKeyGenerateStrategy().getKeyGeneratorName(),
is("snowflake_algorithm"));
+
assertThat(autoTableRule.getKeyGenerateStrategy().getKeyGeneratorName(),
is("t_order_2_snowflake"));
assertThat(config.getShardingAlgorithms().size(), is(4));
assertThat(config.getShardingAlgorithms().get("t_order_mod").getType(),
is("mod"));
assertThat(config.getShardingAlgorithms().get("t_order_mod").getProps().getProperty("sharding_count"),
is("2"));
- assertThat(config.getKeyGenerators().size(), is(1));
+ assertThat(config.getKeyGenerators().size(), is(2));
assertThat(config.getKeyGenerators().get("t_order_snowflake").getType(),
is("snowflake"));
assertThat(config.getKeyGenerators().get("t_order_snowflake").getProps().getProperty(""),
is(""));
assertThat(config.getAuditors().get("sharding_key_required_auditor").getType(),
is("DML_SHARDING_CONDITIONS"));
@@ -103,7 +103,7 @@ class ShardingRuleStatementConverterTest {
AutoTableRuleSegment autoTableRuleSegment2 = new
AutoTableRuleSegment("t_order_2", Arrays.asList("ds0", "ds1"));
autoTableRuleSegment2.setShardingColumn("order_id");
autoTableRuleSegment2.setShardingAlgorithmSegment(new
AlgorithmSegment("MOD", PropertiesBuilder.build(new Property("sharding_count",
"2"))));
- autoTableRuleSegment2.setKeyGenerateStrategySegment(new
KeyGenerateStrategySegment("order_id", "snowflake_algorithm"));
+ autoTableRuleSegment2.setKeyGenerateStrategySegment(new
KeyGenerateStrategySegment("order_id", new AlgorithmSegment("snowflake", new
Properties())));
autoTableRuleSegment2.setAuditStrategySegment(new
AuditStrategySegment(Collections.singleton(new
ShardingAuditorSegment("sharding_key_required_auditor",
new AlgorithmSegment("DML_SHARDING_CONDITIONS", new
Properties()))), true));
TableRuleSegment tableRuleSegment = new TableRuleSegment("t_order",
Arrays.asList("ds0", "ds1"),
diff --git
a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/strategy/KeyGenerateStrategySegment.java
b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/strategy/KeyGenerateStrategySegment.java
index 32c4fed27dc..867246fc8fa 100644
---
a/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/strategy/KeyGenerateStrategySegment.java
+++
b/features/sharding/distsql/statement/src/main/java/org/apache/shardingsphere/sharding/distsql/parser/segment/strategy/KeyGenerateStrategySegment.java
@@ -21,8 +21,6 @@ import lombok.Getter;
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
import org.apache.shardingsphere.sql.parser.api.ASTNode;
-import java.util.Optional;
-
/**
* Key generate strategy segment.
*/
@@ -31,18 +29,10 @@ public final class KeyGenerateStrategySegment implements
ASTNode {
private final String keyGenerateColumn;
- private final Optional<String> keyGenerateAlgorithmName;
-
- private AlgorithmSegment keyGenerateAlgorithmSegment;
-
- public KeyGenerateStrategySegment(final String keyGenerateColumn, final
String keyGenerateAlgorithmName) {
- this.keyGenerateColumn = keyGenerateColumn;
- this.keyGenerateAlgorithmName =
Optional.ofNullable(keyGenerateAlgorithmName);
- }
+ private final AlgorithmSegment keyGenerateAlgorithmSegment;
public KeyGenerateStrategySegment(final String keyGenerateColumn, final
AlgorithmSegment keyGenerateAlgorithmSegment) {
this.keyGenerateColumn = keyGenerateColumn;
- this.keyGenerateAlgorithmName = Optional.empty();
this.keyGenerateAlgorithmSegment = keyGenerateAlgorithmSegment;
}
}