• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19413

09 Aug 2024 11:24PM UTC coverage: 84.488% (-0.01%) from 84.502%
#19413

push

github

ejona86
xds: Replace WrrHelper with a per-child Helper

There's no need to assume which child makes a subchannel based on the
subchannel address.

33267 of 39375 relevant lines covered (84.49%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.38
/../xds/src/main/java/io/grpc/xds/WeightedRoundRobinLoadBalancer.java
1
/*
2
 * Copyright 2023 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.xds;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkNotNull;
21

22
import com.google.common.annotations.VisibleForTesting;
23
import com.google.common.base.MoreObjects;
24
import com.google.common.base.Preconditions;
25
import com.google.common.collect.ImmutableList;
26
import com.google.common.collect.Lists;
27
import io.grpc.ConnectivityState;
28
import io.grpc.ConnectivityStateInfo;
29
import io.grpc.Deadline.Ticker;
30
import io.grpc.DoubleHistogramMetricInstrument;
31
import io.grpc.EquivalentAddressGroup;
32
import io.grpc.ExperimentalApi;
33
import io.grpc.LoadBalancer;
34
import io.grpc.LoadBalancerProvider;
35
import io.grpc.LongCounterMetricInstrument;
36
import io.grpc.MetricInstrumentRegistry;
37
import io.grpc.NameResolver;
38
import io.grpc.Status;
39
import io.grpc.SynchronizationContext;
40
import io.grpc.SynchronizationContext.ScheduledHandle;
41
import io.grpc.services.MetricReport;
42
import io.grpc.util.ForwardingSubchannel;
43
import io.grpc.util.MultiChildLoadBalancer;
44
import io.grpc.xds.orca.OrcaOobUtil;
45
import io.grpc.xds.orca.OrcaOobUtil.OrcaOobReportListener;
46
import io.grpc.xds.orca.OrcaPerRequestUtil;
47
import io.grpc.xds.orca.OrcaPerRequestUtil.OrcaPerRequestReportListener;
48
import java.util.Collection;
49
import java.util.HashMap;
50
import java.util.HashSet;
51
import java.util.List;
52
import java.util.Map;
53
import java.util.Random;
54
import java.util.Set;
55
import java.util.concurrent.ScheduledExecutorService;
56
import java.util.concurrent.TimeUnit;
57
import java.util.concurrent.atomic.AtomicInteger;
58
import java.util.logging.Level;
59
import java.util.logging.Logger;
60

61
/**
62
 * A {@link LoadBalancer} that provides weighted-round-robin load-balancing over the
63
 * {@link EquivalentAddressGroup}s from the {@link NameResolver}. The subchannel weights are
64
 * determined by backend metrics using ORCA.
65
 * To use WRR, users may configure through channel serviceConfig. Example config:
66
 * <pre> {@code
67
 *       String wrrConfig = "{\"loadBalancingConfig\":" +
68
 *           "[{\"weighted_round_robin\":{\"enableOobLoadReport\":true, " +
69
 *           "\"blackoutPeriod\":\"10s\"," +
70
 *           "\"oobReportingPeriod\":\"10s\"," +
71
 *           "\"weightExpirationPeriod\":\"180s\"," +
72
 *           "\"errorUtilizationPenalty\":\"1.0\"," +
73
 *           "\"weightUpdatePeriod\":\"1s\"}}]}";
74
 *        serviceConfig = (Map<String, ?>) JsonParser.parse(wrrConfig);
75
 *        channel = ManagedChannelBuilder.forTarget("test:///lb.test.grpc.io")
76
 *            .defaultServiceConfig(serviceConfig)
77
 *            .build();
78
 *  }
79
 *  </pre>
80
 *  Users may also configure through xDS control plane via custom lb policy. But that is much more
81
 *  complex to set up. Example config:
82
 *  <pre>
83
 *  localityLbPolicies:
84
 *   - customPolicy:
85
 *       name: weighted_round_robin
86
 *       data: '{ "enableOobLoadReport": true }'
87
 *  </pre>
88
 *  See related documentation: https://cloud.google.com/service-mesh/legacy/load-balancing-apis/proxyless-configure-advanced-traffic-management#custom-lb-config
89
 */
90
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/9885")
91
final class WeightedRoundRobinLoadBalancer extends MultiChildLoadBalancer {
92

93
  private static final LongCounterMetricInstrument RR_FALLBACK_COUNTER;
94
  private static final LongCounterMetricInstrument ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER;
95
  private static final LongCounterMetricInstrument ENDPOINT_WEIGHT_STALE_COUNTER;
96
  private static final DoubleHistogramMetricInstrument ENDPOINT_WEIGHTS_HISTOGRAM;
97
  private static final Logger log = Logger.getLogger(
1✔
98
      WeightedRoundRobinLoadBalancer.class.getName());
1✔
99
  private WeightedRoundRobinLoadBalancerConfig config;
100
  private final SynchronizationContext syncContext;
101
  private final ScheduledExecutorService timeService;
102
  private ScheduledHandle weightUpdateTimer;
103
  private final Runnable updateWeightTask;
104
  private final AtomicInteger sequence;
105
  private final long infTime;
106
  private final Ticker ticker;
107
  private String locality = "";
1✔
108
  private SubchannelPicker currentPicker = new FixedResultPicker(PickResult.withNoResult());
1✔
109

110
  // The metric instruments are only registered once and shared by all instances of this LB.
111
  static {
112
    MetricInstrumentRegistry metricInstrumentRegistry
113
        = MetricInstrumentRegistry.getDefaultRegistry();
1✔
114
    RR_FALLBACK_COUNTER = metricInstrumentRegistry.registerLongCounter("grpc.lb.wrr.rr_fallback",
1✔
115
        "EXPERIMENTAL. Number of scheduler updates in which there were not enough endpoints "
116
            + "with valid weight, which caused the WRR policy to fall back to RR behavior",
117
        "{update}", Lists.newArrayList("grpc.target"), Lists.newArrayList("grpc.lb.locality"),
1✔
118
        false);
119
    ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER = metricInstrumentRegistry.registerLongCounter(
1✔
120
        "grpc.lb.wrr.endpoint_weight_not_yet_usable", "EXPERIMENTAL. Number of endpoints "
121
            + "from each scheduler update that don't yet have usable weight information",
122
        "{endpoint}", Lists.newArrayList("grpc.target"), Lists.newArrayList("grpc.lb.locality"),
1✔
123
        false);
124
    ENDPOINT_WEIGHT_STALE_COUNTER = metricInstrumentRegistry.registerLongCounter(
1✔
125
        "grpc.lb.wrr.endpoint_weight_stale",
126
        "EXPERIMENTAL. Number of endpoints from each scheduler update whose latest weight is "
127
            + "older than the expiration period", "{endpoint}", Lists.newArrayList("grpc.target"),
1✔
128
        Lists.newArrayList("grpc.lb.locality"), false);
1✔
129
    ENDPOINT_WEIGHTS_HISTOGRAM = metricInstrumentRegistry.registerDoubleHistogram(
1✔
130
        "grpc.lb.wrr.endpoint_weights",
131
        "EXPERIMENTAL. The histogram buckets will be endpoint weight ranges.",
132
        "{weight}", Lists.newArrayList(), Lists.newArrayList("grpc.target"),
1✔
133
        Lists.newArrayList("grpc.lb.locality"),
1✔
134
        false);
135
  }
1✔
136

137
  public WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker) {
138
    this(helper, ticker, new Random());
1✔
139
  }
1✔
140

141
  @VisibleForTesting
142
  WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker, Random random) {
143
    super(OrcaOobUtil.newOrcaReportingHelper(helper));
1✔
144
    this.ticker = checkNotNull(ticker, "ticker");
1✔
145
    this.infTime = ticker.nanoTime() + Long.MAX_VALUE;
1✔
146
    this.syncContext = checkNotNull(helper.getSynchronizationContext(), "syncContext");
1✔
147
    this.timeService = checkNotNull(helper.getScheduledExecutorService(), "timeService");
1✔
148
    this.updateWeightTask = new UpdateWeightTask();
1✔
149
    this.sequence = new AtomicInteger(random.nextInt());
1✔
150
    log.log(Level.FINE, "weighted_round_robin LB created");
1✔
151
  }
