Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadbalancer-experimental: tighten up load balancing policy #2884

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading