You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/01/19 11:51:44 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #14864: validate whether the sharding rules of the binding tables are consistent

strongduanmu commented on a change in pull request #14864:
URL: https://github.com/apache/shardingsphere/pull/14864#discussion_r787665084



##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -192,11 +194,65 @@ private String getDefaultGenerateKeyColumn(final KeyGenerateStrategyConfiguratio
     private BindingTableRule createBindingTableRule(final String bindingTableGroup) {
         Map<String, TableRule> tableRules = Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream()
                 .map(this::getTableRule).collect(Collectors.toMap(each -> each.getLogicTable().toLowerCase(), Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+        checkTheSameActualDataSourceNames(tableRules, bindingTableGroup);

Review comment:
       @cheese8 Can you merge this three check method to checkBindingTableRule(tableRules)?

##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -192,11 +194,65 @@ private String getDefaultGenerateKeyColumn(final KeyGenerateStrategyConfiguratio
     private BindingTableRule createBindingTableRule(final String bindingTableGroup) {
         Map<String, TableRule> tableRules = Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream()
                 .map(this::getTableRule).collect(Collectors.toMap(each -> each.getLogicTable().toLowerCase(), Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+        checkTheSameActualDataSourceNames(tableRules, bindingTableGroup);
+        checkTheSameShardingStrategyConfiguration(tableRules, bindingTableGroup, true);
+        checkTheSameShardingStrategyConfiguration(tableRules, bindingTableGroup, false);
         BindingTableRule result = new BindingTableRule();
         result.getTableRules().putAll(tableRules);
         return result;
     }
     
+    private void checkTheSameActualDataSourceNames(final Map<String, TableRule> tableRules, final String bindingTableGroup) {

Review comment:
       @cheese8 Can we choose the first tableRule as sample, and then judge whether other tableRules are same with sample?

##########
File path: shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
##########
@@ -192,11 +194,65 @@ private String getDefaultGenerateKeyColumn(final KeyGenerateStrategyConfiguratio
     private BindingTableRule createBindingTableRule(final String bindingTableGroup) {
         Map<String, TableRule> tableRules = Splitter.on(",").trimResults().splitToList(bindingTableGroup).stream()
                 .map(this::getTableRule).collect(Collectors.toMap(each -> each.getLogicTable().toLowerCase(), Function.identity(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
+        checkTheSameActualDataSourceNames(tableRules, bindingTableGroup);
+        checkTheSameShardingStrategyConfiguration(tableRules, bindingTableGroup, true);
+        checkTheSameShardingStrategyConfiguration(tableRules, bindingTableGroup, false);
         BindingTableRule result = new BindingTableRule();
         result.getTableRules().putAll(tableRules);
         return result;
     }
     
+    private void checkTheSameActualDataSourceNames(final Map<String, TableRule> tableRules, final String bindingTableGroup) {
+        Collection<String> savedActualDataSourceNames = new HashSet<>();
+        int countSavedActualDataSourceNames = 0;
+        for (TableRule tableRule : tableRules.values()) {
+            if (savedActualDataSourceNames.isEmpty()) {
+                savedActualDataSourceNames.addAll(tableRule.getActualDatasourceNames());
+                countSavedActualDataSourceNames = savedActualDataSourceNames.size();
+            } else if (!savedActualDataSourceNames.isEmpty()) {
+                savedActualDataSourceNames.addAll(tableRule.getActualDatasourceNames());
+                if (countSavedActualDataSourceNames != savedActualDataSourceNames.size()) {
+                    throw new ShardingSphereConfigurationException("The actualDataSourceNames on bindingTableGroup `%s` are inconsistent", bindingTableGroup);
+                }
+            }
+        }
+    }
+    
+    private void checkTheSameShardingStrategyConfiguration(final Map<String, TableRule> tableRules, final String bindingTableGroup, final Boolean databaseOrTable) {
+        String savedShardingColumn = null;
+        String savedShardingAlgorithm = null;
+        for (TableRule tableRule : tableRules.values()) {
+            ShardingStrategyConfiguration shardingStrategyConfiguration = databaseOrTable ? tableRule.getDatabaseShardingStrategyConfig() : tableRule.getTableShardingStrategyConfig();
+            if (null == shardingStrategyConfiguration) {
+                continue;
+            }
+            String shardingColumn = getShardingColumn(shardingStrategyConfiguration);
+            if (null == savedShardingColumn) {
+                savedShardingColumn = shardingColumn;
+            } else if (null != savedShardingColumn && !savedShardingColumn.equalsIgnoreCase(shardingColumn)) {
+                log.warn("All the route computations will only use the sharding strategy of the primary table, but the databaseShardingStrategyConfig on bindTable `%s` are inconsistent.", 

Review comment:
       @cheese8 I think exception may be better.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org