|
| 1 | +/* |
| 2 | + * Copyright 2025 The gRPC Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.grpc.util; |
| 18 | + |
| 19 | +import static com.google.common.base.Preconditions.checkArgument; |
| 20 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 21 | +import static com.google.common.base.Preconditions.checkState; |
| 22 | + |
| 23 | +import com.google.common.annotations.VisibleForTesting; |
| 24 | +import com.google.common.hash.HashCode; |
| 25 | +import com.google.common.hash.HashFunction; |
| 26 | +import com.google.common.hash.Hashing; |
| 27 | +import com.google.common.primitives.Ints; |
| 28 | +import io.grpc.EquivalentAddressGroup; |
| 29 | +import io.grpc.LoadBalancer; |
| 30 | +import io.grpc.Status; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Collections; |
| 34 | +import java.util.Comparator; |
| 35 | +import java.util.Random; |
| 36 | + |
| 37 | + |
| 38 | +/** |
| 39 | + * Wraps a child {@code LoadBalancer}, separating the total set of backends into smaller subsets for |
| 40 | + * the child balancer to balance across. |
| 41 | + * |
| 42 | + * <p>This implements random subsetting gRFC: |
| 43 | + * https://https://github.com/grpc/proposal/blob/master/A68-random-subsetting.md |
| 44 | + */ |
| 45 | +final class RandomSubsettingLoadBalancer extends LoadBalancer { |
| 46 | + private final GracefulSwitchLoadBalancer switchLb; |
| 47 | + private final HashFunction hashFunc; |
| 48 | + |
| 49 | + public RandomSubsettingLoadBalancer(Helper helper) { |
| 50 | + this(helper, new Random().nextInt()); |
| 51 | + } |
| 52 | + |
| 53 | + @VisibleForTesting |
| 54 | + RandomSubsettingLoadBalancer(Helper helper, int seed) { |
| 55 | + switchLb = new GracefulSwitchLoadBalancer(checkNotNull(helper, "helper")); |
| 56 | + hashFunc = Hashing.murmur3_128(seed); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) { |
| 61 | + RandomSubsettingLoadBalancerConfig config = |
| 62 | + (RandomSubsettingLoadBalancerConfig) |
| 63 | + resolvedAddresses.getLoadBalancingPolicyConfig(); |
| 64 | + |
| 65 | + ResolvedAddresses subsetAddresses = filterEndpoints(resolvedAddresses, config.subsetSize); |
| 66 | + |
| 67 | + return switchLb.acceptResolvedAddresses( |
| 68 | + subsetAddresses.toBuilder() |
| 69 | + .setLoadBalancingPolicyConfig(config.childConfig) |
| 70 | + .build()); |
| 71 | + } |
| 72 | + |
| 73 | + // implements the subsetting algorithm, as described in A68: |
| 74 | + // https://github.com/grpc/proposal/pull/423 |
| 75 | + private ResolvedAddresses filterEndpoints(ResolvedAddresses resolvedAddresses, int subsetSize) { |
| 76 | + if (subsetSize >= resolvedAddresses.getAddresses().size()) { |
| 77 | + return resolvedAddresses; |
| 78 | + } |
| 79 | + |
| 80 | + ArrayList<EndpointWithHash> endpointWithHashList = |
| 81 | + new ArrayList<>(resolvedAddresses.getAddresses().size()); |
| 82 | + |
| 83 | + for (EquivalentAddressGroup addressGroup : resolvedAddresses.getAddresses()) { |
| 84 | + HashCode hashCode = hashFunc.hashString( |
| 85 | + addressGroup.getAddresses().get(0).toString(), |
| 86 | + StandardCharsets.UTF_8); |
| 87 | + endpointWithHashList.add(new EndpointWithHash(addressGroup, hashCode.asLong())); |
| 88 | + } |
| 89 | + |
| 90 | + Collections.sort(endpointWithHashList, new HashAddressComparator()); |
| 91 | + |
| 92 | + ArrayList<EquivalentAddressGroup> addressGroups = new ArrayList<>(subsetSize); |
| 93 | + |
| 94 | + for (int idx = 0; idx < subsetSize; ++idx) { |
| 95 | + addressGroups.add(endpointWithHashList.get(idx).addressGroup); |
| 96 | + } |
| 97 | + |
| 98 | + return resolvedAddresses.toBuilder().setAddresses(addressGroups).build(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public void handleNameResolutionError(Status error) { |
| 103 | + switchLb.handleNameResolutionError(error); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void shutdown() { |
| 108 | + switchLb.shutdown(); |
| 109 | + } |
| 110 | + |
| 111 | + private static final class EndpointWithHash { |
| 112 | + public final EquivalentAddressGroup addressGroup; |
| 113 | + public final long hashCode; |
| 114 | + |
| 115 | + public EndpointWithHash(EquivalentAddressGroup addressGroup, long hashCode) { |
| 116 | + this.addressGroup = addressGroup; |
| 117 | + this.hashCode = hashCode; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + private static final class HashAddressComparator implements Comparator<EndpointWithHash> { |
| 122 | + @Override |
| 123 | + public int compare(EndpointWithHash lhs, EndpointWithHash rhs) { |
| 124 | + return Long.compare(lhs.hashCode, rhs.hashCode); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public static final class RandomSubsettingLoadBalancerConfig { |
| 129 | + public final int subsetSize; |
| 130 | + public final Object childConfig; |
| 131 | + |
| 132 | + private RandomSubsettingLoadBalancerConfig(int subsetSize, Object childConfig) { |
| 133 | + this.subsetSize = subsetSize; |
| 134 | + this.childConfig = childConfig; |
| 135 | + } |
| 136 | + |
| 137 | + public static class Builder { |
| 138 | + int subsetSize; |
| 139 | + Object childConfig; |
| 140 | + |
| 141 | + public Builder setSubsetSize(long subsetSize) { |
| 142 | + checkArgument(subsetSize > 0L, "Subset size must be greater than 0"); |
| 143 | + // clamping subset size to Integer.MAX_VALUE due to collection indexing limitations in JVM |
| 144 | + this.subsetSize = Ints.saturatedCast(subsetSize); |
| 145 | + return this; |
| 146 | + } |
| 147 | + |
| 148 | + public Builder setChildConfig(Object childConfig) { |
| 149 | + this.childConfig = checkNotNull(childConfig, "childConfig"); |
| 150 | + return this; |
| 151 | + } |
| 152 | + |
| 153 | + public RandomSubsettingLoadBalancerConfig build() { |
| 154 | + checkState(subsetSize != 0L, "Subset size must be set before building the config"); |
| 155 | + return new RandomSubsettingLoadBalancerConfig( |
| 156 | + subsetSize, |
| 157 | + checkNotNull(childConfig, "childConfig")); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments