Package io.grpc.xds
Class RingHashLoadBalancer
java.lang.Object
io.grpc.LoadBalancer
io.grpc.util.MultiChildLoadBalancer
io.grpc.xds.RingHashLoadBalancer
final class RingHashLoadBalancer
extends io.grpc.util.MultiChildLoadBalancer
A
LoadBalancer
that provides consistent hashing based load balancing to upstream hosts.
It implements the "Ketama" hashing that maps hosts onto a circle (the "ring") by hashing its
addresses. Each request is routed to a host by hashing some property of the request and finding
the nearest corresponding host clockwise around the ring. Each host is placed on the ring some
number of times proportional to its weight. With the ring partitioned appropriately, the
addition or removal of one host from a set of N hosts will affect only 1/N requests.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
(package private) static final class
Configures the ring property.private static final class
private static final class
An unmodifiable view of a subchannel with state not subject to its real connectivity state changes.Nested classes/interfaces inherited from class io.grpc.util.MultiChildLoadBalancer
io.grpc.util.MultiChildLoadBalancer.AcceptResolvedAddrRetVal, io.grpc.util.MultiChildLoadBalancer.ChildLbState, io.grpc.util.MultiChildLoadBalancer.Endpoint
Nested classes/interfaces inherited from class io.grpc.LoadBalancer
io.grpc.LoadBalancer.CreateSubchannelArgs, io.grpc.LoadBalancer.ErrorPicker, io.grpc.LoadBalancer.Factory, io.grpc.LoadBalancer.FixedResultPicker, io.grpc.LoadBalancer.Helper, io.grpc.LoadBalancer.PickDetailsConsumer, io.grpc.LoadBalancer.PickResult, io.grpc.LoadBalancer.PickSubchannelArgs, io.grpc.LoadBalancer.ResolvedAddresses, io.grpc.LoadBalancer.Subchannel, io.grpc.LoadBalancer.SubchannelPicker, io.grpc.LoadBalancer.SubchannelStateListener
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final XxHash64
private final io.grpc.LoadBalancer.Factory
private final XdsLogger
private List
<RingHashLoadBalancer.RingEntry> private static final io.grpc.Status
private final io.grpc.SynchronizationContext
Fields inherited from class io.grpc.util.MultiChildLoadBalancer
currentConnectivityState, pickFirstLbProvider, resolvingAddresses
Fields inherited from class io.grpc.LoadBalancer
ATTR_HEALTH_CHECKING_CONFIG, DISABLE_SUBCHANNEL_RECONNECT_KEY, EMPTY_PICKER, HAS_HEALTH_PRODUCER_LISTENER_KEY, HEALTH_CONSUMER_LISTENER_ARG_KEY, IS_PETIOLE_POLICY
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionio.grpc.Status
acceptResolvedAddresses
(io.grpc.LoadBalancer.ResolvedAddresses resolvedAddresses) private static List
<RingHashLoadBalancer.RingEntry> protected io.grpc.util.MultiChildLoadBalancer.ChildLbState
createChildLbState
(Object key) static io.grpc.EquivalentAddressGroup
stripAttrs
(io.grpc.EquivalentAddressGroup eag) protected void
Updates the overall balancing state by aggregating the connectivity states of all subchannels.private io.grpc.Status
validateAddrList
(List<io.grpc.EquivalentAddressGroup> addrList) private String
validateNoDuplicateAddresses
(List<io.grpc.EquivalentAddressGroup> addrList) Methods inherited from class io.grpc.util.MultiChildLoadBalancer
acceptResolvedAddressesInternal, aggregateState, createChildAddressesMap, getChildLbState, getChildLbStateEag, getChildLbStates, getHelper, getReadyChildren, handleNameResolutionError, shutdown, shutdownRemoved
Methods inherited from class io.grpc.LoadBalancer
canHandleEmptyAddressListFromNameResolution, handleResolvedAddresses, handleSubchannelState, requestConnection
-
Field Details
-
RPC_HASH_NOT_FOUND
private static final io.grpc.Status RPC_HASH_NOT_FOUND -
hashFunc
-
lazyLbFactory
private final io.grpc.LoadBalancer.Factory lazyLbFactory -
logger
-
syncContext
private final io.grpc.SynchronizationContext syncContext -
ring
-
-
Constructor Details
-
RingHashLoadBalancer
RingHashLoadBalancer(io.grpc.LoadBalancer.Helper helper)
-
-
Method Details
-
acceptResolvedAddresses
public io.grpc.Status acceptResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses resolvedAddresses) - Overrides:
acceptResolvedAddresses
in classio.grpc.util.MultiChildLoadBalancer
-
updateOverallBalancingState
protected void updateOverallBalancingState()Updates the overall balancing state by aggregating the connectivity states of all subchannels.Aggregation rules (in order of dominance):
- If there is at least one subchannel in READY state, overall state is READY
- If there are 2 or more subchannels in TRANSIENT_FAILURE, overall state is TRANSIENT_FAILURE (to allow timely failover to another policy)
- If there is at least one subchannel in CONNECTING state, overall state is CONNECTING
- If there is one subchannel in TRANSIENT_FAILURE state and there is more than one subchannel, report CONNECTING
- If there is at least one subchannel in IDLE state, overall state is IDLE
- Otherwise, overall state is TRANSIENT_FAILURE
- Specified by:
updateOverallBalancingState
in classio.grpc.util.MultiChildLoadBalancer
-
createChildLbState
- Overrides:
createChildLbState
in classio.grpc.util.MultiChildLoadBalancer
-
validateAddrList
-
validateNoDuplicateAddresses
-
buildRing
private static List<RingHashLoadBalancer.RingEntry> buildRing(Map<io.grpc.EquivalentAddressGroup, Long> serverWeights, long totalWeight, double scale) -
stripAttrs
public static io.grpc.EquivalentAddressGroup stripAttrs(io.grpc.EquivalentAddressGroup eag)
-