未验证 提交 552af3ee 编写于 作者: L Liang Zhang 提交者: GitHub

Remove addressingDataSources (#8137)

* Remove TableMetaData.addressingDataSources

* Remove YamlTableMetaData.addressingDataSources

* Move SingleTableRule

* Remove TableAddressingMapperBuilder
上级 e15bdace
......@@ -125,9 +125,6 @@ tables: # 表
generated: false
name: order_id
primaryKey: false
addressingDataSources: # 所在真实数据源名
- ds_0
- ds_1
```
## 动态生效
......
......@@ -125,9 +125,6 @@ tables: # Tables
generated: false
name: order_id
primaryKey: false
addressingDataSources: # Actual data source names
- ds_0
- ds_1
```
## Dynamic Effectiveness
......
/*
* 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.sharding.metadata;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.RuleBasedTableAddressingMapperDecorator;
import org.apache.shardingsphere.sharding.constant.ShardingOrder;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
/**
* Table addressing mapper decorator of sharding.
*/
public final class ShardingTableAddressingMapperDecorator implements RuleBasedTableAddressingMapperDecorator<ShardingRule> {
@Override
public void decorate(final ShardingRule rule, final Map<String, Collection<String>> tableAddressingMapper) {
rule.getTableRules().forEach(each -> decorate(each, tableAddressingMapper));
}
private void decorate(final TableRule tableRule, final Map<String, Collection<String>> tableDataSourceNamesMapper) {
boolean found = false;
for (String each : tableRule.getActualDataNodes().stream().map(DataNode::getTableName).collect(Collectors.toSet())) {
found = null != tableDataSourceNamesMapper.remove(each) || found;
}
if (found) {
tableDataSourceNamesMapper.put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames()));
}
}
@Override
public int getOrder() {
return ShardingOrder.ORDER;
}
@Override
public Class<ShardingRule> getTypeClass() {
return ShardingRule.class;
}
}
......@@ -41,7 +41,8 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShard
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorithm;
import org.apache.shardingsphere.sharding.metadata.SingleTableRuleLoader;
import org.apache.shardingsphere.sharding.rule.single.SingleTableRule;
import org.apache.shardingsphere.sharding.rule.single.SingleTableRuleLoader;
import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
......
......@@ -15,7 +15,7 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.rule;
package org.apache.shardingsphere.sharding.rule.single;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
......
......@@ -15,14 +15,13 @@
* limitations under the License.
*/
package org.apache.shardingsphere.sharding.metadata;
package org.apache.shardingsphere.sharding.rule.single;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader;
import org.apache.shardingsphere.sharding.rule.SingleTableRule;
import javax.sql.DataSource;
import java.sql.SQLException;
......
#
# 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.
#
org.apache.shardingsphere.sharding.metadata.ShardingTableAddressingMapperDecorator
......@@ -29,7 +29,7 @@ import org.apache.shardingsphere.sharding.route.engine.condition.ShardingConditi
import org.apache.shardingsphere.sharding.route.engine.condition.value.ListShardingConditionValue;
import org.apache.shardingsphere.sharding.route.engine.condition.value.ShardingConditionValue;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.SingleTableRule;
import org.apache.shardingsphere.sharding.rule.single.SingleTableRule;
import javax.sql.DataSource;
import java.util.ArrayList;
......
......@@ -23,7 +23,7 @@ import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sharding.rule.SingleTableRule;
import org.apache.shardingsphere.sharding.rule.single.SingleTableRule;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateTableStatement;
import org.junit.Test;
......
......@@ -74,7 +74,6 @@ public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {
new ColumnMetaData("c_date", Types.TIMESTAMP, "timestamp", false, false, false)), Collections.emptySet()));
tableMetaDataMap.put("t_other", new TableMetaData(Collections.singletonList(new ColumnMetaData("order_id", Types.INTEGER, "int", true, false, false)), Collections.emptySet()));
tableMetaDataMap.put("t_category", new TableMetaData());
tableMetaDataMap.get("t_category").getAddressingDataSources().add("single_db");
return new ShardingSphereSchema(tableMetaDataMap);
}
}
......@@ -21,8 +21,6 @@ import lombok.Getter;
import lombok.Setter;
import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Map;
/**
......@@ -35,6 +33,4 @@ public final class YamlTableMetaData implements YamlConfiguration {
private Map<String, YamlColumnMetaData> columns;
private Map<String, YamlIndexMetaData> indexes;
private Collection<String> addressingDataSources = new LinkedHashSet<>();
}
......@@ -60,9 +60,7 @@ public final class SchemaYamlSwapper implements YamlSwapper<YamlSchema, Sharding
}
private TableMetaData convertTable(final YamlTableMetaData table) {
TableMetaData result = new TableMetaData(convertColumns(table.getColumns()), convertIndexes(table.getIndexes()));
result.getAddressingDataSources().addAll(table.getAddressingDataSources());
return result;
return new TableMetaData(convertColumns(table.getColumns()), convertIndexes(table.getIndexes()));
}
private Collection<IndexMetaData> convertIndexes(final Map<String, YamlIndexMetaData> indexes) {
......@@ -85,7 +83,6 @@ public final class SchemaYamlSwapper implements YamlSwapper<YamlSchema, Sharding
YamlTableMetaData result = new YamlTableMetaData();
result.setColumns(convertYamlColumns(table.getColumns()));
result.setIndexes(convertYamlIndexes(table.getIndexes()));
result.setAddressingDataSources(table.getAddressingDataSources());
return result;
}
......
......@@ -33,7 +33,6 @@ import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public final class SchemaYamlSwapperTest {
......@@ -47,9 +46,6 @@ public final class SchemaYamlSwapperTest {
assertThat(yamlSchema.getTables().keySet(), is(Collections.singleton("t_order")));
assertThat(yamlSchema.getTables().get("t_order").getIndexes().keySet(), is(Collections.singleton("primary")));
assertThat(yamlSchema.getTables().get("t_order").getColumns().keySet(), is(Collections.singleton("id")));
assertThat(yamlSchema.getTables().get("t_order").getAddressingDataSources().size(), is(2));
assertTrue(yamlSchema.getTables().get("t_order").getAddressingDataSources().contains("ds_0"));
assertTrue(yamlSchema.getTables().get("t_order").getAddressingDataSources().contains("ds_1"));
}
@Test
......@@ -60,9 +56,6 @@ public final class SchemaYamlSwapperTest {
assertThat(schema.get("t_order").getIndexes().keySet(), is(Collections.singleton("primary")));
assertThat(schema.getAllColumnNames("t_order").size(), is(1));
assertThat(schema.get("t_order").getColumns().keySet(), is(Collections.singleton("id")));
assertThat(schema.get("t_order").getAddressingDataSources().size(), is(2));
assertTrue(schema.get("t_order").getAddressingDataSources().contains("ds_0"));
assertTrue(schema.get("t_order").getAddressingDataSources().contains("ds_1"));
}
@SneakyThrows({URISyntaxException.class, IOException.class})
......
......@@ -20,13 +20,10 @@ package org.apache.shardingsphere.infra.metadata.schema.builder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Map.Entry;
/**
* Schema builder.
......@@ -42,12 +39,6 @@ public final class SchemaBuilder {
* @throws SQLException SQL exception
*/
public static ShardingSphereSchema build(final SchemaBuilderMaterials materials) throws SQLException {
ShardingSphereSchema result = loadSchema(materials);
setTableAddressingMapper(materials, result);
return result;
}
private static ShardingSphereSchema loadSchema(final SchemaBuilderMaterials materials) throws SQLException {
ShardingSphereSchema result = new ShardingSphereSchema();
for (ShardingSphereRule rule : materials.getRules()) {
if (rule instanceof TableContainedRule) {
......@@ -60,14 +51,4 @@ public final class SchemaBuilder {
}
return result;
}
private static void setTableAddressingMapper(final SchemaBuilderMaterials materials, final ShardingSphereSchema schema) throws SQLException {
for (Entry<String, Collection<String>> entry : TableAddressingMapperBuilder.build(materials).entrySet()) {
String tableName = entry.getKey();
if (!schema.containsTable(tableName)) {
schema.put(tableName, new TableMetaData());
}
schema.get(tableName).getAddressingDataSources().addAll(entry.getValue());
}
}
}
/*
* 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.infra.metadata.schema.builder;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader;
import org.apache.shardingsphere.infra.metadata.schema.builder.spi.RuleBasedTableAddressingMapperDecorator;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
/**
* Table addressing mapper builder.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TableAddressingMapperBuilder {
static {
ShardingSphereServiceLoader.register(RuleBasedTableAddressingMapperDecorator.class);
}
/**
* Build table addressing mapper with related data sources.
*
* @param materials schema builder materials
* @return table addressing mapper with related data sources
* @throws SQLException SQL exception
*/
public static Map<String, Collection<String>> build(final SchemaBuilderMaterials materials) throws SQLException {
Map<String, Collection<String>> result = load(materials.getDatabaseType(), materials.getDataSourceMap());
decorate(materials.getRules(), result);
return result;
}
@SuppressWarnings("CollectionWithoutInitialCapacity")
private static Map<String, Collection<String>> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) throws SQLException {
Map<String, Collection<String>> result = new HashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
append(result, databaseType, entry.getKey(), entry.getValue());
}
return result;
}
@SuppressWarnings("CollectionWithoutInitialCapacity")
private static void append(final Map<String, Collection<String>> tableAddressingMapper,
final DatabaseType databaseType, final String dataSourceName, final DataSource dataSource) throws SQLException {
for (String each : SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType)) {
if (!tableAddressingMapper.containsKey(each)) {
tableAddressingMapper.put(each, new LinkedHashSet<>());
}
tableAddressingMapper.get(each).add(dataSourceName);
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
private static void decorate(final Collection<ShardingSphereRule> rules, final Map<String, Collection<String>> tableAddressingMapper) {
for (Entry<ShardingSphereRule, RuleBasedTableAddressingMapperDecorator> entry : OrderedSPIRegistry.getRegisteredServices(rules, RuleBasedTableAddressingMapperDecorator.class).entrySet()) {
entry.getValue().decorate(entry.getKey(), tableAddressingMapper);
}
}
}
/*
* 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.infra.metadata.schema.builder.spi;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI;
import java.util.Collection;
import java.util.Map;
/**
* Table addressing mapper decorator with related rule.
*
* @param <T> type of ShardingSphere rule
*/
public interface RuleBasedTableAddressingMapperDecorator<T extends ShardingSphereRule> extends OrderedSPI<T> {
/**
* Decorate table addressing mapper with data source names.
*
* @param rule ShardingSphere rule
* @param tableAddressingMapper decorated table addressing mapper
*/
void decorate(T rule, Map<String, Collection<String>> tableAddressingMapper);
}
......@@ -26,7 +26,6 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
......@@ -47,8 +46,6 @@ public final class TableMetaData {
private final List<String> primaryKeyColumns = new ArrayList<>();
private final Collection<String> addressingDataSources = new LinkedHashSet<>();
public TableMetaData() {
this(Collections.emptyList(), Collections.emptyList());
}
......
......@@ -45,7 +45,6 @@ public final class CreateTableStatementSchemaRefresher implements SchemaRefreshe
tableMetaData = new TableMetaData();
}
schema.put(tableName, tableMetaData);
schema.get(tableName).getAddressingDataSources().addAll(routeDataSourceNames);
}
private boolean containsInTableContainedRule(final String tableName, final SchemaBuilderMaterials materials) {
......
......@@ -35,6 +35,5 @@ public final class CreateViewStatementSchemaRefresher implements SchemaRefresher
final Collection<String> routeDataSourceNames, final CreateViewStatement sqlStatement, final SchemaBuilderMaterials materials) {
String viewName = sqlStatement.getView().getTableName().getIdentifier().getValue();
schema.put(viewName, new TableMetaData());
schema.get(viewName).getAddressingDataSources().addAll(routeDataSourceNames);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册