Class DelayedClientTransport
- java.lang.Object
-
- io.grpc.internal.DelayedClientTransport
-
- All Implemented Interfaces:
ClientTransport,ManagedClientTransport,InternalInstrumented<InternalChannelz.SocketStats>,InternalWithLogId
final class DelayedClientTransport extends java.lang.Object implements ManagedClientTransport
A client transport that queues requests before a real transport is available. Whenreprocess(io.grpc.LoadBalancer.SubchannelPicker)is called, this class applies the providedLoadBalancer.SubchannelPickerto pick a transport for each pending stream.This transport owns every stream that it has created until a real transport has been picked for that stream, at which point the ownership of the stream is transferred to the real transport, thus the delayed transport stops owning the stream.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classDelayedClientTransport.PendingStream(package private) static classDelayedClientTransport.PickerState-
Nested classes/interfaces inherited from interface io.grpc.internal.ClientTransport
ClientTransport.PingCallback
-
Nested classes/interfaces inherited from interface io.grpc.internal.ManagedClientTransport
ManagedClientTransport.Listener
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.ExecutordefaultAppExecutorprivate ManagedClientTransport.Listenerlistenerprivate java.lang.Objectlockprivate InternalLogIdlogIdprivate java.util.Collection<DelayedClientTransport.PendingStream>pendingStreamsprivate DelayedClientTransport.PickerStatepickerStateImmutable state needed for picking.private java.lang.RunnablereportTransportInUseprivate java.lang.RunnablereportTransportNotInUseprivate java.lang.RunnablereportTransportTerminatedprivate SynchronizationContextsyncContext
-
Constructor Summary
Constructors Constructor Description DelayedClientTransport(java.util.concurrent.Executor defaultAppExecutor, SynchronizationContext syncContext)Creates a new delayed transport.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private DelayedClientTransport.PendingStreamcreatePendingStream(LoadBalancer.PickSubchannelArgs args, ClientStreamTracer[] tracers)Caller must callsyncContext.drain()outside of lock because this method may schedule tasks on syncContext.InternalLogIdgetLogId()Returns an ID that is primarily used in debug logs.(package private) intgetPendingStreamsCount()com.google.common.util.concurrent.ListenableFuture<InternalChannelz.SocketStats>getStats()Returns the stats object.booleanhasPendingStreams()ClientStreamnewStream(MethodDescriptor<?,?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers)If aLoadBalancer.SubchannelPickeris being, or has been provided viareprocess(io.grpc.LoadBalancer.SubchannelPicker), the last picker will be consulted.voidping(ClientTransport.PingCallback callback, java.util.concurrent.Executor executor)Pings a remote endpoint.(package private) voidreprocess(LoadBalancer.SubchannelPicker picker)Use the picker to try picking a transport for every pending stream, proceed the stream if the pick is successful, otherwise keep it pending.voidshutdown(Status status)Prevents creating any new streams.voidshutdownNow(Status status)Shuts down this transport and cancels all streams that it owns, hence immediately terminates this transport.java.lang.Runnablestart(ManagedClientTransport.Listener listener)Starts transport.
-
-
-
Field Detail
-
logId
private final InternalLogId logId
-
lock
private final java.lang.Object lock
-
defaultAppExecutor
private final java.util.concurrent.Executor defaultAppExecutor
-
syncContext
private final SynchronizationContext syncContext
-
reportTransportInUse
private java.lang.Runnable reportTransportInUse
-
reportTransportNotInUse
private java.lang.Runnable reportTransportNotInUse
-
reportTransportTerminated
private java.lang.Runnable reportTransportTerminated
-
listener
private ManagedClientTransport.Listener listener
-
pendingStreams
@Nonnull private java.util.Collection<DelayedClientTransport.PendingStream> pendingStreams
-
pickerState
private volatile DelayedClientTransport.PickerState pickerState
Immutable state needed for picking. 'lock' must be held for writing.
-
-
Constructor Detail
-
DelayedClientTransport
DelayedClientTransport(java.util.concurrent.Executor defaultAppExecutor, SynchronizationContext syncContext)Creates a new delayed transport.- Parameters:
defaultAppExecutor- pending streams will create real streams and run buffered operations in an application executor, which will be this executor, unless there is on provided inCallOptions.syncContext- all listener callbacks of the delayed transport will be run from this SynchronizationContext.
-
-
Method Detail
-
start
public final java.lang.Runnable start(ManagedClientTransport.Listener listener)
Description copied from interface:ManagedClientTransportStarts transport. This method may only be called once.This method and the returned
Runnableshould not throw any exceptions.- Specified by:
startin interfaceManagedClientTransport- Parameters:
listener- non-nulllistener of transport events- Returns:
- a
Runnablethat is executed after-the-fact by the original caller, typically after locks are released
-
newStream
public final ClientStream newStream(MethodDescriptor<?,?> method, Metadata headers, CallOptions callOptions, ClientStreamTracer[] tracers)
If aLoadBalancer.SubchannelPickeris being, or has been provided viareprocess(io.grpc.LoadBalancer.SubchannelPicker), the last picker will be consulted.Otherwise, if the delayed transport is not shutdown, then a
DelayedClientTransport.PendingStreamis returned; if the transport is shutdown, then aFailingClientStreamis returned.- Specified by:
newStreamin interfaceClientTransport- Parameters:
method- the descriptor of the remote method to be called for this stream.headers- to send at the beginning of the callcallOptions- runtime options of the calltracers- a non-empty array of tracers. The last element in it is reserved to be set by the load balancer's pick result and otherwise is a no-op tracer.- Returns:
- the newly created stream.
-
createPendingStream
private DelayedClientTransport.PendingStream createPendingStream(LoadBalancer.PickSubchannelArgs args, ClientStreamTracer[] tracers)
Caller must callsyncContext.drain()outside of lock because this method may schedule tasks on syncContext.
-
ping
public final void ping(ClientTransport.PingCallback callback, java.util.concurrent.Executor executor)
Description copied from interface:ClientTransportPings a remote endpoint. When an acknowledgement is received, the given callback will be invoked using the given executor.Pings are not necessarily sent to the same endpoint, thus a successful ping only means at least one endpoint responded, but doesn't imply the availability of other endpoints (if there is any).
This is an optional method. Transports that do not have any mechanism by which to ping the remote endpoint may throw
UnsupportedOperationException.- Specified by:
pingin interfaceClientTransport
-
getStats
public com.google.common.util.concurrent.ListenableFuture<InternalChannelz.SocketStats> getStats()
Description copied from interface:InternalInstrumentedReturns the stats object.- Specified by:
getStatsin interfaceInternalInstrumented<InternalChannelz.SocketStats>
-
shutdown
public final void shutdown(Status status)
Prevents creating any new streams. Buffered streams are not failed and may still proceed whenreprocess(io.grpc.LoadBalancer.SubchannelPicker)is called. The delayed transport will be terminated when there is no more buffered streams.- Specified by:
shutdownin interfaceManagedClientTransport
-
shutdownNow
public final void shutdownNow(Status status)
Shuts down this transport and cancels all streams that it owns, hence immediately terminates this transport.- Specified by:
shutdownNowin interfaceManagedClientTransport
-
hasPendingStreams
public final boolean hasPendingStreams()
-
getPendingStreamsCount
final int getPendingStreamsCount()
-
reprocess
final void reprocess(@Nullable LoadBalancer.SubchannelPicker picker)Use the picker to try picking a transport for every pending stream, proceed the stream if the pick is successful, otherwise keep it pending.This method may be called concurrently with
newStream(), and it's safe. All pending streams will be served by the latest picker (if a same picker is given more than once, they are considered different pickers) as soon as possible.This method must not be called concurrently with itself.
-
getLogId
public InternalLogId getLogId()
Description copied from interface:InternalWithLogIdReturns an ID that is primarily used in debug logs. It usually contains the class name and a numeric ID that is unique among the instances.The subclasses of this interface usually want to include the log ID in their
Object.toString()results.- Specified by:
getLogIdin interfaceInternalWithLogId
-
-