From b547f07668d7d4aa4561c37e7c7311bd3c55be41 Mon Sep 17 00:00:00 2001 From: zhangliang Date: Tue, 26 Sep 2023 23:31:20 +0800 Subject: [PATCH] Decouple ResourceMetaData and StorageResource --- .../database/ShardingSphereDatabase.java | 2 +- .../database/resource/ResourceMetaData.java | 8 ++-- .../resource/StorageResourceCreator.java | 38 +++++++++---------- .../context/ConfigurationContextManager.java | 5 +-- 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java index 3c83c2bd20618..d7571a1280d41 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/ShardingSphereDatabase.java @@ -124,7 +124,7 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp } private static ResourceMetaData createResourceMetaData(final String databaseName, final StorageResource storageResource, final Map propsMap) { - return new ResourceMetaData(databaseName, storageResource, propsMap); + return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMappers(), propsMap); } /** diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java index 9c4bd9b3e8560..c92a0f6aa0995 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java @@ -26,6 +26,7 @@ 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.StorageUnitMetaData; +import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; import java.util.Collection; @@ -56,9 +57,10 @@ public ResourceMetaData(final String databaseName, final Map StorageResourceUtils.getStorageUnitNodeMappers(dataSources)); } - public ResourceMetaData(final String databaseName, final StorageResource storageResource, final Map propsMap) { - dataSourceMap = storageResource.getDataSourceMap(); - storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageResource.getStorageUnitNodeMappers()); + public ResourceMetaData(final String databaseName, final Map dataSourceMap, + final Map storageUnitNodeMappers, final Map propsMap) { + this.dataSourceMap = dataSourceMap; + storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMappers); } /** 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 70ed45e9df4ad..6dd1b51616cac 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 @@ -65,25 +65,6 @@ public static StorageResource createStorageResource(final Map renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) { Map newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSourceMap(), resource); Map newStorageUnitNodeMappers = getNewStorageUnitNodeMappers(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource); - StorageResource newStorageResource = new StorageResource(newStorageNodes, newStorageUnitNodeMappers); Map propsMap = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new)); - return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase( - database.getName(), database.getProtocolType(), new ResourceMetaData(database.getName(), newStorageResource, propsMap), database.getRuleMetaData(), database.getSchemas())); + return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(), + new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMappers, propsMap), database.getRuleMetaData(), database.getSchemas())); } private Map getNewStorageNodes(final Map currentStorageNodes, final SwitchingResource resource) {