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/12 05:06:20 UTC
[shardingsphere] branch master updated: Add feature configuration changed event and subscribe at encrypt module (#26273)
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 c67524c814b Add feature configuration changed event and subscribe at encrypt module (#26273)
c67524c814b is described below
commit c67524c814b55240c21611175462b7f2db95c128
Author: ChenJiaHao <Pa...@163.com>
AuthorDate: Mon Jun 12 13:06:13 2023 +0800
Add feature configuration changed event and subscribe at encrypt module (#26273)
---
features/encrypt/core/pom.xml | 5 +
.../EncryptRuleConfigurationEventBuilder.java | 90 ++++++++++++++++
.../event/config/AddEncryptConfigurationEvent.java | 36 +++++++
.../config/AlterEncryptConfigurationEvent.java | 38 +++++++
.../config/DeleteEncryptConfigurationEvent.java | 34 ++++++
.../encrypt/event/encryptor/AddEncryptorEvent.java | 38 +++++++
.../event/encryptor/AlterEncryptorEvent.java | 38 +++++++
.../event/encryptor/DeleteEncryptorEvent.java | 34 ++++++
.../subscriber/EncryptConfigurationSubscriber.java | 114 +++++++++++++++++++++
.../encrypt/subscriber/EncryptorSubscriber.java | 101 ++++++++++++++++++
...nfra.rule.RuleConfigurationSubscribeCoordinator | 19 ++++
...mode.event.config.RuleConfigurationEventBuilder | 18 ++++
12 files changed, 565 insertions(+)
diff --git a/features/encrypt/core/pom.xml b/features/encrypt/core/pom.xml
index 8904d2683b0..e03478480f9 100644
--- a/features/encrypt/core/pom.xml
+++ b/features/encrypt/core/pom.xml
@@ -43,6 +43,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/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/EncryptRuleConfigurationEventBuilder.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/EncryptRuleConfigurationEventBuilder.java
new file mode 100644
index 00000000000..d9b8f934d13
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/EncryptRuleConfigurationEventBuilder.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.encrypt.event;
+
+import com.google.common.base.Strings;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.event.config.AddEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.event.config.AlterEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.event.config.DeleteEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.event.encryptor.AddEncryptorEvent;
+import org.apache.shardingsphere.encrypt.event.encryptor.AlterEncryptorEvent;
+import org.apache.shardingsphere.encrypt.event.encryptor.DeleteEncryptorEvent;
+import org.apache.shardingsphere.encrypt.metadata.converter.EncryptNodeConverter;
+import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.yaml.swapper.YamlEncryptRuleConfigurationSwapper;
+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;
+
+import java.util.Optional;
+
+/**
+ * Encrypt rule configuration event builder.
+ */
+public final class EncryptRuleConfigurationEventBuilder implements RuleConfigurationEventBuilder {
+
+ @Override
+ public Optional<GovernanceEvent> build(final String databaseName, final DataChangedEvent event) {
+ if (!EncryptNodeConverter.isEncryptPath(event.getKey()) || Strings.isNullOrEmpty(event.getValue())) {
+ return Optional.empty();
+ }
+ Optional<String> tableName = EncryptNodeConverter.getTableName(event.getKey());
+ if (tableName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
+ return createEncryptConfigEvent(databaseName, tableName.get(), event);
+ }
+ Optional<String> encryptorName = EncryptNodeConverter.getEncryptorName(event.getKey());
+ if (encryptorName.isPresent() && !Strings.isNullOrEmpty(event.getValue())) {
+ return createEncryptorEvent(databaseName, encryptorName.get(), event);
+ }
+ return Optional.empty();
+ }
+
+ private Optional<GovernanceEvent> createEncryptConfigEvent(final String databaseName, final String groupName, final DataChangedEvent event) {
+ if (Type.ADDED == event.getType()) {
+ return Optional.of(new AddEncryptConfigurationEvent<>(databaseName, swapEncryptTableRuleConfig(event.getValue())));
+ }
+ if (Type.UPDATED == event.getType()) {
+ return Optional.of(new AlterEncryptConfigurationEvent<>(databaseName, groupName, swapEncryptTableRuleConfig(event.getValue())));
+ }
+ return Optional.of(new DeleteEncryptConfigurationEvent(databaseName, groupName));
+ }
+
+ private EncryptTableRuleConfiguration swapEncryptTableRuleConfig(final String yamlContext) {
+ return new YamlEncryptRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlEncryptRuleConfiguration.class)).getTables().iterator().next();
+ }
+
+ private Optional<GovernanceEvent> createEncryptorEvent(final String databaseName, final String encryptorName, final DataChangedEvent event) {
+ if (Type.ADDED == event.getType()) {
+ return Optional.of(new AddEncryptorEvent<>(databaseName, encryptorName, swapToAlgorithmConfig(event.getValue())));
+ }
+ if (Type.UPDATED == event.getType()) {
+ return Optional.of(new AlterEncryptorEvent<>(databaseName, encryptorName, swapToAlgorithmConfig(event.getValue())));
+ }
+ return Optional.of(new DeleteEncryptorEvent(databaseName, encryptorName));
+ }
+
+ private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContext) {
+ return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContext, YamlAlgorithmConfiguration.class));
+ }
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AddEncryptConfigurationEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AddEncryptConfigurationEvent.java
new file mode 100644
index 00000000000..a7f4ec1aaf2
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AddEncryptConfigurationEvent.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.encrypt.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Add encrypt configuration event.
+ *
+ * @param <T> encrypt configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AddEncryptConfigurationEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final T config;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AlterEncryptConfigurationEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AlterEncryptConfigurationEvent.java
new file mode 100644
index 00000000000..6ee12e209a7
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/AlterEncryptConfigurationEvent.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.encrypt.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Alter encrypt configuration event.
+ *
+ * @param <T> encrypt configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AlterEncryptConfigurationEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String tableName;
+
+ private final T config;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/DeleteEncryptConfigurationEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/DeleteEncryptConfigurationEvent.java
new file mode 100644
index 00000000000..d68a64ec9bc
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/config/DeleteEncryptConfigurationEvent.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.encrypt.event.config;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Delete encrypt configuration event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class DeleteEncryptConfigurationEvent implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String tableName;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AddEncryptorEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AddEncryptorEvent.java
new file mode 100644
index 00000000000..ec4e585aa3a
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AddEncryptorEvent.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.encrypt.event.encryptor;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Add encryptor event.
+ *
+ * @param <T> encryptor configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AddEncryptorEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String encryptorName;
+
+ private final T config;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AlterEncryptorEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AlterEncryptorEvent.java
new file mode 100644
index 00000000000..6e039889b96
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/AlterEncryptorEvent.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.encrypt.event.encryptor;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Alter encryptor event.
+ *
+ * @param <T> encryptor configuration
+ */
+@RequiredArgsConstructor
+@Getter
+public final class AlterEncryptorEvent<T> implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String encryptorName;
+
+ private final T config;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/DeleteEncryptorEvent.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/DeleteEncryptorEvent.java
new file mode 100644
index 00000000000..ecd264bd64e
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/event/encryptor/DeleteEncryptorEvent.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.encrypt.event.encryptor;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.rule.event.GovernanceEvent;
+
+/**
+ * Delete encryptor event.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class DeleteEncryptorEvent implements GovernanceEvent {
+
+ private final String databaseName;
+
+ private final String encryptorName;
+}
diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptConfigurationSubscriber.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptConfigurationSubscriber.java
new file mode 100644
index 00000000000..be47a095186
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptConfigurationSubscriber.java
@@ -0,0 +1,114 @@
+/*
+ * 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.encrypt.subscriber;
+
+import com.google.common.eventbus.Subscribe;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.encrypt.event.config.AddEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.event.config.AlterEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.event.config.DeleteEncryptConfigurationEvent;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+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.mode.event.config.RuleConfigurationChangedEvent;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Optional;
+
+/**
+ * Encrypt configuration subscriber.
+ */
+@SuppressWarnings("UnstableApiUsage")
+@RequiredArgsConstructor
+public final class EncryptConfigurationSubscriber 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 encrypt configuration.
+ *
+ * @param event add encrypt configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final AddEncryptConfigurationEvent<EncryptTableRuleConfiguration> event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ EncryptTableRuleConfiguration needToAddedConfig = event.getConfig();
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ Optional<EncryptRule> rule = database.getRuleMetaData().findSingleRule(EncryptRule.class);
+ EncryptRuleConfiguration config;
+ if (rule.isPresent()) {
+ config = (EncryptRuleConfiguration) rule.get().getConfiguration();
+ config.getTables().add(needToAddedConfig);
+ } else {
+ config = new EncryptRuleConfiguration(Collections.singletonList(needToAddedConfig), Collections.emptyMap());
+ }
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+
+ /**
+ * Renew with alter encrypt configuration.
+ *
+ * @param event alter encrypt configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final AlterEncryptConfigurationEvent<EncryptTableRuleConfiguration> event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ EncryptTableRuleConfiguration needToAlteredConfig = event.getConfig();
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ EncryptRuleConfiguration config = (EncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.class).getConfiguration();
+ config.getTables().removeIf(each -> each.getName().equals(event.getTableName()));
+ config.getTables().add(needToAlteredConfig);
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+
+ /**
+ * Renew with delete encrypt configuration.
+ *
+ * @param event delete encrypt configuration event
+ */
+ @Subscribe
+ public synchronized void renew(final DeleteEncryptConfigurationEvent event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ EncryptRuleConfiguration config = (EncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.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/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscriber.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscriber.java
new file mode 100644
index 00000000000..16a1f8aed17
--- /dev/null
+++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscriber.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.encrypt.subscriber;
+
+import com.google.common.eventbus.Subscribe;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import org.apache.shardingsphere.encrypt.event.encryptor.AddEncryptorEvent;
+import org.apache.shardingsphere.encrypt.event.encryptor.AlterEncryptorEvent;
+import org.apache.shardingsphere.encrypt.event.encryptor.DeleteEncryptorEvent;
+import org.apache.shardingsphere.encrypt.rule.EncryptRule;
+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.mode.event.config.RuleConfigurationChangedEvent;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * Encrypt encryptor subscriber.
+ */
+@SuppressWarnings("UnstableApiUsage")
+@RequiredArgsConstructor
+public final class EncryptorSubscriber 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 encryptor.
+ *
+ * @param event add encryptor event
+ */
+ @Subscribe
+ public synchronized void renew(final AddEncryptorEvent<AlgorithmConfiguration> event) {
+ renew(event.getDatabaseName(), event.getEncryptorName(), event.getConfig());
+ }
+
+ /**
+ * Renew with alter encryptor.
+ *
+ * @param event alter encryptor event
+ */
+ @Subscribe
+ public synchronized void renew(final AlterEncryptorEvent<AlgorithmConfiguration> event) {
+ renew(event.getDatabaseName(), event.getEncryptorName(), event.getConfig());
+ }
+
+ private void renew(final String databaseName, final String encryptorName, final AlgorithmConfiguration encryptorConfig) {
+ ShardingSphereDatabase database = databases.get(databaseName);
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ EncryptRuleConfiguration config = (EncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.class).getConfiguration();
+ config.getEncryptors().put(encryptorName, encryptorConfig);
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(databaseName, config));
+ }
+
+ /**
+ * Renew with delete encryptor.
+ *
+ * @param event delete encryptor event
+ */
+ @Subscribe
+ public synchronized void renew(final DeleteEncryptorEvent event) {
+ ShardingSphereDatabase database = databases.get(event.getDatabaseName());
+ Collection<RuleConfiguration> ruleConfigs = new LinkedList<>(database.getRuleMetaData().getConfigurations());
+ EncryptRuleConfiguration config = (EncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.class).getConfiguration();
+ config.getEncryptors().remove(event.getEncryptorName());
+ ruleConfigs.add(config);
+ database.getRuleMetaData().getConfigurations().addAll(ruleConfigs);
+ instanceContext.getEventBusContext().post(new RuleConfigurationChangedEvent(event.getDatabaseName(), config));
+ }
+}
diff --git a/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator b/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.rule.RuleConfigurationSubscribeCoordinator
new file mode 100644
index 00000000000..6656296e45c
--- /dev/null
+++ b/features/encrypt/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.encrypt.subscriber.EncryptConfigurationSubscriber
+org.apache.shardingsphere.encrypt.subscriber.EncryptorSubscriber
diff --git a/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder b/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.event.config.RuleConfigurationEventBuilder
new file mode 100644
index 00000000000..aefcf6ca88f
--- /dev/null
+++ b/features/encrypt/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.encrypt.event.EncryptRuleConfigurationEventBuilder