Skip to content

Commit

Permalink
Refactor SwitchingResource
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Oct 7, 2023
1 parent 0aa88fd commit 90c26d7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re

private Map<StorageNode, DataSource> getMergedStorageNodeDataSources(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) {
Map<StorageNode, DataSource> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMeta
Map<String, StorageNode> toBeCreatedStorageUintNodeMap = StorageUnitNodeMapCreator.create(storageUnitDataSourcePoolPropsMap);
Map<StorageNode, DataSourcePoolProperties> 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<String, StorageNode> storageUintNodeMap, final Map<StorageNode, DataSourcePoolProperties> dataSourcePoolPropsMap) {
Map<StorageNode, DataSource> newStorageNodes = new LinkedHashMap<>(storageUintNodeMap.size(), 1F);
private Map<StorageNode, DataSource> getRegisterNewDataSources(final ResourceMetaData resourceMetaData,
final Map<String, StorageNode> storageUintNodeMap, final Map<StorageNode, DataSourcePoolProperties> dataSourcePoolPropsMap) {
Map<StorageNode, DataSource> 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;
}

/**
Expand All @@ -81,16 +81,16 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat
Map<String, StorageNode> toBeAlteredStorageUintNodeMap = StorageUnitNodeMapCreator.create(mergedDataSourcePoolPropsMap);
Map<StorageNode, DataSourcePoolProperties> 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<String, StorageNode> storageUintNodeMap, final Map<StorageNode, DataSourcePoolProperties> dataSourcePoolPropsMap) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>(storageUintNodeMap.size(), 1F);
private Map<StorageNode, DataSource> getAlterNewDataSources(final Map<String, StorageNode> storageUintNodeMap, final Map<StorageNode, DataSourcePoolProperties> dataSourcePoolPropsMap) {
Map<StorageNode, DataSource> 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<String, StorageNode> storageUintNodeMap) {
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M
mergedPropsMap.putAll(toBeChangedPropsMap);
Map<String, StorageNode> toBeChangedStorageUnitNodeMap = StorageUnitNodeMapCreator.create(toBeChangedPropsMap);
Map<StorageNode, DataSourcePoolProperties> 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);
}

/**
Expand All @@ -72,8 +72,8 @@ public SwitchingResource createByDropResource(final ResourceMetaData resourceMet
mergedDataSourcePoolPropertiesMap.keySet().removeIf(toBeDeletedPropsMap::containsKey);
Map<String, StorageNode> 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);
}

/**
Expand All @@ -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<StorageNode, DataSourcePoolProperties> 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<String, StorageNode> toBeChangedStorageUnitNodeMap, final Map<StorageNode, DataSourcePoolProperties> dataSourcePoolPropsMap) {
Map<StorageNode, DataSource> storageNodes = getNewStorageNodes(resourceMetaData, toBeChangedStorageUnitNodeMap.values(), dataSourcePoolPropsMap);
Map<String, StorageNode> storageUnitNodeMap = getNewStorageUnitNodeMap(resourceMetaData, toBeChangedStorageUnitNodeMap);
return new StorageResource(storageNodes, new LinkedHashSet<>(storageUnitNodeMap.keySet()));
}

private Map<StorageNode, DataSource> getNewStorageNodes(final ResourceMetaData resourceMetaData, final Collection<StorageNode> toBeChangedStorageNodes,
final Map<StorageNode, DataSourcePoolProperties> propsMap) {
private Map<StorageNode, DataSource> createNewDataSources(final ResourceMetaData resourceMetaData, final Map<String, StorageNode> toBeChangedStorageUnitNodeMap,
final Map<StorageNode, DataSourcePoolProperties> propsMap) {
Map<StorageNode, DataSource> result = new LinkedHashMap<>(resourceMetaData.getDataSources());
Collection<StorageNode> 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<String, StorageNode> getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map<String, StorageNode> toBeChangedStorageUnitNodeMap) {
Map<String, StorageNode> 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<StorageNode, DataSource> getChangedDataSources(final Map<StorageNode, DataSource> storageNodes,
final Collection<StorageNode> toBeChangedStorageNodes, final Map<StorageNode, DataSourcePoolProperties> propsMap) {
Collection<StorageNode> toBeChangedDataSourceNames = toBeChangedStorageNodes.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,7 +40,7 @@ public final class SwitchingResource {
@Getter(AccessLevel.NONE)
private final ResourceMetaData resourceMetaData;

private final StorageResource newStorageResource;
private final Map<StorageNode, DataSource> newDataSources;

private final Map<StorageNode, DataSource> staleDataSources;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -85,10 +85,10 @@ private Map<String, Object> 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<String, DataSource> originalDataSourceMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down

0 comments on commit 90c26d7

Please sign in to comment.