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

grpc / grpc-java / #19419

12 Aug 2024 11:39PM CUT coverage: 84.476% (-0.01%) from 84.489%
#19419

push

github

ejona86
util: MultiChildLb children should always start with a NoResult picker

That's the obvious default, and all current usages use (something
equivalent to) that default.

33389 of 39525 relevant lines covered (84.48%)

0.84 hits per line

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

98.08
/../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.LoadBalancer;
33
import io.grpc.LoadBalancerProvider;
34
import io.grpc.LongCounterMetricInstrument;
35
import io.grpc.MetricInstrumentRegistry;
36
import io.grpc.NameResolver;
37
import io.grpc.Status;
38
import io.grpc.SynchronizationContext;
39
import io.grpc.SynchronizationContext.ScheduledHandle;
40
import io.grpc.services.MetricReport;
41
import io.grpc.util.ForwardingSubchannel;
42
import io.grpc.util.MultiChildLoadBalancer;
43
import io.grpc.xds.orca.OrcaOobUtil;
44
import io.grpc.xds.orca.OrcaOobUtil.OrcaOobReportListener;
45
import io.grpc.xds.orca.OrcaPerRequestUtil;
46
import io.grpc.xds.orca.OrcaPerRequestUtil.OrcaPerRequestReportListener;
47
import java.util.ArrayList;
48
import java.util.Collection;
49
import java.util.HashSet;
50
import java.util.List;
51
import java.util.Random;
52
import java.util.Set;
53
import java.util.concurrent.ScheduledExecutorService;
54
import java.util.concurrent.TimeUnit;
55
import java.util.concurrent.atomic.AtomicInteger;
56
import java.util.logging.Level;
57
import java.util.logging.Logger;
58

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

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

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

134
  public WeightedRoundRobinLoadBalancer(Helper helper, Ticker ticker) {
135
    this(helper, ticker, new Random());
1✔
136
  }
1✔
137

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

150
  @Override
151
  protected ChildLbState createChildLbState(Object key, Object policyConfig,
152
      ResolvedAddresses unused) {
153
    ChildLbState childLbState = new WeightedChildLbState(key, pickFirstLbProvider, policyConfig);
1✔
154
    return childLbState;
1✔
155
  }
156

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

183
      if (weightUpdateTimer != null && weightUpdateTimer.isPending()) {
1✔
184
        weightUpdateTimer.cancel();
1✔
185
      }
186
      updateWeightTask.run();
1✔
187

188
      createAndApplyOrcaListeners();
1✔
189

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

194
      shutdownRemoved(acceptRetVal.removedChildren);
1✔
195
    } finally {
196
      resolvingAddresses = false;
1✔
197
    }
198

199
    return acceptRetVal.status;
1✔
200
  }
201

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

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

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

233
  private SubchannelPicker createReadyPicker(Collection<ChildLbState> activeList) {
234
    WeightedRoundRobinPicker picker = new WeightedRoundRobinPicker(ImmutableList.copyOf(activeList),
1✔
235
        config.enableOobLoadReport, config.errorUtilizationPenalty, sequence);
236
    updateWeight(picker);
1✔
237
    return picker;
1✔
238
  }
239

