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.data.pipeline.cdc.util;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
23  import org.apache.shardingsphere.data.pipeline.core.exception.param.PipelineInvalidParameterException;
24  import org.apache.shardingsphere.infra.datanode.DataNode;
25  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
26  import org.apache.shardingsphere.infra.rule.attribute.datanode.DataNodeRuleAttribute;
27  import org.apache.shardingsphere.sharding.rule.ShardingRule;
28  import org.apache.shardingsphere.sharding.rule.ShardingTable;
29  import org.apache.shardingsphere.single.rule.SingleRule;
30  
31  import java.util.ArrayList;
32  import java.util.Collection;
33  import java.util.Collections;
34  import java.util.HashMap;
35  import java.util.List;
36  import java.util.Map;
37  import java.util.Optional;
38  
39  /**
40   * CDC data node utils.
41   */
42  @NoArgsConstructor(access = AccessLevel.PRIVATE)
43  public final class CDCDataNodeUtils {
44      
45      /**
46       * Build data nodes map.
47       *
48       * @param database database
49       * @param tableNames table names
50       * @return data nodes map
51       * @throws PipelineInvalidParameterException thrown invalid parameter exception when can't get data nodes.
52       */
53      public static Map<String, List<DataNode>> buildDataNodesMap(final ShardingSphereDatabase database, final Collection<String> tableNames) {
54          Optional<ShardingRule> shardingRule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
55          Optional<SingleRule> singleRule = database.getRuleMetaData().findSingleRule(SingleRule.class);
56          Optional<BroadcastRule> broadcastRule = database.getRuleMetaData().findSingleRule(BroadcastRule.class);
57          Map<String, List<DataNode>> result = new HashMap<>();
58          // TODO support virtual data source name
59          for (String each : tableNames) {
60              if (singleRule.isPresent() && singleRule.get().getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().containsKey(each)) {
61                  result.put(each, new ArrayList<>(singleRule.get().getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().get(each)));
62                  continue;
63              }
64              if (shardingRule.isPresent() && shardingRule.get().findShardingTable(each).isPresent()) {
65                  ShardingTable shardingTable = shardingRule.get().getShardingTable(each);
66                  result.put(each, shardingTable.getActualDataNodes());
67                  continue;
68              }
69              if (broadcastRule.isPresent() && broadcastRule.get().getAttributes().getAttribute(DataNodeRuleAttribute.class).findFirstActualTable(each).isPresent()) {
70                  result.put(each, Collections.singletonList(broadcastRule.get().getAttributes().getAttribute(DataNodeRuleAttribute.class).getAllDataNodes().get(each).iterator().next()));
71                  continue;
72              }
73              throw new PipelineInvalidParameterException(String.format("Not find actual data nodes of `%s`", each));
74          }
75          return result;
76      }
77  }