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



##########
File path: 
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/WeightReplicaLoadBalanceAlgorithm.java
##########
@@ -0,0 +1,116 @@
+/*
+ * 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<>();
+
+    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) >= 0.0001) {

Review comment:
       `0.0001` is a magic number, I suggest to give it a definition




-- 
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