From 01f4968387cb1bb97c3079767427633442ab4bd6 Mon Sep 17 00:00:00 2001 From: Raigor Date: Mon, 26 Dec 2022 16:17:53 +0800 Subject: [PATCH] Revise #23086, optimize some parameters and method naming. (#23096) * Revise #23086, optimize some parameters and method naming. * Change getIdentical to getDuplicated. * Change require to required * Change resources to dataSources * Change resources to dataSources --- ...eadwriteSplittingRuleStatementChecker.java | 50 ++++----- .../checker/ShadowRuleStatementChecker.java | 100 +++++++----------- .../ShadowRuleStatementConverter.java | 22 ++-- .../ShadowRuleStatementSupporter.java | 8 +- ...efaultShadowAlgorithmStatementUpdater.java | 18 ++-- .../AlterShadowRuleStatementUpdater.java | 56 ++++------ .../CreateShadowRuleStatementUpdater.java | 46 ++++---- ...efaultShadowAlgorithmStatementUpdater.java | 11 +- .../DropShadowAlgorithmStatementUpdater.java | 22 ++-- .../DropShadowRuleStatementUpdater.java | 2 +- .../AlterShadowRuleStatementUpdaterTest.java | 7 +- ...scoveryRuleConfigurationImportChecker.java | 12 +-- ...littingRuleConfigurationImportChecker.java | 26 ++--- 13 files changed, 155 insertions(+), 225 deletions(-) diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java index 015daf15cc371..6b57e3dac9f6e 100644 --- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java +++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/checker/ReadwriteSplittingRuleStatementChecker.java @@ -59,8 +59,8 @@ public final class ReadwriteSplittingRuleStatementChecker { public static void checkCreation(final ShardingSphereDatabase database, final Collection segments, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { String databaseName = database.getName(); checkDuplicateRuleNames(databaseName, segments, currentRuleConfig, database.getResourceMetaData()); - checkResourcesExist(databaseName, segments, database); - checkDuplicateResourceNames(databaseName, segments, currentRuleConfig, true); + checkDataSourcesExist(databaseName, segments, database); + checkDuplicatedDataSourceNames(databaseName, segments, currentRuleConfig, true); checkLoadBalancers(segments); } @@ -76,8 +76,8 @@ public static void checkAlteration(final ShardingSphereDatabase database, final checkRuleConfigurationExist(database, currentRuleConfig); checkDuplicateRuleNamesWithSelf(databaseName, segments); checkRuleNamesExist(segments, currentRuleConfig, databaseName); - checkResourcesExist(databaseName, segments, database); - checkDuplicateResourceNames(databaseName, segments, currentRuleConfig, false); + checkDataSourcesExist(databaseName, segments, database); + checkDuplicatedDataSourceNames(databaseName, segments, currentRuleConfig, false); checkLoadBalancers(segments); } @@ -136,39 +136,39 @@ private static void checkDuplicateRuleNamesWithRuleConfiguration(final String da ShardingSpherePreconditions.checkState(duplicateRuleNames.isEmpty(), () -> new DuplicateRuleException("Readwrite splitting", databaseName, duplicateRuleNames)); } - private static void checkResourcesExist(final String databaseName, final Collection segments, final ShardingSphereDatabase database) { - Collection requireResources = new LinkedHashSet<>(); - Collection requireDiscoverableResources = new LinkedHashSet<>(); + private static void checkDataSourcesExist(final String databaseName, final Collection segments, final ShardingSphereDatabase database) { + Collection requiredDataSources = new LinkedHashSet<>(); + Collection requiredLogicalDataSources = new LinkedHashSet<>(); segments.forEach(each -> { if (Strings.isNullOrEmpty(each.getAutoAwareResource())) { - requireResources.add(each.getWriteDataSource()); - requireResources.addAll(each.getReadDataSources()); + requiredDataSources.add(each.getWriteDataSource()); + requiredDataSources.addAll(each.getReadDataSources()); } else { - requireDiscoverableResources.add(each.getAutoAwareResource()); + requiredLogicalDataSources.add(each.getAutoAwareResource()); } }); - Collection notExistResources = database.getResourceMetaData().getNotExistedResources(requireResources); - ShardingSpherePreconditions.checkState(notExistResources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistResources)); - Collection logicResources = getLogicResources(database); - Collection notExistLogicResources = requireDiscoverableResources.stream().filter(each -> !logicResources.contains(each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(notExistLogicResources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistLogicResources)); + Collection notExistedDataSources = database.getResourceMetaData().getNotExistedResources(requiredDataSources); + ShardingSpherePreconditions.checkState(notExistedDataSources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistedDataSources)); + Collection logicalDataSources = getLogicDataSources(database); + Collection notExistedLogicalDataSources = requiredLogicalDataSources.stream().filter(each -> !logicalDataSources.contains(each)).collect(Collectors.toSet()); + ShardingSpherePreconditions.checkState(notExistedLogicalDataSources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistedLogicalDataSources)); } @SuppressWarnings("unchecked") - private static Collection getLogicResources(final ShardingSphereDatabase database) { + private static Collection getLogicDataSources(final ShardingSphereDatabase database) { Collection result = new LinkedHashSet<>(); Optional exportableRule = database.getRuleMetaData().findRules(ExportableRule.class).stream() .filter(each -> new RuleExportEngine(each).containExportableKey(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES))).findAny(); exportableRule.ifPresent(optional -> { Map exportData = new RuleExportEngine(optional).export(Collections.singletonList(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES)); - Collection logicResources = ((Map) exportData.getOrDefault(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, Collections.emptyMap())).keySet(); - result.addAll(logicResources); + Collection logicalDataSources = ((Map) exportData.getOrDefault(ExportableConstants.EXPORT_DB_DISCOVERY_PRIMARY_DATA_SOURCES, Collections.emptyMap())).keySet(); + result.addAll(logicalDataSources); }); return result; } - private static void checkDuplicateResourceNames(final String databaseName, final Collection segments, - final ReadwriteSplittingRuleConfiguration currentRuleConfig, final boolean isCreating) { + private static void checkDuplicatedDataSourceNames(final String databaseName, final Collection segments, + final ReadwriteSplittingRuleConfiguration currentRuleConfig, final boolean isCreating) { Collection existedWriteDataSourceNames = new HashSet<>(); Collection existedReadDataSourceNames = new HashSet<>(); if (null != currentRuleConfig) { @@ -180,15 +180,15 @@ private static void checkDuplicateResourceNames(final String databaseName, final } } } - checkDuplicateWriteResourceNames(databaseName, segments, existedWriteDataSourceNames); - checkDuplicateReadResourceNames(databaseName, segments, existedReadDataSourceNames); + checkDuplicateWriteDataSourceNames(databaseName, segments, existedWriteDataSourceNames); + checkDuplicateReadDataSourceNames(databaseName, segments, existedReadDataSourceNames); } private static Collection getToBeAlteredRuleNames(final Collection segments) { return segments.stream().map(ReadwriteSplittingRuleSegment::getName).collect(Collectors.toSet()); } - private static void checkDuplicateWriteResourceNames(final String databaseName, final Collection segments, final Collection writeDataSourceNames) { + private static void checkDuplicateWriteDataSourceNames(final String databaseName, final Collection segments, final Collection writeDataSourceNames) { for (final ReadwriteSplittingRuleSegment each : segments) { if (!Strings.isNullOrEmpty(each.getWriteDataSource())) { String writeDataSource = each.getWriteDataSource(); @@ -198,8 +198,8 @@ private static void checkDuplicateWriteResourceNames(final String databaseName, } } - private static void checkDuplicateReadResourceNames(final String databaseName, final Collection segments, - final Collection readDataSourceNames) { + private static void checkDuplicateReadDataSourceNames(final String databaseName, final Collection segments, + final Collection readDataSourceNames) { for (ReadwriteSplittingRuleSegment each : segments) { if (null != each.getReadDataSources()) { for (String readDataSource : each.getReadDataSources()) { diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java index 190df6753e3a6..ebb6806139e95 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/checker/ShadowRuleStatementChecker.java @@ -17,13 +17,13 @@ package org.apache.shardingsphere.shadow.distsql.handler.checker; -import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.distsql.handler.exception.DistSQLException; -import org.apache.shardingsphere.distsql.handler.exception.resource.MissingRequiredResourcesException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; +import org.apache.shardingsphere.distsql.handler.exception.resource.MissingRequiredResourcesException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions; +import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.distsql.parser.segment.ShadowAlgorithmSegment; import java.util.Collection; @@ -37,110 +37,82 @@ */ public class ShadowRuleStatementChecker { - public static final String SHADOW = "shadow"; - /** - * Check if the configuration exists. + * Check if the rule configuration exists. * * @param databaseName database name - * @param config configuration + * @param ruleConfig rule configuration */ - public static void checkConfigurationExist(final String databaseName, final DatabaseRuleConfiguration config) { - ShardingSpherePreconditions.checkNotNull(config, () -> new MissingRequiredRuleException(SHADOW, databaseName)); + public static void checkRuleConfigurationExists(final String databaseName, final ShadowRuleConfiguration ruleConfig) { + ShardingSpherePreconditions.checkNotNull(ruleConfig, () -> new MissingRequiredRuleException("shadow", databaseName)); } /** - * Check if resources exist in meta data. + * Check if storage units exist in meta data. * - * @param resources resource being checked + * @param requiredStorageUnits required storage units * @param database database */ - public static void checkResourceExist(final Collection resources, final ShardingSphereDatabase database) { - Collection notExistedResources = database.getResourceMetaData().getNotExistedResources(resources); - ShardingSpherePreconditions.checkState(notExistedResources.isEmpty(), () -> new MissingRequiredResourcesException(database.getName(), notExistedResources)); + public static void checkStorageUnitsExist(final Collection requiredStorageUnits, final ShardingSphereDatabase database) { + Collection notExistedStorageUnits = database.getResourceMetaData().getNotExistedResources(requiredStorageUnits); + ShardingSpherePreconditions.checkState(notExistedStorageUnits.isEmpty(), () -> new MissingRequiredResourcesException(database.getName(), notExistedStorageUnits)); } /** - * Check the completeness of the algorithm. + * Check the completeness of the algorithms. * - * @param algorithmSegments algorithmSegments to be checked + * @param algorithmSegments to be checked segments */ public static void checkAlgorithmCompleteness(final Collection algorithmSegments) { Set incompleteAlgorithms = algorithmSegments.stream().filter(each -> !each.isComplete()).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(incompleteAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException(SHADOW)); - } - - /** - * Check if the rules exist. - * - * @param requireRules require rules - * @param currentRules current rules - * @param thrower thrower - */ - public static void checkRulesExist(final Collection requireRules, - final Collection currentRules, final Function, DistSQLException> thrower) { - ShadowRuleStatementChecker.checkAnyDifferent(requireRules, currentRules, thrower); - } - - /** - * Check if the algorithms exist. - * - * @param requireAlgorithms require algorithms - * @param currentAlgorithms current algorithms - * @param thrower thrower - */ - public static void checkAlgorithmExist(final Collection requireAlgorithms, - final Collection currentAlgorithms, final Function, DistSQLException> thrower) { - ShadowRuleStatementChecker.checkAnyDifferent(requireAlgorithms, currentAlgorithms, thrower); + ShardingSpherePreconditions.checkState(incompleteAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException("shadow")); } /** - * Check for any duplicate data in the rules, and throw the specified exception. + * Check if there are duplicated rules. * * @param rules rules to be checked * @param thrower exception thrower */ - public static void checkAnyDuplicate(final Collection rules, final Function, DistSQLException> thrower) { - Collection duplicateRequire = getDuplicate(rules); - ShardingSpherePreconditions.checkState(duplicateRequire.isEmpty(), () -> thrower.apply(duplicateRequire)); + public static void checkDuplicated(final Collection rules, final Function, DistSQLException> thrower) { + Collection duplicated = getDuplicated(rules); + ShardingSpherePreconditions.checkState(duplicated.isEmpty(), () -> thrower.apply(duplicated)); } /** - * Check if there are duplicates in the rules, and throw the specified exception. + * Check if there are duplicated rules. * - * @param requireRules rules to be checked - * @param currentRules rules to be checked + * @param requiredRules required rules + * @param currentRules current rules * @param thrower exception thrower */ - public static void checkAnyDuplicate(final Collection requireRules, - final Collection currentRules, final Function, DistSQLException> thrower) { - Collection identical = getIdentical(requireRules, currentRules); - ShardingSpherePreconditions.checkState(identical.isEmpty(), () -> thrower.apply(identical)); + public static void checkDuplicated(final Collection requiredRules, final Collection currentRules, final Function, DistSQLException> thrower) { + Collection duplicated = getDuplicated(requiredRules, currentRules); + ShardingSpherePreconditions.checkState(duplicated.isEmpty(), () -> thrower.apply(duplicated)); } /** - * Check for any different data in the rules, and throw the specified exception. + * Check the required rules existed. * - * @param requireRules rules to be checked - * @param currentRules rules to be checked + * @param requiredRules required rules + * @param currentRules current rules * @param thrower exception thrower */ - public static void checkAnyDifferent(final Collection requireRules, - final Collection currentRules, final Function, DistSQLException> thrower) { - Collection different = getDifferent(requireRules, currentRules); - ShardingSpherePreconditions.checkState(different.isEmpty(), () -> thrower.apply(different)); + public static void checkExisted(final Collection requiredRules, final Collection currentRules, final Function, DistSQLException> thrower) { + Collection notExisted = getNotExisted(requiredRules, currentRules); + ShardingSpherePreconditions.checkState(notExisted.isEmpty(), () -> thrower.apply(notExisted)); } - private static Collection getDuplicate(final Collection require) { - return require.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream() + private static Collection getDuplicated(final Collection names) { + return names.stream().collect(Collectors.groupingBy(each -> each, Collectors.counting())).entrySet().stream() .filter(each -> each.getValue() > 1).map(Map.Entry::getKey).collect(Collectors.toSet()); } - private static Collection getDifferent(final Collection require, final Collection current) { - return require.stream().filter(each -> !current.contains(each)).collect(Collectors.toSet()); + private static Collection getDuplicated(final Collection required, final Collection current) { + return required.stream().filter(current::contains).collect(Collectors.toSet()); } - private static Collection getIdentical(final Collection require, final Collection current) { - return require.stream().filter(current::contains).collect(Collectors.toSet()); + private static Collection getNotExisted(final Collection required, final Collection current) { + return required.stream().filter(each -> !current.contains(each)).collect(Collectors.toSet()); } } diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleStatementConverter.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleStatementConverter.java index dc1bf47c28cd4..bbad23886cb51 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleStatementConverter.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/converter/ShadowRuleStatementConverter.java @@ -45,20 +45,20 @@ public final class ShadowRuleStatementConverter { /** * Convert shadow rule segments to shadow rule configuration. * - * @param rules shadow rule statements + * @param segments shadow rule segments * @return shadow rule configuration */ - public static ShadowRuleConfiguration convert(final Collection rules) { + public static ShadowRuleConfiguration convert(final Collection segments) { ShadowRuleConfiguration result = new ShadowRuleConfiguration(); - result.setShadowAlgorithms(getShadowAlgorithms(rules)); - result.setDataSources(getDataSource(rules)); - result.setTables(getTables(rules)); + result.setShadowAlgorithms(getShadowAlgorithms(segments)); + result.setDataSources(getDataSource(segments)); + result.setTables(getTables(segments)); return result; } - private static Map getTables(final Collection rules) { + private static Map getTables(final Collection segments) { Map result = new HashMap<>(); - rules.forEach(each -> { + segments.forEach(each -> { Map currentRuleTableConfig = each.getShadowTableRules().entrySet().stream() .collect(Collectors.toMap(Entry::getKey, entry -> buildShadowTableConfiguration(each.getRuleName(), entry), ShadowRuleStatementSupporter::mergeConfiguration)); currentRuleTableConfig.forEach((key, value) -> result.merge(key, value, ShadowRuleStatementSupporter::mergeConfiguration)); @@ -70,14 +70,14 @@ private static ShadowTableConfiguration buildShadowTableConfiguration(final Stri return new ShadowTableConfiguration(new ArrayList<>(Collections.singleton(ruleName)), entry.getValue().stream().map(ShadowAlgorithmSegment::getAlgorithmName).collect(Collectors.toList())); } - private static Collection getDataSource(final Collection rules) { + private static Collection getDataSource(final Collection segments) { Collection result = new LinkedList<>(); - rules.forEach(each -> result.add(new ShadowDataSourceConfiguration(each.getRuleName(), each.getSource(), each.getShadow()))); + segments.forEach(each -> result.add(new ShadowDataSourceConfiguration(each.getRuleName(), each.getSource(), each.getShadow()))); return result; } - private static Map getShadowAlgorithms(final Collection rules) { - return rules.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) + private static Map getShadowAlgorithms(final Collection segments) { + return segments.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) .collect(Collectors.toMap(ShadowAlgorithmSegment::getAlgorithmName, ShadowRuleStatementConverter::buildAlgorithmConfiguration)); } diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/supporter/ShadowRuleStatementSupporter.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/supporter/ShadowRuleStatementSupporter.java index b5b81069606b0..c68767201814e 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/supporter/ShadowRuleStatementSupporter.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/supporter/ShadowRuleStatementSupporter.java @@ -47,7 +47,7 @@ public static List getRuleNames(final ShadowRuleConfiguration ruleConfig } /** - * Get rule names from the rules. + * Get rule names from the segments. * * @param segments shadow rule segments * @return rule names @@ -63,12 +63,12 @@ private static List getDataSources(final ShadowRuleConfiguration ruleCon } /** - * Get the resource names from the rules. + * Get storage unit names from the segments. * * @param segments shadow rule segments - * @return resource names + * @return storage unit names */ - public static List getResourceNames(final Collection segments) { + public static List getStorageUnitNames(final Collection segments) { return segments.isEmpty() ? Collections.emptyList() : segments.stream().map(each -> Arrays.asList(each.getSource(), each.getShadow())).flatMap(Collection::stream).filter(Objects::nonNull).collect(Collectors.toList()); diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmStatementUpdater.java index 20c6a81bf1509..8e7d385c3dcc6 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterDefaultShadowAlgorithmStatementUpdater.java @@ -40,8 +40,6 @@ */ public final class AlterDefaultShadowAlgorithmStatementUpdater implements RuleDefinitionAlterUpdater { - private static final String SHADOW = "shadow"; - private static final String DEFAULT_ALGORITHM_NAME = "default_shadow_algorithm"; @Override @@ -65,30 +63,26 @@ public void updateCurrentRuleConfiguration(final ShadowRuleConfiguration current @Override public void checkSQLStatement(final ShardingSphereDatabase database, final AlterDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { - checkConfigurationExist(database.getName(), currentRuleConfig); + ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); checkAlgorithms(database.getName(), sqlStatement.getShadowAlgorithmSegment().getAlgorithmSegment(), currentRuleConfig); } - private void checkConfigurationExist(final String databaseName, final ShadowRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkConfigurationExist(databaseName, currentRuleConfig); - } - private void checkAlgorithms(final String databaseName, final AlgorithmSegment algorithmSegment, final ShadowRuleConfiguration currentRuleConfig) { checkAlgorithmCompleteness(algorithmSegment); checkAlgorithmType(algorithmSegment); - Collection requireAlgorithmNames = Collections.singleton(DEFAULT_ALGORITHM_NAME); - ShadowRuleStatementChecker.checkAlgorithmExist(requireAlgorithmNames, currentRuleConfig.getShadowAlgorithms().keySet(), - different -> new MissingRequiredAlgorithmException(SHADOW, databaseName, different)); + Collection requiredAlgorithmNames = Collections.singleton(DEFAULT_ALGORITHM_NAME); + ShadowRuleStatementChecker.checkExisted(requiredAlgorithmNames, currentRuleConfig.getShadowAlgorithms().keySet(), + notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", databaseName, notExistedAlgorithms)); } private static void checkAlgorithmCompleteness(final AlgorithmSegment algorithmSegment) { boolean isCompleteAlgorithm = !Strings.isNullOrEmpty(algorithmSegment.getName()) && !algorithmSegment.getProps().isEmpty(); - ShardingSpherePreconditions.checkState(isCompleteAlgorithm, () -> new InvalidAlgorithmConfigurationException(SHADOW)); + ShardingSpherePreconditions.checkState(isCompleteAlgorithm, () -> new InvalidAlgorithmConfigurationException("shadow")); } private void checkAlgorithmType(final AlgorithmSegment algorithmSegment) { String shadowAlgorithmType = algorithmSegment.getName(); - ShardingSpherePreconditions.checkState(ShadowAlgorithmFactory.contains(shadowAlgorithmType), () -> new InvalidAlgorithmConfigurationException(SHADOW, shadowAlgorithmType)); + ShardingSpherePreconditions.checkState(ShadowAlgorithmFactory.contains(shadowAlgorithmType), () -> new InvalidAlgorithmConfigurationException("shadow", shadowAlgorithmType)); } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleStatementUpdater.java index 8ede42027bc0e..bb3dbc3ed581d 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/AlterShadowRuleStatementUpdater.java @@ -17,13 +17,12 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; -import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment; -import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; -import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; -import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; +import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; +import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionAlterUpdater; +import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; @@ -39,7 +38,6 @@ import java.util.Collection; import java.util.Map; -import java.util.Set; import java.util.stream.Collectors; /** @@ -47,8 +45,6 @@ */ public final class AlterShadowRuleStatementUpdater implements RuleDefinitionAlterUpdater { - private static final String SHADOW = "shadow"; - @Override public RuleConfiguration buildToBeAlteredRuleConfiguration(final AlterShadowRuleStatement sqlStatement) { return ShadowRuleStatementConverter.convert(sqlStatement.getRules()); @@ -72,49 +68,35 @@ private void updateTables(final Map currentTab @Override public void checkSQLStatement(final ShardingSphereDatabase database, final AlterShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { String databaseName = database.getName(); - Collection rules = sqlStatement.getRules(); - checkConfigurationExist(databaseName, currentRuleConfig); - checkRuleNames(databaseName, rules, currentRuleConfig); - checkResources(database, rules); - checkAlgorithmCompleteness(sqlStatement); + ShadowRuleStatementChecker.checkRuleConfigurationExists(databaseName, currentRuleConfig); + checkRuleNames(databaseName, sqlStatement.getRules(), currentRuleConfig); + checkStorageUnits(database, sqlStatement.getRules()); checkAlgorithms(databaseName, sqlStatement.getRules()); checkAlgorithmType(sqlStatement); } - private void checkConfigurationExist(final String databaseName, final DatabaseRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkConfigurationExist(databaseName, currentRuleConfig); - } - - private void checkRuleNames(final String databaseName, final Collection rules, final ShadowRuleConfiguration currentRuleConfig) { + private void checkRuleNames(final String databaseName, final Collection segments, final ShadowRuleConfiguration currentRuleConfig) { Collection currentRuleNames = ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig); - Collection requireRuleNames = ShadowRuleStatementSupporter.getRuleNames(rules); - ShadowRuleStatementChecker.checkAnyDuplicate(requireRuleNames, duplicated -> new DuplicateRuleException(SHADOW, databaseName, duplicated)); - ShadowRuleStatementChecker.checkRulesExist(requireRuleNames, currentRuleNames, different -> new InvalidAlgorithmConfigurationException("shadow rule name ", different)); - } - - private void checkResources(final ShardingSphereDatabase database, final Collection rules) { - Collection requireResource = ShadowRuleStatementSupporter.getResourceNames(rules); - ShadowRuleStatementChecker.checkResourceExist(requireResource, database); + Collection requiredRuleNames = ShadowRuleStatementSupporter.getRuleNames(segments); + ShadowRuleStatementChecker.checkDuplicated(requiredRuleNames, duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated)); + ShadowRuleStatementChecker.checkExisted(requiredRuleNames, currentRuleNames, notExistedRules -> new MissingRequiredRuleException("Shadow", notExistedRules)); } - private static void checkAlgorithmCompleteness(final AlterShadowRuleStatement sqlStatement) { - Collection algorithmSegments = sqlStatement.getRules().stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) - .map(ShadowAlgorithmSegment::getAlgorithmSegment).collect(Collectors.toList()); - Set incompleteAlgorithms = algorithmSegments.stream().filter(each -> each.getName().isEmpty() || each.getProps().isEmpty()).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(incompleteAlgorithms.isEmpty(), () -> new InvalidAlgorithmConfigurationException(SHADOW)); + private void checkStorageUnits(final ShardingSphereDatabase database, final Collection segments) { + ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(segments), database); } private void checkAlgorithmType(final AlterShadowRuleStatement sqlStatement) { - Collection nonexistentAlgorithmTypes = sqlStatement.getRules().stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) + Collection invalidAlgorithmTypes = sqlStatement.getRules().stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) .map(each -> each.getAlgorithmSegment().getName()).collect(Collectors.toSet()).stream().filter(each -> !ShadowAlgorithmFactory.contains(each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(nonexistentAlgorithmTypes.isEmpty(), () -> new InvalidAlgorithmConfigurationException(SHADOW, nonexistentAlgorithmTypes)); + ShardingSpherePreconditions.checkState(invalidAlgorithmTypes.isEmpty(), () -> new InvalidAlgorithmConfigurationException("shadow", invalidAlgorithmTypes)); } - private void checkAlgorithms(final String databaseName, final Collection rules) { - Collection shadowAlgorithmSegment = ShadowRuleStatementSupporter.getShadowAlgorithmSegment(rules); - ShadowRuleStatementChecker.checkAlgorithmCompleteness(shadowAlgorithmSegment); - Collection requireAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(rules); - ShadowRuleStatementChecker.checkAnyDuplicate(requireAlgorithms, duplicated -> new AlgorithmInUsedException("Shadow", databaseName, duplicated)); + private void checkAlgorithms(final String databaseName, final Collection segments) { + Collection shadowAlgorithmSegments = ShadowRuleStatementSupporter.getShadowAlgorithmSegment(segments); + ShadowRuleStatementChecker.checkAlgorithmCompleteness(shadowAlgorithmSegments); + Collection requiredAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(segments); + ShadowRuleStatementChecker.checkDuplicated(requiredAlgorithms, duplicated -> new AlgorithmInUsedException("Shadow", databaseName, duplicated)); } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java index e9ff092ae1546..5ec6d5e73b3d2 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/CreateShadowRuleStatementUpdater.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.shadow.factory.ShadowAlgorithmFactory; import java.util.Collection; +import java.util.LinkedList; import java.util.Map; import java.util.stream.Collectors; @@ -41,17 +42,15 @@ */ public final class CreateShadowRuleStatementUpdater implements RuleDefinitionCreateUpdater { - private static final String SHADOW = "shadow"; - - private Collection identicalRuleNames; + private Collection duplicatedRuleNames = new LinkedList<>(); @Override public RuleConfiguration buildToBeCreatedRuleConfiguration(final CreateShadowRuleStatement sqlStatement) { - Collection rules = sqlStatement.getRules(); - if (null != identicalRuleNames && !identicalRuleNames.isEmpty()) { - rules = sqlStatement.getRules().stream().filter(each -> !identicalRuleNames.contains(each.getRuleName())).collect(Collectors.toSet()); + Collection segments = sqlStatement.getRules(); + if (!duplicatedRuleNames.isEmpty()) { + segments.removeIf(each -> duplicatedRuleNames.contains(each.getRuleName())); } - return ShadowRuleStatementConverter.convert(rules); + return ShadowRuleStatementConverter.convert(segments); } @Override @@ -70,37 +69,36 @@ private void updateTables(final Map currentTab @Override public void checkSQLStatement(final ShardingSphereDatabase database, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { String databaseName = database.getName(); - Collection rules = sqlStatement.getRules(); - checkRuleNames(databaseName, sqlStatement, currentRuleConfig); - checkResources(database, rules); - checkAlgorithms(databaseName, rules); - checkAlgorithmType(rules); + checkDuplicatedRules(databaseName, sqlStatement, currentRuleConfig); + checkStorageUnits(database, sqlStatement.getRules()); + checkAlgorithms(databaseName, sqlStatement.getRules()); + checkAlgorithmType(sqlStatement.getRules()); } - private void checkRuleNames(final String databaseName, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { + private void checkDuplicatedRules(final String databaseName, final CreateShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { Collection toBeCreatedRuleNames = ShadowRuleStatementSupporter.getRuleNames(sqlStatement.getRules()); - ShadowRuleStatementChecker.checkAnyDuplicate(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException(SHADOW, databaseName, duplicated)); + ShadowRuleStatementChecker.checkDuplicated(toBeCreatedRuleNames, duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated)); toBeCreatedRuleNames.retainAll(ShadowRuleStatementSupporter.getRuleNames(currentRuleConfig)); if (sqlStatement.isIfNotExists()) { - identicalRuleNames = toBeCreatedRuleNames; + duplicatedRuleNames = toBeCreatedRuleNames; return; } - ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException(SHADOW, databaseName, toBeCreatedRuleNames)); + ShardingSpherePreconditions.checkState(toBeCreatedRuleNames.isEmpty(), () -> new DuplicateRuleException("shadow", databaseName, toBeCreatedRuleNames)); } - private void checkResources(final ShardingSphereDatabase database, final Collection rules) { - ShadowRuleStatementChecker.checkResourceExist(ShadowRuleStatementSupporter.getResourceNames(rules), database); + private void checkStorageUnits(final ShardingSphereDatabase database, final Collection segments) { + ShadowRuleStatementChecker.checkStorageUnitsExist(ShadowRuleStatementSupporter.getStorageUnitNames(segments), database); } - private void checkAlgorithms(final String databaseName, final Collection rules) { - ShadowRuleStatementChecker.checkAlgorithmCompleteness(ShadowRuleStatementSupporter.getShadowAlgorithmSegment(rules)); - ShadowRuleStatementChecker.checkAnyDuplicate(ShadowRuleStatementSupporter.getAlgorithmNames(rules), duplicated -> new DuplicateRuleException(SHADOW, databaseName, duplicated)); + private void checkAlgorithms(final String databaseName, final Collection segments) { + ShadowRuleStatementChecker.checkAlgorithmCompleteness(ShadowRuleStatementSupporter.getShadowAlgorithmSegment(segments)); + ShadowRuleStatementChecker.checkDuplicated(ShadowRuleStatementSupporter.getAlgorithmNames(segments), duplicated -> new DuplicateRuleException("shadow", databaseName, duplicated)); } - private void checkAlgorithmType(final Collection rules) { - Collection nonexistentAlgorithmTypes = rules.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) + private void checkAlgorithmType(final Collection segments) { + Collection invalidAlgorithmTypes = segments.stream().flatMap(each -> each.getShadowTableRules().values().stream()).flatMap(Collection::stream) .map(each -> each.getAlgorithmSegment().getName()).collect(Collectors.toSet()).stream().filter(each -> !ShadowAlgorithmFactory.contains(each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(nonexistentAlgorithmTypes.isEmpty(), () -> new InvalidAlgorithmConfigurationException(SHADOW, nonexistentAlgorithmTypes)); + ShardingSpherePreconditions.checkState(invalidAlgorithmTypes.isEmpty(), () -> new InvalidAlgorithmConfigurationException("shadow", invalidAlgorithmTypes)); } @Override diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java index 86da9637277aa..dca15348d878d 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropDefaultShadowAlgorithmStatementUpdater.java @@ -19,7 +19,6 @@ import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; import org.apache.shardingsphere.distsql.handler.update.RuleDefinitionDropUpdater; -import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; @@ -33,25 +32,19 @@ */ public final class DropDefaultShadowAlgorithmStatementUpdater implements RuleDefinitionDropUpdater { - private static final String SHADOW = "shadow"; - @Override public void checkSQLStatement(final ShardingSphereDatabase database, final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) { return; } - checkConfigurationExist(database.getName(), currentRuleConfig); + ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); checkAlgorithm(database.getName(), sqlStatement, currentRuleConfig); } - private void checkConfigurationExist(final String databaseName, final DatabaseRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkConfigurationExist(databaseName, currentRuleConfig); - } - private void checkAlgorithm(final String databaseName, final DropDefaultShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfExists()) { ShardingSpherePreconditions.checkNotNull(currentRuleConfig.getDefaultShadowAlgorithmName(), - () -> new MissingRequiredAlgorithmException(SHADOW, databaseName, Collections.singleton("default"))); + () -> new MissingRequiredAlgorithmException("shadow", databaseName, Collections.singleton("default"))); } } diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java index 8c9adb3145ada..3700a9c46a021 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowAlgorithmStatementUpdater.java @@ -17,7 +17,6 @@ package org.apache.shardingsphere.shadow.distsql.handler.update; -import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration; import org.apache.shardingsphere.distsql.handler.exception.DistSQLException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.MissingRequiredAlgorithmException; @@ -42,36 +41,29 @@ */ public final class DropShadowAlgorithmStatementUpdater implements RuleDefinitionDropUpdater { - private static final String SHADOW = "shadow"; - @Override public void checkSQLStatement(final ShardingSphereDatabase database, final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (sqlStatement.isIfExists() && !isExistRuleConfig(currentRuleConfig)) { return; } - checkConfigurationExist(database.getName(), currentRuleConfig); + ShadowRuleStatementChecker.checkRuleConfigurationExists(database.getName(), currentRuleConfig); checkAlgorithm(database.getName(), sqlStatement, currentRuleConfig); } - private void checkConfigurationExist(final String databaseName, final DatabaseRuleConfiguration currentRuleConfig) { - ShadowRuleStatementChecker.checkConfigurationExist(databaseName, currentRuleConfig); - } - private void checkAlgorithm(final String databaseName, final DropShadowAlgorithmStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { Collection currentAlgorithms = ShadowRuleStatementSupporter.getAlgorithmNames(currentRuleConfig); - Collection requireAlgorithms = sqlStatement.getNames(); + Collection requiredAlgorithms = sqlStatement.getNames(); String defaultShadowAlgorithmName = currentRuleConfig.getDefaultShadowAlgorithmName(); if (!sqlStatement.isIfExists()) { - ShadowRuleStatementChecker.checkAlgorithmExist(requireAlgorithms, currentAlgorithms, different -> new MissingRequiredAlgorithmException(SHADOW, databaseName, different)); + ShadowRuleStatementChecker.checkExisted(requiredAlgorithms, currentAlgorithms, notExistedAlgorithms -> new MissingRequiredAlgorithmException("shadow", databaseName, notExistedAlgorithms)); } - checkAlgorithmInUsed(requireAlgorithms, getAlgorithmInUse(currentRuleConfig), identical -> new AlgorithmInUsedException("Sharding", databaseName, identical)); - ShardingSpherePreconditions.checkState(!requireAlgorithms.contains(defaultShadowAlgorithmName), + checkAlgorithmInUsed(requiredAlgorithms, getAlgorithmInUse(currentRuleConfig), identical -> new AlgorithmInUsedException("Sharding", databaseName, identical)); + ShardingSpherePreconditions.checkState(!requiredAlgorithms.contains(defaultShadowAlgorithmName), () -> new AlgorithmInUsedException("Shadow", databaseName, Collections.singleton(defaultShadowAlgorithmName))); } - private void checkAlgorithmInUsed(final Collection requireAlgorithms, final Collection currentAlgorithms, - final Function, DistSQLException> thrower) { - ShadowRuleStatementChecker.checkAnyDuplicate(requireAlgorithms, currentAlgorithms, thrower); + private void checkAlgorithmInUsed(final Collection requiredAlgorithms, final Collection currentAlgorithms, final Function, DistSQLException> thrower) { + ShadowRuleStatementChecker.checkDuplicated(requiredAlgorithms, currentAlgorithms, thrower); } private Collection getAlgorithmInUse(final ShadowRuleConfiguration currentRuleConfig) { diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java index 2f151f8c15899..69acf2b984055 100644 --- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java +++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/update/DropShadowRuleStatementUpdater.java @@ -49,7 +49,7 @@ private void checkConfigurationExisted(final String databaseName, final ShadowRu private void checkRuleExisted(final String databaseName, final DropShadowRuleStatement sqlStatement, final ShadowRuleConfiguration currentRuleConfig) { if (!sqlStatement.isIfExists()) { - ShadowRuleStatementChecker.checkRulesExist(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig), + ShadowRuleStatementChecker.checkExisted(sqlStatement.getNames(), getDataSourceNames(currentRuleConfig), notExistedRuleNames -> new MissingRequiredRuleException("Shadow", databaseName, notExistedRuleNames)); } } diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java index fd62b2bf3598b..69bd4775a44fe 100644 --- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java +++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/update/AlterShadowRuleStatementUpdaterTest.java @@ -17,12 +17,11 @@ package org.apache.shardingsphere.shadow.distsql.update; -import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment; -import org.apache.shardingsphere.distsql.handler.exception.resource.MissingRequiredResourcesException; import org.apache.shardingsphere.distsql.handler.exception.algorithm.AlgorithmInUsedException; +import org.apache.shardingsphere.distsql.handler.exception.resource.MissingRequiredResourcesException; import org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException; -import org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException; import org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException; +import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.ShardingSphereResourceMetaData; import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; @@ -83,7 +82,7 @@ public void assertExecuteWithDuplicateRuleName() { updater.checkSQLStatement(database, createSQLStatement(ruleSegment, ruleSegment), currentConfig); } - @Test(expected = InvalidAlgorithmConfigurationException.class) + @Test(expected = MissingRequiredRuleException.class) public void assertExecuteWithRuleNameNotInMetaData() { ShadowRuleSegment ruleSegment = new ShadowRuleSegment("ruleName", null, null, null); updater.checkSQLStatement(database, createSQLStatement(ruleSegment), currentConfig); diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/DatabaseDiscoveryRuleConfigurationImportChecker.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/DatabaseDiscoveryRuleConfigurationImportChecker.java index 99611c7f04250..ff2bc88eaaac7 100644 --- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/DatabaseDiscoveryRuleConfigurationImportChecker.java +++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/DatabaseDiscoveryRuleConfigurationImportChecker.java @@ -48,15 +48,15 @@ public void check(final ShardingSphereDatabase database, final DatabaseDiscovery return; } String databaseName = database.getName(); - checkResources(databaseName, database, currentRuleConfig); + checkDataSources(databaseName, database, currentRuleConfig); checkDiscoverTypeAndHeartbeat(databaseName, currentRuleConfig); } - private void checkResources(final String databaseName, final ShardingSphereDatabase database, final DatabaseDiscoveryRuleConfiguration currentRuleConfig) { - Collection requireResources = new LinkedHashSet<>(); - currentRuleConfig.getDataSources().forEach(each -> requireResources.addAll(each.getDataSourceNames())); - Collection notExistResources = database.getResourceMetaData().getNotExistedResources(requireResources); - ShardingSpherePreconditions.checkState(notExistResources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistResources)); + private void checkDataSources(final String databaseName, final ShardingSphereDatabase database, final DatabaseDiscoveryRuleConfiguration currentRuleConfig) { + Collection requiredDataSources = new LinkedHashSet<>(); + currentRuleConfig.getDataSources().forEach(each -> requiredDataSources.addAll(each.getDataSourceNames())); + Collection notExistedDataSources = database.getResourceMetaData().getNotExistedResources(requiredDataSources); + ShardingSpherePreconditions.checkState(notExistedDataSources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistedDataSources)); } private void checkDiscoverTypeAndHeartbeat(final String databaseName, final DatabaseDiscoveryRuleConfiguration currentRuleConfig) { diff --git a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ReadwriteSplittingRuleConfigurationImportChecker.java b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ReadwriteSplittingRuleConfigurationImportChecker.java index 83a78394c5bdd..1733a98a1b472 100644 --- a/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ReadwriteSplittingRuleConfigurationImportChecker.java +++ b/proxy/backend/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/common/checker/ReadwriteSplittingRuleConfigurationImportChecker.java @@ -46,34 +46,34 @@ public void check(final ShardingSphereDatabase database, final ReadwriteSplittin return; } String databaseName = database.getName(); - checkResources(databaseName, database, currentRuleConfig); + checkDataSources(databaseName, database, currentRuleConfig); checkLoadBalancers(currentRuleConfig); } - private void checkResources(final String databaseName, final ShardingSphereDatabase database, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { - Collection requireResources = new LinkedHashSet<>(); - Collection requireDiscoverableResources = new LinkedHashSet<>(); + private void checkDataSources(final String databaseName, final ShardingSphereDatabase database, final ReadwriteSplittingRuleConfiguration currentRuleConfig) { + Collection requiredDataSources = new LinkedHashSet<>(); + Collection requiredLogicalDataSources = new LinkedHashSet<>(); currentRuleConfig.getDataSources().forEach(each -> { if (null != each.getDynamicStrategy()) { - requireDiscoverableResources.add(each.getDynamicStrategy().getAutoAwareDataSourceName()); + requiredLogicalDataSources.add(each.getDynamicStrategy().getAutoAwareDataSourceName()); } if (null != each.getStaticStrategy()) { if (null != each.getStaticStrategy().getWriteDataSourceName()) { - requireResources.add(each.getStaticStrategy().getWriteDataSourceName()); + requiredDataSources.add(each.getStaticStrategy().getWriteDataSourceName()); } if (!each.getStaticStrategy().getReadDataSourceNames().isEmpty()) { - requireResources.addAll(each.getStaticStrategy().getReadDataSourceNames()); + requiredDataSources.addAll(each.getStaticStrategy().getReadDataSourceNames()); } } }); - Collection notExistResources = database.getResourceMetaData().getNotExistedResources(requireResources); - ShardingSpherePreconditions.checkState(notExistResources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistResources)); - Collection logicResources = getLogicResources(database); - Collection notExistLogicResources = requireDiscoverableResources.stream().filter(each -> !logicResources.contains(each)).collect(Collectors.toSet()); - ShardingSpherePreconditions.checkState(notExistLogicResources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistLogicResources)); + Collection notExistedDataSources = database.getResourceMetaData().getNotExistedResources(requiredDataSources); + ShardingSpherePreconditions.checkState(notExistedDataSources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistedDataSources)); + Collection logicalDataSources = getLogicDataSources(database); + Collection notExistedLogicalDataSources = requiredLogicalDataSources.stream().filter(each -> !logicalDataSources.contains(each)).collect(Collectors.toSet()); + ShardingSpherePreconditions.checkState(notExistedLogicalDataSources.isEmpty(), () -> new MissingRequiredResourcesException(databaseName, notExistedLogicalDataSources)); } - private Collection getLogicResources(final ShardingSphereDatabase database) { + private Collection getLogicDataSources(final ShardingSphereDatabase database) { return database.getRuleMetaData().getRules().stream().filter(each -> each instanceof DataSourceContainedRule) .map(each -> ((DataSourceContainedRule) each).getDataSourceMapper().keySet()).flatMap(Collection::stream).collect(Collectors.toCollection(LinkedHashSet::new)); }