You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2023/06/24 13:12:01 UTC
[shardingsphere] branch master updated: Refactor shadow event & subscriber (#26488)
This is an automated email from the ASF dual-hosted git repository.
zhaojinchao 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 c065afd4bbe Refactor shadow event & subscriber (#26488)
c065afd4bbe is described below
commit c065afd4bbe5bf73bdade4cc2fbb3c413e139817
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Sat Jun 24 21:11:55 2023 +0800
Refactor shadow event & subscriber (#26488)
---
.../event/ShadowRuleConfigurationEventBuilder.java | 67 ++++++----------------
.../event/algorithm/AlterShadowAlgorithmEvent.java | 3 -
.../algorithm/DeleteShadowAlgorithmEvent.java | 4 --
.../AddShadowDataSourceEvent.java} | 6 +-
.../AlterShadowDataSourceEvent.java} | 7 +--
.../DeleteShadowDataSourceEvent.java} | 13 ++---
.../shadow/event/table/AddShadowTableEvent.java | 3 -
.../shadow/event/table/AlterShadowTableEvent.java | 3 -
.../shadow/event/table/DeleteShadowTableEvent.java | 4 --
.../metadata/converter/ShadowNodeConverter.java | 44 +++++++-------
.../subscriber/ShadowAlgorithmSubscriber.java | 11 +++-
...criber.java => ShadowDataSourceSubscriber.java} | 30 ++++++----
.../shadow/subscriber/ShadowTableSubscriber.java | 13 ++++-
...nfra.rule.RuleConfigurationSubscribeCoordinator | 2 +-
.../converter/ShadowNodeConverterTest.java | 12 ++--
15 files changed, 95 insertions(+), 127 deletions(-)
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/ShadowRuleConfigurationEventBuilder.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/ShadowRuleConfigurationEventBuilder.java
index 24640b86aea..03ccd1d7d80 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/ShadowRuleConfigurationEventBuilder.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/ShadowRuleConfigurationEventBuilder.java
@@ -18,28 +18,19 @@
package org.apache.shardingsphere.shadow.event;
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.spi.RuleConfigurationEventBuilder;
-import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.event.algorithm.AlterShadowAlgorithmEvent;
import org.apache.shardingsphere.shadow.event.algorithm.DeleteShadowAlgorithmEvent;
-import org.apache.shardingsphere.shadow.event.config.AddShadowConfigurationEvent;
-import org.apache.shardingsphere.shadow.event.config.AlterShadowConfigurationEvent;
-import org.apache.shardingsphere.shadow.event.config.DeleteShadowConfigurationEvent;
+import org.apache.shardingsphere.shadow.event.datasource.AddShadowDataSourceEvent;
+import org.apache.shardingsphere.shadow.event.datasource.AlterShadowDataSourceEvent;
+import org.apache.shardingsphere.shadow.event.datasource.DeleteShadowDataSourceEvent;
import org.apache.shardingsphere.shadow.event.table.AddShadowTableEvent;
import org.apache.shardingsphere.shadow.event.table.AlterShadowTableEvent;
import org.apache.shardingsphere.shadow.event.table.DeleteShadowTableEvent;
import org.apache.shardingsphere.shadow.metadata.converter.ShadowNodeConverter;
-import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration;
-import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfiguration;
-import org.apache.shardingsphere.shadow.yaml.swapper.table.YamlShadowTableConfigurationSwapper;
import java.util.Optional;
@@ -53,67 +44,45 @@ public final class ShadowRuleConfigurationEventBuilder implements RuleConfigurat
if (!ShadowNodeConverter.isShadowPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
return Optional.empty();
}
- Optional<String> dataSourceName = ShadowNodeConverter.getDataSourceName(event.getKey());
+ Optional<String> dataSourceName = ShadowNodeConverter.getDataSourceNameByActiveVersionPath(event.getKey());
if (dataSourceName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
- Optional<String> dataSourceVersion = ShadowNodeConverter.getDataSourceVersion(event.getKey());
- if (dataSourceVersion.isPresent()) {
- return createShadowConfigEvent(databaseName, dataSourceName.get(), dataSourceVersion.get(), event);
- }
+ return createShadowConfigEvent(databaseName, dataSourceName.get(), event);
}
Optional<String> tableName = ShadowNodeConverter.getTableName(event.getKey());
if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
- Optional<String> tableVersion = ShadowNodeConverter.getTableVersion(event.getKey());
- if (tableVersion.isPresent()) {
- return createShadowTableConfigEvent(databaseName, tableName.get(), tableVersion.get(), event);
- }
+ return createShadowTableConfigEvent(databaseName, tableName.get(), event);
}
Optional<String> algorithmName = ShadowNodeConverter.getAlgorithmName(event.getKey());
if (algorithmName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
- Optional<String> algorithmVersion = ShadowNodeConverter.getAlgorithmVersion(event.getKey());
- if (algorithmVersion.isPresent()) {
- return createShadowAlgorithmEvent(databaseName, algorithmName.get(), algorithmVersion.get(), event);
- }
+ return createShadowAlgorithmEvent(databaseName, algorithmName.get(), event);
}
return Optional.empty();
}
- private Optional<GovernanceEvent> createShadowConfigEvent(final String databaseName, final String dataSourceName, final String version, final DataChangedEvent event) {
+ private Optional<GovernanceEvent> createShadowConfigEvent(final String databaseName, final String dataSourceName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
- return Optional.of(new AddShadowConfigurationEvent(databaseName, swapShadowDataSourceRuleConfig(dataSourceName, event.getValue()), event.getKey(), version));
+ return Optional.of(new AddShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue()));
}
if (Type.UPDATED == event.getType()) {
- return Optional.of(new AlterShadowConfigurationEvent(databaseName, dataSourceName, swapShadowDataSourceRuleConfig(dataSourceName, event.getValue()), event.getKey(), version));
+ return Optional.of(new AlterShadowDataSourceEvent(databaseName, dataSourceName, event.getKey(), event.getValue()));
}
- return Optional.of(new DeleteShadowConfigurationEvent(databaseName, dataSourceName, event.getKey(), version));
+ return Optional.of(new DeleteShadowDataSourceEvent(databaseName, dataSourceName));
}
- private ShadowDataSourceConfiguration swapShadowDataSourceRuleConfig(final String dataSourceName, final String yamlContext) {
- YamlShadowDataSourceConfiguration yamlConfig = YamlEngine.unmarshal(yamlContext, YamlShadowDataSourceConfiguration.class);
- return new ShadowDataSourceConfiguration(dataSourceName, yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName());
- }
-
- private Optional<GovernanceEvent> createShadowTableConfigEvent(final String databaseName, final String tableName, final String version, final DataChangedEvent event) {
+ private Optional<GovernanceEvent> createShadowTableConfigEvent(final String databaseName, final String tableName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
- return Optional.of(new AddShadowTableEvent(databaseName, tableName, swapToTableConfig(event.getValue()), event.getKey(), version));
+ return Optional.of(new AddShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue()));
}
if (Type.UPDATED == event.getType()) {
- return Optional.of(new AlterShadowTableEvent(databaseName, tableName, swapToTableConfig(event.getValue()), event.getKey(), version));
+ return Optional.of(new AlterShadowTableEvent(databaseName, tableName, event.getKey(), event.getValue()));
}
- return Optional.of(new DeleteShadowTableEvent(databaseName, tableName, event.getKey(), version));
- }
-
- private ShadowTableConfiguration swapToTableConfig(final String yamlContext) {
- return new YamlShadowTableConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlShadowTableConfiguration.class));
+ return Optional.of(new DeleteShadowTableEvent(databaseName, tableName));
}
- private Optional<GovernanceEvent> createShadowAlgorithmEvent(final String databaseName, final String algorithmName, final String version, final DataChangedEvent event) {
+ private Optional<GovernanceEvent> createShadowAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
if (Type.ADDED == event.getType() || Type.UPDATED == event.getType()) {
- return Optional.of(new AlterShadowAlgorithmEvent(databaseName, algorithmName, swapToAlgorithmConfig(event.getValue()), event.getKey(), version));
+ return Optional.of(new AlterShadowAlgorithmEvent(databaseName, algorithmName, event.getKey(), event.getValue()));
}
- return Optional.of(new DeleteShadowAlgorithmEvent(databaseName, algorithmName, event.getKey(), version));
- }
-
- private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
- return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
+ return Optional.of(new DeleteShadowAlgorithmEvent(databaseName, algorithmName));
}
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/AlterShadowAlgorithmEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/AlterShadowAlgorithmEvent.java
index a26cb5d37e5..dd5e53cdc20 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/AlterShadowAlgorithmEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/AlterShadowAlgorithmEvent.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.event.algorithm;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
/**
@@ -33,8 +32,6 @@ public final class AlterShadowAlgorithmEvent implements GovernanceEvent {
private final String algorithmName;
- private final AlgorithmConfiguration config;
-
private final String activeVersionKey;
private final String activeVersion;
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/DeleteShadowAlgorithmEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/DeleteShadowAlgorithmEvent.java
index 1e995d6bf2e..8f4a2365ac5 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/DeleteShadowAlgorithmEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/algorithm/DeleteShadowAlgorithmEvent.java
@@ -31,8 +31,4 @@ public final class DeleteShadowAlgorithmEvent implements GovernanceEvent {
private final String databaseName;
private final String algorithmName;
-
- private final String activeVersionKey;
-
- private final String activeVersion;
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/DeleteShadowConfigurationEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AddShadowDataSourceEvent.java
similarity index 87%
rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/DeleteShadowConfigurationEvent.java
rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AddShadowDataSourceEvent.java
index 73312231953..149c153ac55 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/DeleteShadowConfigurationEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AddShadowDataSourceEvent.java
@@ -15,18 +15,18 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.event.config;
+package org.apache.shardingsphere.shadow.event.datasource;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
/**
- * Delete shadow configuration event.
+ * Add shadow configuration event.
*/
@RequiredArgsConstructor
@Getter
-public final class DeleteShadowConfigurationEvent implements GovernanceEvent {
+public final class AddShadowDataSourceEvent implements GovernanceEvent {
private final String databaseName;
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AlterShadowConfigurationEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AlterShadowDataSourceEvent.java
similarity index 80%
rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AlterShadowConfigurationEvent.java
rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AlterShadowDataSourceEvent.java
index 2c93fb437e6..5e0bc1dd94b 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AlterShadowConfigurationEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/AlterShadowDataSourceEvent.java
@@ -15,26 +15,23 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.event.config;
+package org.apache.shardingsphere.shadow.event.datasource;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
-import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
/**
* Alter shadow configuration event.
*/
@RequiredArgsConstructor
@Getter
-public final class AlterShadowConfigurationEvent implements GovernanceEvent {
+public final class AlterShadowDataSourceEvent implements GovernanceEvent {
private final String databaseName;
private final String dataSourceName;
- private final ShadowDataSourceConfiguration config;
-
private final String activeVersionKey;
private final String activeVersion;
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AddShadowConfigurationEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/DeleteShadowDataSourceEvent.java
similarity index 72%
rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AddShadowConfigurationEvent.java
rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/DeleteShadowDataSourceEvent.java
index 7d1998df59a..1c863d089e3 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/config/AddShadowConfigurationEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/datasource/DeleteShadowDataSourceEvent.java
@@ -15,25 +15,20 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.shadow.event.config;
+package org.apache.shardingsphere.shadow.event.datasource;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
-import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
/**
- * Add shadow configuration event.
+ * Delete shadow configuration event.
*/
@RequiredArgsConstructor
@Getter
-public final class AddShadowConfigurationEvent implements GovernanceEvent {
+public final class DeleteShadowDataSourceEvent implements GovernanceEvent {
private final String databaseName;
- private final ShadowDataSourceConfiguration config;
-
- private final String activeVersionKey;
-
- private final String activeVersion;
+ private final String dataSourceName;
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AddShadowTableEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AddShadowTableEvent.java
index db2dea1a69b..dbbb1eb9551 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AddShadowTableEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AddShadowTableEvent.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.shadow.event.table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
-import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
/**
* Add shadow table event.
@@ -33,8 +32,6 @@ public final class AddShadowTableEvent implements GovernanceEvent {
private final String tableName;
- private final ShadowTableConfiguration config;
-
private final String activeVersionKey;
private final String activeVersion;
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AlterShadowTableEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AlterShadowTableEvent.java
index cf5c2c26869..ae5d45b5ba8 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AlterShadowTableEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/AlterShadowTableEvent.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.shadow.event.table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
-import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
/**
* Alter shadow table event.
@@ -33,8 +32,6 @@ public final class AlterShadowTableEvent implements GovernanceEvent {
private final String tableName;
- private final ShadowTableConfiguration config;
-
private final String activeVersionKey;
private final String activeVersion;
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/DeleteShadowTableEvent.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/DeleteShadowTableEvent.java
index ea4772f8de0..583e0e4f662 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/DeleteShadowTableEvent.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/event/table/DeleteShadowTableEvent.java
@@ -31,8 +31,4 @@ public final class DeleteShadowTableEvent implements GovernanceEvent {
private final String databaseName;
private final String tableName;
-
- private final String activeVersionKey;
-
- private final String activeVersion;
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverter.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverter.java
index 39b9aeee763..14f6bf9bf96 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverter.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverter.java
@@ -44,7 +44,7 @@ public final class ShadowNodeConverter {
private static final String RULE_NAME_PATTERN = "/([\\w\\-]+)?";
- private static final String RULE_VERSION = "/([\\w\\-]+)/versions/([\\w\\-]+)$";
+ private static final String RULE_ACTIVE_VERSION = "/([\\w\\-]+)/active_version$";
/**
* Get data source path.
@@ -182,38 +182,38 @@ public final class ShadowNodeConverter {
}
/**
- * Get data source version.
+ * Get data source name by active version path.
*
- * @param rulePath rule path
- * @return data source version
+ * @param activeVersionPath rule path
+ * @return data source name
*/
- public static Optional<String> getDataSourceVersion(final String rulePath) {
- Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + DATA_SOURCES_NODE + RULE_VERSION, Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rulePath);
- return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty();
+ public static Optional<String> getDataSourceNameByActiveVersionPath(final String activeVersionPath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + DATA_SOURCES_NODE + RULE_ACTIVE_VERSION, Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(activeVersionPath);
+ return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
/**
- * Get table version.
+ * Get table name by active version path.
*
- * @param rulePath rule path
- * @return table version
+ * @param activeVersionPath rule path
+ * @return table name
*/
- public static Optional<String> getTableVersion(final String rulePath) {
- Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + TABLES_NODE + RULE_VERSION, Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rulePath);
- return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty();
+ public static Optional<String> getTableNameByActiveVersionPath(final String activeVersionPath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + TABLES_NODE + RULE_ACTIVE_VERSION, Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(activeVersionPath);
+ return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
/**
- * Get algorithm version.
+ * Get algorithm name by active version path.
*
- * @param rulePath rule path
- * @return algorithm version
+ * @param activeVersionPath rule path
+ * @return algorithm name
*/
- public static Optional<String> getAlgorithmVersion(final String rulePath) {
- Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + ALGORITHMS_NODE + RULE_VERSION, Pattern.CASE_INSENSITIVE);
- Matcher matcher = pattern.matcher(rulePath);
- return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty();
+ public static Optional<String> getAlgorithmNameByActiveVersionPath(final String activeVersionPath) {
+ Pattern pattern = Pattern.compile(RULES_NODE_PREFIX + ROOT_NODE + "/" + ALGORITHMS_NODE + RULE_ACTIVE_VERSION, Pattern.CASE_INSENSITIVE);
+ Matcher matcher = pattern.matcher(activeVersionPath);
+ return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
}
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 26ac490c7f6..c4220752786 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
@@ -19,9 +19,13 @@ package org.apache.shardingsphere.shadow.subscriber;
import com.google.common.eventbus.Subscribe;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator;
+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.config.DatabaseRuleConfigurationChangedEvent;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.event.algorithm.AlterShadowAlgorithmEvent;
@@ -57,7 +61,8 @@ public final class ShadowAlgorithmSubscriber implements RuleConfigurationSubscri
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(), event.getConfig());
+ config.getShadowAlgorithms().put(event.getAlgorithmName(), swapToAlgorithmConfig(
+ instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())));
}
/**
@@ -72,4 +77,8 @@ public final class ShadowAlgorithmSubscriber implements RuleConfigurationSubscri
config.getShadowAlgorithms().remove(event.getAlgorithmName());
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
+
+ private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
+ return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
+ }
}
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowConfigurationSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
similarity index 73%
rename from features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowConfigurationSubscriber.java
rename to features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
index 37c30c9a773..241aafcc0b5 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowConfigurationSubscriber.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java
@@ -22,13 +22,15 @@ import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
-import org.apache.shardingsphere.shadow.event.config.AddShadowConfigurationEvent;
-import org.apache.shardingsphere.shadow.event.config.AlterShadowConfigurationEvent;
-import org.apache.shardingsphere.shadow.event.config.DeleteShadowConfigurationEvent;
+import org.apache.shardingsphere.shadow.event.datasource.AddShadowDataSourceEvent;
+import org.apache.shardingsphere.shadow.event.datasource.AlterShadowDataSourceEvent;
+import org.apache.shardingsphere.shadow.event.datasource.DeleteShadowDataSourceEvent;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration;
import java.util.Map;
import java.util.Optional;
@@ -38,7 +40,7 @@ import java.util.Optional;
*/
@SuppressWarnings("UnstableApiUsage")
@RequiredArgsConstructor
-public final class ShadowConfigurationSubscriber implements RuleConfigurationSubscribeCoordinator {
+public final class ShadowDataSourceSubscriber implements RuleConfigurationSubscribeCoordinator {
private Map<String, ShardingSphereDatabase> databases;
@@ -57,12 +59,13 @@ public final class ShadowConfigurationSubscriber implements RuleConfigurationSub
* @param event add shadow configuration event
*/
@Subscribe
- public synchronized void renew(final AddShadowConfigurationEvent event) {
+ public synchronized void renew(final AddShadowDataSourceEvent event) {
if (!event.getActiveVersion().equals(instanceContext.getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) {
return;
}
ShardingSphereDatabase database = databases.get(event.getDatabaseName());
- ShadowDataSourceConfiguration needToAddedConfig = event.getConfig();
+ ShadowDataSourceConfiguration needToAddedConfig = swapShadowDataSourceRuleConfig(event.getDataSourceName(),
+ instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()));
Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
ShadowRuleConfiguration config;
if (rule.isPresent()) {
@@ -82,12 +85,13 @@ public final class ShadowConfigurationSubscriber implements RuleConfigurationSub
* @param event alter shadow configuration event
*/
@Subscribe
- public synchronized void renew(final AlterShadowConfigurationEvent event) {
+ public synchronized void renew(final AlterShadowDataSourceEvent event) {
if (!event.getActiveVersion().equals(instanceContext.getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) {
return;
}
ShardingSphereDatabase database = databases.get(event.getDatabaseName());
- ShadowDataSourceConfiguration needToAlteredConfig = event.getConfig();
+ ShadowDataSourceConfiguration needToAlteredConfig = swapShadowDataSourceRuleConfig(event.getDataSourceName(),
+ instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()));
ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration();
config.getDataSources().removeIf(each -> each.getName().equals(event.getDataSourceName()));
config.getDataSources().add(needToAlteredConfig);
@@ -100,13 +104,15 @@ public final class ShadowConfigurationSubscriber implements RuleConfigurationSub
* @param event delete shadow configuration event
*/
@Subscribe
- public synchronized void renew(final DeleteShadowConfigurationEvent event) {
- if (!event.getActiveVersion().equals(instanceContext.getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) {
- return;
- }
+ public synchronized void renew(final DeleteShadowDataSourceEvent event) {
ShardingSphereDatabase database = databases.get(event.getDatabaseName());
ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration();
config.getDataSources().removeIf(each -> each.getName().equals(event.getDataSourceName()));
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
+
+ private ShadowDataSourceConfiguration swapShadowDataSourceRuleConfig(final String dataSourceName, final String yamlContext) {
+ YamlShadowDataSourceConfiguration yamlConfig = YamlEngine.unmarshal(yamlContext, YamlShadowDataSourceConfiguration.class);
+ return new ShadowDataSourceConfiguration(dataSourceName, yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName());
+ }
}
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 6fa0a6c4ad5..6efc8d1410c 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
@@ -22,6 +22,7 @@ import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
@@ -29,6 +30,8 @@ import org.apache.shardingsphere.shadow.event.table.AddShadowTableEvent;
import org.apache.shardingsphere.shadow.event.table.AlterShadowTableEvent;
import org.apache.shardingsphere.shadow.event.table.DeleteShadowTableEvent;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.shadow.yaml.config.table.YamlShadowTableConfiguration;
+import org.apache.shardingsphere.shadow.yaml.swapper.table.YamlShadowTableConfigurationSwapper;
import java.util.Map;
@@ -57,7 +60,8 @@ public final class ShadowTableSubscriber implements RuleConfigurationSubscribeCo
*/
@Subscribe
public synchronized void renew(final AddShadowTableEvent event) {
- renew(event.getDatabaseName(), event.getTableName(), event.getConfig());
+ renew(event.getDatabaseName(), event.getTableName(), swapToTableConfig(
+ instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())));
}
/**
@@ -67,7 +71,8 @@ public final class ShadowTableSubscriber implements RuleConfigurationSubscribeCo
*/
@Subscribe
public synchronized void renew(final AlterShadowTableEvent event) {
- renew(event.getDatabaseName(), event.getTableName(), event.getConfig());
+ renew(event.getDatabaseName(), event.getTableName(), swapToTableConfig(
+ instanceContext.getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())));
}
private void renew(final String databaseName, final String tableName, final ShadowTableConfiguration tableConfig) {
@@ -89,4 +94,8 @@ public final class ShadowTableSubscriber implements RuleConfigurationSubscribeCo
config.getTables().remove(event.getTableName());
instanceContext.getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config));
}
+
+ private ShadowTableConfiguration swapToTableConfig(final String yamlContext) {
+ return new YamlShadowTableConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlShadowTableConfiguration.class));
+ }
}
diff --git a/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator b/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
index 7685f858e8f..b78e6e0b888 100644
--- a/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
+++ b/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
@@ -15,6 +15,6 @@
# limitations under the License.
#
-org.apache.shardingsphere.shadow.subscriber.ShadowConfigurationSubscriber
+org.apache.shardingsphere.shadow.subscriber.ShadowDataSourceSubscriber
org.apache.shardingsphere.shadow.subscriber.ShadowTableSubscriber
org.apache.shardingsphere.shadow.subscriber.ShadowAlgorithmSubscriber
diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverterTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverterTest.java
index 7c006b126cb..db02a031518 100644
--- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverterTest.java
+++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/metadata/converter/ShadowNodeConverterTest.java
@@ -85,22 +85,22 @@ class ShadowNodeConverterTest {
@Test
void assertGetDataSourceVersion() {
- Optional<String> actual = ShadowNodeConverter.getDataSourceVersion("/metadata/foo_db/rules/shadow/data_sources/shadow_database/versions/1");
+ Optional<String> actual = ShadowNodeConverter.getDataSourceNameByActiveVersionPath("/metadata/foo_db/rules/shadow/data_sources/shadow_database/active_version");
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("1"));
+ assertThat(actual.get(), is("shadow_database"));
}
@Test
void assertGetTableVersion() {
- Optional<String> actual = ShadowNodeConverter.getTableVersion("/metadata/foo_db/rules/shadow/tables/shadow_table/versions/1");
+ Optional<String> actual = ShadowNodeConverter.getTableNameByActiveVersionPath("/metadata/foo_db/rules/shadow/tables/shadow_table/active_version");
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("1"));
+ assertThat(actual.get(), is("shadow_table"));
}
@Test
void assertGetAlgorithmVersion() {
- Optional<String> actual = ShadowNodeConverter.getAlgorithmVersion("/metadata/foo_db/rules/shadow/algorithms/user_id_insert_match_algorithm/versions/1");
+ Optional<String> actual = ShadowNodeConverter.getAlgorithmNameByActiveVersionPath("/metadata/foo_db/rules/shadow/algorithms/user_id_insert_match_algorithm/active_version");
assertTrue(actual.isPresent());
- assertThat(actual.get(), is("1"));
+ assertThat(actual.get(), is("user_id_insert_match_algorithm"));
}
}