Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ResourceMetaData.storageUnitMetaData #28629

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,6 @@ public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginTyp
}

private int getStorageUnitCount(final MetaDataContexts metaDataContexts) {
return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().size()).reduce(0, Integer::sum);
return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnitMetaDataMap().size()).reduce(0, Integer::sum);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.metadata.persist.MetaDataPersistService;
import org.apache.shardingsphere.mode.manager.ContextManager;
Expand Down Expand Up @@ -75,7 +75,7 @@ void assertExportWithContextManager() {

private ContextManager mockContextManager() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", mock(NewStorageUnitMetaData.class)));
when(database.getResourceMetaData().getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnitMetaData.class)));
when(database.getProtocolType()).thenReturn(TypedSPILoader.getService(DatabaseType.class, "FIXTURE"));
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
when(metaData.getDatabases()).thenReturn(Collections.singletonMap("sharding_db", database));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private void checkToBeCreatedEncryptors(final CreateEncryptRuleStatement sqlStat
}

private void checkDataSources(final ShardingSphereDatabase database) {
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnitMetaDataMap().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ private static Collection<String> getDuplicated(final Collection<String> require
private static void checkDuplicateRuleNamesWithExistsDataSources(final ShardingSphereDatabase database, final Collection<ReadwriteSplittingRuleSegment> segments) {
Collection<String> currentRuleNames = new HashSet<>();
ResourceMetaData resourceMetaData = database.getResourceMetaData();
if (null != resourceMetaData && null != resourceMetaData.getStorageUnitMetaData().getMetaDataMap()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().keySet());
if (null != resourceMetaData && null != resourceMetaData.getStorageUnitMetaDataMap()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaDataMap().keySet());
}
currentRuleNames.addAll(getLogicDataSources(database));
Collection<String> toBeCreatedRuleNames = segments.stream().map(ReadwriteSplittingRuleSegment::getName).filter(currentRuleNames::contains).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
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.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
Expand Down Expand Up @@ -68,19 +67,18 @@ class CreateReadwriteSplittingRuleStatementUpdaterTest {
@BeforeEach
void before() {
when(database.getResourceMetaData()).thenReturn(resourceMetaData);
when(resourceMetaData.getStorageUnitMetaData()).thenReturn(mock(StorageUnitMetaData.class));
when(database.getRuleMetaData().findRules(DataSourceContainedRule.class)).thenReturn(Collections.emptyList());
}

@Test
void assertCheckSQLStatementWithDuplicateRuleNames() {
when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.emptyMap());
when(resourceMetaData.getStorageUnitMetaDataMap()).thenReturn(Collections.emptyMap());
assertThrows(DuplicateRuleException.class, () -> updater.checkSQLStatement(database, createSQLStatement("TEST"), createCurrentRuleConfiguration()));
}

@Test
void assertCheckSQLStatementWithDuplicateResource() {
when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("write_ds", null));
when(resourceMetaData.getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("write_ds", null));
assertThrows(InvalidRuleConfigurationException.class, () -> updater.checkSQLStatement(database, createSQLStatement("write_ds", "TEST"), createCurrentRuleConfiguration()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
Expand Down Expand Up @@ -81,13 +81,13 @@ private void collectForShardingStatisticTable(final ShardingSphereDatabase datab
row.add(each.getLogicTable());
row.add(dataNode.getDataSourceName());
row.add(dataNode.getTableName());
addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(), dataNode, row);
addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaDataMap(), dataNode, row);
tableData.getRows().add(new ShardingSphereRowData(row));
}
}
}

