Top Description Inners Fields Constructors Methods
io.rsocket.loadbalance

public Class WeightedLoadbalanceStrategy

extends Object
implements ClientLoadbalanceStrategy
Class Inheritance
All Implemented Interfaces
io.rsocket.loadbalance.ClientLoadbalanceStrategy, io.rsocket.loadbalance.LoadbalanceStrategy
Imports
io.rsocket.RSocket, io.rsocket.core.RSocketConnector, io.rsocket.plugins.RequestInterceptor, java.util.List, .Map, java.util.concurrent.ConcurrentHashMap, .ThreadLocalRandom, java.util.function.Function, reactor.util.annotation.Nullable

LoadbalanceStrategy that assigns a weight to each RSocket based on availability and usage statistics. The weight is used to decide which RSocket to select.

Use create() or a Builder to create an instance.

Since
1.1
See Also
Predictive Load-Balancing: Unfair but Faster & more Robust, WeightedStatsRequestInterceptor

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
private static class

Field Summary

Modifier and TypeField and Description
private static final double
pack-priv final int
pack-priv final Function<RSocket, WeightedStats>

Constructor Summary

AccessConstructor and Description
private
WeightedLoadbalanceStrategy(int numberOfAttempts, Function<RSocket, WeightedStats> resolver)

Method Summary

Modifier and TypeMethod and Description
private static double
algorithmicWeight(RSocket rSocket, final WeightedStats weightedStats)

public static WeightedLoadbalanceStrategy.Builder
builder()

Return a builder to create a WeightedLoadbalanceStrategy with.

private static double
calculateFactor(final double u, final double l, final double bandWidth)

public static WeightedLoadbalanceStrategy
create()

Create an instance of WeightedLoadbalanceStrategy with default settings, which include round-robin load-balancing and 5 maxPairSelectionAttempts.

public void
initialize(RSocketConnector
the connector to configure
connector
)

Implements io.rsocket.loadbalance.ClientLoadbalanceStrategy.initialize.

Initialize the connector, for example using the InterceptorRegistry, to intercept requests.
public RSocket
select(List<RSocket>
the list to choose from
sockets
)

Implements io.rsocket.loadbalance.LoadbalanceStrategy.select.

Select an RSocket from the given non-empty list.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EXP_FACTORback to summary
private static final double EXP_FACTOR
maxPairSelectionAttemptsback to summary
pack-priv final int maxPairSelectionAttempts
weightedStatsResolverback to summary
pack-priv final Function<RSocket, WeightedStats> weightedStatsResolver

Constructor Detail

WeightedLoadbalanceStrategyback to summary
private WeightedLoadbalanceStrategy(int numberOfAttempts, Function<RSocket, WeightedStats> resolver)

Method Detail

algorithmicWeightback to summary
private static double algorithmicWeight(RSocket rSocket, final WeightedStats weightedStats)
builderback to summary
public static WeightedLoadbalanceStrategy.Builder builder()

Return a builder to create a WeightedLoadbalanceStrategy with.

calculateFactorback to summary
private static double calculateFactor(final double u, final double l, final double bandWidth)
createback to summary
public static WeightedLoadbalanceStrategy create()

Create an instance of WeightedLoadbalanceStrategy with default settings, which include round-robin load-balancing and 5 maxPairSelectionAttempts.

initializeback to summary
public void initialize(RSocketConnector connector)

Implements io.rsocket.loadbalance.ClientLoadbalanceStrategy.initialize.

Doc from io.rsocket.loadbalance.ClientLoadbalanceStrategy.initialize.

Initialize the connector, for example using the InterceptorRegistry, to intercept requests.

Parameters
connector:RSocketConnector

the connector to configure

Annotations
@Override
selectback to summary
public RSocket select(List<RSocket> sockets)

Implements io.rsocket.loadbalance.LoadbalanceStrategy.select.

Doc from io.rsocket.loadbalance.LoadbalanceStrategy.select.

Select an RSocket from the given non-empty list.

Parameters
sockets:List<RSocket>

the list to choose from

Returns:RSocket

the selected instance

Annotations
@Override
io.rsocket.loadbalance back to summary

public Class WeightedLoadbalanceStrategy.Builder

extends Object
Class Inheritance

Builder for WeightedLoadbalanceStrategy.

Field Summary

Modifier and TypeField and Description
private int
private Function<RSocket, WeightedStats>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public WeightedLoadbalanceStrategy
build()

Build the WeightedLoadbalanceStrategy instance.

public WeightedLoadbalanceStrategy.Builder
maxPairSelectionAttempts(int
the iteration count
numberOfAttempts
)

How many times to try to randomly select a pair of RSocket connections with non-zero availability.

public WeightedLoadbalanceStrategy.Builder
weightedStatsResolver(Function<RSocket, WeightedStats>
to find the stats for an RSocket with
resolver
)

Configure how the created WeightedLoadbalanceStrategy should find the stats for a given RSocket.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

maxPairSelectionAttemptsback to summary
private int maxPairSelectionAttempts
weightedStatsResolverback to summary
private Function<RSocket, WeightedStats> weightedStatsResolver
Annotations
@Nullable

Constructor Detail

Builderback to summary
private Builder()

Method Detail

buildback to summary
public WeightedLoadbalanceStrategy build()

Build the WeightedLoadbalanceStrategy instance.

maxPairSelectionAttemptsback to summary
public WeightedLoadbalanceStrategy.Builder maxPairSelectionAttempts(int numberOfAttempts)

How many times to try to randomly select a pair of RSocket connections with non-zero availability. This is applicable when there are more than two connections in the pool. If the number of attempts is exceeded, the last selected pair is used.

By default this is set to 5.

Parameters
numberOfAttempts:int

the iteration count

weightedStatsResolverback to summary
public WeightedLoadbalanceStrategy.Builder weightedStatsResolver(Function<RSocket, WeightedStats> resolver)

Configure how the created WeightedLoadbalanceStrategy should find the stats for a given RSocket.

By default this resolver is not set.

When WeightedLoadbalanceStrategy is used through the LoadbalanceRSocketClient, the resolver does not need to be set because a WeightedStatsRequestInterceptor is automatically installed through the ClientLoadbalanceStrategy callback. If this strategy is used in any other context however, a resolver here must be provided.

Parameters
resolver:Function<RSocket, WeightedStats>

to find the stats for an RSocket with

io.rsocket.loadbalance back to summary

private Class WeightedLoadbalanceStrategy.DefaultWeightedStatsResolver

extends Object
implements Function<RSocket, WeightedStats>
Class Inheritance
  • java.lang.Object
  • io.rsocket.loadbalance.WeightedLoadbalanceStrategy.DefaultWeightedStatsResolver
All Implemented Interfaces
java.util.function.Function

Field Summary

Modifier and TypeField and Description
pack-priv final Map<RSocket, WeightedStats>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public WeightedStats
apply(RSocket
the function argument
rSocket
)

Implements java.util.function.Function.apply.

Applies this function to the given argument.
pack-priv void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

statsMapback to summary
pack-priv final Map<RSocket, WeightedStats> statsMap

Constructor Detail

DefaultWeightedStatsResolverback to summary
private DefaultWeightedStatsResolver()

Method Detail

applyback to summary
public WeightedStats apply(RSocket rSocket)

Implements java.util.function.Function.apply.

Doc from java.util.function.Function.apply.

Applies this function to the given argument.

Parameters
rSocket:RSocket

the function argument

Returns:WeightedStats

the function result

Annotations
@Override
initback to summary
pack-priv void init(RSocketConnector connector)