未验证 提交 3847b306 编写于 作者: L Liang Zhang 提交者: GitHub

Decouple sharding data source name and master slave name (#5412)

* add DataNodes

* Use DataNodes to get actual data nodes in meta data loader

* Use DataNodes to get actual data nodes in meta data parallel load

* remove ShardingDataSourceNames.shardingRuleConfig

* Add DataNodeUtil

* Remove ShardingDataSourceNames

* for checkstyle
上级 64f2a2e1
......@@ -24,6 +24,7 @@ import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaDataL
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.metadata.schema.spi.RuleMetaDataLoader;
import org.apache.shardingsphere.underlying.common.rule.DataNodes;
import javax.sql.DataSource;
import java.sql.SQLException;
......@@ -38,7 +39,7 @@ import java.util.Optional;
public final class EncryptMetaDataLoader implements RuleMetaDataLoader<EncryptRule> {
@Override
public SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
public SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
final EncryptRule encryptRule, final ConfigurationProperties properties, final Collection<String> excludedTableNames) throws SQLException {
DataSource dataSource = dataSourceMap.values().iterator().next();
Collection<String> encryptTableNames = encryptRule.getEncryptTableNames();
......@@ -52,15 +53,15 @@ public final class EncryptMetaDataLoader implements RuleMetaDataLoader<EncryptRu
}
@Override
public Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final String tableName, final EncryptRule encryptRule, final ConfigurationProperties properties) throws SQLException {
public Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
final String tableName, final EncryptRule encryptRule, final ConfigurationProperties properties) throws SQLException {
return encryptRule.findEncryptTable(tableName).isPresent()
? TableMetaDataLoader.load(dataSourceMap.values().iterator().next(), tableName, databaseType.getName()) : Optional.empty();
}
@Override
public int getOrder() {
return 5;
return 10;
}
@Override
......
......@@ -273,4 +273,9 @@ public final class EncryptRule implements BaseRule {
public Collection<String> getEncryptTableNames() {
return tables.keySet();
}
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
return Collections.emptyMap();
}
}
......@@ -30,6 +30,7 @@ import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.apache.shardingsphere.underlying.common.metadata.schema.spi.RuleMetaDataLoader;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.apache.shardingsphere.underlying.common.rule.DataNodes;
import javax.sql.DataSource;
import java.sql.SQLException;
......@@ -56,23 +57,23 @@ public final class ShardingMetaDataLoader implements RuleMetaDataLoader<Sharding
private static final int CPU_CORES = Runtime.getRuntime().availableProcessors();
private static final int FUTURE_GET_TIME_OUT_SEC = 5;
private static final int FUTURE_GET_TIME_OUT_SECOND = 5;
@Override
public SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final ShardingRule shardingRule, final ConfigurationProperties properties, final Collection<String> excludedTableNames) throws SQLException {
public SchemaMetaData load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
final ShardingRule shardingRule, final ConfigurationProperties properties, final Collection<String> excludedTableNames) throws SQLException {
SchemaMetaData result = new SchemaMetaData(new HashMap<>(shardingRule.getTableRules().size(), 1));
for (TableRule each : shardingRule.getTableRules()) {
if (!excludedTableNames.contains(each.getLogicTable())) {
load(databaseType, dataSourceMap, each.getLogicTable(), shardingRule, properties).ifPresent(tableMetaData -> result.put(each.getLogicTable(), tableMetaData));
load(databaseType, dataSourceMap, dataNodes, each.getLogicTable(), shardingRule, properties).ifPresent(tableMetaData -> result.put(each.getLogicTable(), tableMetaData));
}
}
return result;
}
@Override
public Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final String tableName, final ShardingRule shardingRule, final ConfigurationProperties properties) throws SQLException {
public Optional<TableMetaData> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
final String tableName, final ShardingRule shardingRule, final ConfigurationProperties properties) throws SQLException {
if (!shardingRule.findTableRule(tableName).isPresent()) {
return Optional.empty();
}
......@@ -80,46 +81,45 @@ public final class ShardingMetaDataLoader implements RuleMetaDataLoader<Sharding
int maxConnectionsSizePerQuery = properties.getValue(ConfigurationPropertyKey.MAX_CONNECTIONS_SIZE_PER_QUERY);
TableRule tableRule = shardingRule.getTableRule(tableName);
if (!isCheckingMetaData) {
DataNode dataNode = tableRule.getActualDataNodes().iterator().next();
return TableMetaDataLoader.load(dataSourceMap.get(shardingRule.getShardingDataSourceNames()
.getRawMasterDataSourceName(dataNode.getDataSourceName())), dataNode.getTableName(), databaseType.getName());
DataNode dataNode = dataNodes.getDataNodes(tableName).iterator().next();
return TableMetaDataLoader.load(dataSourceMap.get(dataNode.getDataSourceName()), dataNode.getTableName(), databaseType.getName());
}
Map<String, TableMetaData> actualTableMetaDataMap = parallelLoadTables(databaseType, dataSourceMap, shardingRule, tableRule, maxConnectionsSizePerQuery);
Map<String, TableMetaData> actualTableMetaDataMap = parallelLoadTables(databaseType, dataSourceMap, dataNodes, tableName, maxConnectionsSizePerQuery);
checkUniformed(tableRule.getLogicTable(), actualTableMetaDataMap, shardingRule);
return Optional.of(actualTableMetaDataMap.values().iterator().next());
}
private Map<String, TableMetaData> parallelLoadTables(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
final ShardingRule shardingRule, final TableRule tableRule, final int maxConnectionsSizePerQuery) {
Map<String, List<DataNode>> dataNodeGroups = tableRule.getDataNodeGroups();
Map<String, TableMetaData> actualTableMetaDataMap = new HashMap<>(dataNodeGroups.size(), 1);
private Map<String, TableMetaData> parallelLoadTables(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final DataNodes dataNodes,
final String tableName, final int maxConnectionsSizePerQuery) {
Map<String, List<DataNode>> dataNodeGroups = dataNodes.getDataNodeGroups(tableName);
Map<String, TableMetaData> result = new HashMap<>(dataNodeGroups.size(), 1);
Map<String, Future<Optional<TableMetaData>>> tableFutureMap = new HashMap<>(dataNodeGroups.size(), 1);
ExecutorService executorService = Executors.newFixedThreadPool(Math.min(CPU_CORES * 2, dataNodeGroups.size() * maxConnectionsSizePerQuery));
for (Entry<String, List<DataNode>> entry : dataNodeGroups.entrySet()) {
for (DataNode each : entry.getValue()) {
Future<Optional<TableMetaData>> futures = executorService.submit(() -> loadTableByDataNode(shardingRule, each, databaseType, dataSourceMap));
Future<Optional<TableMetaData>> futures = executorService.submit(() -> loadTableByDataNode(each, databaseType, dataSourceMap));
tableFutureMap.put(each.getTableName(), futures);
}
}
tableFutureMap.forEach((key, value) -> {
try {
getTableMetaData(value).ifPresent(tableMetaData -> actualTableMetaDataMap.put(key, tableMetaData));
} catch (InterruptedException | ExecutionException | TimeoutException e) {
throw new IllegalStateException(String.format("Error while fetching tableMetaData with key= %s and Value=%s", key, value), e);
getTableMetaData(value).ifPresent(tableMetaData -> result.put(key, tableMetaData));
} catch (final InterruptedException | ExecutionException | TimeoutException ex) {
throw new IllegalStateException(String.format("Error while fetching tableMetaData with key= %s and Value=%s", key, value), ex);
}
});
executorService.shutdownNow();
return actualTableMetaDataMap;
return result;
}
private Optional<TableMetaData> getTableMetaData(final Future<Optional<TableMetaData>> value) throws InterruptedException, ExecutionException, TimeoutException {
return value.get(FUTURE_GET_TIME_OUT_SEC, TimeUnit.SECONDS);
return value.get(FUTURE_GET_TIME_OUT_SECOND, TimeUnit.SECONDS);
}
private Optional<TableMetaData> loadTableByDataNode(final ShardingRule shardingRule, final DataNode dataNode, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
private Optional<TableMetaData> loadTableByDataNode(final DataNode dataNode, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) {
try {
return TableMetaDataLoader.load(
dataSourceMap.get(shardingRule.getShardingDataSourceNames().getRawMasterDataSourceName(dataNode.getDataSourceName())), dataNode.getTableName(), databaseType.getName());
dataSourceMap.get(dataNode.getDataSourceName()), dataNode.getTableName(), databaseType.getName());
} catch (SQLException e) {
throw new IllegalStateException(String.format("SQLException for DataNode=%s and databaseType=%s", dataNode, databaseType.getName()), e);
}
......
......@@ -26,8 +26,11 @@ import org.apache.shardingsphere.spi.type.TypedSPIRegistry;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
......@@ -106,4 +109,14 @@ public final class MasterSlaveRule implements BaseRule {
disabledDataSourceNames.remove(dataSourceName);
}
}
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
Map<String, Collection<String>> result = new HashMap<>();
Collection<String> actualDataSourceNames = new LinkedList<>();
actualDataSourceNames.add(masterDataSourceName);
actualDataSourceNames.addAll(slaveDataSourceNames);
result.put(name, actualDataSourceNames);
return result;
}
}
......@@ -21,6 +21,10 @@ import lombok.Getter;
import org.apache.shardingsphere.api.config.shadow.ShadowRuleConfiguration;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* Databases shadow rule.
*/
......@@ -35,4 +39,9 @@ public final class ShadowRule implements BaseRule {
column = shadowRuleConfiguration.getColumn();
ruleConfiguration = shadowRuleConfiguration;
}
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
return Collections.emptyMap();
}
}
......@@ -36,6 +36,7 @@ import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.apache.shardingsphere.underlying.common.rule.TablesAggregationRule;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
......@@ -55,9 +56,11 @@ public final class ShardingRule implements TablesAggregationRule {
ShardingSphereServiceLoader.register(KeyGenerateAlgorithm.class);
}
private static final String SHARDING_LOGIC_DATA_SOURCE = "shardingsphere_sharding_logic_ds";
private final ShardingRuleConfiguration ruleConfiguration;
private final ShardingDataSourceNames shardingDataSourceNames;
private final Collection<String> dataSourceNames;
private final Collection<TableRule> tableRules;
......@@ -75,7 +78,7 @@ public final class ShardingRule implements TablesAggregationRule {
Preconditions.checkArgument(null != shardingRuleConfig, "ShardingRuleConfig cannot be null.");
Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty.");
this.ruleConfiguration = shardingRuleConfig;
shardingDataSourceNames = new ShardingDataSourceNames(shardingRuleConfig, getDataSourceNames(shardingRuleConfig.getTableRuleConfigs(), dataSourceNames));
this.dataSourceNames = getDataSourceNames(shardingRuleConfig.getTableRuleConfigs(), dataSourceNames);
tableRules = createTableRules(shardingRuleConfig);
broadcastTables = shardingRuleConfig.getBroadcastTables();
bindingTableRules = createBindingTableRules(shardingRuleConfig.getBindingTableGroups());
......@@ -108,7 +111,7 @@ public final class ShardingRule implements TablesAggregationRule {
private Collection<TableRule> createTableRules(final ShardingRuleConfiguration shardingRuleConfig) {
return shardingRuleConfig.getTableRuleConfigs().stream().map(each ->
new TableRule(each, shardingDataSourceNames, getDefaultGenerateKeyColumn(shardingRuleConfig))).collect(Collectors.toList());
new TableRule(each, dataSourceNames, getDefaultGenerateKeyColumn(shardingRuleConfig))).collect(Collectors.toList());
}
private String getDefaultGenerateKeyColumn(final ShardingRuleConfiguration shardingRuleConfig) {
......@@ -167,7 +170,7 @@ public final class ShardingRule implements TablesAggregationRule {
return tableRule.get();
}
if (isBroadcastTable(logicTableName)) {
return new TableRule(shardingDataSourceNames.getDataSourceNames(), logicTableName);
return new TableRule(dataSourceNames, logicTableName);
}
throw new ShardingSphereConfigurationException("Cannot find table rule with logic table: '%s'", logicTableName);
}
......@@ -331,7 +334,7 @@ public final class ShardingRule implements TablesAggregationRule {
*/
public DataNode getDataNode(final String dataSourceName, final String logicTableName) {
TableRule tableRule = getTableRule(logicTableName);
return tableRule.getActualDataNodes().stream().filter(each -> shardingDataSourceNames.getDataSourceNames().contains(each.getDataSourceName())
return tableRule.getActualDataNodes().stream().filter(each -> dataSourceNames.contains(each.getDataSourceName())
&& each.getDataSourceName().equals(dataSourceName)).findFirst()
.orElseThrow(() -> new ShardingSphereConfigurationException("Cannot find actual data node for data source name: '%s' and logic table name: '%s'", dataSourceName, logicTableName));
}
......@@ -358,8 +361,23 @@ public final class ShardingRule implements TablesAggregationRule {
public Map<String, String> getLogicAndActualTablesFromBindingTable(final String dataSourceName,
final String logicTable, final String actualTable, final Collection<String> availableLogicBindingTables) {
Map<String, String> result = new LinkedHashMap<>();
findBindingTableRule(logicTable).ifPresent(
bindingTableRule -> result.putAll(bindingTableRule.getLogicAndActualTables(dataSourceName, logicTable, actualTable, availableLogicBindingTables)));
findBindingTableRule(logicTable).ifPresent(bindingTableRule -> result.putAll(bindingTableRule.getLogicAndActualTables(dataSourceName, logicTable, actualTable, availableLogicBindingTables)));
return result;
}
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
Map<String, Collection<String>> result = new HashMap<>();
result.put(SHARDING_LOGIC_DATA_SOURCE, getAllActualTables());
return result;
}
@Override
public Map<String, Collection<DataNode>> getAllDataNodes() {
Map<String, Collection<DataNode>> result = new HashMap<>(tableRules.size(), 1);
for (TableRule each : tableRules) {
result.put(each.getLogicTable(), each.getActualDataNodes());
}
return result;
}
......
......@@ -23,19 +23,19 @@ import lombok.Getter;
import lombok.ToString;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.core.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategy;
import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory;
import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy;
import org.apache.shardingsphere.spi.keygen.KeyGenerateAlgorithm;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.core.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.underlying.common.exception.ShardingSphereException;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.apache.shardingsphere.underlying.common.rule.DataNodeUtil;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
......@@ -85,12 +85,11 @@ public final class TableRule {
keyGenerateAlgorithm = null;
}
public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDataSourceNames shardingDataSourceNames, final String defaultGenerateKeyColumn) {
public TableRule(final TableRuleConfiguration tableRuleConfig, final Collection<String> dataSourceNames, final String defaultGenerateKeyColumn) {
logicTable = tableRuleConfig.getLogicTable().toLowerCase();
List<String> dataNodes = new InlineExpressionParser(tableRuleConfig.getActualDataNodes()).splitAndEvaluate();
dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1);
actualDataNodes = isEmptyDataNodes(dataNodes)
? generateDataNodes(tableRuleConfig.getLogicTable(), shardingDataSourceNames.getDataSourceNames()) : generateDataNodes(dataNodes, shardingDataSourceNames.getDataSourceNames());
actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), dataSourceNames) : generateDataNodes(dataNodes, dataSourceNames);
actualTables = getActualTables();
databaseShardingStrategy = null == tableRuleConfig.getDatabaseShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getDatabaseShardingStrategyConfig());
tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategyConfig());
......@@ -105,7 +104,7 @@ public final class TableRule {
}
private void addActualTable(final String datasourceName, final String tableName) {
datasourceToTablesMap.computeIfAbsent(datasourceName, k -> new LinkedHashSet<>()).add(tableName);
datasourceToTablesMap.computeIfAbsent(datasourceName, key -> new LinkedHashSet<>()).add(tableName);
}
private boolean containsKeyGenerateAlgorithm(final TableRuleConfiguration tableRuleConfiguration) {
......@@ -150,18 +149,10 @@ public final class TableRule {
/**
* Get data node groups.
*
* @return data node groups, key is data source name, value is data nodes belong to this data source
* @return data node groups, key is data source name, values are data nodes belong to this data source
*/
public Map<String, List<DataNode>> getDataNodeGroups() {
Map<String, List<DataNode>> result = new LinkedHashMap<>(actualDataNodes.size(), 1);
for (DataNode each : actualDataNodes) {
String dataSourceName = each.getDataSourceName();
if (!result.containsKey(dataSourceName)) {
result.put(dataSourceName, new LinkedList<>());
}
result.get(dataSourceName).add(each);
}
return result;
return DataNodeUtil.getDataNodeGroups(actualDataNodes);
}
/**
......
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.core.rule;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.junit.Test;
......@@ -75,14 +74,10 @@ public final class BindingTableRuleTest {
}
private TableRule createTableRule() {
return new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..1}"), createShardingDataSourceNames(), null);
return new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..1}"), Arrays.asList("ds0", "ds1"), null);
}
private TableRule createSubTableRule() {
return new TableRule(new TableRuleConfiguration("SUB_LOGIC_TABLE", "ds${0..1}.sub_table_${0..1}"), createShardingDataSourceNames(), null);
}
private ShardingDataSourceNames createShardingDataSourceNames() {
return new ShardingDataSourceNames(new ShardingRuleConfiguration(), Arrays.asList("ds0", "ds1"));
return new TableRule(new TableRuleConfiguration("SUB_LOGIC_TABLE", "ds${0..1}.sub_table_${0..1}"), Arrays.asList("ds0", "ds1"), null);
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.core.rule;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public final class ShardingDataSourceNamesTest {
@Test
@Ignore
public void assertGetAllDataSourceNames() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getMasterSlaveRuleConfigs().add(
new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Collections.singletonList("slave_ds"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN")));
Collection<String> actual = new ShardingDataSourceNames(shardingRuleConfig, Arrays.asList("default_ds", "master_ds", "slave_ds")).getDataSourceNames();
assertThat(actual, is(Sets.newLinkedHashSet(Arrays.asList("default_ds", "ms_ds"))));
}
@Test
public void assertGetRawMasterDataSourceNameWithMasterSlaveDataSourceName() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getMasterSlaveRuleConfigs().add(
new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Collections.singletonList("slave_ds"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN")));
String actual = new ShardingDataSourceNames(shardingRuleConfig, Arrays.asList("master_ds", "slave_ds")).getRawMasterDataSourceName("ms_ds");
assertThat(actual, is("master_ds"));
}
@Test
public void assertGetRawMasterDataSourceNameWithoutMasterSlaveDataSourceName() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getMasterSlaveRuleConfigs().add(
new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Collections.singletonList("slave_ds"), new LoadBalanceStrategyConfiguration("ROUND_ROBIN")));
String actual = new ShardingDataSourceNames(shardingRuleConfig, Arrays.asList("master_ds", "slave_ds")).getRawMasterDataSourceName("default_ds");
assertThat(actual, is("default_ds"));
}
@Test(expected = IllegalArgumentException.class)
public void assertConstructShardingDataSourceNamesWithNullShardingRuleConfiguration() {
new ShardingDataSourceNames(null, Arrays.asList("master_ds", "slave_ds")).getRawMasterDataSourceName("default_ds");
}
}
......@@ -19,7 +19,6 @@ package org.apache.shardingsphere.core.rule;
import com.google.common.collect.Sets;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.NoneShardingStrategyConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.StandardShardingStrategyConfiguration;
......@@ -55,7 +54,7 @@ public final class TableRuleTest {
@Test
public void assertCreateMinTableRule() {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration("LOGIC_TABLE");
TableRule actual = new TableRule(tableRuleConfig, createShardingDataSourceNames(), null);
TableRule actual = new TableRule(tableRuleConfig, Arrays.asList("ds0", "ds1"), null);
assertThat(actual.getLogicTable(), is("logic_table"));
assertThat(actual.getActualDataNodes().size(), is(2));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "LOGIC_TABLE")));
......@@ -72,7 +71,7 @@ public final class TableRuleTest {
tableRuleConfig.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
KeyGenerateAlgorithm keyGenerateAlgorithm = TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class, "INCREMENT", new Properties());
tableRuleConfig.setKeyGeneratorConfig(new KeyGeneratorConfiguration("col_1", keyGenerateAlgorithm));
TableRule actual = new TableRule(tableRuleConfig, createShardingDataSourceNames(), null);
TableRule actual = new TableRule(tableRuleConfig, Arrays.asList("ds0", "ds1"), null);
assertThat(actual.getLogicTable(), is("logic_table"));
assertThat(actual.getActualDataNodes().size(), is(6));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "table_0")));
......@@ -90,13 +89,13 @@ public final class TableRuleTest {
@Test
public void assertGetActualDatasourceNames() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertThat(actual.getActualDatasourceNames(), is(Sets.newLinkedHashSet(Arrays.asList("ds0", "ds1"))));
}
@Test
public void assertGetActualTableNames() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertThat(actual.getActualTableNames("ds0"), is(Sets.newLinkedHashSet(Arrays.asList("table_0", "table_1", "table_2"))));
assertThat(actual.getActualTableNames("ds1"), is(Sets.newLinkedHashSet(Arrays.asList("table_0", "table_1", "table_2"))));
assertThat(actual.getActualTableNames("ds2"), is(Collections.emptySet()));
......@@ -104,25 +103,25 @@ public final class TableRuleTest {
@Test
public void assertFindActualTableIndex() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertThat(actual.findActualTableIndex("ds1", "table_1"), is(4));
}
@Test
public void assertNotFindActualTableIndex() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertThat(actual.findActualTableIndex("ds2", "table_2"), is(-1));
}
@Test
public void assertActualTableNameExisted() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertTrue(actual.isExisted("table_2"));
}
@Test
public void assertActualTableNameNotExisted() {
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), createShardingDataSourceNames(), null);
TableRule actual = new TableRule(new TableRuleConfiguration("LOGIC_TABLE", "ds${0..1}.table_${0..2}"), Arrays.asList("ds0", "ds1"), null);
assertFalse(actual.isExisted("table_3"));
}
......@@ -132,10 +131,6 @@ public final class TableRuleTest {
InlineShardingAlgorithm shardingAlgorithm = new InlineShardingAlgorithm();
shardingAlgorithm.getProperties().setProperty("algorithm.expression", "table_${shardingColumn % 3}");
tableRuleConfiguration.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("shardingColumn", shardingAlgorithm));
new TableRule(tableRuleConfiguration, createShardingDataSourceNames(), null);
}
private ShardingDataSourceNames createShardingDataSourceNames() {
return new ShardingDataSourceNames(new ShardingRuleConfiguration(), Arrays.asList("ds0", "ds1"));
new TableRule(tableRuleConfiguration, Arrays.asList("ds0", "ds1"), null);
}
}
......@@ -33,7 +33,7 @@ public final class ShardingDatabaseBroadcastRoutingEngine implements ShardingRou
@Override
public RouteResult route(final ShardingRule shardingRule) {
RouteResult result = new RouteResult();
for (String each : shardingRule.getShardingDataSourceNames().getDataSourceNames()) {
for (String each : shardingRule.getDataSourceNames()) {
result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
}
return result;
......
......@@ -38,7 +38,7 @@ public final class ShardingInstanceBroadcastRoutingEngine implements ShardingRou
@Override
public RouteResult route(final ShardingRule shardingRule) {
RouteResult result = new RouteResult();
for (String each : shardingRule.getShardingDataSourceNames().getDataSourceNames()) {
for (String each : shardingRule.getDataSourceNames()) {
if (dataSourceMetas.getAllInstanceDataSourceNames().contains(each)) {
result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
}
......
......@@ -17,16 +17,17 @@
package org.apache.shardingsphere.sharding.route.engine.type.unicast;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.core.rule.TableRule;
import org.apache.shardingsphere.sharding.route.engine.type.ShardingRouteEngine;
import org.apache.shardingsphere.underlying.common.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.underlying.common.rule.DataNode;
import org.apache.shardingsphere.underlying.route.context.RouteMapper;
import org.apache.shardingsphere.underlying.route.context.RouteResult;
import org.apache.shardingsphere.underlying.route.context.RouteUnit;
import org.apache.shardingsphere.underlying.route.context.RouteMapper;
import java.util.ArrayList;
import java.util.Collection;
......@@ -34,6 +35,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
/**
* Sharding unicast routing engine.
......@@ -46,7 +48,7 @@ public final class ShardingUnicastRoutingEngine implements ShardingRouteEngine {
@Override
public RouteResult route(final ShardingRule shardingRule) {
RouteResult result = new RouteResult();
String dataSourceName = shardingRule.getShardingDataSourceNames().getRandomDataSourceName();
String dataSourceName = getRandomDataSourceName(shardingRule.getDataSourceNames());
RouteMapper dataSourceMapper = new RouteMapper(dataSourceName, dataSourceName);
if (shardingRule.isAllBroadcastTables(logicTables)) {
List<RouteMapper> tableMappers = new ArrayList<>(logicTables.size());
......@@ -86,9 +88,13 @@ public final class ShardingUnicastRoutingEngine implements ShardingRouteEngine {
if (availableDatasourceNames.isEmpty()) {
throw new ShardingSphereConfigurationException("Cannot find actual datasource intersection for logic tables: %s", logicTables);
}
dataSourceName = shardingRule.getShardingDataSourceNames().getRandomDataSourceName(availableDatasourceNames);
dataSourceName = getRandomDataSourceName(availableDatasourceNames);
result.getRouteUnits().add(new RouteUnit(new RouteMapper(dataSourceName, dataSourceName), tableMappers));
}
return result;
}
private String getRandomDataSourceName(final Collection<String> dataSourceNames) {
return Lists.newArrayList(dataSourceNames).get(ThreadLocalRandom.current().nextInt(dataSourceNames.size()));
}
}
......@@ -18,7 +18,6 @@
package org.apache.shardingsphere.sharding.route.engine.type.broadcast;
import com.google.common.collect.Lists;
import org.apache.shardingsphere.core.rule.ShardingDataSourceNames;
import org.apache.shardingsphere.core.rule.ShardingRule;
import org.apache.shardingsphere.underlying.common.metadata.datasource.DataSourceMetas;
import org.apache.shardingsphere.underlying.route.context.RouteResult;
......@@ -28,6 +27,8 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collections;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
......@@ -40,9 +41,6 @@ public final class ShardingInstanceBroadcastRoutingEngineTest {
@Mock
private ShardingRule shardingRule;
@Mock
private ShardingDataSourceNames shardingDataSourceNames;
@Mock
private DataSourceMetas dataSourceMetas;
......@@ -50,9 +48,8 @@ public final class ShardingInstanceBroadcastRoutingEngineTest {
@Before
public void setUp() {
when(shardingRule.getShardingDataSourceNames()).thenReturn(shardingDataSourceNames);
when(shardingRule.getDataSourceNames()).thenReturn(Collections.singletonList(DATASOURCE_NAME));
when(dataSourceMetas.getAllInstanceDataSourceNames()).thenReturn(Lists.newArrayList(DATASOURCE_NAME));
when(shardingDataSourceNames.getDataSourceNames()).thenReturn(Lists.newArrayList(DATASOURCE_NAME));
shardingInstanceBroadcastRoutingEngine = new ShardingInstanceBroadcastRoutingEngine(dataSourceMetas);
}
......
......@@ -147,9 +147,9 @@ public class OrchestrationSpringBootRegistryShardingTest {
ShardingDataSource shardingDataSource = getFieldValue("dataSource", OrchestrationShardingDataSource.class, dataSource);
RuntimeContext runtimeContext = shardingDataSource.getRuntimeContext();
ShardingRule shardingRule = (ShardingRule) runtimeContext.getRules().iterator().next();
assertThat(shardingRule.getShardingDataSourceNames().getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_1"));
assertThat(shardingRule.getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getDataSourceNames().contains("ds_1"));
}
@Test
......
......@@ -79,9 +79,9 @@ public class OrchestrationSpringBootShardingTest {
ShardingDataSource shardingDataSource = getFieldValue("dataSource", OrchestrationShardingDataSource.class, dataSource);
RuntimeContext runtimeContext = shardingDataSource.getRuntimeContext();
ShardingRule shardingRule = (ShardingRule) runtimeContext.getRules().iterator().next();
assertThat(shardingRule.getShardingDataSourceNames().getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_1"));
assertThat(shardingRule.getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getDataSourceNames().contains("ds_1"));
}
@Test
......
......@@ -49,6 +49,6 @@ public class SpringBootBroadcastTableTest {
ShardingDataSource shardingDataSource = (ShardingDataSource) dataSource;
ShardingRule shardingRule = (ShardingRule) shardingDataSource.getRuntimeContext().getRules().iterator().next();
assertThat(shardingRule.getBroadcastTables(), is(Collections.singletonList("t_config")));
assertThat(shardingRule.getShardingDataSourceNames().getDataSourceNames().size(), is(3));
assertThat(shardingRule.getDataSourceNames().size(), is(3));
}
}
......@@ -67,9 +67,9 @@ public class SpringBootShardingTest {
public void assertWithShardingDataSourceNames() {
RuntimeContext runtimeContext = ((ShardingDataSource) dataSource).getRuntimeContext();
ShardingRule shardingRule = (ShardingRule) runtimeContext.getRules().iterator().next();
assertThat(shardingRule.getShardingDataSourceNames().getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getShardingDataSourceNames().getDataSourceNames().contains("ds_1"));
assertThat(shardingRule.getDataSourceNames().size(), is(2));
assertTrue(shardingRule.getDataSourceNames().contains("ds_0"));
assertTrue(shardingRule.getDataSourceNames().contains("ds_1"));
}
@Test
......
......@@ -17,28 +17,28 @@
package org.apache.shardingsphere.ui.util;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.core.yaml.constructor.YamlRootShardingConfigurationConstructor;
import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.orchestration.core.configuration.DataSourceConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.apache.shardingsphere.core.rule.Authentication;
import org.apache.shardingsphere.core.yaml.config.common.YamlAuthenticationConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.core.yaml.config.masterslave.YamlMasterSlaveRuleConfiguration;
import org.apache.shardingsphere.core.yaml.config.sharding.YamlShardingRuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.apache.shardingsphere.core.yaml.constructor.YamlRootShardingConfigurationConstructor;
import org.apache.shardingsphere.core.yaml.swapper.AuthenticationYamlSwapper;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.MasterSlaveRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.ShardingRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.configuration.DataSourceConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.configuration.YamlDataSourceConfiguration;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import java.util.Map;
import java.util.Properties;
......
......@@ -29,6 +29,7 @@ import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.metadata.schema.spi.RuleMetaDataDecorator;
import org.apache.shardingsphere.underlying.common.metadata.schema.spi.RuleMetaDataLoader;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import org.apache.shardingsphere.underlying.common.rule.DataNodes;
import org.apache.shardingsphere.underlying.common.rule.TablesAggregationRule;
import javax.sql.DataSource;
......@@ -67,7 +68,7 @@ public final class RuleSchemaMetaDataLoader {
Collection<String> excludedTableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
SchemaMetaData configuredSchemaMetaData = new SchemaMetaData(new HashMap<>());
for (Entry<BaseRule, RuleMetaDataLoader> entry : OrderedSPIRegistry.getRegisteredServices(rules, RuleMetaDataLoader.class).entrySet()) {
SchemaMetaData schemaMetaData = entry.getValue().load(databaseType, dataSourceMap, entry.getKey(), properties, excludedTableNames);
SchemaMetaData schemaMetaData = entry.getValue().load(databaseType, dataSourceMap, new DataNodes(rules), entry.getKey(), properties, excludedTableNames);
excludedTableNames.addAll(schemaMetaData.getAllTableNames());
if (entry.getKey() instanceof TablesAggregationRule) {
excludedTableNames.addAll(((TablesAggregationRule) entry.getKey()).getAllActualTables());
......@@ -116,7 +117,7 @@ public final class RuleSchemaMetaDataLoader {
public Optional<TableMetaData> load(final DatabaseType databaseType,
final Map<String, DataSource> dataSourceMap, final String tableName, final ConfigurationProperties properties) throws SQLException {
for (Entry<BaseRule, RuleMetaDataLoader> entry : OrderedSPIRegistry.getRegisteredServices(rules, RuleMetaDataLoader.class).entrySet()) {
Optional<TableMetaData> result = entry.getValue().load(databaseType, dataSourceMap, tableName, entry.getKey(), properties);
Optional<TableMetaData> result = entry.getValue().load(databaseType, dataSourceMap, new DataNodes(rules), tableName, entry.getKey(), properties);
if (result.isPresent()) {
return Optional.of(decorate(tableName, result.get()));
}
......
......@@ -23,6 +23,7 @@ import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.underlying.common.database.type.DatabaseType;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import org.apache.shardingsphere.underlying.common.rule.DataNodes;
import javax.sql.DataSource;
import java.sql.SQLException;
......@@ -42,24 +43,28 @@ public interface RuleMetaDataLoader<T extends BaseRule> extends OrderedSPI<T> {
*
* @param databaseType database type
* @param dataSourceMap data source map
* @param dataNodes data nodes
* @param rule rule
* @param properties configuration properties
* @param excludedTableNames excluded table names
* @return table name and meta data map
* @throws SQLException SQL exception
*/
SchemaMetaData load(DatabaseType databaseType, Map<String, DataSource> dataSourceMap, T rule, ConfigurationProperties properties, Collection<String> excludedTableNames) throws SQLException;
SchemaMetaData load(DatabaseType databaseType, Map<String, DataSource> dataSourceMap, DataNodes dataNodes,
T rule, ConfigurationProperties properties, Collection<String> excludedTableNames) throws SQLException;
/**
* Load table meta data.
*
* @param databaseType database type
* @param dataSourceMap data source map
* @param dataNodes data nodes
* @param tableName table name
* @param rule rule
* @param properties configuration properties
* @return meta data
* @throws SQLException SQL exception
*/
Optional<TableMetaData> load(DatabaseType databaseType, Map<String, DataSource> dataSourceMap, String tableName, T rule, ConfigurationProperties properties) throws SQLException;
Optional<TableMetaData> load(DatabaseType databaseType, Map<String, DataSource> dataSourceMap, DataNodes dataNodes,
String tableName, T rule, ConfigurationProperties properties) throws SQLException;
}
......@@ -19,11 +19,21 @@ package org.apache.shardingsphere.underlying.common.rule;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import java.util.Collection;
import java.util.Map;
/**
* Base rule.
*/
public interface BaseRule {
/**
* Get data source mapper.
*
* @return data source mapper
*/
Map<String, Collection<String>> getDataSourceMapper();
/**
* Get rule configuration.
*
......
......@@ -15,66 +15,38 @@
* limitations under the License.
*/
package org.apache.shardingsphere.core.rule;
package org.apache.shardingsphere.underlying.common.rule;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import lombok.Getter;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import java.util.Collection;
import java.util.concurrent.ThreadLocalRandom;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* Sharding data source names.
*
* <p>Will convert actual data source names to master-slave data source name.</p>
* Data node utility.
*/
public final class ShardingDataSourceNames {
private final ShardingRuleConfiguration shardingRuleConfig;
@Getter
private final Collection<String> dataSourceNames;
public ShardingDataSourceNames(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) {
Preconditions.checkArgument(null != shardingRuleConfig, "can not construct ShardingDataSourceNames with null ShardingRuleConfig");
this.shardingRuleConfig = shardingRuleConfig;
this.dataSourceNames = dataSourceNames;
}
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataNodeUtil {
/**
* Get raw master data source name.
* Get data node groups.
*
* @param dataSourceName data source name
* @return raw master data source name
* @param dataNodes data nodes
* @return data node groups, key is data source name, values are data nodes belong to this data source
*/
public String getRawMasterDataSourceName(final String dataSourceName) {
for (MasterSlaveRuleConfiguration each : shardingRuleConfig.getMasterSlaveRuleConfigs()) {
if (each.getName().equals(dataSourceName)) {
return each.getMasterDataSourceName();
public static Map<String, List<DataNode>> getDataNodeGroups(final Collection<DataNode> dataNodes) {
Map<String, List<DataNode>> result = new LinkedHashMap<>(dataNodes.size(), 1);
for (DataNode each : dataNodes) {
String dataSourceName = each.getDataSourceName();
if (!result.containsKey(dataSourceName)) {
result.put(dataSourceName, new LinkedList<>());
}
result.get(dataSourceName).add(each);
}
return dataSourceName;
}
/**
* Get random data source name.
*
* @return random data source name
*/
public String getRandomDataSourceName() {
return getRandomDataSourceName(dataSourceNames);
}
/**
* Get random data source name.
*
* @param dataSourceNames available data source names
* @return random data source name
*/
public String getRandomDataSourceName(final Collection<String> dataSourceNames) {
return Lists.newArrayList(dataSourceNames).get(ThreadLocalRandom.current().nextInt(dataSourceNames.size()));
return result;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.underlying.common.rule;
import lombok.RequiredArgsConstructor;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Data nodes.
*/
@RequiredArgsConstructor
public final class DataNodes {
private final Collection<BaseRule> rules;
/**
* Get data nodes.
*
* @param tableName table name
* @return data nodes
*/
public Collection<DataNode> getDataNodes(final String tableName) {
Optional<TablesAggregationRule> tablesAggregationRule = rules.stream().filter(each -> each instanceof TablesAggregationRule).findFirst().map(rule -> (TablesAggregationRule) rule);
if (!tablesAggregationRule.isPresent()) {
return Collections.emptyList();
}
Collection<DataNode> result = new LinkedList<>(tablesAggregationRule.get().getAllDataNodes().get(tableName));
for (BaseRule each : rules) {
if (each instanceof TablesAggregationRule) {
continue;
}
for (Entry<String, Collection<String>> entry : each.getDataSourceMapper().entrySet()) {
Collection<DataNode> dataNodes = find(result, entry.getKey());
result.removeAll(dataNodes);
result.addAll(regenerate(dataNodes, entry.getValue()));
}
}
return result;
}
private Collection<DataNode> find(final Collection<DataNode> dataNodes, final String logicDataSource) {
return dataNodes.stream().filter(each -> each.getDataSourceName().equals(logicDataSource)).collect(Collectors.toList());
}
private Collection<DataNode> regenerate(final Collection<DataNode> dataNodes, final Collection<String> actualDataSources) {
Collection<DataNode> result = new LinkedHashSet<>();
for (DataNode each : dataNodes) {
result.addAll(regenerate(actualDataSources, each.getTableName()));
}
return result;
}
private Collection<DataNode> regenerate(final Collection<String> dataSources, final String table) {
Collection<DataNode> result = new LinkedHashSet<>();
for (String each : dataSources) {
result.add(new DataNode(each, table));
}
return result;
}
/**
* Get data node groups.
*
* @param tableName table name
* @return data node groups, key is data source name, values are data nodes belong to this data source
*/
public Map<String, List<DataNode>> getDataNodeGroups(final String tableName) {
return DataNodeUtil.getDataNodeGroups(getDataNodes(tableName));
}
}
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.underlying.common.rule;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
/**
......@@ -25,6 +26,13 @@ import java.util.Optional;
*/
public interface TablesAggregationRule extends BaseRule {
/**
* Get all data nodes.
*
* @return all data nodes map, key is logic table name, values are data node collection belong to the key
*/
Map<String, Collection<DataNode>> getAllDataNodes();
/**
* Get all actual tables.
*
......
......@@ -20,11 +20,20 @@ package org.apache.shardingsphere.underlying.merge.result.impl.fixture;
import org.apache.shardingsphere.underlying.common.config.RuleConfiguration;
import org.apache.shardingsphere.underlying.common.rule.BaseRule;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
/**
* Rule for test.
*/
public final class TestRule implements BaseRule {
@Override
public Map<String, Collection<String>> getDataSourceMapper() {
return Collections.emptyMap();
}
@Override
public RuleConfiguration getRuleConfiguration() {
return null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册