You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2023/06/27 03:52:15 UTC
[shardingsphere] branch master updated: Refactor shadow subscriber (#26605)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new ae2fdf09247 Refactor shadow subscriber (#26605)
ae2fdf09247 is described below
commit ae2fdf092472cc4d353e9bc384d8a97accc1ad71
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Tue Jun 27 11:52:08 2023 +0800
Refactor shadow subscriber (#26605)
* Refactor shadow subscriber
* Update code style
---
.../subscriber/DefaultShadowAlgorithmNameSubscriber.java | 10 ++++++++--
.../shadow/subscriber/ShadowAlgorithmSubscriber.java | 16 ++++++++++++----
.../shadow/subscriber/ShadowDataSourceSubscriber.java | 7 ++-----
.../shadow/subscriber/ShadowTableSubscriber.java | 14 ++++++++++----
4 files changed, 32 insertions(+), 15 deletions(-)
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java
index 9fa1b8c6f44..fb5ef540b57 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.shadow.event.algorithm.DeleteDefaultShadowAlgor
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Map;
+import java.util.Optional;
/**
* Default shadow algorithm name subscriber.
@@ -48,8 +49,13 @@ public final class DefaultShadowAlgorithmNameSubscriber implements RuleChangedSu
*/
@Subscribe
public synchronized void renew(final AlterDefaultShadowAlgorithmNameEvent event) {
- ShardingSphereDatabase database = databases.get(event.getDatabaseName());
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration();
+ Optional<ShadowRule> rule = databases.get(event.getDatabaseName()).getRuleMetaData().findSingleRule(ShadowRule.class);
+ ShadowRuleConfiguration config;
+ if (rule.isPresent()) {
+ config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ } else {
+ config = new ShadowRuleConfiguration();
+ }
config.setDefaultShadowAlgorithmName(instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()));
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscriber.java
index 9ba033ddb12..8a8e6b41072 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscriber.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscriber.java
@@ -33,6 +33,7 @@ import org.apache.shardingsphere.shadow.event.algorithm.DeleteShadowAlgorithmEve
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Map;
+import java.util.Optional;
/**
* Shadow algorithm subscriber.
@@ -52,10 +53,17 @@ public final class ShadowAlgorithmSubscriber implements RuleChangedSubscriber {
*/
@Subscribe
public synchronized void renew(final AlterShadowAlgorithmEvent event) {
- ShardingSphereDatabase database = databases.get(event.getDatabaseName());
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration();
- config.getShadowAlgorithms().put(event.getAlgorithmName(), swapToAlgorithmConfig(
- instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())));
+ AlgorithmConfiguration needToAlteredConfig =
+ swapToAlgorithmConfig(instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()));
+ Optional<ShadowRule> rule = databases.get(event.getDatabaseName()).getRuleMetaData().findSingleRule(ShadowRule.class);
+ ShadowRuleConfiguration config;
+ if (rule.isPresent()) {
+ config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ } else {
+ config = new ShadowRuleConfiguration();
+ }
+ config.getShadowAlgorithms().put(event.getAlgorithmName(), needToAlteredConfig);
+ instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
/**
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
index 3d877162b0b..dc591dfc937 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
@@ -56,19 +56,16 @@ public final class ShadowDataSourceSubscriber implements RuleChangedSubscriber {
if (!event.getActiveVersion().equals(instanceContext.getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) {
return;
}
- ShardingSphereDatabase database = databases.get(event.getDatabaseName());
ShadowDataSourceConfiguration needToAddedConfig = swapShadowDataSourceRuleConfig(event.getDataSourceName(),
instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()));
- Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
+ Optional<ShadowRule> rule = databases.get(event.getDatabaseName()).getRuleMetaData().findSingleRule(ShadowRule.class);
ShadowRuleConfiguration config;
if (rule.isPresent()) {
config = (ShadowRuleConfiguration) rule.get().getConfiguration();
- config.getDataSources().removeIf(each -> each.getName().equals(needToAddedConfig.getName()));
- config.getDataSources().add(needToAddedConfig);
} else {
config = new ShadowRuleConfiguration();
- config.getDataSources().add(needToAddedConfig);
}
+ config.getDataSources().add(needToAddedConfig);
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscriber.java
index 48f4fb584e7..5fdbcc239b2 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscriber.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscriber.java
@@ -34,6 +34,7 @@ import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfigu
import org.apache.shardingsphere.shadow.yaml.swapper.table.YamlShadowTableConfigurationSwapper;
import java.util.Map;
+import java.util.Optional;
/**
* Shadow table subscriber.
@@ -68,10 +69,15 @@ public final class ShadowTableSubscriber implements RuleChangedSubscriber {
instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())));
}
- private void renew(final String databaseName, final String tableName, final ShadowTableConfiguration tableConfig) {
- ShardingSphereDatabase database = databases.get(databaseName);
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration();
- config.getTables().put(tableName, tableConfig);
+ private void renew(final String databaseName, final String tableName, final ShadowTableConfiguration needToAlteredConfig) {
+ Optional<ShadowRule> rule = databases.get(databaseName).getRuleMetaData().findSingleRule(ShadowRule.class);
+ ShadowRuleConfiguration config;
+ if (rule.isPresent()) {
+ config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ } else {
+ config = new ShadowRuleConfiguration();
+ }
+ config.getTables().put(tableName, needToAlteredConfig);
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(databaseName, config));
}