1✔
152

153
  @Override
154
  protected ChildLbState createChildLbState(Object key, Object policyConfig,
155
      SubchannelPicker initialPicker, ResolvedAddresses unused) {
156
    ChildLbState childLbState = new WeightedChildLbState(key, pickFirstLbProvider, policyConfig,
1✔
157
        initialPicker);
158
    return childLbState;
1✔
159
  }
160

161
  @Override
162
  public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
163
    if (resolvedAddresses.getLoadBalancingPolicyConfig() == null) {
1✔
164
      Status unavailableStatus = Status.UNAVAILABLE.withDescription(
1✔
165
              "NameResolver returned no WeightedRoundRobinLoadBalancerConfig. addrs="
166
                      + resolvedAddresses.getAddresses()
1✔
167
                      + ", attrs=" + resolvedAddresses.getAttributes());
1✔
168
      handleNameResolutionError(unavailableStatus);
1✔
169
      return unavailableStatus;
1✔
170
    }
171
    String locality = resolvedAddresses.getAttributes().get(WeightedTargetLoadBalancer.CHILD_NAME);
1✔
172
    if (locality != null) {
1✔
173
      this.locality = locality;
1✔
174
    } else {
175
      this.locality = "";
1✔
176
    }
177
    config =
1✔
178
            (WeightedRoundRobinLoadBalancerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
1✔
179
    AcceptResolvedAddrRetVal acceptRetVal;
180
    try {
181
      resolvingAddresses = true;
1✔
182
      acceptRetVal = acceptResolvedAddressesInternal(resolvedAddresses);
1✔
183
      if (!acceptRetVal.status.isOk()) {
1✔
184
        return acceptRetVal.status;
×
185
      }
186

187
      if (weightUpdateTimer != null && weightUpdateTimer.isPending()) {
1✔
188
        weightUpdateTimer.cancel();
1✔
189
      }
190
      updateWeightTask.run();
1✔
191

192
      createAndApplyOrcaListeners();
1✔
193

194
      // Must update channel picker before return so that new RPCs will not be routed to deleted
195
      // clusters and resolver can remove them in service config.
196
      updateOverallBalancingState();
1✔
197

198
      shutdownRemoved(acceptRetVal.removedChildren);
1✔
199
    } finally {
200
      resolvingAddresses = false;
1✔
201
    }
202

203
    return acceptRetVal.status;
1✔
204
  }
