From ecaa6d948f40635832fd621e335b7616a2277f58 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 27 Sep 2023 15:30:16 +0800 Subject: [PATCH] Rename StorageUnitNodeMapperUtils (#28599) --- .../DataSourceGeneratedDatabaseConfiguration.java | 4 ++-- .../StorageUnitNodeMapperUtils.java} | 14 ++++---------- .../manager/switcher/NewResourceSwitchManager.java | 10 +++++----- .../manager/switcher/ResourceSwitchManager.java | 12 ++++++------ 4 files changed, 17 insertions(+), 23 deletions(-) rename infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/{StorageResourceCreator.java => unit/StorageUnitNodeMapperUtils.java} (92%) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java index 6c481d63f620b..b877a421cb1a2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/database/impl/DataSourceGeneratedDatabaseConfiguration.java @@ -25,7 +25,7 @@ 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.StorageResource; -import org.apache.shardingsphere.infra.metadata.database.resource.StorageResourceCreator; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; @@ -52,7 +52,7 @@ public DataSourceGeneratedDatabaseConfiguration(final Map DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); - Map mappers = StorageResourceCreator.getStorageUnitNodeMappers(dataSourcePoolPropertiesMap); + Map mappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(dataSourcePoolPropertiesMap); storageResource = new StorageResource(getStorageNodeDataSourceMap(mappers), mappers); } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java similarity index 92% rename from infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java rename to infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java index 20c504edfc0bc..20587b2591481 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResourceCreator.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitNodeMapperUtils.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.metadata.database.resource; +package org.apache.shardingsphere.infra.metadata.database.resource.unit; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -26,19 +26,16 @@ 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.node.StorageNode; -import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; -import java.util.Collection; -import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; /** - * Storage resource creator. + * Storage unit node mapper utility class. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class StorageResourceCreator { +public final class StorageUnitNodeMapperUtils { /** * Get storage unit node mappers. @@ -90,15 +87,12 @@ private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNo */ public static Map getStorageNodeDataSourcePoolProperties(final Map storageUnitDataSourcePoolProps) { Map result = new LinkedHashMap<>(); - Collection storageNodes = new HashSet<>(); for (Entry entry : storageUnitDataSourcePoolProps.entrySet()) { Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); String url = standardProps.get("url").toString(); boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - if (storageNodes.add(storageNode)) { - result.put(storageNode, entry.getValue()); - } + result.putIfAbsent(storageNode, entry.getValue()); } return result; } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java index 5caa61cfe569f..b13736b6cc1fe 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/NewResourceSwitchManager.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; 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.resource.StorageResourceCreator; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; @@ -50,7 +50,7 @@ public final class NewResourceSwitchManager { public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(storageUnitDataSourcePoolProps); - Map toBeCreatedMappers = StorageResourceCreator.getStorageUnitNodeMappers(storageUnitDataSourcePoolProps); + Map toBeCreatedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(storageUnitDataSourcePoolProps); return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedMappers, storageUnitDataSourcePoolProps), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); } @@ -58,7 +58,7 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, final Map mappers, final Map storageUnitDataSourcePoolProps) { Collection storageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map newStorageNodes = new LinkedHashMap<>(storageNodes.size(), 1F); for (StorageNode each : storageNodes) { if (!resourceMetaData.getDataSourceMap().containsKey(each)) { @@ -78,14 +78,14 @@ private StorageResource getRegisterNewStorageResource(final ResourceMetaData res public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.putAll(propsMap); - Map toBeAlteredMappers = StorageResourceCreator.getStorageUnitNodeMappers(mergedDataSourcePoolPropertiesMap); + Map toBeAlteredMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(mergedDataSourcePoolPropertiesMap); return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredMappers, mergedDataSourcePoolPropertiesMap), getStaleStorageResource(resourceMetaData, toBeAlteredMappers), mergedDataSourcePoolPropertiesMap); } private StorageResource getAlterNewStorageResource(final Map mappers, final Map storageUnitDataSourcePoolProps) { Collection toBeAlteredStorageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodes.size(), 1F); for (StorageNode each : toBeAlteredStorageNodes) { storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java index 47abbbf68845b..864109645184c 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManager.java @@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties; 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.resource.StorageResourceCreator; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapperUtils; import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; @@ -51,7 +51,7 @@ public final class ResourceSwitchManager { public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(toBeChangedPropsMap); - Map toBeChangedMappers = StorageResourceCreator.getStorageUnitNodeMappers(toBeChangedPropsMap); + Map toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap); return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), getStaleDataSources(resourceMetaData, toBeChangedMappers, mergedPropsMap), mergedPropsMap); } @@ -66,7 +66,7 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M public SwitchingResource createByDropResource(final ResourceMetaData resourceMetaData, final Map toBeDeletedPropsMap) { Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); - Map toRemovedMappers = StorageResourceCreator.getStorageUnitNodeMappers(toBeDeletedPropsMap); + Map toRemovedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), getToBeRemovedStaleDataSources(resourceMetaData, toRemovedMappers), mergedDataSourcePoolPropertiesMap); } @@ -82,7 +82,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); - Map toBeChangedMappers = StorageResourceCreator.getStorageUnitNodeMappers(toBeChangedPropsMap); + Map toBeChangedMappers = StorageUnitNodeMapperUtils.getStorageUnitNodeMappers(toBeChangedPropsMap); StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap); Collection toBeChangedStorageNodes = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); staleStorageResource.getDataSourceMap().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes)); @@ -95,7 +95,7 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { Collection toBeChangedStorageNode = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); - Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); + Map storageNodeDataSourcePoolProps = StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageNode, storageNodeDataSourcePoolProps); Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedMappers); @@ -162,7 +162,7 @@ private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaD final Map storageUnitDataSourcePoolProps) { Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSourceMap().size(), 1F); Map storageUnitNodeMappers = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().size(), 1F); - storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); + storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageUnitNodeMapperUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers)); return new StorageResource(storageNodes, storageUnitNodeMappers); }