-
Notifications
You must be signed in to change notification settings - Fork 13
/
BalancedNodeSelectionStrategyChannel.java
171 lines (153 loc) · 7.95 KB
/
BalancedNodeSelectionStrategyChannel.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
/*
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved.
*
* 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 com.palantir.dialogue.core;
import com.github.benmanes.caffeine.cache.Ticker;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.math.IntMath;
import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.Request;
import com.palantir.dialogue.Response;
import com.palantir.dialogue.core.BalancedScoreTracker.ChannelScoreInfo;
import com.palantir.dialogue.core.BalancedScoreTracker.ScoreSnapshot;
import com.palantir.dialogue.futures.DialogueFutures;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import com.palantir.tritium.metrics.registry.TaggedMetricRegistry;
import java.util.Optional;
import java.util.Random;
import java.util.stream.IntStream;
/**
* Chooses nodes to achieve the best possible client-side load balancing, by computing a 'score' for each channel and
* always routing requests to the channel with the lowest score. See {@link BalancedScoreTracker} for details of what
* goes into a 'score'.
*
* This is intended to be a strict improvement over Round Robin and Random Selection which can leave fast servers
* underutilized, as it sends the same number to both a slow and fast node. It is *not* appropriate for transactional
* workloads (where n requests must all land on the same server) or scenarios where cache warming is very important.
* {@link PinUntilErrorNodeSelectionStrategyChannel} remains the best choice for these.
*/
final class BalancedNodeSelectionStrategyChannel implements LimitedChannel {
private static final SafeLogger log = SafeLoggerFactory.get(BalancedNodeSelectionStrategyChannel.class);
private static final int INFLIGHT_COMPARISON_THRESHOLD = 5;
// When a channel has UNHEALTHY_SCORE_MULTIPLIER times the score of a channel with INFLIGHT_COMPARISON_THRESHOLD
// active requests, it's considered unhealthy and may not be attempted.
private static final int UNHEALTHY_SCORE_MULTIPLIER = 2;
private final BalancedScoreTracker tracker;
private final ImmutableList<BalancedChannel> channels;
BalancedNodeSelectionStrategyChannel(
ImmutableList<LimitedChannel> channels,
Random random,
Ticker ticker,
TaggedMetricRegistry taggedMetrics,
String channelName) {
Preconditions.checkState(channels.size() >= 2, "At least two channels required");
this.tracker = new BalancedScoreTracker(channels.size(), random, ticker, taggedMetrics, channelName);
this.channels = IntStream.range(0, channels.size())
.mapToObj(index -> new BalancedChannel(
channels.get(index), tracker.channelStats().get(index)))
.collect(ImmutableList.toImmutableList());
log.debug("Initialized", SafeArg.of("count", channels.size()), UnsafeArg.of("channels", channels));
}
@Override
public Optional<ListenableFuture<Response>> maybeExecute(
Endpoint endpoint, Request request, LimitEnforcement limitEnforcement) {
ScoreSnapshot[] snapshotsByScore = tracker.getSnapshotsInOrderOfIncreasingScore();
int giveUpThreshold = Integer.MAX_VALUE;
for (ScoreSnapshot snapshot : snapshotsByScore) {
/*
* If we're considering a channel that has a *drastically* higher score than the last one (i.e. we
* think it's much worse), then we can often get better outcomes by just refusing to send a
* request (and queueing) rather than sending something to this known-bad channel.
*
* This allows us to avoid sending requests to an unhealthy channel after a node has failed while
* the concurrency limit on the healthy channel is slowly expanded to meet increased load. Otherwise
* the assumed concurrency limit base don lower request load on the healthy channel may result in requests
* being sent to a node that's no longer alive.
*
* Note that this functionality is not safe if the preferred channel had zero inflight requests (as this
* could result in infinite queuing).
*/
if (snapshot.getScore() > giveUpThreshold) {
if (log.isDebugEnabled()) {
log.debug(
"Giving up and queueing because channel score ({}) for channel {} is not worth sending a "
+ "request to ({})",
SafeArg.of("score", snapshot.getScore()),
SafeArg.of("hostIndex", snapshot.getDelegate().channelIndex()),
SafeArg.of("giveUpScore", giveUpThreshold));
}
return Optional.empty();
}
if (snapshot.getInflight() > INFLIGHT_COMPARISON_THRESHOLD) {
int newThreshold = IntMath.saturatedMultiply(snapshot.getScore(), UNHEALTHY_SCORE_MULTIPLIER);
if (log.isDebugEnabled()) {
log.debug(
"When considering channel {}, giveUpThreshold {} -> {}",
SafeArg.of("hostIndex", snapshot.getDelegate().channelIndex()),
SafeArg.of("old", giveUpThreshold),
SafeArg.of("new", newThreshold));
}
giveUpThreshold = newThreshold;
}
BalancedChannel channel = channels.get(snapshot.getDelegate().channelIndex());
Optional<ListenableFuture<Response>> maybe =
StickyAttachments.maybeAddStickyToken(channel, endpoint, request, limitEnforcement);
if (maybe.isPresent()) {
return maybe;
}
}
return Optional.empty();
}
private static final class BalancedChannel implements LimitedChannel {
private final LimitedChannel delegate;
private final ChannelScoreInfo channelInfo;
BalancedChannel(LimitedChannel delegate, ChannelScoreInfo channelInfo) {
this.delegate = delegate;
this.channelInfo = channelInfo;
}
@Override
public Optional<ListenableFuture<Response>> maybeExecute(
Endpoint endpoint, Request request, LimitEnforcement limitEnforcement) {
channelInfo.startRequest();
Optional<ListenableFuture<Response>> maybe = delegate.maybeExecute(endpoint, request, limitEnforcement);
if (maybe.isPresent()) {
channelInfo.observability().markRequestMade();
DialogueFutures.addDirectCallback(maybe.get(), channelInfo);
return maybe;
} else {
channelInfo.undoStartRequest();
}
return Optional.empty();
}
@Override
public String toString() {
return "BalancedChannel{delegate=" + delegate + ", channelInfo=" + channelInfo + '}';
}
}
@VisibleForTesting
IntStream getScoresForTesting() {
return tracker.getScoresForTesting();
}
@Override
public String toString() {
return "BalancedNodeSelectionStrategyChannel{channels=" + channels + ", tracker=" + tracker + '}';
}
}