From 707643c9caa74db0f5b58334fcbcfdcb79c83aeb Mon Sep 17 00:00:00 2001 From: Bryce Anderson Date: Tue, 10 Dec 2024 20:16:29 -0700 Subject: [PATCH] loadbalancer-experimental: Add default policies Motivation: Users currently need to understand a lot about the different policies in order to make a good decision. Modifications: Add default connection pool and load balancing policies. This makes it easy for users to pick the recommended default. This also gives us as maintainers a place to change the recommended default. --- .../loadbalancer/ConnectionPoolPolicies.java | 17 +++++++++++++---- .../DefaultLoadBalancerBuilder.java | 14 ++------------ .../loadbalancer/LoadBalancingPolicies.java | 11 +++++++++++ 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/ConnectionPoolPolicies.java b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/ConnectionPoolPolicies.java index c27dfe8430..a1306a4a76 100644 --- a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/ConnectionPoolPolicies.java +++ b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/ConnectionPoolPolicies.java @@ -25,6 +25,15 @@ private ConnectionPoolPolicies() { // no instances } + /** + * Get the recommended default {@link ConnectionPoolPolicy}. + * @param the concrete type of the {@link LoadBalancedConnection} + * @return the recommended default {@link ConnectionPoolPolicy}. + */ + public static ConnectionPoolPolicy defaultPolicy() { + return linearSearch(); + } + /** * A connection selection policy that prioritizes a configurable "core" pool. *

@@ -44,8 +53,8 @@ private ConnectionPoolPolicies() { * @param the concrete type of the {@link LoadBalancedConnection} * @return the configured {@link ConnectionPoolPolicy}. */ - public static ConnectionPoolPolicy - corePool(final int corePoolSize, final boolean forceCorePool) { + public static + ConnectionPoolPolicy corePool(final int corePoolSize, final boolean forceCorePool) { return CorePoolConnectionSelector.factory(corePoolSize, forceCorePool); } @@ -94,8 +103,8 @@ public static ConnectionPoolPolicy linearS * @param the concrete type of the {@link LoadBalancedConnection} * @return the configured {@link ConnectionPoolPolicy}. */ - public static ConnectionPoolPolicy - p2c(int corePoolSize, boolean forceCorePool) { + public static + ConnectionPoolPolicy p2c(int corePoolSize, boolean forceCorePool) { return p2c(DEFAULT_MAX_EFFORT, corePoolSize, forceCorePool); } diff --git a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancerBuilder.java b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancerBuilder.java index 6a141a7e66..3d17fe8fd5 100644 --- a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancerBuilder.java +++ b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/DefaultLoadBalancerBuilder.java @@ -39,8 +39,8 @@ final class DefaultLoadBalancerBuilder loadBalancingPolicy = defaultLoadBalancingPolicy(); - private ConnectionPoolPolicy connectionPoolPolicy = defaultConnectionSelectorFactory(); + private LoadBalancingPolicy loadBalancingPolicy = LoadBalancingPolicies.defaultPolicy(); + private ConnectionPoolPolicy connectionPoolPolicy = ConnectionPoolPolicies.defaultPolicy(); private OutlierDetectorConfig outlierDetectorConfig = OutlierDetectorConfig.DEFAULT_CONFIG; // package private constructor so users must funnel through providers in `LoadBalancers` @@ -181,14 +181,4 @@ private Executor getExecutor() { return backgroundExecutor == null ? RoundRobinLoadBalancerFactory.SharedExecutor.getInstance() : backgroundExecutor; } - - private static - LoadBalancingPolicy defaultLoadBalancingPolicy() { - return LoadBalancingPolicies.roundRobin().build(); - } - - private static ConnectionPoolPolicy - defaultConnectionSelectorFactory() { - return ConnectionPoolPolicies.linearSearch(); - } } diff --git a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/LoadBalancingPolicies.java b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/LoadBalancingPolicies.java index 260e8008d0..bdcd410853 100644 --- a/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/LoadBalancingPolicies.java +++ b/servicetalk-loadbalancer-experimental/src/main/java/io/servicetalk/loadbalancer/LoadBalancingPolicies.java @@ -15,6 +15,8 @@ */ package io.servicetalk.loadbalancer; +import io.servicetalk.client.api.LoadBalancedConnection; + /** * A collections of factories for constructing a {@link LoadBalancingPolicy}. */ @@ -24,6 +26,15 @@ private LoadBalancingPolicies() { // no instances. } + /** + * Get the recommended default {@link LoadBalancingPolicy}. + * @return the recommended default {@link LoadBalancingPolicy}. + */ + public static + LoadBalancingPolicy defaultPolicy() { + return LoadBalancingPolicies.roundRobin().build(); + } + /** * Builder for the round-robin {@link LoadBalancingPolicy}. * Round-robin load balancing is a strategy that maximizes fairness of the request distribution. This comes at the