Class LoadBalancer.Helper
- java.lang.Object
-
- io.grpc.LoadBalancer.Helper
-
- Direct Known Subclasses:
ForwardingLoadBalancerHelper
- Enclosing class:
- LoadBalancer
@ThreadSafe @ExperimentalApi("https://github.com/grpc/grpc-java/issues/1771") public abstract static class LoadBalancer.Helper extends Object
Provides essentials for LoadBalancer implementations.- Since:
- 1.2.0
-
-
Constructor Summary
Constructors Constructor Description Helper()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description abstract ManagedChannel
createOobChannel(EquivalentAddressGroup eag, String authority)
Create an out-of-band channel for the LoadBalancer’s own RPC needs, e.g., talking to an external load-balancer service.ManagedChannel
createOobChannel(List<EquivalentAddressGroup> eag, String authority)
Create an out-of-band channel for the LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service.ManagedChannel
createResolvingOobChannel(String target)
Creates an out-of-band channel for LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service, that is specified by a target string.ManagedChannelBuilder<?>
createResolvingOobChannelBuilder(String target)
Deprecated.ManagedChannelBuilder<?>
createResolvingOobChannelBuilder(String target, ChannelCredentials creds)
Creates an out-of-band channel builder for LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service, that is specified by a target string and credentials.LoadBalancer.Subchannel
createSubchannel(LoadBalancer.CreateSubchannelArgs args)
Creates a Subchannel, which is a logical connection to the given group of addresses which are considered equivalent.abstract String
getAuthority()
Returns the authority string of the channel, which is derived from the DNS-style target name.ChannelCredentials
getChannelCredentials()
Returns the ChannelCredentials used to construct the channel, without bearer tokens.ChannelLogger
getChannelLogger()
Returns theChannelLogger
for the Channel served by this LoadBalancer.String
getChannelTarget()
Returns the target string of the channel, guaranteed to include its scheme.io.grpc.MetricRecorder
getMetricRecorder()
Returns theMetricRecorder
that the channel uses to record metrics.NameResolver.Args
getNameResolverArgs()
Returns theNameResolver.Args
that the Channel uses to createNameResolver
s.NameResolverRegistry
getNameResolverRegistry()
Returns theNameResolverRegistry
that the Channel uses to look forNameResolver
s.ScheduledExecutorService
getScheduledExecutorService()
Returns aScheduledExecutorService
for scheduling delayed tasks.SynchronizationContext
getSynchronizationContext()
Returns aSynchronizationContext
that runs tasks in the same Synchronization Context as that the callback methods on theLoadBalancer
interface are run in.ChannelCredentials
getUnsafeChannelCredentials()
Returns the UNSAFE ChannelCredentials used to construct the channel, including bearer tokens.void
ignoreRefreshNameResolutionCheck()
Deprecated.Warning has been removedvoid
refreshNameResolution()
CallNameResolver.refresh()
on the channel's resolver.abstract void
updateBalancingState(ConnectivityState newState, LoadBalancer.SubchannelPicker newPicker)
Set a new state with a new picker to the channel.void
updateOobChannelAddresses(ManagedChannel channel, EquivalentAddressGroup eag)
Updates the addresses used for connections in theChannel
that was created bycreateOobChannel(EquivalentAddressGroup, String)
.void
updateOobChannelAddresses(ManagedChannel channel, List<EquivalentAddressGroup> eag)
Updates the addresses with a new EAG list.
-
-
-
Method Detail
-
createSubchannel
public LoadBalancer.Subchannel createSubchannel(LoadBalancer.CreateSubchannelArgs args)
Creates a Subchannel, which is a logical connection to the given group of addresses which are considered equivalent. Theattrs
are custom attributes associated with this Subchannel, and can be accessed later throughSubchannel.getAttributes()
.The LoadBalancer is responsible for closing unused Subchannels, and closing all Subchannels within
LoadBalancer.shutdown()
.It must be called from
the Synchronization Context
- Returns:
- Must return a valid Subchannel object, may not return null.
- Since:
- 1.22.0
-
createOobChannel
public abstract ManagedChannel createOobChannel(EquivalentAddressGroup eag, String authority)
Create an out-of-band channel for the LoadBalancer’s own RPC needs, e.g., talking to an external load-balancer service.The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB channels within
LoadBalancer.shutdown()
.- Since:
- 1.4.0
-
createOobChannel
public ManagedChannel createOobChannel(List<EquivalentAddressGroup> eag, String authority)
Create an out-of-band channel for the LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service. This version of the method allows multiple EAGs, so different addresses can have different authorities.The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB channels within
LoadBalancer.shutdown()
.
-
updateOobChannelAddresses
public void updateOobChannelAddresses(ManagedChannel channel, EquivalentAddressGroup eag)
Updates the addresses used for connections in theChannel
that was created bycreateOobChannel(EquivalentAddressGroup, String)
. This is superior tocreateOobChannel(EquivalentAddressGroup, String)
when the old and new addresses overlap, since the channel can continue using an existing connection.- Throws:
IllegalArgumentException
- ifchannel
was not returned fromcreateOobChannel(io.grpc.EquivalentAddressGroup, java.lang.String)
- Since:
- 1.4.0
-
updateOobChannelAddresses
public void updateOobChannelAddresses(ManagedChannel channel, List<EquivalentAddressGroup> eag)
Updates the addresses with a new EAG list. Connection is continued when old and new addresses overlap.
-
createResolvingOobChannel
public ManagedChannel createResolvingOobChannel(String target)
Creates an out-of-band channel for LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service, that is specified by a target string. See the documentation onManagedChannelBuilder.forTarget(java.lang.String)
for the format of a target string.The target string will be resolved by a
NameResolver
created according to the target string.The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB channels within
LoadBalancer.shutdown()
.- Since:
- 1.20.0
-
createResolvingOobChannelBuilder
@Deprecated public ManagedChannelBuilder<?> createResolvingOobChannelBuilder(String target)
Deprecated.Creates an out-of-band channel builder for LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service, that is specified by a target string. See the documentation onManagedChannelBuilder.forTarget(java.lang.String)
for the format of a target string.The target string will be resolved by a
NameResolver
created according to the target string.The returned oob-channel builder defaults to use the same authority and ChannelCredentials (without bearer tokens) as the parent channel's for authentication. This is different from
createResolvingOobChannelBuilder(String, ChannelCredentials)
.The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB channels within
LoadBalancer.shutdown()
.- Since:
- 1.31.0
-
createResolvingOobChannelBuilder
public ManagedChannelBuilder<?> createResolvingOobChannelBuilder(String target, ChannelCredentials creds)
Creates an out-of-band channel builder for LoadBalancer's own RPC needs, e.g., talking to an external load-balancer service, that is specified by a target string and credentials. See the documentation onGrpc.newChannelBuilder(java.lang.String, io.grpc.ChannelCredentials)
for the format of a target string.The target string will be resolved by a
NameResolver
created according to the target string.The LoadBalancer is responsible for closing unused OOB channels, and closing all OOB channels within
LoadBalancer.shutdown()
.- Since:
- 1.35.0
-
updateBalancingState
public abstract void updateBalancingState(@Nonnull ConnectivityState newState, @Nonnull LoadBalancer.SubchannelPicker newPicker)
Set a new state with a new picker to the channel.When a new picker is provided via
updateBalancingState()
, the channel will apply the picker on all buffered RPCs, by callingLoadBalancer.SubchannelPicker.pickSubchannel( LoadBalancer.PickSubchannelArgs)
.The channel will hold the picker and use it for all RPCs, until
updateBalancingState()
is called again and a new picker replaces the old one. IfupdateBalancingState()
has never been called, the channel will buffer all RPCs until a picker is provided.It should be called from the Synchronization Context. Currently will log a warning if violated. It will become an exception eventually. See #5015 for the background.
The passed state will be the channel's new state. The SHUTDOWN state should not be passed and its behavior is undefined.
- Since:
- 1.6.0
-
refreshNameResolution
public void refreshNameResolution()
CallNameResolver.refresh()
on the channel's resolver.It should be called from the Synchronization Context. Currently will log a warning if violated. It will become an exception eventually. See #5015 for the background.
- Since:
- 1.18.0
-
ignoreRefreshNameResolutionCheck
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8088") @Deprecated public void ignoreRefreshNameResolutionCheck()
Deprecated.Warning has been removedHistorically the channel automatically refreshes name resolution if any subchannel connection is broken. It's transitioning to let load balancers make the decision. To avoid silent breakages, the channel checks ifrefreshNameResolution()
is called by the load balancer. If not, it will do it and log a warning. This will be removed in the future and load balancers are completely responsible for triggering the refresh. See #8088 for the background.This should rarely be used, but sometimes the address for the subchannel wasn't provided by the name resolver and a refresh needs to be directed somewhere else instead. Then you can call this method to disable the short-tem check for detecting LoadBalancers that need to be updated for the new expected behavior.
- Since:
- 1.38.0
-
getSynchronizationContext
public SynchronizationContext getSynchronizationContext()
Returns aSynchronizationContext
that runs tasks in the same Synchronization Context as that the callback methods on theLoadBalancer
interface are run in.Pro-tip: in order to call
SynchronizationContext.schedule(java.lang.Runnable, long, java.util.concurrent.TimeUnit, java.util.concurrent.ScheduledExecutorService)
, you need to provide aScheduledExecutorService
.getScheduledExecutorService()
is provided for your convenience.- Since:
- 1.17.0
-
getScheduledExecutorService
public ScheduledExecutorService getScheduledExecutorService()
Returns aScheduledExecutorService
for scheduling delayed tasks.This service is a shared resource and is only meant for quick tasks. DO NOT block or run time-consuming tasks.
The returned service doesn't support
shutdown()
andshutdownNow()
. They will throw if called.- Since:
- 1.17.0
-
getAuthority
public abstract String getAuthority()
Returns the authority string of the channel, which is derived from the DNS-style target name. If overridden by a load balancer,getUnsafeChannelCredentials()
must also be overridden to callgetChannelCredentials()
or provide appropriate credentials.- Since:
- 1.2.0
-
getChannelTarget
public String getChannelTarget()
Returns the target string of the channel, guaranteed to include its scheme.
-
getChannelCredentials
public ChannelCredentials getChannelCredentials()
Returns the ChannelCredentials used to construct the channel, without bearer tokens.- Since:
- 1.35.0
-
getUnsafeChannelCredentials
public ChannelCredentials getUnsafeChannelCredentials()
Returns the UNSAFE ChannelCredentials used to construct the channel, including bearer tokens. Load balancers should generally have no use for these credentials and use of them is heavily discouraged. These must be used very carefully to avoid sending bearer tokens to untrusted servers as the server could then impersonate the client. Generally it is only safe to use these credentials when communicating with the backend.- Since:
- 1.35.0
-
getChannelLogger
public ChannelLogger getChannelLogger()
Returns theChannelLogger
for the Channel served by this LoadBalancer.- Since:
- 1.17.0
-
getNameResolverArgs
public NameResolver.Args getNameResolverArgs()
Returns theNameResolver.Args
that the Channel uses to createNameResolver
s.- Since:
- 1.22.0
-
getNameResolverRegistry
public NameResolverRegistry getNameResolverRegistry()
Returns theNameResolverRegistry
that the Channel uses to look forNameResolver
s.- Since:
- 1.22.0
-
getMetricRecorder
@Internal public io.grpc.MetricRecorder getMetricRecorder()
Returns theMetricRecorder
that the channel uses to record metrics.- Since:
- 1.64.0
-
-