Skip to content

Commit

Permalink
loadbalancer-experimental: tighten up load balancing policy (#2884)
Browse files Browse the repository at this point in the history
Motivation:

The LoadBalancingPolicy interface suffers from the same problems
that the OutlierDetectorFactory did, namely that it's more powerful
than users need.

Modifications:

- Make the LoadBalancingPolicy an abstract class with package private
  constructor to let us control its proliferation for now.

Result:

A more constrained API. We can always open it up more later if we
want to.
  • Loading branch information
bryce-anderson authored Mar 28, 2024
1 parent 8ad0548 commit 1aafb7a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,32 @@
* @param <ResolvedAddress> the type of the resolved address
* @param <C> the type of the load balanced connection
*/
public interface LoadBalancingPolicy<ResolvedAddress, C extends LoadBalancedConnection> {
public abstract class LoadBalancingPolicy<ResolvedAddress, C extends LoadBalancedConnection> {

/**
* The default fail-open policy to use for {@link HostSelector} implementations.
*/
boolean DEFAULT_FAIL_OPEN_POLICY = false;
static final boolean DEFAULT_FAIL_OPEN_POLICY = false;

LoadBalancingPolicy() {
// package private constructor to control proliferation.
}

/**
* The name of the load balancing policy.
* @return the name of the load balancing policy
*/
String name();
public abstract String name();

@Override
public abstract String toString();

/**
* Construct a {@link HostSelector}.
* @param hosts the set of {@link Host}s to select from.
* @param targetResource the name of the target resource, useful for debugging purposes.
* @return a {@link HostSelector}
*/
HostSelector<ResolvedAddress, C> buildSelector(
abstract HostSelector<ResolvedAddress, C> buildSelector(
List<Host<ResolvedAddress, C>> hosts, String targetResource);
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* * Choices in Randomized Load Balancing</a>
*/
public final class P2CLoadBalancingPolicy<ResolvedAddress, C extends LoadBalancedConnection>
implements LoadBalancingPolicy<ResolvedAddress, C> {
extends LoadBalancingPolicy<ResolvedAddress, C> {

private final int maxEffort;
private final boolean failOpen;
Expand All @@ -54,7 +54,7 @@ private P2CLoadBalancingPolicy(final int maxEffort, final boolean failOpen, @Nul
}

@Override
public HostSelector<ResolvedAddress, C> buildSelector(
HostSelector<ResolvedAddress, C> buildSelector(
List<Host<ResolvedAddress, C>> hosts, String targetResource) {
return new P2CSelector<>(hosts, targetResource, maxEffort, failOpen, random);
}
Expand All @@ -66,7 +66,7 @@ public String name() {

@Override
public String toString() {
return name() + "(maxEffort=" + maxEffort + ')';
return name() + "(failOpen=" + failOpen + ", maxEffort=" + maxEffort + ')';
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* @param <C> the type of the load balanced connection
*/
public final class RoundRobinLoadBalancingPolicy<ResolvedAddress, C extends LoadBalancedConnection>
implements LoadBalancingPolicy<ResolvedAddress, C> {
extends LoadBalancingPolicy<ResolvedAddress, C> {

private final boolean failOpen;

Expand All @@ -37,7 +37,7 @@ private RoundRobinLoadBalancingPolicy(final boolean failOpen) {
}

@Override
public HostSelector<ResolvedAddress, C>
HostSelector<ResolvedAddress, C>
buildSelector(final List<Host<ResolvedAddress, C>> hosts, final String targetResource) {
return new RoundRobinSelector<>(hosts, targetResource, failOpen);
}
Expand All @@ -47,6 +47,11 @@ public String name() {
return "RoundRobin";
}

@Override
public String toString() {
return name() + "(failOpen=" + failOpen + ")";
}

/**
* A builder for immutable {@link RoundRobinLoadBalancingPolicy} instances.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,22 @@ List<TestHealthIndicator> getIndicators() {
}
}

private static class TestLoadBalancerPolicy implements LoadBalancingPolicy<String, TestLoadBalancedConnection> {
private static class TestLoadBalancerPolicy extends LoadBalancingPolicy<String, TestLoadBalancedConnection> {

int rebuilds;

@Override
public String name() {
return "test-selector";
return "TestPolicy";
}

@Override
public HostSelector<String, TestLoadBalancedConnection> buildSelector(
public String toString() {
return name() + "()";
}

@Override
HostSelector<String, TestLoadBalancedConnection> buildSelector(
List<Host<String, TestLoadBalancedConnection>> hosts, String targetResource) {
return new TestSelector(hosts);
}
Expand Down

0 comments on commit 1aafb7a

Please sign in to comment.