View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.sharding.route.engine.type.broadcast;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
22  import org.apache.shardingsphere.infra.binder.context.type.IndexAvailable;
23  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24  import org.apache.shardingsphere.infra.datanode.DataNode;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedTable;
27  import org.apache.shardingsphere.infra.metadata.database.schema.util.IndexMetaDataUtils;
28  import org.apache.shardingsphere.infra.route.context.RouteContext;
29  import org.apache.shardingsphere.infra.route.context.RouteMapper;
30  import org.apache.shardingsphere.infra.route.context.RouteUnit;
31  import org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
32  import org.apache.shardingsphere.sharding.route.engine.type.complex.ShardingCartesianRoutingEngine;
33  import org.apache.shardingsphere.sharding.rule.ShardingRule;
34  import org.apache.shardingsphere.sharding.rule.ShardingTable;
35  import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
36  
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.LinkedList;
40  import java.util.stream.Collectors;
41  
42  /**
43   * Sharding broadcast routing engine for tables.
44   */
45  @RequiredArgsConstructor
46  public final class ShardingTableBroadcastRoutingEngine implements ShardingRouteEngine {
47      
48      private final ShardingSphereDatabase database;
49      
50      private final SQLStatementContext sqlStatementContext;
51      
52      private final Collection<String> shardingRuleTableNames;
53      
54      @Override
55      public RouteContext route(final ShardingRule shardingRule) {
56          RouteContext result = new RouteContext();
57          Collection<String> logicTableNames = getLogicTableNames();
58          if (logicTableNames.isEmpty()) {
59              result.getRouteUnits().addAll(getBroadcastTableRouteUnits(shardingRule, ""));
60              return result;
61          }
62          if (logicTableNames.size() > 1 && shardingRule.isAllBindingTables(logicTableNames)) {
63              result.getRouteUnits().addAll(getBindingTableRouteUnits(shardingRule, logicTableNames));
64          } else {
65              Collection<RouteContext> routeContexts = getRouteContexts(shardingRule, logicTableNames);
66              RouteContext routeContext = new ShardingCartesianRoutingEngine(routeContexts).route(shardingRule);
67              result.getOriginalDataNodes().addAll(routeContext.getOriginalDataNodes());
68              result.getRouteUnits().addAll(routeContext.getRouteUnits());
69          }
70          return result;
71      }
72      
73      private Collection<RouteContext> getRouteContexts(final ShardingRule shardingRule, final Collection<String> logicTableNames) {
74          Collection<RouteContext> result = new LinkedList<>();
75          for (String each : logicTableNames) {
76              RouteContext routeContext = new RouteContext();
77              if (shardingRule.findShardingTable(each).isPresent()) {
78                  routeContext.getRouteUnits().addAll(getAllRouteUnits(shardingRule, each));
79              }
80              if (!routeContext.getRouteUnits().isEmpty()) {
81                  result.add(routeContext);
82              }
83          }
84          return result;
85      }
86      
87      private Collection<RouteUnit> getBindingTableRouteUnits(final ShardingRule shardingRule, final Collection<String> tableNames) {
88          String primaryTableName = tableNames.iterator().next();
89          Collection<RouteUnit> result = new LinkedList<>();
90          ShardingTable shardingTable = shardingRule.getShardingTable(primaryTableName);
91          for (DataNode each : shardingTable.getActualDataNodes()) {
92              result.add(new RouteUnit(new RouteMapper(each.getDataSourceName(), each.getDataSourceName()), getBindingTableMappers(shardingRule, each, primaryTableName, tableNames)));
93          }
94          return result;
95      }
96      
97      private Collection<RouteMapper> getBindingTableMappers(final ShardingRule shardingRule, final DataNode dataNode, final String primaryTableName, final Collection<String> tableNames) {
98          Collection<RouteMapper> result = new LinkedList<>();
99          result.add(new RouteMapper(primaryTableName, dataNode.getTableName()));
100         result.addAll(shardingRule.getLogicAndActualTablesFromBindingTable(dataNode.getDataSourceName(), primaryTableName, dataNode.getTableName(), tableNames)
101                 .entrySet().stream().map(each -> new RouteMapper(each.getKey(), each.getValue())).collect(Collectors.toList()));
102         return result;
103     }
104     
105     private Collection<String> getLogicTableNames() {
106         if (!shardingRuleTableNames.isEmpty()) {
107             return shardingRuleTableNames;
108         }
109         return sqlStatementContext instanceof IndexAvailable
110                 ? getTableNames(database, sqlStatementContext.getDatabaseType(), ((IndexAvailable) sqlStatementContext).getIndexes())
111                 : Collections.emptyList();
112     }
113     
114     private Collection<String> getTableNames(final ShardingSphereDatabase database, final DatabaseType databaseType, final Collection<IndexSegment> indexes) {
115         Collection<String> result = new LinkedList<>();
116         for (QualifiedTable each : IndexMetaDataUtils.getTableNames(database, databaseType, indexes)) {
117             result.add(each.getTableName());
118         }
119         return result;
120     }
121     
122     private Collection<RouteUnit> getBroadcastTableRouteUnits(final ShardingRule shardingRule, final String broadcastTableName) {
123         Collection<RouteUnit> result = new LinkedList<>();
124         for (String each : shardingRule.getDataSourceNames()) {
125             result.add(new RouteUnit(new RouteMapper(each, each), Collections.singletonList(new RouteMapper(broadcastTableName, broadcastTableName))));
126         }
127         return result;
128     }
129     
130     private Collection<RouteUnit> getAllRouteUnits(final ShardingRule shardingRule, final String logicTableName) {
131         Collection<RouteUnit> result = new LinkedList<>();
132         ShardingTable shardingTable = shardingRule.getShardingTable(logicTableName);
133         for (DataNode each : shardingTable.getActualDataNodes()) {
134             result.add(new RouteUnit(new RouteMapper(each.getDataSourceName(), each.getDataSourceName()), Collections.singletonList(new RouteMapper(logicTableName, each.getTableName()))));
135         }
136         return result;
137     }
138 }