This is an automated email from the ASF dual-hosted git repository.
zhangliang 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 0981ecd Aggregate logical data sources of shadow. (#13531)
0981ecd is described below
commit 0981ecd722d2795ba0bfda9872e36c59600c6108
Author: gin <[email protected]>
AuthorDate: Wed Nov 10 21:00:48 2021 +0800
Aggregate logical data sources of shadow. (#13531)
---
.../shadow/route/ShadowSQLRouter.java | 30 ++++++++++++++++++--
.../shadow/route/engine/ShadowRouteEngine.java | 13 +++++----
.../shardingsphere/shadow/rule/ShadowRule.java | 32 ++++++++++++++++++----
3 files changed, 61 insertions(+), 14 deletions(-)
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
index 2d1238f..f0cc378 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/ShadowSQLRouter.java
@@ -22,10 +22,16 @@ import
org.apache.shardingsphere.infra.config.properties.ConfigurationProperties
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.SQLRouter;
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.shadow.constant.ShadowOrder;
import org.apache.shardingsphere.shadow.route.engine.ShadowRouteEngineFactory;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Optional;
+
/**
* Shadow SQL router.
*/
@@ -40,13 +46,31 @@ public final class ShadowSQLRouter implements
SQLRouter<ShadowRule> {
@Override
public void decorateRouteContext(final RouteContext routeContext,
final LogicSQL logicSQL, final
ShardingSphereMetaData metaData, final ShadowRule rule, final
ConfigurationProperties props) {
+ doDecorate(routeContext, rule);
if (rule.isEnable()) {
- doShadowDecorate(routeContext, logicSQL, rule, props);
+ doShadowDecorate(routeContext, logicSQL, rule);
+ }
+ }
+
+ private void doDecorate(final RouteContext routeContext, final ShadowRule
shadowRule) {
+ Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
+ Collection<RouteUnit> toBeRemoved = new LinkedList<>();
+ Collection<RouteUnit> toBeAdded = new LinkedList<>();
+ for (RouteUnit each : routeUnits) {
+ String logicName = each.getDataSourceMapper().getLogicName();
+ String actualName = each.getDataSourceMapper().getActualName();
+ Optional<String> sourceDataSourceName =
shadowRule.getSourceDataSourceName(actualName);
+ if (sourceDataSourceName.isPresent()) {
+ toBeRemoved.add(each);
+ toBeAdded.add(new RouteUnit(new RouteMapper(logicName,
sourceDataSourceName.get()), each.getTableMappers()));
+ }
}
+ routeUnits.removeAll(toBeRemoved);
+ routeUnits.addAll(toBeAdded);
}
- private void doShadowDecorate(final RouteContext routeContext, final
LogicSQL logicSQL, final ShadowRule rule, final ConfigurationProperties props) {
- ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext,
rule);
+ private void doShadowDecorate(final RouteContext routeContext, final
LogicSQL logicSQL, final ShadowRule shadowRule) {
+ ShadowRouteEngineFactory.newInstance(logicSQL).route(routeContext,
shadowRule);
}
@Override
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
index ccccafd..18ae46a 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteEngine.java
@@ -39,15 +39,18 @@ public interface ShadowRouteEngine {
*/
default void shadowRouteDecorate(final RouteContext routeContext, final
Map<String, String> shadowDataSourceMappings) {
Collection<RouteUnit> routeUnits = routeContext.getRouteUnits();
+ Collection<RouteUnit> toBeRemoved = new LinkedList<>();
Collection<RouteUnit> toBeAdded = new LinkedList<>();
for (RouteUnit each : routeUnits) {
- RouteMapper dataSourceMapper = each.getDataSourceMapper();
- String shadowActualName =
shadowDataSourceMappings.get(dataSourceMapper.getActualName());
- if (null != shadowActualName) {
- toBeAdded.add(new RouteUnit(new
RouteMapper(dataSourceMapper.getLogicName(), shadowActualName),
each.getTableMappers()));
+ String logicName = each.getDataSourceMapper().getLogicName();
+ String actualName = each.getDataSourceMapper().getActualName();
+ String shadowDataSourceName =
shadowDataSourceMappings.get(actualName);
+ if (null != shadowDataSourceName) {
+ toBeRemoved.add(each);
+ toBeAdded.add(new RouteUnit(new RouteMapper(logicName,
shadowDataSourceName), each.getTableMappers()));
}
}
- routeUnits.clear();
+ routeUnits.removeAll(toBeRemoved);
routeUnits.addAll(toBeAdded);
}
diff --git
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
index b040d7f..723f65a 100644
---
a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
+++
b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
@@ -32,9 +32,7 @@ import
org.apache.shardingsphere.shadow.api.shadow.hint.HintShadowAlgorithm;
import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm;
import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
-import java.util.Arrays;
import java.util.Collection;
-import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
@@ -192,14 +190,36 @@ public final class ShadowRule implements SchemaRule,
DataSourceContainedRule {
* @return all shadow data source mappings
*/
public Map<String, String> getAllShadowDataSourceMappings() {
- return
shadowDataSourceMappings.values().stream().collect(Collectors.toMap(ShadowDataSourceRule::getSourceDataSource,
ShadowDataSourceRule::getShadowDataSource, (key, value) -> value,
- LinkedHashMap::new));
+ return shadowDataSourceMappings.values().stream()
+
.collect(Collectors.toMap(ShadowDataSourceRule::getSourceDataSource,
ShadowDataSourceRule::getShadowDataSource, (a, b) -> b, LinkedHashMap::new));
+ }
+
+ /**
+ * Get source data source name.
+ *
+ * @param actualDataSourceName actual data source name
+ * @return source data source name
+ */
+ public Optional<String> getSourceDataSourceName(final String
actualDataSourceName) {
+ ShadowDataSourceRule shadowDataSourceRule =
shadowDataSourceMappings.get(actualDataSourceName);
+ if (null != shadowDataSourceRule) {
+ return Optional.of(shadowDataSourceRule.getSourceDataSource());
+ }
+ return Optional.empty();
}
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
- return
shadowDataSourceMappings.values().stream().collect(Collectors.toMap(ShadowDataSourceRule::getSourceDataSource,
each ->
- Arrays.asList(each.getSourceDataSource(),
each.getShadowDataSource()), (key, value) -> value, () -> new
HashMap<>(shadowDataSourceMappings.size(), 1)));
+ Map<String, Collection<String>> result = new LinkedHashMap<>();
+ shadowDataSourceMappings.forEach((key, value) -> result.put(key,
createShadowDataSources(value)));
+ return result;
+ }
+
+ private Collection<String> createShadowDataSources(final
ShadowDataSourceRule shadowDataSourceRule) {
+ Collection<String> result = new LinkedList<>();
+ result.add(shadowDataSourceRule.getSourceDataSource());
+ result.add(shadowDataSourceRule.getShadowDataSource());
+ return result;
}
@Override