You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2021/09/22 02:50:58 UTC

[shardingsphere] branch master updated: Fix broad cast table meta data missing. (#12559)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 c99ab65  Fix broad cast table meta data missing. (#12559)
c99ab65 is described below

commit c99ab658faf129ee692a1da6c73c834b9764b3bc
Author: tuichenchuxin <86...@users.noreply.github.com>
AuthorDate: Wed Sep 22 10:50:18 2021 +0800

    Fix broad cast table meta data missing. (#12559)
---
 .../metadata/ShardingTableMetaDataBuilder.java     |  4 ++--
 .../shardingsphere/sharding/rule/ShardingRule.java |  5 +++--
 .../sharding/rule/ShardingRuleTest.java            |  2 +-
 .../schema/builder/util/TableMetaDataUtil.java     | 23 +++++++++++++++++-----
 4 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
index de101e5..5b40149 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableMetaDataBuilder.java
@@ -52,7 +52,7 @@ public final class ShardingTableMetaDataBuilder implements RuleBasedTableMetaDat
     
     @Override
     public Map<String, TableMetaData> load(final Collection<String> tableNames, final ShardingRule rule, final SchemaBuilderMaterials materials) throws SQLException {
-        Collection<String> needLoadTables = tableNames.stream().filter(each -> rule.findTableRule(each).isPresent()).collect(Collectors.toList());
+        Collection<String> needLoadTables = tableNames.stream().filter(each -> rule.findTableRule(each).isPresent() || rule.isBroadcastTable(each)).collect(Collectors.toList());
         if (needLoadTables.isEmpty()) {
             return Collections.emptyMap();
         }
@@ -86,7 +86,7 @@ public final class ShardingTableMetaDataBuilder implements RuleBasedTableMetaDat
     private Map<String, TableMetaData> getTableMetaDataMap(final Collection<TableMetaData> tableMetaDataList, final ShardingRule rule) {
         Map<String, TableMetaData> result = new LinkedHashMap<>();
         for (TableMetaData each : tableMetaDataList) {
-            rule.findLogicTableByActualTable(each.getName()).ifPresent(tableName -> result.putIfAbsent(tableName, each));
+            result.putIfAbsent(rule.findLogicTableByActualTable(each.getName()).orElse(each.getName()), each);
         }
         return result;
     }
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index efda0db..ffc9d6f 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -186,7 +186,6 @@ public final class ShardingRule implements SchemaRule, DataNodeContainedRule, Ta
     public Collection<String> getAllTables() {
         Collection<String> result = new HashSet<>(getTables());
         result.addAll(getAllActualTables());
-        result.addAll(broadcastTables);
         return result;
     }
     
@@ -491,7 +490,9 @@ public final class ShardingRule implements SchemaRule, DataNodeContainedRule, Ta
     
     @Override
     public Collection<String> getTables() {
-        return tableRules.values().stream().map(TableRule::getLogicTable).collect(Collectors.toSet());
+        Collection<String> result = tableRules.values().stream().map(TableRule::getLogicTable).collect(Collectors.toSet());
+        result.addAll(broadcastTables);
+        return result;
     }
     
     @Override
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
index f37ed50..a3f552d 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-core/src/test/java/org/apache/shardingsphere/sharding/rule/ShardingRuleTest.java
@@ -303,7 +303,7 @@ public final class ShardingRuleTest {
     
     @Test
     public void assertGetTables() {
-        assertThat(createMaximumShardingRule().getTables(), is(new LinkedHashSet<>(Arrays.asList("LOGIC_TABLE", "SUB_LOGIC_TABLE"))));
+        assertThat(createMaximumShardingRule().getTables(), is(new LinkedHashSet<>(Arrays.asList("LOGIC_TABLE", "SUB_LOGIC_TABLE", "BROADCAST_TABLE"))));
     }
     
     @Test
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/TableMetaDataUtil.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/TableMetaDataUtil.java
index 09fc0c4..429564a 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/TableMetaDataUtil.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/schema/builder/util/TableMetaDataUtil.java
@@ -50,17 +50,30 @@ public class TableMetaDataUtil {
         DataNodes dataNodes = new DataNodes(materials.getRules());
         for (String each : tableNames) {
             if (checkMetaDataEnable) {
-                dataNodes.getDataNodes(each).forEach(dataNode -> addDataSourceTableGroups(dataNode.getDataSourceName(), dataNode.getTableName(), dataSourceTableGroups));
+                addAllActualTableDataNode(materials, dataSourceTableGroups, dataNodes, each);
             } else {
-                Optional<DataNode> optional = dataNodes.getDataNodes(each).stream().findFirst();
-                String dataSourceName = optional.map(DataNode::getDataSourceName).orElse(materials.getDataSourceMap().keySet().iterator().next());
-                String tableName = optional.map(DataNode::getTableName).orElse(each);
-                addDataSourceTableGroups(dataSourceName, tableName, dataSourceTableGroups);
+                addOneActualTableDataNode(materials, dataSourceTableGroups, dataNodes, each);
             }
         }
         return dataSourceTableGroups.entrySet().stream().map(entry -> new TableMetaDataLoaderMaterial(entry.getValue(), materials.getDataSourceMap().get(entry.getKey()))).collect(Collectors.toList());
     }
     
+    private static void addOneActualTableDataNode(final SchemaBuilderMaterials materials, final Map<String, Collection<String>> dataSourceTableGroups, final DataNodes dataNodes, final String table) {
+        Optional<DataNode> optional = dataNodes.getDataNodes(table).stream().findFirst();
+        String dataSourceName = optional.map(DataNode::getDataSourceName).orElse(materials.getDataSourceMap().keySet().iterator().next());
+        String tableName = optional.map(DataNode::getTableName).orElse(table);
+        addDataSourceTableGroups(dataSourceName, tableName, dataSourceTableGroups);
+    }
+    
+    private static void addAllActualTableDataNode(final SchemaBuilderMaterials materials, final Map<String, Collection<String>> dataSourceTableGroups, final DataNodes dataNodes, final String table) {
+        Collection<DataNode> tableDataNodes = dataNodes.getDataNodes(table);
+        if (tableDataNodes.isEmpty()) {
+            addDataSourceTableGroups(materials.getDataSourceMap().keySet().iterator().next(), table, dataSourceTableGroups);
+        } else {
+            tableDataNodes.forEach(dataNode -> addDataSourceTableGroups(dataNode.getDataSourceName(), dataNode.getTableName(), dataSourceTableGroups));
+        }
+    }
+    
     private static void addDataSourceTableGroups(final String dataSourceName, final String tableName, final Map<String, Collection<String>> dataSourceTableGroups) {
         Collection<String> tables = dataSourceTableGroups.getOrDefault(dataSourceName, new LinkedList<>());
         tables.add(tableName);