240
  private void updateWeight(WeightedRoundRobinPicker picker) {
241
    Helper helper = getHelper();
1✔
242
    float[] newWeights = new float[picker.children.size()];
1✔
243
    AtomicInteger staleEndpoints = new AtomicInteger();
1✔
244
    AtomicInteger notYetUsableEndpoints = new AtomicInteger();
1✔
245
    for (int i = 0; i < picker.children.size(); i++) {
1✔
246
      double newWeight = ((WeightedChildLbState) picker.children.get(i)).getWeight(staleEndpoints,
1✔
247
          notYetUsableEndpoints);
248
      helper.getMetricRecorder()
1✔
249
          .recordDoubleHistogram(ENDPOINT_WEIGHTS_HISTOGRAM, newWeight,
1✔
250
              ImmutableList.of(helper.getChannelTarget()),
1✔
251
              ImmutableList.of(locality));
1✔
252
      newWeights[i] = newWeight > 0 ? (float) newWeight : 0.0f;
1✔
253
    }
254

255
    if (staleEndpoints.get() > 0) {
1✔
256
      helper.getMetricRecorder()
1✔
257
          .addLongCounter(ENDPOINT_WEIGHT_STALE_COUNTER, staleEndpoints.get(),
1✔
258
              ImmutableList.of(helper.getChannelTarget()),
1✔
259
              ImmutableList.of(locality));
1✔
260
    }
261
    if (notYetUsableEndpoints.get() > 0) {
1✔
262
      helper.getMetricRecorder()
1✔
263
          .addLongCounter(ENDPOINT_WEIGHT_NOT_YET_USEABLE_COUNTER, notYetUsableEndpoints.get(),
1✔
264
              ImmutableList.of(helper.getChannelTarget()), ImmutableList.of(locality));
1✔
265
    }
266
    boolean weightsEffective = picker.updateWeight(newWeights);
1✔
267
    if (!weightsEffective) {
1✔
268
      helper.getMetricRecorder()
1✔
269
          .addLongCounter(RR_FALLBACK_COUNTER, 1, ImmutableList.of(helper.getChannelTarget()),
1✔
270
              ImmutableList.of(locality));
1✔
271
    }
272
  }
1✔
273

274
  private void updateBalancingState(ConnectivityState state, SubchannelPicker picker) {
275
    if (state != currentConnectivityState || !picker.equals(currentPicker)) {
1✔
276
      getHelper().updateBalancingState(state, picker);
1✔
277
      currentConnectivityState = state;
1✔
278
      currentPicker = picker;
1✔
279
    }
280
  }
1✔
281

282
  @VisibleForTesting
283
  final class WeightedChildLbState extends ChildLbState {
284

285
    private final Set<WrrSubchannel> subchannels = new HashSet<>();
1✔
286
    private volatile long lastUpdated;
287
    private volatile long nonEmptySince;
288
    private volatile double weight = 0;
1✔
289

290
    private OrcaReportListener orcaReportListener;
291

292
    public WeightedChildLbState(
293
        Object key, LoadBalancerProvider policyProvider, Object childConfig) {
1✔
294
      super(key, policyProvider, childConfig);
1✔
295
    }
1✔
296

297
    @Override
298
    protected ChildLbStateHelper createChildHelper() {
299
      return new WrrChildLbStateHelper();
1✔
300
    }
301

302
    private double getWeight(AtomicInteger staleEndpoints, AtomicInteger notYetUsableEndpoints) {
303
      if (config == null) {
1✔
304
        return 0;
×
305
      }
306
      long now = ticker.nanoTime();
1✔
307
      if (now - lastUpdated >= config.weightExpirationPeriodNanos) {
1✔
308
        nonEmptySince = infTime;
1✔
309
        staleEndpoints.incrementAndGet();
1✔
310
        return 0;
1✔
311
      } else if (now - nonEmptySince < config.blackoutPeriodNanos
1✔
312
          && config.blackoutPeriodNanos > 0) {
1✔
313
        notYetUsableEndpoints.incrementAndGet();
1✔
314
        return 0;
1✔
315
      } else {
316
        return weight;
1✔
317
      }
318
    }
319

320
    public void addSubchannel(WrrSubchannel wrrSubchannel) {
321
      subchannels.add(wrrSubchannel);
1✔
322
    }
1✔
323

324
    public OrcaReportListener getOrCreateOrcaListener(float errorUtilizationPenalty) {
325
      if (orcaReportListener != null
1✔
326
          && orcaReportListener.errorUtilizationPenalty == errorUtilizationPenalty) {
1✔
327
        return orcaReportListener;
1✔
328
      }
329
      orcaReportListener = new OrcaReportListener(errorUtilizationPenalty);
1✔
330
      return orcaReportListener;
1✔
331
    }
332

333
    public void removeSubchannel(WrrSubchannel wrrSubchannel) {
334
      subchannels.remove(wrrSubchannel);
1✔
335
    }
1✔
336

337
    final class WrrChildLbStateHelper extends ChildLbStateHelper {
1✔
338
      @Override
339
      public Subchannel createSubchannel(CreateSubchannelArgs args) {
340
        return new WrrSubchannel(super.createSubchannel(args), WeightedChildLbState.this);
1✔
341
      }
342

343
      @Override
344
      public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
345
        super.updateBalancingState(newState, newPicker);
1✔
346
        if (!resolvingAddresses && newState == ConnectivityState.IDLE) {
1✔
347
          getLb().requestConnection();
×
348
        }
349
      }
1✔
350
    }
