Skip to content

Commit

Permalink
loadbalancer: remove RoundRobinLoadBalancerBuilderProvider
Browse files Browse the repository at this point in the history
Motivation:

Now that DefaultLoadBalancer is in the main loadbalancer package
we can avoid using a provider to facilitate the migration.

Modifications:

Remove the migration provider and instead use the types directly.
  • Loading branch information
bryce-anderson committed Dec 19, 2024
1 parent e4d4418 commit 2454018
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
public final class RoundRobinLoadBalancerFactory<ResolvedAddress, C extends LoadBalancedConnection>
implements LoadBalancerFactory<ResolvedAddress, C> {

static final String ROUND_ROBIN_USER_DEFAULT_LOAD_BALANCER =
"io.servicetalk.loadbalancer.roundRobinUsesDefaultLoadBalancer";

private final String id;
private final int linearSearchSpace;
@Nullable
Expand All @@ -74,17 +77,21 @@ public <T extends C> LoadBalancer<T> newLoadBalancer(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
// We have to indirect here instead of at the `Builder.build()` call because as it turns out
// `Builder.build()` has a return type of RoundRobinLoadBalancerFactory and is public API.
return useDefaultLoadBalancer() ?
buildDefaultLoadBalancerFactory(targetResource, eventPublisher, connectionFactory) :
new RoundRobinLoadBalancer<>(
id, targetResource, eventPublisher, connectionFactory, linearSearchSpace, healthCheckConfig);
}

@Override
public LoadBalancer<C> newLoadBalancer(
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, C> connectionFactory,
final String targetResource) {
return new RoundRobinLoadBalancer<>(id, targetResource, eventPublisher, connectionFactory,
linearSearchSpace, healthCheckConfig);
// For now, we forward to the deprecated method since it is more generic.
return newLoadBalancer(targetResource, eventPublisher, connectionFactory);
}

@Override
Expand All @@ -102,6 +109,59 @@ public String toString() {
'}';
}

private <T extends C> LoadBalancer<T> buildDefaultLoadBalancerFactory(
final String targetResource,
final Publisher<? extends Collection<? extends ServiceDiscovererEvent<ResolvedAddress>>> eventPublisher,
final ConnectionFactory<ResolvedAddress, T> connectionFactory) {
final int healthCheckFailedConnectionsThreshold;
final Duration healthCheckInterval;
final Duration healthCheckJitter;
final Duration healthCheckResubscribeInterval;
final Duration healthCheckResubscribeJitter;
final Executor backgroundExecutor;
if (healthCheckConfig == null) {
healthCheckFailedConnectionsThreshold = -1; // disabled, the rest are fillers.
healthCheckInterval = DEFAULT_HEALTH_CHECK_INTERVAL;
healthCheckJitter = DEFAULT_HEALTH_CHECK_JITTER;
healthCheckResubscribeInterval = DEFAULT_HEALTH_CHECK_RESUBSCRIBE_INTERVAL;
healthCheckResubscribeJitter = DEFAULT_HEALTH_CHECK_JITTER;
backgroundExecutor = null;
} else {
healthCheckFailedConnectionsThreshold = healthCheckConfig.failedThreshold;
healthCheckInterval = healthCheckConfig.healthCheckInterval;
healthCheckJitter = healthCheckConfig.jitter;
healthCheckResubscribeInterval = healthCheckConfig.resubscribeInterval;
healthCheckResubscribeJitter = healthCheckConfig.healthCheckResubscribeJitter;
backgroundExecutor = healthCheckConfig.executor;
}

OutlierDetectorConfig outlierDetectorConfig = new OutlierDetectorConfig.Builder()
.ewmaHalfLife(Duration.ZERO)
// disable the xDS outlier detectors
.enforcingFailurePercentage(0)
.enforcingSuccessRate(0)
.enforcingConsecutive5xx(0)
// set the ServiceTalk L4 connection outlier detector settings
.failedConnectionsThreshold(healthCheckFailedConnectionsThreshold)
.failureDetectorInterval(healthCheckInterval, healthCheckJitter)
.serviceDiscoveryResubscribeInterval(healthCheckResubscribeInterval, healthCheckResubscribeJitter)
.build();
LoadBalancingPolicy<ResolvedAddress, T> loadBalancingPolicy =
LoadBalancingPolicies.roundRobin()
.failOpen(false)
.ignoreWeights(true)
.build();
LoadBalancerBuilder<ResolvedAddress, T> builder = LoadBalancers.builder(id);
if (backgroundExecutor != null) {
builder = builder.backgroundExecutor(backgroundExecutor);
}
return builder.outlierDetectorConfig(outlierDetectorConfig)
.loadBalancingPolicy(loadBalancingPolicy)
.connectionSelectorPolicy(ConnectionSelectorPolicies.linearSearch(linearSearchSpace))
.build()
.newLoadBalancer(eventPublisher, connectionFactory, targetResource);
}

/**
* Builder for {@link RoundRobinLoadBalancerFactory}.
*
Expand Down Expand Up @@ -227,4 +287,10 @@ static Executor getInstance() {
return INSTANCE;
}
}

private static boolean useDefaultLoadBalancer() {
// Enabled by default.
String propValue = System.getProperty(ROUND_ROBIN_USER_DEFAULT_LOAD_BALANCER);
return propValue == null || Boolean.parseBoolean(propValue);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 2454018

Please sign in to comment.