vernedeng commented on code in PR #5595: URL: https://github.com/apache/inlong/pull/5595#discussion_r949748961
########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/DefaultMessageSender.java: ########## @@ -211,20 +211,22 @@ public SendResult sendMessage(byte[] body, String groupId, String streamId, long } else if (msgtype == 3 || msgtype == 5) { if (isCompressEnd) { return sender.syncSendMessage(new EncodeObject(body, "groupId=" + groupId - + "&streamId=" + streamId + "&dt=" + dt + "&cp=snappy", - idGenerator.getNextId(), this.getMsgtype(), true, groupId), msgUUID, timeout, timeUnit); + + "&streamId=" + streamId + "&dt=" + dt + "&cp=snappy", + idGenerator.getNextId(), this.getMsgtype(), true, groupId), msgUUID, + timeout, timeUnit); } else { return sender.syncSendMessage(new EncodeObject(body, "groupId=" + groupId - + "&streamId=" + streamId + "&dt=" + dt, - idGenerator.getNextId(), this.getMsgtype(), false, groupId), msgUUID, timeout, timeUnit); + + "&streamId=" + streamId + "&dt=" + dt, Review Comment: ditto ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/network/ClientMgr.java: ########## @@ -348,6 +351,37 @@ public synchronized NettyClient getClientByRoundRobin() { return client; } + public synchronized NettyClient getClientByRandom() { + NettyClient client = null; + if (clientList.isEmpty()) { + return null; + } + int currSize = clientList.size(); + int maxRetry = 1000; Review Comment: magic number. please make **_maxRetry_** configurable ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/network/ClientMgr.java: ########## @@ -348,6 +351,37 @@ public synchronized NettyClient getClientByRoundRobin() { return client; } + public synchronized NettyClient getClientByRandom() { + NettyClient client = null; + if (clientList.isEmpty()) { + return null; + } + int currSize = clientList.size(); + int maxRetry = 1000; + Random random = new Random(System.currentTimeMillis()); + do { + int randomId = random.nextInt(); + client = clientList.get(randomId % currSize); + maxRetry--; + } while (client != null && client.isActive() && maxRetry > 0); Review Comment: why loop **_maxRetry_** times to get client? If you want to make it random enough, just loop **_random.nextInt()_** to get randomId, then get client from clientList once. Or you just make a wrong condition? client != null && **!client.isActive()** && maxRetry > 0 ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; + private TreeMap<String, HostInfo> virtualNode2RealNode = new TreeMap<>(); + private List<HostInfo> nodeList = new ArrayList<>(); + private static final HashRing instance = new HashRing(); + private static final Logger LOGGER = LoggerFactory.getLogger(HashRing.class); + + public static HashRing getInstance() { + return instance; + } + + public TreeMap<String, HostInfo> getVirtualNode2RealNode() { + return virtualNode2RealNode; + } + + public String node2VirtualNode(HostInfo node, int index) { + return "virtual&&" + index + "&&" + node.toString(); + } + + public void initHashRing(List<HostInfo> ipList) { + this.virtualNode2RealNode = new TreeMap<>(); + this.nodeList = ipList; + for (HostInfo host : this.nodeList) { + for (int i = 0; i < this.virtualNode; i++) { + String key = node2VirtualNode(host, i); + String hash = ConsistencyHashUtil.hashMurMurHash(key); + virtualNode2RealNode.put(hash, host); + } + } + LOGGER.info("init hash ring {}", this.virtualNode2RealNode); + } + + private void setVirtualNode(int virtualNode) { + this.virtualNode = virtualNode; + } + + public HostInfo getNode(String key) { + String hash = ConsistencyHashUtil.hashMurMurHash(key); + SortedMap<String, HostInfo> tailMap = this.virtualNode2RealNode.tailMap(hash); + HostInfo node; + if (tailMap.isEmpty()) { + node = this.virtualNode2RealNode.get(this.virtualNode2RealNode.firstKey()); + } else { + node = this.virtualNode2RealNode.get(tailMap.firstKey()); + } + LOGGER.info("{} located to {}", key, node); + return node; + } + + public void appendNode(HostInfo host) { Review Comment: private ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; + private TreeMap<String, HostInfo> virtualNode2RealNode = new TreeMap<>(); + private List<HostInfo> nodeList = new ArrayList<>(); + private static final HashRing instance = new HashRing(); + private static final Logger LOGGER = LoggerFactory.getLogger(HashRing.class); + + public static HashRing getInstance() { + return instance; + } + + public TreeMap<String, HostInfo> getVirtualNode2RealNode() { + return virtualNode2RealNode; + } + + public String node2VirtualNode(HostInfo node, int index) { + return "virtual&&" + index + "&&" + node.toString(); + } + + public void initHashRing(List<HostInfo> ipList) { Review Comment: since the instance has been constructed at the very beginning, why not directly init it at the constructor? if multi thread call initHashRing, what will happend? ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; + private TreeMap<String, HostInfo> virtualNode2RealNode = new TreeMap<>(); + private List<HostInfo> nodeList = new ArrayList<>(); + private static final HashRing instance = new HashRing(); + private static final Logger LOGGER = LoggerFactory.getLogger(HashRing.class); + + public static HashRing getInstance() { + return instance; + } + + public TreeMap<String, HostInfo> getVirtualNode2RealNode() { + return virtualNode2RealNode; + } + + public String node2VirtualNode(HostInfo node, int index) { + return "virtual&&" + index + "&&" + node.toString(); + } + + public void initHashRing(List<HostInfo> ipList) { + this.virtualNode2RealNode = new TreeMap<>(); + this.nodeList = ipList; + for (HostInfo host : this.nodeList) { + for (int i = 0; i < this.virtualNode; i++) { + String key = node2VirtualNode(host, i); + String hash = ConsistencyHashUtil.hashMurMurHash(key); + virtualNode2RealNode.put(hash, host); + } + } + LOGGER.info("init hash ring {}", this.virtualNode2RealNode); + } + + private void setVirtualNode(int virtualNode) { Review Comment: setter should not be private ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/DefaultMessageSender.java: ########## @@ -273,20 +277,24 @@ public SendResult sendMessage(List<byte[]> bodyList, String groupId, String stre return sender.syncSendMessage(encodeObject, msgUUID, timeout, timeUnit); } else if (msgtype == 3 || msgtype == 5) { if (isCompress) { - return sender.syncSendMessage(new EncodeObject(bodyList, "groupId=" + groupId + "&streamId=" + streamId + return sender.syncSendMessage(new EncodeObject(bodyList, "groupId=" + groupId + + "&streamId=" + streamId Review Comment: ditto ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; Review Comment: magic number. and the it seems like virtualNodeNum instead of virtualNode ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/ConsistencyHashUtil.java: ########## @@ -0,0 +1,36 @@ +/* + * 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.inlong.sdk.dataproxy.utils; + +import com.google.common.hash.HashFunction; +import com.google.common.hash.Hashing; + +import java.nio.charset.StandardCharsets; + +public class ConsistencyHashUtil { + @Deprecated Review Comment: since it's deprecated and never used, please remove it ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/DefaultMessageSender.java: ########## @@ -211,20 +211,22 @@ public SendResult sendMessage(byte[] body, String groupId, String streamId, long } else if (msgtype == 3 || msgtype == 5) { if (isCompressEnd) { return sender.syncSendMessage(new EncodeObject(body, "groupId=" + groupId - + "&streamId=" + streamId + "&dt=" + dt + "&cp=snappy", - idGenerator.getNextId(), this.getMsgtype(), true, groupId), msgUUID, timeout, timeUnit); + + "&streamId=" + streamId + "&dt=" + dt + "&cp=snappy", Review Comment: Unnecessary code formatting. Personally, I think it's worse than before. ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; + private TreeMap<String, HostInfo> virtualNode2RealNode = new TreeMap<>(); + private List<HostInfo> nodeList = new ArrayList<>(); + private static final HashRing instance = new HashRing(); + private static final Logger LOGGER = LoggerFactory.getLogger(HashRing.class); + + public static HashRing getInstance() { + return instance; + } + + public TreeMap<String, HostInfo> getVirtualNode2RealNode() { + return virtualNode2RealNode; + } + + public String node2VirtualNode(HostInfo node, int index) { + return "virtual&&" + index + "&&" + node.toString(); + } + + public void initHashRing(List<HostInfo> ipList) { + this.virtualNode2RealNode = new TreeMap<>(); + this.nodeList = ipList; + for (HostInfo host : this.nodeList) { + for (int i = 0; i < this.virtualNode; i++) { + String key = node2VirtualNode(host, i); + String hash = ConsistencyHashUtil.hashMurMurHash(key); + virtualNode2RealNode.put(hash, host); + } + } + LOGGER.info("init hash ring {}", this.virtualNode2RealNode); + } + + private void setVirtualNode(int virtualNode) { + this.virtualNode = virtualNode; + } + + public HostInfo getNode(String key) { Review Comment: seems like private function ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { Review Comment: Do not make **_utils_** to singleton. utils are usually static. please move HashRing to other directory ########## inlong-sdk/dataproxy-sdk/src/main/java/org/apache/inlong/sdk/dataproxy/utils/HashRing.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.inlong.sdk.dataproxy.utils; + +import org.apache.inlong.sdk.dataproxy.config.HostInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.ArrayList; +import java.util.TreeMap; +import java.util.SortedMap; +import java.util.stream.Collectors; + +public class HashRing { + private int virtualNode = 1000; + private TreeMap<String, HostInfo> virtualNode2RealNode = new TreeMap<>(); + private List<HostInfo> nodeList = new ArrayList<>(); + private static final HashRing instance = new HashRing(); + private static final Logger LOGGER = LoggerFactory.getLogger(HashRing.class); + + public static HashRing getInstance() { + return instance; + } + + public TreeMap<String, HostInfo> getVirtualNode2RealNode() { + return virtualNode2RealNode; + } + + public String node2VirtualNode(HostInfo node, int index) { + return "virtual&&" + index + "&&" + node.toString(); + } + + public void initHashRing(List<HostInfo> ipList) { + this.virtualNode2RealNode = new TreeMap<>(); + this.nodeList = ipList; + for (HostInfo host : this.nodeList) { + for (int i = 0; i < this.virtualNode; i++) { + String key = node2VirtualNode(host, i); + String hash = ConsistencyHashUtil.hashMurMurHash(key); + virtualNode2RealNode.put(hash, host); + } + } + LOGGER.info("init hash ring {}", this.virtualNode2RealNode); + } + + private void setVirtualNode(int virtualNode) { + this.virtualNode = virtualNode; + } + + public HostInfo getNode(String key) { + String hash = ConsistencyHashUtil.hashMurMurHash(key); + SortedMap<String, HostInfo> tailMap = this.virtualNode2RealNode.tailMap(hash); + HostInfo node; + if (tailMap.isEmpty()) { + node = this.virtualNode2RealNode.get(this.virtualNode2RealNode.firstKey()); + } else { + node = this.virtualNode2RealNode.get(tailMap.firstKey()); + } + LOGGER.info("{} located to {}", key, node); + return node; + } + + public void appendNode(HostInfo host) { + this.nodeList.add(host); + for (int i = 0; i < this.virtualNode; i++) { + String key = node2VirtualNode(host, i); + String hash = ConsistencyHashUtil.hashMurMurHash(key); + virtualNode2RealNode.put(hash, host); + LOGGER.info("append node {}", host); + } + } + + public void extendNode(List<HostInfo> nodes) { Review Comment: private -- 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: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org