351

352
    final class OrcaReportListener implements OrcaPerRequestReportListener, OrcaOobReportListener {
353
      private final float errorUtilizationPenalty;
354

355
      OrcaReportListener(float errorUtilizationPenalty) {
1✔
356
        this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
357
      }
1✔
358

359
      @Override
360
      public void onLoadReport(MetricReport report) {
361
        double newWeight = 0;
1✔
362
        // Prefer application utilization and fallback to CPU utilization if unset.
363
        double utilization =
364
            report.getApplicationUtilization() > 0 ? report.getApplicationUtilization()
1✔
365
                : report.getCpuUtilization();
1✔
366
        if (utilization > 0 && report.getQps() > 0) {
1✔
367
          double penalty = 0;
1✔
368
          if (report.getEps() > 0 && errorUtilizationPenalty > 0) {
1✔
369
            penalty = report.getEps() / report.getQps() * errorUtilizationPenalty;
1✔
370
          }
371
          newWeight = report.getQps() / (utilization + penalty);
1✔
372
        }
373
        if (newWeight == 0) {
1✔
374
          return;
1✔
375
        }
376
        if (nonEmptySince == infTime) {
1✔
377
          nonEmptySince = ticker.nanoTime();
1✔
378
        }
379
        lastUpdated = ticker.nanoTime();
1✔
380
        weight = newWeight;
1✔
381
      }
1✔
382
    }
383
  }
384

385
  private final class UpdateWeightTask implements Runnable {
1✔
386
    @Override
387
    public void run() {
388
      if (currentPicker != null && currentPicker instanceof WeightedRoundRobinPicker) {
1✔
389
        updateWeight((WeightedRoundRobinPicker) currentPicker);
1✔
390
      }
391
      weightUpdateTimer = syncContext.schedule(this, config.weightUpdatePeriodNanos,
1✔
392
          TimeUnit.NANOSECONDS, timeService);
1✔
393
    }
1✔
394
  }
395

396
  private void createAndApplyOrcaListeners() {
397
    for (ChildLbState child : getChildLbStates()) {
1✔
398
      WeightedChildLbState wChild = (WeightedChildLbState) child;
1✔
399
      for (WrrSubchannel weightedSubchannel : wChild.subchannels) {
1✔
400
        if (config.enableOobLoadReport) {
1✔
401
          OrcaOobUtil.setListener(weightedSubchannel,
1✔
402
              wChild.getOrCreateOrcaListener(config.errorUtilizationPenalty),
1✔
403
              OrcaOobUtil.OrcaReportingConfig.newBuilder()
1✔
404
                  .setReportInterval(config.oobReportingPeriodNanos, TimeUnit.NANOSECONDS)
1✔
405
                  .build());
1✔
406
        } else {
407
          OrcaOobUtil.setListener(weightedSubchannel, null, null);
1✔
408
        }
409
      }
1✔
410
    }
1✔
411
  }
1✔
412

413
  @Override
414
  public void shutdown() {
415
    if (weightUpdateTimer != null) {
1✔
416
      weightUpdateTimer.cancel();
1✔
417
    }
418
    super.shutdown();
1✔
419
  }
1✔
420

421
  @VisibleForTesting
422
  final class WrrSubchannel extends ForwardingSubchannel {
423
    private final Subchannel delegate;
424
    private final WeightedChildLbState owner;
425

426
    WrrSubchannel(Subchannel delegate, WeightedChildLbState owner) {
1✔
427
      this.delegate = checkNotNull(delegate, "delegate");
1✔
428
      this.owner = checkNotNull(owner, "owner");
1✔
429
    }
1✔
430

431
    @Override
432
    public void start(SubchannelStateListener listener) {
433
      owner.addSubchannel(this);
1✔
434
      delegate().start(new SubchannelStateListener() {
1✔
435
        @Override
436
        public void onSubchannelState(ConnectivityStateInfo newState) {
437
          if (newState.getState().equals(ConnectivityState.READY)) {
1✔
438
            owner.nonEmptySince = infTime;
1✔
439
          }
440
          listener.onSubchannelState(newState);
1✔
441
        }
1✔
442
      });
443
    }
1✔
444

445
    @Override
446
    protected Subchannel delegate() {
447
      return delegate;
1✔
448
    }
449

450
    @Override
451
    public void shutdown() {
452
      super.shutdown();
1✔
453
      owner.removeSubchannel(this);
1✔
454
    }
1✔
455
  }
