Skip to content

Commit e404564

Browse files
committed
Changed hashing function to murmur3_128
1 parent f5147ee commit e404564

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

util/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ dependencies {
1919
api project(':grpc-api')
2020

2121
implementation project(':grpc-core'),
22-
project(':grpc-third-party:zero-allocation-hashing'),
2322
libraries.animalsniffer.annotations,
2423
libraries.guava
2524
testImplementation libraries.guava.testlib,

util/src/main/java/io/grpc/util/RandomSubsettingLoadBalancer.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,14 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import com.google.common.hash.HashCode;
23+
import com.google.common.hash.HashFunction;
24+
import com.google.common.hash.Hashing;
25+
import com.google.common.primitives.UnsignedBytes;
2226
import io.grpc.EquivalentAddressGroup;
2327
import io.grpc.LoadBalancer;
2428
import io.grpc.Status;
25-
import io.grpc.tp.zah.XxHash64;
29+
import java.nio.charset.StandardCharsets;
2630
import java.util.ArrayList;
2731
import java.util.Collections;
2832
import java.util.Comparator;
@@ -37,13 +41,16 @@
3741
* https://https://github.com/grpc/proposal/blob/master/A68-random-subsetting.md
3842
*/
3943
final class RandomSubsettingLoadBalancer extends LoadBalancer {
44+
private static final Comparator<byte[]> BYTE_ARRAY_COMPARATOR =
45+
UnsignedBytes.lexicographicalComparator();
46+
4047
private final GracefulSwitchLoadBalancer switchLb;
41-
private final XxHash64 hashFunc;
48+
private final HashFunction hashFunc;
4249

4350
public RandomSubsettingLoadBalancer(Helper helper) {
4451
switchLb = new GracefulSwitchLoadBalancer(checkNotNull(helper, "helper"));
45-
long seed = new Random().nextLong();
46-
hashFunc = new XxHash64(seed);
52+
int seed = new Random().nextInt();
53+
hashFunc = Hashing.murmur3_128(seed);
4754
}
4855

4956
@Override
@@ -76,7 +83,9 @@ private ResolvedAddresses filterEndpoints(ResolvedAddresses resolvedAddresses, l
7683
endpointWithHashList.add(
7784
new EndpointWithHash(
7885
addressGroup,
79-
hashFunc.hashAsciiString(addressGroup.getAddresses().get(0).toString())));
86+
hashFunc.hashString(
87+
addressGroup.getAddresses().get(0).toString(),
88+
StandardCharsets.UTF_8)));
8089
}
8190

8291
Collections.sort(endpointWithHashList, new HashAddressComparator());
@@ -106,18 +115,18 @@ public void shutdown() {
106115

107116
private static final class EndpointWithHash {
108117
public final EquivalentAddressGroup addressGroup;
109-
public final long hash;
118+
public final HashCode hashCode;
110119

111-
public EndpointWithHash(EquivalentAddressGroup addressGroup, long hash) {
120+
public EndpointWithHash(EquivalentAddressGroup addressGroup, HashCode hashCode) {
112121
this.addressGroup = addressGroup;
113-
this.hash = hash;
122+
this.hashCode = hashCode;
114123
}
115124
}
116125

117126
private static final class HashAddressComparator implements Comparator<EndpointWithHash> {
118127
@Override
119128
public int compare(EndpointWithHash lhs, EndpointWithHash rhs) {
120-
return Long.compare(lhs.hash, rhs.hash);
129+
return BYTE_ARRAY_COMPARATOR.compare(lhs.hashCode.asBytes(), rhs.hashCode.asBytes());
121130
}
122131
}
123132

0 commit comments

Comments
 (0)