|
19 | 19 | import static com.google.common.base.Preconditions.checkArgument; |
20 | 20 | import static com.google.common.base.Preconditions.checkNotNull; |
21 | 21 |
|
| 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; |
22 | 26 | import io.grpc.EquivalentAddressGroup; |
23 | 27 | import io.grpc.LoadBalancer; |
24 | 28 | import io.grpc.Status; |
25 | | -import io.grpc.tp.zah.XxHash64; |
| 29 | +import java.nio.charset.StandardCharsets; |
26 | 30 | import java.util.ArrayList; |
27 | 31 | import java.util.Collections; |
28 | 32 | import java.util.Comparator; |
|
37 | 41 | * https://https://github.com/grpc/proposal/blob/master/A68-random-subsetting.md |
38 | 42 | */ |
39 | 43 | final class RandomSubsettingLoadBalancer extends LoadBalancer { |
| 44 | + private static final Comparator<byte[]> BYTE_ARRAY_COMPARATOR = |
| 45 | + UnsignedBytes.lexicographicalComparator(); |
| 46 | + |
40 | 47 | private final GracefulSwitchLoadBalancer switchLb; |
41 | | - private final XxHash64 hashFunc; |
| 48 | + private final HashFunction hashFunc; |
42 | 49 |
|
43 | 50 | public RandomSubsettingLoadBalancer(Helper helper) { |
44 | 51 | 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); |
47 | 54 | } |
48 | 55 |
|
49 | 56 | @Override |
@@ -76,7 +83,9 @@ private ResolvedAddresses filterEndpoints(ResolvedAddresses resolvedAddresses, l |
76 | 83 | endpointWithHashList.add( |
77 | 84 | new EndpointWithHash( |
78 | 85 | addressGroup, |
79 | | - hashFunc.hashAsciiString(addressGroup.getAddresses().get(0).toString()))); |
| 86 | + hashFunc.hashString( |
| 87 | + addressGroup.getAddresses().get(0).toString(), |
| 88 | + StandardCharsets.UTF_8))); |
80 | 89 | } |
81 | 90 |
|
82 | 91 | Collections.sort(endpointWithHashList, new HashAddressComparator()); |
@@ -106,18 +115,18 @@ public void shutdown() { |
106 | 115 |
|
107 | 116 | private static final class EndpointWithHash { |
108 | 117 | public final EquivalentAddressGroup addressGroup; |
109 | | - public final long hash; |
| 118 | + public final HashCode hashCode; |
110 | 119 |
|
111 | | - public EndpointWithHash(EquivalentAddressGroup addressGroup, long hash) { |
| 120 | + public EndpointWithHash(EquivalentAddressGroup addressGroup, HashCode hashCode) { |
112 | 121 | this.addressGroup = addressGroup; |
113 | | - this.hash = hash; |
| 122 | + this.hashCode = hashCode; |
114 | 123 | } |
115 | 124 | } |
116 | 125 |
|
117 | 126 | private static final class HashAddressComparator implements Comparator<EndpointWithHash> { |
118 | 127 | @Override |
119 | 128 | 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()); |
121 | 130 | } |
122 | 131 | } |
123 | 132 |
|
|
0 commit comments