Skip to content

Commit 271dede

Browse files
authored
Pick 5.5.0-fix, optimize show/alter storage units (#26)
1 parent e8a5dc5 commit 271dede

File tree

13 files changed

+391
-32
lines changed

13 files changed

+391
-32
lines changed

infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/AlterStorageUnitConnectionInfoException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public final class AlterStorageUnitConnectionInfoException extends ResourceDefin
3232
public AlterStorageUnitConnectionInfoException(final Collection<String> storageUnitNames) {
3333
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 11, "Can not alter connection info in storage units: '%s'.", storageUnitNames);
3434
}
35+
36+
public AlterStorageUnitConnectionInfoException(final String message) {
37+
super(XOpenSQLState.FEATURE_NOT_SUPPORTED, 11, message);
38+
}
3539
}

infra/distsql-handler/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@
4747
<artifactId>shardingsphere-mode-core</artifactId>
4848
<version>${project.version}</version>
4949
</dependency>
50+
<!-- SPEX ADDED: BEGIN -->
51+
<dependency>
52+
<groupId>com.zaxxer</groupId>
53+
<artifactId>HikariCP</artifactId>
54+
<scope>compile</scope>
55+
</dependency>
56+
<!-- SPEX ADDED: END -->
5057

5158
<dependency>
5259
<groupId>org.apache.shardingsphere</groupId>

infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/AlterStorageUnitExecutor.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.apache.shardingsphere.distsql.handler.executor.rdl.resource;
1919

20+
import com.sphereex.dbplusengine.SphereEx;
21+
import com.sphereex.dbplusengine.SphereEx.Type;
2022
import lombok.Setter;
2123
import lombok.extern.slf4j.Slf4j;
2224
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
2325
import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecutor;
2426
import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator;
27+
import org.apache.shardingsphere.distsql.segment.AlterPoolPropertiesSegment;
2528
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
2629
import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
2730
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
@@ -44,6 +47,7 @@
4447

4548
import java.sql.SQLException;
4649
import java.util.Collection;
50+
import java.util.LinkedHashMap;
4751
import java.util.Map;
4852
import java.util.Map.Entry;
4953
import java.util.Objects;
@@ -60,11 +64,14 @@ public final class AlterStorageUnitExecutor implements DistSQLUpdateExecutor<Alt
6064

6165
private ShardingSphereDatabase database;
6266