456

457
  @VisibleForTesting
458
  static final class WeightedRoundRobinPicker extends SubchannelPicker {
459
    // Parallel lists (column-based storage instead of normal row-based storage of List<Struct>).
460
    // The ith element of children corresponds to the ith element of pickers, listeners, and even
461
    // updateWeight(float[]).
462
    private final List<ChildLbState> children; // May only be accessed from sync context
463
    private final List<SubchannelPicker> pickers;
464
    private final List<OrcaPerRequestReportListener> reportListeners;
465
    private final boolean enableOobLoadReport;
466
    private final float errorUtilizationPenalty;
467
    private final AtomicInteger sequence;
468
    private final int hashCode;
469
    private volatile StaticStrideScheduler scheduler;
470

471
    WeightedRoundRobinPicker(List<ChildLbState> children, boolean enableOobLoadReport,
472
        float errorUtilizationPenalty, AtomicInteger sequence) {
1✔
473
      checkNotNull(children, "children");
1✔
474
      Preconditions.checkArgument(!children.isEmpty(), "empty child list");
1✔
475
      this.children = children;
1✔
476
      List<SubchannelPicker> pickers = new ArrayList<>(children.size());
1✔
477
      List<OrcaPerRequestReportListener> reportListeners = new ArrayList<>(children.size());
1✔
478
      for (ChildLbState child : children) {
1✔
479
        WeightedChildLbState wChild = (WeightedChildLbState) child;
1✔
480
        pickers.add(wChild.getCurrentPicker());
1✔
481
        reportListeners.add(wChild.getOrCreateOrcaListener(errorUtilizationPenalty));
1✔
482
      }
1✔
483
      this.pickers = pickers;
1✔
484
      this.reportListeners = reportListeners;
1✔
485
      this.enableOobLoadReport = enableOobLoadReport;
1✔
486
      this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
487
      this.sequence = checkNotNull(sequence, "sequence");
1✔
488

489
      // For equality we treat pickers as a set; use hash code as defined by Set
490
      int sum = 0;
1✔
491
      for (SubchannelPicker picker : pickers) {
1✔
492
        sum += picker.hashCode();
1✔
493
      }
1✔
494
      this.hashCode = sum
1✔
495
          ^ Boolean.hashCode(enableOobLoadReport)
1✔
496
          ^ Float.hashCode(errorUtilizationPenalty);
1✔
497
    }
1✔
498

499
    @Override
500
    public PickResult pickSubchannel(PickSubchannelArgs args) {
501
      int pick = scheduler.pick();
1✔
502
      PickResult pickResult = pickers.get(pick).pickSubchannel(args);
1✔
503
      Subchannel subchannel = pickResult.getSubchannel();
1✔
504
      if (subchannel == null) {
1✔
505
        return pickResult;
1✔
506
      }
507
      if (!enableOobLoadReport) {
1✔
508
        return PickResult.withSubchannel(subchannel,
1✔
509
            OrcaPerRequestUtil.getInstance().newOrcaClientStreamTracerFactory(
1✔
510
                reportListeners.get(pick)));
1✔
511
      } else {
512
        return PickResult.withSubchannel(subchannel);
1✔
513
      }
514
    }
515

516
    /** Returns {@code true} if weights are different than round_robin. */
517
    private boolean updateWeight(float[] newWeights) {
518
      this.scheduler = new StaticStrideScheduler(newWeights, sequence);
1✔
519
      return !this.scheduler.usesRoundRobin();
1✔
520
    }
521

522
    @Override
523
    public String toString() {
524
      return MoreObjects.toStringHelper(WeightedRoundRobinPicker.class)
1✔
525
          .add("enableOobLoadReport", enableOobLoadReport)
1✔
526
          .add("errorUtilizationPenalty", errorUtilizationPenalty)
1✔
527
          .add("pickers", pickers)
1✔
528
          .toString();
1✔
529
    }
530

531
    @VisibleForTesting
532
    List<ChildLbState> getChildren() {
533
      return children;
1✔
534
    }
535

536
    @Override
537
    public int hashCode() {
538
      return hashCode;
×
539
    }
540

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

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

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

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

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

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

651
      this.scaledWeights = scaledWeights;
1✔
652
      this.sequence = sequence;
1✔
653
    }
