-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
668 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...src/test/java/org/apache/shardingsphere/core/strategy/route/value/ListRouteValueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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.strategy.route.value; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.junit.Assert.assertThat; | ||
|
||
public class ListRouteValueTest { | ||
|
||
@Test | ||
public void assertToStringWithEqual() { | ||
assertThat(new ListRouteValue("order_id", "t_order", Collections.singleton(10)).toString(), is("t_order.order_id = 10")); | ||
} | ||
|
||
@Test | ||
public void assertToStringWithIn() { | ||
assertThat(new ListRouteValue("order_id", "t_order", Arrays.asList(10, 20)).toString(), is("t_order.order_id in (10,20)")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
.../org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRoutingResultChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ") | ||
.append(getAllRouteValues(((ShardingConditionOptimizedStatement) shardingStatement).getShardingConditions().getConditions())) | ||
.append(", "); | ||
} | ||
throwExceptionForAbsentDataNode(absentDataNodes, detail); | ||
} | ||
} | ||
|
||
private Multimap<RoutingUnit, TableUnit> getAbsentRoutingUnit(final Collection<RoutingUnit> routingUnits) { | ||
Multimap<RoutingUnit, TableUnit> result = HashMultimap.create(); | ||
for (RoutingUnit each : routingUnits) { | ||
String dataSourceName = each.getDataSourceName(); | ||
result.putAll(each, getAbsentTableUnit(each, dataSourceName)); | ||
} | ||
return result; | ||
} | ||
|
||
private Collection<TableUnit> getAbsentTableUnit(final RoutingUnit routingUnit, final String dataSourceName) { | ||
Collection<TableUnit> result = new LinkedList<>(); | ||
for (TableUnit each : routingUnit.getTableUnits()) { | ||
if (containsInMetaData(dataSourceName, each.getActualTableName()) || containsInShardingRule(dataSourceName, each)) { | ||
continue; | ||
} | ||
result.add(each); | ||
} | ||
return result; | ||
} | ||
|
||
private boolean containsInShardingRule(final String dataSourceName, final TableUnit tableUnit) { | ||
Optional<TableRule> tableRule = shardingRule.findTableRule(tableUnit.getLogicTableName()); | ||
return tableRule.isPresent() && tableRule.get().getActualTableNames(dataSourceName).contains(tableUnit.getActualTableName()); | ||
} | ||
|
||
private boolean containsInMetaData(final String dataSourceName, final String actualTableName) { | ||
if (checkDataSource && (null == metaData.getDataSources() || null == metaData.getDataSources().getDataSourceMetaData(dataSourceName))) { | ||
return false; | ||
} | ||
if (checkTable && (null == metaData.getTables() || !metaData.getTables().containsTable(actualTableName))) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private void throwExceptionForAbsentDataNode(final Collection<String> absentDataNodes, final CharSequence detail) { | ||
String msg = "We get some absent DataNodes=" + absentDataNodes + " in routing result, " | ||
+ (Strings.isNullOrEmpty(detail.toString()) ? "" : detail) | ||
+ "please check the configuration of rule and data node."; | ||
throw new ShardingException(msg.replace("%", "%%")); | ||
} | ||
|
||
private Optional<TableRuleConfiguration> getTableRuleConfiguration(final Collection<TableRuleConfiguration> tableRuleConfigurations, final String tableName) { | ||
for (TableRuleConfiguration each : tableRuleConfigurations) { | ||
if (tableName.equals(each.getLogicTable())) { | ||
return Optional.of(each); | ||
} | ||
} | ||
return Optional.absent(); | ||
} | ||
|
||
private Collection<RouteValue> getAllRouteValues(final Collection<ShardingCondition> shardingConditions) { | ||
Collection<RouteValue> result = new LinkedList<>(); | ||
for (ShardingCondition each : shardingConditions) { | ||
result.addAll(each.getRouteValues()); | ||
} | ||
return result; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
.../main/java/org/apache/shardingsphere/core/route/router/sharding/RoutingResultChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* 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 org.apache.shardingsphere.core.route.type.RoutingEngine; | ||
import org.apache.shardingsphere.core.route.type.RoutingResult; | ||
|
||
/** | ||
* RoutingResult checker. | ||
* | ||
* @author sunbufu | ||
*/ | ||
public interface RoutingResultChecker { | ||
|
||
/** | ||
* Check routing result. | ||
* | ||
* @param routingEngine routing engine | ||
* @param routingResult routing result | ||
*/ | ||
void check(RoutingEngine routingEngine, RoutingResult routingResult); | ||
} |
Oops, something went wrong.