-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
OutlierDetectionLoadBalancer.java
1070 lines (905 loc) · 37.7 KB
/
OutlierDetectionLoadBalancer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2022 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.util;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.grpc.Attributes;
import io.grpc.ClientStreamTracer;
import io.grpc.ClientStreamTracer.StreamInfo;
import io.grpc.ConnectivityState;
import io.grpc.ConnectivityStateInfo;
import io.grpc.EquivalentAddressGroup;
import io.grpc.Internal;
import io.grpc.LoadBalancer;
import io.grpc.Metadata;
import io.grpc.Status;
import io.grpc.SynchronizationContext;
import io.grpc.SynchronizationContext.ScheduledHandle;
import io.grpc.internal.ServiceConfigUtil.PolicySelection;
import io.grpc.internal.TimeProvider;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import javax.annotation.Nullable;
/**
* Wraps a child {@code LoadBalancer} while monitoring for outlier backends and removing them from
* the use of the child LB.
*
* <p>This implements the outlier detection gRFC:
* https://github.com/grpc/proposal/blob/master/A50-xds-outlier-detection.md
*/
@Internal
public final class OutlierDetectionLoadBalancer extends LoadBalancer {
@VisibleForTesting
final AddressTrackerMap trackerMap;
private final SynchronizationContext syncContext;
private final Helper childHelper;
private final GracefulSwitchLoadBalancer switchLb;
private TimeProvider timeProvider;
private final ScheduledExecutorService timeService;
private ScheduledHandle detectionTimerHandle;
private Long detectionTimerStartNanos;
private static final Attributes.Key<AddressTracker> ADDRESS_TRACKER_ATTR_KEY
= Attributes.Key.create("addressTrackerKey");
/**
* Creates a new instance of {@link OutlierDetectionLoadBalancer}.
*/
public OutlierDetectionLoadBalancer(Helper helper, TimeProvider timeProvider) {
childHelper = new ChildHelper(checkNotNull(helper, "helper"));
switchLb = new GracefulSwitchLoadBalancer(childHelper);
trackerMap = new AddressTrackerMap();
this.syncContext = checkNotNull(helper.getSynchronizationContext(), "syncContext");
this.timeService = checkNotNull(helper.getScheduledExecutorService(), "timeService");
this.timeProvider = timeProvider;
}
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
OutlierDetectionLoadBalancerConfig config
= (OutlierDetectionLoadBalancerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
// The map should only retain entries for addresses in this latest update.
ArrayList<SocketAddress> addresses = new ArrayList<>();
for (EquivalentAddressGroup addressGroup : resolvedAddresses.getAddresses()) {
addresses.addAll(addressGroup.getAddresses());
}
trackerMap.keySet().retainAll(addresses);
trackerMap.updateTrackerConfigs(config);
// Add any new ones.
trackerMap.putNewTrackers(config, addresses);
switchLb.switchTo(config.childPolicy.getProvider());
// If outlier detection is actually configured, start a timer that will periodically try to
// detect outliers.
if (config.outlierDetectionEnabled()) {
Long initialDelayNanos;
if (detectionTimerStartNanos == null) {
// On the first go we use the configured interval.
initialDelayNanos = config.intervalNanos;
} else {
// If a timer has started earlier we cancel it and use the difference between the start
// time and now as the interval.
initialDelayNanos = Math.max(0L,
config.intervalNanos - (timeProvider.currentTimeNanos() - detectionTimerStartNanos));
}
// If a timer has been previously created we need to cancel it and reset all the call counters
// for a fresh start.
if (detectionTimerHandle != null) {
detectionTimerHandle.cancel();
trackerMap.resetCallCounters();
}
detectionTimerHandle = syncContext.scheduleWithFixedDelay(new DetectionTimer(config),
initialDelayNanos, config.intervalNanos, NANOSECONDS, timeService);
} else if (detectionTimerHandle != null) {
// Outlier detection is not configured, but we have a lingering timer. Let's cancel it and
// uneject any addresses we may have ejected.
detectionTimerHandle.cancel();
detectionTimerStartNanos = null;
trackerMap.cancelTracking();
}
switchLb.handleResolvedAddresses(resolvedAddresses);
}
@Override
public void handleNameResolutionError(Status error) {
switchLb.handleNameResolutionError(error);
}
@Override
public void shutdown() {
switchLb.shutdown();
}
/**
* This timer will be invoked periodically, according to configuration, and it will look for any
* outlier subchannels.
*/
class DetectionTimer implements Runnable {
OutlierDetectionLoadBalancerConfig config;
DetectionTimer(OutlierDetectionLoadBalancerConfig config) {
this.config = config;
}
@Override
public void run() {
detectionTimerStartNanos = timeProvider.currentTimeNanos();
trackerMap.swapCounters();
for (OutlierEjectionAlgorithm algo : OutlierEjectionAlgorithm.forConfig(config)) {
algo.ejectOutliers(trackerMap, detectionTimerStartNanos);
}
trackerMap.maybeUnejectOutliers(detectionTimerStartNanos);
}
}
/**
* This child helper wraps the provided helper so that it can hand out wrapped {@link
* OutlierDetectionSubchannel}s and manage the address info map.
*/
class ChildHelper extends ForwardingLoadBalancerHelper {
private Helper delegate;
ChildHelper(Helper delegate) {
this.delegate = delegate;
}
@Override
protected Helper delegate() {
return delegate;
}
@Override
public Subchannel createSubchannel(CreateSubchannelArgs args) {
// Subchannels are wrapped so that we can monitor call results and to trigger failures when
// we decide to eject the subchannel.
return new OutlierDetectionSubchannel(delegate.createSubchannel(args));
}
@Override
public void updateBalancingState(ConnectivityState newState, SubchannelPicker newPicker) {
delegate.updateBalancingState(newState, new OutlierDetectionPicker(newPicker));
}
}
class OutlierDetectionSubchannel extends ForwardingSubchannel {
private final Subchannel delegate;
private AddressTracker addressTracker;
private boolean ejected;
private ConnectivityStateInfo lastSubchannelState;
private OutlierDetectionSubchannelStateListener subchannelStateListener;
OutlierDetectionSubchannel(Subchannel delegate) {
this.delegate = delegate;
}
@Override
public void start(SubchannelStateListener listener) {
subchannelStateListener = new OutlierDetectionSubchannelStateListener(listener);
super.start(subchannelStateListener);
}
@Override
public Attributes getAttributes() {
if (addressTracker != null) {
return delegate.getAttributes().toBuilder().set(ADDRESS_TRACKER_ATTR_KEY, addressTracker)
.build();
} else {
return delegate.getAttributes();
}
}
@Override
public void updateAddresses(List<EquivalentAddressGroup> addressGroups) {
// Outlier detection only supports subchannels with a single address, but the list of
// addressGroups associated with a subchannel can change at any time, so we need to react to
// changes in the address list plurality.
// No change in address plurality, we replace the single one with a new one.
if (hasSingleAddress(getAllAddresses()) && hasSingleAddress(addressGroups)) {
// Remove the current subchannel from the old address it is associated with in the map.
if (trackerMap.containsValue(addressTracker)) {
addressTracker.removeSubchannel(this);
}
// If the map has an entry for the new address, we associate this subchannel with it.
SocketAddress address = addressGroups.get(0).getAddresses().get(0);
if (trackerMap.containsKey(address)) {
trackerMap.get(address).addSubchannel(this);
}
} else if (hasSingleAddress(getAllAddresses()) && !hasSingleAddress(addressGroups)) {
// We go from a single address to having multiple, making this subchannel uneligible for
// outlier detection. Remove it from all trackers and reset the call counters of all the
// associated trackers.
// Remove the current subchannel from the old address it is associated with in the map.
if (trackerMap.containsKey(getAddresses().getAddresses().get(0))) {
AddressTracker tracker = trackerMap.get(getAddresses().getAddresses().get(0));
tracker.removeSubchannel(this);
tracker.resetCallCounters();
}
} else if (!hasSingleAddress(getAllAddresses()) && hasSingleAddress(addressGroups)) {
// We go from, previously uneligble, multiple address mode to a single address. If the map
// has an entry for the new address, we associate this subchannel with it.
SocketAddress address = addressGroups.get(0).getAddresses().get(0);
if (trackerMap.containsKey(address)) {
AddressTracker tracker = trackerMap.get(address);
tracker.addSubchannel(this);
}
}
// We could also have multiple addressGroups and get an update for multiple new ones. This is
// a no-op as we will just continue to ignore multiple address subchannels.
delegate.updateAddresses(addressGroups);
}
/**
* If the {@link Subchannel} is considered for outlier detection the associated {@link
* AddressTracker} should be set.
*/
void setAddressTracker(AddressTracker addressTracker) {
this.addressTracker = addressTracker;
}
void clearAddressTracker() {
this.addressTracker = null;
}
void eject() {
ejected = true;
subchannelStateListener.onSubchannelState(
ConnectivityStateInfo.forTransientFailure(Status.UNAVAILABLE));
}
void uneject() {
ejected = false;
if (lastSubchannelState != null) {
subchannelStateListener.onSubchannelState(lastSubchannelState);
}
}
boolean isEjected() {
return ejected;
}
@Override
protected Subchannel delegate() {
return delegate;
}
/**
* Wraps the actual listener so that state changes from the actual one can be intercepted.
*/
class OutlierDetectionSubchannelStateListener implements SubchannelStateListener {
private final SubchannelStateListener delegate;
OutlierDetectionSubchannelStateListener(SubchannelStateListener delegate) {
this.delegate = delegate;
}
@Override
public void onSubchannelState(ConnectivityStateInfo newState) {
lastSubchannelState = newState;
if (newState.getState().equals(ConnectivityState.READY)) {
// If the subchannel is associated with a single address that is also already in the map
// the subchannel will be added to the map and be included in outlier detection.
List<EquivalentAddressGroup> addressGroups = getAllAddresses();
if (hasSingleAddress(addressGroups)
&& trackerMap.containsKey(addressGroups.get(0).getAddresses().get(0))) {
AddressTracker tracker = trackerMap.get(addressGroups.get(0).getAddresses().get(0));
tracker.addSubchannel(OutlierDetectionSubchannel.this);
// If this address has already been ejected, we need to immediately eject this
// Subchannel.
if (tracker.ejectionTimeNanos != null) {
eject();
}
}
}
if (!ejected) {
delegate.onSubchannelState(newState);
}
}
}
}
/**
* This picker delegates the actual picking logic to a wrapped delegate, but associates a {@link
* ClientStreamTracer} with each pick to track the results of each subchannel stream.
*/
class OutlierDetectionPicker extends SubchannelPicker {
private final SubchannelPicker delegate;
OutlierDetectionPicker(SubchannelPicker delegate) {
this.delegate = delegate;
}
@Override
public PickResult pickSubchannel(PickSubchannelArgs args) {
PickResult pickResult = delegate.pickSubchannel(args);
Subchannel subchannel = pickResult.getSubchannel();
if (subchannel != null) {
return PickResult.withSubchannel(subchannel,
new ResultCountingClientStreamTracerFactory(
subchannel.getAttributes().get(ADDRESS_TRACKER_ATTR_KEY)));
}
return pickResult;
}
/**
* Builds instances of {@link ResultCountingClientStreamTracer}.
*/
class ResultCountingClientStreamTracerFactory extends ClientStreamTracer.Factory {
private final AddressTracker tracker;
ResultCountingClientStreamTracerFactory(AddressTracker tracker) {
this.tracker = tracker;
}
@Override
public ClientStreamTracer newClientStreamTracer(StreamInfo info, Metadata headers) {
return new ResultCountingClientStreamTracer(tracker);
}
}
/**
* Counts the results (successful/unsuccessful) of a particular {@link
* OutlierDetectionSubchannel}s streams and increments the counter in the associated {@link
* AddressTracker}.
*/
class ResultCountingClientStreamTracer extends ClientStreamTracer {
AddressTracker tracker;
public ResultCountingClientStreamTracer(AddressTracker tracker) {
this.tracker = tracker;
}
@Override
public void streamClosed(Status status) {
tracker.incrementCallCount(status.isOk());
}
}
}
/**
* Tracks additional information about a set of equivalent addresses needed for outlier
* detection.
*/
static class AddressTracker {
private OutlierDetectionLoadBalancerConfig config;
// Marked as volatile to assure that when the inactive counter is swapped in as the new active
// one, all threads see the change and don't hold on to a reference to the now inactive counter.
private volatile CallCounter activeCallCounter = new CallCounter();
private CallCounter inactiveCallCounter = new CallCounter();
private Long ejectionTimeNanos;
private int ejectionTimeMultiplier;
private final Set<OutlierDetectionSubchannel> subchannels = new HashSet<>();
AddressTracker(OutlierDetectionLoadBalancerConfig config) {
this.config = config;
}
void setConfig(OutlierDetectionLoadBalancerConfig config) {
this.config = config;
}
/**
* Adds a subchannel to the tracker, while assuring that the subchannel ejection status is
* updated to match the tracker's if needed.
*/
boolean addSubchannel(OutlierDetectionSubchannel subchannel) {
// Make sure that the subchannel is in the same ejection state as the new tracker it is
// associated with.
if (subchannelsEjected() && !subchannel.isEjected()) {
subchannel.eject();
} else if (!subchannelsEjected() && subchannel.isEjected()) {
subchannel.uneject();
}
subchannel.setAddressTracker(this);
return subchannels.add(subchannel);
}
boolean removeSubchannel(OutlierDetectionSubchannel subchannel) {
subchannel.clearAddressTracker();
return subchannels.remove(subchannel);
}
boolean containsSubchannel(OutlierDetectionSubchannel subchannel) {
return subchannels.contains(subchannel);
}
@VisibleForTesting
Set<OutlierDetectionSubchannel> getSubchannels() {
return ImmutableSet.copyOf(subchannels);
}
void incrementCallCount(boolean success) {
// If neither algorithm is configured, no point in incrementing counters.
if (config.successRateEjection == null && config.failurePercentageEjection == null) {
return;
}
if (success) {
activeCallCounter.successCount.getAndIncrement();
} else {
activeCallCounter.failureCount.getAndIncrement();
}
}
@VisibleForTesting
long activeVolume() {
return activeCallCounter.successCount.get() + activeCallCounter.failureCount.get();
}
long inactiveVolume() {
return inactiveCallCounter.successCount.get() + inactiveCallCounter.failureCount.get();
}
double successRate() {
return ((double) inactiveCallCounter.successCount.get()) / inactiveVolume();
}
double failureRate() {
return ((double)inactiveCallCounter.failureCount.get()) / inactiveVolume();
}
void resetCallCounters() {
activeCallCounter.reset();
inactiveCallCounter.reset();
}
void decrementEjectionTimeMultiplier() {
// The multiplier should not go negative.
ejectionTimeMultiplier = ejectionTimeMultiplier == 0 ? 0 : ejectionTimeMultiplier - 1;
}
void resetEjectionTimeMultiplier() {
ejectionTimeMultiplier = 0;
}
/**
* Swaps the active and inactive counters.
*
* <p>Note that this method is not thread safe as the swap is not done atomically. This is
* expected to only be called from the timer that is scheduled at a fixed delay, assuring that
* only one timer is active at a time.
*/
void swapCounters() {
inactiveCallCounter.reset();
CallCounter tempCounter = activeCallCounter;
activeCallCounter = inactiveCallCounter;
inactiveCallCounter = tempCounter;
}
void ejectSubchannels(long ejectionTimeNanos) {
this.ejectionTimeNanos = ejectionTimeNanos;
ejectionTimeMultiplier++;
for (OutlierDetectionSubchannel subchannel : subchannels) {
subchannel.eject();
}
}
/**
* Uneject a currently ejected address.
*/
void unejectSubchannels() {
checkState(ejectionTimeNanos != null, "not currently ejected");
ejectionTimeNanos = null;
for (OutlierDetectionSubchannel subchannel : subchannels) {
subchannel.uneject();
}
}
boolean subchannelsEjected() {
return ejectionTimeNanos != null;
}
public boolean maxEjectionTimeElapsed(long currentTimeNanos) {
// The instant in time beyond which the address should no longer be ejected. Also making sure
// we honor any maximum ejection time setting.
long maxEjectionDurationSecs
= Math.max(config.baseEjectionTimeNanos, config.maxEjectionTimeNanos);
long maxEjectionTimeNanos =
ejectionTimeNanos + Math.min(
config.baseEjectionTimeNanos * ejectionTimeMultiplier,
maxEjectionDurationSecs);
return currentTimeNanos > maxEjectionTimeNanos;
}
/** Tracks both successful and failed call counts. */
private static class CallCounter {
AtomicLong successCount = new AtomicLong();
AtomicLong failureCount = new AtomicLong();
void reset() {
successCount.set(0);
failureCount.set(0);
}
}
}
/**
* Maintains a mapping from addresses to their trackers.
*/
static class AddressTrackerMap extends ForwardingMap<SocketAddress, AddressTracker> {
private final Map<SocketAddress, AddressTracker> trackerMap;
AddressTrackerMap() {
trackerMap = new HashMap<>();
}
@Override
protected Map<SocketAddress, AddressTracker> delegate() {
return trackerMap;
}
void updateTrackerConfigs(OutlierDetectionLoadBalancerConfig config) {
for (AddressTracker tracker: trackerMap.values()) {
tracker.setConfig(config);
}
}
/** Adds a new tracker for every given address. */
void putNewTrackers(OutlierDetectionLoadBalancerConfig config,
Collection<SocketAddress> addresses) {
for (SocketAddress address : addresses) {
if (!trackerMap.containsKey(address)) {
trackerMap.put(address, new AddressTracker(config));
}
}
}
/** Resets the call counters for all the trackers in the map. */
void resetCallCounters() {
for (AddressTracker tracker : trackerMap.values()) {
tracker.resetCallCounters();
}
}
/**
* When OD gets disabled we need to uneject any subchannels that may have been ejected and
* to reset the ejection time multiplier.
*/
void cancelTracking() {
for (AddressTracker tracker : trackerMap.values()) {
if (tracker.subchannelsEjected()) {
tracker.unejectSubchannels();
}
tracker.resetEjectionTimeMultiplier();
}
}
/** Swaps the active and inactive counters for each tracker. */
void swapCounters() {
for (AddressTracker tracker : trackerMap.values()) {
tracker.swapCounters();
}
}
/**
* At the end of a timer run we need to decrement the ejection time multiplier for trackers
* that don't have ejected subchannels and uneject ones that have spent the maximum ejection
* time allowed.
*/
void maybeUnejectOutliers(Long detectionTimerStartNanos) {
for (AddressTracker tracker : trackerMap.values()) {
if (!tracker.subchannelsEjected()) {
tracker.decrementEjectionTimeMultiplier();
}
if (tracker.subchannelsEjected() && tracker.maxEjectionTimeElapsed(
detectionTimerStartNanos)) {
tracker.unejectSubchannels();
}
}
}
/**
* How many percent of the addresses would have their subchannels ejected if we proceeded
* with the next ejection.
*/
double nextEjectionPercentage() {
if (trackerMap.isEmpty()) {
return 0;
}
int totalAddresses = 0;
int ejectedAddresses = 0;
for (AddressTracker tracker : trackerMap.values()) {
totalAddresses++;
if (tracker.subchannelsEjected()) {
ejectedAddresses++;
}
}
return ((double)(ejectedAddresses + 1) / totalAddresses) * 100;
}
}
/**
* Implementations provide different ways of ejecting outlier addresses..
*/
interface OutlierEjectionAlgorithm {
/** Eject any outlier addresses. */
void ejectOutliers(AddressTrackerMap trackerMap, long ejectionTimeNanos);
/** Builds a list of algorithms that are enabled in the given config. */
@Nullable
static List<OutlierEjectionAlgorithm> forConfig(OutlierDetectionLoadBalancerConfig config) {
ImmutableList.Builder<OutlierEjectionAlgorithm> algoListBuilder = ImmutableList.builder();
if (config.successRateEjection != null) {
algoListBuilder.add(new SuccessRateOutlierEjectionAlgorithm(config));
}
if (config.failurePercentageEjection != null) {
algoListBuilder.add(new FailurePercentageOutlierEjectionAlgorithm(config));
}
return algoListBuilder.build();
}
}
/**
* This algorithm ejects addresses that don't maintain a required rate of successful calls. The
* required rate is not fixed, but is based on the mean and standard deviation of the success
* rates of all of the addresses.
*/
static class SuccessRateOutlierEjectionAlgorithm implements OutlierEjectionAlgorithm {
private final OutlierDetectionLoadBalancerConfig config;
SuccessRateOutlierEjectionAlgorithm(OutlierDetectionLoadBalancerConfig config) {
checkArgument(config.successRateEjection != null, "success rate ejection config is null");
this.config = config;
}
@Override
public void ejectOutliers(AddressTrackerMap trackerMap, long ejectionTimeNanos) {
// Only consider addresses that have the minimum request volume specified in the config.
List<AddressTracker> trackersWithVolume = trackersWithVolume(trackerMap, config);
// If we don't have enough addresses with significant volume then there's nothing to do.
if (trackersWithVolume.size() < config.successRateEjection.minimumHosts
|| trackersWithVolume.size() == 0) {
return;
}
// Calculate mean and standard deviation of the fractions of successful calls.
List<Double> successRates = new ArrayList<>();
for (AddressTracker tracker : trackersWithVolume) {
successRates.add(tracker.successRate());
}
double mean = mean(successRates);
double stdev = standardDeviation(successRates, mean);
double requiredSuccessRate =
mean - stdev * (config.successRateEjection.stdevFactor / 1000f);
for (AddressTracker tracker : trackersWithVolume) {
// If an ejection now would take us past the max configured ejection percentage, stop here.
if (trackerMap.nextEjectionPercentage() > config.maxEjectionPercent) {
return;
}
// If success rate is below the threshold, eject the address.
if (tracker.successRate() < requiredSuccessRate) {
// Only eject some addresses based on the enforcement percentage.
if (new Random().nextInt(100) < config.successRateEjection.enforcementPercentage) {
tracker.ejectSubchannels(ejectionTimeNanos);
}
}
}
}
/** Returns only the trackers that have the minimum configured volume to be considered. */
private List<AddressTracker> trackersWithVolume(AddressTrackerMap trackerMap,
OutlierDetectionLoadBalancerConfig config) {
List<AddressTracker> trackersWithVolume = new ArrayList<>();
for (AddressTracker tracker : trackerMap.values()) {
if (tracker.inactiveVolume() >= config.successRateEjection.requestVolume) {
trackersWithVolume.add(tracker);
}
}
return trackersWithVolume;
}
/** Calculates the mean of the given values. */
@VisibleForTesting
static double mean(Collection<Double> values) {
double totalValue = 0;
for (double value : values) {
totalValue += value;
}
return totalValue / values.size();
}
/** Calculates the standard deviation for the given values and their mean. */
@VisibleForTesting
static double standardDeviation(Collection<Double> values, double mean) {
double squaredDifferenceSum = 0;
for (double value : values) {
double difference = value - mean;
squaredDifferenceSum += difference * difference;
}
double variance = squaredDifferenceSum / values.size();
return Math.sqrt(variance);
}
}
static class FailurePercentageOutlierEjectionAlgorithm implements OutlierEjectionAlgorithm {
private final OutlierDetectionLoadBalancerConfig config;
FailurePercentageOutlierEjectionAlgorithm(OutlierDetectionLoadBalancerConfig config) {
this.config = config;
}
@Override
public void ejectOutliers(AddressTrackerMap trackerMap, long ejectionTimeNanos) {
// If we don't have the minimum amount of addresses the config calls for, then return.
if (trackerMap.size() < config.failurePercentageEjection.minimumHosts) {
return;
}
// If this address does not have enough volume to be considered, skip to the next one.
for (AddressTracker tracker : trackerMap.values()) {
// If an ejection now would take us past the max configured ejection percentage stop here.
if (trackerMap.nextEjectionPercentage() > config.maxEjectionPercent) {
return;
}
if (tracker.inactiveVolume() < config.failurePercentageEjection.requestVolume) {
continue;
}
// If the failure rate is above the threshold, we should eject...
double maxFailureRate = ((double)config.failurePercentageEjection.threshold) / 100;
if (tracker.failureRate() > maxFailureRate) {
// ...but only enforce this based on the enforcement percentage.
if (new Random().nextInt(100) < config.failurePercentageEjection.enforcementPercentage) {
tracker.ejectSubchannels(ejectionTimeNanos);
}
}
}
}
}
/** Counts how many addresses are in a given address group. */
private static boolean hasSingleAddress(List<EquivalentAddressGroup> addressGroups) {
int addressCount = 0;
for (EquivalentAddressGroup addressGroup : addressGroups) {
addressCount += addressGroup.getAddresses().size();
if (addressCount > 1) {
return false;
}
}
return true;
}
/**
* The configuration for {@link OutlierDetectionLoadBalancer}.
*/
public static final class OutlierDetectionLoadBalancerConfig {
public final Long intervalNanos;
public final Long baseEjectionTimeNanos;
public final Long maxEjectionTimeNanos;
public final Integer maxEjectionPercent;
public final SuccessRateEjection successRateEjection;
public final FailurePercentageEjection failurePercentageEjection;
public final PolicySelection childPolicy;
private OutlierDetectionLoadBalancerConfig(Long intervalNanos,
Long baseEjectionTimeNanos,
Long maxEjectionTimeNanos,
Integer maxEjectionPercent,
SuccessRateEjection successRateEjection,
FailurePercentageEjection failurePercentageEjection,
PolicySelection childPolicy) {
this.intervalNanos = intervalNanos;
this.baseEjectionTimeNanos = baseEjectionTimeNanos;
this.maxEjectionTimeNanos = maxEjectionTimeNanos;
this.maxEjectionPercent = maxEjectionPercent;
this.successRateEjection = successRateEjection;
this.failurePercentageEjection = failurePercentageEjection;
this.childPolicy = childPolicy;
}
/** Builds a new {@link OutlierDetectionLoadBalancerConfig}. */
public static class Builder {
Long intervalNanos = 10_000_000_000L; // 10s
Long baseEjectionTimeNanos = 30_000_000_000L; // 30s
Long maxEjectionTimeNanos = 30_000_000_000L; // 30s
Integer maxEjectionPercent = 10;
SuccessRateEjection successRateEjection;
FailurePercentageEjection failurePercentageEjection;
PolicySelection childPolicy;
/** The interval between outlier detection sweeps. */
public Builder setIntervalNanos(Long intervalNanos) {
checkArgument(intervalNanos != null);
this.intervalNanos = intervalNanos;
return this;
}
/** The base time an address is ejected for. */
public Builder setBaseEjectionTimeNanos(Long baseEjectionTimeNanos) {
checkArgument(baseEjectionTimeNanos != null);
this.baseEjectionTimeNanos = baseEjectionTimeNanos;
return this;
}
/** The longest time an address can be ejected. */
public Builder setMaxEjectionTimeNanos(Long maxEjectionTimeNanos) {
checkArgument(maxEjectionTimeNanos != null);
this.maxEjectionTimeNanos = maxEjectionTimeNanos;
return this;
}
/** The algorithm agnostic maximum percentage of addresses that can be ejected. */
public Builder setMaxEjectionPercent(Integer maxEjectionPercent) {
checkArgument(maxEjectionPercent != null);
this.maxEjectionPercent = maxEjectionPercent;
return this;
}
/** Set to enable success rate ejection. */
public Builder setSuccessRateEjection(
SuccessRateEjection successRateEjection) {
this.successRateEjection = successRateEjection;
return this;
}
/** Set to enable failure percentage ejection. */
public Builder setFailurePercentageEjection(
FailurePercentageEjection failurePercentageEjection) {
this.failurePercentageEjection = failurePercentageEjection;
return this;
}
/** Sets the child policy the {@link OutlierDetectionLoadBalancer} delegates to. */
public Builder setChildPolicy(PolicySelection childPolicy) {
checkState(childPolicy != null);
this.childPolicy = childPolicy;
return this;
}
/** Builds a new instance of {@link OutlierDetectionLoadBalancerConfig}. */
public OutlierDetectionLoadBalancerConfig build() {
checkState(childPolicy != null);
return new OutlierDetectionLoadBalancerConfig(intervalNanos, baseEjectionTimeNanos,
maxEjectionTimeNanos, maxEjectionPercent, successRateEjection,
failurePercentageEjection, childPolicy);
}
}
/** The configuration for success rate ejection. */
public static class SuccessRateEjection {
public final Integer stdevFactor;
public final Integer enforcementPercentage;
public final Integer minimumHosts;
public final Integer requestVolume;
SuccessRateEjection(Integer stdevFactor, Integer enforcementPercentage, Integer minimumHosts,
Integer requestVolume) {
this.stdevFactor = stdevFactor;
this.enforcementPercentage = enforcementPercentage;
this.minimumHosts = minimumHosts;
this.requestVolume = requestVolume;
}
/** Builds new instances of {@link SuccessRateEjection}. */
public static final class Builder {
Integer stdevFactor = 1900;
Integer enforcementPercentage = 100;
Integer minimumHosts = 5;
Integer requestVolume = 100;
/** The product of this and the standard deviation of success rates determine the ejection
* threshold.
*/
public Builder setStdevFactor(Integer stdevFactor) {
checkArgument(stdevFactor != null);
this.stdevFactor = stdevFactor;
return this;
}
/** Only eject this percentage of outliers. */
public Builder setEnforcementPercentage(Integer enforcementPercentage) {
checkArgument(enforcementPercentage != null);
checkArgument(enforcementPercentage >= 0 && enforcementPercentage <= 100);
this.enforcementPercentage = enforcementPercentage;
return this;
}
/** The minimum amount of hosts needed for success rate ejection. */
public Builder setMinimumHosts(Integer minimumHosts) {
checkArgument(minimumHosts != null);
checkArgument(minimumHosts >= 0);
this.minimumHosts = minimumHosts;
return this;
}
/** The minimum address request volume to be considered for success rate ejection. */
public Builder setRequestVolume(Integer requestVolume) {
checkArgument(requestVolume != null);
checkArgument(requestVolume >= 0);
this.requestVolume = requestVolume;
return this;
}
/** Builds a new instance of {@link SuccessRateEjection}. */
public SuccessRateEjection build() {
return new SuccessRateEjection(stdevFactor, enforcementPercentage, minimumHosts,
requestVolume);
}
}
}
/** The configuration for failure percentage ejection. */