You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2023/04/23 07:31:52 UTC

[shardingsphere] branch master updated: Improve properties verification of ReadQueryLoadBalanceAlgorithm (#24934)

This is an automated email from the ASF dual-hosted git repository.

jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 7fd38f10c09 Improve properties verification of ReadQueryLoadBalanceAlgorithm (#24934)
7fd38f10c09 is described below

commit 7fd38f10c09c5b26fcbd6532fc12a6ffc0f74dc4
Author: Qiang <85...@qq.com>
AuthorDate: Sun Apr 23 03:31:43 2023 -0400

    Improve properties verification of ReadQueryLoadBalanceAlgorithm (#24934)
    
    * Improve properties verification of ReadQueryLoadBalanceAlgorithm
    
    Signed-off-by: QiangYuan0828 <85...@qq.com>
    
    * improve properties verification of WeightReadQueryLoadBalanceAlgorithm
    
    * Revert "Improve properties verification of ReadQueryLoadBalanceAlgorithm"
    
    This reverts commit 432a041f5c54fec808cac47da10573fdd5a4e98f.
    
    * update error code and remove unnecessary check
    
    * try to resolve a conflict
    
    * use natural syntax and getProperty().
    
    ---------
    
    Signed-off-by: QiangYuan0828 <85...@qq.com>
---
 .../WeightReadQueryLoadBalanceAlgorithm.java       | 19 +++++++++-----
 ...oadBalanceAlgorithmInitializationExcpetion.java | 30 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 6 deletions(-)

diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
index e9d487e69e0..fdac948277a 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/WeightReadQueryLoadBalanceAlgorithm.java
@@ -21,6 +21,7 @@ import com.google.common.base.Preconditions;
 import lombok.Getter;
 import org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.readwritesplitting.exception.algorithm.InvalidReadDatabaseWeightException;
+import org.apache.shardingsphere.readwritesplitting.exception.algorithm.ReadQueryLoadBalanceAlgorithmInitializationExcpetion;
 import org.apache.shardingsphere.readwritesplitting.exception.algorithm.MissingRequiredReadDatabaseWeightException;
 import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
 
@@ -50,6 +51,17 @@ public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadB
     public void init(final Properties props) {
         this.props = props;
         dataSourceNames = props.stringPropertyNames();
+        ShardingSpherePreconditions.checkState(!dataSourceNames.isEmpty(), () -> new ReadQueryLoadBalanceAlgorithmInitializationExcpetion(getType(), "Read data source is required"));
+        for (String dataSourceName : dataSourceNames) {
+            String weight = props.getProperty(dataSourceName);
+            ShardingSpherePreconditions.checkNotNull(weight,
+                    () -> new MissingRequiredReadDatabaseWeightException(getType(), String.format("Read database `%s` access weight is not configured.", dataSourceName)));
+            try {
+                Double.parseDouble(weight);
+            } catch (final NumberFormatException ex) {
+                throw new InvalidReadDatabaseWeightException(weight);
+            }
+        }
     }
     
     @Override
@@ -108,12 +120,7 @@ public final class WeightReadQueryLoadBalanceAlgorithm implements ReadQueryLoadB
         Object weightObject = props.get(readDataSourceName);
         ShardingSpherePreconditions.checkNotNull(weightObject,
                 () -> new MissingRequiredReadDatabaseWeightException(getType(), String.format("Read database `%s` access weight is not configured.", readDataSourceName)));
-        double result;
-        try {
-            result = Double.parseDouble(weightObject.toString());
-        } catch (final NumberFormatException ignored) {
-            throw new InvalidReadDatabaseWeightException(weightObject);
-        }
+        double result = Double.parseDouble(weightObject.toString());
         if (Double.isInfinite(result)) {
             result = 10000.0D;
         }
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/ReadQueryLoadBalanceAlgorithmInitializationExcpetion.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/ReadQueryLoadBalanceAlgorithmInitializationExcpetion.java
new file mode 100644
index 00000000000..edea7142c91
--- /dev/null
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/exception/algorithm/ReadQueryLoadBalanceAlgorithmInitializationExcpetion.java
@@ -0,0 +1,30 @@
+/*
+ * 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.exception.algorithm;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.readwritesplitting.exception.ReadwriteSplittingSQLException;
+
+public class ReadQueryLoadBalanceAlgorithmInitializationExcpetion extends ReadwriteSplittingSQLException {
+    
+    private static final long serialVersionUID = -8673960967175624027L;
+    
+    public ReadQueryLoadBalanceAlgorithmInitializationExcpetion(final String loadBalanceAlgorithmType, final String reason) {
+        super(XOpenSQLState.CHECK_OPTION_VIOLATION, 83, "'%s' read query load balance algorithm initialization failed, reason is: %s.", loadBalanceAlgorithmType, reason);
+    }
+}