205

206
  /**
207
   * Updates picker with the list of active subchannels (state == READY).
208
   */
209
  @Override
210
  protected void updateOverallBalancingState() {
211
    List<ChildLbState> activeList = getReadyChildren();
1✔
212
    if (activeList.isEmpty()) {
1✔
213
      // No READY subchannels
214

215
      // MultiChildLB will request connection immediately on subchannel IDLE.
216
      boolean isConnecting = false;
1✔
217
      for (ChildLbState childLbState : getChildLbStates()) {
1✔
218
        ConnectivityState state = childLbState.getCurrentState();
1✔
219
        if (state == ConnectivityState.CONNECTING || state == ConnectivityState.IDLE) {
1✔
220
          isConnecting = true;
1✔
221
          break;
1✔
222
        }
223
      }
1✔
224

225
      if (isConnecting) {
1✔
226
        updateBalancingState(
1✔
227
            ConnectivityState.CONNECTING, new FixedResultPicker(PickResult.withNoResult()));
1✔
228
      } else {
229
        updateBalancingState(
1✔
230
            ConnectivityState.TRANSIENT_FAILURE, createReadyPicker(getChildLbStates()));
1✔
231
      }
232
    } else {
1✔
233
      updateBalancingState(ConnectivityState.READY, createReadyPicker(activeList));
1✔
234
    }
235
  }
1✔
236

237
  private SubchannelPicker createReadyPicker(Collection<ChildLbState> activeList) {
238
    return new WeightedRoundRobinPicker(ImmutableList.copyOf(activeList),
1✔
239
        config.enableOobLoadReport, config.errorUtilizationPenalty, sequence, getHelper(),
1✔
240
        locality);
241
  }
242

243
  private void updateBalancingState(ConnectivityState state, SubchannelPicker picker) {
244
    if (state != currentConnectivityState || !picker.equals(currentPicker)) {
1✔
245
      getHelper().updateBalancingState(state, picker);
1✔
246
      currentConnectivityState = state;
1✔
247
      currentPicker = picker;
1✔
248
    }
249
  }
1✔
250

251
  @VisibleForTesting
