Skip to content

Commit

Permalink
optimize: optimize ConsistentHashLoadBalance Algorithm (#6748)
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored Aug 13, 2024
1 parent 595277d commit fff45ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] upgrade npmjs version in saga
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] optimize compatible dependencies
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] fix node-gyp build error on arm64 and macos
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] optimize ConsistentHashLoadBalance Algorithm
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] optimize fastjson deserialization


Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] 升级saga模块npmjs版本
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] 优化 compatible 模块依赖
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] 修复 node-gyp 在 arm64 和 macos 构建失败问题
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] 优化 ConsistentHashLoadBalance 算法
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] 优化 fastjson 反序列化


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.seata.discovery.loadbalance;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
Expand All @@ -29,19 +30,20 @@

/**
* The type consistent hash load balance.
*
*/
@LoadLevel(name = LoadBalanceFactory.CONSISTENT_HASH_LOAD_BALANCE)
public class ConsistentHashLoadBalance implements LoadBalance {

/**
* The constant LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES.
*/
public static final String LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES = LoadBalanceFactory.LOAD_BALANCE_PREFIX + "virtualNodes";
public static final String LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES = LoadBalanceFactory.LOAD_BALANCE_PREFIX
+ "virtualNodes";
/**
* The constant VIRTUAL_NODES_NUM.
*/
private static final int VIRTUAL_NODES_NUM = ConfigurationFactory.getInstance().getInt(LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES, VIRTUAL_NODES_DEFAULT);
private static final int VIRTUAL_NODES_NUM = ConfigurationFactory.getInstance().getInt(
LOAD_BALANCE_CONSISTENT_HASH_VIRTUAL_NODES, VIRTUAL_NODES_DEFAULT);

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

private final SortedMap<Long, T> virtualInvokers = new TreeMap<>();
private final HashFunction hashFunction = new MD5Hash();
private final HashFunction hashFunction = new SHA256Hash();

ConsistentHashSelector(List<T> invokers, int virtualNodes) {
for (T invoker : invokers) {
Expand All @@ -68,12 +70,12 @@ public T select(String objectKey) {
}
}

@SuppressWarnings("lgtm[java/weak-cryptographic-algorithm]")
private static class MD5Hash implements HashFunction {
private static class SHA256Hash implements HashFunction {
MessageDigest instance;
public MD5Hash() {

public SHA256Hash() {
try {
instance = MessageDigest.getInstance("MD5");
instance = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e.getMessage(), e);
}
Expand All @@ -83,13 +85,13 @@ public MD5Hash() {
public long hash(String key) {
instance.reset();
instance.update(key.getBytes());
byte[] digest = instance.digest();
long h = 0;
for (int i = 0; i < 4; i++) {
h <<= 8;
h |= ((int) digest[i]) & 0xFF;
byte[] digest = instance.digest(key.getBytes(StandardCharsets.UTF_8));
long hash = 0;
for (int i = 0; i < 8 && i < digest.length; i++) {
hash <<= 8;
hash |= digest[i] & 0xff;
}
return h;
return hash;
}
}

Expand Down

0 comments on commit fff45ce

Please sign in to comment.