This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 2483912aa91 Decouple DropReadwriteSplittingRuleStatementUpdater and
ResourceRequiredRuleConfiguration (#18586)
2483912aa91 is described below
commit 2483912aa911f9e00d4822cc0c82a75d40fbe808
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Jun 25 19:37:07 2022 +0800
Decouple DropReadwriteSplittingRuleStatementUpdater and
ResourceRequiredRuleConfiguration (#18586)
* Decouple DropReadwriteSplittingRuleStatementUpdater and
ResourceRequiredRuleConfiguration
* Decouple DropReadwriteSplittingRuleStatementUpdater and
ResourceRequiredRuleConfiguration
---
...DropReadwriteSplittingRuleStatementUpdater.java | 33 ++++++++++++++++++----
...ReadwriteSplittingRuleStatementUpdaterTest.java | 15 ++++++----
.../shardingsphere/sharding/rule/ShardingRule.java | 4 +--
.../checker/ShardingTableRuleStatementChecker.java | 2 +-
.../mode/manager/ContextManager.java | 10 +++----
5 files changed, 44 insertions(+), 20 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/re
[...]
index edc2fa17eda..7321aa8d00c 100644
---
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
+++
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdater.java
@@ -17,17 +17,21 @@
package org.apache.shardingsphere.readwritesplitting.distsql.handler.update;
-import
org.apache.shardingsphere.infra.config.function.ResourceRequiredRuleConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.DropReadwriteSplittingRuleStatement;
+import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule;
import java.util.Collection;
+import java.util.HashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
@@ -46,7 +50,7 @@ public final class DropReadwriteSplittingRuleStatementUpdater
implements RuleDef
String databaseName = database.getName();
checkCurrentRuleConfiguration(databaseName, currentRuleConfig);
checkToBeDroppedRuleNames(databaseName, sqlStatement,
currentRuleConfig);
- checkToBeDroppedInUsed(databaseName, sqlStatement, database);
+ checkToBeDroppedInUsed(database, sqlStatement);
}
private void checkCurrentRuleConfiguration(final String databaseName,
final ReadwriteSplittingRuleConfiguration currentRuleConfig) throws
RequiredRuleMissedException {
@@ -67,15 +71,32 @@ public final class
DropReadwriteSplittingRuleStatementUpdater implements RuleDef
}
}
- private void checkToBeDroppedInUsed(final String databaseName, final
DropReadwriteSplittingRuleStatement sqlStatement, final ShardingSphereDatabase
database) throws RuleInUsedException {
- Collection<String> resourceBeUsed =
database.getRuleMetaData().findRuleConfigurations(ResourceRequiredRuleConfiguration.class).stream()
-
.map(ResourceRequiredRuleConfiguration::getRequiredResource).flatMap(Collection::stream).collect(Collectors.toSet());
+ private void checkToBeDroppedInUsed(final ShardingSphereDatabase database,
final DropReadwriteSplittingRuleStatement sqlStatement) throws
RuleInUsedException {
+ Collection<String> resourceBeUsed = getInUsedResources(database);
Collection<String> ruleInUsed =
sqlStatement.getRuleNames().stream().filter(resourceBeUsed::contains).collect(Collectors.toSet());
if (!ruleInUsed.isEmpty()) {
- throw new RuleInUsedException("Readwrite splitting", databaseName,
ruleInUsed);
+ throw new RuleInUsedException("Readwrite splitting",
database.getName(), ruleInUsed);
}
}
+ private Collection<String> getInUsedResources(final ShardingSphereDatabase
database) {
+ Collection<String> result = new HashSet<>();
+ for (DataSourceContainedRule each :
database.getRuleMetaData().findRules(DataSourceContainedRule.class)) {
+ if (each instanceof ReadwriteSplittingRule) {
+ continue;
+ }
+ Collection<String> actualDataSources = new HashSet<>();
+
each.getDataSourceMapper().values().forEach(actualDataSources::addAll);
+ result.addAll(actualDataSources);
+ }
+ for (DataNodeContainedRule each :
database.getRuleMetaData().findRules(DataNodeContainedRule.class)) {
+ Collection<DataNode> actualDataNodes = new HashSet<>();
+ each.getAllDataNodes().values().forEach(actualDataNodes::addAll);
+
result.addAll(actualDataNodes.stream().map(DataNode::getDataSourceName).collect(Collectors.toSet()));
+ }
+ return result;
+ }
+
@Override
public boolean updateCurrentRuleConfiguration(final
DropReadwriteSplittingRuleStatement sqlStatement, final
ReadwriteSplittingRuleConfiguration currentRuleConfig) {
for (String each : sqlStatement.getRuleNames()) {
diff --git
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingspher
[...]
index 3a66eaeb2a9..98f1f7388a0 100644
---
a/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java
+++
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/update/DropReadwriteSplittingRuleStatementUpdaterTest.java
@@ -18,11 +18,13 @@
package org.apache.shardingsphere.readwritesplitting.distsql.handler.update;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-import
org.apache.shardingsphere.infra.config.function.ResourceRequiredRuleConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNode;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleDefinitionViolationException;
import
org.apache.shardingsphere.infra.distsql.exception.rule.RuleInUsedException;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataNodeContainedRule;
+import
org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.DropReadwriteSplittingRuleStatement;
@@ -42,7 +44,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
@@ -65,7 +67,6 @@ public final class
DropReadwriteSplittingRuleStatementUpdaterTest {
@Test
public void assertCheckSQLStatementWithIfExists() throws
RuleDefinitionViolationException {
-
when(database.getRuleMetaData().findRuleConfigurations(ResourceRequiredRuleConfiguration.class)).thenReturn(Collections.emptyList());
updater.checkSQLStatement(database, new
DropReadwriteSplittingRuleStatement(true,
Collections.singleton("readwrite_ds")),
new
ReadwriteSplittingRuleConfiguration(Collections.emptyList(),
Collections.emptyMap()));
updater.checkSQLStatement(database, new
DropReadwriteSplittingRuleStatement(true,
Collections.singleton("readwrite_ds")), null);
@@ -73,8 +74,12 @@ public final class
DropReadwriteSplittingRuleStatementUpdaterTest {
@Test(expected = RuleInUsedException.class)
public void assertCheckSQLStatementWithInUsed() throws
RuleDefinitionViolationException {
- when(database.getRuleMetaData().findRuleConfigurations(any()))
-
.thenReturn(Collections.singletonList((ResourceRequiredRuleConfiguration) () ->
Collections.singleton("readwrite_ds")));
+ DataSourceContainedRule dataSourceContainedRule =
mock(DataSourceContainedRule.class);
+
when(dataSourceContainedRule.getDataSourceMapper()).thenReturn(Collections.singletonMap("foo_ds",
Collections.singleton("readwrite_ds")));
+
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.singleton(dataSourceContainedRule));
+ DataNodeContainedRule dataNodeContainedRule =
mock(DataNodeContainedRule.class);
+
when(dataNodeContainedRule.getAllDataNodes()).thenReturn(Collections.singletonMap("foo_ds",
Collections.singleton(new DataNode("readwrite_ds.tbl"))));
+
when(database.getRuleMetaData().findRules(DataNodeContainedRule.class)).thenReturn(Collections.singleton(dataNodeContainedRule));
updater.checkSQLStatement(database, createSQLStatement(),
createCurrentRuleConfiguration());
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index e75bdbee847..e23d29d4123 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -82,8 +82,6 @@ import java.util.stream.Collectors;
@Getter
public final class ShardingRule implements DatabaseRule,
DataNodeContainedRule, TableContainedRule, InstanceAwareRule {
- private static final String EQUAL = "=";
-
private static final String ALGORITHM_EXPRESSION_KEY =
"algorithm-expression";
private final RuleConfiguration configuration;
@@ -760,7 +758,7 @@ public final class ShardingRule implements DatabaseRule,
DataNodeContainedRule,
return false;
}
BinaryOperationExpression binaryExpression =
(BinaryOperationExpression) expression;
- return binaryExpression.getLeft() instanceof ColumnSegment &&
binaryExpression.getRight() instanceof ColumnSegment &&
EQUAL.equals(binaryExpression.getOperator());
+ return binaryExpression.getLeft() instanceof ColumnSegment &&
binaryExpression.getRight() instanceof ColumnSegment &&
"=".equals(binaryExpression.getOperator());
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
index af71de15d07..fd7791bdcfe 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/checker/ShardingTableRuleStatementChecker.java
@@ -308,7 +308,7 @@ public final class ShardingTableRuleStatementChecker {
}
ShardingRuleConfiguration toBeAlteredRuleConfig =
ShardingTableRuleStatementConverter.convert(rules);
Collection<String> toBeAlteredLogicTableNames =
getAlteredLogicalTableNames(toBeAlteredRuleConfig);
- Collection<String> toBeAlteredBindingTableNames =
toBeAlteredLogicTableNames.stream().filter(each ->
bindingTables.contains(each)).collect(Collectors.toSet());
+ Collection<String> toBeAlteredBindingTableNames =
toBeAlteredLogicTableNames.stream().filter(bindingTables::contains).collect(Collectors.toSet());
if (toBeAlteredBindingTableNames.isEmpty()) {
return;
}
diff --git
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index 9c1311f3b5d..a3fdd39ed0e 100644
---
a/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++
b/shardingsphere-mode/shardingsphere-mode-core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -86,6 +86,11 @@ public final class ContextManager implements AutoCloseable {
setInstanceContext();
}
+ private void setInstanceContext() {
+
metaDataContexts.getMetaData().getGlobalRuleMetaData().findRules(InstanceAwareRule.class).forEach(each
-> each.setInstanceContext(instanceContext));
+ metaDataContexts.getMetaData().getDatabases().forEach((key, value) ->
value.getRuleMetaData().findRules(InstanceAwareRule.class).forEach(each ->
each.setInstanceContext(instanceContext)));
+ }
+
/**
* Renew meta data contexts.
*
@@ -635,11 +640,6 @@ public final class ContextManager implements AutoCloseable
{
.filter(each -> each instanceof ResourceHeldRule).map(each ->
(ResourceHeldRule<?>) each).forEach(each ->
each.closeStaleResource(databaseName));
}
- private void setInstanceContext() {
-
metaDataContexts.getMetaData().getGlobalRuleMetaData().findRules(InstanceAwareRule.class).forEach(each
-> each.setInstanceContext(instanceContext));
- metaDataContexts.getMetaData().getDatabases().forEach((key, value) ->
value.getRuleMetaData().findRules(InstanceAwareRule.class).forEach(each ->
each.setInstanceContext(instanceContext)));
- }
-
@Override
public void close() throws Exception {
executorEngine.close();