Skip to content

Commit

Permalink
Refactor alterRuleConfiguration and removeRuleConfigurationItem inter…
Browse files Browse the repository at this point in the history
…face in StandaloneModeContextManager (#31347)

* Refactor alterRuleConfiguration and removeRuleConfigurationItem interface in StandaloneModeContextManager

* Fix checkstyle

* Fix checkstyle

* Fix checkstyle
  • Loading branch information
zhaojinchao95 authored May 23, 2024
1 parent bae03f1 commit eca90be
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.event.NamedRuleItemChangedEventCreator;
import org.apache.shardingsphere.mode.event.UniqueRuleItemChangedEventCreator;
import org.apache.shardingsphere.mode.spi.RuleNodePathProvider;
Expand Down Expand Up @@ -54,7 +55,7 @@ public Optional<GovernanceEvent> build(final String databaseName, final DataChan
}

private Optional<GovernanceEvent> build(final RuleNodePath ruleNodePath, final String databaseName, final DataChangedEvent event) {
if (!ruleNodePath.getRoot().isValidatedPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
if (!ruleNodePath.getRoot().isValidatedPath(event.getKey()) || Type.DELETED != event.getType() && Strings.isNullOrEmpty(event.getValue())) {
return Optional.empty();
}
for (Entry<String, NamedRuleItemNodePath> entry : ruleNodePath.getNamedItems().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,23 @@ private void sendDatabaseRuleChangedEvent(final String databaseName, final MetaD
@Override
public void removeRuleConfigurationItem(final String databaseName, final RuleConfiguration toBeRemovedRuleConfig) {
if (null != toBeRemovedRuleConfig) {
sendDatabaseRuleChangedEvent(databaseName,
sendDatabaseRuleDeletedEvent(databaseName,
contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().deleteConfigurations(databaseName, Collections.singleton(toBeRemovedRuleConfig)));
clearServiceCache();
}
}

private void sendDatabaseRuleDeletedEvent(final String databaseName, final Collection<MetaDataVersion> metaDataVersions) {
for (MetaDataVersion each : metaDataVersions) {
sendDatabaseRuleDeletedEvent(databaseName, each);
}
}

private void sendDatabaseRuleDeletedEvent(final String databaseName, final MetaDataVersion metaDataVersion) {
ruleConfigurationEventBuilder.build(databaseName, new DataChangedEvent(metaDataVersion.getActiveVersionNodePath(), "", Type.DELETED))
.ifPresent(optional -> contextManager.getComputeNodeInstanceContext().getEventBusContext().post(optional));
}

@Override
public void removeRuleConfiguration(final String databaseName, final String ruleName) {
contextManager.getMetaDataContexts().getPersistService().getDatabaseRulePersistService().delete(databaseName, ruleName);
Expand Down

0 comments on commit eca90be

Please sign in to comment.