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

grpc / grpc-java / #19116

22 Mar 2024 09:40PM UTC coverage: 88.289% (-0.003%) from 88.292%
#19116

push

github

web-flow
util: Status desc for outlier detection ejection (#11036)

Including a Status description makes it easier to debug subchannel
closure issues if it's clear that a subchannel became unavailable because
of an outlier detection ejection.

31211 of 35351 relevant lines covered (88.29%)

0.88 hits per line

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

96.93
/../util/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java
1
/*
2
 * Copyright 2022 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.util;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static com.google.common.base.Preconditions.checkNotNull;
21
import static com.google.common.base.Preconditions.checkState;
22
import static java.util.concurrent.TimeUnit.NANOSECONDS;
23

24
import com.google.common.annotations.VisibleForTesting;
25
import com.google.common.collect.ForwardingMap;
26
import com.google.common.collect.ImmutableList;
27
import com.google.common.collect.ImmutableSet;
28
import io.grpc.Attributes;
29
import io.grpc.ChannelLogger;
30
import io.grpc.ChannelLogger.ChannelLogLevel;
31
import io.grpc.ClientStreamTracer;
32
import io.grpc.ClientStreamTracer.StreamInfo;
33
import io.grpc.ConnectivityState;
34
import io.grpc.ConnectivityStateInfo;
35
import io.grpc.EquivalentAddressGroup;
36
import io.grpc.Internal;
37
import io.grpc.LoadBalancer;
38
import io.grpc.Metadata;
39
import io.grpc.Status;
40
import io.grpc.SynchronizationContext;
41
import io.grpc.SynchronizationContext.ScheduledHandle;
42
import io.grpc.internal.ServiceConfigUtil.PolicySelection;
43
import io.grpc.internal.TimeProvider;
44
import java.net.SocketAddress;
45
import java.util.ArrayList;
46
import java.util.Collection;
47
import java.util.HashMap;
48
import java.util.HashSet;
49
import java.util.List;
50
import java.util.Map;
51
import java.util.Random;
52
import java.util.Set;
53
import java.util.concurrent.ScheduledExecutorService;
54
import java.util.concurrent.atomic.AtomicLong;
55
import javax.annotation.Nullable;
56

57
/**
58
 * Wraps a child {@code LoadBalancer} while monitoring for outlier backends and removing them from
59
 * the use of the child LB.
60
 *
61
 * <p>This implements the outlier detection gRFC:
62
 * https://github.com/grpc/proposal/blob/master/A50-xds-outlier-detection.md
63
 *
64
 * <p>The implementation maintains two maps. Each endpoint status is tracked using an
65
 * EndpointTracker. E.g. for two endpoints with these address list and their tracker:
66
 * Endpoint e1 : [a1, a2] is tracked with EndpointTracker t1
67
 * Endpoint e2 : [a3] is tracked with EndpointTracker t2
68
 * The two maps are:
69
 * First, addressMap maps from socket address -> endpoint tracker : [a1 -> t1, a2 -> t1, a3 -> t2]
70
 * EndpointTracker has reference to all the subchannels of the corresponding endpoint.
71
 * Second, trackerMap maps from unordered address set -> endpoint tracker.
72
 * Updated upon address updates.
73
 */
74
@Internal
75
public final class OutlierDetectionLoadBalancer extends LoadBalancer {
76

77
  @VisibleForTesting
78
  final EndpointTrackerMap endpointTrackerMap;
79

80
  @VisibleForTesting
1✔
81
  final Map<SocketAddress, EndpointTracker> addressMap = new HashMap<>();
82

83
  private final SynchronizationContext syncContext;
84
  private final Helper childHelper;
85
  private final GracefulSwitchLoadBalancer switchLb;
86
  private TimeProvider timeProvider;
87
  private final ScheduledExecutorService timeService;
88
  private ScheduledHandle detectionTimerHandle;
89
  private Long detectionTimerStartNanos;
90

91
  private final ChannelLogger logger;
92

93
  private static final Attributes.Key<EndpointTracker> ENDPOINT_TRACKER_KEY
1✔
94
      = Attributes.Key.create("endpointTrackerKey");
1✔
95

96
  /**
97
   * Creates a new instance of {@link OutlierDetectionLoadBalancer}.
98
   */
99
  public OutlierDetectionLoadBalancer(Helper helper, TimeProvider timeProvider) {
1✔
100
    logger = helper.getChannelLogger();
1✔
101
    childHelper = new ChildHelper(checkNotNull(helper, "helper"));
1✔
102
    switchLb = new GracefulSwitchLoadBalancer(childHelper);
1✔
103
    endpointTrackerMap = new EndpointTrackerMap();
1✔
104
    this.syncContext = checkNotNull(helper.getSynchronizationContext(), "syncContext");
1✔
105
    this.timeService = checkNotNull(helper.getScheduledExecutorService(), "timeService");
1✔
106
    this.timeProvider = timeProvider;
1✔
107
    logger.log(ChannelLogLevel.DEBUG, "OutlierDetection lb created.");
1✔
108
  }
1✔
109

110
  @Override
111
  public Status acceptResolvedAddresses(ResolvedAddresses resolvedAddresses) {
112
    logger.log(ChannelLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
1✔
113
    OutlierDetectionLoadBalancerConfig config
1✔
114
        = (OutlierDetectionLoadBalancerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
1✔
115

116
    // The map should only retain entries for endpoints in this latest update.
117
    Set<Set<SocketAddress>> endpoints = new HashSet<>();
1✔
118
    Map<SocketAddress, Set<SocketAddress>> addressEndpointMap = new HashMap<>();
1✔
119
    for (EquivalentAddressGroup addressGroup : resolvedAddresses.getAddresses()) {
1✔
120
      Set<SocketAddress> endpoint = ImmutableSet.copyOf(addressGroup.getAddresses());
1✔
121
      endpoints.add(endpoint);
1✔
122
      for (SocketAddress address : addressGroup.getAddresses()) {
1✔
123
        if (addressEndpointMap.containsKey(address)) {
1✔
124
          logger.log(ChannelLogLevel.WARNING,
×
125
              "Unexpected duplicated address {0} belongs to multiple endpoints", address);
126
        }
127
        addressEndpointMap.put(address, endpoint);
1✔
128
      }
1✔
129
    }
1✔
130
    endpointTrackerMap.keySet().retainAll(endpoints);
1✔
131

132
    endpointTrackerMap.updateTrackerConfigs(config);
1✔
133

134
    // Add any new ones.
135
    endpointTrackerMap.putNewTrackers(config, endpoints);
1✔
136

137
    // Update address -> tracker map.
138
    addressMap.clear();
1✔
139
    for (Map.Entry<SocketAddress, Set<SocketAddress>> e : addressEndpointMap.entrySet()) {
1✔
140
      addressMap.put(e.getKey(), endpointTrackerMap.get(e.getValue()));
1✔
141
    }
1✔
142

143
    switchLb.switchTo(config.childPolicy.getProvider());
1✔
144

145
    // If outlier detection is actually configured, start a timer that will periodically try to
146
    // detect outliers.
147
    if (config.outlierDetectionEnabled()) {
1✔
148
      Long initialDelayNanos;
149

150
      if (detectionTimerStartNanos == null) {
1✔
151
        // On the first go we use the configured interval.
152
        initialDelayNanos = config.intervalNanos;
1✔
153
      } else {
154
        // If a timer has started earlier we cancel it and use the difference between the start
155
        // time and now as the interval.
156
        initialDelayNanos = Math.max(0L,
1✔
157
            config.intervalNanos - (timeProvider.currentTimeNanos() - detectionTimerStartNanos));
1✔
158
      }
159

160
      // If a timer has been previously created we need to cancel it and reset all the call counters
161
      // for a fresh start.
162
      if (detectionTimerHandle != null) {
1✔
163
        detectionTimerHandle.cancel();
1✔
164
        endpointTrackerMap.resetCallCounters();
1✔
165
      }
166

167
      detectionTimerHandle = syncContext.scheduleWithFixedDelay(new DetectionTimer(config, logger),
1✔
168
          initialDelayNanos, config.intervalNanos, NANOSECONDS, timeService);
1✔
169
    } else if (detectionTimerHandle != null) {
1✔
170
      // Outlier detection is not configured, but we have a lingering timer. Let's cancel it and
171
      // uneject any addresses we may have ejected.
172
      detectionTimerHandle.cancel();
1✔
173
      detectionTimerStartNanos = null;
1✔
174
      endpointTrackerMap.cancelTracking();
1✔
175
    }
176

177
    switchLb.handleResolvedAddresses(
1✔
178
        resolvedAddresses.toBuilder().setLoadBalancingPolicyConfig(config.childPolicy.getConfig())
1✔
179
            .build());
1✔
180
    return Status.OK;
1✔
181
  }
182

183
  @Override
184
  public void handleNameResolutionError(Status error) {
185
    switchLb.handleNameResolutionError(error);
1✔
186
  }
1✔
187

188
  @Override
189
  public void shutdown() {
190
    switchLb.shutdown();
1✔
191
  }
1✔
192

193
  /**
194
   * This timer will be invoked periodically, according to configuration, and it will look for any
195
   * outlier subchannels.
196
   */
197
  class DetectionTimer implements Runnable {
198

199
    OutlierDetectionLoadBalancerConfig config;
200
    ChannelLogger logger;
201

202
    DetectionTimer(OutlierDetectionLoadBalancerConfig config, ChannelLogger logger) {
1✔
203
      this.config = config;
1✔
204
      this.logger = logger;
1✔
205
    }
1✔
206

207
    @Override
208
    public void run() {
209
      detectionTimerStartNanos = timeProvider.currentTimeNanos();
1✔
210

211
      endpointTrackerMap.swapCounters();
1✔
212

213
      for (OutlierEjectionAlgorithm algo : OutlierEjectionAlgorithm.forConfig(config, logger)) {
1✔
214
        algo.ejectOutliers(endpointTrackerMap, detectionTimerStartNanos);
1✔
215
      }
1✔
216

217
      endpointTrackerMap.maybeUnejectOutliers(detectionTimerStartNanos);
1✔
218
    }
1✔
219
  }
220

221
  /**
222
   * This child helper wraps the provided helper so that it can hand out wrapped {@link
223
   * OutlierDetectionSubchannel}s and manage the address info map.
224
   */
225
  class ChildHelper extends ForwardingLoadBalancerHelper {
226

227
    private Helper delegate;
228

229
    ChildHelper(Helper delegate) {
1✔
230
      this.delegate = new HealthProducerHelper(delegate);
1✔
231
    }
1✔
232

233
    @Override
234
    protected Helper delegate() {
235
      return delegate;
1✔
236
    }
237

238
    @Override
239
    public Subchannel createSubchannel(CreateSubchannelArgs args) {
240
      // Subchannels are wrapped so that we can monitor call results and to trigger failures when
241
      // we decide to eject the subchannel.
242
      OutlierDetectionSubchannel subchannel = new OutlierDetectionSubchannel(args, delegate);
1✔
243

244
      // If the subchannel is associated with a single address that is also already in the map
245
      // the subchannel will be added to the map and be included in outlier detection.
246
      List<EquivalentAddressGroup> addressGroups = args.getAddresses();
1✔
247
      if (hasSingleAddress(addressGroups)
1✔
248
          && addressMap.containsKey(addressGroups.get(0).getAddresses().get(0))) {
1✔
249
        EndpointTracker tracker = addressMap.get(addressGroups.get(0).getAddresses().get(0));
1✔
250
        tracker.addSubchannel(subchannel);
1✔
251

252
        // If this address has already been ejected, we need to immediately eject this Subchannel.
253
        if (tracker.ejectionTimeNanos != null) {
1✔
254
          subchannel.eject();
×
255
        }
256
      }
257

258
      return subchannel;
1✔
259
    }
260

261
    @Override
262
    public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
263
      delegate.updateBalancingState(newState, new OutlierDetectionPicker(newPicker));
1✔
264
    }
1✔
265
  }
266

267
  class OutlierDetectionSubchannel extends ForwardingSubchannel {
268

269
    private final Subchannel delegate;
270
    private EndpointTracker endpointTracker;
271
    private boolean ejected;
272
    private ConnectivityStateInfo lastSubchannelState;
273

274
    // In the new pick first: created at construction, delegates to health consumer listener;
275
    // In th old pick first: created at subchannel.start(), delegates to subchannel state listener.
276
    private SubchannelStateListener subchannelStateListener;
277
    private final ChannelLogger logger;
278

279
    OutlierDetectionSubchannel(CreateSubchannelArgs args, Helper helper) {
1✔
280
      LoadBalancer.SubchannelStateListener healthConsumerListener =
1✔
281
          args.getOption(HEALTH_CONSUMER_LISTENER_ARG_KEY);
1✔
282
      if (healthConsumerListener != null) {
1✔
283
        this.subchannelStateListener = healthConsumerListener;
1✔
284
        SubchannelStateListener upstreamListener =
1✔
285
            new OutlierDetectionSubchannelStateListener(healthConsumerListener);
286
        this.delegate = helper.createSubchannel(args.toBuilder()
1✔
287
             .addOption(HEALTH_CONSUMER_LISTENER_ARG_KEY, upstreamListener).build());
1✔
288
      } else {
1✔
289
        this.delegate = helper.createSubchannel(args);
1✔
290
      }
291
      this.logger = delegate.getChannelLogger();
1✔
292
    }
1✔
293

294
    @Override
295
    public void start(SubchannelStateListener listener) {
296
      if (subchannelStateListener != null) {
1✔
297
        super.start(listener);
1✔
298
      } else {
299
        subchannelStateListener = listener;
1✔
300
        super.start(new OutlierDetectionSubchannelStateListener(listener));
1✔
301
      }
302
    }
1✔
303

304
    @Override
305
    public void shutdown() {
306
      if (endpointTracker != null) {
1✔
307
        endpointTracker.removeSubchannel(this);
1✔
308
      }
309
      super.shutdown();
1✔
310
    }
1✔
311

312
    @Override
313
    public Attributes getAttributes() {
314
      if (endpointTracker != null) {
1✔
315
        return delegate.getAttributes().toBuilder().set(ENDPOINT_TRACKER_KEY, endpointTracker)
1✔
316
            .build();
1✔
317
      } else {
318
        return delegate.getAttributes();
×
319
      }
320
    }
321

322
    @Override
323
    public void updateAddresses(List<EquivalentAddressGroup> addressGroups) {
324
      // Outlier detection only supports subchannels with a single address, but the list of
325
      // addressGroups associated with a subchannel can change at any time, so we need to react to
326
      // changes in the address list plurality.
327

328
      // No change in address plurality, we replace the single one with a new one.
329
      if (hasSingleAddress(getAllAddresses()) && hasSingleAddress(addressGroups)) {
1✔
330
        // Remove the current subchannel from the old address it is associated with in the map.
331
        if (endpointTrackerMap.containsValue(endpointTracker)) {
1✔
332
          endpointTracker.removeSubchannel(this);
1✔
333
        }
334

335
        // If the map has an entry for the new address, we associate this subchannel with it.
336
        SocketAddress address = addressGroups.get(0).getAddresses().get(0);
1✔
337
        if (addressMap.containsKey(address)) {
1✔
338
          addressMap.get(address).addSubchannel(this);
1✔
339
        }
340
      } else if (hasSingleAddress(getAllAddresses()) && !hasSingleAddress(addressGroups)) {
1✔
341
        // We go from a single address to having multiple, making this subchannel uneligible for
342
        // outlier detection. Remove it from all trackers and reset the call counters of all the
343
        // associated trackers.
344
        // Remove the current subchannel from the old address it is associated with in the map.
345
        if (addressMap.containsKey(getAddresses().getAddresses().get(0))) {
1✔
346
          EndpointTracker tracker = addressMap.get(getAddresses().getAddresses().get(0));
1✔
347
          tracker.removeSubchannel(this);
1✔
348
          tracker.resetCallCounters();
1✔
349
        }
1✔
350
      } else if (!hasSingleAddress(getAllAddresses()) && hasSingleAddress(addressGroups)) {
1✔
351
        // We go from, previously uneligble, multiple address mode to a single address. If the map
352
        // has an entry for the new address, we associate this subchannel with it.
353
        SocketAddress address = addressGroups.get(0).getAddresses().get(0);
1✔
354
        if (addressMap.containsKey(address)) {
1✔
355
          EndpointTracker tracker = addressMap.get(address);
1✔
356
          tracker.addSubchannel(this);
1✔
357
        }
358
      }
359

360
      // We could also have multiple addressGroups and get an update for multiple new ones. This is
361
      // a no-op as we will just continue to ignore multiple address subchannels.
362

363
      delegate.updateAddresses(addressGroups);
1✔
364
    }
1✔
365

366
    /**
367
     * If the {@link Subchannel} is considered for outlier detection the associated {@link
368
     * EndpointTracker} should be set.
369
     */
370
    void setEndpointTracker(EndpointTracker endpointTracker) {
371
      this.endpointTracker = endpointTracker;
1✔
372
    }
1✔
373

374
    void clearEndpointTracker() {
375
      this.endpointTracker = null;
1✔
376
    }
1✔
377

378
    void eject() {
379
      ejected = true;
1✔
380
      subchannelStateListener.onSubchannelState(ConnectivityStateInfo.forTransientFailure(
1✔
381
          Status.UNAVAILABLE.withDescription(
1✔
382
              "The subchannel has been ejected by outlier detection")));
383
      logger.log(ChannelLogLevel.INFO, "Subchannel ejected: {0}", this);
1✔
384
    }
1✔
385

386
    void uneject() {
387
      ejected = false;
1✔
388
      if (lastSubchannelState != null) {
1✔
389
        subchannelStateListener.onSubchannelState(lastSubchannelState);
1✔
390
        logger.log(ChannelLogLevel.INFO, "Subchannel unejected: {0}", this);
1✔
391
      }
392
    }
1✔
393

394
    boolean isEjected() {
395
      return ejected;
1✔
396
    }
397

398
    @Override
399
    protected Subchannel delegate() {
400
      return delegate;
1✔
401
    }
402

403
    /**
404
     * Wraps the actual listener so that state changes from the actual one can be intercepted.
405
     */
406
    class OutlierDetectionSubchannelStateListener implements SubchannelStateListener {
407

408
      private final SubchannelStateListener delegate;
409

410
      OutlierDetectionSubchannelStateListener(SubchannelStateListener delegate) {
1✔
411
        this.delegate = delegate;
1✔
412
      }
1✔
413

414
      @Override
415
      public void onSubchannelState(ConnectivityStateInfo newState) {
416
        lastSubchannelState = newState;
1✔
417
        if (!ejected) {
1✔
418
          delegate.onSubchannelState(newState);
1✔
419
        }
420
      }
1✔
421
    }
422

423
    @Override
424
    public String toString() {
425
      return "OutlierDetectionSubchannel{"
×
426
              + "addresses=" + delegate.getAllAddresses()
×
427
              + '}';
428
    }
429
  }
430

431

432
  /**
433
   * This picker delegates the actual picking logic to a wrapped delegate, but associates a {@link
434
   * ClientStreamTracer} with each pick to track the results of each subchannel stream.
435
   */
436
  class OutlierDetectionPicker extends SubchannelPicker {
437

438
    private final SubchannelPicker delegate;
439

440
    OutlierDetectionPicker(SubchannelPicker delegate) {
1✔
441
      this.delegate = delegate;
1✔
442
    }
1✔
443

444
    @Override
445
    public PickResult pickSubchannel(PickSubchannelArgs args) {
446
      PickResult pickResult = delegate.pickSubchannel(args);
1✔
447

448
      Subchannel subchannel = pickResult.getSubchannel();
1✔
449
      if (subchannel != null) {
1✔
450
        return PickResult.withSubchannel(subchannel, new ResultCountingClientStreamTracerFactory(
1✔
451
            subchannel.getAttributes().get(ENDPOINT_TRACKER_KEY),
1✔
452
            pickResult.getStreamTracerFactory()));
1✔
453
      }
454

455
      return pickResult;
×
456
    }
457

458
    /**
459
     * Builds instances of a {@link ClientStreamTracer} that increments the call count in the
460
     * tracker for each closed stream.
461
     */
462
    class ResultCountingClientStreamTracerFactory extends ClientStreamTracer.Factory {
463

464
      private final EndpointTracker tracker;
465

466
      @Nullable
467
      private final ClientStreamTracer.Factory delegateFactory;
468

469
      ResultCountingClientStreamTracerFactory(EndpointTracker tracker,
470
          @Nullable ClientStreamTracer.Factory delegateFactory) {
1✔
471
        this.tracker = tracker;
1✔
472
        this.delegateFactory = delegateFactory;
1✔
473
      }
1✔
474

475
      @Override
476
      public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
477
        if (delegateFactory != null) {
1✔
478
          ClientStreamTracer delegateTracer = delegateFactory.newClientStreamTracer(info, headers);
1✔
479
          return new ForwardingClientStreamTracer() {
1✔
480
            @Override
481
            protected ClientStreamTracer delegate() {
482
              return delegateTracer;
1✔
483
            }
484

485
            @Override
486
            public void streamClosed(Status status) {
487
              tracker.incrementCallCount(status.isOk());
1✔
488
              delegate().streamClosed(status);
1✔
489
            }
1✔
490
          };
491
        } else {
492
          return new ClientStreamTracer() {
1✔
493
            @Override
494
            public void streamClosed(Status status) {
495
              tracker.incrementCallCount(status.isOk());
1✔
496
            }
1✔
497
          };
498
        }
499
      }
500
    }
501
  }
502

503
  /**
504
   * Tracks additional information about the endpoint needed for outlier detection.
505
   */
506
  static class EndpointTracker {
507

508
    private OutlierDetectionLoadBalancerConfig config;
509
    // Marked as volatile to assure that when the inactive counter is swapped in as the new active
510
    // one, all threads see the change and don't hold on to a reference to the now inactive counter.
511
    private volatile CallCounter activeCallCounter = new CallCounter();
1✔
512
    private CallCounter inactiveCallCounter = new CallCounter();
1✔
513
    private Long ejectionTimeNanos;
514
    private int ejectionTimeMultiplier;
515
    private final Set<OutlierDetectionSubchannel> subchannels = new HashSet<>();
1✔
516

517
    EndpointTracker(OutlierDetectionLoadBalancerConfig config) {
1✔
518
      this.config = config;
1✔
519
    }
1✔
520

521
    void setConfig(OutlierDetectionLoadBalancerConfig config) {
522
      this.config = config;
1✔
523
    }
1✔
524

525
    /**
526
     * Adds a subchannel to the tracker, while assuring that the subchannel ejection status is
527
     * updated to match the tracker's if needed.
528
     */
529
    boolean addSubchannel(OutlierDetectionSubchannel subchannel) {
530
      // Make sure that the subchannel is in the same ejection state as the new tracker it is
531
      // associated with.
532
      if (subchannelsEjected() && !subchannel.isEjected()) {
1✔
533
        subchannel.eject();
×
534
      } else if (!subchannelsEjected() && subchannel.isEjected()) {
1✔
535
        subchannel.uneject();
1✔
536
      }
537
      subchannel.setEndpointTracker(this);
1✔
538
      return subchannels.add(subchannel);
1✔
539
    }
540

541
    boolean removeSubchannel(OutlierDetectionSubchannel subchannel) {
542
      subchannel.clearEndpointTracker();
1✔
543
      return subchannels.remove(subchannel);
1✔
544
    }
545

546
    boolean containsSubchannel(OutlierDetectionSubchannel subchannel) {
547
      return subchannels.contains(subchannel);
×
548
    }
549

550
    @VisibleForTesting
551
    Set<OutlierDetectionSubchannel> getSubchannels() {
552
      return ImmutableSet.copyOf(subchannels);
1✔
553
    }
554

555
    void incrementCallCount(boolean success) {
556
      // If neither algorithm is configured, no point in incrementing counters.
557
      if (config.successRateEjection == null && config.failurePercentageEjection == null) {
1✔
558
        return;
×
559
      }
560

561
      if (success) {
1✔
562
        activeCallCounter.successCount.getAndIncrement();
1✔
563
      } else {
564
        activeCallCounter.failureCount.getAndIncrement();
1✔
565
      }
566
    }
1✔
567

568
    @VisibleForTesting
569
    long activeVolume() {
570
      return activeCallCounter.successCount.get() + activeCallCounter.failureCount.get();
1✔
571
    }
572

573
    long inactiveVolume() {
574
      return inactiveCallCounter.successCount.get() + inactiveCallCounter.failureCount.get();
1✔
575
    }
576

577
    double successRate() {
578
      return ((double) inactiveCallCounter.successCount.get()) / inactiveVolume();
1✔
579
    }
580

581
    double failureRate() {
582
      return ((double)inactiveCallCounter.failureCount.get()) / inactiveVolume();
1✔
583
    }
584

585
    void resetCallCounters() {
586
      activeCallCounter.reset();
1✔
587
      inactiveCallCounter.reset();
1✔
588
    }
1✔
589

590
    void decrementEjectionTimeMultiplier() {
591
      // The multiplier should not go negative.
592
      ejectionTimeMultiplier = ejectionTimeMultiplier == 0 ? 0 : ejectionTimeMultiplier - 1;
1✔
593
    }
1✔
594

595
    void resetEjectionTimeMultiplier() {
596
      ejectionTimeMultiplier = 0;
1✔
597
    }
1✔
598

599
    /**
600
     * Swaps the active and inactive counters.
601
     *
602
     * <p>Note that this method is not thread safe as the swap is not done atomically. This is
603
     * expected to only be called from the timer that is scheduled at a fixed delay, assuring that
604
     * only one timer is active at a time.
605
     */
606
    void swapCounters() {
607
      inactiveCallCounter.reset();
1✔
608
      CallCounter tempCounter = activeCallCounter;
1✔
609
      activeCallCounter = inactiveCallCounter;
1✔
610
      inactiveCallCounter = tempCounter;
1✔
611
    }
1✔
612

613
    void ejectSubchannels(long ejectionTimeNanos) {
614
      this.ejectionTimeNanos = ejectionTimeNanos;
1✔
615
      ejectionTimeMultiplier++;
1✔
616
      for (OutlierDetectionSubchannel subchannel : subchannels) {
1✔
617
        subchannel.eject();
1✔
618
      }
1✔
619
    }
1✔
620

621
    /**
622
     * Uneject a currently ejected address.
623
     */
624
    void unejectSubchannels() {
625
      checkState(ejectionTimeNanos != null, "not currently ejected");
1✔
626
      ejectionTimeNanos = null;
1✔
627
      for (OutlierDetectionSubchannel subchannel : subchannels) {
1✔
628
        subchannel.uneject();
1✔
629
      }
1✔
630
    }
1✔
631

632
    boolean subchannelsEjected() {
633
      return ejectionTimeNanos != null;
1✔
634
    }
635

636
    public boolean maxEjectionTimeElapsed(long currentTimeNanos) {
637
      // The instant in time beyond which the address should no longer be ejected. Also making sure
638
      // we honor any maximum ejection time setting.
639
      long maxEjectionDurationSecs
1✔
640
          = Math.max(config.baseEjectionTimeNanos, config.maxEjectionTimeNanos);
1✔
641
      long maxEjectionTimeNanos =
1✔
642
          ejectionTimeNanos + Math.min(
1✔
643
              config.baseEjectionTimeNanos * ejectionTimeMultiplier,
1✔
644
              maxEjectionDurationSecs);
645

646
      return currentTimeNanos > maxEjectionTimeNanos;
1✔
647
    }
648

649
    /** Tracks both successful and failed call counts. */
650
    private static class CallCounter {
1✔
651
      AtomicLong successCount = new AtomicLong();
1✔
652
      AtomicLong failureCount = new AtomicLong();
1✔
653

654
      void reset() {
655
        successCount.set(0);
1✔
656
        failureCount.set(0);
1✔
657
      }
1✔
658
    }
659

660
    @Override
661
    public String toString() {
662
      return "EndpointTracker{"
×
663
              + "subchannels=" + subchannels
664
              + '}';
665
    }
666
  }
667

668
  /**
669
   * Maintains a mapping from endpoint (a set of addresses) to their trackers.
670
   */
671
  static class EndpointTrackerMap extends ForwardingMap<Set<SocketAddress>, EndpointTracker> {
672
    private final Map<Set<SocketAddress>, EndpointTracker> trackerMap;
673

674
    EndpointTrackerMap() {
1✔
675
      trackerMap = new HashMap<>();
1✔
676
    }
1✔
677

678
    @Override
679
    protected Map<Set<SocketAddress>, EndpointTracker> delegate() {
680
      return trackerMap;
1✔
681
    }
682

683
    void updateTrackerConfigs(OutlierDetectionLoadBalancerConfig config) {
684
      for (EndpointTracker tracker: trackerMap.values()) {
1✔
685
        tracker.setConfig(config);
1✔
686
      }
1✔
687
    }
1✔
688

689
    /** Adds a new tracker for every given address. */
690
    void putNewTrackers(OutlierDetectionLoadBalancerConfig config,
691
        Set<Set<SocketAddress>> endpoints) {
692
      endpoints.forEach(e -> trackerMap.putIfAbsent(e, new EndpointTracker(config)));
1✔
693
    }
1✔
694

695
    /** Resets the call counters for all the trackers in the map. */
696
    void resetCallCounters() {
697
      for (EndpointTracker tracker : trackerMap.values()) {
1✔
698
        tracker.resetCallCounters();
1✔
699
      }
1✔
700
    }
1✔
701

702
    /**
703
     * When OD gets disabled we need to uneject any subchannels that may have been ejected and
704
     * to reset the ejection time multiplier.
705
     */
706
    void cancelTracking() {
707
      for (EndpointTracker tracker : trackerMap.values()) {
1✔
708
        if (tracker.subchannelsEjected()) {
1✔
709
          tracker.unejectSubchannels();
×
710
        }
711
        tracker.resetEjectionTimeMultiplier();
1✔
712
      }
1✔
713
    }
1✔
714

715
    /** Swaps the active and inactive counters for each tracker. */
716
    void swapCounters() {
717
      for (EndpointTracker tracker : trackerMap.values()) {
1✔
718
        tracker.swapCounters();
1✔
719
      }
1✔
720
    }
1✔
721

722
    /**
723
     * At the end of a timer run we need to decrement the ejection time multiplier for trackers
724
     * that don't have ejected subchannels and uneject ones that have spent the maximum ejection
725
     * time allowed.
726
     */
727
    void maybeUnejectOutliers(Long detectionTimerStartNanos) {
728
      for (EndpointTracker tracker : trackerMap.values()) {
1✔
729
        if (!tracker.subchannelsEjected()) {
1✔
730
          tracker.decrementEjectionTimeMultiplier();
1✔
731
        }
732

733
        if (tracker.subchannelsEjected() && tracker.maxEjectionTimeElapsed(
1✔
734
            detectionTimerStartNanos)) {
1✔
735
          tracker.unejectSubchannels();
1✔
736
        }
737
      }
1✔
738
    }
1✔
739

740
    /**
741
     * How many percent of the addresses have been ejected.
742
     */
743
    double ejectionPercentage() {
744
      if (trackerMap.isEmpty()) {
1✔
745
        return 0;
×
746
      }
747
      int totalEndpoints = 0;
1✔
748
      int ejectedEndpoints = 0;
1✔
749
      for (EndpointTracker tracker : trackerMap.values()) {
1✔
750
        totalEndpoints++;
1✔
751
        if (tracker.subchannelsEjected()) {
1✔
752
          ejectedEndpoints++;
1✔
753
        }
754
      }
1✔
755
      return ((double)ejectedEndpoints / totalEndpoints) * 100;
1✔
756
    }
757
  }
758

759

760
  /**
761
   * Implementations provide different ways of ejecting outlier addresses..
762
   */
763
  interface OutlierEjectionAlgorithm {
764

765
    /** Eject any outlier addresses. */
766
    void ejectOutliers(EndpointTrackerMap trackerMap, long ejectionTimeNanos);
767

768
    /** Builds a list of algorithms that are enabled in the given config. */
769
    @Nullable
770
    static List<OutlierEjectionAlgorithm> forConfig(OutlierDetectionLoadBalancerConfig config,
771
                                                    ChannelLogger logger) {
772
      ImmutableList.Builder<OutlierEjectionAlgorithm> algoListBuilder = ImmutableList.builder();
1✔
773
      if (config.successRateEjection != null) {
1✔
774
        algoListBuilder.add(new SuccessRateOutlierEjectionAlgorithm(config, logger));
1✔
775
      }
776
      if (config.failurePercentageEjection != null) {
1✔
777
        algoListBuilder.add(new FailurePercentageOutlierEjectionAlgorithm(config, logger));
1✔
778
      }
779
      return algoListBuilder.build();
1✔
780
    }
781
  }
782

783
  /**
784
   * This algorithm ejects addresses that don't maintain a required rate of successful calls. The
785
   * required rate is not fixed, but is based on the mean and standard deviation of the success
786
   * rates of all of the addresses.
787
   */
788
  static class SuccessRateOutlierEjectionAlgorithm implements OutlierEjectionAlgorithm {
789

790
    private final OutlierDetectionLoadBalancerConfig config;
791

792
    private final ChannelLogger logger;
793

794
    SuccessRateOutlierEjectionAlgorithm(OutlierDetectionLoadBalancerConfig config,
795
                                        ChannelLogger logger) {
1✔
796
      checkArgument(config.successRateEjection != null, "success rate ejection config is null");
1✔
797
      this.config = config;
1✔
798
      this.logger = logger;
1✔
799
    }
1✔
800

801
    @Override
802
    public void ejectOutliers(EndpointTrackerMap trackerMap, long ejectionTimeNanos) {
803

804
      // Only consider addresses that have the minimum request volume specified in the config.
805
      List<EndpointTracker> trackersWithVolume = trackersWithVolume(trackerMap,
1✔
806
          config.successRateEjection.requestVolume);
1✔
807
      // If we don't have enough endpoints with significant volume then there's nothing to do.
808
      if (trackersWithVolume.size() < config.successRateEjection.minimumHosts
1✔
809
          || trackersWithVolume.size() == 0) {
1✔
810
        return;
1✔
811
      }
812

813
      // Calculate mean and standard deviation of the fractions of successful calls.
814
      List<Double> successRates = new ArrayList<>();
1✔
815
      for (EndpointTracker tracker : trackersWithVolume) {
1✔
816
        successRates.add(tracker.successRate());
1✔
817
      }
1✔
818
      double mean = mean(successRates);
1✔
819
      double stdev = standardDeviation(successRates, mean);
1✔
820

821
      double requiredSuccessRate =
1✔
822
          mean - stdev * (config.successRateEjection.stdevFactor / 1000f);
1✔
823

824
      for (EndpointTracker tracker : trackersWithVolume) {
1✔
825
        // If we are above or equal to the max ejection percentage, don't eject any more. This will
826
        // allow the total ejections to go one above the max, but at the same time it assures at
827
        // least one ejection, which the spec calls for. This behavior matches what Envoy proxy
828
        // does.
829
        if (trackerMap.ejectionPercentage() >= config.maxEjectionPercent) {
1✔
830
          return;
1✔
831
        }
832

833
        // If success rate is below the threshold, eject the address.
834
        if (tracker.successRate() < requiredSuccessRate) {
1✔
835
          logger.log(ChannelLogLevel.DEBUG,
1✔
836
                  "SuccessRate algorithm detected outlier: {0}. "
837
                          + "Parameters: successRate={1}, mean={2}, stdev={3}, "
838
                          + "requiredSuccessRate={4}",
839
                  tracker, tracker.successRate(),  mean, stdev, requiredSuccessRate);
1✔
840
          // Only eject some endpoints based on the enforcement percentage.
841
          if (new Random().nextInt(100) < config.successRateEjection.enforcementPercentage) {
1✔
842
            tracker.ejectSubchannels(ejectionTimeNanos);
1✔
843
          }
844
        }
845
      }
1✔
846
    }
1✔
847

848
    /** Calculates the mean of the given values. */
849
    @VisibleForTesting
850
    static double mean(Collection<Double> values) {
851
      double totalValue = 0;
1✔
852
      for (double value : values) {
1✔
853
        totalValue += value;
1✔
854
      }
1✔
855

856
      return totalValue / values.size();
1✔
857
    }
858

859
    /** Calculates the standard deviation for the given values and their mean. */
860
    @VisibleForTesting
861
    static double standardDeviation(Collection<Double> values, double mean) {
862
      double squaredDifferenceSum = 0;
1✔
863
      for (double value : values) {
1✔
864
        double difference = value - mean;
1✔
865
        squaredDifferenceSum += difference * difference;
1✔
866
      }
1✔
867
      double variance = squaredDifferenceSum / values.size();
1✔
868

869
      return Math.sqrt(variance);
1✔
870
    }
871
  }
872

873
  static class FailurePercentageOutlierEjectionAlgorithm implements OutlierEjectionAlgorithm {
874

875
    private final OutlierDetectionLoadBalancerConfig config;
876

877
    private final ChannelLogger logger;
878

879
    FailurePercentageOutlierEjectionAlgorithm(OutlierDetectionLoadBalancerConfig config,
880
                                              ChannelLogger logger) {
1✔
881
      this.config = config;
1✔
882
      this.logger = logger;
1✔
883
    }
1✔
884

885
    @Override
886
    public void ejectOutliers(EndpointTrackerMap trackerMap, long ejectionTimeNanos) {
887

888
      // Only consider endpoints that have the minimum request volume specified in the config.
889
      List<EndpointTracker> trackersWithVolume = trackersWithVolume(trackerMap,
1✔
890
          config.failurePercentageEjection.requestVolume);
1✔
891
      // If we don't have enough endpoints with significant volume then there's nothing to do.
892
      if (trackersWithVolume.size() < config.failurePercentageEjection.minimumHosts
1✔
893
          || trackersWithVolume.size() == 0) {
1✔
894
        return;
1✔
895
      }
896

897
      // If this endpoint does not have enough volume to be considered, skip to the next one.
898
      for (EndpointTracker tracker : trackersWithVolume) {
1✔
899
        // If we are above or equal to the max ejection percentage, don't eject any more. This will
900
        // allow the total ejections to go one above the max, but at the same time it assures at
901
        // least one ejection, which the spec calls for. This behavior matches what Envoy proxy
902
        // does.
903
        if (trackerMap.ejectionPercentage() >= config.maxEjectionPercent) {
1✔
904
          return;
×
905
        }
906

907
        if (tracker.inactiveVolume() < config.failurePercentageEjection.requestVolume) {
1✔
908
          continue;
×
909
        }
910

911
        // If the failure rate is above the threshold, we should eject...
912
        double maxFailureRate = ((double)config.failurePercentageEjection.threshold) / 100;
1✔
913
        if (tracker.failureRate() > maxFailureRate) {
1✔
914
          logger.log(ChannelLogLevel.DEBUG,
1✔
915
                  "FailurePercentage algorithm detected outlier: {0}, failureRate={1}",
916
                  tracker, tracker.failureRate());
1✔
917
          // ...but only enforce this based on the enforcement percentage.
918
          if (new Random().nextInt(100) < config.failurePercentageEjection.enforcementPercentage) {
1✔
919
            tracker.ejectSubchannels(ejectionTimeNanos);
1✔
920
          }
921
        }
922
      }
1✔
923
    }
1✔
924
  }
925

926
  /** Returns only the trackers that have the minimum configured volume to be considered. */
927
  private static List<EndpointTracker> trackersWithVolume(EndpointTrackerMap trackerMap,
928
                                                          int volume) {
929
    List<EndpointTracker> trackersWithVolume = new ArrayList<>();
1✔
930
    for (EndpointTracker tracker : trackerMap.values()) {
1✔
931
      if (tracker.inactiveVolume() >= volume) {
1✔
932
        trackersWithVolume.add(tracker);
1✔
933
      }
934
    }
1✔
935
    return trackersWithVolume;
1✔
936
  }
937

938
  /** Counts how many addresses are in a given address group. */
939
  private static boolean hasSingleAddress(List<EquivalentAddressGroup> addressGroups) {
940
    int addressCount = 0;
1✔
941
    for (EquivalentAddressGroup addressGroup : addressGroups) {
1✔
942
      addressCount += addressGroup.getAddresses().size();
1✔
943
      if (addressCount > 1) {
1✔
944
        return false;
1✔
945
      }
946
    }
1✔
947
    return true;
1✔
948
  }
949

950
  /**
951
   * The configuration for {@link OutlierDetectionLoadBalancer}.
952
   */
953
  public static final class OutlierDetectionLoadBalancerConfig {
954

955
    public final Long intervalNanos;
956
    public final Long baseEjectionTimeNanos;
957
    public final Long maxEjectionTimeNanos;
958
    public final Integer maxEjectionPercent;
959
    public final SuccessRateEjection successRateEjection;
960
    public final FailurePercentageEjection failurePercentageEjection;
961
    public final PolicySelection childPolicy;
962

963
    private OutlierDetectionLoadBalancerConfig(Long intervalNanos,
964
        Long baseEjectionTimeNanos,
965
        Long maxEjectionTimeNanos,
966
        Integer maxEjectionPercent,
967
        SuccessRateEjection successRateEjection,
968
        FailurePercentageEjection failurePercentageEjection,
969
        PolicySelection childPolicy) {
1✔
970
      this.intervalNanos = intervalNanos;
1✔
971
      this.baseEjectionTimeNanos = baseEjectionTimeNanos;
1✔
972
      this.maxEjectionTimeNanos = maxEjectionTimeNanos;
1✔
973
      this.maxEjectionPercent = maxEjectionPercent;
1✔
974
      this.successRateEjection = successRateEjection;
1✔
975
      this.failurePercentageEjection = failurePercentageEjection;
1✔
976
      this.childPolicy = childPolicy;
1✔
977
    }
1✔
978

979
    /** Builds a new {@link OutlierDetectionLoadBalancerConfig}. */
980
    public static class Builder {
1✔
981
      Long intervalNanos = 10_000_000_000L; // 10s
1✔
982
      Long baseEjectionTimeNanos = 30_000_000_000L; // 30s
1✔
983
      Long maxEjectionTimeNanos = 300_000_000_000L; // 300s
1✔
984
      Integer maxEjectionPercent = 10;
1✔
985
      SuccessRateEjection successRateEjection;
986
      FailurePercentageEjection failurePercentageEjection;
987
      PolicySelection childPolicy;
988

989
      /** The interval between outlier detection sweeps. */
990
      public Builder setIntervalNanos(Long intervalNanos) {
991
        checkArgument(intervalNanos != null);
1✔
992
        this.intervalNanos = intervalNanos;
1✔
993
        return this;
1✔
994
      }
995

996
      /** The base time an address is ejected for. */
997
      public Builder setBaseEjectionTimeNanos(Long baseEjectionTimeNanos) {
998
        checkArgument(baseEjectionTimeNanos != null);
1✔
999
        this.baseEjectionTimeNanos = baseEjectionTimeNanos;
1✔
1000
        return this;
1✔
1001
      }
1002

1003
      /** The longest time an address can be ejected. */
1004
      public Builder setMaxEjectionTimeNanos(Long maxEjectionTimeNanos) {
1005
        checkArgument(maxEjectionTimeNanos != null);
1✔
1006
        this.maxEjectionTimeNanos = maxEjectionTimeNanos;
1✔
1007
        return this;
1✔
1008
      }
1009

1010
      /** The algorithm agnostic maximum percentage of addresses that can be ejected. */
1011
      public Builder setMaxEjectionPercent(Integer maxEjectionPercent) {
1012
        checkArgument(maxEjectionPercent != null);
1✔
1013
        this.maxEjectionPercent = maxEjectionPercent;
1✔
1014
        return this;
1✔
1015
      }
1016

1017
      /** Set to enable success rate ejection. */
1018
      public Builder setSuccessRateEjection(
1019
          SuccessRateEjection successRateEjection) {
1020
        this.successRateEjection = successRateEjection;
1✔
1021
        return this;
1✔
1022
      }
1023

1024
      /** Set to enable failure percentage ejection. */
1025
      public Builder setFailurePercentageEjection(
1026
          FailurePercentageEjection failurePercentageEjection) {
1027
        this.failurePercentageEjection = failurePercentageEjection;
1✔
1028
        return this;
1✔
1029
      }
1030

1031
      /** Sets the child policy the {@link OutlierDetectionLoadBalancer} delegates to. */
1032
      public Builder setChildPolicy(PolicySelection childPolicy) {
1033
        checkState(childPolicy != null);
1✔
1034
        this.childPolicy = childPolicy;
1✔
1035
        return this;
1✔
1036
      }
1037

1038
      /** Builds a new instance of {@link OutlierDetectionLoadBalancerConfig}. */
1039
      public OutlierDetectionLoadBalancerConfig build() {
1040
        checkState(childPolicy != null);
1✔
1041
        return new OutlierDetectionLoadBalancerConfig(intervalNanos, baseEjectionTimeNanos,
1✔
1042
            maxEjectionTimeNanos, maxEjectionPercent, successRateEjection,
1043
            failurePercentageEjection, childPolicy);
1044
      }
1045
    }
1046

1047
    /** The configuration for success rate ejection. */
1048
    public static class SuccessRateEjection {
1049

1050
      public final Integer stdevFactor;
1051
      public final Integer enforcementPercentage;
1052
      public final Integer minimumHosts;
1053
      public final Integer requestVolume;
1054

1055
      SuccessRateEjection(Integer stdevFactor, Integer enforcementPercentage, Integer minimumHosts,
1056
          Integer requestVolume) {
1✔
1057
        this.stdevFactor = stdevFactor;
1✔
1058
        this.enforcementPercentage = enforcementPercentage;
1✔
1059
        this.minimumHosts = minimumHosts;
1✔
1060
        this.requestVolume = requestVolume;
1✔
1061
      }
1✔
1062

1063
      /** Builds new instances of {@link SuccessRateEjection}. */
1064
      public static final class Builder {
1✔
1065

1066
        Integer stdevFactor = 1900;
1✔
1067
        Integer enforcementPercentage = 100;
1✔
1068
        Integer minimumHosts = 5;
1✔
1069
        Integer requestVolume = 100;
1✔
1070

1071
        /** The product of this and the standard deviation of success rates determine the ejection
1072
         * threshold.
1073
         */
1074
        public Builder setStdevFactor(Integer stdevFactor) {
1075
          checkArgument(stdevFactor != null);
1✔
1076
          this.stdevFactor = stdevFactor;
1✔
1077
          return this;
1✔
1078
        }
1079

1080
        /** Only eject this percentage of outliers. */
1081
        public Builder setEnforcementPercentage(Integer enforcementPercentage) {
1082
          checkArgument(enforcementPercentage != null);
1✔
1083
          checkArgument(enforcementPercentage >= 0 && enforcementPercentage <= 100);
1✔
1084
          this.enforcementPercentage = enforcementPercentage;
1✔
1085
          return this;
1✔
1086
        }
1087

1088
        /** The minimum amount of hosts needed for success rate ejection. */
1089
        public Builder setMinimumHosts(Integer minimumHosts) {
1090
          checkArgument(minimumHosts != null);
1✔
1091
          checkArgument(minimumHosts >= 0);
1✔
1092
          this.minimumHosts = minimumHosts;
1✔
1093
          return this;
1✔
1094
        }
1095

1096
        /** The minimum address request volume to be considered for success rate ejection. */
1097
        public Builder setRequestVolume(Integer requestVolume) {
1098
          checkArgument(requestVolume != null);
1✔
1099
          checkArgument(requestVolume >= 0);
1✔
1100
          this.requestVolume = requestVolume;
1✔
1101
          return this;
1✔
1102
        }
1103

1104
        /** Builds a new instance of {@link SuccessRateEjection}. */
1105
        public SuccessRateEjection build() {
1106
          return new SuccessRateEjection(stdevFactor, enforcementPercentage, minimumHosts,
1✔
1107
              requestVolume);
1108
        }
1109
      }
1110
    }
1111

1112
    /** The configuration for failure percentage ejection. */
1113
    public static class FailurePercentageEjection {
1114
      public final Integer threshold;
1115
      public final Integer enforcementPercentage;
1116
      public final Integer minimumHosts;
1117
      public final Integer requestVolume;
1118

1119
      FailurePercentageEjection(Integer threshold, Integer enforcementPercentage,
1120
          Integer minimumHosts, Integer requestVolume) {
1✔
1121
        this.threshold = threshold;
1✔
1122
        this.enforcementPercentage = enforcementPercentage;
1✔
1123
        this.minimumHosts = minimumHosts;
1✔
1124
        this.requestVolume = requestVolume;
1✔
1125
      }
1✔
1126

1127
      /** For building new {@link FailurePercentageEjection} instances. */
1128
      public static class Builder {
1✔
1129
        Integer threshold = 85;
1✔
1130
        Integer enforcementPercentage = 100;
1✔
1131
        Integer minimumHosts = 5;
1✔
1132
        Integer requestVolume = 50;
1✔
1133

1134
        /** The failure percentage that will result in an address being considered an outlier. */
1135
        public Builder setThreshold(Integer threshold) {
1136
          checkArgument(threshold != null);
1✔
1137
          checkArgument(threshold >= 0 && threshold <= 100);
1✔
1138
          this.threshold = threshold;
1✔
1139
          return this;
1✔
1140
        }
1141

1142
        /** Only eject this percentage of outliers. */
1143
        public Builder setEnforcementPercentage(Integer enforcementPercentage) {
1144
          checkArgument(enforcementPercentage != null);
1✔
1145
          checkArgument(enforcementPercentage >= 0 && enforcementPercentage <= 100);
1✔
1146
          this.enforcementPercentage = enforcementPercentage;
1✔
1147
          return this;
1✔
1148
        }
1149

1150
        /** The minimum amount of host for failure percentage ejection to be enabled. */
1151
        public Builder setMinimumHosts(Integer minimumHosts) {
1152
          checkArgument(minimumHosts != null);
1✔
1153
          checkArgument(minimumHosts >= 0);
1✔
1154
          this.minimumHosts = minimumHosts;
1✔
1155
          return this;
1✔
1156
        }
1157

1158
        /**
1159
         * The request volume required for an address to be considered for failure percentage
1160
         * ejection.
1161
         */
1162
        public Builder setRequestVolume(Integer requestVolume) {
1163
          checkArgument(requestVolume != null);
1✔
1164
          checkArgument(requestVolume >= 0);
1✔
1165
          this.requestVolume = requestVolume;
1✔
1166
          return this;
1✔
1167
        }
1168

1169
        /** Builds a new instance of {@link FailurePercentageEjection}. */
1170
        public FailurePercentageEjection build() {
1171
          return new FailurePercentageEjection(threshold, enforcementPercentage, minimumHosts,
1✔
1172
              requestVolume);
1173
        }
1174
      }
1175
    }
1176

1177
    /** Determine if any outlier detection algorithms are enabled in the config. */
1178
    boolean outlierDetectionEnabled() {
1179
      return successRateEjection != null || failurePercentageEjection != null;
1✔
1180
    }
1181
  }
1182
}
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