This is an automated email from the ASF dual-hosted git repository.

morningman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-doris.git


The following commit(s) were added to refs/heads/master by this push:
     new 2e9c8dd  [Doris On ES][Bug-Fix] fix problem for selecting random be 
(#4972)
2e9c8dd is described below

commit 2e9c8dda04c55bd96a235f5d3ee4420380a3055e
Author: Yunfeng,Wu <[email protected]>
AuthorDate: Sat Nov 28 09:57:44 2020 +0800

    [Doris On ES][Bug-Fix] fix problem for selecting random be (#4972)
    
    1.  Random().nextInt() maybe return negative numeric value which would 
result in `java.lang.ArrayIndexOutOfBoundsException`,
    pass a positive numeric value would avoid this problem.
    
    ```
    int seed = new Random().nextInt(Short.MAX_VALUE) % nodesInfo.size()
    ```
    
    2.  EsNodeInfo[] nodeInfos = (EsNodeInfo[]) nodesInfo.values().toArray() 
maybe lead `java.lang.ClassCastException  in some JDK version : 
[Ljava.lang.Object; cannot be cast to 
[Lorg.apache.doris.external.elasticsearch.EsNodeInfo` , pass the original 
`Class Type` can resolve this.
    
    ```
    EsNodeInfo[] nodeInfos = nodesInfo.values().toArray(new EsNodeInfo[0]);
    ```
---
 .../doris/external/elasticsearch/EsShardPartitions.java    | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git 
a/fe/fe-core/src/main/java/org/apache/doris/external/elasticsearch/EsShardPartitions.java
 
b/fe/fe-core/src/main/java/org/apache/doris/external/elasticsearch/EsShardPartitions.java
index 41687cf..b81dead 100644
--- 
a/fe/fe-core/src/main/java/org/apache/doris/external/elasticsearch/EsShardPartitions.java
+++ 
b/fe/fe-core/src/main/java/org/apache/doris/external/elasticsearch/EsShardPartitions.java
@@ -21,14 +21,14 @@ import org.apache.doris.analysis.SingleRangePartitionDesc;
 import org.apache.doris.catalog.PartitionKey;
 import org.apache.doris.thrift.TNetworkAddress;
 
+import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
+
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.json.JSONArray;
 import org.json.JSONObject;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
-
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
@@ -82,7 +82,8 @@ public class EsShardPartitions {
                                         jsonObject.getJSONObject("nodes")));
                     } catch (Exception e) {
                         LOG.error("fetch index [{}] shard partitions failure", 
indexName, e);
-                        throw new DorisEsException("fetch [" + indexName + "] 
shard partitions failure [" + e.getMessage() + "]");                   }
+                        throw new DorisEsException("fetch [" + indexName + "] 
shard partitions failure [" + e.getMessage() + "]");
+                    }
                 }
             }
             if (singleShardRouting.isEmpty()) {
@@ -108,8 +109,9 @@ public class EsShardPartitions {
     }
 
     public TNetworkAddress randomAddress(Map<String, EsNodeInfo> nodesInfo) {
-        int seed = new Random().nextInt() % nodesInfo.size();
-        EsNodeInfo[] nodeInfos = (EsNodeInfo[]) nodesInfo.values().toArray();
+        // return a random value between 0 and 32767 : [0, 32767)
+        int seed = new Random().nextInt(Short.MAX_VALUE) % nodesInfo.size();
+        EsNodeInfo[] nodeInfos = nodesInfo.values().toArray(new EsNodeInfo[0]);
         return nodeInfos[seed].getPublishAddress();
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to