Skip to content

Commit fff45ce

Browse files
authored
optimize: optimize ConsistentHashLoadBalance Algorithm (apache#6748)
1 parent 595277d commit fff45ce

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

changes/en-us/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Add changes here for all PR submitted to the 2.x branch.
5151
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] upgrade npmjs version in saga
5252
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] optimize compatible dependencies
5353
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] fix node-gyp build error on arm64 and macos
54+
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] optimize ConsistentHashLoadBalance Algorithm
5455
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] optimize fastjson deserialization
5556

5657

changes/zh-cn/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] 升级saga模块npmjs版本
5353
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] 优化 compatible 模块依赖
5454
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] 修复 node-gyp 在 arm64 和 macos 构建失败问题
55+
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] 优化 ConsistentHashLoadBalance 算法
5556
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] 优化 fastjson 反序列化
5657

5758

discovery/seata-discovery-core/src/main/java/org/apache/seata/discovery/loadbalance/ConsistentHashLoadBalance.java

+16-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.seata.discovery.loadbalance;
1818

19+
import java.nio.charset.StandardCharsets;
1920
import java.security.MessageDigest;
2021
import java.security.NoSuchAlgorithmException;
2122
import java.util.List;
@@ -29,19 +30,20 @@
2930

3031
/**
3132
* The type consistent hash load balance.
32-
*
3333
*/
3434
@LoadLevel(name = LoadBalanceFactory.CONSISTENT_HASH_LOAD_BALANCE)
3535
public class ConsistentHashLoadBalance implements LoadBalance {
3636

3737
/**
3838
* The constant LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES.
3939
*/
40-
public static final String LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES = LoadBalanceFactory.LOAD_BALANCE_PREFIX + "virtualNodes";
40+
public static final String LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES = LoadBalanceFactory.LOAD_BALANCE_PREFIX
41+
+ "virtualNodes";
4142
/**
4243
* The constant VIRTUAL_NODES_NUM.
4344
*/
44-
private static final int VIRTUAL_NODES_NUM = ConfigurationFactory.getInstance().getInt(LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES, VIRTUAL_NODES_DEFAULT);
45+
private static final int VIRTUAL_NODES_NUM = ConfigurationFactory.getInstance().getInt(
46+
LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES, VIRTUAL_NODES_DEFAULT);
4547

4648
@Override
4749
public <T> T select(List<T> invokers, String xid) {
@@ -51,7 +53,7 @@ public <T> T select(List<T> invokers, String xid) {
5153
private static final class ConsistentHashSelector<T> {
5254

5355
private final SortedMap<Long, T> virtualInvokers = new TreeMap<>();
54-
private final HashFunction hashFunction = new MD5Hash();
56+
private final HashFunction hashFunction = new SHA256Hash();
5557

5658
ConsistentHashSelector(List<T> invokers, int virtualNodes) {
5759
for (T invoker : invokers) {
@@ -68,12 +70,12 @@ public T select(String objectKey) {
6870
}
6971
}
7072

71-
@SuppressWarnings("lgtm[java/weak-cryptographic-algorithm]")
72-
private static class MD5Hash implements HashFunction {
73+
private static class SHA256Hash implements HashFunction {
7374
MessageDigest instance;
74-
public MD5Hash() {
75+
76+
public SHA256Hash() {
7577
try {
76-
instance = MessageDigest.getInstance("MD5");
78+
instance = MessageDigest.getInstance("SHA-256");
7779
} catch (NoSuchAlgorithmException e) {
7880
throw new IllegalStateException(e.getMessage(), e);
7981
}
@@ -83,13 +85,13 @@ public MD5Hash() {
8385
public long hash(String key) {
8486
instance.reset();
8587
instance.update(key.getBytes());
86-
byte[] digest = instance.digest();
87-
long h = 0;
88-
for (int i = 0; i < 4; i++) {
89-
h <<= 8;
90-
h |= ((int) digest[i]) & 0xFF;
88+
byte[] digest = instance.digest(key.getBytes(StandardCharsets.UTF_8));
89+
long hash = 0;
90+
for (int i = 0; i < 8 && i < digest.length; i++) {
91+
hash <<= 8;
92+
hash |= digest[i] & 0xff;
9193
}
92-
return h;
94+
return hash;
9395
}
9496
}
9597

0 commit comments

Comments
 (0)