From 90c26d7d06c2ba0930c31cedbd5ce531e272378e Mon Sep 17 00:00:00 2001 From: zhangliang Date: Sat, 7 Oct 2023 18:59:24 +0800 Subject: [PATCH] Refactor SwitchingResource --- .../context/ConfigurationContextManager.java | 4 +-- .../switcher/NewResourceSwitchManager.java | 24 +++++++------- .../switcher/ResourceSwitchManager.java | 32 +++++-------------- .../manager/switcher/SwitchingResource.java | 3 +- .../switcher/ResourceSwitchManagerTest.java | 10 +++--- .../switcher/SwitchingResourceTest.java | 4 +-- 6 files changed, 29 insertions(+), 48 deletions(-) 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 41a9592498003..e1b4b029f51f5 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,8 +365,8 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re private Map getMergedStorageNodeDataSources(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) { Map result = currentResourceMetaData.getDataSources(); - if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) { - result.putAll(switchingResource.getNewStorageResource().getDataSources()); + if (null != switchingResource && null != switchingResource.getNewDataSources() && !switchingResource.getNewDataSources().isEmpty()) { + result.putAll(switchingResource.getNewDataSources()); } 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 cd07f09e0a7d1..392211bf4aa29 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 @@ -53,18 +53,18 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta Map toBeCreatedStorageUintNodeMap = StorageUnitNodeMapCreator.create(storageUnitDataSourcePoolPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(storageUnitDataSourcePoolPropsMap); return new SwitchingResource(resourceMetaData, - getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageUintNodeMap, dataSourcePoolPropsMap), Collections.emptyMap(), Collections.emptyList(), mergedPropsMap); + getRegisterNewDataSources(resourceMetaData, toBeCreatedStorageUintNodeMap, dataSourcePoolPropsMap), Collections.emptyMap(), Collections.emptyList(), mergedPropsMap); } - private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, - final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { - Map newStorageNodes = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); + private Map getRegisterNewDataSources(final ResourceMetaData resourceMetaData, + final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { + Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); for (StorageNode each : storageUintNodeMap.values()) { if (!resourceMetaData.getDataSources().containsKey(each)) { - newStorageNodes.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); + result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); } } - return new StorageResource(newStorageNodes, new LinkedHashSet<>(storageUintNodeMap.keySet())); + return result; } /** @@ -81,16 +81,16 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat Map toBeAlteredStorageUintNodeMap = StorageUnitNodeMapCreator.create(mergedDataSourcePoolPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(mergedDataSourcePoolPropsMap); StorageResource staleStorageResource = getStaleStorageResource(resourceMetaData, toBeAlteredStorageUintNodeMap); - return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageUintNodeMap, dataSourcePoolPropsMap), + return new SwitchingResource(resourceMetaData, getAlterNewDataSources(toBeAlteredStorageUintNodeMap, dataSourcePoolPropsMap), staleStorageResource.getDataSources(), staleStorageResource.getStorageUnitNames(), mergedDataSourcePoolPropsMap); } - private StorageResource getAlterNewStorageResource(final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { - Map storageNodes = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); + private Map getAlterNewDataSources(final Map storageUintNodeMap, final Map dataSourcePoolPropsMap) { + Map result = new LinkedHashMap<>(storageUintNodeMap.size(), 1F); for (StorageNode each : storageUintNodeMap.values()) { - storageNodes.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); + result.put(each, DataSourcePoolCreator.create(dataSourcePoolPropsMap.get(each))); } - return new StorageResource(storageNodes, new LinkedHashSet<>(storageUintNodeMap.keySet())); + return result; } private StorageResource getStaleStorageResource(final ResourceMetaData resourceMetaData, final Map storageUintNodeMap) { @@ -116,7 +116,7 @@ public SwitchingResource unregisterStorageUnit(final ResourceMetaData resourceMe mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> each.equals(storageUnitName)); resourceMetaData.getStorageUnits().remove(storageUnitName); StorageResource toBeRemovedStaleStorageResource = getToBeRemovedStaleStorageResource(resourceMetaData, storageUnitName); - return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyList()), + return new SwitchingResource(resourceMetaData, Collections.emptyMap(), toBeRemovedStaleStorageResource.getDataSources(), toBeRemovedStaleStorageResource.getStorageUnitNames(), mergedDataSourcePoolPropertiesMap); } 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 1f2800b52bffa..dc4c622a3d169 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 @@ -54,9 +54,9 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M mergedPropsMap.putAll(toBeChangedPropsMap); Map toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(toBeChangedPropsMap); - StorageResource newStorageResource = createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap); StorageResource staleDataSources = getStaleDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, mergedPropsMap); - return new SwitchingResource(resourceMetaData, newStorageResource, staleDataSources.getDataSources(), staleDataSources.getStorageUnitNames(), mergedPropsMap); + return new SwitchingResource(resourceMetaData, createNewDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap), + staleDataSources.getDataSources(), staleDataSources.getStorageUnitNames(), mergedPropsMap); } /** @@ -72,8 +72,8 @@ public SwitchingResource createByDropResource(final ResourceMetaData resourceMet mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey); Map toRemovedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeDeletedPropsMap); StorageResource toBeRemovedStaleStorageResource = getToBeRemovedStaleStorageResource(resourceMetaData, toRemovedStorageUnitNodeMap); - return new SwitchingResource(resourceMetaData, new StorageResource(Collections.emptyMap(), Collections.emptyList()), - toBeRemovedStaleStorageResource.getDataSources(), toBeRemovedStaleStorageResource.getStorageUnitNames(), mergedDataSourcePoolPropertiesMap); + return new SwitchingResource( + resourceMetaData, Collections.emptyMap(), toBeRemovedStaleStorageResource.getDataSources(), toBeRemovedStaleStorageResource.getStorageUnitNames(), mergedDataSourcePoolPropertiesMap); } /** @@ -93,36 +93,20 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet staleStorageResource.getDataSources().putAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageUnitNodeMap.values())); staleStorageResource.getStorageUnitNames().addAll(getToBeDeletedStorageUnitNames(resourceMetaData.getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet())); Map dataSourcePoolPropsMap = StorageNodeAggregator.aggregateDataSourcePoolProperties(toBeChangedPropsMap); - return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap), + return new SwitchingResource(resourceMetaData, createNewDataSources(resourceMetaData, toBeChangedStorageUnitNodeMap, dataSourcePoolPropsMap), staleStorageResource.getDataSources(), staleStorageResource.getStorageUnitNames(), mergedDataSourcePoolPropertiesMap); } - private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, - final Map toBeChangedStorageUnitNodeMap, final Map dataSourcePoolPropsMap) { - Map storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageUnitNodeMap.values(), dataSourcePoolPropsMap); - Map storageUnitNodeMap = getNewStorageUnitNodeMap(resourceMetaData, toBeChangedStorageUnitNodeMap); - return new StorageResource(storageNodes, new LinkedHashSet<>(storageUnitNodeMap.keySet())); - } - - private Map getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection toBeChangedStorageNodes, - final Map propsMap) { + private Map createNewDataSources(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap, + final Map propsMap) { Map result = new LinkedHashMap<>(resourceMetaData.getDataSources()); + Collection toBeChangedStorageNodes = toBeChangedStorageUnitNodeMap.values(); result.keySet().removeAll(getToBeDeletedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes).keySet()); result.putAll(getChangedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes, propsMap)); result.putAll(getToBeAddedDataSources(resourceMetaData.getDataSources(), toBeChangedStorageNodes, propsMap)); return result; } - private Map getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map toBeChangedStorageUnitNodeMap) { - Map result = new LinkedHashMap<>(resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new))); - result.keySet().removeAll(getToBeDeletedStorageUnitNames(resourceMetaData.getStorageUnits(), toBeChangedStorageUnitNodeMap.keySet())); - result.putAll(getChangedStorageUnitNodeMap(resourceMetaData.getStorageUnits(), toBeChangedStorageUnitNodeMap)); - result.putAll(getToBeAddedStorageUnitNodeMap(resourceMetaData.getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getStorageNode(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), toBeChangedStorageUnitNodeMap)); - return result; - } - private Map getChangedDataSources(final Map storageNodes, final Collection toBeChangedStorageNodes, final Map propsMap) { Collection toBeChangedDataSourceNames = toBeChangedStorageNodes.stream() diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java index 3292629ad1977..9dfa8962ad481 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResource.java @@ -23,7 +23,6 @@ import org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer; 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.node.StorageNode; import javax.sql.DataSource; @@ -41,7 +40,7 @@ public final class SwitchingResource { @Getter(AccessLevel.NONE) private final ResourceMetaData resourceMetaData; - private final StorageResource newStorageResource; + private final Map newDataSources; private final Map staleDataSources; diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java index f522305ffa945..b6c31cfea8a3c 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/ResourceSwitchManagerTest.java @@ -53,7 +53,7 @@ void assertCreateByAlterDataSourcePoolProperties() { dataSourceMap.put("ds_0", new MockedDataSource()); dataSourceMap.put("ds_1", new MockedDataSource()); SwitchingResource actual = new ResourceSwitchManager().createByAlterDataSourcePoolProperties(new ResourceMetaData(dataSourceMap), Collections.emptyMap()); - assertTrue(actual.getNewStorageResource().getDataSources().isEmpty()); + assertTrue(actual.getNewDataSources().isEmpty()); assertThat(actual.getStaleDataSources().size(), is(2)); actual.closeStaleDataSources(); assertStaleDataSource((MockedDataSource) dataSourceMap.get("ds_0")); @@ -85,10 +85,10 @@ private Map getDataSourcePoolProperties(final int initialCapacit } private void assertNewDataSources(final SwitchingResource actual) { - assertThat(actual.getNewStorageResource().getDataSources().size(), is(3)); - assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNode("not_change"))); - assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNode("new"))); - assertTrue(actual.getNewStorageResource().getDataSources().containsKey(new StorageNode("replace"))); + assertThat(actual.getNewDataSources().size(), is(3)); + assertTrue(actual.getNewDataSources().containsKey(new StorageNode("not_change"))); + assertTrue(actual.getNewDataSources().containsKey(new StorageNode("new"))); + assertTrue(actual.getNewDataSources().containsKey(new StorageNode("replace"))); } private void assertStaleDataSources(final Map originalDataSourceMap) { diff --git a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java index 3ea06fd307cea..09be1d7dbc6a3 100644 --- a/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java +++ b/mode/core/src/test/java/org/apache/shardingsphere/mode/manager/switcher/SwitchingResourceTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.mode.manager.switcher; 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.node.StorageNode; import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource; import org.awaitility.Awaitility; @@ -36,8 +35,7 @@ class SwitchingResourceTest { void assertCloseStaleDataSources() { MockedDataSource staleDataSource = new MockedDataSource(); ResourceMetaData resourceMetaData = mock(ResourceMetaData.class); - StorageResource newStorageResource = new StorageResource(Collections.singletonMap(new StorageNode("new_ds"), new MockedDataSource()), Collections.emptyList()); - new SwitchingResource(resourceMetaData, newStorageResource, + new SwitchingResource(resourceMetaData, Collections.singletonMap(new StorageNode("new_ds"), new MockedDataSource()), Collections.singletonMap(new StorageNode("stale_ds"), staleDataSource), Collections.emptyList(), Collections.emptyMap()).closeStaleDataSources(); Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(staleDataSource::isClosed); assertTrue(staleDataSource.isClosed());