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

grpc / grpc-java / #19415

12 Aug 2024 06:19PM UTC coverage: 84.471% (+0.002%) from 84.469%
#19415

push

github

ejona86
xds: Remove useless ExperimentalApi for WRR

A package-private class isn't visible and `@Internal` is stronger than
experimental. The only way users should use WRR is via the
weight_round_robin string, and that's already not suffixed with
_experimental.

Closes #9885

33389 of 39527 relevant lines covered (84.47%)

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.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.Collection;
48
import java.util.HashMap;
49
import java.util.HashSet;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.Random;
53
import java.util.Set;
54
import java.util.concurrent.ScheduledExecutorService;
55
import java.util.concurrent.TimeUnit;
56
import java.util.concurrent.atomic.AtomicInteger;
57
import java.util.logging.Level;
58
import java.util.logging.Logger;
59

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

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

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

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

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

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

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

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

190
      createAndApplyOrcaListeners();
1✔
191

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

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

201
    return acceptRetVal.status;
1✔
202
  }
203

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

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

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

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

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

249
  @VisibleForTesting
250
  final class WeightedChildLbState extends ChildLbState {
251

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

257
    private OrcaReportListener orcaReportListener;
258

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

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

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

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

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

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

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

311
    final class OrcaReportListener implements OrcaPerRequestReportListener, OrcaOobReportListener {
312
      private final float errorUtilizationPenalty;
313

314
      OrcaReportListener(float errorUtilizationPenalty) {
1✔
315
        this.errorUtilizationPenalty = errorUtilizationPenalty;
1✔
316
      }
1✔
317

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

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

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

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

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

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

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

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

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

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

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

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

457
      updateWeight();
1✔
458
    }
1✔
459

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

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

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

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

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

529
    @Override
530
    public int hashCode() {
531
      return hashCode;
×
532
    }
533

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

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

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

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

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

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

644
      this.scaledWeights = scaledWeights;
1✔
645
      this.sequence = sequence;
1✔
646
    }
1✔
647

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

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

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

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

713
    public static Builder newBuilder() {
714
      return new Builder();
1✔
715
    }
716

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

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

739
      private Builder() {
1✔
740

741
      }
1✔
742

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

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

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

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

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

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

775
      WeightedRoundRobinLoadBalancerConfig build() {
776
        return new WeightedRoundRobinLoadBalancerConfig(blackoutPeriodNanos,
1✔
777
                weightExpirationPeriodNanos, enableOobLoadReport, oobReportingPeriodNanos,
778
                weightUpdatePeriodNanos, errorUtilizationPenalty);
779
      }
780
    }
781
  }
782
}
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