This is an automated email from the ASF dual-hosted git repository.
panjuan 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 16f7677 optimize sharding single date node route logic & federate
route logic (#12486)
16f7677 is described below
commit 16f7677593fc9e219b023d0acfff754a1a77df47
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Thu Sep 16 18:40:20 2021 +0800
optimize sharding single date node route logic & federate route logic
(#12486)
* optimize federate route logic
* optimize sharding single route logic
* optimize sharding single route logic
* fix integration test
---
.../engine/type/ShardingRouteEngineFactory.java | 15 ++++++-----
.../federated/ShardingFederatedRoutingEngine.java | 29 ++++++++++++++--------
.../standard/ShardingStandardRoutingEngine.java | 12 +++++++++
.../docker/tbl/h2/proxy/conf/config-tbl.yaml | 1 +
.../docker/tbl/mysql/proxy/conf/config-tbl.yaml | 1 +
.../tbl/postgresql/proxy/conf/config-tbl.yaml | 1 +
.../src/test/resources/env/tbl/rules.yaml | 1 +
.../resources/scenario/sharding/case/select.xml | 2 +-
8 files changed, 45 insertions(+), 17 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
index c759bc0..b95dd07 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java
@@ -177,11 +177,15 @@ public final class ShardingRouteEngineFactory {
private static ShardingRouteEngine getDQLRouteEngineForShardingTable(final
ShardingRule shardingRule, final SQLStatementContext<?> sqlStatementContext,
final
ShardingConditions shardingConditions, final Collection<String> tableNames,
final ConfigurationProperties props) {
- if (isShardingStandardQuery(sqlStatementContext, tableNames,
shardingRule)) {
- return new
ShardingStandardRoutingEngine(getLogicTableName(shardingConditions,
tableNames), shardingConditions, props);
+ if (isShardingStandardQuery(tableNames, shardingRule)) {
+ ShardingStandardRoutingEngine result = new
ShardingStandardRoutingEngine(getLogicTableName(shardingConditions,
tableNames), shardingConditions, props);
+ boolean needExecuteByCalcite = sqlStatementContext instanceof
SelectStatementContext && ((SelectStatementContext)
sqlStatementContext).isNeedExecuteByCalcite();
+ if (!needExecuteByCalcite ||
result.route(shardingRule).isSingleRouting()) {
+ return result;
+ }
}
if (isShardingFederatedQuery(sqlStatementContext, tableNames,
shardingRule)) {
- return new ShardingFederatedRoutingEngine(tableNames);
+ return new ShardingFederatedRoutingEngine(tableNames,
shardingConditions, props);
}
// TODO config for cartesian set
return new ShardingComplexRoutingEngine(tableNames,
shardingConditions, props);
@@ -192,9 +196,8 @@ public final class ShardingRouteEngineFactory {
.map(ShardingConditionValue::getTableName).findFirst().orElseGet(() ->
tableNames.iterator().next());
}
- private static boolean isShardingStandardQuery(final
SQLStatementContext<?> sqlStatementContext, final Collection<String>
tableNames, final ShardingRule shardingRule) {
- boolean needExecuteByCalcite = sqlStatementContext instanceof
SelectStatementContext && ((SelectStatementContext)
sqlStatementContext).isNeedExecuteByCalcite();
- return !needExecuteByCalcite && (1 == tableNames.size() &&
shardingRule.isAllShardingTables(tableNames) ||
shardingRule.isAllBindingTables(tableNames));
+ private static boolean isShardingStandardQuery(final Collection<String>
tableNames, final ShardingRule shardingRule) {
+ return 1 == tableNames.size() &&
shardingRule.isAllShardingTables(tableNames) ||
shardingRule.isAllBindingTables(tableNames);
}
private static boolean isShardingFederatedQuery(final
SQLStatementContext<?> sqlStatementContext, final Collection<String>
tableNames, final ShardingRule shardingRule) {
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
index 8869089..44685ff 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/federated/ShardingFederatedRoutingEngine.java
@@ -18,12 +18,14 @@
package org.apache.shardingsphere.sharding.route.engine.type.federated;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.datanode.DataNode;
+import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.route.context.RouteMapper;
+import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import
org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditions;
import
org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
+import
org.apache.shardingsphere.sharding.route.engine.type.standard.ShardingStandardRoutingEngine;
+import
org.apache.shardingsphere.sharding.route.engine.type.unicast.ShardingUnicastRoutingEngine;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.Collection;
import java.util.Collections;
@@ -36,22 +38,29 @@ public final class ShardingFederatedRoutingEngine
implements ShardingRouteEngine
private final Collection<String> logicTables;
+ private final ShardingConditions shardingConditions;
+
+ private final ConfigurationProperties properties;
+
@Override
public RouteContext route(final ShardingRule shardingRule) {
RouteContext result = new RouteContext();
for (String each : logicTables) {
- fillRouteContext(result, shardingRule, each);
+ RouteContext newRouteContext;
+ if (shardingRule.isShardingTable(each)) {
+ newRouteContext = new ShardingStandardRoutingEngine(each,
shardingConditions, properties).route(shardingRule);
+ } else {
+ newRouteContext = new
ShardingUnicastRoutingEngine(Collections.singletonList(each)).route(shardingRule);
+ }
+ fillRouteContext(result, newRouteContext);
}
result.setFederated(true);
return result;
}
- private void fillRouteContext(final RouteContext routeContext, final
ShardingRule shardingRule, final String logicTableName) {
- TableRule tableRule = shardingRule.getTableRule(logicTableName);
- for (DataNode each : tableRule.getActualDataNodes()) {
- RouteMapper dataSource = new RouteMapper(each.getDataSourceName(),
each.getDataSourceName());
- RouteMapper table = new RouteMapper(logicTableName,
each.getTableName());
- routeContext.putRouteUnit(dataSource,
Collections.singletonList(table));
+ private void fillRouteContext(final RouteContext routeContext, final
RouteContext newRouteContext) {
+ for (RouteUnit each : newRouteContext.getRouteUnits()) {
+ routeContext.putRouteUnit(each.getDataSourceMapper(),
each.getTableMappers());
}
}
}
diff --git
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
index 37a87ab..a4d8681 100644
---
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
+++
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/standard/ShardingStandardRoutingEngine.java
@@ -49,6 +49,7 @@ import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
/**
* Sharding standard routing engine.
@@ -64,8 +65,19 @@ public final class ShardingStandardRoutingEngine implements
ShardingRouteEngine
private final Collection<Collection<DataNode>> originalDataNodes = new
LinkedList<>();
+ private final Map<String, RouteContext> cachedRouteContexts = new
ConcurrentHashMap<>();
+
@Override
public RouteContext route(final ShardingRule shardingRule) {
+ RouteContext result = cachedRouteContexts.get(logicTableName);
+ if (null == result) {
+ result = getRouteContext(shardingRule);
+ }
+ cachedRouteContexts.put(logicTableName, result);
+ return result;
+ }
+
+ private RouteContext getRouteContext(final ShardingRule shardingRule) {
RouteContext result = new RouteContext();
Collection<DataNode> dataNodes = getDataNodes(shardingRule,
shardingRule.getTableRule(logicTableName));
result.getOriginalDataNodes().addAll(originalDataNodes);
diff --git
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/h2/proxy/conf/config-tbl.yaml
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/h2/proxy/conf/config-tbl.yaml
index 3341fa8..a63c881 100644
---
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/h2/proxy/conf/config-tbl.yaml
+++
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/h2/proxy/conf/config-tbl.yaml
@@ -81,6 +81,7 @@ rules:
type: INLINE
props:
algorithm-expression: t_order_federate_sharding_${order_id_sharding %
2}
+ allow-range-query-with-inline-sharding: true
keyGenerators:
constant:
diff --git
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/mysql/proxy/conf/config-tbl.yaml
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/mysql/proxy/conf/config-tbl.yaml
index 3341fa8..a63c881 100644
---
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/mysql/proxy/conf/config-tbl.yaml
+++
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/mysql/proxy/conf/config-tbl.yaml
@@ -81,6 +81,7 @@ rules:
type: INLINE
props:
algorithm-expression: t_order_federate_sharding_${order_id_sharding %
2}
+ allow-range-query-with-inline-sharding: true
keyGenerators:
constant:
diff --git
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/postgresql/proxy/conf/config-tbl.yaml
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/postgresql/proxy/conf/config-tbl.yaml
index e21cff1..ccb4f98 100644
---
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/postgresql/proxy/conf/config-tbl.yaml
+++
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/docker/tbl/postgresql/proxy/conf/config-tbl.yaml
@@ -81,6 +81,7 @@ rules:
type: INLINE
props:
algorithm-expression: t_order_federate_sharding_${order_id_sharding %
2}
+ allow-range-query-with-inline-sharding: true
keyGenerators:
constant:
diff --git
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/tbl/rules.yaml
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/tbl/rules.yaml
index ff402a2..9166adf 100644
---
a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/tbl/rules.yaml
+++
b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-suite/src/test/resources/env/tbl/rules.yaml
@@ -68,6 +68,7 @@ rules:
type: INLINE
props:
algorithm-expression: t_order_federate_sharding_${order_id_sharding %
2}
+ allow-range-query-with-inline-sharding: true
keyGenerators:
constant:
diff --git
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
index c59a811..c4d7693 100644
---
a/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
+++
b/shardingsphere-test/shardingsphere-rewrite-test/src/test/resources/scenario/sharding/case/select.xml
@@ -34,7 +34,7 @@
<rewrite-assertion id="select_with_sum_fun">
<input sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM
t_account WHERE account_id = 100" />
- <output sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM
t_account WHERE account_id = 100" />
+ <output sql="SELECT SUM(DISTINCT account_id), SUM(account_id) FROM
t_account_0 WHERE account_id = 100" />
</rewrite-assertion>
<rewrite-assertion id="select_with_avg_fun">