Skip to content

Commit

Permalink
Refactor StorageUnitMetaData (#28616)
Browse files Browse the repository at this point in the history
* Refactor StorageUnitMetaData

* Refactor StorageUnitMetaData

* Refactor StorageUnitMetaData
  • Loading branch information
terrymanu authored Sep 28, 2023
1 parent 6e8d44f commit 5eb742b
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,21 @@
public final class StorageUnitMetaData {

// TODO zhangliang: should refactor
private final Map<String, StorageNode> storageUnitNodeMap;
private final Map<String, StorageNode> storageNodes;

private final Map<String, StorageUnit> storageUnits;

// TODO zhangliang: should refactor
private final Map<String, DataSource> dataSources;

public StorageUnitMetaData(final String databaseName, final Map<StorageNodeName, DataSource> storageNodeDataSources,
final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> storageUnitNodeMap) {
this.storageUnitNodeMap = storageUnitNodeMap;
storageUnits = new LinkedHashMap<>(this.storageUnitNodeMap.size(), 1F);
for (Entry<String, StorageNode> entry : this.storageUnitNodeMap.entrySet()) {
storageUnits.put(entry.getKey(), new StorageUnit(databaseName, storageNodeDataSources, dataSourcePoolPropertiesMap.get(entry.getKey()), entry.getValue()));
}
dataSources = createDataSources();
final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap, final Map<String, StorageNode> 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)));
}

/**
Expand All @@ -61,12 +61,4 @@ public Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap() {
return storageUnits.entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private Map<String, DataSource> createDataSources() {
Map<String, DataSource> result = new LinkedHashMap<>(storageUnits.size(), 1F);
for (Entry<String, StorageUnit> entry : storageUnits.entrySet()) {
result.put(entry.getKey(), entry.getValue().getDataSource());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ private DatabaseConfiguration getDatabaseConfiguration(final ResourceMetaData re

private StorageResource getMergedStorageResource(final ResourceMetaData currentResourceMetaData, final SwitchingResource switchingResource) {
Map<StorageNodeName, DataSource> storageNodeDataSources = currentResourceMetaData.getDataSources();
Map<String, StorageNode> storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap();
Map<String, StorageNode> storageUnitNodeMap = currentResourceMetaData.getStorageUnitMetaData().getStorageNodes();
if (null != switchingResource && null != switchingResource.getNewStorageResource() && !switchingResource.getNewStorageResource().getDataSources().isEmpty()) {
storageNodeDataSources.putAll(switchingResource.getNewStorageResource().getDataSources());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, StorageNode> reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap();
StorageNode storageNode = resourceMetaData.getStorageUnitMetaData().getStorageNodes().remove(storageUnitName);
Map<String, StorageNode> reservedStorageUintNodeMap = resourceMetaData.getStorageUnitMetaData().getStorageNodes();
Map<StorageNodeName, DataSource> storageNodes = new LinkedHashMap<>(1, 1F);
if (reservedStorageUintNodeMap.values().stream().noneMatch(each -> each.equals(storageNode))) {
storageNodes.put(storageNode.getName(), resourceMetaData.getDataSources().get(storageNode.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ private Map<StorageNodeName, DataSource> getNewStorageNodes(final ResourceMetaDa
}

private Map<String, StorageNode> getNewStorageUnitNodeMap(final ResourceMetaData resourceMetaData, final Map<String, StorageNode> toBeChangedStorageUnitNodeMap) {
Map<String, StorageNode> result = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap());
Map<String, StorageNode> 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;
}

Expand Down Expand Up @@ -164,7 +164,7 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re
private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final Map<String, StorageNode> toBeChangedStorageUnitNodeMap,
final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<StorageNodeName, DataSource> storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSources().size(), 1F);
Map<String, StorageNode> storageUnitNodeMap = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getStorageUnitNodeMap().size(), 1F);
Map<String, StorageNode> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 5eb742b

Please sign in to comment.