diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowImportRuleConfigurationProvider.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowImportRuleConfigurationProvider.java deleted file mode 100644 index 0c00be2c6d92c..0000000000000 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowImportRuleConfigurationProvider.java +++ /dev/null @@ -1,72 +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.shadow.checker; - -import org.apache.shardingsphere.infra.config.rule.checker.ImportRuleConfigurationProvider; -import org.apache.shardingsphere.infra.exception.rule.DuplicateRuleException; -import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; -import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; -import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; -import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; - -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.Map.Entry; -import java.util.stream.Collectors; - -/** - * Shadow import rule configuration provider. - */ -public final class ShadowImportRuleConfigurationProvider implements ImportRuleConfigurationProvider { - - @Override - public void check(final String databaseName, final ShadowRuleConfiguration ruleConfig) { - checkTables(databaseName, ruleConfig); - checkShadowAlgorithms(ruleConfig); - } - - private void checkTables(final String databaseName, final ShadowRuleConfiguration ruleConfig) { - Collection tableNames = ruleConfig.getTables().keySet(); - Collection duplicatedTables = tableNames.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream() - .filter(each -> each.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(duplicatedTables.isEmpty(), () -> new DuplicateRuleException("SHADOW", databaseName, duplicatedTables)); - } - - private void checkShadowAlgorithms(final ShadowRuleConfiguration ruleConfig) { - ruleConfig.getShadowAlgorithms().values().forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getType(), each.getProps())); - } - - @Override - public Collection getRequiredDataSourceNames(final ShadowRuleConfiguration ruleConfig) { - Collection result = new LinkedHashSet<>(); - ruleConfig.getDataSources().forEach(each -> { - if (null != each.getShadowDataSourceName()) { - result.add(each.getShadowDataSourceName()); - } - if (null != each.getProductionDataSourceName()) { - result.add(each.getProductionDataSourceName()); - } - }); - return result; - } - - @Override - public Class getType() { - return ShadowRuleConfiguration.class; - } -} diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java index 86bb0103fa6c5..ae58d08e91f66 100644 --- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java +++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationChecker.java @@ -19,8 +19,10 @@ import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker; -import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; +import org.apache.shardingsphere.infra.exception.rule.DuplicateRuleException; +import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration; import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration; @@ -29,13 +31,16 @@ import org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredShadowAlgorithmException; import org.apache.shardingsphere.shadow.exception.metadata.MissingRequiredShadowConfigurationException; import org.apache.shardingsphere.shadow.exception.metadata.ShadowDataSourceMappingNotFoundException; +import org.apache.shardingsphere.shadow.spi.ShadowAlgorithm; import javax.sql.DataSource; import java.util.Collection; import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; /** * Shadow rule configuration checker. @@ -44,6 +49,8 @@ public final class ShadowRuleConfigurationChecker implements RuleConfigurationCh @Override public void check(final String databaseName, final ShadowRuleConfiguration ruleConfig, final Map dataSourceMap, final Collection builtRules) { + checkShadowAlgorithms(ruleConfig); + checkTablesNotDuplicated(databaseName, ruleConfig); Map dataSources = initShadowDataSources(ruleConfig.getDataSources()); checkDataSources(dataSources, dataSourceMap, databaseName); Map shadowTables = ruleConfig.getTables(); @@ -56,6 +63,17 @@ public void check(final String databaseName, final ShadowRuleConfiguration ruleC checkShadowTableAlgorithmsReferences(shadowTables, databaseName); } + private void checkShadowAlgorithms(final ShadowRuleConfiguration ruleConfig) { + ruleConfig.getShadowAlgorithms().values().forEach(each -> TypedSPILoader.checkService(ShadowAlgorithm.class, each.getType(), each.getProps())); + } + + private void checkTablesNotDuplicated(final String databaseName, final ShadowRuleConfiguration ruleConfig) { + Collection tableNames = ruleConfig.getTables().keySet(); + Collection duplicatedTables = tableNames.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream() + .filter(each -> each.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet()); + ShardingSpherePreconditions.checkState(duplicatedTables.isEmpty(), () -> new DuplicateRuleException("SHADOW", databaseName, duplicatedTables)); + } + private void checkDataSources(final Map shadowDataSources, final Map dataSourceMap, final String databaseName) { Set dataSourceName = dataSourceMap.keySet(); for (Entry entry : shadowDataSources.entrySet()) { @@ -113,6 +131,20 @@ private Map initShadowDataSources(final C return result; } + @Override + public Collection getRequiredDataSourceNames(final ShadowRuleConfiguration ruleConfig) { + Collection result = new LinkedHashSet<>(); + ruleConfig.getDataSources().forEach(each -> { + if (null != each.getShadowDataSourceName()) { + result.add(each.getShadowDataSourceName()); + } + if (null != each.getProductionDataSourceName()) { + result.add(each.getProductionDataSourceName()); + } + }); + return result; + } + @Override public int getOrder() { return ShadowOrder.ORDER; diff --git a/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.ImportRuleConfigurationProvider b/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.ImportRuleConfigurationProvider deleted file mode 100644 index 083093ff316e3..0000000000000 --- a/features/shadow/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.config.rule.checker.ImportRuleConfigurationProvider +++ /dev/null @@ -1,18 +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. -# - -org.apache.shardingsphere.shadow.checker.ShadowImportRuleConfigurationProvider diff --git a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java index 0da4b74b984d4..2a0cc53bf2f28 100644 --- a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java +++ b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/checker/ShadowRuleConfigurationCheckerTest.java @@ -55,7 +55,7 @@ private ShadowRuleConfiguration createShadowRuleConfiguration() { } private AlgorithmConfiguration createAlgorithmConfiguration() { - return new AlgorithmConfiguration("user-id-insert-match-algorithm", + return new AlgorithmConfiguration("REGEX_MATCH", PropertiesBuilder.build(new Property("column", "shadow"), new Property("operation", "insert"), new Property("regex", "[1]"))); } }