diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java index 0dfa7ce06c002..34ce3e90ef46e 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/unit/StorageUnitMetaData.java @@ -35,7 +35,7 @@ public final class StorageUnitMetaData { // TODO zhangliang: should refactor - private final Map storageUnitNodeMap; + private final Map storageNodes; private final Map storageUnits; @@ -43,13 +43,13 @@ public final class StorageUnitMetaData { private final Map dataSources; public StorageUnitMetaData(final String databaseName, final Map storageNodeDataSources, - final Map dataSourcePoolPropertiesMap, final Map storageUnitNodeMap) { - this.storageUnitNodeMap = storageUnitNodeMap; - storageUnits = new LinkedHashMap<>(this.storageUnitNodeMap.size(), 1F); - for (Entry entry : this.storageUnitNodeMap.entrySet()) { - storageUnits.put(entry.getKey(), new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue())); - } - dataSources = createDataSources(); + final Map dataSourcePoolPropertiesMap, final Map storageNodes) { + this.storageNodes = storageNodes; + storageUnits = storageNodes.entrySet().stream().collect( + Collectors.toMap(Entry::getKey, entry -> new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()), + (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(this.storageNodes.size(), 1F))); + dataSources = storageUnits.entrySet().stream().collect( + Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> currentValue, () -> new LinkedHashMap<>(storageUnits.size(), 1F))); } /** @@ -61,12 +61,4 @@ public Map getDataSourcePoolPropertiesMap() { return storageUnits.entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)); } - - private Map createDataSources() { - Map result = new LinkedHashMap<>(storageUnits.size(), 1F); - for (Entry entry : storageUnits.entrySet()) { - result.put(entry.getKey(), entry.getValue().getDataSource()); - } - return result; - } } 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 1de6e2f1164c6..2528f26dfc0ce 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 @@ -365,7 +365,7 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { Map storageNodeDataSources = currentResourceMetaData.getDataSources(); - Map storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(); + Map storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageNodes(); if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) { storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSources()); } 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 7c5fdeba9cdc0..c39f88e79667d 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 @@ -120,8 +120,8 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe } private StorageResource getToBeRemovedStaleStorageResource(final ResourceMetaData resourceMetaData, final String storageUnitName) { - StorageNode storageNode = resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap().remove(storageUnitName); - Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(); + StorageNode storageNode = resourceMetaData.getStorageUnitMetaData().getStorageNodes().remove(storageUnitName); + Map reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageNodes(); Map storageNodes = new LinkedHashMap<>(1, 1F); if (reservedStorageUintNodeMap.values().stream().noneMatch(each -> each.equals(storageNode))) { storageNodes.put(storageNode.getName(), resourceMetaData.getDataSources().get(storageNode.getName())); 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 a4006d5d0122f..8a0c7da949d34 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 @@ -112,10 +112,10 @@ private Map getNewStorageNodes(final ResourceMetaDa } private Map getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap()); + Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageNodes()); result.keySet().removeAll(getToBeDeletedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet()).keySet()); result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); - result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap(), toBeChangedStorageUnitNodeMap)); + result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageNodes(), toBeChangedStorageUnitNodeMap)); return result; } @@ -164,7 +164,7 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap, final Map storageUnitDataSourcePoolProps) { Map storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSources().size(), 1F); - Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap().size(), 1F); + Map storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageNodes().size(), 1F); storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSources(), StorageUnitNodeMapUtils.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps))); storageUnitNodeMap.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageUnitNodeMap)); return new StorageResource(storageNodes, storageUnitNodeMap); diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java index 2b099cd8d381c..1656a47c299e4 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/ContextManagerTest.java @@ -261,7 +261,7 @@ private ResourceMetaData createOriginalResource() { storageUnits.put(entry.getKey(), new StorageUnit("foo_db", storageNodeDataSourceMap, mock(DataSourcePoolProperties.class), entry.getValue())); } when(result.getStorageUnitMetaData().getStorageUnits()).thenReturn(storageUnits); - when(result.getStorageUnitMetaData().getStorageUnitNodeMap()).thenReturn(storageUnitNodeMap); + when(result.getStorageUnitMetaData().getStorageNodes()).thenReturn(storageUnitNodeMap); when(result.getDataSources()).thenReturn(storageNodeDataSourceMap); return result; }