You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/05/23 09:42:57 UTC

[GitHub] [shardingsphere] strongduanmu commented on a diff in pull request #17747: Support opengauss route strategy on read write split

strongduanmu commented on code in PR #17747:
URL: https://github.com/apache/shardingsphere/pull/17747#discussion_r879225353


##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/RandomReplicaLoadBalanceAlgorithm.java:
##########
@@ -39,6 +40,9 @@ public void init(final Properties props) {
     
     @Override
     public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        if (TransactionHolder.isTransaction()) {

Review Comment:
   @jingshanglu Since our load balance algorithm also includes the logic of the primary datasource, do we need to rename ReplicaLoadBalanceAlgorithm to ReadWriteSplittingLoadBalanceAlgorithm?



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToMasterLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Select-to-master replica load-balance algorithm.
+ */
+public final class SelectToMasterLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        return writeDataSourceName;
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_MASTER";

Review Comment:
   Is it better to name it FIXED_PRIMARY?



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToMasterLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+
+/**
+ * Select-to-master replica load-balance algorithm.
+ */
+public final class SelectToMasterLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        return writeDataSourceName;
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_MASTER";
+    }
+    
+    @Override
+    public boolean isDefault() {

Review Comment:
   Please remove isDefault method here, because we have a default ReplicaLoadBalanceAlgorithm implementation.



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToOneSlaveRandomReplicaLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.ThreadLocalRandom;
+
+/**
+ * Select-to-one-slave-random replica load-balance algorithm.
+ */
+@Getter
+public final class SelectToOneSlaveRandomReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    private static final ThreadLocal<String> SLAVE_ROUTE_HOLDER = new ThreadLocal<>();
+    
+    private Properties props = new Properties();
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        if (null == SLAVE_ROUTE_HOLDER.get()) {
+            SLAVE_ROUTE_HOLDER.set(readDataSourceNames.get(ThreadLocalRandom.current().nextInt(readDataSourceNames.size())));
+        }
+        return SLAVE_ROUTE_HOLDER.get();
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_ONE_SLAVE_RANDOM";

Review Comment:
   Is it better to name it FIXED_REPLICA?



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToOneSlaveRoundRobinReplicaLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Select-to-one-slave-round-robin replica load-balance algorithm.
+ */
+public final class SelectToOneSlaveRoundRobinReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    private static final ThreadLocal<String> SLAVE_ROUTE_HOLDER = new ThreadLocal<>();
+    
+    private final AtomicInteger count = new AtomicInteger(0);
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        if (null == SLAVE_ROUTE_HOLDER.get()) {
+            SLAVE_ROUTE_HOLDER.set(readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()));
+        }
+        return SLAVE_ROUTE_HOLDER.get();
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_ONE_SLAVE_ROUND_ROBIN";

Review Comment:
   Is it better to name it FIXED_REPLICA_ROUND_ROBIN?



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToOneSlaveRoundRobinReplicaLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,61 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+import org.apache.shardingsphere.readwritesplitting.spi.ReplicaLoadBalanceAlgorithm;
+
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Select-to-one-slave-round-robin replica load-balance algorithm.
+ */
+public final class SelectToOneSlaveRoundRobinReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    private static final ThreadLocal<String> SLAVE_ROUTE_HOLDER = new ThreadLocal<>();
+    
+    private final AtomicInteger count = new AtomicInteger(0);
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        if (null == SLAVE_ROUTE_HOLDER.get()) {
+            SLAVE_ROUTE_HOLDER.set(readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size()));
+        }
+        return SLAVE_ROUTE_HOLDER.get();
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_ONE_SLAVE_ROUND_ROBIN";
+    }
+    
+    @Override
+    public boolean isDefault() {

Review Comment:
   Please remove isDefault here.



##########
shardingsphere-features/shardingsphere-readwrite-splitting/shardingsphere-readwrite-splitting-core/src/main/java/org/apache/shardingsphere/readwritesplitting/algorithm/loadbalance/SelectToOneSlaveWeightReplicaLoadBalanceAlgorithm.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.loadbalance;
+
+import lombok.Getter;
+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;
+
+/**
+ * Select-to-one-slave-weight replica load-balance algorithm.
+ */
+@Getter
+public final class SelectToOneSlaveWeightReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
+    
+    private static final double ACCURACY_THRESHOLD = 0.0001;
+    
+    private static final ConcurrentHashMap<String, double[]> WEIGHT_MAP = new ConcurrentHashMap<>();
+    
+    private static final ThreadLocal<String> SLAVE_ROUTE_HOLDER = new ThreadLocal<>();
+    
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+    }
+    
+    @Override
+    public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
+        if (null != SLAVE_ROUTE_HOLDER.get()) {
+            return SLAVE_ROUTE_HOLDER.get();
+        }
+        double[] weight = WEIGHT_MAP.containsKey(name) ? WEIGHT_MAP.get(name) : initWeight(readDataSourceNames);
+        WEIGHT_MAP.putIfAbsent(name, weight);
+        SLAVE_ROUTE_HOLDER.set(getDataSourceName(readDataSourceNames, weight));
+        return SLAVE_ROUTE_HOLDER.get();
+    }
+    
+    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);
+        }
+        return readDataSourceNames.get(index);
+    }
+    
+    private double[] initWeight(final List<String> readDataSourceNames) {
+        double[] result = getWeights(readDataSourceNames);
+        if (result.length != 0 && Math.abs(result[result.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 result;
+    }
+    
+    private double[] getWeights(final List<String> readDataSourceNames) {
+        double[] exactWeights = new double[readDataSourceNames.size()];
+        int index = 0;
+        double sum = 0D;
+        for (String readDataSourceName : readDataSourceNames) {
+            double weight = getWeightValue(readDataSourceName);
+            exactWeights[index++] = weight;
+            sum += weight;
+        }
+        for (int i = 0; i < index; i++) {
+            if (exactWeights[i] <= 0) {
+                continue;
+            }
+            exactWeights[i] = exactWeights[i] / sum;
+        }
+        return calcWeight(exactWeights);
+    }
+    
+    private double[] calcWeight(final double[] exactWeights) {
+        double[] result = new double[exactWeights.length];
+        double randomRange = 0D;
+        for (int i = 0; i < result.length; i++) {
+            result[i] = randomRange + exactWeights[i];
+            randomRange += exactWeights[i];
+        }
+        return result;
+    }
+    
+    private double getWeightValue(final String readDataSourceName) {
+        Object weightObject = props.get(readDataSourceName);
+        if (null == weightObject) {
+            throw new IllegalStateException("Read database access weight is not configured:" + readDataSourceName);
+        }
+        double result;
+        try {
+            result = Double.parseDouble(weightObject.toString());
+        } catch (final NumberFormatException ex) {
+            throw new NumberFormatException("Read database weight configuration error, configuration parameters:" + weightObject);
+        }
+        if (Double.isInfinite(result)) {
+            result = 10000.0D;
+        }
+        if (Double.isNaN(result)) {
+            result = 1.0D;
+        }
+        return result;
+    }
+    
+    @Override
+    public String getType() {
+        return "SELECT_TO_ONE_SLAVE_WEIGHT";

Review Comment:
   Is it better to name it FIXED_REPLICA_WEIGHT?



-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org