Skip to content

Commit

Permalink
Remove StorageUnitMetaData.storageUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 29, 2023
1 parent 1abeb4a commit f28ce10
Show file tree
Hide file tree
Showing 59 changed files with 240 additions and 222 deletions.
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().getStorageUnits().size()).reduce(0, Integer::sum);
return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().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.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
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().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.class)));
when(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", mock(NewStorageUnitMetaData.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().getStorageUnits().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().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().getStorageUnits()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaData().getStorageUnits().keySet());
if (null != resourceMetaData && null != resourceMetaData.getStorageUnitMetaData().getMetaDataMap()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaData().getMetaDataMap().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 @@ -74,13 +74,13 @@ void before() {

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

@Test
void assertCheckSQLStatementWithDuplicateResource() {
when(resourceMetaData.getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("write_ds", null));
when(resourceMetaData.getStorageUnitMetaData().getMetaDataMap()).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.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
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,18 +81,18 @@ private void collectForShardingStatisticTable(final ShardingSphereDatabase datab
row.add(each.getLogicTable());
row.add(dataNode.getDataSourceName());
row.add(dataNode.getTableName());
addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), dataNode, row);
addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap(), dataNode, row);
tableData.getRows().add(new ShardingSphereRowData(row));
}
}
}

private void addTableRowsAndDataLength(final Map<String, StorageUnit> storageUnits, final DataNode dataNode, final List<Object> row) throws SQLException {
DatabaseType databaseType = storageUnits.get(dataNode.getDataSourceName()).getStorageType();
private void addTableRowsAndDataLength(final Map<String, NewStorageUnitMetaData> 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;
if (dialectCollector.isPresent()) {
try (Connection connection = storageUnits.get(dataNode.getDataSourceName()).getDataSource().getConnection()) {
try (Connection connection = metaDataMap.get(dataNode.getDataSourceName()).getDataSource().getConnection()) {
isAppended = dialectCollector.get().appendRow(connection, dataNode, row);
}
}
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().getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
.ifPresent(optional -> optional.getStorageUnitMetaData().getMetaDataMap().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().getStorageUnits().isEmpty();
return !ruleMetaData.getRules().isEmpty() && !resourceMetaData.getStorageUnitMetaData().getMetaDataMap().isEmpty();
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeName;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNodeUtils;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
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.resource.unit.StorageUnitNodeMapUtils;

Expand Down Expand Up @@ -68,7 +68,7 @@ public ResourceMetaData(final String databaseName, final Map<StorageNodeName, Da
*/
public Collection<String> getAllInstanceDataSourceNames() {
Collection<String> result = new LinkedList<>();
for (Entry<String, StorageUnit> entry : storageUnitMetaData.getStorageUnits().entrySet()) {
for (Entry<String, NewStorageUnitMetaData> entry : storageUnitMetaData.getMetaDataMap().entrySet()) {
if (!isExisted(entry.getKey(), result)) {
result.add(entry.getKey());
}
Expand All @@ -77,8 +77,8 @@ public Collection<String> getAllInstanceDataSourceNames() {
}

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

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

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

/**
Expand All @@ -108,6 +108,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.getStorageUnits().containsKey(each)).collect(Collectors.toSet());
return resourceNames.stream().filter(each -> !storageUnitMetaData.getMetaDataMap().containsKey(each)).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,13 @@
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

/**
* Storage unit meta data.
*/
@Getter
public final class StorageUnitMetaData {

private final Map<String, StorageUnit> storageUnits;

private final Map<String, NewStorageUnitMetaData> metaDataMap;

public StorageUnitMetaData(final String databaseName, final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap,
Expand All @@ -44,8 +41,5 @@ public StorageUnitMetaData(final String databaseName, final Map<String, StorageN
for (Entry<String, StorageNode> entry : storageNodes.entrySet()) {
metaDataMap.put(entry.getKey(), new NewStorageUnitMetaData(databaseName, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSources.get(entry.getValue().getName())));
}
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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ 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)),
this(protocolType, storageUnitMetaData.getMetaDataMap().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageUnit().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 @@ -20,7 +20,7 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.NewStorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
Expand Down Expand Up @@ -57,7 +57,7 @@ public static Collection<ShardingSphereRowData> collectRowData(final ShardingSph
return Collections.emptyList();
}
Collection<ShardingSphereRowData> result = new LinkedList<>();
for (StorageUnit each : database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values()) {
for (NewStorageUnitMetaData each : database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values()) {
try (
Connection connection = each.getDataSource().getConnection();
Statement statement = connection.createStatement();
Expand All @@ -69,7 +69,7 @@ public static Collection<ShardingSphereRowData> collectRowData(final ShardingSph
}

private static boolean isDifferentProtocolAndStorageType(final ShardingSphereDatabase database) {
return !database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().values().stream().allMatch(each -> each.getStorageType().equals(database.getProtocolType()));
return !database.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap().values().stream().allMatch(each -> each.getStorageUnit().getStorageType().equals(database.getProtocolType()));
}

private static Collection<ShardingSphereRowData> getRows(final ShardingSphereTable table, final Collection<String> selectedColumnNames, final ResultSet resultSet) throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
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.unit.StorageUnit;
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.rule.RuleMetaData;
import org.apache.shardingsphere.infra.rule.identifier.type.ResourceHeldRule;
import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
Expand Down Expand Up @@ -88,9 +90,8 @@ private ShardingSphereDatabase mockDatabase(final ResourceMetaData resourceMetaD
ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
when(result.getName()).thenReturn("foo_db");
when(result.getResourceMetaData()).thenReturn(resourceMetaData);
StorageUnit storageUnit = mock(StorageUnit.class);
when(storageUnit.getDataSource()).thenReturn(dataSource);
when(result.getResourceMetaData().getStorageUnitMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("foo_db", storageUnit));
NewStorageUnitMetaData storageUnitMetaData = new NewStorageUnitMetaData("foo_db", mock(StorageNode.class, RETURNS_DEEP_STUBS), mock(DataSourcePoolProperties.class), dataSource);
when(result.getResourceMetaData().getStorageUnitMetaData().getMetaDataMap()).thenReturn(Collections.singletonMap("foo_db", storageUnitMetaData));
when(result.getRuleMetaData()).thenReturn(new RuleMetaData(Collections.singleton(databaseResourceHeldRule)));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected List<ExecutionGroup<T>> group(final String dataSourceName, final int c
@SuppressWarnings("unchecked")
private ExecutionGroup<T> createExecutionGroup(final String dataSourceName, final List<SQLUnit> sqlUnits, final C connection, final ConnectionMode connectionMode) throws SQLException {
List<T> inputs = new LinkedList<>();
DatabaseType databaseType = storageUnitMetaData.getStorageUnits().get(dataSourceName).getStorageType();
DatabaseType databaseType = storageUnitMetaData.getMetaDataMap().get(dataSourceName).getStorageUnit().getStorageType();
for (SQLUnit each : sqlUnits) {
inputs.add((T) sqlExecutionUnitBuilder.build(new ExecutionUnit(dataSourceName, each), statementManager, connection, connectionMode, option, databaseType));
}
Expand Down
Loading

0 comments on commit f28ce10

Please sign in to comment.