@Immutable @ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/1771") public static final class LoadBalancer.PickResult extends Object
SubchannelPicker
for an RPC.
The outcome of the decision will be one of the following:
withSubchannel()
, and is
in READY state when the RPC tries to start on it, the RPC will proceed on that
Subchannel.withError()
, and the RPC is not
wait-for-ready (i.e., CallOptions.withWaitForReady()
was not called), the RPC will
fail immediately with the given error.Helper.updateBalancingState()
,
when the RPC will go through the same picking process again.Modifier and Type | Method and Description |
---|---|
boolean |
equals(Object other)
|
Status |
getStatus()
The status associated with this result.
|
ClientStreamTracer.Factory |
getStreamTracerFactory()
The stream tracer factory this result was created with.
|
LoadBalancer.Subchannel |
getSubchannel()
The Subchannel if this result was created by
withSubchannel() , or
null otherwise. |
int |
hashCode() |
boolean |
isDrop()
Returns
true if this result was created by withDrop() . |
String |
toString() |
static LoadBalancer.PickResult |
withDrop(Status status)
A decision to fail an RPC immediately.
|
static LoadBalancer.PickResult |
withError(Status error)
A decision to report a connectivity error to the RPC.
|
static LoadBalancer.PickResult |
withNoResult()
No decision could be made.
|
static LoadBalancer.PickResult |
withSubchannel(LoadBalancer.Subchannel subchannel)
Equivalent to
withSubchannel(subchannel, null) . |
static LoadBalancer.PickResult |
withSubchannel(LoadBalancer.Subchannel subchannel,
ClientStreamTracer.Factory streamTracerFactory)
A decision to proceed the RPC on a Subchannel.
|
public static LoadBalancer.PickResult withSubchannel(LoadBalancer.Subchannel subchannel, @Nullable ClientStreamTracer.Factory streamTracerFactory)
The Subchannel should either be an original Subchannel returned by Helper.createSubchannel()
, or a wrapper of it preferably based on
ForwardingSubchannel
. At the very least its getInternalSubchannel()
must return the same object as the one returned by the original.
Otherwise the Channel cannot use it for the RPC.
When the RPC tries to use the return Subchannel, which is briefly after this method returns, the state of the Subchannel will decide where the RPC would go:
All buffered RPCs will stay buffered until the next call of Helper.updateBalancingState()
, which will trigger a new picking
process.
Note that Subchannel's state may change at the same time the picker is making the decision, which means the decision may be made with (to-be) outdated information. For example, a picker may return a Subchannel known to be READY, but it has become IDLE when is about to be used by the RPC, which makes the RPC to be buffered. The LoadBalancer will soon learn about the Subchannels' transition from READY to IDLE, create a new picker and allow the RPC to use another READY transport if there is any.
You will want to avoid running into a situation where there are READY Subchannels out there but some RPCs are still buffered for longer than a brief time.
In order to prevent unnecessary delay of RPCs, the rules of thumb are:
Subchannel.requestConnection()
whenever the Subchannel has transitioned to IDLE, then you don't need to include
IDLE Subchannels in your pick results.Helper.updateBalancingState()
whenever handleSubchannelState()
is called, unless the new state is SHUTDOWN. See
handleSubchannelState
's javadoc for more details.subchannel
- the picked Subchannel. It must have been started
streamTracerFactory
- if not null, will be used to trace the activities of the stream
created as a result of this pick. Note it's possible that no
stream is created at all in some cases.public static LoadBalancer.PickResult withSubchannel(LoadBalancer.Subchannel subchannel)
withSubchannel(subchannel, null)
.public static LoadBalancer.PickResult withError(Status error)
wait-for-ready
, it will stay buffered. Otherwise, it will fail
with the given error.error
- the error status. Must not be OK.public static LoadBalancer.PickResult withDrop(Status status)
status
- the status with which the RPC will fail. Must not be OK.public static LoadBalancer.PickResult withNoResult()
@Nullable public LoadBalancer.Subchannel getSubchannel()
withSubchannel()
, or
null otherwise.@Nullable public ClientStreamTracer.Factory getStreamTracerFactory()
public Status getStatus()
public boolean isDrop()
true
if this result was created by withDrop()
.