From 91d84ad3cf9464ae8754b7de7ff55b561897e22e Mon Sep 17 00:00:00 2001 From: Raigor Date: Tue, 24 Dec 2024 19:30:35 +0800 Subject: [PATCH] Optimize props check for weight load balancer (#34140) --- .../ReadwriteSplittingRuleStatementChecker.java | 10 +++++----- .../InvalidAlgorithmConfigurationException.java | 4 ++++ 2 files changed, 9 insertions(+), 5 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 894a61dc1c994..b536498cb7182 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 @@ -237,10 +237,10 @@ private static void checkProperties(final ReadwriteSplittingRuleSegment each) { } private static void checkDataSource(final ReadwriteSplittingRuleSegment ruleSegment) { - for (Object each : ruleSegment.getLoadBalancer().getProps().keySet()) { - String dataSourceName = (String) each; - ShardingSpherePreconditions.checkState(ruleSegment.getReadDataSources().contains(dataSourceName) || ruleSegment.getWriteDataSource().equals(dataSourceName), - () -> new InvalidAlgorithmConfigurationException("Load balancer", ruleSegment.getLoadBalancer().getName())); - } + Collection weightKeys = ruleSegment.getLoadBalancer().getProps().stringPropertyNames(); + weightKeys.forEach(each -> ShardingSpherePreconditions.checkContains(ruleSegment.getReadDataSources(), each, + () -> new InvalidAlgorithmConfigurationException("Load balancer", ruleSegment.getLoadBalancer().getName(), String.format("Can not find read storge unit '%s'", each)))); + ruleSegment.getReadDataSources().forEach(each -> ShardingSpherePreconditions.checkContains(weightKeys, each, + () -> new InvalidAlgorithmConfigurationException("Load balancer", ruleSegment.getLoadBalancer().getName(), String.format("Weight of '%s' is required", each)))); } } diff --git a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/InvalidAlgorithmConfigurationException.java b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/InvalidAlgorithmConfigurationException.java index c509efaeeae03..dc28fcee8656c 100644 --- a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/InvalidAlgorithmConfigurationException.java +++ b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/InvalidAlgorithmConfigurationException.java @@ -34,4 +34,8 @@ public InvalidAlgorithmConfigurationException(final String algorithmType, final public InvalidAlgorithmConfigurationException(final String algorithmType) { super(XOpenSQLState.CHECK_OPTION_VIOLATION, 4, "Invalid %s algorithm configuration.", algorithmType); } + + public InvalidAlgorithmConfigurationException(final String algorithmType, final String algorithm, final String message) { + super(XOpenSQLState.CHECK_OPTION_VIOLATION, 4, "Invalid %s algorithm configuration '%s'. %s.", algorithmType, algorithm, message); + } }