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 172497d  Optimized read-write splitting syntax check (#15973)
172497d is described below

commit 172497d42b6a5f1710bb9eb4b833167e16211038
Author: lanchengx <[email protected]>
AuthorDate: Sat Mar 12 16:49:57 2022 +0800

    Optimized read-write splitting syntax check (#15973)
    
    * Create readwrite_splitting rule syntax, the rule name is not allowed to 
have the same name as the resource
    
    * check if the rule is in use.
    
    * Support `DROP DEFAULT SHADOW ALGORITHM` syntax.
    
    * Reformat.
    
    * Fix ci exception.
---
 ...eateReadwriteSplittingRuleStatementUpdater.java | 27 ++++++++++++-----
 ...DropReadwriteSplittingRuleStatementUpdater.java | 13 +++++++++
 ...ReadwriteSplittingRuleStatementUpdaterTest.java | 14 +++++++++
 ...ReadwriteSplittingRuleStatementUpdaterTest.java | 15 +++++++++-
 .../shadow/api/config/ShadowRuleConfiguration.java | 11 ++++++-
 .../api/config/ShardingRuleConfiguration.java      | 18 +++++++++++-
 .../ResourceRequiredRuleConfiguration.java         | 34 ++++++++++++++++++++++
 7 files changed, 121 insertions(+), 11 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/CreateReadwriteSplittingRuleStatementUpdater.java
 
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/main/java/org/apache/shardingsphere/
 [...]
index 579a997..da97dec 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/CreateReadwriteSplittingRuleStatementUpdater.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/CreateReadwriteSplittingRuleStatementUpdater.java
@@ -23,8 +23,10 @@ import 
org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidRuleConfigurationException;
 import 
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionCreateUpdater;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import org.apache.shardingsphere.infra.rule.identifier.type.ExportableRule;
 import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
 import 
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
@@ -35,6 +37,7 @@ import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgori
 import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
 import org.apache.shardingsphere.spi.typed.TypedSPIRegistry;
 
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
@@ -59,19 +62,27 @@ public final class 
CreateReadwriteSplittingRuleStatementUpdater implements RuleD
     public void checkSQLStatement(final ShardingSphereMetaData 
shardingSphereMetaData, final CreateReadwriteSplittingRuleStatement 
sqlStatement, 
                                   final ReadwriteSplittingRuleConfiguration 
currentRuleConfig) throws DistSQLException {
         String schemaName = shardingSphereMetaData.getName();
-        checkDuplicateRuleNames(schemaName, sqlStatement, currentRuleConfig);
+        checkDuplicateRuleNames(schemaName, sqlStatement, currentRuleConfig, 
shardingSphereMetaData.getResource());
         checkToBeCreatedResources(schemaName, sqlStatement, 
shardingSphereMetaData);
         checkToBeCreatedLoadBalancers(sqlStatement);
     }
     
-    private void checkDuplicateRuleNames(final String schemaName, final 
CreateReadwriteSplittingRuleStatement sqlStatement, 
-                                         final 
ReadwriteSplittingRuleConfiguration currentRuleConfig) throws 
DuplicateRuleException {
+    private void checkDuplicateRuleNames(final String schemaName, final 
CreateReadwriteSplittingRuleStatement sqlStatement,
+                                         final 
ReadwriteSplittingRuleConfiguration currentRuleConfig, final 
ShardingSphereResource resource) throws DistSQLException {
+        Collection<String> currentRuleNames = new ArrayList<>();
+        if (null != resource && null != resource.getDataSources()) {
+            currentRuleNames.addAll(resource.getDataSources().keySet());
+        }
+        Collection<String> duplicateRuleNames = 
sqlStatement.getRules().stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList());
+        if (!duplicateRuleNames.isEmpty()) {
+            throw new InvalidRuleConfigurationException("readwrite splitting", 
duplicateRuleNames, Collections.singleton(String.format("%s already exists in 
resource", duplicateRuleNames)));
+        }
         if (null != currentRuleConfig) {
-            Collection<String> currentRuleNames = 
currentRuleConfig.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList());
-            Collection<String> duplicateRuleNames = 
sqlStatement.getRules().stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList());
-            if (!duplicateRuleNames.isEmpty()) {
-                throw new DuplicateRuleException("readwrite splitting", 
schemaName, duplicateRuleNames);
-            }
+            
currentRuleNames.addAll(currentRuleConfig.getDataSources().stream().map(ReadwriteSplittingDataSourceRuleConfiguration::getName).collect(Collectors.toList()));
+        }
+        duplicateRuleNames = 
sqlStatement.getRules().stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList());
+        if (!duplicateRuleNames.isEmpty()) {
+            throw new DuplicateRuleException("readwrite splitting", 
schemaName, duplicateRuleNames);
         }
     }
     
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 1258e02..912834e 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,8 +17,10 @@
 
 package org.apache.shardingsphere.readwritesplitting.distsql.handler.update;
 
+import 
org.apache.shardingsphere.infra.config.function.ResourceRequiredRuleConfiguration;
 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.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
@@ -44,6 +46,7 @@ public final class DropReadwriteSplittingRuleStatementUpdater 
implements RuleDef
         }
         checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
         checkToBeDroppedRuleNames(schemaName, sqlStatement, currentRuleConfig);
+        checkToBeDroppedInUsed(schemaName, sqlStatement, 
shardingSphereMetaData);
     }
     
     private void checkCurrentRuleConfiguration(final String schemaName, final 
ReadwriteSplittingRuleConfiguration currentRuleConfig) throws 
RequiredRuleMissedException {
@@ -64,6 +67,16 @@ public final class 
DropReadwriteSplittingRuleStatementUpdater implements RuleDef
         }
     }
     
