Package io.grpc.internal
Class RetriableStream<ReqT>
java.lang.Object
io.grpc.internal.RetriableStream<ReqT>
- All Implemented Interfaces:
ClientStream,Stream
A logical
ClientStream that is retriable.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static interface(package private) classTraces the buffer used by a substream.(package private) static final classUsed to keep track of the total amount of memory used to buffer retryable or hedged RPCs for the Channel.private static final classAllows cancelling a Future without racing with setting the future.private static final classprivate final classprivate static final classprivate static final class(package private) classprivate static final classprivate final classprivate static final classA wrapper of a physical stream of a retry/hedging attempt, that comes with some useful attributes.(package private) static final classUsed for retry throttling. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Executorprivate Statusprivate static final Statusprivate final longprivate final RetriableStream.ChannelBufferMeterprivate final InsightBuilder(package private) static final Metadata.Key<String> (package private) static final Metadata.Key<String> private final Metadataprivate final HedgingPolicyprivate final AtomicIntegerprivate booleanprivate final booleanprivate final Executorprivate final AtomicIntegerprivate final ObjectMust be held when updating state, accessing state.buffer, or certain substream attributes.private ClientStreamListenerprivate final MethodDescriptor<ReqT, ?> private longprivate final AtomicBooleanEither non-local transparent retry happened or reached server's application logic.private final longprivate longprivate static Randomprivate final RetryPolicyprivate final ScheduledExecutorServiceprivate RetriableStream.FutureCancellerprivate RetriableStream.FutureCancellerprivate RetriableStream.Stateprivate final RetriableStream.Throttle -
Constructor Summary
ConstructorsConstructorDescriptionRetriableStream(MethodDescriptor<ReqT, ?> method, Metadata headers, RetriableStream.ChannelBufferMeter channelBufferUsed, long perRpcBufferLimit, long channelBufferLimit, Executor callExecutor, ScheduledExecutorService scheduledExecutorService, RetryPolicy retryPolicy, HedgingPolicy hedgingPolicy, RetriableStream.Throttle throttle) -
Method Summary
Modifier and TypeMethodDescriptionvoidappendTimeoutInsight(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.final voidAbnormally terminates the stream.private Runnablecommit(RetriableStream.Substream winningSubstream) private voidcommitAndRun(RetriableStream.Substream winningSubstream) Calls commit() and if successful runs the post commit task.private RetriableStream.SubstreamcreateSubstream(int previousAttemptCount, boolean isTransparentRetry) private voiddelayOrExecute(RetriableStream.BufferEntry bufferEntry) private voiddrain(RetriableStream.Substream substream) final voidflush()Flushes any internally buffered messages to the remote end-point.private voidfinal AttributesAttributes that the stream holds at the current moment.final voidCloses the local side of this stream and flushes any remaining messages.private booleanWhether there is any potential hedge at the moment.final booleanisReady()Iftrue, indicates that the transport is capable of sending additional messages without requiring excessive buffering internally.(package private) abstract ClientStreamnewSubstream(Metadata headers, ClientStreamTracer.Factory tracerFactory, int previousAttempts, boolean isTransparentRetry) Creates a new physical ClientStream that represents a retry/hedging attempt.voidProvides a hint that directExecutor is being used by the listener for callbacks to the application.(package private) abstract void(package private) abstract Statusprestart()Runs pre-start tasks.private voidpushbackHedging(Integer delayMillis) final voidrequest(int numMessages) Requests up to the given number of messages from the call to be delivered viaStreamListener.messagesAvailable(StreamListener.MessageProducer).private voidsafeCloseMasterListener(Status status, ClientStreamListener.RpcProgress progress, Metadata metadata) (package private) final voidsendMessage(ReqT message) final voidsetAuthority(String authority) Override the default authority withauthority.final voidsetCompressor(Compressor compressor) Sets the compressor on the framer.final voidsetDeadline(Deadline deadline) Sets the effective deadline of the RPC.final voidsetDecompressorRegistry(DecompressorRegistry decompressorRegistry) Sets the registry to find a decompressor for the framer.final voidsetFullStreamDecompression(boolean fullStreamDecompression) Enables full-stream decompression, allowing the client stream to useGzipInflatingBufferto decode inbound GZIP compressed streams.final voidsetMaxInboundMessageSize(int maxSize) Sets the max size accepted from the remote endpoint.final voidsetMaxOutboundMessageSize(int maxSize) Sets the max size sent to the remote endpoint.final voidsetMessageCompression(boolean enable) Enables per-message compression, if an encoding type has been negotiated.(package private) static voidfinal voidstart(ClientStreamListener listener) Starts the first PRC attempt.(package private) final MetadataupdateHeaders(Metadata originalHeaders, int previousAttemptCount) Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC.final voidwriteMessage(InputStream message) Do not use it directly.
-
Field Details
-
GRPC_PREVIOUS_RPC_ATTEMPTS
-
GRPC_RETRY_PUSHBACK_MS
-
CANCELLED_BECAUSE_COMMITTED
-
method
-
callExecutor
-
listenerSerializeExecutor
-
scheduledExecutorService
-
headers
-
retryPolicy
-
hedgingPolicy
-
isHedging
private final boolean isHedging -
lock
Must be held when updating state, accessing state.buffer, or certain substream attributes. -
channelBufferUsed
-
perRpcBufferLimit
private final long perRpcBufferLimit -
channelBufferLimit
private final long channelBufferLimit -
throttle
-
closedSubstreamsInsight
-
state
-
noMoreTransparentRetry
Either non-local transparent retry happened or reached server's application logic.Note that local-only transparent retries are unlimited.
-
localOnlyTransparentRetries
-
inFlightSubStreams
-
savedCloseMasterListenerReason
-
perRpcBufferUsed
private long perRpcBufferUsed -
masterListener
-
scheduledRetry
-
scheduledHedging
-
nextBackoffIntervalNanos
private long nextBackoffIntervalNanos -
cancellationStatus
-
isClosed
private boolean isClosed -
random
-
-
Constructor Details
-
RetriableStream
RetriableStream(MethodDescriptor<ReqT, ?> method, Metadata headers, RetriableStream.ChannelBufferMeter channelBufferUsed, long perRpcBufferLimit, long channelBufferLimit, Executor callExecutor, ScheduledExecutorService scheduledExecutorService, @Nullable RetryPolicy retryPolicy, @Nullable HedgingPolicy hedgingPolicy, @Nullable RetriableStream.Throttle throttle)
-
-
Method Details
-
commit
-
postCommit
abstract void postCommit() -
commitAndRun
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
Adds grpc-previous-rpc-attempts in the headers of a retry/hedging RPC. -
drain
-
prestart
Runs pre-start tasks. Returns the Status of shutdown if the channel is shutdown. -
start
Starts the first PRC attempt.- Specified by:
startin interfaceClientStream- Parameters:
listener- non-nulllistener of stream events
-
pushbackHedging
-
cancel
Description copied from interface:ClientStreamAbnormally 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:
cancelin interfaceClientStream- Parameters:
reason- must be non-OK
-
delayOrExecute
-
writeMessage
Do not use it directly. UsesendMessage(Object)instead because we don't use InputStream for buffering.- Specified by:
writeMessagein interfaceStream- Parameters:
message- stream containing the serialized message to be sent
-
sendMessage
-
request
public final void request(int numMessages) Description copied from interface:StreamRequests 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:StreamFlushes any internally buffered messages to the remote end-point. -
isReady
public final boolean isReady()Description copied from interface:StreamIftrue, 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:StreamProvides 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:
optimizeForDirectExecutorin interfaceStream
-
setCompressor
Description copied from interface:StreamSets the compressor on the framer.- Specified by:
setCompressorin interfaceStream- Parameters:
compressor- the compressor to use
-
setFullStreamDecompression
public final void setFullStreamDecompression(boolean fullStreamDecompression) Description copied from interface:ClientStreamEnables full-stream decompression, allowing the client stream to useGzipInflatingBufferto decode inbound GZIP compressed streams.- Specified by:
setFullStreamDecompressionin interfaceClientStream
-
setMessageCompression
public final void setMessageCompression(boolean enable) Description copied from interface:StreamEnables 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:
setMessageCompressionin interfaceStream
-
halfClose
public final void halfClose()Description copied from interface:ClientStreamCloses 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:
halfClosein interfaceClientStream
-
setAuthority
Description copied from interface:ClientStreamOverride the default authority withauthority. May only be called beforeClientStream.start(io.grpc.internal.ClientStreamListener).- Specified by:
setAuthorityin interfaceClientStream
-
setDecompressorRegistry
Description copied from interface:ClientStreamSets 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:
setDecompressorRegistryin interfaceClientStream- Parameters:
decompressorRegistry- the registry of decompressors for decoding responses
-
setMaxInboundMessageSize
public final void setMaxInboundMessageSize(int maxSize) Description copied from interface:ClientStreamSets the max size accepted from the remote endpoint.- Specified by:
setMaxInboundMessageSizein interfaceClientStream
-
setMaxOutboundMessageSize
public final void setMaxOutboundMessageSize(int maxSize) Description copied from interface:ClientStreamSets the max size sent to the remote endpoint.- Specified by:
setMaxOutboundMessageSizein interfaceClientStream
-
setDeadline
Description copied from interface:ClientStreamSets the effective deadline of the RPC.- Specified by:
setDeadlinein interfaceClientStream
-
getAttributes
Description copied from interface:ClientStreamAttributes 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:
getAttributesin interfaceClientStream
-
appendTimeoutInsight
Description copied from interface:ClientStreamAppend 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:
appendTimeoutInsightin interfaceClientStream
-
setRandom
-
hasPotentialHedging
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)
-