Skip to content

Commit

Permalink
Merge branch 'apache:master' into dev_25892
Browse files Browse the repository at this point in the history
  • Loading branch information
zihaoAK47 authored Sep 26, 2023
2 parents a4e3944 + 95c8394 commit 4601e37
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

import lombok.Getter;
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -41,18 +39,10 @@ public final class StorageResource {

private final Map<String, DataSource> wrappedDataSources;

private final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap;

public StorageResource(final Map<StorageNode, DataSource> dataSourceMap, final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers) {
this(dataSourceMap, storageUnitNodeMappers, Collections.emptyMap());
}

public StorageResource(final Map<StorageNode, DataSource> dataSourceMap,
final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers, final Map<String, DataSourcePoolProperties> dataSourcePoolPropertiesMap) {
this.dataSourceMap = dataSourceMap;
this.storageUnitNodeMappers = storageUnitNodeMappers;
wrappedDataSources = createWrappedDataSources();
this.dataSourcePoolPropertiesMap = dataSourcePoolPropertiesMap;
}

private Map<String, DataSource> createWrappedDataSources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -91,7 +93,6 @@ private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode
public static StorageResource createStorageResourceWithoutDataSource(final Map<String, DataSourcePoolProperties> propsMap) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>();
Map<String, StorageUnitNodeMapper> mappers = new LinkedHashMap<>();
Map<String, DataSourcePoolProperties> newPropsMap = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
String storageUnitName = entry.getKey();
Map<String, Object> standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties();
Expand All @@ -100,10 +101,30 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map<S
StorageNode storageNode = new StorageNode(getStorageNodeName(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable));
if (!storageNodes.containsKey(storageNode)) {
storageNodes.put(storageNode, null);
newPropsMap.put(storageNode.getName(), entry.getValue());
}
mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable));
}
return new StorageResource(storageNodes, mappers, newPropsMap);
return new StorageResource(storageNodes, mappers);
}

/**
* Get storage node grouped data source pool properties map.
*
* @param storageUnitDataSourcePoolProps storage unit grouped data source pool properties map
* @return storage node grouped data source pool properties map
*/
public static Map<String, DataSourcePoolProperties> getStorageNodeDataSourcePoolProperties(final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>();
Collection<StorageNode> storageNodes = new HashSet<>();
for (Entry<String, DataSourcePoolProperties> entry : storageUnitDataSourcePoolProps.entrySet()) {
Map<String, Object> standardProps = entry.getValue().getConnectionPropertySynonyms().getStandardProperties();
String url = standardProps.get("url").toString();
boolean isInstanceConnectionAvailable = new DatabaseTypeRegistry(DatabaseTypeFactory.get(url)).getDialectDatabaseMetaData().isInstanceConnectionAvailable();
StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), url, standardProps.get("username").toString(), isInstanceConnectionAvailable));
if (storageNodes.add(storageNode)) {
result.put(storageNode.getName(), entry.getValue());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,24 @@ public final class NewResourceSwitchManager {
* Register storage unit.
*
* @param resourceMetaData resource meta data
* @param propsMap data source pool properties map
* @param storageUnitDataSourcePoolProps storage unit grouped data source pool properties map
* @return created switching resource
*/
public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> propsMap) {
public SwitchingResource registerStorageUnit(final ResourceMetaData resourceMetaData, final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedPropsMap.putAll(propsMap);
StorageResource toBeCreatedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(propsMap);
return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageResource),
mergedPropsMap.putAll(storageUnitDataSourcePoolProps);
StorageResource toBeCreatedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(storageUnitDataSourcePoolProps);
return new SwitchingResource(resourceMetaData, getRegisterNewStorageResource(resourceMetaData, toBeCreatedStorageResource, storageUnitDataSourcePoolProps),
new StorageResource(Collections.emptyMap(), Collections.emptyMap()), mergedPropsMap);
}

private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData, final StorageResource toBeCreatedStorageResource) {
private StorageResource getRegisterNewStorageResource(final ResourceMetaData resourceMetaData,
final StorageResource toBeCreatedStorageResource, final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps);
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>(toBeCreatedStorageResource.getDataSourceMap().size(), 1F);
for (StorageNode each : toBeCreatedStorageResource.getDataSourceMap().keySet()) {
if (!resourceMetaData.getDataSourceMap().containsKey(each)) {
storageNodes.put(each, DataSourcePoolCreator.create(toBeCreatedStorageResource.getDataSourcePoolPropertiesMap().get(each.getName())));
storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each.getName())));
}
}
return new StorageResource(storageNodes, toBeCreatedStorageResource.getStorageUnitNodeMappers());
Expand All @@ -74,14 +76,15 @@ public SwitchingResource alterStorageUnit(final ResourceMetaData resourceMetaDat
Map<String, DataSourcePoolProperties> mergedDataSourcePoolPropertiesMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedDataSourcePoolPropertiesMap.putAll(propsMap);
StorageResource toBeAlteredStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(mergedDataSourcePoolPropertiesMap);
return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageResource),
return new SwitchingResource(resourceMetaData, getAlterNewStorageResource(toBeAlteredStorageResource, mergedDataSourcePoolPropertiesMap),
getStaleStorageResource(resourceMetaData, toBeAlteredStorageResource), mergedDataSourcePoolPropertiesMap);
}