private void addTableRowsAndDataLength(final Map<String, NewStorageUnitMetaData> metaDataMap, final DataNode dataNode, final List<Object> row) throws SQLException {
private void addTableRowsAndDataLength(final Map<String, StorageUnitMetaData> metaDataMap, final DataNode dataNode, final List<Object> row) throws SQLException {
DatabaseType databaseType = metaDataMap.get(dataNode.getDataSourceName()).getStorageUnit().getStorageType();
Optional<DialectShardingStatisticsTableCollector> dialectCollector = DatabaseTypedSPILoader.findService(DialectShardingStatisticsTableCollector.class, databaseType);
boolean isAppended = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ private void closeResources(final ShardingSphereDatabase database) {
database.getRuleMetaData().findRules(ResourceHeldRule.class).forEach(each -> each.closeStaleResource(databaseName));
database.getRuleMetaData().findSingleRule(StaticDataSourceContainedRule.class).ifPresent(StaticDataSourceContainedRule::cleanStorageNodeDataSources);
Optional.ofNullable(database.getResourceMetaData())
.ifPresent(optional -> optional.getStorageUnitMetaData().getMetaDataMap().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
.ifPresent(optional -> optional.getStorageUnitMetaDataMap().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void dropSchema(final String schemaName) {
* @return is completed or not
*/
public boolean isComplete() {
return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnitMetaData().getMetaDataMap().isEmpty();
return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnitMetaDataMap().isEmpty();
}

/**
Expand All @@ -184,7 +184,7 @@ public boolean isComplete() {
* @return contains data source or not
*/
public boolean containsDataSource() {
return !resourceMetaData.getStorageUnitMetaData().getMetaDataMap().isEmpty();
return !resourceMetaData.getStorageUnitMetaDataMap().isEmpty();
}

/**
Expand All @@ -198,7 +198,7 @@ public synchronized void reloadRules(final Class<? extends ShardingSphereRule> r
Collection<ShardingSphereRule> databaseRules = new LinkedList<>(ruleMetaData.getRules());
toBeReloadedRules.stream().findFirst().ifPresent(optional -> {
databaseRules.removeAll(toBeReloadedRules);
Map<String, DataSource> dataSources = resourceMetaData.getStorageUnitMetaData().getMetaDataMap().entrySet().stream()
Map<String, DataSource> dataSources = resourceMetaData.getStorageUnitMetaDataMap().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));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.Getter;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
Expand All @@ -44,20 +45,34 @@ public final class ResourceMetaData {

private final Map<StorageNodeName, DataSource> dataSources;

private final StorageUnitMetaData storageUnitMetaData;
private final Map<String, StorageUnitMetaData> storageUnitMetaDataMap;

public ResourceMetaData(final Map<String, DataSource> dataSources) {
this.dataSources = StorageNodeUtils.getStorageNodeDataSources(dataSources);
storageUnitMetaData = new StorageUnitMetaData(null, StorageUnitNodeMapUtils.fromDataSources(dataSources),
dataSources.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
this.dataSources);
Map<String, StorageNode> storageNodes = StorageUnitNodeMapUtils.fromDataSources(dataSources);
Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap = dataSources.entrySet().stream().collect(
Collectors.toMap(Entry::getKey, entry -> DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
storageUnitMetaDataMap = new LinkedHashMap<>();
for (Entry<String, StorageNode> entry : storageNodes.entrySet()) {
DataSource dataSource = dataSources.get(entry.getValue().getName().getName());
if (!(dataSource instanceof CatalogSwitchableDataSource)) {
dataSource = new CatalogSwitchableDataSource(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl());
}
storageUnitMetaDataMap.put(entry.getKey(), new StorageUnitMetaData(null, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSource));
}
}

public ResourceMetaData(final String databaseName, final Map<StorageNodeName, DataSource> dataSources,
final Map<String, StorageNode> storageUnitNodeMap, final Map<String, DataSourcePoolProperties> propsMap) {
final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> propsMap) {
this.dataSources = dataSources;
storageUnitMetaData = new StorageUnitMetaData(databaseName, storageUnitNodeMap, propsMap, dataSources);
storageUnitMetaDataMap = new LinkedHashMap<>();
for (Entry<String, StorageNode> entry : storageNodes.entrySet()) {
DataSource dataSource = dataSources.get(entry.getValue().getName());
if (!(dataSource instanceof CatalogSwitchableDataSource)) {
dataSource = new CatalogSwitchableDataSource(dataSource, entry.getValue().getCatalog(), entry.getValue().getUrl());
}
storageUnitMetaDataMap.put(entry.getKey(), new StorageUnitMetaData(databaseName, entry.getValue(), propsMap.get(entry.getKey()), dataSource));
}
}

/**
Expand All @@ -67,7 +82,7 @@ public ResourceMetaData(final String databaseName, final Map<StorageNodeName, Da
*/
public Collection<String> getAllInstanceDataSourceNames() {
Collection<String> result = new LinkedList<>();
for (String each : storageUnitMetaData.getMetaDataMap().keySet()) {
for (String each : storageUnitMetaDataMap.keySet()) {
if (!isExisted(each, result)) {
result.add(each);
}
Expand All @@ -76,8 +91,8 @@ public Collection<String> getAllInstanceDataSourceNames() {
}

private boolean isExisted(final String dataSourceName, final Collection<String> existedDataSourceNames) {
return existedDataSourceNames.stream().anyMatch(each -> storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getConnectionProperties()
.isInSameDatabaseInstance(storageUnitMetaData.getMetaDataMap().get(each).getStorageUnit().getConnectionProperties()));
return existedDataSourceNames.stream().anyMatch(each -> storageUnitMetaDataMap.get(dataSourceName).getStorageUnit().getConnectionProperties()
.isInSameDatabaseInstance(storageUnitMetaDataMap.get(each).getStorageUnit().getConnectionProperties()));
}

/**
Expand All @@ -87,7 +102,7 @@ private boolean isExisted(final String dataSourceName, final Collection<String>
* @return connection properties
*/
public ConnectionProperties getConnectionProperties(final String dataSourceName) {
return storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getConnectionProperties();
return storageUnitMetaDataMap.get(dataSourceName).getStorageUnit().getConnectionProperties();
}

/**
Expand All @@ -97,7 +112,7 @@ public ConnectionProperties getConnectionProperties(final String dataSourceName)
* @return storage type
*/
public DatabaseType getStorageType(final String dataSourceName) {
return storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getStorageType();
return storageUnitMetaDataMap.get(dataSourceName).getStorageUnit().getStorageType();
}

/**
Expand All @@ -107,6 +122,6 @@ public DatabaseType getStorageType(final String dataSourceName) {
* @return not existed resource names
*/
public Collection<String> getNotExistedDataSources(final Collection<String> resourceNames) {
return resourceNames.stream().filter(each -> !storageUnitMetaData.getMetaDataMap().containsKey(each)).collect(Collectors.toSet());
return resourceNames.stream().filter(each -> !storageUnitMetaDataMap.containsKey(each)).collect(Collectors.toSet());
}
}

This file was deleted.

Loading