+    private void checkToBeDroppedInUsed(final String schemaName, final 
DropReadwriteSplittingRuleStatement sqlStatement, 
+                                        final ShardingSphereMetaData 
shardingSphereMetaData) throws RuleInUsedException {
+        Collection<String> resourceBeUsed = 
shardingSphereMetaData.getRuleMetaData().findRuleConfiguration(ResourceRequiredRuleConfiguration.class).stream()
+                
.map(ResourceRequiredRuleConfiguration::getRequiredResource).flatMap(Collection::stream).collect(Collectors.toSet());
+        Collection<String> ruleInUsed = 
sqlStatement.getRuleNames().stream().filter(resourceBeUsed::contains).collect(Collectors.toSet());
+        if (!ruleInUsed.isEmpty()) {
+            throw new RuleInUsedException("Readwrite splitting", schemaName, 
ruleInUsed);
+        }
+    }
+    
     @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/CreateReadwriteSplittingRuleStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-distsql/shardingsphere-readwrite-splitting-distsql-handler/src/test/java/org/apache/shardingsph
 [...]
index 9ca8b9f..ee6d1d4 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/CreateReadwriteSplittingRuleStatementUpdaterTest.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/CreateReadwriteSplittingRuleStatementUpdaterTest.java
@@ -21,6 +21,7 @@ import 
org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.resource.RequiredResourceMissedException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateRuleException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidAlgorithmConfigurationException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.InvalidRuleConfigurationException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource;
 import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
@@ -64,6 +65,14 @@ public final class 
CreateReadwriteSplittingRuleStatementUpdaterTest {
         updater.checkSQLStatement(mock(ShardingSphereMetaData.class), 
createSQLStatement("TEST"), getCurrentRuleConfig());
     }
     
+    @Test(expected = InvalidRuleConfigurationException.class)
+    public void assertCheckSQLStatementWithDuplicateResource() throws 
DistSQLException {
+        ShardingSphereMetaData shardingSphereMetaData = 
mock(ShardingSphereMetaData.class);
+        when(shardingSphereMetaData.getResource()).thenReturn(resource);
+        
when(resource.getDataSources()).thenReturn(Collections.singletonMap("write_ds", 
null));
+        updater.checkSQLStatement(shardingSphereMetaData, 
createSQLStatement("write_ds", "TEST"), getCurrentRuleConfig());
+    }
+    
     @Test(expected = RequiredResourceMissedException.class)
     public void assertCheckSQLStatementWithoutExistedResources() throws 
