You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2018/08/06 07:09:58 UTC

[GitHub] vongosling closed pull request #385: pickOneAtLeast optimize

vongosling closed pull request #385: pickOneAtLeast optimize
URL: https://github.com/apache/rocketmq/pull/385
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/client/src/main/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java b/client/src/main/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java
index 72d43476f..ef136b8ad 100644
--- a/client/src/main/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java
+++ b/client/src/main/java/org/apache/rocketmq/client/latency/LatencyFaultToleranceImpl.java
@@ -17,10 +17,7 @@
 
 package org.apache.rocketmq.client.latency;
 
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.LinkedList;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import org.apache.rocketmq.client.common.ThreadLocalIndex;
 
@@ -72,10 +69,9 @@ public String pickOneAtLeast() {
         }
 
         if (!tmpList.isEmpty()) {
-            Collections.shuffle(tmpList);
-
-            Collections.sort(tmpList);
-
+            Object[] objects = tmpList.toArray();
+            shuffle(objects,null);
+            Arrays.sort(objects);
             final int half = tmpList.size() / 2;
             if (half <= 0) {
                 return tmpList.get(0).getName();
@@ -88,6 +84,19 @@ public String pickOneAtLeast() {
         return null;
     }
 
+    private void shuffle(Object[] arr, Random rnd) {
+        int size = arr.length;
+        rnd = rnd == null ? new Random() : rnd;
+        for (int i=size; i>1; i--) {
+            swap(arr, i-1, rnd.nextInt(i));
+        }
+    }
+    private void swap(Object[] arr, int i, int nextInt) {
+        Object o = arr[i];
+        arr[i] = arr[nextInt];
+        arr[nextInt] = o;
+    }
+
     @Override
     public String toString() {
         return "LatencyFaultToleranceImpl{" +


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services