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/11 15:54:59 UTC
[shardingsphere] branch master updated: Add feature configuration changed event and subscribe at mask module (#26267)
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 1816d7105e4 Add feature configuration changed event and subscribe at mask module (#26267)
1816d7105e4 is described below
commit 1816d7105e4bab9394189b20d50304325635dc5f
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Sun Jun 11 23:54:52 2023 +0800
Add feature configuration changed event and subscribe at mask module (#26267)
* Add feature configuration changed event and subscribe at mask module
* Fix comments
---
features/mask/core/pom.xml | 5 +
.../event/MaskRuleConfigurationEventBuilder.java | 90 ++++++++++++++++
.../event/algorithm/AddMaskAlgorithmEvent.java | 38 +++++++
.../event/algorithm/AlterMaskAlgorithmEvent.java | 38 +++++++
.../event/algorithm/DeleteMaskAlgorithmEvent.java | 34 +++++++
.../event/config/AddMaskConfigurationEvent.java | 36 +++++++
.../event/config/AlterMaskConfigurationEvent.java | 38 +++++++
.../event/config/DeleteMaskConfigurationEvent.java | 34 +++++++
.../mask/subscriber/MaskAlgorithmSubscriber.java | 101 ++++++++++++++++++
.../subscriber/MaskConfigurationSubscriber.java | 113 +++++++++++++++++++++
...nfra.rule.RuleConfigurationSubscribeCoordinator | 19 ++++
...mode.event.config.RuleConfigurationEventBuilder | 18 ++++
...riteSplittingRuleConfigurationEventBuilder.java | 2 -
13 files changed, 564 insertions(+), 2 deletions(-)
diff --git a/features/mask/core/pom.xml b/features/mask/core/pom.xml
index f863a2f057f..b2f8e4622c5 100644
--- a/features/mask/core/pom.xml
+++ b/features/mask/core/pom.xml
@@ -38,6 +38,11 @@
<artifactId>shardingsphere-infra-merge</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-mode-core</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/MaskRuleConfigurationEventBuilder.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/MaskRuleConfigurationEventBuilder.java
new file mode 100644
index 00000000000..75cb07648b7
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/MaskRuleConfigurationEventBuilder.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.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.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.event.algorithm.AddMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.event.algorithm.AlterMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.event.algorithm.DeleteMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.event.config.AddMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.event.config.AlterMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.event.config.DeleteMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.metadata.converter.MaskNodeConverter;
+import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.swapper.YamlMaskRuleConfigurationSwapper;
+import org.apache.shardingsphere.mode.event.DataChangedEvent;
+import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
+import org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder;
+
+import java.util.Optional;
+
+/**
+ * Mask rule configuration event builder.
+ */
+public final class MaskRuleConfigurationEventBuilder implements RuleConfigurationEventBuilder {
+
+ @Override
+ public Optional<GovernanceEvent> build(final String databaseName, final DataChangedEvent event) {
+ if (!MaskNodeConverter.isMaskPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
+ return Optional.empty();
+ }
+ Optional<String> tableName = MaskNodeConverter.getTableName(event.getKey());
+ if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
+ return createMaskConfigEvent(databaseName, tableName.get(), event);
+ }
+ Optional<String> algorithmName = MaskNodeConverter.getAlgorithmName(event.getKey());
+ if (algorithmName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
+ return createMaskAlgorithmEvent(databaseName, algorithmName.get(), event);
+ }
+ return Optional.empty();
+ }
+
+ private Optional<GovernanceEvent> createMaskConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
+ if (Type.ADDED == event.getType()) {
+ return Optional.of(new AddMaskConfigurationEvent<>(databaseName, swapMaskTableRuleConfig(event.getValue())));
+ }
+ if (Type.UPDATED == event.getType()) {
+ return Optional.of(new AlterMaskConfigurationEvent<>(databaseName, groupName, swapMaskTableRuleConfig(event.getValue())));
+ }
+ return Optional.of(new DeleteMaskConfigurationEvent(databaseName, groupName));
+ }
+
+ private MaskTableRuleConfiguration swapMaskTableRuleConfig(final String yamlContext) {
+ return new YamlMaskRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlMaskRuleConfiguration.class)).getTables().iterator().next();
+ }
+
+ private Optional<GovernanceEvent> createMaskAlgorithmEvent(final String databaseName, final String algorithmName, final DataChangedEvent event) {
+ if (Type.ADDED == event.getType()) {
+ return Optional.of(new AddMaskAlgorithmEvent<>(databaseName, algorithmName, swapToAlgorithmConfig(event.getValue())));
+ }
+ if (Type.UPDATED == event.getType()) {
+ return Optional.of(new AlterMaskAlgorithmEvent<>(databaseName, algorithmName, swapToAlgorithmConfig(event.getValue())));
+ }
+ return Optional.of(new DeleteMaskAlgorithmEvent(databaseName, algorithmName));
+ }
+
+ private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
+ return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AddMaskAlgorithmEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AddMaskAlgorithmEvent.java
new file mode 100644
index 00000000000..fcbd55b48e8
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AddMaskAlgorithmEvent.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.algorithm;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Add mask algorithm event.
+ *
+ * @param <T> algorithm configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AddMaskAlgorithmEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String algorithmName;
+
+ private final T config;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AlterMaskAlgorithmEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AlterMaskAlgorithmEvent.java
new file mode 100644
index 00000000000..65a83b6b327
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/AlterMaskAlgorithmEvent.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.algorithm;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Alter mask algorithm event.
+ *
+ * @param <T> algorithm configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AlterMaskAlgorithmEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String algorithmName;
+
+ private final T config;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/DeleteMaskAlgorithmEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/DeleteMaskAlgorithmEvent.java
new file mode 100644
index 00000000000..07f484e4e86
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/algorithm/DeleteMaskAlgorithmEvent.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.algorithm;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Delete mask algorithm event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class DeleteMaskAlgorithmEvent implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String algorithmName;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AddMaskConfigurationEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AddMaskConfigurationEvent.java
new file mode 100644
index 00000000000..01084178b90
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AddMaskConfigurationEvent.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Add mask configuration event.
+ *
+ * @param <T> mask configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AddMaskConfigurationEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final T config;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AlterMaskConfigurationEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AlterMaskConfigurationEvent.java
new file mode 100644
index 00000000000..885e7a156a4
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/AlterMaskConfigurationEvent.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Alter mask configuration event.
+ *
+ * @param <T> mask configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AlterMaskConfigurationEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String tableName;
+
+ private final T config;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/DeleteMaskConfigurationEvent.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/DeleteMaskConfigurationEvent.java
new file mode 100644
index 00000000000..af3123e9246
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/event/config/DeleteMaskConfigurationEvent.java
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Delete mask configuration event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class DeleteMaskConfigurationEvent implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String tableName;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscriber.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscriber.java
new file mode 100644
index 00000000000..c330094b852
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscriber.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.subscriber;
+
+import com.google.common.eventbus.Subscribe;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+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.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.event.algorithm.AddMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.event.algorithm.AlterMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.event.algorithm.DeleteMaskAlgorithmEvent;
+import org.apache.shardingsphere.mask.rule.MaskRule;
+import org.apache.shardingsphere.mode.event.config.RuleConfigurationChangedEvent;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * Mask algorithm subscriber.
+ */
+@SuppressWarnings("UnstableApiUsage")
+@RequiredArgsConstructor
+public final class MaskAlgorithmSubscriber implements RuleConfigurationSubscribeCoordinator {
+
+ private Map<String, ShardingSphereDatabase> databases;
+
+ private InstanceContext instanceContext;
+
+ @Override
+ public void registerRuleConfigurationSubscriber(final Map<String, ShardingSphereDatabase> databases, final InstanceContext instanceContext) {
+ this.databases = databases;
+ this.instanceContext = instanceContext;
+ instanceContext.getEventBusContext().register(this);
+ }
+
+ /**
+ * Renew with add algorithm.
+ *
+ * @param event add algorithm event
+ */
+ @Subscribe
+ public synchronized void renew(final AddMaskAlgorithmEvent<AlgorithmConfiguration> event) {
+ renew(event.getDatabaseName(), event.getAlgorithmName(), event.getConfig());
+ }
+
+ /**
+ * Renew with alter algorithm.
+ *
+ * @param event alter algorithm event
+ */
+ @Subscribe
+ public synchronized void renew(final AlterMaskAlgorithmEvent<AlgorithmConfiguration> event) {
+ renew(event.getDatabaseName(), event.getAlgorithmName(), event.getConfig());
+ }
+
+ private void renew(final String databaseName, final String algorithmName, final AlgorithmConfiguration algorithmConfig) {
+ ShardingSphereDatabase database = databases.get(databaseName);
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration();
+ config.getMaskAlgorithms().put(algorithmName, algorithmConfig);
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(databaseName, config));
+ }
+
+ /**
+ * Renew with delete algorithm.
+ *
+ * @param event delete algorithm event
+ */
+ @Subscribe
+ public synchronized void renew(final DeleteMaskAlgorithmEvent event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration();
+ config.getMaskAlgorithms().remove(event.getAlgorithmName());
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskConfigurationSubscriber.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskConfigurationSubscriber.java
new file mode 100644
index 00000000000..666b6ca67a7
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskConfigurationSubscriber.java
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.subscriber;
+
+import com.google.common.eventbus.Subscribe;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+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.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.event.config.AddMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.event.config.AlterMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.event.config.DeleteMaskConfigurationEvent;
+import org.apache.shardingsphere.mask.rule.MaskRule;
+import org.apache.shardingsphere.mode.event.config.RuleConfigurationChangedEvent;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Mask configuration subscriber.
+ */
+@SuppressWarnings("UnstableApiUsage")
+@RequiredArgsConstructor
+public final class MaskConfigurationSubscriber implements RuleConfigurationSubscribeCoordinator {
+
+ private Map<String, ShardingSphereDatabase> databases;
+
+ private InstanceContext instanceContext;
+
+ @Override
+ public void registerRuleConfigurationSubscriber(final Map<String, ShardingSphereDatabase> databases, final InstanceContext instanceContext) {
+ this.databases = databases;
+ this.instanceContext = instanceContext;
+ instanceContext.getEventBusContext().register(this);
+ }
+
+ /**
+ * Renew with add mask configuration.
+ *
+ * @param event add mask configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final AddMaskConfigurationEvent<MaskTableRuleConfiguration> event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ MaskTableRuleConfiguration needToAddedConfig = event.getConfig();
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ Optional<MaskRule> rule = database.getRuleMetaData().findSingleRule(MaskRule.class);
+ MaskRuleConfiguration config;
+ if (rule.isPresent()) {
+ config = (MaskRuleConfiguration) rule.get().getConfiguration();
+ config.getTables().add(needToAddedConfig);
+ } else {
+ config = new MaskRuleConfiguration(Collections.singletonList(needToAddedConfig), Collections.emptyMap());
+ }
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+
+ /**
+ * Renew with alter mask configuration.
+ *
+ * @param event alter mask configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final AlterMaskConfigurationEvent<MaskTableRuleConfiguration> event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ MaskTableRuleConfiguration needToAlteredConfig = event.getConfig();
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration();
+ config.getTables().removeIf(each -> each.getName().equals(event.getTableName()));
+ ruleConfigs.add(new MaskRuleConfiguration(Collections.singletonList(needToAlteredConfig), Collections.emptyMap()));
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+
+ /**
+ * Renew with delete mask configuration.
+ *
+ * @param event delete mask configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final DeleteMaskConfigurationEvent event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration();
+ config.getTables().removeIf(each -> each.getName().equals(event.getTableName()));
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+}
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
new file mode 100644
index 00000000000..286c20177a0
--- /dev/null
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.mask.subscriber.MaskConfigurationSubscriber
+org.apache.shardingsphere.mask.subscriber.MaskAlgorithmSubscriber
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder
new file mode 100644
index 00000000000..71d30521709
--- /dev/null
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder
@@ -0,0 +1,18 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.mask.event.MaskRuleConfigurationEventBuilder
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 29aa9c7f414..69773133252 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
@@ -77,8 +77,6 @@ public final class ReadwriteSplittingRuleConfigurationEventBuilder implements Ru
private Optional<GovernanceEvent> createLoadBalanceEvent(final String databaseName, final String loadBalanceName, final DataChangedEvent event) {
if (Type.ADDED == event.getType()) {
- new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlAlgorithmConfiguration.class));
-
return Optional.of(new AddLoadBalanceEvent<>(databaseName, loadBalanceName, swapToAlgorithmConfig(event.getValue())));
}
if (Type.UPDATED == event.getType()) {