-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
loadbalancer-experimental: remove random-subsetting API from LoadBala…
…ncerBuilder Motivation: There are many forms of subsetting and I don't think we have sorted out the best way to make that configurable from an API perspective. As we make the LoadBalancerBuilder API non-experimental, we don't want to yet commit to the existing API. Modifications: - Remove the configuration from LoadBalancerBuilder - Extract the random subsetting logic into it's own abstraction to prep for future subsetting strategies Result: - Smaller API commitments - More flexibility to experiment internally
- Loading branch information
1 parent
3a45a23
commit 10133e7
Showing
11 changed files
with
206 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/RandomSubsetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project 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.servicetalk.loadbalancer; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
import static io.servicetalk.utils.internal.NumberUtils.ensurePositive; | ||
|
||
/** | ||
* A {@link Subsetter} that takes a random subset of the provided hosts. | ||
*/ | ||
final class RandomSubsetter implements Subsetter { | ||
|
||
private final int randomSubsetSize; | ||
|
||
RandomSubsetter(int randomSubsetSize) { | ||
this.randomSubsetSize = ensurePositive(randomSubsetSize, "randomSubsetSize"); | ||
} | ||
|
||
@Override | ||
public <T extends PrioritizedHost> List<T> subset(List<T> nextHosts) { | ||
if (nextHosts.size() <= randomSubsetSize) { | ||
return nextHosts; | ||
} | ||
|
||
// We need to sort, and then return the list with the subsetSize number of healthy elements. | ||
List<T> result = new ArrayList<>(nextHosts); | ||
result.sort(Comparator.comparingLong(PrioritizedHost::randomSeed)); | ||
|
||
// We don't want to consider the unhealthy elements to be a part of our subset, so we're going to grow it | ||
// to account for un-health endpoints. However, we need to know how many that is. | ||
for (int i = 0, healthyCount = 0; i < result.size(); i++) { | ||
if (result.get(i).isHealthy()) { | ||
++healthyCount; | ||
if (healthyCount == randomSubsetSize) { | ||
// Trim elements after i to form the subset. | ||
while (result.size() > i + 1) { | ||
result.remove(result.size() - 1); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...cetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/Subsetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright © 2024 Apple Inc. and the ServiceTalk project 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.servicetalk.loadbalancer; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* An abstraction for picking a subset of hosts to use for load balancing purposes. | ||
*/ | ||
interface Subsetter { | ||
|
||
/** | ||
* Subset the provided host list into the list of hosts that will be used for load balancing. | ||
* @param hosts the eligible list of hosts. | ||
* @return the list of hosts that should be used for load balancing. | ||
* @param <T> the type of the {@link PrioritizedHost} | ||
*/ | ||
<T extends PrioritizedHost> List<T> subset(List<T> hosts); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.