Skip to content

Commit

Permalink
Rename StorageUnitMetaData to StorageUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Oct 5, 2023
1 parent 9ee53f2 commit e03a43d
Show file tree
Hide file tree
Showing 97 changed files with 387 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginTyp
ShardingSphereDataSource dataSource = (ShardingSphereDataSource) entry.getValue();
String databaseName = AgentReflectionUtils.getFieldValue(dataSource, "databaseName");
ContextManager contextManager = AgentReflectionUtils.getFieldValue(dataSource, "contextManager");
result.addMetric(Arrays.asList(databaseName, "storage_unit_count"), contextManager.getStorageUnitMetaDataMap(databaseName).size());
result.addMetric(Arrays.asList(databaseName, "storage_unit_count"), contextManager.getStorageUnits(databaseName).size());
}
return Optional.of(result);
}
Expand Down
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().getStorageUnitMetaDataMap().size()).reduce(0, Integer::sum);
return metaDataContexts.getMetaData().getDatabases().values().stream().map(each -> each.getResourceMetaData().getStorageUnits().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.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
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().getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnitMetaData.class)));
when(database.getResourceMetaData().getStorageUnits()).thenReturn(Collections.singletonMap("ds_0", mock(StorageUnit.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().getStorageUnitMetaDataMap().isEmpty(), () -> new EmptyStorageUnitException(database.getName()));
ShardingSpherePreconditions.checkState(!database.getResourceMetaData().getStorageUnits().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.getStorageUnitMetaDataMap()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnitMetaDataMap().keySet());
if (null != resourceMetaData && null != resourceMetaData.getStorageUnits()) {
currentRuleNames.addAll(resourceMetaData.getStorageUnits().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 @@ -72,13 +72,13 @@ void before() {

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

@Test
void assertCheckSQLStatementWithDuplicateResource() {
when(resourceMetaData.getStorageUnitMetaDataMap()).thenReturn(Collections.singletonMap("write_ds", null));
when(resourceMetaData.getStorageUnits()).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.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
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().getStorageUnitMetaDataMap(), dataNode, row);
addTableRowsAndDataLength(database.getResourceMetaData().getStorageUnits(), dataNode, row);
tableData.getRows().add(new ShardingSphereRowData(row));
}
}
}

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

/**
Expand All @@ -184,7 +184,7 @@ public boolean isComplete() {
* @return contains data source or not
*/
public boolean containsDataSource() {
return !resourceMetaData.getStorageUnitMetaDataMap().isEmpty();
return !resourceMetaData.getStorageUnits().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.getStorageUnitMetaDataMap().entrySet().stream()
Map<String, DataSource> dataSources = resourceMetaData.getStorageUnits().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 @@ -26,7 +26,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.StorageNodeAggregator;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapUtils;

import javax.sql.DataSource;
Expand All @@ -45,33 +45,33 @@ public final class ResourceMetaData {

private final Map<StorageNodeName, DataSource> dataSources;

private final Map<String, StorageUnitMetaData> storageUnitMetaDataMap;
private final Map<String, StorageUnit> storageUnits;

public ResourceMetaData(final Map<String, DataSource> dataSources) {
this.dataSources = StorageNodeAggregator.aggregateDataSources(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<>();
storageUnits = 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));
storageUnits.put(entry.getKey(), new StorageUnit(null, entry.getValue(), dataSourcePoolPropertiesMap.get(entry.getKey()), dataSource));
}
}

public ResourceMetaData(final String databaseName, final Map<StorageNodeName, DataSource> dataSources,
final Map<String, StorageNode> storageNodes, final Map<String, DataSourcePoolProperties> propsMap) {
this.dataSources = dataSources;
storageUnitMetaDataMap = new LinkedHashMap<>();
storageUnits = 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));
storageUnits.put(entry.getKey(), new StorageUnit(databaseName, entry.getValue(), propsMap.get(entry.getKey()), dataSource));
}
}

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

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

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

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

/**
Expand All @@ -122,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 -> !storageUnitMetaDataMap.containsKey(each)).collect(Collectors.toSet());
return resourceNames.stream().filter(each -> !storageUnits.containsKey(each)).collect(Collectors.toSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
import java.util.Map;

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

private final StorageNode storageNode;

Expand All @@ -48,7 +48,7 @@ public final class StorageUnitMetaData {

private final ConnectionProperties connectionProperties;

public StorageUnitMetaData(final String databaseName, final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) {
public StorageUnit(final String databaseName, final StorageNode storageNode, final DataSourcePoolProperties dataSourcePoolProperties, final DataSource dataSource) {
this.storageNode = storageNode;
this.dataSource = new CatalogSwitchableDataSource(dataSource, storageNode.getCatalog(), storageNode.getUrl());
this.dataSourcePoolProperties = dataSourcePoolProperties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.config.props.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;

import javax.sql.DataSource;
Expand Down Expand Up @@ -50,11 +50,11 @@ public final class GenericSchemaBuilderMaterial {

private final String defaultSchemaName;

public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final Map<String, StorageUnitMetaData> storageUnitMetaDataMap,
public GenericSchemaBuilderMaterial(final DatabaseType protocolType, final Map<String, StorageUnit> storageUnits,
final Collection<ShardingSphereRule> rules, final ConfigurationProperties props, final String defaultSchemaName) {
this(protocolType, storageUnitMetaDataMap.entrySet().stream()
this(protocolType, storageUnits.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageType(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)),
storageUnitMetaDataMap.entrySet().stream()
storageUnits.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.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
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 (StorageUnitMetaData each : database.getResourceMetaData().getStorageUnitMetaDataMap().values()) {
for (StorageUnit each : database.getResourceMetaData().getStorageUnits().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().getStorageUnitMetaDataMap().values().stream().allMatch(each -> each.getStorageType().equals(database.getProtocolType()));
return !database.getResourceMetaData().getStorageUnits().values().stream().allMatch(each -> each.getStorageType().equals(database.getProtocolType()));
}

private static Collection<ShardingSphereRowData> getRows(final ShardingSphereTable table, final Collection<String> selectedColumnNames, final ResultSet resultSet) throws SQLException {
Expand Down
Loading

0 comments on commit e03a43d

Please sign in to comment.