67+
@SphereEx(Type.MODIFY)
6368
@Override
6469
public void executeUpdate(final AlterStorageUnitStatement sqlStatement, final ContextManager contextManager) {
6570
checkBefore(sqlStatement);
66-
Map<String, DataSourcePoolProperties> propsMap = DataSourceSegmentsConverter.convert(database.getProtocolType(), sqlStatement.getStorageUnits());
67-
validateHandler.validate(propsMap, getExpectedPrivileges(sqlStatement));
71+
Map<String, DataSourcePoolProperties> propsMap = DataSourceSegmentsConverter.convert(database.getProtocolType(), database.getResourceMetaData(), sqlStatement.getStorageUnits());
72+
if (!sqlStatement.getStorageUnits().stream().allMatch(each -> each instanceof AlterPoolPropertiesSegment && null == each.getUser())) {
73+
validateHandler.validate(propsMap, getExpectedPrivileges(sqlStatement));
74+
}
6875
try {
6976
contextManager.getPersistServiceFacade().getMetaDataManagerPersistService().alterStorageUnits(database.getName(), propsMap);
7077
} catch (final SQLException | ShardingSphereExternalException ex) {
@@ -89,8 +96,15 @@ private void checkStorageUnitNameExisted(final Collection<String> storageUnitNam
8996
ShardingSpherePreconditions.checkMustEmpty(notExistedStorageUnitNames, () -> new MissingRequiredStorageUnitsException(database.getName(), notExistedStorageUnitNames));
9097
}
9198

99+
@SphereEx(Type.MODIFY)
92100
private void checkDatabase(final AlterStorageUnitStatement sqlStatement) {
93-
Collection<String> invalidStorageUnitNames = sqlStatement.getStorageUnits().stream().collect(Collectors.toMap(DataSourceSegment::getName, each -> each)).entrySet().stream()
101+
Map<String, DataSourceSegment> toBeCheckedSegments = new LinkedHashMap<>(sqlStatement.getStorageUnits().size(), 1F);
102+
for (DataSourceSegment each : sqlStatement.getStorageUnits()) {
103+
if (each instanceof HostnameAndPortBasedDataSourceSegment || each instanceof URLBasedDataSourceSegment) {
104+
toBeCheckedSegments.put(each.getName(), each);
105+
}
106+
}
107+
Collection<String> invalidStorageUnitNames = toBeCheckedSegments.entrySet().stream()
94108
.filter(each -> !isSameDatabase(each.getValue(), database.getResourceMetaData().getStorageUnits().get(each.getKey()))).map(Entry::getKey).collect(Collectors.toSet());
95109
ShardingSpherePreconditions.checkMustEmpty(invalidStorageUnitNames, () -> new AlterStorageUnitConnectionInfoException(invalidStorageUnitNames));
96110
}

infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rdl/resource/RegisterStorageUnitExecutor.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717

1818
package org.apache.shardingsphere.distsql.handler.executor.rdl.resource;
1919

20+
import com.sphereex.dbplusengine.SphereEx;
2021
import lombok.Setter;
2122
import lombok.extern.slf4j.Slf4j;
2223
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
2324
import org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecutor;
2425
import org.apache.shardingsphere.distsql.handler.validate.DistSQLDataSourcePoolPropertiesValidator;
2526
import org.apache.shardingsphere.distsql.segment.DataSourceSegment;
27+
import org.apache.shardingsphere.distsql.segment.HostnameAndPortBasedDataSourceSegment;
28+
import org.apache.shardingsphere.distsql.segment.URLBasedDataSourceSegment;
2629
import org.apache.shardingsphere.distsql.segment.converter.DataSourceSegmentsConverter;
2730
import org.apache.shardingsphere.distsql.statement.rdl.resource.unit.type.RegisterStorageUnitStatement;
2831
import org.apache.shardingsphere.infra.database.core.checker.PrivilegeCheckType;
@@ -55,6 +58,9 @@ public final class RegisterStorageUnitExecutor implements DistSQLUpdateExecutor<
5558

5659
@Override
5760
public void executeUpdate(final RegisterStorageUnitStatement sqlStatement, final ContextManager contextManager) {
61+
// SPEX ADDED: BEGIN
62+
checkSegmentType(sqlStatement);
63+
// SPEX ADDED: END
5864
checkDataSource(sqlStatement, contextManager);
5965
Map<String, DataSourcePoolProperties> propsMap = DataSourceSegmentsConverter.convert(database.getProtocolType(), sqlStatement.getStorageUnits());
6066
if (sqlStatement.isIfNotExists()) {
@@ -74,6 +80,14 @@ public void executeUpdate(final RegisterStorageUnitStatement sqlStatement, final
7480
}
7581
}
7682

83+
@SphereEx
84+
private void checkSegmentType(final RegisterStorageUnitStatement sqlStatement) {
85+
for (DataSourceSegment each : sqlStatement.getStorageUnits()) {
86+
ShardingSpherePreconditions.checkState(each instanceof HostnameAndPortBasedDataSourceSegment || each instanceof URLBasedDataSourceSegment,
87+
() -> new UnsupportedOperationException(String.format("Missing connection information for register storage unit %s.", each.getName())));
88+
}
89+
}
90+
7791
private void checkDataSource(final RegisterStorageUnitStatement sqlStatement, final ContextManager contextManager) {
7892
if (!sqlStatement.isIfNotExists()) {
7993
Collection<String> dataSourceNames = new ArrayList<>(sqlStatement.getStorageUnits().size());

infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/executor/rql/resource/ShowStorageUnitExecutor.java

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
package org.apache.shardingsphere.distsql.handler.executor.rql.resource;
1919

20+
import com.sphereex.dbplusengine.SphereEx;
21+
import com.sphereex.dbplusengine.SphereEx.Type;
22+
import com.zaxxer.hikari.HikariDataSource;
2023
import lombok.Setter;
2124
import org.apache.shardingsphere.distsql.handler.aware.DistSQLExecutorDatabaseAware;
2225
import org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecutor;
2326
import org.apache.shardingsphere.distsql.statement.rql.resource.ShowStorageUnitsStatement;
2427
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
25-
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
2628
import org.apache.shardingsphere.infra.datasource.pool.CatalogSwitchableDataSource;
2729
import org.apache.shardingsphere.infra.datasource.pool.props.creator.DataSourcePoolPropertiesCreator;
2830
import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
@@ -51,26 +53,36 @@ public final class ShowStorageUnitExecutor implements DistSQLQueryExecutor<ShowS
5153

5254
private ShardingSphereDatabase database;
5355

56+
@SphereEx(Type.MODIFY)
5457
@Override
5558
public Collection<String> getColumnNames(final ShowStorageUnitsStatement sqlStatement) {
5659
return Arrays.asList("name", "type", "host", "port", "db", "connection_timeout_milliseconds", "idle_timeout_milliseconds",
57-
"max_lifetime_milliseconds", "max_pool_size", "min_pool_size", "read_only", "other_attributes");
60+
"max_lifetime_milliseconds", "max_pool_size", "min_pool_size", "read_only", "other_attributes", "username", "pool_name", "actual_jdbc_url");
5861
}
5962

63+
@SphereEx(Type.MODIFY)
6064
@Override
6165
public Collection<LocalDataQueryResultRow> getRows(final ShowStorageUnitsStatement sqlStatement, final ContextManager contextManager) {
6266
return getStorageUnits(sqlStatement).entrySet().stream().map(entry -> getRow(entry.getKey(), entry.getValue())).collect(Collectors.toList());
6367
}
6468

69+
@SphereEx(Type.MODIFY)
6570
private LocalDataQueryResultRow getRow(final String name, final StorageUnit storageUnit) {
6671
ConnectionProperties connectionProps = storageUnit.getConnectionProperties();
67-
DataSourcePoolProperties dataSourcePoolProps = getDataSourcePoolProperties(storageUnit);
72+
DataSource actualDataSource = getActualDataSource(storageUnit.getDataSource());
73+
DataSourcePoolProperties dataSourcePoolProps = getDataSourcePoolProperties(actualDataSource);
6874
Map<String, Object> poolProps = dataSourcePoolProps.getPoolPropertySynonyms().getStandardProperties();
6975
Map<String, Object> customProps = getCustomProperties(dataSourcePoolProps.getCustomProperties().getProperties(), connectionProps.getQueryProperties());
7076
return new LocalDataQueryResultRow(name, storageUnit.getStorageType().getType(), connectionProps.getHostname(), connectionProps.getPort(), connectionProps.getCatalog(),
71-
getStandardProperty(poolProps, "connectionTimeoutMilliseconds"), getStandardProperty(poolProps, "idleTimeoutMilliseconds"),
72-
getStandardProperty(poolProps, "maxLifetimeMilliseconds"), getStandardProperty(poolProps, "maxPoolSize"), getStandardProperty(poolProps, "minPoolSize"),
73-
getStandardProperty(poolProps, "readOnly"), customProps);
77+
getStandardProperty(poolProps, "connectionTimeoutMilliseconds"),
78+
getStandardProperty(poolProps, "idleTimeoutMilliseconds"),
79+
getStandardProperty(poolProps, "maxLifetimeMilliseconds"),
80+
getStandardProperty(poolProps, "maxPoolSize"),
81+
getStandardProperty(poolProps, "minPoolSize"),
82+
getStandardProperty(poolProps, "readOnly"),
83+
customProps,
84+
getUsername(actualDataSource, dataSourcePoolProps.getAllStandardProperties()),
85+
getPoolName(actualDataSource), getActualURL(actualDataSource));
7486
}
7587

7688
private Map<String, StorageUnit> getStorageUnits(final ShowStorageUnitsStatement sqlStatement) {
@@ -82,18 +94,9 @@ private Optional<Pattern> getLikePattern(final ShowStorageUnitsStatement sqlStat
8294
return sqlStatement.getLikePattern().map(optional -> Pattern.compile(RegexUtils.convertLikePatternToRegex(optional), Pattern.CASE_INSENSITIVE));
8395
}
8496

85-
private DataSourcePoolProperties getDataSourcePoolProperties(final StorageUnit storageUnit) {
86-
DataSource dataSource = storageUnit.getDataSource();
87-
DataSourcePoolProperties result = DataSourcePoolPropertiesCreator.create(
88-
dataSource instanceof CatalogSwitchableDataSource ? ((CatalogSwitchableDataSource) dataSource).getDataSource() : dataSource);
89-
if (new DatabaseTypeRegistry(storageUnit.getStorageType()).getDialectDatabaseMetaData().isInstanceConnectionAvailable()) {
90-
for (Entry<String, Object> entry : storageUnit.getDataSourcePoolProperties().getPoolPropertySynonyms().getStandardProperties().entrySet()) {
91-
if (null != entry.getValue()) {
92-
result.getPoolPropertySynonyms().getStandardProperties().put(entry.getKey(), entry.getValue());
93-
}
94-
}
95-
}
96-
return result;
97+
@SphereEx(Type.MODIFY)
98+
private DataSourcePoolProperties getDataSourcePoolProperties(final DataSource actualDataSource) {
99+
return DataSourcePoolPropertiesCreator.create(actualDataSource);
97100
}
98101

99102
private Map<String, Object> getCustomProperties(final Map<String, Object> customProps, final Properties queryProps) {
@@ -109,6 +112,35 @@ private String getStandardProperty(final Map<String, Object> standardProps, fina
109112
return standardProps.containsKey(key) && null != standardProps.get(key) ? standardProps.get(key).toString() : "";
110113
}
111114

115+
@SphereEx
116+
private DataSource getActualDataSource(final DataSource dataSource) {
117+
return dataSource instanceof CatalogSwitchableDataSource ? ((CatalogSwitchableDataSource) dataSource).getDataSource() : dataSource;
118+
}
119+
120+
@SphereEx
121+
private String getUsername(final DataSource actualDataSource, final Map<String, Object> standardProps) {
122+
if (actualDataSource instanceof HikariDataSource) {
123+
return ((HikariDataSource) actualDataSource).getUsername();
124+
}
125+
return getStandardProperty(standardProps, "username");
126+
}
127+
128+
@SphereEx
129+
private String getPoolName(final DataSource actualDataSource) {
130+
if (actualDataSource instanceof HikariDataSource) {
131+
return ((HikariDataSource) actualDataSource).getPoolName();
132+
}
133+
return "";
134+
}
135+
136+
@SphereEx
137+
private String getActualURL(final DataSource actualDataSource) {
138+
if (actualDataSource instanceof HikariDataSource) {
139+
return ((HikariDataSource) actualDataSource).getJdbcUrl();
140+
}
141+
return "";
142+
}
143+
112144
@Override
113145
public Class<ShowStorageUnitsStatement> getType() {
114146
return ShowStorageUnitsStatement.class;

mode/core/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
<artifactId>shardingsphere-metadata-core</artifactId>
4343
<version>${project.version}</version>
4444
</dependency>
45+
<!-- SPEX ADDED: BEGIN -->
46+
<dependency>
47+
<groupId>com.zaxxer</groupId>
48+
<artifactId>HikariCP</artifactId>
49+
<scope>compile</scope>
50+
</dependency>
51+
<!-- SPEX ADDED: END -->
4552

4653
<dependency>
4754
<groupId>org.apache.shardingsphere</groupId>

0 commit comments

Comments
 (0)