Skip to content

Commit

Permalink
Refactor ShowStorageUnitExecutor (#28687)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 8, 2023
1 parent d742f4e commit e4ec1dd
Showing 1 changed file with 16 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowStorageUnitsStatement;
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
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.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.metadata.database.resource.ResourceMetaData;
import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.util.json.JsonUtils;
Expand Down Expand Up @@ -57,15 +55,15 @@ public Collection<String> getColumnNames() {

@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowStorageUnitsStatement sqlStatement) {
ResourceMetaData resourceMetaData = database.getResourceMetaData();
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
for (Entry<String, DataSourcePoolProperties> entry : getDataSourcePoolPropertiesMap(database, sqlStatement).entrySet()) {
String key = entry.getKey();
ConnectionProperties connectionProps = resourceMetaData.getStorageUnits().get(key).getConnectionProperties();
String storageUnitName = entry.getKey();
StorageUnit storageUnit = database.getResourceMetaData().getStorageUnits().get(storageUnitName);
ConnectionProperties connectionProps = storageUnit.getConnectionProperties();
Map<String, Object> poolProps = entry.getValue().getPoolPropertySynonyms().getStandardProperties();
Map<String, Object> customProps = getCustomProps(entry.getValue().getCustomProperties().getProperties(), connectionProps.getQueryProperties());
result.add(new LocalDataQueryResultRow(key,
resourceMetaData.getStorageUnits().get(key).getStorageType().getType(),
Map<String, Object> customProps = getCustomProperties(entry.getValue().getCustomProperties().getProperties(), connectionProps.getQueryProperties());
result.add(new LocalDataQueryResultRow(storageUnitName,
storageUnit.getStorageType().getType(),
connectionProps.getHostname(),
connectionProps.getPort(),
connectionProps.getCatalog(),
Expand All @@ -80,15 +78,6 @@ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase
return result;
}

private Map<String, Object> getCustomProps(final Map<String, Object> customProps, final Properties queryProps) {
Map<String, Object> result = new LinkedHashMap<>(customProps.size() + 1, 1F);
result.putAll(customProps);
if (!queryProps.isEmpty()) {
result.put("queryProperties", queryProps);
}
return result;
}

private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(final ShardingSphereDatabase database, final ShowStorageUnitsStatement sqlStatement) {
Map<String, DataSourcePoolProperties> result = new LinkedHashMap<>(database.getResourceMetaData().getStorageUnits().size(), 1F);
Map<String, DataSourcePoolProperties> propsMap = database.getResourceMetaData().getStorageUnits().entrySet().stream()
Expand All @@ -113,11 +102,10 @@ private Map<String, DataSourcePoolProperties> getDataSourcePoolPropertiesMap(fin

private DataSourcePoolProperties getDataSourcePoolProperties(final Map<String, DataSourcePoolProperties> propsMap, final String storageUnitName,
final DatabaseType databaseType, final DataSource dataSource) {
DataSourcePoolProperties result = getDataSourcePoolProperties(dataSource);
DialectDatabaseMetaData dialectDatabaseMetaData = new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData();
if (dialectDatabaseMetaData.isInstanceConnectionAvailable() && propsMap.containsKey(storageUnitName)) {
DataSourcePoolProperties unitDataSourcePoolProps = propsMap.get(storageUnitName);
for (Entry<String, Object> entry : unitDataSourcePoolProps.getPoolPropertySynonyms().getStandardProperties().entrySet()) {
DataSourcePoolProperties result = DataSourcePoolPropertiesCreator.create(
dataSource instanceof CatalogSwitchableDataSource ? ((CatalogSwitchableDataSource) dataSource).getDataSource() : dataSource);
if (new DatabaseTypeRegistry(databaseType).getDialectDatabaseMetaData().isInstanceConnectionAvailable() && propsMap.containsKey(storageUnitName)) {
for (Entry<String, Object> entry : propsMap.get(storageUnitName).getPoolPropertySynonyms().getStandardProperties().entrySet()) {
if (null != entry.getValue()) {
result.getPoolPropertySynonyms().getStandardProperties().put(entry.getKey(), entry.getValue());
}
Expand All @@ -126,10 +114,12 @@ private DataSourcePoolProperties getDataSourcePoolProperties(final Map<String, D
return result;
}

private DataSourcePoolProperties getDataSourcePoolProperties(final DataSource dataSource) {
return dataSource instanceof CatalogSwitchableDataSource
? DataSourcePoolPropertiesCreator.create(((CatalogSwitchableDataSource) dataSource).getDataSource())
: DataSourcePoolPropertiesCreator.create(dataSource);
private Map<String, Object> getCustomProperties(final Map<String, Object> customProps, final Properties queryProps) {
Map<String, Object> result = new LinkedHashMap<>(customProps);
if (!queryProps.isEmpty()) {
result.put("queryProperties", queryProps);
}
return result;
}

private String getStandardProperty(final Map<String, Object> standardProps, final String key) {
Expand Down

0 comments on commit e4ec1dd

Please sign in to comment.