tuohai666 commented on a change in pull request #3021: Feature#2601 Check 
RoutingResult.
URL: 
https://github.com/apache/incubator-shardingsphere/pull/3021#discussion_r324973711
 
 

 ##########
 File path: 
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRoutingResultChecker.java
 ##########
 @@ -0,0 +1,212 @@
+/*
+ * 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.core.route.router.sharding;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Strings;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.Lists;
+import com.google.common.collect.Multimap;
+import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
+import 
org.apache.shardingsphere.api.config.sharding.strategy.ShardingStrategyConfiguration;
+import org.apache.shardingsphere.core.exception.ShardingException;
+import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingCondition;
+import 
org.apache.shardingsphere.core.optimize.sharding.statement.ShardingOptimizedStatement;
+import 
org.apache.shardingsphere.core.optimize.sharding.statement.ddl.ShardingDropIndexOptimizedStatement;
+import 
org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingConditionOptimizedStatement;
+import org.apache.shardingsphere.core.parse.sql.statement.dml.DMLStatement;
+import org.apache.shardingsphere.core.route.type.RoutingEngine;
+import org.apache.shardingsphere.core.route.type.RoutingResult;
+import org.apache.shardingsphere.core.route.type.RoutingUnit;
+import org.apache.shardingsphere.core.route.type.TableUnit;
+import 
org.apache.shardingsphere.core.route.type.standard.StandardRoutingEngine;
+import org.apache.shardingsphere.core.rule.ShardingRule;
+import org.apache.shardingsphere.core.rule.TableRule;
+import org.apache.shardingsphere.core.strategy.route.value.RouteValue;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Checker for sharding router with parse.
+ *
+ * @author sunbufu
+ */
+public class ParsingSQLRoutingResultChecker implements RoutingResultChecker {
+    
+    private final ShardingRule shardingRule;
+    
+    private final ShardingSphereMetaData metaData;
+    
+    private final ShardingOptimizedStatement shardingStatement;
+    
+    private boolean checkDataSource;
+    
+    private boolean checkTable;
+    
+    public ParsingSQLRoutingResultChecker(final ShardingRule shardingRule, 
final ShardingSphereMetaData metaData, final ShardingOptimizedStatement 
shardingStatement) {
+        this.shardingRule = shardingRule;
+        this.metaData = metaData;
+        this.shardingStatement = shardingStatement;
+        if 
(shardingRule.getRuleConfiguration().getMasterSlaveRuleConfigs().isEmpty()) {
+            checkDataSource = true;
+        }
+        if (shardingStatement.getSQLStatement() instanceof DMLStatement) {
+            checkTable = true;
+        }
+    }
+    
+    /**
+     * Check for ParsingSQLRouter's routing result.
+     *
+     * @param routingEngine routing engine
+     * @param routingResult routing result
+     */
+    @Override
+    public void check(final RoutingEngine routingEngine, final RoutingResult 
routingResult) {
+        if (shardingStatement instanceof ShardingDropIndexOptimizedStatement) {
+            return;
+        }
+        Multimap<RoutingUnit, TableUnit> absentRoutingUnitMap = 
getAbsentRoutingUnit(routingResult.getRoutingUnits());
+        if (absentRoutingUnitMap.isEmpty()) {
+            return;
+        }
+        if (routingEngine instanceof StandardRoutingEngine) {
+            throwAbsentStandardRoutingResultException(shardingStatement, 
absentRoutingUnitMap);
+        } else {
+            throwOthersRoutingResultException(absentRoutingUnitMap);
+        }
+    }
+    
+    /**
+     * Throw other's absent routing result exception.
+     *
+     * @param absentRoutingUnitMap absent routingUnit map
+     */
+    private void throwOthersRoutingResultException(final Multimap<RoutingUnit, 
TableUnit> absentRoutingUnitMap) {
+        RoutingUnit routingUnit = 
absentRoutingUnitMap.keySet().iterator().next();
+        Collection<String> absentDataNodes = 
Lists.newArrayListWithExpectedSize(absentRoutingUnitMap.get(routingUnit).size());
+        for (TableUnit each : absentRoutingUnitMap.get(routingUnit)) {
+            absentDataNodes.add(routingUnit.getDataSourceName() + "." + 
each.getActualTableName());
+        }
+        if (!absentDataNodes.isEmpty()) {
+            throwExceptionForAbsentDataNode(absentDataNodes, "");
+        }
+    }
+    
+    /**
+     * Throw StandardRoutingEngine's absent routing result exception.
+     *
+     * @param shardingStatement    sharding statement
+     * @param absentRoutingUnitMap absent routing unit map
+     */
+    private void throwAbsentStandardRoutingResultException(final 
ShardingOptimizedStatement shardingStatement, final Multimap<RoutingUnit, 
TableUnit> absentRoutingUnitMap) {
+        RoutingUnit routingUnit = 
absentRoutingUnitMap.keySet().iterator().next();
+        Collection<String> absentDataNodes = 
Lists.newArrayListWithExpectedSize(absentRoutingUnitMap.get(routingUnit).size());
+        ShardingStrategyConfiguration databaseStrategy = null;
+        ShardingStrategyConfiguration tableStrategy = null;
+        if (null != 
shardingRule.getRuleConfiguration().getDefaultDatabaseShardingStrategyConfig()) 
{
+            databaseStrategy = 
shardingRule.getRuleConfiguration().getDefaultDatabaseShardingStrategyConfig();
+        }
+        if (null != 
shardingRule.getRuleConfiguration().getDefaultTableShardingStrategyConfig()) {
+            tableStrategy = 
shardingRule.getRuleConfiguration().getDefaultTableShardingStrategyConfig();
+        }
+        for (TableUnit each : absentRoutingUnitMap.get(routingUnit)) {
+            absentDataNodes.add(routingUnit.getDataSourceName() + "." + 
each.getActualTableName());
+            Optional<TableRuleConfiguration> tableRuleConfiguration = 
getTableRuleConfiguration(shardingRule.getRuleConfiguration().getTableRuleConfigs(),
 each.getLogicTableName());
+            if (!tableRuleConfiguration.isPresent()) {
+                continue;
+            }
+            databaseStrategy = 
tableRuleConfiguration.get().getDatabaseShardingStrategyConfig();
+            tableStrategy = 
tableRuleConfiguration.get().getTableShardingStrategyConfig();
+        }
+        if (!absentDataNodes.isEmpty()) {
+            StringBuilder detail = new StringBuilder();
+            if (null != databaseStrategy) {
+                
detail.append("DatabaseStrategy=[").append(databaseStrategy).append("], ");
+            }
+            detail.append("TableStrategy=[").append(tableStrategy).append("], 
");
+            if (shardingStatement instanceof 
ShardingConditionOptimizedStatement) {
+                detail.append("with ")
 
 Review comment:
   Please don't start a new line until the current line reaches 200 characters.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to