From 95c83940e4186b82584ea680165e6d76996e65e6 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 26 Sep 2023 18:51:33 +0800 Subject: [PATCH] Add StorageResourceCreator.getStorageNodeDataSourcePoolProperties() (#28589) --- .../database/resource/StorageResource.java | 10 ------- .../resource/StorageResourceCreator.java | 27 ++++++++++++++++--- .../switcher/NewResourceSwitchManager.java | 23 +++++++++------- .../switcher/ResourceSwitchManager.java | 20 ++++++++------ 4 files changed, 49 insertions(+), 31 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java index 593f9f80e570e..b46f82d9799d2 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/StorageResource.java @@ -19,12 +19,10 @@ import lombok.Getter; import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource; -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 javax.sql.DataSource; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -41,18 +39,10 @@ public final class StorageResource { private final Map wrappedDataSources; - private final Map dataSourcePoolPropertiesMap; - public StorageResource(final Map dataSourceMap, final Map storageUnitNodeMappers) { - this(dataSourceMap, storageUnitNodeMappers, Collections.emptyMap()); - } - - public StorageResource(final Map dataSourceMap, - final Map storageUnitNodeMappers, final Map dataSourcePoolPropertiesMap) { this.dataSourceMap = dataSourceMap; this.storageUnitNodeMappers = storageUnitNodeMappers; wrappedDataSources = createWrappedDataSources(); - this.dataSourcePoolPropertiesMap = dataSourcePoolPropertiesMap; } private Map createWrappedDataSources() { 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/StorageResourceCreator.java index 8f1bf5c93a071..fd8c98d429d81 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/StorageResourceCreator.java @@ -30,6 +30,8 @@ import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; +import java.util.Collection; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; @@ -91,7 +93,6 @@ private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode public static StorageResource createStorageResourceWithoutDataSource(final Map propsMap) { Map storageNodes = new LinkedHashMap<>(); Map mappers = new LinkedHashMap<>(); - Map newPropsMap = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { String storageUnitName = entry.getKey(); Map standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties(); @@ -100,10 +101,30 @@ public static StorageResource createStorageResourceWithoutDataSource(final 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.getName(), 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 96e8310f3a4e2..670f6af2caa86 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 @@ -42,22 +42,24 @@ public final class NewResourceSwitchManager { * Register storage unit. * * @param resourceMetaData resource meta data - * @param propsMap data source pool properties map + * @param storageUnitDataSourcePoolProps storage unit grouped data source pool properties map * @return created switching resource */ - public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map propsMap) { + public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); - mergedPropsMap.putAll(propsMap); - StorageResource toBeCreatedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(propsMap); - return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageResource), + mergedPropsMap.putAll(storageUnitDataSourcePoolProps); + StorageResource toBeCreatedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(storageUnitDataSourcePoolProps); + return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageResource, storageUnitDataSourcePoolProps), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); } - private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, final StorageResource toBeCreatedStorageResource) { + private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, + final StorageResource toBeCreatedStorageResource, final Map storageUnitDataSourcePoolProps) { + Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = new LinkedHashMap<>(toBeCreatedStorageResource.getDataSourceMap().size(), 1F); for (StorageNode each : toBeCreatedStorageResource.getDataSourceMap().keySet()) { if (!resourceMetaData.getDataSourceMap().containsKey(each)) { - storageNodes.put(each, DataSourcePoolCreator.create(toBeCreatedStorageResource.getDataSourcePoolPropertiesMap().get(each.getName()))); + storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each.getName()))); } } return new StorageResource(storageNodes, toBeCreatedStorageResource.getStorageUnitNodeMappers()); @@ -74,14 +76,15 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.putAll(propsMap); StorageResource toBeAlteredStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(mergedDataSourcePoolPropertiesMap); - return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageResource), + return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageResource, mergedDataSourcePoolPropertiesMap), getStaleStorageResource(resourceMetaData, toBeAlteredStorageResource), mergedDataSourcePoolPropertiesMap); } - private StorageResource getAlterNewStorageResource(final StorageResource toBeAlteredStorageResource) { + private StorageResource getAlterNewStorageResource(final StorageResource toBeAlteredStorageResource, final Map storageUnitDataSourcePoolProps) { + Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageResource.getDataSourceMap().size(), 1F); for (StorageNode each : toBeAlteredStorageResource.getDataSourceMap().keySet()) { - storageNodes.put(each, DataSourcePoolCreator.create(toBeAlteredStorageResource.getDataSourcePoolPropertiesMap().get(each.getName()))); + storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each.getName()))); } return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers()); } 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 65e5fbdf8a77a..fc7310135292b 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 @@ -52,8 +52,8 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(toBeChangedPropsMap); StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap); - return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource), - getStaleDataSources(resourceMetaData, toBeChangedStorageResource), mergedPropsMap); + return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap), + getStaleDataSources(resourceMetaData, toBeChangedStorageResource, mergedPropsMap), mergedPropsMap); } /** @@ -83,17 +83,20 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap); - StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource); + StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap); staleStorageResource.getDataSourceMap() .putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageResource.getDataSourceMap().keySet())); staleStorageResource.getStorageUnitNodeMappers().putAll( getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers().keySet())); - return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource), staleStorageResource, mergedDataSourcePoolPropertiesMap); + return new SwitchingResource(resourceMetaData, + createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); } - private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource) { + private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, + final StorageResource toBeChangedStorageResource, final Map storageUnitDataSourcePoolProps) { + Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = - getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getDataSourceMap(), toBeChangedStorageResource.getDataSourcePoolPropertiesMap()); + getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getDataSourceMap(), storageNodeDataSourcePoolProps); Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedStorageResource.getStorageUnitNodeMappers()); return new StorageResource(storageNodes, storageUnitNodeMappers); } @@ -153,10 +156,11 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re return new StorageResource(staleStorageNodes, staleStorageUnitNodeMappers); } - private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource) { + private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource, + 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(), toBeChangedStorageResource.getDataSourcePoolPropertiesMap())); + storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers())); return new StorageResource(storageNodes, storageUnitNodeMappers); }