Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StorageUnitMetaData #28616

Merged
merged 3 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading