Skip to content

Commit

Permalink
Remove StorageUnitMetaData.dataSourceMap
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 29, 2023
1 parent 7869839 commit 1abeb4a
Show file tree
Hide file tree
Showing 19 changed files with 139 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.StorageResource;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilder;
import org.apache.shardingsphere.infra.metadata.database.schema.builder.GenericSchemaBuilderMaterial;
Expand All @@ -38,12 +38,15 @@
import org.apache.shardingsphere.infra.rule.identifier.type.MutableDataNodeRule;
import org.apache.shardingsphere.infra.state.datasource.DataSourceStateManager;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* ShardingSphere database.
Expand Down Expand Up @@ -195,7 +198,9 @@ public synchronized void reloadRules(final Class<? extends ShardingSphereRule> r
Collection<ShardingSphereRule> databaseRules = new LinkedList<>(ruleMetaData.getRules());
toBeReloadedRules.stream().findFirst().ifPresent(optional -> {
databaseRules.removeAll(toBeReloadedRules);
databaseRules.add(((MutableDataNodeRule) optional).reloadRule(ruleConfig, name, resourceMetaData.getStorageUnitMetaData().getDataSources(), databaseRules));
Map<String, DataSource> dataSources = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
databaseRules.add(((MutableDataNodeRule) optional).reloadRule(ruleConfig, name, dataSources, databaseRules));
});
ruleMetaData.getRules().clear();
ruleMetaData.getRules().addAll(databaseRules);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ public final class StorageUnitMetaData {

private final Map<String, StorageUnit> storageUnits;

private final Map<String, DataSource> dataSources;

private final Map<String, NewStorageUnitMetaData> metaDataMap;

public StorageUnitMetaData(final String databaseName, final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap,
Expand All @@ -49,7 +47,5 @@ public StorageUnitMetaData(final String databaseName, final Map<String, StorageN
storageUnits = storageNodes.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, dataSources.get(entry.getValue().getName()), dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()),
(oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageNodes.size(), 1F)));
this.dataSources = storageUnits.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageUnits.size(), 1F)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ public final class GenericSchemaBuilderMaterial {
public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final StorageUnitMetaData storageUnitMetaData,
final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName) {
this(protocolType, storageUnitMetaData.getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
storageUnitMetaData.getDataSources(), rules, props, defaultSchemaName);
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
storageUnitMetaData.getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
rules, props, defaultSchemaName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
Expand Down Expand Up @@ -146,10 +149,10 @@ private DataNode createDataNode(final String dataSourceName) {

private ShardingSphereDatabase mockDatabaseWithMultipleResources() {
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1F);
dataSourceMap.put("ds_0", new MockedDataSource());
dataSourceMap.put("ds_1", new MockedDataSource());
when(result.getResourceMetaData().getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap);
Map<String, NewStorageUnitMetaData> metaDataMap = new HashMap<>(2, 1F);
metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap);
when(result.getName()).thenReturn(DefaultDatabase.LOGIC_NAME);
ShardingSphereSchema schema = mock(ShardingSphereSchema.class);
when(schema.containsTable("t_order")).thenReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.stream.Collectors;

/**
* Show unloaded single table executor.
Expand Down Expand Up @@ -61,7 +64,9 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
private Map<String, Collection<DataNode>> getActualDataNodes(final ShardingSphereDatabase database) {
ResourceMetaData resourceMetaData = database.getResourceMetaData();
Map<String, DataSource> aggregateDataSourceMap = SingleTableLoadUtils.getAggregatedDataSourceMap(
resourceMetaData.getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules());
resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules());
Collection<String> excludedTables = SingleTableLoadUtils.getExcludedTables(database.getRuleMetaData().getRules());
return SingleTableDataNodeLoader.load(database.getName(), database.getProtocolType(), aggregateDataSourceMap, excludedTables);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -113,7 +114,9 @@ private void checkActualTableExist(final ShardingSphereDatabase database, final
}
ResourceMetaData resourceMetaData = database.getResourceMetaData();
Map<String, DataSource> aggregateDataSourceMap = SingleTableLoadUtils.getAggregatedDataSourceMap(
resourceMetaData.getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules());
resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules());
Map<String, Map<String, Collection<String>>> actualTableNodes = new LinkedHashMap<>();
for (String each : requiredDataSources) {
Map<String, Collection<String>> schemaTableNames = SingleTableDataNodeLoader.loadSchemaTableNames(database.getName(), database.getProtocolType(), aggregateDataSourceMap.get(each), each);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@

package org.apache.shardingsphere.transaction.rule;

import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
import org.apache.shardingsphere.transaction.core.fixture.ShardingSphereTransactionManagerFixture;
import org.junit.jupiter.api.Test;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Map;

import static org.hamcrest.CoreMatchers.instanceOf;
Expand Down Expand Up @@ -60,14 +62,14 @@ void assertAddResource() {
assertThat(actual.getDatabases().size(), is(2));
assertTrue(actual.getDatabases().containsKey(SHARDING_DB_1));
ResourceMetaData resourceMetaData1 = actual.getDatabases().get(SHARDING_DB_1).getResourceMetaData();
assertThat(resourceMetaData1.getStorageUnitMetaData().getDataSources().size(), is(2));
assertTrue(resourceMetaData1.getStorageUnitMetaData().getDataSources().containsKey("ds_0"));
assertTrue(resourceMetaData1.getStorageUnitMetaData().getDataSources().containsKey("ds_1"));
assertThat(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().size(), is(2));
assertTrue(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_0"));
assertTrue(resourceMetaData1.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_1"));
assertTrue(actual.getDatabases().containsKey(SHARDING_DB_2));
ResourceMetaData resourceMetaData2 = actual.getDatabases().get(SHARDING_DB_2).getResourceMetaData();
assertThat(resourceMetaData2.getStorageUnitMetaData().getDataSources().size(), is(2));
assertTrue(resourceMetaData2.getStorageUnitMetaData().getDataSources().containsKey("ds_0"));
assertTrue(resourceMetaData2.getStorageUnitMetaData().getDataSources().containsKey("ds_1"));
assertThat(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().size(), is(2));
assertTrue(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_0"));
assertTrue(resourceMetaData2.getStorageUnitMetaData().getMetaDataMap().containsKey("ds_1"));
assertThat(actual.getResource().getTransactionManager(TransactionType.XA), instanceOf(ShardingSphereTransactionManagerFixture.class));
}

Expand Down Expand Up @@ -97,10 +99,10 @@ private ShardingSphereDatabase createDatabase() {

private ResourceMetaData createResourceMetaData() {
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(2, 1F);
dataSourceMap.put("ds_0", new MockedDataSource());
dataSourceMap.put("ds_1", new MockedDataSource());
when(result.getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap);
Map<String, NewStorageUnitMetaData> metaDataMap = new HashMap<>(2, 1F);
metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap);
return result;
}

Expand All @@ -114,10 +116,10 @@ private ShardingSphereDatabase createAddDatabase() {

private ResourceMetaData createAddResourceMetaData() {
ResourceMetaData result = mock(ResourceMetaData.class, RETURNS_DEEP_STUBS);
Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(2, 1F);
dataSourceMap.put("ds_0", new MockedDataSource());
dataSourceMap.put("ds_1", new MockedDataSource());
when(result.getStorageUnitMetaData().getDataSources()).thenReturn(dataSourceMap);
Map<String, NewStorageUnitMetaData> metaDataMap = new HashMap<>(2, 1F);
metaDataMap.put("ds_0", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
metaDataMap.put("ds_1", new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), new MockedDataSource()));
when(result.getStorageUnitMetaData().getMetaDataMap()).thenReturn(metaDataMap);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public synchronized void alterRuleConfiguration(final String databaseName, final
Collection<ShardingSphereRule> rules = new LinkedList<>(database.getRuleMetaData().getRules());
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
rules.addAll(DatabaseRulesBuilder.build(databaseName,
database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig, instanceContext));
database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules(), ruleConfig, instanceContext));
refreshMetadata(databaseName, database, rules);
} catch (final SQLException ex) {
log.error("Alter database: {} rule configurations failed", databaseName, ex);
Expand All @@ -195,7 +197,9 @@ public synchronized void dropRuleConfiguration(final String databaseName, final
rules.removeIf(each -> each.getConfiguration().getClass().isAssignableFrom(ruleConfig.getClass()));
if (isNotEmptyConfig(ruleConfig)) {
rules.addAll(DatabaseRulesBuilder.build(databaseName,
database.getResourceMetaData().getStorageUnitMetaData().getDataSources(), database.getRuleMetaData().getRules(), ruleConfig, instanceContext));
database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
database.getRuleMetaData().getRules(), ruleConfig, instanceContext));
}
refreshMetadata(databaseName, database, rules);
} catch (final SQLException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -146,7 +147,8 @@ private static void persistDatabaseConfigurations(final MetaDataContexts metadat
for (Entry<String, ? extends DatabaseConfiguration> entry : param.getDatabaseConfigs().entrySet()) {
String databaseName = entry.getKey();
metadataContexts.getPersistService().persistConfigurations(entry.getKey(), entry.getValue(),
metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getDataSources(),
metadataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, each -> each.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
metadataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules());
}
}
Expand Down
Loading

0 comments on commit 1abeb4a

Please sign in to comment.