private StorageResource getAlterNewStorageResource(final StorageResource toBeAlteredStorageResource) {
private StorageResource getAlterNewStorageResource(final StorageResource toBeAlteredStorageResource, final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps);
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>(toBeAlteredStorageResource.getDataSourceMap().size(), 1F);
for (StorageNode each : toBeAlteredStorageResource.getDataSourceMap().keySet()) {
storageNodes.put(each, DataSourcePoolCreator.create(toBeAlteredStorageResource.getDataSourcePoolPropertiesMap().get(each.getName())));
storageNodes.put(each, DataSourcePoolCreator.create(storageNodeDataSourcePoolProps.get(each.getName())));
}
return new StorageResource(storageNodes, toBeAlteredStorageResource.getStorageUnitNodeMappers());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public SwitchingResource create(final ResourceMetaData resourceMetaData, final M
Map<String, DataSourcePoolProperties> mergedPropsMap = new HashMap<>(resourceMetaData.getStorageUnitMetaData().getDataSourcePoolPropertiesMap());
mergedPropsMap.putAll(toBeChangedPropsMap);
StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap);
return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource),
getStaleDataSources(resourceMetaData, toBeChangedStorageResource), mergedPropsMap);
return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap),
getStaleDataSources(resourceMetaData, toBeChangedStorageResource, mergedPropsMap), mergedPropsMap);
}

/**
Expand Down Expand Up @@ -83,17 +83,20 @@ public SwitchingResource createByAlterDataSourcePoolProperties(final ResourceMet
mergedDataSourcePoolPropertiesMap.keySet().removeIf(each -> !toBeChangedPropsMap.containsKey(each));
mergedDataSourcePoolPropertiesMap.putAll(toBeChangedPropsMap);
StorageResource toBeChangedStorageResource = StorageResourceCreator.createStorageResourceWithoutDataSource(toBeChangedPropsMap);
StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource);
StorageResource staleStorageResource = getStaleDataSources(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap);
staleStorageResource.getDataSourceMap()
.putAll(getToBeDeletedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageResource.getDataSourceMap().keySet()));
staleStorageResource.getStorageUnitNodeMappers().putAll(
getToBeDeletedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers().keySet()));
return new SwitchingResource(resourceMetaData, createNewStorageResource(resourceMetaData, toBeChangedStorageResource), staleStorageResource, mergedDataSourcePoolPropertiesMap);
return new SwitchingResource(resourceMetaData,
createNewStorageResource(resourceMetaData, toBeChangedStorageResource, toBeChangedPropsMap), staleStorageResource, mergedDataSourcePoolPropertiesMap);
}

private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource) {
private StorageResource createNewStorageResource(final ResourceMetaData resourceMetaData,
final StorageResource toBeChangedStorageResource, final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<String, DataSourcePoolProperties> storageNodeDataSourcePoolProps = StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps);
Map<StorageNode, DataSource> storageNodes =
getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getDataSourceMap(), toBeChangedStorageResource.getDataSourcePoolPropertiesMap());
getNewStorageNodes(resourceMetaData, toBeChangedStorageResource.getDataSourceMap(), storageNodeDataSourcePoolProps);
Map<String, StorageUnitNodeMapper> storageUnitNodeMappers = getNewStorageUnitNodeMappers(resourceMetaData, toBeChangedStorageResource.getStorageUnitNodeMappers());
return new StorageResource(storageNodes, storageUnitNodeMappers);
}
Expand Down Expand Up @@ -153,10 +156,11 @@ private StorageResource getToBeRemovedStaleDataSources(final ResourceMetaData re
return new StorageResource(staleStorageNodes, staleStorageUnitNodeMappers);
}

private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource) {
private StorageResource getStaleDataSources(final ResourceMetaData resourceMetaData, final StorageResource toBeChangedStorageResource,
final Map<String, DataSourcePoolProperties> storageUnitDataSourcePoolProps) {
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>(resourceMetaData.getDataSourceMap().size(), 1F);
Map<String, StorageUnitNodeMapper> storageUnitNodeMappers = new LinkedHashMap<>(resourceMetaData.getStorageUnitMetaData().getUnitNodeMappers().size(), 1F);
storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), toBeChangedStorageResource.getDataSourcePoolPropertiesMap()));
storageNodes.putAll(getToBeChangedDataSources(resourceMetaData.getDataSourceMap(), StorageResourceCreator.getStorageNodeDataSourcePoolProperties(storageUnitDataSourcePoolProps)));
storageUnitNodeMappers.putAll(getChangedStorageUnitNodeMappers(resourceMetaData.getStorageUnitMetaData().getStorageUnits(), toBeChangedStorageResource.getStorageUnitNodeMappers()));
return new StorageResource(storageNodes, storageUnitNodeMappers);
}
Expand Down

0 comments on commit 4601e37

Please sign in to comment.