Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor StorageUnitManager #34551

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public final class StorageUnitManager {
* @param propsMap data source pool properties map
*/
public synchronized void register(final String databaseName, final Map<String, DataSourcePoolProperties> propsMap) {
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
try {
closeStaleRules(databaseName);
SwitchingResource switchingResource = resourceSwitchManager.switchByRegisterStorageUnit(metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap);
buildNewMetaDataContext(databaseName, switchingResource);
closeStaleRules(database);
buildNewMetaDataContext(databaseName, resourceSwitchManager.switchByRegisterStorageUnit(database.getResourceMetaData(), propsMap));
} catch (final SQLException ex) {
log.error("Alter database: {} register storage unit failed.", databaseName, ex);
}
Expand All @@ -73,10 +73,10 @@ public synchronized void register(final String databaseName, final Map<String, D
* @param propsMap data source pool properties map
*/
public synchronized void alter(final String databaseName, final Map<String, DataSourcePoolProperties> propsMap) {
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
try {
closeStaleRules(databaseName);
SwitchingResource switchingResource = resourceSwitchManager.switchByAlterStorageUnit(metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap);
buildNewMetaDataContext(databaseName, switchingResource);
closeStaleRules(database);
buildNewMetaDataContext(databaseName, resourceSwitchManager.switchByAlterStorageUnit(database.getResourceMetaData(), propsMap));
} catch (final SQLException ex) {
log.error("Alter database: {} alter storage unit failed.", databaseName, ex);
}
Expand All @@ -89,11 +89,10 @@ public synchronized void alter(final String databaseName, final Map<String, Data
* @param storageUnitName storage unit name
*/
public synchronized void unregister(final String databaseName, final String storageUnitName) {
ShardingSphereDatabase database = metaDataContexts.getMetaData().getDatabase(databaseName);
try {
closeStaleRules(databaseName);
SwitchingResource switchingResource = resourceSwitchManager.switchByUnregisterStorageUnit(
metaDataContexts.getMetaData().getDatabase(databaseName).getResourceMetaData(), Collections.singleton(storageUnitName));
buildNewMetaDataContext(databaseName, switchingResource);
closeStaleRules(database);
buildNewMetaDataContext(databaseName, resourceSwitchManager.switchByUnregisterStorageUnit(database.getResourceMetaData(), Collections.singleton(storageUnitName)));
} catch (final SQLException ex) {
log.error("Alter database: {} register storage unit failed.", databaseName, ex);
}
Expand All @@ -118,8 +117,8 @@ private Collection<ShardingSphereSchema> buildSchemas(final ShardingSphereDataba
}

@SneakyThrows(Exception.class)
private void closeStaleRules(final String databaseName) {
for (ShardingSphereRule each : metaDataContexts.getMetaData().getDatabase(databaseName).getRuleMetaData().getRules()) {
private void closeStaleRules(final ShardingSphereDatabase database) {
for (ShardingSphereRule each : database.getRuleMetaData().getRules()) {
if (each instanceof AutoCloseable) {
((AutoCloseable) each).close();
}
Expand Down