From e7284bf77a5a4756c3383b0fe85911fc9fa9b5fe Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 5 Jul 2023 00:07:50 +0800 Subject: [PATCH] Refactor rule item changed subscriber (#26763) * Refactor SingleTableSubscriber * Refactor EncryptTableSubscriber * Refactor EncryptTableSubscriber * Refactor RuleItemChangedSubscribeEngine * Refactor EncryptorSubscriber * Refactor CompatibleEncryptorSubscriber and CompatibleEncryptTableSubscriber * Refactor MaskAlgorithmSubscriber and MaskTableSubscriber * Refactor ReadwriteSplittingLoadBalancerSubscriber * Refactor ReadwriteSplittingDataSourceSubscriber * Refactor BroadcastTableSubscriber * Refactor DefaultStrategySubscribeEngine * Refactor DefaultShardingAuditorStrategySubscriber * Refactor DefaultShardingColumnSubscriber * Refactor KeyGeneratorSubscriber * Refactor ShardingAuditorSubscriber * Refactor ShardingAutoTableSubscriber * Refactor ShardingCacheSubscriber * Refactor ShardingTableSubscriber * Refactor ShardingTableReferenceSubscriber * Refactor ShadowTableSubscriber * Refactor ShadowDataSourceSubscriber * Refactor ShadowAlgorithmSubscriber * Refactor DefaultShadowAlgorithmNameSubscriber --- .../BroadcastTableSubscribeEngine.java | 61 ++++++++++++ .../subscriber/BroadcastTableSubscriber.java | 42 ++------ .../EncryptTableSubscribeEngine.java | 2 +- .../subscriber/EncryptorSubscribeEngine.java | 2 +- .../subscriber/EncryptorSubscriber.java | 2 - ...CompatibleEncryptTableSubscribeEngine.java | 74 ++++++++++++++ .../CompatibleEncryptTableSubscriber.java | 46 ++------- .../CompatibleEncryptorSubscribeEngine.java | 73 ++++++++++++++ .../CompatibleEncryptorSubscriber.java | 44 ++------- .../MaskAlgorithmSubscribeEngine.java | 70 ++++++++++++++ .../subscriber/MaskAlgorithmSubscriber.java | 43 ++------- .../subscriber/MaskTableSubscribeEngine.java | 71 ++++++++++++++ .../mask/subscriber/MaskTableSubscriber.java | 50 ++-------- ...eSplittingDataSourceRuleConfiguration.java | 3 +- ...iteSplittingDataSourceSubscribeEngine.java | 81 ++++++++++++++++ ...eadwriteSplittingDataSourceSubscriber.java | 74 ++------------ ...adwriteSplittingLoadBalanceSubscriber.java | 96 ------------------- ...eSplittingLoadBalancerSubscribeEngine.java | 66 +++++++++++++ ...dwriteSplittingLoadBalancerSubscriber.java | 50 ++++++++++ ...phere.mode.subsciber.RuleChangedSubscriber | 2 +- ...ultShadowAlgorithmNameSubscribeEngine.java | 56 +++++++++++ .../DefaultShadowAlgorithmNameSubscriber.java | 36 ++----- .../ShadowAlgorithmSubscribeEngine.java | 62 ++++++++++++ .../subscriber/ShadowAlgorithmSubscriber.java | 46 ++------- .../ShadowDataSourceSubscribeEngine.java | 64 +++++++++++++ .../ShadowDataSourceSubscriber.java | 42 ++------ .../ShadowTableSubscribeEngine.java | 62 ++++++++++++ .../subscriber/ShadowTableSubscriber.java | 36 ++----- ...tabaseShardingStrategySubscribeEngine.java | 60 ++++++++++++ ...ultDatabaseShardingStrategySubscriber.java | 36 ++----- ...ultKeyGenerateStrategySubscribeEngine.java | 60 ++++++++++++ .../DefaultKeyGenerateStrategySubscriber.java | 37 ++----- ...hardingAuditorStrategySubscribeEngine.java | 60 ++++++++++++ ...aultShardingAuditorStrategySubscriber.java | 37 ++----- .../DefaultShardingColumnSubscribeEngine.java | 56 +++++++++++ .../DefaultShardingColumnSubscriber.java | 31 ++---- ...tTableShardingStrategySubscribeEngine.java | 60 ++++++++++++ ...efaultTableShardingStrategySubscriber.java | 48 ++-------- .../KeyGeneratorSubscribeEngine.java | 62 ++++++++++++ .../subscriber/KeyGeneratorSubscriber.java | 34 ++----- .../ShardingAlgorithmSubscribeEngine.java | 62 ++++++++++++ .../ShardingAlgorithmSubscriber.java | 34 ++----- .../ShardingAuditorSubscribeEngine.java | 62 ++++++++++++ .../subscriber/ShardingAuditorSubscriber.java | 34 ++----- .../ShardingAutoTableSubscribeEngine.java | 63 ++++++++++++ .../ShardingAutoTableSubscriber.java | 50 ++-------- .../ShardingCacheSubscribeEngine.java | 60 ++++++++++++ .../subscriber/ShardingCacheSubscriber.java | 36 ++----- ...ShardingTableReferenceSubscribeEngine.java | 61 ++++++++++++ .../ShardingTableReferenceSubscriber.java | 35 ++----- .../ShardingTableSubscribeEngine.java | 63 ++++++++++++ .../subscriber/ShardingTableSubscriber.java | 37 ++----- .../SingleTableSubscribeEngine.java | 2 +- .../RuleItemChangedSubscribeEngine.java | 4 +- 54 files changed, 1706 insertions(+), 834 deletions(-) create mode 100644 features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscribeEngine.java create mode 100644 features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscribeEngine.java create mode 100644 features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscribeEngine.java create mode 100644 features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscribeEngine.java create mode 100644 features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscribeEngine.java create mode 100644 features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscribeEngine.java delete mode 100644 features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalanceSubscriber.java create mode 100644 features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscribeEngine.java create mode 100644 features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscriber.java create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscribeEngine.java create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscribeEngine.java create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscribeEngine.java create mode 100644 features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscribeEngine.java create mode 100644 features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscribeEngine.java diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscribeEngine.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscribeEngine.java new file mode 100644 index 0000000000000..9b00143452cd8 --- /dev/null +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscribeEngine.java @@ -0,0 +1,61 @@ +/* + * 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.broadcast.subscriber; + +import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; +import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; + +import java.util.LinkedList; + +/** + * Broadcast table subscribe engine. + */ +public final class BroadcastTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public BroadcastTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected BroadcastRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new BroadcastRuleConfiguration(YamlEngine.unmarshal(yamlContent, YamlBroadcastRuleConfiguration.class).getTables()); + } + + @Override + protected BroadcastRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(BroadcastRule.class).map(BroadcastRule::getConfiguration).orElseGet(() -> new BroadcastRuleConfiguration(new LinkedList<>())); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final BroadcastRuleConfiguration currentRuleConfig, final BroadcastRuleConfiguration toBeChangedItemConfig) { + currentRuleConfig.getTables().clear(); + currentRuleConfig.getTables().addAll(toBeChangedItemConfig.getTables()); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final BroadcastRuleConfiguration currentRuleConfig) { + currentRuleConfig.getTables().clear(); + } +} diff --git a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscriber.java b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscriber.java index a821e06da7d48..69ee3aebb0538 100644 --- a/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscriber.java +++ b/features/broadcast/core/src/main/java/org/apache/shardingsphere/broadcast/subscriber/BroadcastTableSubscriber.java @@ -18,59 +18,33 @@ package org.apache.shardingsphere.broadcast.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; import org.apache.shardingsphere.broadcast.event.table.AlterBroadcastTableEvent; import org.apache.shardingsphere.broadcast.event.table.DropBroadcastTableEvent; -import org.apache.shardingsphere.broadcast.rule.BroadcastRule; -import org.apache.shardingsphere.broadcast.yaml.config.YamlBroadcastRuleConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import java.util.Optional; - /** * Broadcast table subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class BroadcastTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private BroadcastTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new BroadcastTableSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterBroadcastTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - BroadcastRuleConfiguration toBeChangedConfig = new BroadcastRuleConfiguration(YamlEngine.unmarshal(yamlContent, YamlBroadcastRuleConfiguration.class).getTables()); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - Optional rule = database.getRuleMetaData().findSingleRule(BroadcastRule.class); - BroadcastRuleConfiguration config; - if (rule.isPresent()) { - config = rule.get().getConfiguration(); - config.getTables().clear(); - config.getTables().addAll(toBeChangedConfig.getTables()); - } else { - config = new BroadcastRuleConfiguration(toBeChangedConfig.getTables()); - } - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropBroadcastTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - BroadcastRuleConfiguration config = database.getRuleMetaData().getSingleRule(BroadcastRule.class).getConfiguration(); - config.getTables().clear(); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptTableSubscribeEngine.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptTableSubscribeEngine.java index ce6073d05ea75..f1f7e9949928b 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptTableSubscribeEngine.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptTableSubscribeEngine.java @@ -43,7 +43,7 @@ public EncryptTableSubscribeEngine(final ContextManager contextManager) { } @Override - protected EncryptTableRuleConfiguration swapRuleItemConfigurationFromEvent(final String yamlContent) { + protected EncryptTableRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { return new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class)); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscribeEngine.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscribeEngine.java index 65e3a2f06dde2..0378cb3d32552 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscribeEngine.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/EncryptorSubscribeEngine.java @@ -44,7 +44,7 @@ public EncryptorSubscribeEngine(final ContextManager contextManager) { } @Override - protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final String yamlContent) { + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); } 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 index e07fe7d1edfee..d1e8c6c5439b1 100644 --- 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 @@ -18,7 +18,6 @@ package org.apache.shardingsphere.encrypt.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; import org.apache.shardingsphere.encrypt.event.encryptor.AlterEncryptorEvent; import org.apache.shardingsphere.encrypt.event.encryptor.DropEncryptorEvent; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -28,7 +27,6 @@ * Encryptor subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class EncryptorSubscriber implements RuleChangedSubscriber { private EncryptorSubscribeEngine engine; diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscribeEngine.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscribeEngine.java new file mode 100644 index 0000000000000..9aa2476b7bf6d --- /dev/null +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscribeEngine.java @@ -0,0 +1,74 @@ +/* + * 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.compatible; + +import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration; +import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; +import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration; +import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; + +import java.util.LinkedHashMap; +import java.util.LinkedList; + +/** + * Compatible encrypt table subscribe engine. + * @deprecated compatible support will remove in next version. + */ +@Deprecated +public final class CompatibleEncryptTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public CompatibleEncryptTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected EncryptTableRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class)); + } + + @Override + protected CompatibleEncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(EncryptRule.class) + .map(optional -> getCompatibleEncryptRuleConfiguration((CompatibleEncryptRuleConfiguration) optional.getConfiguration())) + .orElseGet(() -> new CompatibleEncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + private CompatibleEncryptRuleConfiguration getCompatibleEncryptRuleConfiguration(final CompatibleEncryptRuleConfiguration config) { + return null == config.getTables() ? new CompatibleEncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config; + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final CompatibleEncryptRuleConfiguration currentRuleConfig, final EncryptTableRuleConfiguration toBeChangedItemConfig) { + // TODO refactor DistSQL to only persist config + currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName())); + currentRuleConfig.getTables().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final CompatibleEncryptRuleConfiguration currentRuleConfig) { + currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscriber.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscriber.java index 6c370a04a42bf..bc8f3ceb4ea30 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscriber.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptTableSubscriber.java @@ -18,65 +18,35 @@ package org.apache.shardingsphere.encrypt.subscriber.compatible; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration; -import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; import org.apache.shardingsphere.encrypt.event.compatible.table.AlterCompatibleEncryptTableEvent; import org.apache.shardingsphere.encrypt.event.compatible.table.DropCompatibleEncryptTableEvent; -import org.apache.shardingsphere.encrypt.rule.EncryptRule; -import org.apache.shardingsphere.encrypt.yaml.config.rule.YamlEncryptTableRuleConfiguration; -import org.apache.shardingsphere.encrypt.yaml.swapper.rule.YamlEncryptTableRuleConfigurationSwapper; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import java.util.LinkedHashMap; -import java.util.LinkedList; - /** * Compatible encrypt table subscriber. * @deprecated compatible support will remove in next version. */ @Deprecated @SuppressWarnings("UnstableApiUsage") -@Setter public final class CompatibleEncryptTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private CompatibleEncryptTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new CompatibleEncryptTableSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterCompatibleEncryptTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - EncryptTableRuleConfiguration toBeChangedConfig = new YamlEncryptTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlEncryptTableRuleConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - CompatibleEncryptRuleConfiguration config = database.getRuleMetaData().findSingleRule(EncryptRule.class) - .map(optional -> getCompatibleEncryptRuleConfiguration((CompatibleEncryptRuleConfiguration) optional.getConfiguration())) - .orElseGet(() -> new CompatibleEncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - // TODO refactor DistSQL to only persist config - config.getTables().removeIf(each -> each.getName().equals(toBeChangedConfig.getName())); - config.getTables().add(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropCompatibleEncryptTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - CompatibleEncryptRuleConfiguration config = (CompatibleEncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.class).getConfiguration(); - config.getTables().removeIf(each -> each.getName().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private CompatibleEncryptRuleConfiguration getCompatibleEncryptRuleConfiguration(final CompatibleEncryptRuleConfiguration config) { - return null == config.getTables() ? new CompatibleEncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config; + engine.renew(event); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscribeEngine.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscribeEngine.java new file mode 100644 index 0000000000000..a418d6220edf7 --- /dev/null +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscribeEngine.java @@ -0,0 +1,73 @@ +/* + * 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.compatible; + +import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration; +import org.apache.shardingsphere.encrypt.rule.EncryptRule; +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; + +import java.util.LinkedHashMap; +import java.util.LinkedList; + +/** + * Compatible encryptor subscribe engine. + * @deprecated compatible support will remove in next version. + */ +@Deprecated +public final class CompatibleEncryptorSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public CompatibleEncryptorSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected CompatibleEncryptRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(EncryptRule.class) + .map(optional -> getEncryptRuleConfiguration((CompatibleEncryptRuleConfiguration) optional.getConfiguration())) + .orElseGet(() -> new CompatibleEncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + private CompatibleEncryptRuleConfiguration getEncryptRuleConfiguration(final CompatibleEncryptRuleConfiguration config) { + return null == config.getTables() ? new CompatibleEncryptRuleConfiguration(new LinkedList<>(), config.getEncryptors()) : config; + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final CompatibleEncryptRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getEncryptors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final CompatibleEncryptRuleConfiguration currentRuleConfig) { + currentRuleConfig.getEncryptors().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscriber.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscriber.java index 9c39e1c579fe7..062ec4fa44173 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscriber.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/subscriber/compatible/CompatibleEncryptorSubscriber.java @@ -18,63 +18,35 @@ package org.apache.shardingsphere.encrypt.subscriber.compatible; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration; import org.apache.shardingsphere.encrypt.event.compatible.encryptor.AlterCompatibleEncryptorEvent; import org.apache.shardingsphere.encrypt.event.compatible.encryptor.DropCompatibleEncryptorEvent; -import org.apache.shardingsphere.encrypt.rule.EncryptRule; -import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import java.util.LinkedHashMap; -import java.util.LinkedList; - /** * Compatible encryptor subscriber. * @deprecated compatible support will remove in next version. */ @Deprecated @SuppressWarnings("UnstableApiUsage") -@Setter public final class CompatibleEncryptorSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private CompatibleEncryptorSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new CompatibleEncryptorSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterCompatibleEncryptorEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - AlgorithmConfiguration toBeChangedConfig = new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - CompatibleEncryptRuleConfiguration config = database.getRuleMetaData().findSingleRule(EncryptRule.class) - .map(encryptRule -> getEncryptRuleConfiguration((CompatibleEncryptRuleConfiguration) encryptRule.getConfiguration())) - .orElseGet(() -> new CompatibleEncryptRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - config.getEncryptors().put(event.getItemName(), toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropCompatibleEncryptorEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - CompatibleEncryptRuleConfiguration config = (CompatibleEncryptRuleConfiguration) database.getRuleMetaData().getSingleRule(EncryptRule.class).getConfiguration(); - config.getEncryptors().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private CompatibleEncryptRuleConfiguration getEncryptRuleConfiguration(final CompatibleEncryptRuleConfiguration config) { - return null == config.getEncryptors() ? new CompatibleEncryptRuleConfiguration(config.getTables(), new LinkedHashMap<>()) : config; + engine.renew(event); } } diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscribeEngine.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscribeEngine.java new file mode 100644 index 0000000000000..340deed48e82f --- /dev/null +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskAlgorithmSubscribeEngine.java @@ -0,0 +1,70 @@ +/* + * 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 org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.MaskRuleConfiguration; +import org.apache.shardingsphere.mask.rule.MaskRule; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; + +import java.util.LinkedHashMap; +import java.util.LinkedList; + +/** + * Mask algorithm subscribe engine. + */ +public final class MaskAlgorithmSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public MaskAlgorithmSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(MaskRule.class) + .map(maskRule -> getConfiguration((MaskRuleConfiguration) maskRule.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { + return null == config.getMaskAlgorithms() ? new MaskRuleConfiguration(config.getTables(), new LinkedHashMap<>()) : config; + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getMaskAlgorithms().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig) { + currentRuleConfig.getMaskAlgorithms().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} 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 index 6892a1e346ce1..4df26c43bf4ee 100644 --- 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 @@ -18,60 +18,33 @@ package org.apache.shardingsphere.mask.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.MaskRuleConfiguration; import org.apache.shardingsphere.mask.event.algorithm.AlterMaskAlgorithmEvent; import org.apache.shardingsphere.mask.event.algorithm.DropMaskAlgorithmEvent; -import org.apache.shardingsphere.mask.rule.MaskRule; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import java.util.LinkedHashMap; -import java.util.LinkedList; - /** * Mask algorithm subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class MaskAlgorithmSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private MaskAlgorithmSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new MaskAlgorithmSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterMaskAlgorithmEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - AlgorithmConfiguration toBeChangedConfig = new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - MaskRuleConfiguration config = database.getRuleMetaData().findSingleRule(MaskRule.class) - .map(maskRule -> getConfiguration((MaskRuleConfiguration) maskRule.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - config.getMaskAlgorithms().put(event.getItemName(), toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropMaskAlgorithmEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration(); - config.getMaskAlgorithms().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { - return null == config.getMaskAlgorithms() ? new MaskRuleConfiguration(config.getTables(), new LinkedHashMap<>()) : config; + engine.renew(event); } } diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscribeEngine.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscribeEngine.java new file mode 100644 index 0000000000000..5484448450d93 --- /dev/null +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscribeEngine.java @@ -0,0 +1,71 @@ +/* + * 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 org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; +import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; +import org.apache.shardingsphere.mask.rule.MaskRule; +import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration; +import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; + +import java.util.LinkedHashMap; +import java.util.LinkedList; + +/** + * Mask table subscribe engine. + */ +public final class MaskTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public MaskTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected MaskTableRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlMaskTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlMaskTableRuleConfiguration.class)); + } + + @Override + protected MaskRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(MaskRule.class) + .map(optional -> getConfiguration((MaskRuleConfiguration) optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { + return null == config.getTables() ? new MaskRuleConfiguration(new LinkedList<>(), config.getMaskAlgorithms()) : config; + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig, final MaskTableRuleConfiguration toBeChangedItemConfig) { + // TODO refactor DistSQL to only persist config + currentRuleConfig.getTables().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName())); + currentRuleConfig.getTables().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final MaskRuleConfiguration currentRuleConfig) { + currentRuleConfig.getTables().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscriber.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscriber.java index f6c9bf5fdefec..0de57d438c40e 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscriber.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/subscriber/MaskTableSubscriber.java @@ -18,67 +18,33 @@ package org.apache.shardingsphere.mask.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration; -import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration; import org.apache.shardingsphere.mask.event.table.AlterMaskTableEvent; import org.apache.shardingsphere.mask.event.table.DropMaskTableEvent; -import org.apache.shardingsphere.mask.rule.MaskRule; -import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration; -import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import java.util.LinkedHashMap; -import java.util.LinkedList; - /** * Mask table subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class MaskTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private MaskTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new MaskTableSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterMaskTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - MaskTableRuleConfiguration toBeChangedConfig = new YamlMaskTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlMaskTableRuleConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - MaskRuleConfiguration config = getConfiguration(database, toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropMaskTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - MaskRuleConfiguration config = (MaskRuleConfiguration) database.getRuleMetaData().getSingleRule(MaskRule.class).getConfiguration(); - config.getTables().removeIf(each -> each.getName().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private MaskRuleConfiguration getConfiguration(final ShardingSphereDatabase database, final MaskTableRuleConfiguration toBeChangedConfig) { - MaskRuleConfiguration result = database.getRuleMetaData().findSingleRule(MaskRule.class) - .map(optional -> getConfiguration((MaskRuleConfiguration) optional.getConfiguration())).orElseGet(() -> new MaskRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); - // TODO refactor DistSQL to only persist config - result.getTables().removeIf(each -> each.getName().equals(toBeChangedConfig.getName())); - result.getTables().add(toBeChangedConfig); - return result; - } - - private MaskRuleConfiguration getConfiguration(final MaskRuleConfiguration config) { - return null == config.getTables() ? new MaskRuleConfiguration(new LinkedList<>(), config.getMaskAlgorithms()) : config; + engine.renew(event); } } diff --git a/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/rule/ReadwriteSplittingDataSourceRuleConfiguration.java b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/rule/ReadwriteSplittingDataSourceRuleConfiguration.java index aca211e2fe769..a330d40e8da22 100644 --- a/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/rule/ReadwriteSplittingDataSourceRuleConfiguration.java +++ b/features/readwrite-splitting/api/src/main/java/org/apache/shardingsphere/readwritesplitting/api/rule/ReadwriteSplittingDataSourceRuleConfiguration.java @@ -40,8 +40,7 @@ public final class ReadwriteSplittingDataSourceRuleConfiguration { private final String loadBalancerName; - public ReadwriteSplittingDataSourceRuleConfiguration(final String name, final String writeDataSourceName, - final List readDataSourceNames, final String loadBalancerName) { + public ReadwriteSplittingDataSourceRuleConfiguration(final String name, final String writeDataSourceName, final List readDataSourceNames, final String loadBalancerName) { this(name, writeDataSourceName, readDataSourceNames, TransactionalReadQueryStrategy.DYNAMIC, loadBalancerName); } } diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscribeEngine.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscribeEngine.java new file mode 100644 index 0000000000000..ad454313b6d4f --- /dev/null +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscribeEngine.java @@ -0,0 +1,81 @@ +/* + * 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.readwritesplitting.subscriber; + +import com.google.common.base.Strings; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionalReadQueryStrategy; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; +import org.apache.shardingsphere.readwritesplitting.yaml.config.rule.YamlReadwriteSplittingDataSourceRuleConfiguration; + +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.Optional; + +/** + * Readwrite-splitting data source subscribe engine. + */ +public final class ReadwriteSplittingDataSourceSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ReadwriteSplittingDataSourceSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ReadwriteSplittingDataSourceRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig = YamlEngine.unmarshal(yamlContent, YamlReadwriteSplittingDataSourceRuleConfiguration.class); + return new ReadwriteSplittingDataSourceRuleConfiguration(((AlterNamedRuleItemEvent) event).getItemName(), + yamlDataSourceRuleConfig.getWriteDataSourceName(), yamlDataSourceRuleConfig.getReadDataSourceNames(), + getTransactionalReadQueryStrategy(yamlDataSourceRuleConfig), yamlDataSourceRuleConfig.getLoadBalancerName()); + } + + private TransactionalReadQueryStrategy getTransactionalReadQueryStrategy(final YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig) { + return Strings.isNullOrEmpty(yamlDataSourceRuleConfig.getTransactionalReadQueryStrategy()) + ? TransactionalReadQueryStrategy.DYNAMIC + : TransactionalReadQueryStrategy.valueOf(yamlDataSourceRuleConfig.getTransactionalReadQueryStrategy()); + } + + @Override + protected ReadwriteSplittingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + Optional rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class); + return rule.map(optional -> (ReadwriteSplittingRuleConfiguration) optional.getConfiguration()) + .orElseGet(() -> new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ReadwriteSplittingRuleConfiguration currentRuleConfig, + final ReadwriteSplittingDataSourceRuleConfiguration toBeChangedItemConfig) { + // TODO refactor DistSQL to only persist config + currentRuleConfig.getDataSources().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName())); + currentRuleConfig.getDataSources().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getDataSources().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscriber.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscriber.java index b48889b77da97..6517e03549901 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscriber.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingDataSourceSubscriber.java @@ -17,92 +17,34 @@ package org.apache.shardingsphere.readwritesplitting.subscriber; -import com.google.common.base.Strings; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; -import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration; -import org.apache.shardingsphere.readwritesplitting.api.transaction.TransactionalReadQueryStrategy; import org.apache.shardingsphere.readwritesplitting.event.datasource.AlterReadwriteSplittingDataSourceEvent; import org.apache.shardingsphere.readwritesplitting.event.datasource.DropReadwriteSplittingDataSourceEvent; -import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; -import org.apache.shardingsphere.readwritesplitting.yaml.config.rule.YamlReadwriteSplittingDataSourceRuleConfiguration; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Optional; /** * Readwrite-splitting configuration subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ReadwriteSplittingDataSourceSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ReadwriteSplittingDataSourceSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ReadwriteSplittingDataSourceSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterReadwriteSplittingDataSourceEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ReadwriteSplittingDataSourceRuleConfiguration toBeChangedConfig = swapDataSource(event.getItemName(), yamlContent); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ReadwriteSplittingRuleConfiguration config = getConfiguration(database, toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropReadwriteSplittingDataSourceEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ReadwriteSplittingRuleConfiguration config = (ReadwriteSplittingRuleConfiguration) database.getRuleMetaData().getSingleRule(ReadwriteSplittingRule.class).getConfiguration(); - config.getDataSources().removeIf(each -> each.getName().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private ReadwriteSplittingRuleConfiguration getConfiguration(final ShardingSphereDatabase database, final ReadwriteSplittingDataSourceRuleConfiguration needToAddedConfig) { - Optional rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class); - if (rule.isPresent()) { - return getConfiguration((ReadwriteSplittingRuleConfiguration) rule.get().getConfiguration(), needToAddedConfig); - } - Collection dataSourceConfigs = new LinkedList<>(); - dataSourceConfigs.add(needToAddedConfig); - return new ReadwriteSplittingRuleConfiguration(dataSourceConfigs, new LinkedHashMap<>()); - } - - private ReadwriteSplittingRuleConfiguration getConfiguration(final ReadwriteSplittingRuleConfiguration result, final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig) { - if (null == result.getDataSources()) { - Collection dataSources = new LinkedList<>(); - dataSources.add(dataSourceRuleConfig); - return new ReadwriteSplittingRuleConfiguration(dataSources, result.getLoadBalancers()); - } - // TODO refactor DistSQL to only persist config - result.getDataSources().removeIf(each -> each.getName().equals(dataSourceRuleConfig.getName())); - result.getDataSources().add(dataSourceRuleConfig); - return result; - } - - private ReadwriteSplittingDataSourceRuleConfiguration swapDataSource(final String name, final String yamlContent) { - YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig = YamlEngine.unmarshal(yamlContent, YamlReadwriteSplittingDataSourceRuleConfiguration.class); - return new ReadwriteSplittingDataSourceRuleConfiguration(name, yamlDataSourceRuleConfig.getWriteDataSourceName(), yamlDataSourceRuleConfig.getReadDataSourceNames(), - getTransactionalReadQueryStrategy(yamlDataSourceRuleConfig), yamlDataSourceRuleConfig.getLoadBalancerName()); - } - - private TransactionalReadQueryStrategy getTransactionalReadQueryStrategy(final YamlReadwriteSplittingDataSourceRuleConfiguration yamlDataSourceRuleConfig) { - return Strings.isNullOrEmpty(yamlDataSourceRuleConfig.getTransactionalReadQueryStrategy()) - ? TransactionalReadQueryStrategy.DYNAMIC - : TransactionalReadQueryStrategy.valueOf(yamlDataSourceRuleConfig.getTransactionalReadQueryStrategy()); + engine.renew(event); } } diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalanceSubscriber.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalanceSubscriber.java deleted file mode 100644 index 532fd1e2025cf..0000000000000 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalanceSubscriber.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.readwritesplitting.subscriber; - -import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; -import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; -import org.apache.shardingsphere.readwritesplitting.event.loadbalance.AlterReadwriteSplittingLoadBalancerEvent; -import org.apache.shardingsphere.readwritesplitting.event.loadbalance.DropReadwriteSplittingLoadBalancerEvent; -import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; - -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.Map; -import java.util.Optional; - -/** - * Readwrite-splitting load-balance subscriber. - */ -@SuppressWarnings("UnstableApiUsage") -@Setter -public final class ReadwriteSplittingLoadBalanceSubscriber implements RuleChangedSubscriber { - - private ContextManager contextManager; - - @Subscribe - @Override - public synchronized void renew(final AlterReadwriteSplittingLoadBalancerEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - AlgorithmConfiguration toBeChangedConfig = - swapToAlgorithmConfig(contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), - getConfiguration(contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()), event.getItemName(), toBeChangedConfig))); - } - - @Subscribe - @Override - public synchronized void renew(final DropReadwriteSplittingLoadBalancerEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ReadwriteSplittingRuleConfiguration config = (ReadwriteSplittingRuleConfiguration) database.getRuleMetaData().getSingleRule(ReadwriteSplittingRule.class).getConfiguration(); - config.getLoadBalancers().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private ReadwriteSplittingRuleConfiguration getConfiguration(final ShardingSphereDatabase database, final String loadBalanceName, final AlgorithmConfiguration needToAltered) { - Optional rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class); - if (rule.isPresent()) { - return getConfiguration((ReadwriteSplittingRuleConfiguration) rule.get().getConfiguration(), loadBalanceName, needToAltered); - } - Map loadBalancers = new LinkedHashMap<>(); - loadBalancers.put(loadBalanceName, needToAltered); - return new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), loadBalancers); - } - - private ReadwriteSplittingRuleConfiguration getConfiguration(final ReadwriteSplittingRuleConfiguration result, final String loadBalanceName, final AlgorithmConfiguration toBeChangedConfig) { - if (null == result.getLoadBalancers()) { - Map loadBalancers = new LinkedHashMap<>(); - loadBalancers.put(loadBalanceName, toBeChangedConfig); - return new ReadwriteSplittingRuleConfiguration(result.getDataSources(), loadBalancers); - } - result.getLoadBalancers().put(loadBalanceName, toBeChangedConfig); - return result; - } - - private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContent) { - return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); - } -} diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscribeEngine.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscribeEngine.java new file mode 100644 index 0000000000000..4d5abbfcc3864 --- /dev/null +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscribeEngine.java @@ -0,0 +1,66 @@ +/* + * 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.readwritesplitting.subscriber; + +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration; +import org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingRule; + +import java.util.LinkedHashMap; +import java.util.LinkedList; + +/** + * Readwrite-splitting load-balancer subscribe engine. + */ +public final class ReadwriteSplittingLoadBalancerSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ReadwriteSplittingLoadBalancerSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected ReadwriteSplittingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class).map(optional -> (ReadwriteSplittingRuleConfiguration) optional.getConfiguration()) + .orElseGet(() -> new ReadwriteSplittingRuleConfiguration(new LinkedList<>(), new LinkedHashMap<>())); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ReadwriteSplittingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getLoadBalancers().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getLoadBalancers().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscriber.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscriber.java new file mode 100644 index 0000000000000..36ef3c96b2f36 --- /dev/null +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/subscriber/ReadwriteSplittingLoadBalancerSubscriber.java @@ -0,0 +1,50 @@ +/* + * 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.readwritesplitting.subscriber; + +import com.google.common.eventbus.Subscribe; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; +import org.apache.shardingsphere.readwritesplitting.event.loadbalance.AlterReadwriteSplittingLoadBalancerEvent; +import org.apache.shardingsphere.readwritesplitting.event.loadbalance.DropReadwriteSplittingLoadBalancerEvent; + +/** + * Readwrite-splitting load-balancer subscriber. + */ +@SuppressWarnings("UnstableApiUsage") +public final class ReadwriteSplittingLoadBalancerSubscriber implements RuleChangedSubscriber { + + private ReadwriteSplittingLoadBalancerSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ReadwriteSplittingLoadBalancerSubscribeEngine(contextManager); + } + + @Subscribe + @Override + public synchronized void renew(final AlterReadwriteSplittingLoadBalancerEvent event) { + engine.renew(event); + } + + @Subscribe + @Override + public synchronized void renew(final DropReadwriteSplittingLoadBalancerEvent event) { + engine.renew(event); + } +} diff --git a/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber b/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber index bd22a8bd3eba7..d006f745082d5 100644 --- a/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber +++ b/features/readwrite-splitting/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber @@ -16,4 +16,4 @@ # org.apache.shardingsphere.readwritesplitting.subscriber.ReadwriteSplittingDataSourceSubscriber -org.apache.shardingsphere.readwritesplitting.subscriber.ReadwriteSplittingLoadBalanceSubscriber +org.apache.shardingsphere.readwritesplitting.subscriber.ReadwriteSplittingLoadBalancerSubscriber diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscribeEngine.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscribeEngine.java new file mode 100644 index 0000000000000..cc93dfda96643 --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscribeEngine.java @@ -0,0 +1,56 @@ +/* + * 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.shadow.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; +import org.apache.shardingsphere.shadow.rule.ShadowRule; + +/** + * Default shadow algorithm name subscribe engine. + */ +public final class DefaultShadowAlgorithmNameSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultShadowAlgorithmNameSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected String swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return yamlContent; + } + + @Override + protected ShadowRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShadowRule.class).map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig, final String toBeChangedItemConfig) { + currentRuleConfig.setDefaultShadowAlgorithmName(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultShadowAlgorithmName(null); + } +} diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java index cf51c6dba594c..fc12f11c35a18 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/DefaultShadowAlgorithmNameSubscriber.java @@ -18,53 +18,33 @@ package org.apache.shardingsphere.shadow.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.event.algorithm.AlterDefaultShadowAlgorithmEvent; import org.apache.shardingsphere.shadow.event.algorithm.DropDefaultShadowAlgorithmEvent; -import org.apache.shardingsphere.shadow.rule.ShadowRule; - -import java.util.Optional; /** * Default shadow algorithm name subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultShadowAlgorithmNameSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultShadowAlgorithmNameSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultShadowAlgorithmNameSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterDefaultShadowAlgorithmEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - Optional rule = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()).getRuleMetaData().findSingleRule(ShadowRule.class); - ShadowRuleConfiguration config; - if (rule.isPresent()) { - config = (ShadowRuleConfiguration) rule.get().getConfiguration(); - } else { - config = new ShadowRuleConfiguration(); - } - config.setDefaultShadowAlgorithmName(contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropDefaultShadowAlgorithmEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration(); - config.setDefaultShadowAlgorithmName(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscribeEngine.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscribeEngine.java new file mode 100644 index 0000000000000..7689434dbd739 --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowAlgorithmSubscribeEngine.java @@ -0,0 +1,62 @@ +/* + * 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.shadow.subscriber; + +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; +import org.apache.shardingsphere.shadow.rule.ShadowRule; + +/** + * Shadow algorithm subscribe engine. + */ +public final class ShadowAlgorithmSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShadowAlgorithmSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected ShadowRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShadowRule.class).map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getShadowAlgorithms().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig) { + currentRuleConfig.getShadowAlgorithms().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} 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 2f8b3ad73b522..dcf8954bd9f16 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 @@ -18,63 +18,33 @@ package org.apache.shardingsphere.shadow.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.event.algorithm.AlterShadowAlgorithmEvent; import org.apache.shardingsphere.shadow.event.algorithm.DropShadowAlgorithmEvent; -import org.apache.shardingsphere.shadow.rule.ShadowRule; - -import java.util.Optional; /** * Shadow algorithm subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShadowAlgorithmSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShadowAlgorithmSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShadowAlgorithmSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShadowAlgorithmEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - AlgorithmConfiguration needToAlteredConfig = swapToAlgorithmConfig( - contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion())); - Optional rule = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()).getRuleMetaData().findSingleRule(ShadowRule.class); - ShadowRuleConfiguration config; - if (rule.isPresent()) { - config = (ShadowRuleConfiguration) rule.get().getConfiguration(); - } else { - config = new ShadowRuleConfiguration(); - } - config.getShadowAlgorithms().put(event.getItemName(), needToAlteredConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShadowAlgorithmEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration(); - config.getShadowAlgorithms().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private AlgorithmConfiguration swapToAlgorithmConfig(final String yamlContent) { - return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + engine.renew(event); } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscribeEngine.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscribeEngine.java new file mode 100644 index 0000000000000..b68bfad767c4e --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscribeEngine.java @@ -0,0 +1,64 @@ +/* + * 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.shadow.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; +import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration; +import org.apache.shardingsphere.shadow.rule.ShadowRule; +import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration; + +/** + * Shadow data source subscribe engine. + */ +public final class ShadowDataSourceSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShadowDataSourceSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShadowDataSourceConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + YamlShadowDataSourceConfiguration yamlConfig = YamlEngine.unmarshal(yamlContent, YamlShadowDataSourceConfiguration.class); + return new ShadowDataSourceConfiguration(((AlterNamedRuleItemEvent) event).getItemName(), yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName()); + } + + @Override + protected ShadowRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShadowRule.class).map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig, final ShadowDataSourceConfiguration toBeChangedItemConfig) { + // TODO refactor DistSQL to only persist config + currentRuleConfig.getDataSources().removeIf(each -> each.getName().equals(toBeChangedItemConfig.getName())); + currentRuleConfig.getDataSources().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig) { + currentRuleConfig.getDataSources().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java index cc99727974c43..1c5c057f302f8 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowDataSourceSubscriber.java @@ -18,59 +18,33 @@ package org.apache.shardingsphere.shadow.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; -import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration; import org.apache.shardingsphere.shadow.event.datasource.AlterShadowDataSourceEvent; import org.apache.shardingsphere.shadow.event.datasource.DropShadowDataSourceEvent; -import org.apache.shardingsphere.shadow.rule.ShadowRule; -import org.apache.shardingsphere.shadow.yaml.config.datasource.YamlShadowDataSourceConfiguration; /** * Shadow data source subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShadowDataSourceSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShadowDataSourceSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShadowDataSourceSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShadowDataSourceEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShadowDataSourceConfiguration toBeChangedConfig = swapShadowDataSourceRuleConfig(event.getItemName(), yamlContent); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShadowRule.class) - .map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); - // TODO refactor DistSQL to only persist config - config.getDataSources().removeIf(each -> each.getName().equals(toBeChangedConfig.getName())); - config.getDataSources().add(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShadowDataSourceEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration(); - config.getDataSources().removeIf(each -> each.getName().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); - } - - private ShadowDataSourceConfiguration swapShadowDataSourceRuleConfig(final String dataSourceName, final String yamlContent) { - YamlShadowDataSourceConfiguration yamlConfig = YamlEngine.unmarshal(yamlContent, YamlShadowDataSourceConfiguration.class); - return new ShadowDataSourceConfiguration(dataSourceName, yamlConfig.getProductionDataSourceName(), yamlConfig.getShadowDataSourceName()); + engine.renew(event); } } diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscribeEngine.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscribeEngine.java new file mode 100644 index 0000000000000..a3158080bd8fa --- /dev/null +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/subscriber/ShadowTableSubscribeEngine.java @@ -0,0 +1,62 @@ +/* + * 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.shadow.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; +import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; +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; + +/** + * Shadow table subscribe engine. + */ +public final class ShadowTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShadowTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShadowTableConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShadowTableConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShadowTableConfiguration.class)); + } + + @Override + protected ShadowRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShadowRule.class).map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig, final ShadowTableConfiguration toBeChangedItemConfig) { + currentRuleConfig.getTables().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShadowRuleConfiguration currentRuleConfig) { + currentRuleConfig.getTables().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} 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 298883af96d4a..d31af0821c0f9 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 @@ -18,53 +18,33 @@ package org.apache.shardingsphere.shadow.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; -import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; import org.apache.shardingsphere.shadow.event.table.AlterShadowTableEvent; import org.apache.shardingsphere.shadow.event.table.DropShadowTableEvent; -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; /** * Shadow table subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShadowTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShadowTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShadowTableSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShadowTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShadowTableConfiguration toBeChangedConfig = new YamlShadowTableConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShadowTableConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShadowRule.class) - .map(optional -> (ShadowRuleConfiguration) optional.getConfiguration()).orElseGet(ShadowRuleConfiguration::new); - config.getTables().put(event.getItemName(), toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShadowTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShadowRuleConfiguration config = (ShadowRuleConfiguration) database.getRuleMetaData().getSingleRule(ShadowRule.class).getConfiguration(); - config.getTables().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscribeEngine.java new file mode 100644 index 0000000000000..bb831ed474211 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscribeEngine.java @@ -0,0 +1,60 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper; + +/** + * Default database sharding strategy subscribe engine. + */ +public final class DefaultDatabaseShardingStrategySubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultDatabaseShardingStrategySubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingStrategyConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingStrategyConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingStrategyConfiguration toBeChangedItemConfig) { + currentRuleConfig.setDefaultDatabaseShardingStrategy(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultDatabaseShardingStrategy(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscriber.java index 782000e2d31cc..12b7ef89addaa 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultDatabaseShardingStrategySubscriber.java @@ -18,53 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.event.strategy.database.AlterDefaultDatabaseShardingStrategyEvent; import org.apache.shardingsphere.sharding.event.strategy.database.DropDefaultDatabaseShardingStrategyEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper; /** * Default database sharding strategy subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultDatabaseShardingStrategySubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultDatabaseShardingStrategySubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultDatabaseShardingStrategySubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterDefaultDatabaseShardingStrategyEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingStrategyConfiguration toBeChangedConfig = new YamlShardingStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingStrategyConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setDefaultDatabaseShardingStrategy(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropDefaultDatabaseShardingStrategyEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setDefaultDatabaseShardingStrategy(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscribeEngine.java new file mode 100644 index 0000000000000..ba51d242f7ae5 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscribeEngine.java @@ -0,0 +1,60 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlKeyGenerateStrategyConfigurationSwapper; + +/** + * Default key generate strategy subscribe engine. + */ +public final class DefaultKeyGenerateStrategySubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultKeyGenerateStrategySubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected KeyGenerateStrategyConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlKeyGenerateStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlKeyGenerateStrategyConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final KeyGenerateStrategyConfiguration toBeChangedItemConfig) { + currentRuleConfig.setDefaultKeyGenerateStrategy(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultKeyGenerateStrategy(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscriber.java index 9e115f21c4d73..7d0150a947e88 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultKeyGenerateStrategySubscriber.java @@ -18,54 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration; import org.apache.shardingsphere.sharding.event.strategy.keygenerate.AlterDefaultKeyGenerateStrategyEvent; import org.apache.shardingsphere.sharding.event.strategy.keygenerate.DropDefaultKeyGenerateStrategyEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.strategy.keygen.YamlKeyGenerateStrategyConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlKeyGenerateStrategyConfigurationSwapper; /** * Default key generate strategy subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultKeyGenerateStrategySubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultKeyGenerateStrategySubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultKeyGenerateStrategySubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterDefaultKeyGenerateStrategyEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - KeyGenerateStrategyConfiguration toBeChangedConfig = new YamlKeyGenerateStrategyConfigurationSwapper().swapToObject( - YamlEngine.unmarshal(yamlContent, YamlKeyGenerateStrategyConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setDefaultKeyGenerateStrategy(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropDefaultKeyGenerateStrategyEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setDefaultKeyGenerateStrategy(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscribeEngine.java new file mode 100644 index 0000000000000..43720a474041a --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscribeEngine.java @@ -0,0 +1,60 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingAuditStrategyConfigurationSwapper; + +/** + * Default sharding auditor strategy subscribe engine. + */ +public final class DefaultShardingAuditorStrategySubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultShardingAuditorStrategySubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingAuditStrategyConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingAuditStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingAuditStrategyConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingAuditStrategyConfiguration toBeChangedItemConfig) { + currentRuleConfig.setDefaultAuditStrategy(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultAuditStrategy(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscriber.java index 0f913e94ccae5..e9bb15432f07c 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingAuditorStrategySubscriber.java @@ -18,54 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAuditStrategyConfiguration; import org.apache.shardingsphere.sharding.event.strategy.audit.AlterDefaultShardingAuditorStrategyEvent; import org.apache.shardingsphere.sharding.event.strategy.audit.DropDefaultShardingAuditorStrategyEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.strategy.audit.YamlShardingAuditStrategyConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingAuditStrategyConfigurationSwapper; /** * Default sharding auditor strategy subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultShardingAuditorStrategySubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultShardingAuditorStrategySubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultShardingAuditorStrategySubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterDefaultShardingAuditorStrategyEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingAuditStrategyConfiguration toBeChangedConfig = new YamlShardingAuditStrategyConfigurationSwapper().swapToObject( - YamlEngine.unmarshal(yamlContent, YamlShardingAuditStrategyConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setDefaultAuditStrategy(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropDefaultShardingAuditorStrategyEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setDefaultAuditStrategy(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscribeEngine.java new file mode 100644 index 0000000000000..9d0310b49415d --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscribeEngine.java @@ -0,0 +1,56 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; + +/** + * Default sharding column subscribe engine. + */ +public final class DefaultShardingColumnSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultShardingColumnSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected String swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return yamlContent; + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final String toBeChangedItemConfig) { + currentRuleConfig.setDefaultShardingColumn(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultShardingColumn(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscriber.java index f4df6bbfa7daf..c033e65cfa979 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultShardingColumnSubscriber.java @@ -18,48 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.event.strategy.shardingcolumn.AlterDefaultShardingColumnEvent; import org.apache.shardingsphere.sharding.event.strategy.shardingcolumn.DropDefaultShardingColumnEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; /** * Default sharding column subscriber subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultShardingColumnSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultShardingColumnSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultShardingColumnSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterDefaultShardingColumnEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - String toBeChangedConfig = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setDefaultShardingColumn(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropDefaultShardingColumnEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setDefaultAuditStrategy(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscribeEngine.java new file mode 100644 index 0000000000000..0ebc7e4004d6c --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscribeEngine.java @@ -0,0 +1,60 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper; + +/** + * Default table sharding strategy subscribe engine. + */ +public final class DefaultTableShardingStrategySubscribeEngine extends RuleItemChangedSubscribeEngine { + + public DefaultTableShardingStrategySubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingStrategyConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingStrategyConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingStrategyConfiguration toBeChangedItemConfig) { + currentRuleConfig.setDefaultTableShardingStrategy(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setDefaultTableShardingStrategy(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscriber.java index b2f5bd21476ab..9cc92f214651f 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/DefaultTableShardingStrategySubscriber.java @@ -18,61 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration; import org.apache.shardingsphere.sharding.event.strategy.table.AlterDefaultTableShardingStrategyEvent; import org.apache.shardingsphere.sharding.event.strategy.table.DropDefaultTableShardingStrategyEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.strategy.sharding.YamlShardingStrategyConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.strategy.YamlShardingStrategyConfigurationSwapper; /** * Default table sharding strategy subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class DefaultTableShardingStrategySubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private DefaultTableShardingStrategySubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new DefaultTableShardingStrategySubscribeEngine(contextManager); + } - /** - * Renew with alter default table sharding strategy. - * - * @param event alter default table sharding strategy event - */ @Subscribe + @Override public synchronized void renew(final AlterDefaultTableShardingStrategyEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingStrategyConfiguration toBeChangedConfig = new YamlShardingStrategyConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingStrategyConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setDefaultTableShardingStrategy(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } - /** - * Renew with drop default table sharding strategy. - * - * @param event drop default table sharding strategy event - */ @Subscribe + @Override public synchronized void renew(final DropDefaultTableShardingStrategyEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setDefaultTableShardingStrategy(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscribeEngine.java new file mode 100644 index 0000000000000..8679c4b5f28d2 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscribeEngine.java @@ -0,0 +1,62 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; + +/** + * Key generator subscribe engine. + */ +public final class KeyGeneratorSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public KeyGeneratorSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getKeyGenerators().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getKeyGenerators().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscriber.java index 5ff5233997550..428a2ce9e9083 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/KeyGeneratorSubscriber.java @@ -18,51 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.event.algorithm.keygenerator.AlterKeyGeneratorEvent; import org.apache.shardingsphere.sharding.event.algorithm.keygenerator.DropKeyGeneratorEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; /** * Key generator subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class KeyGeneratorSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private KeyGeneratorSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new KeyGeneratorSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterKeyGeneratorEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - config.getKeyGenerators().put(event.getItemName(), new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class))); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropKeyGeneratorEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getKeyGenerators().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscribeEngine.java new file mode 100644 index 0000000000000..b45847a42ec90 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscribeEngine.java @@ -0,0 +1,62 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; + +/** + * Sharding algorithm subscribe engine. + */ +public final class ShardingAlgorithmSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingAlgorithmSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getShardingAlgorithms().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getShardingAlgorithms().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscriber.java index f13698d56852c..6be5f973efcdc 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAlgorithmSubscriber.java @@ -18,51 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.event.algorithm.sharding.AlterShardingAlgorithmEvent; import org.apache.shardingsphere.sharding.event.algorithm.sharding.DropShardingAlgorithmEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; /** * Sharding algorithm subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingAlgorithmSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingAlgorithmSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingAlgorithmSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShardingAlgorithmEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - config.getShardingAlgorithms().put(event.getItemName(), new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class))); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShardingAlgorithmEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getShardingAlgorithms().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscribeEngine.java new file mode 100644 index 0000000000000..4e2e9374b207a --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscribeEngine.java @@ -0,0 +1,62 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +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.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; + +/** + * Sharding auditor subscribe engine. + */ +public final class ShardingAuditorSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingAuditorSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected AlgorithmConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final AlgorithmConfiguration toBeChangedItemConfig) { + currentRuleConfig.getAuditors().put(((AlterNamedRuleItemEvent) event).getItemName(), toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getAuditors().remove(((DropNamedRuleItemEvent) event).getItemName()); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscriber.java index e0db654a6f2f6..e9e97f8da1177 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAuditorSubscriber.java @@ -18,51 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -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.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.event.algorithm.auditor.AlterShardingAuditorEvent; import org.apache.shardingsphere.sharding.event.algorithm.auditor.DropShardingAuditorEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; /** * Sharding auditor subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingAuditorSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingAuditorSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingAuditorSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShardingAuditorEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - config.getAuditors().put(event.getItemName(), new YamlAlgorithmConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlAlgorithmConfiguration.class))); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShardingAuditorEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getAuditors().remove(event.getItemName()); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscribeEngine.java new file mode 100644 index 0000000000000..e9e6c24ce2928 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscribeEngine.java @@ -0,0 +1,63 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingAutoTableRuleConfigurationSwapper; + +/** + * Sharding auto table subscribe engine. + */ +public final class ShardingAutoTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingAutoTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingAutoTableRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingAutoTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingAutoTableRuleConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingAutoTableRuleConfiguration toBeChangedItemConfig) { + currentRuleConfig.getAutoTables().removeIf(each -> each.getLogicTable().equals(((AlterNamedRuleItemEvent) event).getItemName())); + currentRuleConfig.getAutoTables().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getAutoTables().removeIf(each -> each.getLogicTable().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscriber.java index 8ff2228bdd06a..d55ec3d523cbb 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingAutoTableSubscriber.java @@ -18,63 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration; import org.apache.shardingsphere.sharding.event.table.auto.AlterShardingAutoTableEvent; import org.apache.shardingsphere.sharding.event.table.auto.DropShardingAutoTableEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.rule.YamlShardingAutoTableRuleConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingAutoTableRuleConfigurationSwapper; /** * Sharding auto table subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingAutoTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingAutoTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingAutoTableSubscribeEngine(contextManager); + } - /** - * Renew with alter sharding auto table. - * - * @param event alter sharding auto table event - */ @Subscribe + @Override public synchronized void renew(final AlterShardingAutoTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingAutoTableRuleConfiguration toBeChangedConfig = new YamlShardingAutoTableRuleConfigurationSwapper().swapToObject( - YamlEngine.unmarshal(yamlContent, YamlShardingAutoTableRuleConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.getAutoTables().removeIf(each -> each.getLogicTable().equals(event.getItemName())); - config.getAutoTables().add(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } - /** - * Renew with delete sharding auto table. - * - * @param event delete sharding auto table event - */ @Subscribe + @Override public synchronized void renew(final DropShardingAutoTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getAutoTables().removeIf(each -> each.getLogicTable().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscribeEngine.java new file mode 100644 index 0000000000000..1e1a4a23dbe17 --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscribeEngine.java @@ -0,0 +1,60 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.cache.YamlShardingCacheConfigurationSwapper; + +/** + * Sharding cache subscribe engine. + */ +public final class ShardingCacheSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingCacheSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingCacheConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingCacheConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingCacheConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingCacheConfiguration toBeChangedItemConfig) { + currentRuleConfig.setShardingCache(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.setShardingCache(null); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscriber.java index 3129ca4a2a172..cd032dc321b07 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingCacheSubscriber.java @@ -18,53 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.cache.ShardingCacheConfiguration; import org.apache.shardingsphere.sharding.event.cache.AlterShardingCacheEvent; import org.apache.shardingsphere.sharding.event.cache.DropShardingCacheEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.cache.YamlShardingCacheConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.cache.YamlShardingCacheConfigurationSwapper; /** * Sharding cache subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingCacheSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingCacheSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingCacheSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShardingCacheEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingCacheConfiguration toBeChangedConfig = new YamlShardingCacheConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlShardingCacheConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.setShardingCache(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShardingCacheEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.setShardingCache(null); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscribeEngine.java new file mode 100644 index 0000000000000..648e0664c468a --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscribeEngine.java @@ -0,0 +1,61 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableReferenceRuleConfigurationConverter; + +/** + * Sharding table reference subscribe engine. + */ +public final class ShardingTableReferenceSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingTableReferenceSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingTableReferenceRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return YamlShardingTableReferenceRuleConfigurationConverter.convertToObject(yamlContent); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingTableReferenceRuleConfiguration toBeChangedItemConfig) { + currentRuleConfig.getBindingTableGroups().removeIf(each -> each.getName().equals(((AlterNamedRuleItemEvent) event).getItemName())); + currentRuleConfig.getBindingTableGroups().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getBindingTableGroups().removeIf(each -> each.getName().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscriber.java index f82dc76c042ad..612a8ca273fe4 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableReferenceSubscriber.java @@ -18,52 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration; import org.apache.shardingsphere.sharding.event.table.binding.AlterShardingTableReferenceEvent; import org.apache.shardingsphere.sharding.event.table.binding.DropShardingTableReferenceEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableReferenceRuleConfigurationConverter; /** * Sharding table reference subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingTableReferenceSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingTableReferenceSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingTableReferenceSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShardingTableReferenceEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingTableReferenceRuleConfiguration toBeChangedConfig = YamlShardingTableReferenceRuleConfigurationConverter.convertToObject(yamlContent); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.getBindingTableGroups().removeIf(each -> each.getName().equals(event.getItemName())); - config.getBindingTableGroups().add(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShardingTableReferenceEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getBindingTableGroups().removeIf(each -> each.getName().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscribeEngine.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscribeEngine.java new file mode 100644 index 0000000000000..6ac9c395dee7a --- /dev/null +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscribeEngine.java @@ -0,0 +1,63 @@ +/* + * 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.sharding.subscriber; + +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.alter.AlterRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropNamedRuleItemEvent; +import org.apache.shardingsphere.infra.rule.event.rule.drop.DropRuleItemEvent; +import org.apache.shardingsphere.infra.util.yaml.YamlEngine; +import org.apache.shardingsphere.mode.manager.ContextManager; +import org.apache.shardingsphere.mode.subsciber.RuleItemChangedSubscribeEngine; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration; +import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableRuleConfigurationSwapper; + +/** + * Sharding table subscribe engine. + */ +public final class ShardingTableSubscribeEngine extends RuleItemChangedSubscribeEngine { + + public ShardingTableSubscribeEngine(final ContextManager contextManager) { + super(contextManager); + } + + @Override + protected ShardingTableRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { + return new YamlShardingTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlTableRuleConfiguration.class)); + } + + @Override + protected ShardingRuleConfiguration findRuleConfiguration(final ShardingSphereDatabase database) { + return database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); + } + + @Override + protected void changeRuleItemConfiguration(final AlterRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig, final ShardingTableRuleConfiguration toBeChangedItemConfig) { + currentRuleConfig.getTables().removeIf(each -> each.getLogicTable().equals(((AlterNamedRuleItemEvent) event).getItemName())); + currentRuleConfig.getTables().add(toBeChangedItemConfig); + } + + @Override + protected void dropRuleItemConfiguration(final DropRuleItemEvent event, final ShardingRuleConfiguration currentRuleConfig) { + currentRuleConfig.getTables().removeIf(each -> each.getLogicTable().equals(((DropNamedRuleItemEvent) event).getItemName())); + } +} diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscriber.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscriber.java index 6fd60445290e6..0efb65ecec4e3 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscriber.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/subscriber/ShardingTableSubscriber.java @@ -18,54 +18,33 @@ package org.apache.shardingsphere.sharding.subscriber; import com.google.common.eventbus.Subscribe; -import lombok.Setter; -import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; -import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.mode.event.config.DatabaseRuleConfigurationChangedEvent; import org.apache.shardingsphere.mode.manager.ContextManager; import org.apache.shardingsphere.mode.subsciber.RuleChangedSubscriber; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; -import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration; import org.apache.shardingsphere.sharding.event.table.sharding.AlterShardingTableEvent; import org.apache.shardingsphere.sharding.event.table.sharding.DropShardingTableEvent; -import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.yaml.config.rule.YamlTableRuleConfiguration; -import org.apache.shardingsphere.sharding.yaml.swapper.rule.YamlShardingTableRuleConfigurationSwapper; /** * Sharding table subscriber. */ @SuppressWarnings("UnstableApiUsage") -@Setter public final class ShardingTableSubscriber implements RuleChangedSubscriber { - private ContextManager contextManager; + private ShardingTableSubscribeEngine engine; + + @Override + public void setContextManager(final ContextManager contextManager) { + engine = new ShardingTableSubscribeEngine(contextManager); + } @Subscribe @Override public synchronized void renew(final AlterShardingTableEvent event) { - if (!event.getActiveVersion().equals(contextManager.getInstanceContext().getModeContextManager().getActiveVersionByKey(event.getActiveVersionKey()))) { - return; - } - String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); - ShardingTableRuleConfiguration toBeChangedConfig = new YamlShardingTableRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlTableRuleConfiguration.class)); - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = database.getRuleMetaData().findSingleRule(ShardingRule.class) - .map(optional -> (ShardingRuleConfiguration) optional.getConfiguration()).orElseGet(ShardingRuleConfiguration::new); - config.getTables().removeIf(each -> each.getLogicTable().equals(event.getItemName())); - config.getTables().add(toBeChangedConfig); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } @Subscribe @Override public synchronized void renew(final DropShardingTableEvent event) { - if (!contextManager.getMetaDataContexts().getMetaData().containsDatabase(event.getDatabaseName())) { - return; - } - ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); - ShardingRuleConfiguration config = (ShardingRuleConfiguration) database.getRuleMetaData().getSingleRule(ShardingRule.class).getConfiguration(); - config.getTables().removeIf(each -> each.getLogicTable().equals(event.getItemName())); - contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), config)); + engine.renew(event); } } diff --git a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/subscriber/SingleTableSubscribeEngine.java b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/subscriber/SingleTableSubscribeEngine.java index b3ba41044d813..3c3d14e0b1ff8 100644 --- a/kernel/single/core/src/main/java/org/apache/shardingsphere/single/subscriber/SingleTableSubscribeEngine.java +++ b/kernel/single/core/src/main/java/org/apache/shardingsphere/single/subscriber/SingleTableSubscribeEngine.java @@ -38,7 +38,7 @@ public SingleTableSubscribeEngine(final ContextManager contextManager) { } @Override - protected SingleRuleConfiguration swapRuleItemConfigurationFromEvent(final String yamlContent) { + protected SingleRuleConfiguration swapRuleItemConfigurationFromEvent(final AlterRuleItemEvent event, final String yamlContent) { return new YamlSingleRuleConfigurationSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlSingleRuleConfiguration.class)); } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscribeEngine.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscribeEngine.java index 7d06946cef3b8..fa48d6b0216f8 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscribeEngine.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/subsciber/RuleItemChangedSubscribeEngine.java @@ -48,7 +48,7 @@ public final void renew(final AlterRuleItemEvent event) { String yamlContent = contextManager.getInstanceContext().getModeContextManager().getVersionPathByActiveVersionKey(event.getActiveVersionKey(), event.getActiveVersion()); ShardingSphereDatabase database = contextManager.getMetaDataContexts().getMetaData().getDatabases().get(event.getDatabaseName()); T currentRuleConfig = findRuleConfiguration(database); - changeRuleItemConfiguration(event, currentRuleConfig, swapRuleItemConfigurationFromEvent(yamlContent)); + changeRuleItemConfiguration(event, currentRuleConfig, swapRuleItemConfigurationFromEvent(event, yamlContent)); contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), currentRuleConfig)); } @@ -67,7 +67,7 @@ public final void renew(final DropRuleItemEvent event) { contextManager.getInstanceContext().getEventBusContext().post(new DatabaseRuleConfigurationChangedEvent(event.getDatabaseName(), currentRuleConfig)); } - protected abstract I swapRuleItemConfigurationFromEvent(String yamlContent); + protected abstract I swapRuleItemConfigurationFromEvent(AlterRuleItemEvent event, String yamlContent); protected abstract T findRuleConfiguration(ShardingSphereDatabase database);