1✔
654

655
    // Without properly weighted channels, we do plain vanilla round_robin.
656
    boolean usesRoundRobin() {
657
      return usesRoundRobin;
1✔
658
    }
659

660
    /**
661
     * Returns the next sequence number and atomically increases sequence with wraparound.
662
     */
663
    private long nextSequence() {
664
      return Integer.toUnsignedLong(sequence.getAndIncrement());
1✔
665
    }
666

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

712
  static final class WeightedRoundRobinLoadBalancerConfig {
713
    final long blackoutPeriodNanos;
714
    final long weightExpirationPeriodNanos;
715
    final boolean enableOobLoadReport;
716
    final long oobReportingPeriodNanos;
717
    final long weightUpdatePeriodNanos;
718
    final float errorUtilizationPenalty;
719

720
    public static Builder newBuilder() {
721
      return new Builder();
1✔
722
    }
723

724
    private WeightedRoundRobinLoadBalancerConfig(long blackoutPeriodNanos,
725
                                                 long weightExpirationPeriodNanos,
726
                                                 boolean enableOobLoadReport,
727
                                                 long oobReportingPeriodNanos,
728
                                                 long weightUpdatePeriodNanos,
729
                                                 float errorUtilizationPenalty) {
1✔
730
      this.blackoutPeriodNanos = blackoutPeriodNanos;
1✔
731
      this.weightExpirationPeriodNanos = weightExpirationPeriodNanos;
1✔
732
      this.enableOobLoadReport = enableOobLoadReport;
1✔
733
      this.oobReportingPeriodNanos = oobReportingPeriodNanos;
1✔
734
      this.weightUpdatePeriodNanos = weightUpdatePeriodNanos;
1✔
735
      this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
736
    }
1✔
737

738
    static final class Builder {
739
      long blackoutPeriodNanos = 10_000_000_000L; // 10s
1✔
740
      long weightExpirationPeriodNanos = 180_000_000_000L; //3min
1✔
741
      boolean enableOobLoadReport = false;
1✔
742
      long oobReportingPeriodNanos = 10_000_000_000L; // 10s
1✔
743
      long weightUpdatePeriodNanos = 1_000_000_000L; // 1s
1✔
744
      float errorUtilizationPenalty = 1.0F;
1✔
745

746
      private Builder() {
1✔
747

748
      }
1✔
749

750
      @SuppressWarnings("UnusedReturnValue")
751
      Builder setBlackoutPeriodNanos(long blackoutPeriodNanos) {
752
        this.blackoutPeriodNanos = blackoutPeriodNanos;
1✔
753
        return this;
1✔
754
      }
755

756
      @SuppressWarnings("UnusedReturnValue")
757
      Builder setWeightExpirationPeriodNanos(long weightExpirationPeriodNanos) {
758
        this.weightExpirationPeriodNanos = weightExpirationPeriodNanos;
1✔
759
        return this;
1✔
760
      }
761

762
      Builder setEnableOobLoadReport(boolean enableOobLoadReport) {
763
        this.enableOobLoadReport = enableOobLoadReport;
1✔
764
        return this;
1✔
765
      }
766

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

772
      Builder setWeightUpdatePeriodNanos(long weightUpdatePeriodNanos) {
773
        this.weightUpdatePeriodNanos = weightUpdatePeriodNanos;
1✔
774
        return this;
1✔
775
      }
776

777
      Builder setErrorUtilizationPenalty(float errorUtilizationPenalty) {
778
        this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
779
        return this;
1✔
780
      }
781

782
      WeightedRoundRobinLoadBalancerConfig build() {
783
        return new WeightedRoundRobinLoadBalancerConfig(blackoutPeriodNanos,
1✔
784
                weightExpirationPeriodNanos, enableOobLoadReport, oobReportingPeriodNanos,
785
                weightUpdatePeriodNanos, errorUtilizationPenalty);
786
      }
787
    }
788
  }
789
}
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