252
  final class WeightedChildLbState extends ChildLbState {
253

254
    private final Set<WrrSubchannel> subchannels = new HashSet<>();
1✔
255
    private volatile long lastUpdated;
256
    private volatile long nonEmptySince;
257
    private volatile double weight = 0;
1✔
258

259
    private OrcaReportListener orcaReportListener;
260

261
    public WeightedChildLbState(Object key, LoadBalancerProvider policyProvider, Object childConfig,
262
        SubchannelPicker initialPicker) {
1✔
263
      super(key, policyProvider, childConfig, initialPicker);
1✔
264
    }
1✔
265

266
    @Override
267
    protected ChildLbStateHelper createChildHelper() {
268
      return new WrrChildLbStateHelper();
1✔
269
    }
270

271
    private double getWeight(AtomicInteger staleEndpoints, AtomicInteger notYetUsableEndpoints) {
272
      if (config == null) {
1✔
273
        return 0;
×
274
      }
275
      long now = ticker.nanoTime();
1✔
276
      if (now - lastUpdated >= config.weightExpirationPeriodNanos) {
1✔
277
        nonEmptySince = infTime;
1✔
278
        staleEndpoints.incrementAndGet();
1✔
279
        return 0;
1✔
280
      } else if (now - nonEmptySince < config.blackoutPeriodNanos
1✔
281
          && config.blackoutPeriodNanos > 0) {
1✔
282
        notYetUsableEndpoints.incrementAndGet();
1✔
283
        return 0;
1✔
284
      } else {
285
        return weight;
1✔
286
      }
287
    }
288

289
    public void addSubchannel(WrrSubchannel wrrSubchannel) {
290
      subchannels.add(wrrSubchannel);
1✔
291
    }
1✔
292

293
    public OrcaReportListener getOrCreateOrcaListener(float errorUtilizationPenalty) {
294
      if (orcaReportListener != null
1✔
295
          && orcaReportListener.errorUtilizationPenalty == errorUtilizationPenalty) {
1✔
296
        return orcaReportListener;
1✔
297
      }
298
      orcaReportListener = new OrcaReportListener(errorUtilizationPenalty);
1✔
299
      return orcaReportListener;
1✔
300
    }
301

302
    public void removeSubchannel(WrrSubchannel wrrSubchannel) {
303
      subchannels.remove(wrrSubchannel);
1✔
304
    }
1✔
305

306
    final class WrrChildLbStateHelper extends ChildLbStateHelper {
1✔
307
      @Override
308
      public Subchannel createSubchannel(CreateSubchannelArgs args) {
309
        return new WrrSubchannel(super.createSubchannel(args), WeightedChildLbState.this);
1✔
310
      }
311
    }
312

313
    final class OrcaReportListener implements OrcaPerRequestReportListener, OrcaOobReportListener {
314
      private final float errorUtilizationPenalty;
315

316
      OrcaReportListener(float errorUtilizationPenalty) {
1✔
317
        this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
318
      }
1✔
319

320
      @Override
321
      public void onLoadReport(MetricReport report) {
322
        double newWeight = 0;
1✔
323
        // Prefer application utilization and fallback to CPU utilization if unset.
324
        double utilization =
325
            report.getApplicationUtilization() > 0 ? report.getApplicationUtilization()
1✔
326
                : report.getCpuUtilization();
1✔
327
        if (utilization > 0 && report.getQps() > 0) {
1✔
328
          double penalty = 0;
1✔
329
          if (report.getEps() > 0 && errorUtilizationPenalty > 0) {
1✔
330
            penalty = report.getEps() / report.getQps() * errorUtilizationPenalty;
1✔
331
          }
332
          newWeight = report.getQps() / (utilization + penalty);
1✔
333
        }
334
        if (newWeight == 0) {
1✔
335
          return;
1✔
336
        }
337
        if (nonEmptySince == infTime) {
1✔
338
          nonEmptySince = ticker.nanoTime();
1✔
339
        }
340
        lastUpdated = ticker.nanoTime();
1✔
341
        weight = newWeight;
1✔
342
      }
1✔
343
    }
344
  }
345

346
  private final class UpdateWeightTask implements Runnable {
1✔
347
    @Override
348
    public void run() {
349
      if (currentPicker != null && currentPicker instanceof WeightedRoundRobinPicker) {
1✔
350
        ((WeightedRoundRobinPicker) currentPicker).updateWeight();
1✔
351
      }
352
      weightUpdateTimer = syncContext.schedule(this, config.weightUpdatePeriodNanos,
1✔
353
          TimeUnit.NANOSECONDS, timeService);
1✔
354
    }
1✔
355
  }
356

357
  private void createAndApplyOrcaListeners() {
358
    for (ChildLbState child : getChildLbStates()) {
1✔
359
      WeightedChildLbState wChild = (WeightedChildLbState) child;
1✔
360
      for (WrrSubchannel weightedSubchannel : wChild.subchannels) {
1✔
361
        if (config.enableOobLoadReport) {
1✔
362
          OrcaOobUtil.setListener(weightedSubchannel,
1✔
363
              wChild.getOrCreateOrcaListener(config.errorUtilizationPenalty),
1✔
364
              OrcaOobUtil.OrcaReportingConfig.newBuilder()
1✔
365
                  .setReportInterval(config.oobReportingPeriodNanos, TimeUnit.NANOSECONDS)
1✔
366
                  .build());
1✔
367
        } else {
368
          OrcaOobUtil.setListener(weightedSubchannel, null, null);
1✔
369
        }
370
      }
1✔
371
    }
1✔
372
  }
1✔
373

374
  @Override
375
  public void shutdown() {
376
    if (weightUpdateTimer != null) {
1✔
377
      weightUpdateTimer.cancel();
1✔
378
    }
379
    super.shutdown();
1✔
380
  }
1✔
381

382
  @VisibleForTesting
