strongduanmu commented on a change in pull request #13943:
URL: https://github.com/apache/shardingsphere/pull/13943#discussion_r763780704



##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.algorithm;
+
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Weight replica load-balance algorithm.
+ */
+public final class WeightReplicaLoadBalanceAlgorithm implements 
ReplicaLoadBalanceAlgorithm {
+    
+    private static final ConcurrentHashMap<String, Double[]> WEIGHT_MAP = new 
ConcurrentHashMap<>();
+
+    /**
+     * Cumulative accuracy loss threshold
+     */
+    private static final double ACCURACY_THRESHOLD = 0.0001;
+
+    private Properties props = new Properties();
+
+    @Override
+    public Properties getProps() {
+        return this.props;
+    }
+
+    @Override
+    public void setProps(final Properties props) {
+        this.props = props;
+    }
+
+    @Override
+    public String getType() {
+        return "WEIGHT";
+    }
+
+    @Override
+    public String getDataSource(final String name, final String 
writeDataSourceName, final List<String> readDataSourceNames) {
+        Double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) 
: initWeight(readDataSourceNames);
+        WEIGHT_MAP.putIfAbsent(name, weight);
+        return getDataSourceName(readDataSourceNames, weight);
+    }
+
+    private String getDataSourceName(final List<String> readDataSourceNames, 
final Double[] weight) {
+        double randomWeight = ThreadLocalRandom.current().nextDouble(0, 1);
+        int index = Arrays.binarySearch(weight, randomWeight);
+        if (index < 0) {
+            index = -index - 1;
+            return index < weight.length && randomWeight < weight[index] ? 
readDataSourceNames.get(index) : 
readDataSourceNames.get(readDataSourceNames.size() - 1);
+        } else {
+            return readDataSourceNames.get(index);
+        }
+    }
+
+    private Double[] initWeight(final List<String> readDataSourceNames) {
+        Double[] weights = getWeights(readDataSourceNames);
+        if (weights.length != 0 && Math.abs(weights[weights.length - 1] - 
1.0D) >= ACCURACY_THRESHOLD) {
+            throw new IllegalStateException("The cumulative weight is 
calculated incorrectly, and the sum of the probabilities is not equal to 1.");
+        }
+        return weights;
+    }
+    
+    private Double[] getWeights(final List<String> readDataSourceNames) {

Review comment:
       @cherubimLV This method is too long, and some logic is not easy to 
understand. Is it possible to improve readability by extracting methods and 
naming methods?

##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.algorithm;
+
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Weight replica load-balance algorithm.
+ */
+public final class WeightReplicaLoadBalanceAlgorithm implements 
ReplicaLoadBalanceAlgorithm {
+    
+    private static final ConcurrentHashMap<String, Double[]> WEIGHT_MAP = new 
ConcurrentHashMap<>();
+
+    /**
+     * Cumulative accuracy loss threshold
+     */
+    private static final double ACCURACY_THRESHOLD = 0.0001;
+
+    private Properties props = new Properties();
+
+    @Override
+    public Properties getProps() {
+        return this.props;
+    }
+
+    @Override
+    public void setProps(final Properties props) {
+        this.props = props;
+    }
+
+    @Override
+    public String getType() {
+        return "WEIGHT";
+    }
+
+    @Override
+    public String getDataSource(final String name, final String 
writeDataSourceName, final List<String> readDataSourceNames) {
+        Double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) 
: initWeight(readDataSourceNames);

Review comment:
       @cherubimLV Can we make the ReplicaLoadBalanceAlgorithm interface 
implement ShardingSphereAlgorithmPostProcessor? So that the weight parameter 
can be initialized in the init method?

##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,99 @@
+package org.apache.shardingsphere.readwritesplitting.algorithm;
+
+import lombok.Getter;
+import lombok.Setter;
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Weight replica load-balance algorithm.
+ */
+@Getter
+@Setter
+public class WeightReplicaLoadBalanceAlgorithm implements 
ReplicaLoadBalanceAlgorithm {
+    private static final ConcurrentHashMap<String, Double[]> WEIGHT_MAP = new 
ConcurrentHashMap<>();

Review comment:
       @cherubimLV Why is the value of this object Double data? Instead of 
Double?

##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.algorithm;
+
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Weight replica load-balance algorithm.
+ */
+public final class WeightReplicaLoadBalanceAlgorithm implements 
ReplicaLoadBalanceAlgorithm {
+    
+    private static final ConcurrentHashMap<String, Double[]> WEIGHT_MAP = new 
ConcurrentHashMap<>();
+
+    /**
+     * Cumulative accuracy loss threshold

Review comment:
       Hi @cherubimLV, please remove this doc for field.

##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,99 @@
+package org.apache.shardingsphere.readwritesplitting.algorithm;
+
+import lombok.Getter;
+import lombok.Setter;
+import 
org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Weight replica load-balance algorithm.
+ */
+@Getter
+@Setter
+public class WeightReplicaLoadBalanceAlgorithm implements 
ReplicaLoadBalanceAlgorithm {
+    private static final ConcurrentHashMap<String, Double[]> WEIGHT_MAP = new 
ConcurrentHashMap<>();
+
+    private Properties props = new Properties();
+
+    @Override
+    public Properties getProps() {
+        return this.props;
+    }
+
+    @Override
+    public void setProps(final Properties props) {

Review comment:
       As @RaigorJiang said, we recommend the lombok method more than the get 
set method, so please delete these methods. 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to