You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/07/28 01:27:37 UTC

[shardingsphere] branch master updated: Fixed In one config, can config two - !SHARDING bug (#19604)

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 2861e28657f Fixed In one config, can config two - !SHARDING bug (#19604)
2861e28657f is described below

commit 2861e28657f6c211e8d4aa10753fc7539b45458c
Author: liguoping <xd...@163.com>
AuthorDate: Thu Jul 28 09:27:31 2022 +0800

    Fixed In one config, can config two - !SHARDING bug (#19604)
    
    * checkDuplicateRule
    
    * remove guava here
    
    * tab
---
 .../backend/config/ProxyConfigurationLoader.java   | 26 ++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
index 409653c101e..9f9367b51d1 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoader.java
@@ -22,7 +22,10 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.SneakyThrows;
 import org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapper;
+import org.apache.shardingsphere.infra.yaml.config.swapper.YamlRuleConfigurationSwapperFactory;
 import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
 import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
 import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyServerConfiguration;
@@ -35,6 +38,8 @@ import java.util.Collection;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -105,9 +110,30 @@ public final class ProxyConfigurationLoader {
             result.setDatabaseName(result.getSchemaName());
         }
         Preconditions.checkNotNull(result.getDatabaseName(), "Property `databaseName` in file `%s` is required.", yamlFile.getName());
+        checkDuplicateRule(result.getRules(), yamlFile);
         return Optional.of(result);
     }
     
+    private static void checkDuplicateRule(final Collection<YamlRuleConfiguration> ruleConfigurations, final File yamlFile) {
+        if (ruleConfigurations.isEmpty()) {
+            return;
+        }
+        Map<Class<? extends RuleConfiguration>, Long> ruleConfigTypeCountMap = ruleConfigurations.stream().collect(Collectors.groupingBy(YamlRuleConfiguration::getRuleConfigurationType, Collectors.counting()));
+        Optional<Entry<Class<? extends RuleConfiguration>, Long>> duplicateRuleConfiguration = ruleConfigTypeCountMap.entrySet().stream().filter(each -> each.getValue() > 1).findFirst();
+        if (duplicateRuleConfiguration.isPresent()) {
+            throw new IllegalStateException(String.format("Duplicate rule tag '!%s' in file %s.", getDuplicateRuleTagName(duplicateRuleConfiguration.get().getKey()), yamlFile.getName()));
+        }
+    }
+    
+    @SuppressWarnings("rawtypes")
+    private static Object getDuplicateRuleTagName(final Class<? extends RuleConfiguration> ruleConfigurationClass) {
+        Optional<YamlRuleConfigurationSwapper> optional = YamlRuleConfigurationSwapperFactory.getAllInstances().stream().filter(each -> ruleConfigurationClass.equals(each.getTypeClass())).findFirst();
+        if (optional.isPresent()) {
+            return optional.get().getRuleTagName();
+        }
+        throw new IllegalStateException("Not find rule tag name of class " + ruleConfigurationClass);
+    }
+    
     private static File[] findRuleConfigurationFiles(final File path) {
         return path.listFiles(each -> SCHEMA_CONFIG_FILE_PATTERN.matcher(each.getName()).matches());
     }