383
  final class WrrSubchannel extends ForwardingSubchannel {
384
    private final Subchannel delegate;
385
    private final WeightedChildLbState owner;
386

387
    WrrSubchannel(Subchannel delegate, WeightedChildLbState owner) {
1✔
388
      this.delegate = checkNotNull(delegate, "delegate");
1✔
389
      this.owner = checkNotNull(owner, "owner");
1✔
390
    }
1✔
391

392
    @Override
393
    public void start(SubchannelStateListener listener) {
394
      owner.addSubchannel(this);
1✔
395
      delegate().start(new SubchannelStateListener() {
1✔
396
        @Override
397
        public void onSubchannelState(ConnectivityStateInfo newState) {
398
          if (newState.getState().equals(ConnectivityState.READY)) {
1✔
399
            owner.nonEmptySince = infTime;
1✔
400
          }
401
          listener.onSubchannelState(newState);
1✔
402
        }
1✔
403
      });
404
    }
1✔
405

406
    @Override
407
    protected Subchannel delegate() {
408
      return delegate;
1✔
409
    }
410

411
    @Override
412
    public void shutdown() {
413
      super.shutdown();
1✔
414
      owner.removeSubchannel(this);
1✔
415
    }
1✔
416
  }
417

418
  @VisibleForTesting
419
  static final class WeightedRoundRobinPicker extends SubchannelPicker {
420
    private final List<ChildLbState> children;
421
    private final Map<Subchannel, OrcaPerRequestReportListener> subchannelToReportListenerMap =
1✔
422
        new HashMap<>();
423
    private final boolean enableOobLoadReport;
424
    private final float errorUtilizationPenalty;
425
    private final AtomicInteger sequence;
426
    private final int hashCode;
427
    private final LoadBalancer.Helper helper;
428
    private final String locality;
429
    private volatile StaticStrideScheduler scheduler;
430

431
    WeightedRoundRobinPicker(List<ChildLbState> children, boolean enableOobLoadReport,
432
        float errorUtilizationPenalty, AtomicInteger sequence, LoadBalancer.Helper helper,
433
        String locality) {
1✔
434
      checkNotNull(children, "children");
1✔
435
      Preconditions.checkArgument(!children.isEmpty(), "empty child list");
1✔
436
      this.children = children;
1✔
437
      for (ChildLbState child : children) {
1✔
438
        WeightedChildLbState wChild = (WeightedChildLbState) child;
1✔
439
        for (WrrSubchannel subchannel : wChild.subchannels) {
1✔
440
          this.subchannelToReportListenerMap
1✔
441
              .put(subchannel, wChild.getOrCreateOrcaListener(errorUtilizationPenalty));
1✔
442
        }
1✔
443
      }
1✔
444
      this.enableOobLoadReport = enableOobLoadReport;
1✔
445
      this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
446
      this.sequence = checkNotNull(sequence, "sequence");
1✔
447
      this.helper = helper;
1✔
448
      this.locality = checkNotNull(locality, "locality");
1✔
449

450
      // For equality we treat children as a set; use hash code as defined by Set
451
      int sum = 0;
1✔
452
      for (ChildLbState child : children) {
1✔
453
        sum += child.hashCode();
1✔
454
      }
1✔
455
      this.hashCode = sum
1✔
456
          ^ Boolean.hashCode(enableOobLoadReport)
1✔
457
          ^ Float.hashCode(errorUtilizationPenalty);
1✔
458

459
      updateWeight();
1✔
460
    }
1✔
461

462
    @Override
463
    public PickResult pickSubchannel(PickSubchannelArgs args) {
464
      ChildLbState childLbState = children.get(scheduler.pick());
1✔
465
      WeightedChildLbState wChild = (WeightedChildLbState) childLbState;
1✔
466
      PickResult pickResult = childLbState.getCurrentPicker().pickSubchannel(args);
1✔
467
      Subchannel subchannel = pickResult.getSubchannel();
1✔
468
      if (subchannel == null) {
1✔
469
        return pickResult;
1✔
470
      }
471
      if (!enableOobLoadReport) {
1✔
472
        return PickResult.withSubchannel(subchannel,
1✔
473
            OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(
1✔
474
                subchannelToReportListenerMap.getOrDefault(subchannel,
1✔
475
                    wChild.getOrCreateOrcaListener(errorUtilizationPenalty))));
1✔
476
      } else {
477
        return PickResult.withSubchannel(subchannel);
1✔
478
      }
479
    }
480

481
    private void updateWeight() {
482
      float[] newWeights = new float[children.size()];
1✔
483
      AtomicInteger staleEndpoints = new AtomicInteger();
1✔
484
      AtomicInteger notYetUsableEndpoints = new AtomicInteger();
1✔
485
      for (int i = 0; i < children.size(); i++) {
1✔
486
        double newWeight = ((WeightedChildLbState) children.get(i)).getWeight(staleEndpoints,
1✔
487
            notYetUsableEndpoints);
488
        // TODO: add locality label once available
489
        helper.getMetricRecorder()
1✔
490
            .recordDoubleHistogram(ENDPOINT_WEIGHTS_HISTOGRAM, newWeight,
1✔
491
                ImmutableList.of(helper.getChannelTarget()),
1✔
492
                ImmutableList.of(locality));
1✔
493
        newWeights[i] = newWeight > 0 ? (float) newWeight : 0.0f;
1✔
494
      }
495
      if (staleEndpoints.get() > 0) {
1✔
496
        // TODO: add locality label once available
497
        helper.getMetricRecorder()
1✔
498
            .addLongCounter(ENDPOINT_WEIGHT_STALE_COUNTER, staleEndpoints.get(),
1✔
499
                ImmutableList.of(helper.getChannelTarget()),
1✔
500
                ImmutableList.of(locality));
1✔
501
      }
502
      if (notYetUsableEndpoints.get() > 0) {
1✔
503
        // TODO: add locality label once available
504
        helper.getMetricRecorder()
1✔
505
            .addLongCounter(ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER, notYetUsableEndpoints.get(),
1✔
506
                ImmutableList.of(helper.getChannelTarget()), ImmutableList.of(locality));
1✔
507
      }
508

509
      this.scheduler = new StaticStrideScheduler(newWeights, sequence);
1✔
510
      if (this.scheduler.usesRoundRobin()) {
1✔
511
        // TODO: locality label once available
512
        helper.getMetricRecorder()
1✔
513
            .addLongCounter(RR_FALLBACK_COUNTER, 1, ImmutableList.of(helper.getChannelTarget()),
1✔
514
                ImmutableList.of(locality));
1✔
515
      }
516
    }
1✔
517

518
    @Override
519
    public String toString() {
520
      return MoreObjects.toStringHelper(WeightedRoundRobinPicker.class)
1✔
521
          .add("enableOobLoadReport", enableOobLoadReport)
1✔
522
          .add("errorUtilizationPenalty", errorUtilizationPenalty)
1✔
523
          .add("list", children).toString();
1✔
524
    }
525

526
    @VisibleForTesting
527
    List<ChildLbState> getChildren() {
528
      return children;
1✔
529
    }
530

531
    @Override
532
    public int hashCode() {
533
      return hashCode;
×
534
    }
535

536
    @Override
537
    public boolean equals(Object o) {
538
      if (!(o instanceof WeightedRoundRobinPicker)) {
1✔
539
        return false;
×
540
      }
541
      WeightedRoundRobinPicker other = (WeightedRoundRobinPicker) o;
1✔
542
      if (other == this) {
1✔
543
        return true;
×
544
      }
545
      // the lists cannot contain duplicate subchannels
546
      return hashCode == other.hashCode
1✔
547
          && sequence == other.sequence
548
          && enableOobLoadReport == other.enableOobLoadReport
549
          && Float.compare(errorUtilizationPenalty, other.errorUtilizationPenalty) == 0
1✔
550
          && children.size() == other.children.size()
1✔
551
          && new HashSet<>(children).containsAll(other.children);
1✔
552
    }
553
  }
