Skip to content

Commit

Permalink
Refactor StorageResourceCreator (#28583)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 25, 2023
1 parent 015e40e commit 52fd6fc
Showing 1 changed file with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl;
import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;
import org.apache.shardingsphere.infra.database.core.connector.url.UnrecognizedDatabaseURLException;
import org.apache.shardingsphere.infra.database.core.metadata.database.DialectDatabaseMetaData;
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datasource.pool.creator.DataSourcePoolCreator;
Expand Down Expand Up @@ -52,24 +50,23 @@ public static StorageResource createStorageResource(final Map<String, DataSource
Map<StorageNode, DataSource> storageNodes = new LinkedHashMap<>();
Map<String, StorageUnitNodeMapper> mappers = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue()));
String storageUnitName = entry.getKey();
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(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable));
if (!storageNodes.containsKey(storageNode)) {
storageNodes.put(storageNode, DataSourcePoolCreator.create(entry.getKey(), entry.getValue(), true, storageNodes.values()));
storageNodes.put(storageNode, DataSourcePoolCreator.create(storageUnitName, entry.getValue(), true, storageNodes.values()));
}
appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue());
mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable));
}
return new StorageResource(storageNodes, mappers);
}

private static String getStorageNodeName(final String dataSourceName, final DataSourcePoolProperties storageNodeProps) {
Map<String, Object> standardProps = storageNodeProps.getConnectionPropertySynonyms().getStandardProperties();
String url = standardProps.get("url").toString();
String username = standardProps.get("username").toString();
DatabaseType databaseType = DatabaseTypeFactory.get(url);
private static String getStorageNodeName(final String dataSourceName, final String url, final String username, final boolean isInstanceConnectionAvailable) {
try {
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData();
return dialectDatabaseMetaData.isInstanceConnectionAvailable() ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName;
return isInstanceConnectionAvailable ? generateStorageNodeName(jdbcUrl.getHostname(), jdbcUrl.getPort(), username) : dataSourceName;
} catch (final UnrecognizedDatabaseURLException ex) {
return dataSourceName;
}
Expand All @@ -79,17 +76,10 @@ private static String generateStorageNodeName(final String hostname, final int p
return String.format("%s_%s_%s", hostname, port, username);
}

private static void appendStorageUnitNodeMapper(final Map<String, StorageUnitNodeMapper> storageUnitNodeMappers, final StorageNode storageNode,
final String storageUnitName, final DataSourcePoolProperties props) {
String url = props.getConnectionPropertySynonyms().getStandardProperties().get("url").toString();
storageUnitNodeMappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, DatabaseTypeFactory.get(url), storageUnitName, url));
}

private static StorageUnitNodeMapper getStorageUnitNodeMapper(final StorageNode storageNode, final DatabaseType databaseType, final String unitName, final String url) {
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData();
return dialectDatabaseMetaData.isInstanceConnectionAvailable()
? new StorageUnitNodeMapper(unitName, storageNode, new StandardJdbcUrlParser().parse(url).getDatabase(), url)
: new StorageUnitNodeMapper(unitName, storageNode, url);
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);
}

/**
Expand All @@ -103,14 +93,16 @@ public static StorageResource createStorageResourceWithoutDataSource(final Map<S
Map<String, StorageUnitNodeMapper> mappers = new LinkedHashMap<>();
Map<String, DataSourcePoolProperties> newPropsMap = new LinkedHashMap<>();
for (Entry<String, DataSourcePoolProperties> entry : propsMap.entrySet()) {
StorageNode storageNode = new StorageNode(getStorageNodeName(entry.getKey(), entry.getValue()));
if (storageNodes.containsKey(storageNode)) {
appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue());
continue;
String storageUnitName = entry.getKey();
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(storageUnitName, url, standardProps.get("username").toString(), isInstanceConnectionAvailable));
if (!storageNodes.containsKey(storageNode)) {
storageNodes.put(storageNode, null);
newPropsMap.put(storageNode.getName(), entry.getValue());
}
storageNodes.put(storageNode, null);
appendStorageUnitNodeMapper(mappers, storageNode, entry.getKey(), entry.getValue());
newPropsMap.put(storageNode.getName(), entry.getValue());
mappers.put(storageUnitName, getStorageUnitNodeMapper(storageNode, storageUnitName, url, isInstanceConnectionAvailable));
}
return new StorageResource(storageNodes, mappers, newPropsMap);
}
Expand Down

0 comments on commit 52fd6fc

Please sign in to comment.