Package io.grpc.internal
Class RetriableStream<ReqT>
- java.lang.Object
-
- io.grpc.internal.RetriableStream<ReqT>
-
- All Implemented Interfaces:
ClientStream
,Stream
abstract class RetriableStream<ReqT> extends java.lang.Object implements ClientStream
A logicalClientStream
that is retriable.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static interface
RetriableStream.BufferEntry
(package private) class
RetriableStream.BufferSizeTracer
Traces the buffer used by a substream.(package private) static class
RetriableStream.ChannelBufferMeter
Used to keep track of the total amount of memory used to buffer retryable or hedged RPCs for the Channel.private static class
RetriableStream.FutureCanceller
Allows cancelling a Future without racing with setting the future.private static class
RetriableStream.HedgingPlan
private class
RetriableStream.HedgingRunnable
private static class
RetriableStream.RetryPlan
private static class
RetriableStream.SavedCloseMasterListenerReason
(package private) class
RetriableStream.StartEntry
private static class
RetriableStream.State
private class
RetriableStream.Sublistener
private static class
RetriableStream.Substream
A wrapper of a physical stream of a retry/hedging attempt, that comes with some useful attributes.(package private) static class
RetriableStream.Throttle
Used for retry throttling.
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.Executor
callExecutor
private Status
cancellationStatus
private static Status
CANCELLED_BECAUSE_COMMITTED
private long
channelBufferLimit
private RetriableStream.ChannelBufferMeter
channelBufferUsed
private InsightBuilder
closedSubstreamsInsight
(package private) static Metadata.Key<java.lang.String>
GRPC_PREVIOUS_RPC_ATTEMPTS
(package private) static Metadata.Key<java.lang.String>
GRPC_RETRY_PUSHBACK_MS
private Metadata
headers
private HedgingPolicy
hedgingPolicy
private java.util.concurrent.atomic.AtomicInteger
inFlightSubStreams
private boolean
isClosed
private boolean
isHedging
private java.util.concurrent.Executor
listenerSerializeExecutor
private java.util.concurrent.atomic.AtomicInteger
localOnlyTransparentRetries
private java.lang.Object
lock
Must be held when updating state, accessing state.buffer, or certain substream attributes.private ClientStreamListener
masterListener
private MethodDescriptor<ReqT,?>
method
private long
nextBackoffIntervalNanos
private java.util.concurrent.atomic.AtomicBoolean
noMoreTransparentRetry
Either non-local transparent retry happened or reached server's application logic.private long
perRpcBufferLimit
private long
perRpcBufferUsed
private static java.util.Random
random
private RetryPolicy
retryPolicy
private RetriableStream.SavedCloseMasterListenerReason
savedCloseMasterListenerReason
private java.util.concurrent.ScheduledExecutorService
scheduledExecutorService
private RetriableStream.FutureCanceller
scheduledHedging
private RetriableStream.FutureCanceller
scheduledRetry
private RetriableStream.State
state
private RetriableStream.Throttle
throttle
-
Constructor Summary
Constructors Constructor Description RetriableStream(MethodDescriptor<ReqT,?> method, Metadata headers, RetriableStream.ChannelBufferMeter channelBufferUsed, long perRpcBufferLimit, long channelBufferLimit, java.util.concurrent.Executor callExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService, RetryPolicy retryPolicy, HedgingPolicy hedgingPolicy, RetriableStream.Throttle throttle)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
appendTimeoutInsight(InsightBuilder insight)
Append information that will be included in the locally generated DEADLINE_EXCEEDED errors to the givenInsightBuilder
, in order to tell the user about the state of the stream so that they can better diagnose the cause of the error.void
cancel(Status reason)
Abnormally terminates the stream.private java.lang.Runnable
commit(RetriableStream.Substream winningSubstream)
private void
commitAndRun(RetriableStream.Substream winningSubstream)
Calls commit() and if successful runs the post commit task.private RetriableStream.Substream
createSubstream(int previousAttemptCount, boolean isTransparentRetry)
private void
delayOrExecute(RetriableStream.BufferEntry bufferEntry)
private void
drain(RetriableStream.Substream substream)
void
flush()
Flushes any internally buffered messages to the remote end-point.private void
freezeHedging()
Attributes
getAttributes()
Attributes that the stream holds at the current moment.void
halfClose()
Closes the local side of this stream and flushes any remaining messages.private boolean
hasPotentialHedging(RetriableStream.State state)
Whether there is any potential hedge at the moment.boolean
isReady()
Iftrue
, indicates that the transport is capable of sending additional messages without requiring excessive buffering internally.(package private) abstract ClientStream
newSubstream(Metadata headers, ClientStreamTracer.Factory tracerFactory, int previousAttempts, boolean isTransparentRetry)
Creates a new physical ClientStream that represents a retry/hedging attempt.void
optimizeForDirectExecutor()
Provides a hint that directExecutor is being used by the listener for callbacks to the application.(package private) abstract void
postCommit()
(package private) abstract Status
prestart()
Runs pre-start tasks.private void
pushbackHedging(java.lang.Integer delayMillis)
void
request(int numMessages)
Requests up to the given number of messages from the call to be delivered viaStreamListener.messagesAvailable(StreamListener.MessageProducer)
.private void
safeCloseMasterListener(Status status, ClientStreamListener.RpcProgress progress, Metadata metadata)
(package private) void
sendMessage(ReqT message)
void
setAuthority(java.lang.String authority)
Override the default authority withauthority
.void
setCompressor(Compressor compressor)
Sets the compressor on the framer.void
setDeadline(Deadline deadline)
Sets the effective deadline of the RPC.void
setDecompressorRegistry(DecompressorRegistry decompressorRegistry)
Sets the registry to find a decompressor for the framer.void
setFullStreamDecompression(boolean fullStreamDecompression)
Enables full-stream decompression, allowing the client stream to useGzipInflatingBuffer
to decode inbound GZIP compressed streams.void
setMaxInboundMessageSize(int maxSize)
Sets the max size accepted from the remote endpoint.void
setMaxOutboundMessageSize(int maxSize)
Sets the max size sent to the remote endpoint.void
setMessageCompression(boolean enable)
Enables per-message compression, if an encoding type has been negotiated.(package private) static void
setRandom(java.util.Random random)
void
start(ClientStreamListener listener)
Starts the first PRC attempt.(package private) Metadata
updateHeaders(Metadata originalHeaders, int previousAttemptCount)
Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC.void
writeMessage(java.io.InputStream message)
Do not use it directly.
-
-
-
Field Detail
-
GRPC_PREVIOUS_RPC_ATTEMPTS
static final Metadata.Key<java.lang.String> GRPC_PREVIOUS_RPC_ATTEMPTS
-
GRPC_RETRY_PUSHBACK_MS
static final Metadata.Key<java.lang.String> GRPC_RETRY_PUSHBACK_MS
-
CANCELLED_BECAUSE_COMMITTED
private static final Status CANCELLED_BECAUSE_COMMITTED
-
method
private final MethodDescriptor<ReqT,?> method
-
callExecutor
private final java.util.concurrent.Executor callExecutor
-
listenerSerializeExecutor
private final java.util.concurrent.Executor listenerSerializeExecutor
-
scheduledExecutorService
private final java.util.concurrent.ScheduledExecutorService scheduledExecutorService
-
headers
private final Metadata headers
-
retryPolicy
@Nullable private final RetryPolicy retryPolicy
-
hedgingPolicy
@Nullable private final HedgingPolicy hedgingPolicy
-
isHedging
private final boolean isHedging
-
lock
private final java.lang.Object lock
Must be held when updating state, accessing state.buffer, or certain substream attributes.
-
channelBufferUsed
private final RetriableStream.ChannelBufferMeter channelBufferUsed
-
perRpcBufferLimit
private final long perRpcBufferLimit
-
channelBufferLimit
private final long channelBufferLimit
-
throttle
@Nullable private final RetriableStream.Throttle throttle
-
closedSubstreamsInsight
private final InsightBuilder closedSubstreamsInsight
-
state
private volatile RetriableStream.State state
-
noMoreTransparentRetry
private final java.util.concurrent.atomic.AtomicBoolean noMoreTransparentRetry
Either non-local transparent retry happened or reached server's application logic.Note that local-only transparent retries are unlimited.
-
localOnlyTransparentRetries
private final java.util.concurrent.atomic.AtomicInteger localOnlyTransparentRetries
-
inFlightSubStreams
private final java.util.concurrent.atomic.AtomicInteger inFlightSubStreams
-
savedCloseMasterListenerReason
private RetriableStream.SavedCloseMasterListenerReason savedCloseMasterListenerReason
-
perRpcBufferUsed
private long perRpcBufferUsed
-
masterListener
private ClientStreamListener masterListener
-
scheduledRetry
private RetriableStream.FutureCanceller scheduledRetry
-
scheduledHedging
private RetriableStream.FutureCanceller scheduledHedging
-
nextBackoffIntervalNanos
private long nextBackoffIntervalNanos
-
cancellationStatus
private Status cancellationStatus
-
isClosed
private boolean isClosed
-
random
private static java.util.Random random
-
-
Constructor Detail
-
RetriableStream
RetriableStream(MethodDescriptor<ReqT,?> method, Metadata headers, RetriableStream.ChannelBufferMeter channelBufferUsed, long perRpcBufferLimit, long channelBufferLimit, java.util.concurrent.Executor callExecutor, java.util.concurrent.ScheduledExecutorService scheduledExecutorService, @Nullable RetryPolicy retryPolicy, @Nullable HedgingPolicy hedgingPolicy, @Nullable RetriableStream.Throttle throttle)
-
-
Method Detail
-
commit
@Nullable @CheckReturnValue private java.lang.Runnable commit(RetriableStream.Substream winningSubstream)
-
postCommit
abstract void postCommit()
-
commitAndRun
private void commitAndRun(RetriableStream.Substream winningSubstream)
Calls commit() and if successful runs the post commit task. Post commit task will be non-null for only once. The post commit task cancels other non-winning streams on separate transport threads, thus it must be run on the callExecutor to prevent deadlocks between multiple stream transports.(issues/10314) This method should be called only in subListener callbacks. This guarantees callExecutor schedules tasks before master listener closes, which is protected by the inFlightSubStreams decorative. That is because: For a successful winning stream, other streams won't attempt to close master listener. For a cancelled winning stream (noop), other stream won't attempt to close master listener. For a failed/closed winning stream, the last closed stream closes the master listener, and callExecutor scheduling happens-before that.
-
createSubstream
@Nullable private RetriableStream.Substream createSubstream(int previousAttemptCount, boolean isTransparentRetry)
-
newSubstream
abstract ClientStream newSubstream(Metadata headers, ClientStreamTracer.Factory tracerFactory, int previousAttempts, boolean isTransparentRetry)
Creates a new physical ClientStream that represents a retry/hedging attempt. The returned Client stream is not yet started.
-
updateHeaders
final Metadata updateHeaders(Metadata originalHeaders, int previousAttemptCount)
Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC.
-
drain
private void drain(RetriableStream.Substream substream)
-
prestart
@CheckReturnValue @Nullable abstract Status prestart()
Runs pre-start tasks. Returns the Status of shutdown if the channel is shutdown.
-
start
public final void start(ClientStreamListener listener)
Starts the first PRC attempt.- Specified by:
start
in interfaceClientStream
- Parameters:
listener
- non-null
listener of stream events
-
pushbackHedging
private void pushbackHedging(@Nullable java.lang.Integer delayMillis)
-
cancel
public final void cancel(Status reason)
Description copied from interface:ClientStream
Abnormally terminates the stream. After calling this method, no further messages will be sent or received, however it may still be possible to receive buffered messages for a brief period untilClientStreamListener.closed(io.grpc.Status, io.grpc.internal.ClientStreamListener.RpcProgress, io.grpc.Metadata)
is called. This method may only be called afterClientStream.start(io.grpc.internal.ClientStreamListener)
, but else is safe to be called at any time and multiple times and from any thread.- Specified by:
cancel
in interfaceClientStream
- Parameters:
reason
- must be non-OK
-
delayOrExecute
private void delayOrExecute(RetriableStream.BufferEntry bufferEntry)
-
writeMessage
public final void writeMessage(java.io.InputStream message)
Do not use it directly. UsesendMessage(Object)
instead because we don't use InputStream for buffering.- Specified by:
writeMessage
in interfaceStream
- Parameters:
message
- stream containing the serialized message to be sent
-
sendMessage
final void sendMessage(ReqT message)
-
request
public final void request(int numMessages)
Description copied from interface:Stream
Requests up to the given number of messages from the call to be delivered viaStreamListener.messagesAvailable(StreamListener.MessageProducer)
. No additional messages will be delivered. If the stream has astart()
method, it must be called before requesting messages.
-
flush
public final void flush()
Description copied from interface:Stream
Flushes any internally buffered messages to the remote end-point.
-
isReady
public final boolean isReady()
Description copied from interface:Stream
Iftrue
, indicates that the transport is capable of sending additional messages without requiring excessive buffering internally. Otherwise,StreamListener.onReady()
will be called when it turnstrue
.This is just a suggestion and the application is free to ignore it, however doing so may result in excessive buffering within the transport.
-
optimizeForDirectExecutor
public void optimizeForDirectExecutor()
Description copied from interface:Stream
Provides a hint that directExecutor is being used by the listener for callbacks to the application. No action is required. There is no requirement that this method actually matches the executor used.- Specified by:
optimizeForDirectExecutor
in interfaceStream
-
setCompressor
public final void setCompressor(Compressor compressor)
Description copied from interface:Stream
Sets the compressor on the framer.- Specified by:
setCompressor
in interfaceStream
- Parameters:
compressor
- the compressor to use
-
setFullStreamDecompression
public final void setFullStreamDecompression(boolean fullStreamDecompression)
Description copied from interface:ClientStream
Enables full-stream decompression, allowing the client stream to useGzipInflatingBuffer
to decode inbound GZIP compressed streams.- Specified by:
setFullStreamDecompression
in interfaceClientStream
-
setMessageCompression
public final void setMessageCompression(boolean enable)
Description copied from interface:Stream
Enables per-message compression, if an encoding type has been negotiated. If no message encoding has been negotiated, this is a no-op. By default per-message compression is enabled, but may not have any effect if compression is not enabled on the call.- Specified by:
setMessageCompression
in interfaceStream
-
halfClose
public final void halfClose()
Description copied from interface:ClientStream
Closes the local side of this stream and flushes any remaining messages. After this is called, no further messages may be sent on this stream, but additional messages may be received until the remote end-point is closed. This method may only be called once, and only afterClientStream.start(io.grpc.internal.ClientStreamListener)
.- Specified by:
halfClose
in interfaceClientStream
-
setAuthority
public final void setAuthority(java.lang.String authority)
Description copied from interface:ClientStream
Override the default authority withauthority
. May only be called beforeClientStream.start(io.grpc.internal.ClientStreamListener)
.- Specified by:
setAuthority
in interfaceClientStream
-
setDecompressorRegistry
public final void setDecompressorRegistry(DecompressorRegistry decompressorRegistry)
Description copied from interface:ClientStream
Sets the registry to find a decompressor for the framer. May only be called beforeClientStream.start(io.grpc.internal.ClientStreamListener)
. If the transport does not support compression, this may do nothing.- Specified by:
setDecompressorRegistry
in interfaceClientStream
- Parameters:
decompressorRegistry
- the registry of decompressors for decoding responses
-
setMaxInboundMessageSize
public final void setMaxInboundMessageSize(int maxSize)
Description copied from interface:ClientStream
Sets the max size accepted from the remote endpoint.- Specified by:
setMaxInboundMessageSize
in interfaceClientStream
-
setMaxOutboundMessageSize
public final void setMaxOutboundMessageSize(int maxSize)
Description copied from interface:ClientStream
Sets the max size sent to the remote endpoint.- Specified by:
setMaxOutboundMessageSize
in interfaceClientStream
-
setDeadline
public final void setDeadline(Deadline deadline)
Description copied from interface:ClientStream
Sets the effective deadline of the RPC.- Specified by:
setDeadline
in interfaceClientStream
-
getAttributes
public final Attributes getAttributes()
Description copied from interface:ClientStream
Attributes that the stream holds at the current moment. Thread-safe and can be called at any time, although some attributes are there only after a certain point.- Specified by:
getAttributes
in interfaceClientStream
-
appendTimeoutInsight
public void appendTimeoutInsight(InsightBuilder insight)
Description copied from interface:ClientStream
Append information that will be included in the locally generated DEADLINE_EXCEEDED errors to the givenInsightBuilder
, in order to tell the user about the state of the stream so that they can better diagnose the cause of the error.- Specified by:
appendTimeoutInsight
in interfaceClientStream
-
setRandom
static void setRandom(java.util.Random random)
-
hasPotentialHedging
private boolean hasPotentialHedging(RetriableStream.State state)
Whether there is any potential hedge at the moment. A false return value implies there is absolutely no potential hedge. At least one of the hedges will observe a false return value when calling this method, unless otherwise the rpc is committed.
-
freezeHedging
private void freezeHedging()
-
safeCloseMasterListener
private void safeCloseMasterListener(Status status, ClientStreamListener.RpcProgress progress, Metadata metadata)
-
-