554

555
  /*
556
   * The Static Stride Scheduler is an implementation of an earliest deadline first (EDF) scheduler
557
   * in which each object's deadline is the multiplicative inverse of the object's weight.
558
   * <p>
559
   * The way in which this is implemented is through a static stride scheduler. 
560
   * The Static Stride Scheduler works by iterating through the list of subchannel weights
561
   * and using modular arithmetic to proportionally distribute picks, favoring entries 
562
   * with higher weights. It is based on the observation that the intended sequence generated 
563
   * from an EDF scheduler is a periodic one that can be achieved through modular arithmetic. 
564
   * The Static Stride Scheduler is more performant than other implementations of the EDF
565
   * Scheduler, as it removes the need for a priority queue (and thus mutex locks).
566
   * <p>
567
   * go/static-stride-scheduler
568
   * <p>
569
   *
570
   * <ul>
571
   *  <li>nextSequence() - O(1)
572
   *  <li>pick() - O(n)
573
   */
574
  @VisibleForTesting
575
  static final class StaticStrideScheduler {
576
    private final short[] scaledWeights;
577
    private final AtomicInteger sequence;
578
    private final boolean usesRoundRobin;
579
    private static final int K_MAX_WEIGHT = 0xFFFF;
580

581
    // Assuming the mean of all known weights is M, StaticStrideScheduler will clamp
582
    // weights bigger than M*kMaxRatio and weights smaller than M*kMinRatio.
583
    //
584
    // This is done as a performance optimization by limiting the number of rounds for picks
585
    // for edge cases where channels have large differences in subchannel weights.
586
    // In this case, without these clips, it would potentially require the scheduler to
587
    // frequently traverse through the entire subchannel list within the pick method.
588
    //
589
    // The current values of 10 and 0.1 were chosen without any experimenting. It should
590
    // decrease the amount of sequences that the scheduler must traverse through in order
591
    // to pick a high weight subchannel in such corner cases.
592
    // But, it also makes WeightedRoundRobin to send slightly more requests to
593
    // potentially very bad tasks (that would have near-zero weights) than zero.
594
    // This is not necessarily a downside, though. Perhaps this is not a problem at
595
    // all, and we can increase this value if needed to save CPU cycles.
596
    private static final double K_MAX_RATIO = 10;
597
    private static final double K_MIN_RATIO = 0.1;
598

599
    StaticStrideScheduler(float[] weights, AtomicInteger sequence) {
1✔
600
      checkArgument(weights.length >= 1, "Couldn't build scheduler: requires at least one weight");
1✔
601
      int numChannels = weights.length;
1✔
602
      int numWeightedChannels = 0;
1✔
603
      double sumWeight = 0;
1✔
604
      double unscaledMeanWeight;
605
      float unscaledMaxWeight = 0;
1✔
606
      for (float weight : weights) {
1✔
607
        if (weight > 0) {
1✔
608
          sumWeight += weight;
1✔
609
          unscaledMaxWeight = Math.max(weight, unscaledMaxWeight);
1✔
610
          numWeightedChannels++;
1✔
611
        }
612
      }
613

614
      // Adjust max value s.t. ratio does not exceed K_MAX_RATIO. This should
615
      // ensure that we on average do at most K_MAX_RATIO rounds for picks.
616
      if (numWeightedChannels > 0) {
1✔
617
        unscaledMeanWeight = sumWeight / numWeightedChannels;
1✔
618
        unscaledMaxWeight = Math.min(unscaledMaxWeight, (float) (K_MAX_RATIO * unscaledMeanWeight));
1✔
619
      } else {
620
        // Fall back to round robin if all values are non-positives. Note that
621
        // numWeightedChannels == 1 also behaves like RR because the weights are all the same, but
622
        // the weights aren't 1, so it doesn't go through this path.
623
        unscaledMeanWeight = 1;
1✔
624
        unscaledMaxWeight = 1;
1✔
625
      }
626
      // We need at least two weights for WRR to be distinguishable from round_robin.
627
      usesRoundRobin = numWeightedChannels < 2;
1✔
628

629
      // Scales weights s.t. max(weights) == K_MAX_WEIGHT, meanWeight is scaled accordingly.
630
      // Note that, since we cap the weights to stay within K_MAX_RATIO, meanWeight might not
631
      // match the actual mean of the values that end up in the scheduler.
632
      double scalingFactor = K_MAX_WEIGHT / unscaledMaxWeight;
1✔
633
      // We compute weightLowerBound and clamp it to 1 from below so that in the
634
      // worst case, we represent tiny weights as 1.
635
      int weightLowerBound = (int) Math.ceil(scalingFactor * unscaledMeanWeight * K_MIN_RATIO);
1✔
636
      short[] scaledWeights = new short[numChannels];
1✔
637
      for (int i = 0; i < numChannels; i++) {
1✔
638
        if (weights[i] <= 0) {
1✔
639
          scaledWeights[i] = (short) Math.round(scalingFactor * unscaledMeanWeight);
1✔
640
        } else {
641
          int weight = (int) Math.round(scalingFactor * Math.min(weights[i], unscaledMaxWeight));
1✔
642
          scaledWeights[i] = (short) Math.max(weight, weightLowerBound);
1✔
643
        }
644
      }
645

646
      this.scaledWeights = scaledWeights;
1✔
647
      this.sequence = sequence;
1✔
648
    }
1✔
649

650
    // Without properly weighted channels, we do plain vanilla round_robin.
651
    boolean usesRoundRobin() {
652
      return usesRoundRobin;
1✔
653
    }
654

655
    /**
656
     * Returns the next sequence number and atomically increases sequence with wraparound.
657
     */
658
    private long nextSequence() {
659
      return Integer.toUnsignedLong(sequence.getAndIncrement());
1✔
660
    }
661

662
    /*
663
     * Selects index of next backend server.
664
     * <p>
665
     * A 2D array is compactly represented as a function of W(backend), where the row
666
     * represents the generation and the column represents the backend index:
667
     * X(backend,generation) | generation ∈ [0,kMaxWeight).
668
     * Each element in the conceptual array is a boolean indicating whether the backend at
669
     * this index should be picked now. If false, the counter is incremented again,
670
     * and the new element is checked. An atomically incremented counter keeps track of our
671
     * backend and generation through modular arithmetic within the pick() method.
672
     * <p>
673
     * Modular arithmetic allows us to evenly distribute picks and skips between
674
     * generations based on W(backend).
675
     * X(backend,generation) = (W(backend) * generation) % kMaxWeight >= kMaxWeight - W(backend)
676
     * If we have the same three backends with weights:
677
     * W(backend) = {2,3,6} scaled to max(W(backend)) = 6, then X(backend,generation) is:
678
     * <p>
679
     * B0    B1    B2
680
     * T     T     T
681
     * F     F     T
682
     * F     T     T
683
     * T     F     T
684
     * F     T     T
685
     * F     F     T
686
     * The sequence of picked backend indices is given by
687
     * walking across and down: {0,1,2,2,1,2,0,2,1,2,2}.
688
     * <p>
689
     * To reduce the variance and spread the wasted work among different picks,
690
     * an offset that varies per backend index is also included to the calculation.
691
     */
692
    int pick() {
693
      while (true) {
694
        long sequence = this.nextSequence();
1✔
695
        int backendIndex = (int) (sequence % scaledWeights.length);
1✔
696
        long generation = sequence / scaledWeights.length;
1✔
697
        int weight = Short.toUnsignedInt(scaledWeights[backendIndex]);
1✔
698
        long offset = (long) K_MAX_WEIGHT / 2 * backendIndex;
1✔
699
        if ((weight * generation + offset) % K_MAX_WEIGHT < K_MAX_WEIGHT - weight) {
1✔
700
          continue;
1✔
701
        }
702
        return backendIndex;
1✔
703
      }
704
    }
705
  }
