Skip to content

Commit 5fb80d0

Browse files
loadbalancer-experimental: Unify the Host.weight() methods (#3146)
Motivation: We have a few Host related weight methods, but they have slightly different names. Worse yet, we don't use them properly when adjusting weights. Modifications: Rename the LoadBalancerObserver.Host.loadBalancingWeight() to simply .weight()
1 parent e49939f commit 5fb80d0

File tree

8 files changed

+26
-34
lines changed

8 files changed

+26
-34
lines changed

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHost.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Addr address() {
117117
}
118118

119119
@Override
120-
public double loadBalancingWeight() {
120+
public double weight() {
121121
return 1;
122122
}
123123

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultHostPriorityStrategy.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ private void addToResults(int groupProbability, List<H> results) {
133133
// instead to all receive an equal portion of the groups weight.
134134
double weight = ((double) groupProbability) / hosts.size();
135135
for (H host : hosts) {
136-
host.loadBalancingWeight(weight);
136+
host.weight(weight);
137137
results.add(host);
138138
}
139139
} else {
140140
double scalingFactor = groupProbability / groupTotalWeight;
141141
for (H host : hosts) {
142-
double hostWeight = host.loadBalancingWeight() * scalingFactor;
143-
host.loadBalancingWeight(hostWeight);
142+
double hostWeight = host.weight() * scalingFactor;
143+
host.weight(hostWeight);
144144
if (hostWeight > 0) {
145145
results.add(host);
146146
}
@@ -152,7 +152,7 @@ private void addToResults(int groupProbability, List<H> results) {
152152
private static double totalWeight(Iterable<? extends PrioritizedHost> hosts) {
153153
double sum = 0;
154154
for (PrioritizedHost host : hosts) {
155-
sum += host.loadBalancingWeight();
155+
sum += host.weight();
156156
}
157157
return sum;
158158
}

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ private void sequentialUpdateUsedHosts(List<PrioritizedHostImpl<ResolvedAddress,
508508
// We need to reset the load balancing weights before we run the host set through the rest
509509
// of the operations that will transform and consume the load balancing weight.
510510
for (PrioritizedHostImpl<?, ?> host : nextHosts) {
511-
host.loadBalancingWeight(host.serviceDiscoveryWeight());
511+
host.weight(host.serviceDiscoveryWeight());
512512
}
513513
nextHosts = priorityStrategy.prioritize(nextHosts);
514514
nextHosts = subsetter.subset(nextHosts);
@@ -706,12 +706,12 @@ void serviceDiscoveryWeight(final double weight) {
706706
// Set the weight to use in load balancing. This includes derived weight information such as prioritization
707707
// and is what the host selectors will use when picking hosts.
708708
@Override
709-
public void loadBalancingWeight(final double weight) {
709+
public void weight(final double weight) {
710710
this.loadBalancingWeight = weight;
711711
}
712712

713713
@Override
714-
public double loadBalancingWeight() {
714+
public double weight() {
715715
return loadBalancingWeight;
716716
}
717717

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/Host.java

-8
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,4 @@ interface Host<ResolvedAddress, C extends LoadBalancedConnection> extends Listen
7373
* @return true if the host is now in the closed state, false otherwise.
7474
*/
7575
boolean markExpired();
76-
77-
/**
78-
* The weight of the host, relative to the weights of associated hosts.
79-
* @return the relative weight of the host.
80-
*/
81-
default double weight() {
82-
return 1.0;
83-
}
8476
}

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/LoadBalancerObserver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,6 @@ interface Host {
125125
* The weight of the host, relative to the weights of associated hosts as used for load balancing.
126126
* @return the relative weight of the host.
127127
*/
128-
double loadBalancingWeight();
128+
double weight();
129129
}
130130
}

servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/PrioritizedHost.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ interface PrioritizedHost {
4242
* The weight of the host to use for load balancing.
4343
* @return the weight of the host to use for load balancing.
4444
*/
45-
double loadBalancingWeight();
45+
double weight();
4646

4747
/**
4848
* Set the weight of the host to use during load balancing.
4949
* @param weight the weight of the host to use during load balancing.
5050
*/
51-
void loadBalancingWeight(double weight);
51+
void weight(double weight);
5252
}

servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultHostPriorityStrategyTest.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void noPriorities() {
4747
void noPrioritiesWithWeights() {
4848
List<TestPrioritizedHost> hosts = makeHosts(4);
4949
for (int i = 0; i < hosts.size(); i++) {
50-
hosts.get(i).loadBalancingWeight(i + 1d);
50+
hosts.get(i).weight(i + 1d);
5151
}
5252
List<TestPrioritizedHost> result = hostPriorityStrategy.prioritize(hosts);
5353
assertThat(result.size(), equalTo(hosts.size()));
@@ -79,7 +79,7 @@ void twoPrioritiesNoWeights() {
7979
void twoPrioritiesWithWeights() {
8080
List<TestPrioritizedHost> hosts = makeHosts(6);
8181
for (int i = 0; i < hosts.size(); i++) {
82-
hosts.get(i).loadBalancingWeight(i + 1d);
82+
hosts.get(i).weight(i + 1d);
8383
if (i >= 3) {
8484
hosts.get(i).priority(1);
8585
}
@@ -99,7 +99,7 @@ void twoPrioritiesWithWeights() {
9999
void twoPrioritiesWithWeightsButSkipNumbers() {
100100
List<TestPrioritizedHost> hosts = makeHosts(6);
101101
for (int i = 0; i < hosts.size(); i++) {
102-
hosts.get(i).loadBalancingWeight(i + 1d);
102+
hosts.get(i).weight(i + 1d);
103103
if (i >= 3) {
104104
hosts.get(i).priority(2);
105105
}
@@ -119,7 +119,7 @@ void twoPrioritiesWithWeightsButSkipNumbers() {
119119
void noHealthyNodesDoesntFilterOutElements() {
120120
List<TestPrioritizedHost> hosts = makeHosts(6);
121121
for (int i = 0; i < hosts.size(); i++) {
122-
hosts.get(i).loadBalancingWeight(i + 1d);
122+
hosts.get(i).weight(i + 1d);
123123
hosts.get(i).isHealthy = false;
124124
if (i >= 3) {
125125
hosts.get(i).priority(1);
@@ -206,10 +206,10 @@ void priorityGroupsWithWeightedUnhealthyNodes() {
206206
hosts.get(0).isHealthy(false);
207207
for (int i = 0; i < hosts.size(); i++) {
208208
if (i >= 3) {
209-
hosts.get(i).loadBalancingWeight(i - 3 + 1d);
209+
hosts.get(i).weight(i - 3 + 1d);
210210
hosts.get(i).priority(1);
211211
} else {
212-
hosts.get(i).loadBalancingWeight(i + 1d);
212+
hosts.get(i).weight(i + 1d);
213213
}
214214
}
215215
List<TestPrioritizedHost> result = hostPriorityStrategy.prioritize(hosts);
@@ -241,10 +241,10 @@ void onlyHealthyNodesAreZeroWeight() {
241241
List<TestPrioritizedHost> hosts = makeHosts(6);
242242
for (int i = 0; i < hosts.size(); i++) {
243243
if (i >= 3) {
244-
hosts.get(i).loadBalancingWeight(0);
244+
hosts.get(i).weight(0);
245245
hosts.get(i).priority(1);
246246
} else {
247-
hosts.get(i).loadBalancingWeight(1);
247+
hosts.get(i).weight(1);
248248
hosts.get(i).isHealthy = false;
249249
}
250250
}
@@ -312,7 +312,7 @@ void priority(final int priority) {
312312
// Set the weight to use in load balancing. This includes derived weight information such as prioritization
313313
// and is what the host selectors will use when picking endpoints.
314314
@Override
315-
public void loadBalancingWeight(final double weight) {
315+
public void weight(final double weight) {
316316
this.loadBalancedWeight = weight;
317317
}
318318

@@ -330,7 +330,7 @@ public void isHealthy(final boolean isHealthy) {
330330
}
331331

332332
@Override
333-
public double loadBalancingWeight() {
333+
public double weight() {
334334
return loadBalancedWeight;
335335
}
336336
}

servicetalk-loadbalancer-experimental/src/test/java/io/servicetalk/loadbalancer/DefaultLoadBalancerTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ void changesToPriorityOrWeightTriggerRebuilds() throws Exception {
185185
public <T extends PrioritizedHost> List<T> prioritize(List<T> hosts) {
186186
assert hosts.size() == 1;
187187
T host = hosts.get(0);
188-
value.set(host.loadBalancingWeight());
188+
value.set(host.weight());
189189
// We want to adjust the weight here so that if the `loadBalancingWeight()` fails to be
190190
// reset then the test will fail.
191-
host.loadBalancingWeight(0.5 * host.loadBalancingWeight());
191+
host.weight(0.5 * host.weight());
192192
return hosts;
193193
}
194194
};
@@ -201,23 +201,23 @@ public <T extends PrioritizedHost> List<T> prioritize(List<T> hosts) {
201201
List<? extends DefaultLoadBalancer.PrioritizedHostImpl<?, ?>> curentHosts = refinedLb.hosts();
202202
assertThat(curentHosts, hasSize(1));
203203
assertThat(curentHosts.get(0).priority(), equalTo(0));
204-
assertThat(curentHosts.get(0).loadBalancingWeight(), equalTo(0.5));
204+
assertThat(curentHosts.get(0).weight(), equalTo(0.5));
205205
assertThat(value.getAndSet(null), equalTo(1.0));
206206

207207
// send a new event with a different priority group. This should trigger another build.
208208
sendServiceDiscoveryEvents(richEvent(upEvent("address-1"), 1.0, 1));
209209
curentHosts = refinedLb.hosts();
210210
assertThat(curentHosts, hasSize(1));
211211
assertThat(curentHosts.get(0).priority(), equalTo(1));
212-
assertThat(curentHosts.get(0).loadBalancingWeight(), equalTo(0.5));
212+
assertThat(curentHosts.get(0).weight(), equalTo(0.5));
213213
assertThat(value.getAndSet(null), equalTo(1.0));
214214

215215
// send a new event with a different weight. This should trigger yet another build.
216216
sendServiceDiscoveryEvents(richEvent(upEvent("address-1"), 2.0, 1));
217217
curentHosts = refinedLb.hosts();
218218
assertThat(curentHosts, hasSize(1));
219219
assertThat(curentHosts.get(0).priority(), equalTo(1));
220-
assertThat(curentHosts.get(0).loadBalancingWeight(), equalTo(1.0));
220+
assertThat(curentHosts.get(0).weight(), equalTo(1.0));
221221
assertThat(value.getAndSet(null), equalTo(2.0));
222222
}
223223

0 commit comments

Comments
 (0)