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..5dd9d6e24683c 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 @@ -53,18 +53,36 @@ public static StorageResource createStorageResource(final Map mappers = new LinkedHashMap<>(); for (Entry entry : propsMap.entrySet()) { String storageUnitName = entry.getKey(); - 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(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - if (!storageNodes.containsKey(storageNode)) { - storageNodes.put(storageNode, DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values())); - } - mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable)); + StorageUnitNodeMapper mapper = getStorageUnitNodeMapper(storageUnitName, entry.getValue()); + mappers.put(storageUnitName, mapper); + storageNodes.computeIfAbsent(mapper.getStorageNode(), key -> DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values())); } return new StorageResource(storageNodes, mappers); } + /** + * Get storage unit node mappers. + * + * @param propsMap data source pool properties map + * @return storage unit node mappers + */ + public static Map getStorageUnitNodeMappers(final Map propsMap) { + Map result = new LinkedHashMap<>(); + for (Entry entry : propsMap.entrySet()) { + String storageUnitName = entry.getKey(); + result.put(storageUnitName, getStorageUnitNodeMapper(storageUnitName, entry.getValue())); + } + return result; + } + + private static StorageUnitNodeMapper getStorageUnitNodeMapper(final String storageUnitName, final DataSourcePoolProperties props) { + Map standardProps = props.getConnectionPropertySynonyms().getStandardProperties(); + String url = standardProps.get("url").toString(); + boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable(); + StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); + return createStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable); + } + private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) { try { JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url); @@ -78,35 +96,12 @@ private static String generateStorageNodeName(final String hostname, final int p return String.format("%s_%s_%s", hostname, port, username); } - private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) { + private static StorageUnitNodeMapper createStorageUnitNodeMapper(final StorageNode storageNode, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) { return isInstanceConnectionAvailable ? new StorageUnitNodeMapper(storageUnitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url) : new StorageUnitNodeMapper(storageUnitName, storageNode, url); } - /** - * Create storage resource without data source. - * - * @param propsMap data source pool properties map - * @return created storage resource - */ - public static StorageResource createStorageResourceWithoutDataSource(final Map propsMap) { - Map storageNodes = new LinkedHashMap<>(); - Map mappers = new LinkedHashMap<>(); - for (Entry entry : propsMap.entrySet()) { - String storageUnitName = entry.getKey(); - 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(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable)); - if (!storageNodes.containsKey(storageNode)) { - storageNodes.put(storageNode, null); - } - mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable)); - } - return new StorageResource(storageNodes, mappers); - } - /** * Get storage node grouped data source pool properties map. * diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java index 6bf91cfe33748..02e22e03c09a3 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/context/ConfigurationContextManager.java @@ -276,11 +276,10 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD public 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) { 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 709c63526a19f..5caa61cfe569f 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 @@ -26,11 +26,13 @@ import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper; import javax.sql.DataSource; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.stream.Collectors; /** * TODO Rename ResourceSwitchManager when metadata structure adjustment completed. #25485 @@ -48,21 +50,22 @@ public final class NewResourceSwitchManager { public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map storageUnitDataSourcePoolProps) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(storageUnitDataSourcePoolProps); - StorageResource toBeCreatedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(storageUnitDataSourcePoolProps); - return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageResource, storageUnitDataSourcePoolProps), + Map toBeCreatedMappers = StorageResourceCreator.getStorageUnitNodeMappers(storageUnitDataSourcePoolProps); + return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedMappers, storageUnitDataSourcePoolProps), new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap); } private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, - final StorageResource toBeCreatedStorageResource, final Map storageUnitDataSourcePoolProps) { + final Map mappers, final Map storageUnitDataSourcePoolProps) { + Collection storageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); - Map storageNodes = new LinkedHashMap<>(toBeCreatedStorageResource.getDataSourceMap().size(), 1F); - for (StorageNode each : toBeCreatedStorageResource.getDataSourceMap().keySet()) { + Map newStorageNodes = new LinkedHashMap<>(storageNodes.size(), 1F); + for (StorageNode each : storageNodes) { if (!resourceMetaData.getDataSourceMap().containsKey(each)) { - storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); + newStorageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); } } - return new StorageResource(storageNodes, toBeCreatedStorageResource.getStorageUnitNodeMappers()); + return new StorageResource(newStorageNodes, mappers); } /** @@ -75,28 +78,30 @@ 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); - StorageResource toBeAlteredStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(mergedDataSourcePoolPropertiesMap); - return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageResource, mergedDataSourcePoolPropertiesMap), - getStaleStorageResource(resourceMetaData, toBeAlteredStorageResource), mergedDataSourcePoolPropertiesMap); + Map toBeAlteredMappers = StorageResourceCreator.getStorageUnitNodeMappers(mergedDataSourcePoolPropertiesMap); + return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredMappers, mergedDataSourcePoolPropertiesMap), + getStaleStorageResource(resourceMetaData, toBeAlteredMappers), mergedDataSourcePoolPropertiesMap); } - private StorageResource getAlterNewStorageResource(final StorageResource toBeAlteredStorageResource, final Map storageUnitDataSourcePoolProps) { + 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 storageNodes = new LinkedHashMap<>(toBeAlteredStorageResource.getDataSourceMap().size(), 1F); - for (StorageNode each : toBeAlteredStorageResource.getDataSourceMap().keySet()) { + Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodes.size(), 1F); + for (StorageNode each : toBeAlteredStorageNodes) { storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each))); } - return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers()); + return new StorageResource(storageNodes, mappers); } - private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final StorageResource toBeAlteredStorageResource) { - Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageResource.getDataSourceMap().size(), 1F); + private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final Map mappers) { + Collection toBeAlteredStorageNodes = mappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); + Map storageNodes = new LinkedHashMap<>(toBeAlteredStorageNodes.size(), 1F); for (Entry entry : resourceMetaData.getDataSourceMap().entrySet()) { - if (toBeAlteredStorageResource.getDataSourceMap().containsKey(entry.getKey())) { + if (toBeAlteredStorageNodes.contains(entry.getKey())) { storageNodes.put(entry.getKey(), entry.getValue()); } } - return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers()); + return new StorageResource(storageNodes, mappers); } /** 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 14bd869122293..47abbbf68845b 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 @@ -51,9 +51,9 @@ public final class ResourceSwitchManager { public SwitchingResource create(final ResourceMetaData resourceMetaData, final Map toBeChangedPropsMap) { Map mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedPropsMap.putAll(toBeChangedPropsMap); - StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap); - return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap), - getStaleDataSources(resourceMetaData, toBeChangedStorageResource, mergedPropsMap), mergedPropsMap); + Map toBeChangedMappers = StorageResourceCreator.getStorageUnitNodeMappers(toBeChangedPropsMap); + return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), + getStaleDataSources(resourceMetaData, toBeChangedMappers, mergedPropsMap), mergedPropsMap); } /** @@ -66,9 +66,9 @@ 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); - StorageResource toToBeRemovedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeDeletedPropsMap); + Map toRemovedMappers = StorageResourceCreator.getStorageUnitNodeMappers(toBeDeletedPropsMap); return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyMap()), - getToBeRemovedStaleDataSources(resourceMetaData, toToBeRemovedStorageResource), mergedDataSourcePoolPropertiesMap); + getToBeRemovedStaleDataSources(resourceMetaData, toRemovedMappers), mergedDataSourcePoolPropertiesMap); } /** @@ -82,31 +82,32 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet Map mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap()); mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each)); mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap); - StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap); - StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap); - staleStorageResource.getDataSourceMap() - .putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageResource.getDataSourceMap().keySet())); + Map toBeChangedMappers = StorageResourceCreator.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)); staleStorageResource.getStorageUnitNodeMappers().putAll( - getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers().keySet())); + getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers.keySet())); return new SwitchingResource(resourceMetaData, - createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); + createNewStorageResource(resourceMetaData, toBeChangedMappers, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap); } private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, - final StorageResource toBeChangedStorageResource, final Map storageUnitDataSourcePoolProps) { + final Map toBeChangedMappers, final Map storageUnitDataSourcePoolProps) { + Collection toBeChangedStorageNode = toBeChangedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); Map storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps); Map storageNodes = - getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getDataSourceMap(), storageNodeDataSourcePoolProps); - Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedStorageResource.getStorageUnitNodeMappers()); + getNewStorageNodes(resourceMetaData, toBeChangedStorageNode, storageNodeDataSourcePoolProps); + Map storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedMappers); return new StorageResource(storageNodes, storageUnitNodeMappers); } - private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageNodes, + private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection toBeChangedStorageNode, final Map propsMap) { Map result = new LinkedHashMap<>(resourceMetaData.getDataSourceMap()); - result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes.keySet()).keySet()); - result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes, propsMap)); - result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNodes, propsMap)); + result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode).keySet()); + result.putAll(getChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode, propsMap)); + result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageNode, propsMap)); return result; } @@ -119,10 +120,10 @@ private Map getNewStorageUnitNodeMappers(final Re } private Map getChangedDataSources(final Map storageNodes, - final Map toBeChangedStorageNodes, final Map propsMap) { - Collection toBeChangedDataSourceNames = toBeChangedStorageNodes.keySet().stream() + final Collection toBeChangedStorageNode, final Map propsMap) { + Collection toBeChangedDataSourceNames = toBeChangedStorageNode.stream() .filter(each -> isModifiedDataSource(storageNodes, each, propsMap.get(each))).collect(Collectors.toList()); - Map result = new LinkedHashMap<>(toBeChangedStorageNodes.size(), 1F); + Map result = new LinkedHashMap<>(toBeChangedStorageNode.size(), 1F); for (StorageNode each : toBeChangedDataSourceNames) { result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); } @@ -133,9 +134,9 @@ private boolean isModifiedDataSource(final Map original return originalDataSources.containsKey(storageNode) && !propsMap.equals(DataSourcePoolPropertiesCreator.create(originalDataSources.get(storageNode))); } - private Map getToBeAddedDataSources(final Map storageNodes, final Map toBeChangedStorageNodes, + private Map getToBeAddedDataSources(final Map storageNodes, final Collection toBeChangedStorageNode, final Map propsMap) { - Collection toBeAddedDataSourceNames = toBeChangedStorageNodes.keySet().stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); + Collection toBeAddedDataSourceNames = toBeChangedStorageNode.stream().filter(each -> !storageNodes.containsKey(each)).collect(Collectors.toList()); Map result = new LinkedHashMap<>(); for (StorageNode each : toBeAddedDataSourceNames) { result.put(each, DataSourcePoolCreator.create(propsMap.get(each))); @@ -143,25 +144,26 @@ private Map getToBeAddedDataSources(final Map toRemovedMappers) { Map reservedStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() - .filter(entry -> !toBeRemovedStorageResource.getStorageUnitNodeMappers().containsKey(entry.getKey())) + .filter(entry -> !toRemovedMappers.containsKey(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getUnitNodeMapper())); + Collection toBeRemovedStorageNodes = toRemovedMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); Collection inUsedDataSourceNames = reservedStorageUnitNodeMappers.values().stream().map(StorageUnitNodeMapper::getStorageNode).collect(Collectors.toSet()); Map staleStorageNodes = resourceMetaData.getDataSourceMap().entrySet().stream() - .filter(entry -> toBeRemovedStorageResource.getDataSourceMap().containsKey(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) + .filter(entry -> toBeRemovedStorageNodes.contains(entry.getKey()) && !inUsedDataSourceNames.contains(entry.getKey())) .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); Map staleStorageUnitNodeMappers = resourceMetaData.getStorageUnitMetaData().getStorageUnits().entrySet().stream() .filter(entry -> !reservedStorageUnitNodeMappers.containsKey(entry.getKey())).collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getUnitNodeMapper())); return new StorageResource(staleStorageNodes, staleStorageUnitNodeMappers); } - private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource, + private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedMappers, 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))); - storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers())); + storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedMappers)); return new StorageResource(storageNodes, storageUnitNodeMappers); }