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/10 11:42:31 UTC
[shardingsphere] branch master updated: The watch event parameter is adjusted to Object (#26254)
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 7f354a5df5f The watch event parameter is adjusted to Object (#26254)
7f354a5df5f is described below
commit 7f354a5df5ff90c1ac38f5848e33cbce970dd2f2
Author: zhaojinchao <zh...@apache.org>
AuthorDate: Sat Jun 10 19:42:22 2023 +0800
The watch event parameter is adjusted to Object (#26254)
---
...riteSplittingRuleConfigurationEventBuilder.java | 27 +++++++++++++++-------
.../infra/converter/GlobalRuleNodeConverter.java | 13 +++++++++++
.../converter/GlobalRuleNodeConverterTest.java | 5 ++++
3 files changed, 37 insertions(+), 8 deletions(-)
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/event/ReadwriteSplittingRuleConfigurationEventBuilder.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/event/ReadwriteSplittingRuleConfigurationEventBuilder.java
index df689860622..29aa9c7f414 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/event/ReadwriteSplittingRuleConfigurationEventBuilder.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/event/ReadwriteSplittingRuleConfigurationEventBuilder.java
@@ -21,6 +21,8 @@ import com.google.common.base.Strings;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder;
@@ -32,6 +34,8 @@ import org.apache.shardingsphere.readwritesplitting.event.loadbalance.AddLoadBal
import org.apache.shardingsphere.readwritesplitting.event.loadbalance.AlterLoadBalanceEvent;
import org.apache.shardingsphere.readwritesplitting.event.loadbalance.DeleteLoadBalanceEvent;
import org.apache.shardingsphere.readwritesplitting.metadata.converter.ReadwriteSplittingNodeConverter;
+import org.apache.shardingsphere.readwritesplitting.yaml.config.YamlReadwriteSplittingRuleConfiguration;
+import org.apache.shardingsphere.readwritesplitting.yaml.swapper.YamlReadwriteSplittingRuleConfigurationSwapper;
import java.util.Optional;
@@ -58,25 +62,32 @@ public final class ReadwriteSplittingRuleConfigurationEventBuilder implements Ru
private Optional<GovernanceEvent> createReadwriteSplittingConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
- return Optional.of(new AddReadwriteSplittingConfigurationEvent<>(databaseName,
- YamlEngine.unmarshal(event.getValue(), ReadwriteSplittingDataSourceRuleConfiguration.class)));
+ return Optional.of(new AddReadwriteSplittingConfigurationEvent<>(databaseName, swapReadwriteSplittingDataSourceRuleConfig(event.getValue())));
}
if (Type.UPDATED == event.getType()) {
- return Optional.of(new AlterReadwriteSplittingConfigurationEvent<>(databaseName, groupName,
- YamlEngine.unmarshal(event.getValue(), ReadwriteSplittingDataSourceRuleConfiguration.class)));
+ return Optional.of(new AlterReadwriteSplittingConfigurationEvent<>(databaseName, groupName, swapReadwriteSplittingDataSourceRuleConfig(event.getValue())));
}
return Optional.of(new DeleteReadwriteSplittingConfigurationEvent(databaseName, groupName));
}
+ // TODO Consider extract ReadwriteSplittingDataSourceRuleConfigurationSwapper
+ private ReadwriteSplittingDataSourceRuleConfiguration swapReadwriteSplittingDataSourceRuleConfig(final String yamlContext) {
+ return new YamlReadwriteSplittingRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlReadwriteSplittingRuleConfiguration.class)).getDataSources().iterator().next();
+ }
+
private Optional<GovernanceEvent> createLoadBalanceEvent(final String databaseName, final String loadBalanceName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
- return Optional.of(new AddLoadBalanceEvent<>(databaseName, loadBalanceName,
- YamlEngine.unmarshal(event.getValue(), AlgorithmConfiguration.class)));
+ new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlAlgorithmConfiguration.class));
+
+ return Optional.of(new AddLoadBalanceEvent<>(databaseName, loadBalanceName, swapToAlgorithmConfig(event.getValue())));
}
if (Type.UPDATED == event.getType()) {
- return Optional.of(new AlterLoadBalanceEvent<>(databaseName, loadBalanceName,
- YamlEngine.unmarshal(event.getValue(), AlgorithmConfiguration.class)));
+ return Optional.of(new AlterLoadBalanceEvent<>(databaseName, loadBalanceName, swapToAlgorithmConfig(event.getValue())));
}
return Optional.of(new DeleteLoadBalanceEvent(databaseName, loadBalanceName));
}
+
+ private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
+ return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
+ }
}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverter.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverter.java
index 6f3284f124b..04da98a6f99 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverter.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverter.java
@@ -60,4 +60,17 @@ public final class GlobalRuleNodeConverter {
private static String getVersionsNode(final String ruleName) {
return String.join(getRootNode(ruleName), VERSIONS);
}
+
+ /**
+ * Is expect rule name.
+ *
+ * @param ruleName rule name
+ * @param rulePath rule path
+ * @return true or false
+ */
+ public static boolean isExpectRuleName(final String ruleName, final String rulePath) {
+ Pattern pattern = Pattern.compile(getRootNode(ruleName) + "\\.*", Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(rulePath);
+ return matcher.find();
+ }
}
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverterTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverterTest.java
index 2354b10e4f0..c7d911bffdb 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverterTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/converter/GlobalRuleNodeConverterTest.java
@@ -38,4 +38,9 @@ class GlobalRuleNodeConverterTest {
assertTrue(actual.isPresent());
assertThat(actual.get(), is("0"));
}
+
+ @Test
+ void assertIsExpectRuleName() {
+ assertTrue(GlobalRuleNodeConverter.isExpectRuleName("transaction", "/rules/transaction/versions/0"));
+ }
}