Skip to content

Commit

Permalink
Fix broad cast table meta data missing. (apache#12559)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuichenchuxin authored Sep 22, 2021
1 parent 8fd67fa commit c99ab65
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -86,7 +86,7 @@ private void checkTableMetaData(final Collection<TableMetaData> tableMetaDataLis
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private BindingTableRule createBindingTableRule(final String bindingTableGroup)
public Collection<String> getAllTables() {
Collection<String> result = new HashSet<>(getTables());
result.addAll(getAllActualTables());
result.addAll(broadcastTables);
return result;
}

Expand Down Expand Up @@ -491,7 +490,9 @@ public Optional<String> findLogicTableByActualTable(final String actualTable) {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ public void assertTableRuleNotExists() {

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,30 @@ public static Collection<TableMetaDataLoaderMaterial> getTableMetaDataLoadMateri
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);
Expand Down

0 comments on commit c99ab65

Please sign in to comment.