Skip to content

Commit

Permalink
Refactor ResourceHeldRuleAttribute (#30518)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Mar 17, 2024
1 parent 097556f commit 0cc83bc
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public void dropDatabase(final String databaseName) {
}

private void cleanResources(final ShardingSphereDatabase database) {
globalRuleMetaData.getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeStaleResource(database.getName()));
database.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeStaleResource(database.getName()));
globalRuleMetaData.getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeResource(database.getName()));
database.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(each -> each.closeResource(database.getName()));
database.getRuleMetaData().getAttributes(StaticDataSourceRuleAttribute.class).forEach(StaticDataSourceRuleAttribute::cleanStorageNodeDataSources);
Optional.ofNullable(database.getResourceMetaData())
.ifPresent(optional -> optional.getStorageUnits().values().forEach(each -> new DataSourcePoolDestroyer(each.getDataSource()).asyncDestroy()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public interface ResourceHeldRuleAttribute<T> extends RuleAttribute {
void addResource(ShardingSphereDatabase database);

/**
* Close stale resource with database name.
* Close resource.
*
* @param databaseName database name
*/
void closeStaleResource(String databaseName);
void closeResource(String databaseName);

/**
* Close stale resource.
* Close stale resources.
*/
void closeStaleResource();
void closeStaleResources();
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ void assertDropDatabase() {
assertTrue(metaData.getDatabases().isEmpty());
Awaitility.await().pollDelay(10L, TimeUnit.MILLISECONDS).until(dataSource::isClosed);
assertTrue(dataSource.isClosed());
verify(databaseResourceHeldRuleAttribute).closeStaleResource("foo_db");
verify(globalResourceHeldRuleAttribute).closeStaleResource("foo_db");
verify(databaseResourceHeldRuleAttribute).closeResource("foo_db");
verify(globalResourceHeldRuleAttribute).closeResource("foo_db");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public synchronized void addResource(final ShardingSphereDatabase database) {
}

@Override
public synchronized void closeStaleResource(final String databaseName) {
public synchronized void closeResource(final String databaseName) {
if (!databases.containsKey(databaseName.toLowerCase())) {
return;
}
Expand All @@ -76,7 +76,7 @@ public synchronized void closeStaleResource(final String databaseName) {
}

@Override
public synchronized void closeStaleResource() {
public synchronized void closeStaleResources() {
databases.clear();
closeEngine();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void assertAddResource() {
@Test
void assertCloseStaleResource() {
TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase()));
actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeStaleResource(SHARDING_DB_1);
actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeResource(SHARDING_DB_1);
assertTrue(actual.getDatabases().isEmpty());
assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA),
instanceOf(ShardingSphereTransactionManagerFixture.class));
Expand All @@ -89,7 +89,7 @@ void assertCloseStaleResource() {
@Test
void assertCloseAllStaleResources() {
TransactionRule actual = new TransactionRule(createTransactionRuleConfiguration(), Collections.singletonMap(SHARDING_DB_1, createDatabase()));
actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeStaleResource();
actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).closeStaleResources();
assertTrue(actual.getDatabases().isEmpty());
assertThat(((ShardingSphereTransactionManagerEngine) actual.getAttributes().getAttribute(ResourceHeldRuleAttribute.class).getResource()).getTransactionManager(TransactionType.XA),
instanceOf(ShardingSphereTransactionManagerFixture.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public final class ConfigurationContextManager {
public synchronized void registerStorageUnit(final String databaseName, final Map<String, DataSourcePoolProperties> propsMap) {
try {
Collection<ResourceHeldRuleAttribute> staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResources);
SwitchingResource switchingResource =
new NewResourceSwitchManager().registerStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap);
buildNewMetaDataContext(databaseName, switchingResource);
Expand All @@ -100,7 +100,7 @@ public synchronized void registerStorageUnit(final String databaseName, final Ma
public synchronized void alterStorageUnit(final String databaseName, final Map<String, DataSourcePoolProperties> propsMap) {
try {
Collection<ResourceHeldRuleAttribute> staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResources);
SwitchingResource switchingResource =
new NewResourceSwitchManager().alterStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap);
buildNewMetaDataContext(databaseName, switchingResource);
Expand All @@ -119,7 +119,7 @@ public synchronized void alterStorageUnit(final String databaseName, final Map<S
public synchronized void unregisterStorageUnit(final String databaseName, final String storageUnitName) {
try {
Collection<ResourceHeldRuleAttribute> staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResources);
SwitchingResource switchingResource = new NewResourceSwitchManager().unregisterStorageUnit(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(),
Collections.singletonList(storageUnitName));
buildNewMetaDataContext(databaseName, switchingResource);
Expand Down Expand Up @@ -153,7 +153,7 @@ public synchronized void alterRuleConfiguration(final String databaseName, final
try {
Collection<ResourceHeldRuleAttribute> staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName);
// TODO consider rename this method to alterDatabaseRuleConfiguration
staleResourceHeldRuleAttributes.stream().filter(DatabaseRule.class::isInstance).forEach(ResourceHeldRuleAttribute::closeStaleResource);
staleResourceHeldRuleAttributes.stream().filter(DatabaseRule.class::isInstance).forEach(ResourceHeldRuleAttribute::closeStaleResources);
MetaDataContexts reloadMetaDataContexts = createMetaDataContextsWhenRuleChanged(databaseName, false, null, ruleConfigs);
alterSchemaMetaData(databaseName, reloadMetaDataContexts.getMetaData().getDatabase(databaseName), metaDataContexts.get().getMetaData().getDatabase(databaseName));
metaDataContexts.set(reloadMetaDataContexts);
Expand Down Expand Up @@ -236,7 +236,7 @@ private MetaDataContexts createMetaDataContextsByAlterRule(final String database
public synchronized void alterDataSourceUnitsConfiguration(final String databaseName, final Map<String, DataSourcePoolProperties> propsMap) {
try {
Collection<ResourceHeldRuleAttribute> staleResourceHeldRuleAttributes = getStaleResourceHeldRules(databaseName);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResource);
staleResourceHeldRuleAttributes.forEach(ResourceHeldRuleAttribute::closeStaleResources);
SwitchingResource switchingResource =
new ResourceSwitchManager().createByAlterDataSourcePoolProperties(metaDataContexts.get().getMetaData().getDatabase(databaseName).getResourceMetaData(), propsMap);
metaDataContexts.get().getMetaData().getDatabases().putAll(renewDatabase(metaDataContexts.get().getMetaData().getDatabase(databaseName), switchingResource));
Expand Down Expand Up @@ -422,7 +422,7 @@ public synchronized void alterGlobalRuleConfiguration(final Collection<RuleConfi
if (ruleConfigs.isEmpty()) {
return;
}
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResource);
metaDataContexts.get().getMetaData().getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResources);
RuleMetaData toBeChangedGlobalRuleMetaData = new RuleMetaData(
GlobalRulesBuilder.buildRules(ruleConfigs, metaDataContexts.get().getMetaData().getDatabases(), metaDataContexts.get().getMetaData().getProps()));
ShardingSphereMetaData toBeChangedMetaData = new ShardingSphereMetaData(metaDataContexts.get().getMetaData().getDatabases(), metaDataContexts.get().getMetaData().getGlobalResourceMetaData(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ private void useLoadedToReplaceInitBySchemaData(final ShardingSphereSchemaData i
@Override
public void close() {
persistService.getRepository().close();
metaData.getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResource);
metaData.getGlobalRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResources);
for (ShardingSphereDatabase each : metaData.getDatabases().values()) {
each.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResource);
each.getRuleMetaData().getAttributes(ResourceHeldRuleAttribute.class).forEach(ResourceHeldRuleAttribute::closeStaleResources);
}
}
}

0 comments on commit 0cc83bc

Please sign in to comment.