DistSQLException {
         
when(resource.getNotExistedResources(any())).thenReturn(Arrays.asList("read_ds_0",
 "read_ds_1"));
@@ -81,6 +90,11 @@ public final class 
CreateReadwriteSplittingRuleStatementUpdaterTest {
         return new 
CreateReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment));
     }
     
+    private CreateReadwriteSplittingRuleStatement createSQLStatement(final 
String ruleName, final String loadBalancerName) {
+        ReadwriteSplittingRuleSegment ruleSegment = new 
ReadwriteSplittingRuleSegment(ruleName, "write_ds", Arrays.asList("read_ds_0", 
"read_ds_1"), loadBalancerName, new Properties());
+        return new 
CreateReadwriteSplittingRuleStatement(Collections.singleton(ruleSegment));
+    }
+    
     private ReadwriteSplittingRuleConfiguration getCurrentRuleConfig() {
         Properties props = new Properties();
         props.setProperty("write-data-source-name", "ds_write");
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 5f03f31..bb3a8da 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,14 +18,17 @@
 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.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.ShardingSphereMetaData;
 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.junit.Test;
 import org.junit.runner.RunWith;
+import org.mockito.Answers;
 import org.mockito.Mock;
 import org.mockito.junit.MockitoJUnitRunner;
 
@@ -40,11 +43,13 @@ 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.when;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class DropReadwriteSplittingRuleStatementUpdaterTest {
     
-    @Mock
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
     private ShardingSphereMetaData shardingSphereMetaData;
     
     private final DropReadwriteSplittingRuleStatementUpdater updater = new 
DropReadwriteSplittingRuleStatementUpdater();
@@ -61,11 +66,19 @@ public final class 
DropReadwriteSplittingRuleStatementUpdaterTest {
     
     @Test
     public void assertCheckSQLStatementWithIfExists() throws 
RuleDefinitionViolationException {
+        
when(shardingSphereMetaData.getRuleMetaData().findRuleConfiguration(ResourceRequiredRuleConfiguration.class)).thenReturn(Collections.emptyList());
         updater.checkSQLStatement(shardingSphereMetaData, new 
DropReadwriteSplittingRuleStatement(true, 
Collections.singleton("readwrite_ds")),
                 new 
ReadwriteSplittingRuleConfiguration(Collections.emptyList(), 
Collections.emptyMap()));
         updater.checkSQLStatement(shardingSphereMetaData, new 
DropReadwriteSplittingRuleStatement(true, 
Collections.singleton("readwrite_ds")), null);
     }
     
+    @Test(expected = RuleInUsedException.class)
+    public void assertCheckSQLStatementWithInUsed() throws 
RuleDefinitionViolationException {
+        
when(shardingSphereMetaData.getRuleMetaData().findRuleConfiguration(any()))
+                
.thenReturn(Collections.singletonList((ResourceRequiredRuleConfiguration) () -> 
Collections.singleton("readwrite_ds")));
+        updater.checkSQLStatement(shardingSphereMetaData, 
createSQLStatement(), createCurrentRuleConfiguration());
+    }
+    
     @Test
     public void assertUpdateCurrentRuleConfiguration() {
         ReadwriteSplittingRuleConfiguration 
readwriteSplittingRuleConfiguration = createCurrentRuleConfiguration();
diff --git 
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
 
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
index b039dda..c03f4a3 100644
--- 
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
+++ 
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-api/src/main/java/org/apache/shardingsphere/shadow/api/config/ShadowRuleConfiguration.java
@@ -21,19 +21,23 @@ import lombok.Getter;
 import lombok.Setter;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.config.function.DistributedRuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.function.ResourceRequiredRuleConfiguration;
 import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
 import 
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
 
+import java.util.Arrays;
+import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * Shadow rule configuration.
  */
 @Getter
 @Setter
-public final class ShadowRuleConfiguration implements SchemaRuleConfiguration, 
DistributedRuleConfiguration {
+public final class ShadowRuleConfiguration implements SchemaRuleConfiguration, 
DistributedRuleConfiguration, ResourceRequiredRuleConfiguration {
     
     private String defaultShadowAlgorithmName;
     
@@ -42,4 +46,9 @@ public final class ShadowRuleConfiguration implements 
SchemaRuleConfiguration, D
     private Map<String, ShadowTableConfiguration> tables = new 
LinkedHashMap<>();
     
     private Map<String, ShardingSphereAlgorithmConfiguration> shadowAlgorithms 
= new LinkedHashMap<>();
+    
+    @Override
+    public Collection<String> getRequiredResource() {
+        return dataSources.values().stream().map(each -> 
Arrays.asList(each.getSourceDataSourceName(), 
each.getShadowDataSourceName())).flatMap(Collection::stream).collect(Collectors.toSet());
+    }
 }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
index e25f2f4..8781bf3 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-api/src/main/java/org/apache/shardingsphere/sharding/api/config/ShardingRuleConfiguration.java
@@ -17,28 +17,34 @@
 
 package org.apache.shardingsphere.sharding.api.config;
 
+import com.google.common.base.Splitter;
 import lombok.Getter;
 import lombok.Setter;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.infra.config.function.DistributedRuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.function.ResourceRequiredRuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.rulealtered.OnRuleAlteredActionConfiguration;
 import org.apache.shardingsphere.infra.config.scope.SchemaRuleConfiguration;
+import org.apache.shardingsphere.infra.datanode.DataNode;
 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.support.InlineExpressionParser;
 
 import java.util.Collection;
 import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.stream.Collectors;
 
 /**
  * Sharding rule configuration.
  */
 @Getter
 @Setter
-public final class ShardingRuleConfiguration implements 
SchemaRuleConfiguration, DistributedRuleConfiguration {
+public final class ShardingRuleConfiguration implements 
SchemaRuleConfiguration, DistributedRuleConfiguration, 
ResourceRequiredRuleConfiguration {
     
     private Collection<ShardingTableRuleConfiguration> tables = new 
LinkedList<>();
     
@@ -63,4 +69,14 @@ public final class ShardingRuleConfiguration implements 
SchemaRuleConfiguration,
     private String scalingName;
     
     private Map<String, OnRuleAlteredActionConfiguration> scaling = new 
LinkedHashMap<>();
+    
+    @Override
+    public Collection<String> getRequiredResource() {
+        Collection<String> result = new LinkedHashSet<>();
+        
result.addAll(autoTables.stream().map(ShardingAutoTableRuleConfiguration::getActualDataSources)
+                .map(each -> 
Splitter.on(",").trimResults().splitToList(each)).flatMap(Collection::stream).collect(Collectors.toSet()));
+        result.addAll(tables.stream().map(each -> new 
InlineExpressionParser(each.getActualDataNodes()).splitAndEvaluate())
+                .flatMap(Collection::stream).distinct().map(each -> new 
DataNode(each).getDataSourceName()).collect(Collectors.toSet()));
+        return result;
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/function/ResourceRequiredRuleConfiguration.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/function/ResourceRequiredRuleConfiguration.java
new file mode 100644
index 0000000..71a1252
--- /dev/null
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/function/ResourceRequiredRuleConfiguration.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.config.function;
+
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+
+import java.util.Collection;
+
+/**
+ * Resource Required rule configuration.
+ */
+public interface ResourceRequiredRuleConfiguration extends RuleConfiguration {
+    
+    /**
+     * Get Required Resource.
+     * @return required resource names
+     */
+    Collection<String> getRequiredResource();
+}

Reply via email to