From 9462a44726244abd6f3830735fd474c70dca6997 Mon Sep 17 00:00:00 2001 From: yx9o Date: Tue, 12 Mar 2024 14:50:41 +0800 Subject: [PATCH] Add BroadcastImportRuleConfigurationProvider (#30460) --- ...adcastImportRuleConfigurationProvider.java | 53 +++++++++++++++++++ ...i.database.ImportRuleConfigurationProvider | 18 +++++++ ...mlDatabaseConfigurationImportExecutor.java | 14 +---- 3 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastImportRuleConfigurationProvider.java create mode 100644 features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastImportRuleConfigurationProvider.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastImportRuleConfigurationProvider.java new file mode 100644 index 0000000000000..e11ab484b9f6d --- /dev/null +++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/provider/BroadcastImportRuleConfigurationProvider.java @@ -0,0 +1,53 @@ +/* + * 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.distsql.handler.provider; + +import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; +import org.apache.shardingsphere.broadcast.rule.BroadcastRule; +import org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; +import org.apache.shardingsphere.infra.instance.InstanceContext; +import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; +import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule; + +import java.util.LinkedHashMap; +import java.util.Map.Entry; +import java.util.stream.Collectors; + +/** + * Broadcast import rule configuration provider. + */ +public final class BroadcastImportRuleConfigurationProvider implements ImportRuleConfigurationProvider { + + @Override + public void check(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig) { + } + + @Override + public DatabaseRule build(final ShardingSphereDatabase database, final RuleConfiguration ruleConfig, final InstanceContext instanceContext) { + return new BroadcastRule((BroadcastRuleConfiguration) ruleConfig, database.getName(), + database.getResourceMetaData().getStorageUnits().entrySet().stream() + .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), + database.getRuleMetaData().getRules()); + } + + @Override + public Class getType() { + return BroadcastRuleConfiguration.class; + } +} diff --git a/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider b/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider new file mode 100644 index 0000000000000..cd1fe6c6ae379 --- /dev/null +++ b/features/broadcast/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider @@ -0,0 +1,18 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +org.apache.shardingsphere.broadcast.distsql.handler.provider.BroadcastImportRuleConfigurationProvider diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java index 509322df9121f..7720a2984dd5a 100644 --- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java +++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/util/YamlDatabaseConfigurationImportExecutor.java @@ -17,8 +17,6 @@ package org.apache.shardingsphere.proxy.backend.util; -import org.apache.shardingsphere.broadcast.api.config.BroadcastRuleConfiguration; -import org.apache.shardingsphere.broadcast.rule.BroadcastRule; import org.apache.shardingsphere.distsql.handler.engine.update.ral.rule.spi.database.ImportRuleConfigurationProvider; import org.apache.shardingsphere.distsql.handler.exception.datasource.MissingRequiredDataSourcesException; import org.apache.shardingsphere.distsql.handler.exception.storageunit.InvalidStorageUnitsException; @@ -141,9 +139,7 @@ private void addRules(final String databaseName, final Collection ruleConfigs, final RuleConfiguration ruleConfig, final ShardingSphereDatabase database) { - if (ruleConfig instanceof BroadcastRuleConfiguration) { - addBroadcastRuleConfiguration((BroadcastRuleConfiguration) ruleConfig, ruleConfigs, database); - } else if (ruleConfig instanceof SingleRuleConfiguration) { + if (ruleConfig instanceof SingleRuleConfiguration) { addSingleRuleConfiguration((SingleRuleConfiguration) ruleConfig, ruleConfigs, database); } else { ImportRuleConfigurationProvider provider = TypedSPILoader.getService(ImportRuleConfigurationProvider.class, ruleConfig.getClass()); @@ -164,14 +160,6 @@ private Map swapToRuleConfigs(final Collection ruleConfigs, final ShardingSphereDatabase database) { - ruleConfigs.add(ruleConfig); - database.getRuleMetaData().getRules().add(new BroadcastRule(ruleConfig, database.getName(), - database.getResourceMetaData().getStorageUnits().entrySet().stream() - .collect(Collectors.toMap(Entry::getKey, entry -> entry.getValue().getDataSource(), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)), - database.getRuleMetaData().getRules())); - } - private void addSingleRuleConfiguration(final SingleRuleConfiguration ruleConfig, final Collection ruleConfigs, final ShardingSphereDatabase database) { ruleConfigs.add(ruleConfig); database.getRuleMetaData().getRules().add(