Skip to content

Commit

Permalink
Decouple ResourceMetaData and StorageResource
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 26, 2023
1 parent ddc6db9 commit b547f07
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public static ShardingSphereDatabase create(final String name, final DatabaseTyp
}

private static ResourceMetaData createResourceMetaData(final String databaseName, final StorageResource storageResource, final Map<String, DataSourcePoolProperties> propsMap) {
return new ResourceMetaData(databaseName, storageResource, propsMap);
return new ResourceMetaData(databaseName, storageResource.getDataSourceMap(), storageResource.getStorageUnitNodeMappers(), propsMap);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.shardingsphere.infra.metadata.database.resource.node.StorageNode;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnitNodeMapper;

import javax.sql.DataSource;
import java.util.Collection;
Expand Down Expand Up @@ -56,9 +57,10 @@ public ResourceMetaData(final String databaseName, final Map<String, DataSource>
StorageResourceUtils.getStorageUnitNodeMappers(dataSources));
}

public ResourceMetaData(final String databaseName, final StorageResource storageResource, final Map<String, DataSourcePoolProperties> propsMap) {
dataSourceMap = storageResource.getDataSourceMap();
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageResource.getStorageUnitNodeMappers());
public ResourceMetaData(final String databaseName, final Map<StorageNode, DataSource> dataSourceMap,
final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers, final Map<String, DataSourcePoolProperties> propsMap) {
this.dataSourceMap = dataSourceMap;
storageUnitMetaData = new StorageUnitMetaData(databaseName, dataSourceMap, propsMap, storageUnitNodeMappers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,6 @@ public static StorageResource createStorageResource(final Map<String, DataSource
return new StorageResource(storageNodes, mappers);
}

private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
try {
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
return isInstanceConnectionAvailable ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName;
} catch (final UnrecognizedDatabaseURLException ex) {
return dataSourceName;
}
}

private static String generateStorageNodeName(final String hostname, final int port, final String username) {
return String.format("%s_%s_%s", hostname, port, username);
}

private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) {
return isInstanceConnectionAvailable
? new StorageUnitNodeMapper(storageUnitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url)
: new StorageUnitNodeMapper(storageUnitName, storageNode, url);
}

/**
* Create storage resource without data source.
*
Expand All @@ -107,6 +88,25 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map<S
return new StorageResource(storageNodes, mappers);
}

private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
try {
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
return isInstanceConnectionAvailable ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName;
} catch (final UnrecognizedDatabaseURLException ex) {
return dataSourceName;
}
}

private static String generateStorageNodeName(final String hostname, final int port, final String username) {
return String.format("%s_%s_%s", hostname, port, username);
}

private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final String storageUnitName, final String url, final boolean isInstanceConnectionAvailable) {
return isInstanceConnectionAvailable
? new StorageUnitNodeMapper(storageUnitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url)
: new StorageUnitNodeMapper(storageUnitName, storageNode, url);
}

/**
* Get storage node grouped data source pool properties map.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,10 @@ public void alterSchemaMetaData(final String databaseName, final ShardingSphereD
public Map<String, ShardingSphereDatabase> renewDatabase(final ShardingSphereDatabase database, final SwitchingResource resource) {
Map<StorageNode, DataSource> newStorageNodes = getNewStorageNodes(database.getResourceMetaData().getDataSourceMap(), resource);
Map<String, StorageUnitNodeMapper> newStorageUnitNodeMappers = getNewStorageUnitNodeMappers(database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits(), resource);
StorageResource newStorageResource = new StorageResource(newStorageNodes, newStorageUnitNodeMappers);
Map<String, DataSourcePoolProperties> propsMap = database.getResourceMetaData().getStorageUnitMetaData().getStorageUnits().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSourcePoolProperties(), (oldValue, currentValue) -> currentValue, LinkedHashMap::new));
return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(
database.getName(), database.getProtocolType(), new ResourceMetaData(database.getName(), newStorageResource, propsMap), database.getRuleMetaData(), database.getSchemas()));
return Collections.singletonMap(database.getName().toLowerCase(), new ShardingSphereDatabase(database.getName(), database.getProtocolType(),
new ResourceMetaData(database.getName(), newStorageNodes, newStorageUnitNodeMappers, propsMap), database.getRuleMetaData(), database.getSchemas()));
}

private Map<StorageNode, DataSource> getNewStorageNodes(final Map<StorageNode, DataSource> currentStorageNodes, final SwitchingResource resource) {
Expand Down

0 comments on commit b547f07

Please sign in to comment.