xds: Replace WrrHelper with a per-child Helper

There's no need to assume which child makes a subchannel based on the
subchannel address.
This commit is contained in:
Eric Anderson 2024-07-27 11:55:27 -07:00
parent 2f4f7f0ece
commit d1dcfb0451
1 changed files with 16 additions and 37 deletions

View File

@ -17,7 +17,6 @@
package io.grpc.xds;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
@ -40,7 +39,6 @@ import io.grpc.Status;
import io.grpc.SynchronizationContext;
import io.grpc.SynchronizationContext.ScheduledHandle;
import io.grpc.services.MetricReport;
import io.grpc.util.ForwardingLoadBalancerHelper;
import io.grpc.util.ForwardingSubchannel;
import io.grpc.util.MultiChildLoadBalancer;
import io.grpc.xds.orca.OrcaOobUtil;
@ -137,12 +135,12 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
}
public WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker) {
this(new WrrHelper(OrcaOobUtil.newOrcaReportingHelper(helper)), ticker, new Random());
this(helper, ticker, new Random());
}
public WeightedRoundRobinLoadBalancer(WrrHelper helper, Ticker ticker, Random random) {
super(helper);
helper.setLoadBalancer(this);
@VisibleForTesting
WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker, Random random) {
super(OrcaOobUtil.newOrcaReportingHelper(helper));
this.ticker = checkNotNull(ticker, "ticker");
this.infTime = ticker.nanoTime() + Long.MAX_VALUE;
this.syncContext = checkNotNull(helper.getSynchronizationContext(), "syncContext");
@ -152,11 +150,6 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
log.log(Level.FINE, "weighted_round_robin LB created");
}
@VisibleForTesting
WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker, Random random) {
this(new WrrHelper(OrcaOobUtil.newOrcaReportingHelper(helper)), ticker, random);
}
@Override
protected ChildLbState createChildLbState(Object key, Object policyConfig,
SubchannelPicker initialPicker, ResolvedAddresses unused) {
@ -270,6 +263,11 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
super(key, policyProvider, childConfig, initialPicker);
}
@Override
protected ChildLbStateHelper createChildHelper() {
return new WrrChildLbStateHelper();
}
private double getWeight(AtomicInteger staleEndpoints, AtomicInteger notYetUsableEndpoints) {
if (config == null) {
return 0;
@ -305,6 +303,13 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
subchannels.remove(wrrSubchannel);
}
final class WrrChildLbStateHelper extends ChildLbStateHelper {
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
return new WrrSubchannel(super.createSubchannel(args), WeightedChildLbState.this);
}
}
final class OrcaReportListener implements OrcaPerRequestReportListener, OrcaOobReportListener {
private final float errorUtilizationPenalty;
@ -374,32 +379,6 @@ final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
super.shutdown();
}
private static final class WrrHelper extends ForwardingLoadBalancerHelper {
private final Helper delegate;
private WeightedRoundRobinLoadBalancer wrr;
WrrHelper(Helper helper) {
this.delegate = helper;
}
void setLoadBalancer(WeightedRoundRobinLoadBalancer lb) {
this.wrr = lb;
}
@Override
protected Helper delegate() {
return delegate;
}
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
checkElementIndex(0, args.getAddresses().size(), "Empty address group");
WeightedChildLbState childLbState =
(WeightedChildLbState) wrr.getChildLbStateEag(args.getAddresses().get(0));
return wrr.new WrrSubchannel(delegate().createSubchannel(args), childLbState);
}
}
@VisibleForTesting
final class WrrSubchannel extends ForwardingSubchannel {
private final Subchannel delegate;