Interface ChunkedRestResponseBodyPart
- All Known Implementing Classes:
LoggingChunkedRestResponseBodyPart
A body (or a part thereof) of an HTTP response that uses the chunked
transfer-encoding. This allows Elasticsearch to avoid
materializing the full response into on-heap buffers up front, instead serializing only as much of the response as can be flushed to the
network right away.
Each ChunkedRestResponseBodyPart
represents a sequence of chunks that are ready for immediate transmission: if
isPartComplete()
returns false
then encodeChunk(int, org.elasticsearch.common.recycler.Recycler<org.apache.lucene.util.BytesRef>)
can be called at any time and must synchronously return the next
chunk to be sent. Many HTTP responses will be a single part, but if an implementation's isLastPart()
returns false
at the
end of the part then the transmission is paused and getNextPart(org.elasticsearch.action.ActionListener<org.elasticsearch.rest.ChunkedRestResponseBodyPart>)
is called to compute the next sequence of chunks
asynchronously.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionencodeChunk
(int sizeHint, Recycler<org.apache.lucene.util.BytesRef> recycler) Serializes approximately as many bytes of the response as request bysizeHint
to aReleasableBytesReference
that is created from buffers backed by the givenrecycler
.static ChunkedRestResponseBodyPart
fromTextChunks
(String contentType, Iterator<CheckedConsumer<Writer, IOException>> chunkIterator) Create a one-part chunked response body to be written to a specificRestChannel
from a stream of UTF-8-encoded text chunks, each represented as a consumer of aWriter
.static ChunkedRestResponseBodyPart
fromXContent
(ChunkedToXContent chunkedToXContent, ToXContent.Params params, RestChannel channel) Create a one-part chunked response body to be written to a specificRestChannel
from aChunkedToXContent
.void
getNextPart
(ActionListener<ChunkedRestResponseBodyPart> listener) Asynchronously retrieves the next part of the response body.boolean
boolean
-
Field Details
-
logger
-
-
Method Details
-
isPartComplete
boolean isPartComplete()- Returns:
true
if this body part contains no more chunks and the REST layer should check for a possible continuation by callingisLastPart()
, orfalse
if the REST layer should request another chunk from this body usingencodeChunk(int, org.elasticsearch.common.recycler.Recycler<org.apache.lucene.util.BytesRef>)
.
-
isLastPart
boolean isLastPart()- Returns:
true
if this is the last chunked body part in the response, orfalse
if the REST layer should request further chunked bodies by callinggetNextPart(org.elasticsearch.action.ActionListener<org.elasticsearch.rest.ChunkedRestResponseBodyPart>)
.
-
getNextPart
Asynchronously retrieves the next part of the response body. Called if
isLastPart()
returnsfalse
.Note that this is called on a transport thread: implementations must take care to dispatch any nontrivial work elsewhere.
Note that the
Task
corresponding to any invocation ofElasticsearchClient.execute(org.elasticsearch.action.ActionType<Response>, Request)
completes as soon as the client action returns its response, so it no longer exists when this method is called and cannot be used to receive cancellation notifications. Instead, if the HTTP channel is closed while sending a response then the REST layer will invokeRestResponse.close()
. If the HTTP channel is closed while the REST layer is waiting for a continuation then theRestResponse
will not be closed until the continuation listener is completed. Implementations will typically explicitly create aCancellableTask
to represent the computation and transmission of the entireRestResponse
, and will cancel this task if theRestResponse
is closed prematurely.- Parameters:
listener
- Listener to complete with the next part of the body. By the point this is called we have already started to send the body of the response, so there's no good ways to handle an exception here. Completing the listener exceptionally will log an error, abort sending the response, and close the HTTP connection.
-
encodeChunk
ReleasableBytesReference encodeChunk(int sizeHint, Recycler<org.apache.lucene.util.BytesRef> recycler) throws IOException Serializes approximately as many bytes of the response as request bysizeHint
to aReleasableBytesReference
that is created from buffers backed by the givenrecycler
.- Parameters:
sizeHint
- how many bytes to approximately serialize for the given chunkrecycler
- recycler used to acquire buffers- Returns:
- serialized chunk
- Throws:
IOException
- on serialization failure
-
getResponseContentTypeString
String getResponseContentTypeString()- Returns:
- the response Content-Type header value for this response body
-
fromXContent
static ChunkedRestResponseBodyPart fromXContent(ChunkedToXContent chunkedToXContent, ToXContent.Params params, RestChannel channel) throws IOException Create a one-part chunked response body to be written to a specificRestChannel
from aChunkedToXContent
.- Parameters:
chunkedToXContent
- chunked x-content instance to serializeparams
- parameters to use for serializationchannel
- channel the response will be written to- Returns:
- chunked rest response body
- Throws:
IOException
-
fromTextChunks
static ChunkedRestResponseBodyPart fromTextChunks(String contentType, Iterator<CheckedConsumer<Writer, IOException>> chunkIterator) Create a one-part chunked response body to be written to a specificRestChannel
from a stream of UTF-8-encoded text chunks, each represented as a consumer of aWriter
.
-