706

707
  static final class WeightedRoundRobinLoadBalancerConfig {
708
    final long blackoutPeriodNanos;
709
    final long weightExpirationPeriodNanos;
710
    final boolean enableOobLoadReport;
711
    final long oobReportingPeriodNanos;
712
    final long weightUpdatePeriodNanos;
713
    final float errorUtilizationPenalty;
714

715
    public static Builder newBuilder() {
716
      return new Builder();
1✔
717
    }
718

719
    private WeightedRoundRobinLoadBalancerConfig(long blackoutPeriodNanos,
720
                                                 long weightExpirationPeriodNanos,
721
                                                 boolean enableOobLoadReport,
722
                                                 long oobReportingPeriodNanos,
723
                                                 long weightUpdatePeriodNanos,
724
                                                 float errorUtilizationPenalty) {
1✔
725
      this.blackoutPeriodNanos = blackoutPeriodNanos;
1✔
726
      this.weightExpirationPeriodNanos = weightExpirationPeriodNanos;
1✔
727
      this.enableOobLoadReport = enableOobLoadReport;
1✔
728
      this.oobReportingPeriodNanos = oobReportingPeriodNanos;
1✔
729
      this.weightUpdatePeriodNanos = weightUpdatePeriodNanos;
1✔
730
      this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
731
    }
1✔
732

733
    static final class Builder {
734
      long blackoutPeriodNanos = 10_000_000_000L; // 10s
1✔
735
      long weightExpirationPeriodNanos = 180_000_000_000L; //3min
1✔
736
      boolean enableOobLoadReport = false;
1✔
737
      long oobReportingPeriodNanos = 10_000_000_000L; // 10s
1✔
738
      long weightUpdatePeriodNanos = 1_000_000_000L; // 1s
1✔
739
      float errorUtilizationPenalty = 1.0F;
1✔
740

741
      private Builder() {
1✔
742

743
      }
1✔
744

745
      @SuppressWarnings("UnusedReturnValue")
746
      Builder setBlackoutPeriodNanos(long blackoutPeriodNanos) {
747
        this.blackoutPeriodNanos = blackoutPeriodNanos;
1✔
748
        return this;
1✔
749
      }
750

751
      @SuppressWarnings("UnusedReturnValue")
752
      Builder setWeightExpirationPeriodNanos(long weightExpirationPeriodNanos) {
753
        this.weightExpirationPeriodNanos = weightExpirationPeriodNanos;
1✔
754
        return this;
1✔
755
      }
756

757
      Builder setEnableOobLoadReport(boolean enableOobLoadReport) {
758
        this.enableOobLoadReport = enableOobLoadReport;
1✔
759
        return this;
1✔
760
      }
761

762
      Builder setOobReportingPeriodNanos(long oobReportingPeriodNanos) {
763
        this.oobReportingPeriodNanos = oobReportingPeriodNanos;
1✔
764
        return this;
1✔
765
      }
766

767
      Builder setWeightUpdatePeriodNanos(long weightUpdatePeriodNanos) {
768
        this.weightUpdatePeriodNanos = weightUpdatePeriodNanos;
1✔
769
        return this;
1✔
770
      }
771

772
      Builder setErrorUtilizationPenalty(float errorUtilizationPenalty) {
773
        this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
774
        return this;
1✔
775
      }
776

777
      WeightedRoundRobinLoadBalancerConfig build() {
778
        return new WeightedRoundRobinLoadBalancerConfig(blackoutPeriodNanos,
1✔
779
                weightExpirationPeriodNanos, enableOobLoadReport, oobReportingPeriodNanos,
780
                weightUpdatePeriodNanos, errorUtilizationPenalty);
781
      }
782
    }
783
  }
784
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc