1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
41
42 @NoArgsConstructor(access = AccessLevel.PRIVATE)
43 public final class CDCDataNodeUtils {
44
45
46
47
48
49
50
51
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
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 }