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/07 08:38:31 UTC

[GitHub] [shardingsphere] strongduanmu opened a new issue, #17419: Consider optimizing RoundRobinTrafficLoadBalanceAlgorithm and RoundRobinTrafficLoadBalanceAlgorithm logic

strongduanmu opened a new issue, #17419:
URL: https://github.com/apache/shardingsphere/issues/17419

   ## Feature Request
   
   ### Is your feature request related to a problem?
   
   No.
   
   ### Describe the feature you would like.
   
   As shown below, in the `RoundRobinTrafficLoadBalanceAlgorithm` and `RoundRobinReplicaLoadBalanceAlgorithm` algorithms, the static variable `COUNTS` is used to store the count during the load balancing process. This implementation is unnecessary and increases the complexity of the algorithm. Also, the counters stored in `COUNTS` run the risk of overflowing. Therefore, we need to optimize these problems, simplify the implementation of the algorithm and solve the problem of COUNTS overflow.
   
   RoundRobinTrafficLoadBalanceAlgorithm:
   
   ```java
   /**
    * Round-robin replica load-balance algorithm.
    */
   public final class RoundRobinReplicaLoadBalanceAlgorithm implements ReplicaLoadBalanceAlgorithm {
       
       private static final ConcurrentHashMap<String, AtomicInteger> COUNTS = new ConcurrentHashMap<>();
       
       @Getter
       @Setter
       private Properties props;
       
       @Override
       public String getDataSource(final String name, final String writeDataSourceName, final List<String> readDataSourceNames) {
           AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0);
           COUNTS.putIfAbsent(name, count);
           count.compareAndSet(readDataSourceNames.size(), 0);
           return readDataSourceNames.get(Math.abs(count.getAndIncrement()) % readDataSourceNames.size());
       }
       
       @Override
       public String getType() {
           return "ROUND_ROBIN";
       }
       
       @Override
       public boolean isDefault() {
           return true;
       }
   }
   ```
   
   RoundRobinTrafficLoadBalanceAlgorithm:
   
   ```java
   /**
    * Round-robin traffic load balance algorithm.
    */
   public final class RoundRobinTrafficLoadBalanceAlgorithm implements TrafficLoadBalanceAlgorithm {
       
       private static final ConcurrentHashMap<String, AtomicInteger> COUNTS = new ConcurrentHashMap<>();
       
       @Override
       public InstanceId getInstanceId(final String name, final List<InstanceId> instanceIds) {
           AtomicInteger count = COUNTS.containsKey(name) ? COUNTS.get(name) : new AtomicInteger(0);
           COUNTS.putIfAbsent(name, count);
           count.compareAndSet(instanceIds.size(), 0);
           return instanceIds.get(Math.abs(count.getAndIncrement()) % instanceIds.size());
       }
       
       @Override
       public String getType() {
           return "ROUND_ROBIN";
       }
       
       @Override
       public boolean isDefault() {
           return true;
       }
   }
   ```
   


-- 
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.apache.org

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


[GitHub] [shardingsphere] terrymanu closed issue #17419: Consider optimizing RoundRobinTrafficLoadBalanceAlgorithm and RoundRobinTrafficLoadBalanceAlgorithm logic

Posted by GitBox <gi...@apache.org>.
terrymanu closed issue #17419: Consider optimizing RoundRobinTrafficLoadBalanceAlgorithm and RoundRobinTrafficLoadBalanceAlgorithm logic
URL: https://github.com/apache/shardingsphere/issues/17419


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