Skip to content

Commit

Permalink
loadbalancer-experimental: rename ConnectionPoolStrategy -> Connectio…
Browse files Browse the repository at this point in the history
…nSelector (#3130)

Motivation:

The ConnectionPoolStrategy carries a lot of parallels with the HostSelector
abstraction, in that it's goal is to select a connection. However, the names
are dissimilar and 'Strategy' doesn't have as strong an implication of
being capable of action.

Modifications:

Rename ConnectionPoolStrategy to ConnectionSelector to make its role more
clear.
  • Loading branch information
bryce-anderson authored Dec 6, 2024
1 parent 8f72e01 commit 69c9cb3
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static io.servicetalk.utils.internal.NumberUtils.ensurePositive;

/**
* Configuration of the strategy for selecting connections from a pool to the same endpoint.
* Configuration of the policy for selecting connections from a pool to the same endpoint.
*/
public abstract class ConnectionPoolPolicy {

Expand All @@ -30,7 +30,7 @@ private ConnectionPoolPolicy() {
}

/**
* A connection selection strategy that prioritizes a configurable "core" pool.
* A connection selection policy that prioritizes a configurable "core" pool.
* <p>
* This {@link ConnectionPoolPolicy} attempts to emulate the pooling behavior often seen in thread pools.
* Specifically it allows for the configuration of a "core pool" size which are intended to be long-lived.
Expand All @@ -49,11 +49,11 @@ private ConnectionPoolPolicy() {
* @return the configured {@link ConnectionPoolPolicy}.
*/
public static ConnectionPoolPolicy corePool(final int corePoolSize, final boolean forceCorePool) {
return new CorePoolStrategy(corePoolSize, forceCorePool);
return new CorePoolPolicy(corePoolSize, forceCorePool);
}

/**
* A connection selection strategy that prioritizes connection reuse.
* A connection selection policy that prioritizes connection reuse.
* <p>
* This {@link ConnectionPoolPolicy} attempts to minimize the number of connections by attempting to direct
* traffic to connections in the order they were created in linear order up until a configured quantity. After
Expand All @@ -66,7 +66,7 @@ public static ConnectionPoolPolicy linearSearch() {
}

/**
* A connection selection strategy that prioritizes connection reuse.
* A connection selection policy that prioritizes connection reuse.
* <p>
* This {@link ConnectionPoolPolicy} attempts to minimize the number of connections by attempting to direct
* traffic to connections in the order they were created in linear order up until a configured quantity. After
Expand All @@ -77,12 +77,12 @@ public static ConnectionPoolPolicy linearSearch() {
* @return the configured {@link ConnectionPoolPolicy}.
*/
public static ConnectionPoolPolicy linearSearch(int linearSearchSpace) {
return new LinearSearchStrategy(linearSearchSpace);
return new LinearSearchPolicy(linearSearchSpace);
}

/**
* A {@link ConnectionPoolPolicy} that attempts to discern between the health of individual connections.
* If individual connections have health data the P2C strategy can be used to bias traffic toward the best
* If individual connections have health data the P2C policy can be used to bias traffic toward the best
* connections. This has the following algorithm:
* - Randomly select two connections from the 'core pool' (pick-two).
* - Try to select the 'best' of the two connections.
Expand All @@ -100,7 +100,7 @@ public static ConnectionPoolPolicy p2c(int corePoolSize, boolean forceCorePool)

/**
* A {@link ConnectionPoolPolicy} that attempts to discern between the health of individual connections.
* If individual connections have health data the P2C strategy can be used to bias traffic toward the best
* If individual connections have health data the P2C policy can be used to bias traffic toward the best
* connections. This has the following algorithm:
* - Randomly select two connections from the 'core pool' (pick-two).
* - Try to select the 'best' of the two connections.
Expand All @@ -114,36 +114,36 @@ public static ConnectionPoolPolicy p2c(int corePoolSize, boolean forceCorePool)
* @return the configured {@link ConnectionPoolPolicy}.
*/
public static ConnectionPoolPolicy p2c(int maxEffort, int corePoolSize, boolean forceCorePool) {
return new P2CStrategy(maxEffort, corePoolSize, forceCorePool);
return new P2CPolicy(maxEffort, corePoolSize, forceCorePool);
}

// instance types
static final class CorePoolStrategy extends ConnectionPoolPolicy {
static final class CorePoolPolicy extends ConnectionPoolPolicy {
final int corePoolSize;
final boolean forceCorePool;

CorePoolStrategy(final int corePoolSize, final boolean forceCorePool) {
CorePoolPolicy(final int corePoolSize, final boolean forceCorePool) {
this.corePoolSize = ensurePositive(corePoolSize, "corePoolSize");
this.forceCorePool = forceCorePool;
}
}

static final class P2CStrategy extends ConnectionPoolPolicy {
static final class P2CPolicy extends ConnectionPoolPolicy {
final int maxEffort;
final int corePoolSize;
final boolean forceCorePool;

P2CStrategy(final int maxEffort, final int corePoolSize, final boolean forceCorePool) {
P2CPolicy(final int maxEffort, final int corePoolSize, final boolean forceCorePool) {
this.maxEffort = ensurePositive(maxEffort, "maxEffort");
this.corePoolSize = ensurePositive(corePoolSize, "corePoolSize");
this.forceCorePool = forceCorePool;
}
}

static final class LinearSearchStrategy extends ConnectionPoolPolicy {
static final class LinearSearchPolicy extends ConnectionPoolPolicy {
final int linearSearchSpace;

LinearSearchStrategy(int linearSearchSpace) {
LinearSearchPolicy(int linearSearchSpace) {
this.linearSearchSpace = ensurePositive(linearSearchSpace, "linearSearchSpace");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
import javax.annotation.Nullable;

/**
* A strategy for selecting connections at the {@link Host} level connection pool.
* An abstraction for selecting connections at the {@link Host} level connection pool.
* @param <C> the concrete type of the connection.
*/
interface ConnectionPoolStrategy<C extends LoadBalancedConnection> {
interface ConnectionSelector<C extends LoadBalancedConnection> {

/**
* Select a connection from the ordered list of connections.
Expand All @@ -37,18 +37,18 @@ interface ConnectionPoolStrategy<C extends LoadBalancedConnection> {
C select(List<C> connections, Predicate<C> selector);

/**
* The factory of {@link ConnectionPoolStrategy} instances.
* @param <C> the least specific connection type necessary for properly implementing the strategy.
* @see ConnectionPoolStrategy for available strategies.
* The factory of {@link ConnectionSelector} instances.
* @param <C> the least specific connection type necessary for properly implementing the selector.
* @see ConnectionSelector for available strategies.
*/

interface ConnectionPoolStrategyFactory<C extends LoadBalancedConnection> {
interface ConnectionSelectorFactory<C extends LoadBalancedConnection> {

/**
* Provide an instance of the {@link ConnectionPoolStrategy} to use with a {@link Host}.
* Provide an instance of the {@link ConnectionSelector} to use with a {@link Host}.
* @param lbDescription description of the resource, used for logging purposes.
* @return an instance of the {@link ConnectionPoolStrategy} to use with a {@link Host}.
* @return an instance of the {@link ConnectionSelector} to use with a {@link Host}.
*/
ConnectionPoolStrategy<C> buildStrategy(String lbDescription);
ConnectionSelector<C> buildConnectionSelector(String lbDescription);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* A connection selection strategy that prioritizes a configurable "core" pool.
* <p>
* This {@link ConnectionPoolStrategy} attempts to emulate the pooling behavior often seen in thread pools.
* This {@link ConnectionSelector} attempts to emulate the pooling behavior often seen in thread pools.
* Specifically it allows for the configuration of a "core pool" size which are intended to be long-lived.
* Iteration starts in the core pool at a random position and then iterates through the entire core pool before
* moving to an overflow pool. Because iteration of this core pool starts at a random position the core connections
Expand All @@ -40,13 +40,13 @@
*
* @param <C> the concrete type of the {@link LoadBalancedConnection}.
*/
final class CorePoolConnectionPoolStrategy<C extends LoadBalancedConnection>
implements ConnectionPoolStrategy<C> {
final class CorePoolConnectionSelector<C extends LoadBalancedConnection>
implements ConnectionSelector<C> {

private final int corePoolSize;
private final boolean forceCorePool;

private CorePoolConnectionPoolStrategy(final int corePoolSize, final boolean forceCorePool) {
private CorePoolConnectionSelector(final int corePoolSize, final boolean forceCorePool) {
this.corePoolSize = ensurePositive(corePoolSize, "corePoolSize");
this.forceCorePool = forceCorePool;
}
Expand Down Expand Up @@ -83,30 +83,30 @@ public C select(List<C> connections, Predicate<C> selector) {
return null;
}

static <C extends LoadBalancedConnection> ConnectionPoolStrategyFactory<C> factory(
static <C extends LoadBalancedConnection> ConnectionSelectorFactory<C> factory(
int corePoolSize, boolean forceCorePool) {
return new CorePoolConnectionPoolStrategyFactory<>(corePoolSize, forceCorePool);
return new CorePoolConnectionSelectorFactory<>(corePoolSize, forceCorePool);
}

private static final class CorePoolConnectionPoolStrategyFactory<C extends LoadBalancedConnection>
implements ConnectionPoolStrategyFactory<C> {
private static final class CorePoolConnectionSelectorFactory<C extends LoadBalancedConnection>
implements ConnectionSelectorFactory<C> {

private final int corePoolSize;
private final boolean forceCorePool;

CorePoolConnectionPoolStrategyFactory(int corePoolSize, boolean forceCorePool) {
CorePoolConnectionSelectorFactory(int corePoolSize, boolean forceCorePool) {
this.corePoolSize = ensurePositive(corePoolSize, "corePoolSize");
this.forceCorePool = forceCorePool;
}

@Override
public ConnectionPoolStrategy<C> buildStrategy(String lbDescription) {
return new CorePoolConnectionPoolStrategy<>(corePoolSize, forceCorePool);
public ConnectionSelector<C> buildConnectionSelector(String lbDescription) {
return new CorePoolConnectionSelector<>(corePoolSize, forceCorePool);
}

@Override
public String toString() {
return "CorePoolConnectionPoolStrategyFactory{" +
return CorePoolConnectionSelectorFactory.class.getSimpleName() + "{" +
"corePoolSize=" + corePoolSize +
", forceCorePool=" + forceCorePool +
'}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private enum State {
private final Addr address;
@Nullable
private final HealthCheckConfig healthCheckConfig;
private final ConnectionPoolStrategy<C> connectionPoolStrategy;
private final ConnectionSelector<C> connectionSelector;
@Nullable
private final HealthIndicator<Addr, C> healthIndicator;
private final LoadBalancerObserver.HostObserver hostObserver;
Expand All @@ -94,14 +94,14 @@ private enum State {
private volatile ConnState connState = new ConnState(emptyList(), State.ACTIVE, 0, null);

DefaultHost(final String lbDescription, final Addr address,
final ConnectionPoolStrategy<C> connectionPoolStrategy,
final ConnectionSelector<C> connectionSelector,
final ConnectionFactory<Addr, ? extends C> connectionFactory,
final HostObserver hostObserver, @Nullable final HealthCheckConfig healthCheckConfig,
@Nullable final HealthIndicator<Addr, C> healthIndicator) {
this.lbDescription = requireNonNull(lbDescription, "lbDescription");
this.address = requireNonNull(address, "address");
this.healthIndicator = healthIndicator;
this.connectionPoolStrategy = requireNonNull(connectionPoolStrategy, "connectionPoolStrategy");
this.connectionSelector = requireNonNull(connectionSelector, "connectionSelector");
requireNonNull(connectionFactory, "connectionFactory");
this.connectionFactory = healthIndicator == null ? connectionFactory :
new InstrumentedConnectionFactory<>(connectionFactory, healthIndicator);
Expand Down Expand Up @@ -181,7 +181,7 @@ public boolean markExpired() {
@Nullable
public C pickConnection(Predicate<C> selector, @Nullable final ContextMap context) {
final List<C> connections = connState.connections;
return connectionPoolStrategy.select(connections, selector);
return connectionSelector.select(connections, selector);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ final class DefaultLoadBalancer<ResolvedAddress, C extends LoadBalancedConnectio
private final Processor<Object, Object> eventStreamProcessor = newPublisherProcessorDropHeadOnOverflow(32);
private final Publisher<Object> eventStream;
private final SequentialCancellable discoveryCancellable = new SequentialCancellable();
private final ConnectionPoolStrategy<C> connectionPoolStrategy;
private final ConnectionSelector<C> connectionSelector;
private final ConnectionFactory<ResolvedAddress, ? extends C> connectionFactory;
private final int randomSubsetSize;
@Nullable
Expand All @@ -130,7 +130,7 @@ final class DefaultLoadBalancer<ResolvedAddress, C extends LoadBalancedConnectio
* @param priorityStrategyFactory a builder of the {@link HostPriorityStrategy} to use with the load balancer.
* @param loadBalancingPolicy a factory of the initial host selector to use with this load balancer.
* @param randomSubsetSize the maximum number of health hosts to use when load balancing.
* @param connectionPoolStrategyFactory factory of the connection pool strategy to use with this load balancer.
* @param connectionSelectorFactory factory of the connection pool strategy to use with this load balancer.
* @param connectionFactory a function which creates new connections.
* @param loadBalancerObserverFactory factory used to build a {@link LoadBalancerObserver} to use with this
* load balancer.
Expand All @@ -146,7 +146,7 @@ final class DefaultLoadBalancer<ResolvedAddress, C extends LoadBalancedConnectio
final Function<String, HostPriorityStrategy> priorityStrategyFactory,
final LoadBalancingPolicy<ResolvedAddress, C> loadBalancingPolicy,
final int randomSubsetSize,
final ConnectionPoolStrategy.ConnectionPoolStrategyFactory<C> connectionPoolStrategyFactory,
final ConnectionSelector.ConnectionSelectorFactory<C> connectionSelectorFactory,
final ConnectionFactory<ResolvedAddress, ? extends C> connectionFactory,
final LoadBalancerObserverFactory loadBalancerObserverFactory,
@Nullable final HealthCheckConfig healthCheckConfig,
Expand All @@ -157,8 +157,8 @@ final class DefaultLoadBalancer<ResolvedAddress, C extends LoadBalancedConnectio
.buildSelector(Collections.emptyList(), lbDescription);
this.priorityStrategy = requireNonNull(
priorityStrategyFactory, "priorityStrategyFactory").apply(lbDescription);
this.connectionPoolStrategy = requireNonNull(connectionPoolStrategyFactory,
"connectionPoolStrategyFactory").buildStrategy(lbDescription);
this.connectionSelector = requireNonNull(connectionSelectorFactory,
"connectionSelectorFactory").buildConnectionSelector(lbDescription);
this.eventPublisher = requireNonNull(eventPublisher);
this.eventStream = fromSource(eventStreamProcessor)
.replay(1); // Allow for multiple subscribers and provide new subscribers with last signal.
Expand Down Expand Up @@ -427,7 +427,7 @@ private PrioritizedHostImpl<ResolvedAddress, C> createHost(ServiceDiscovererEven
final HealthCheckConfig hostHealthCheckConfig =
healthCheckConfig == null || healthCheckConfig.failedThreshold < 0 ? null : healthCheckConfig;
final PrioritizedHostImpl<ResolvedAddress, C> host = new PrioritizedHostImpl<>(
new DefaultHost<>(lbDescription, addr, connectionPoolStrategy,
new DefaultHost<>(lbDescription, addr, connectionSelector,
connectionFactory, hostObserver, hostHealthCheckConfig, indicator),
eventWeight(event), eventPriority(event));
if (indicator != null) {
Expand Down
Loading

0 comments on commit 69c9cb3

Please sign in to comment.