Skip to content

MLE-32953 Send Connection: close on gzipped rows requests - #1995

Draft
prgs-rsingh wants to merge 3 commits into
developfrom
MLE-32953-connection-close
Draft

prgs-rsingh wants to merge 3 commits into
developfrom
MLE-32953-connection-close

Conversation

@prgs-rsingh

@prgs-rsingh prgs-rsingh commented Nov 24, 2026 •

Copy link
Copy Markdown

Problem

A Data Services call with a streaming parameter (for example a Reader) fails intermittently when it follows a Row Manager query.

MarkLogic answers a request that has both TE: trailers and Accept-Encoding: gzip with a chunked, gzipped response marked Connection: Keep-Alive, and then closes the connection. Every /v1/rows call from this client sends both headers. OkHttp puts the dead connection back in its pool, so the next request on it fails. A request with a one-shot body cannot be resent, so the call fails.

This is a MarkLogic server bug, raised separately. The change here is a client workaround until the server is fixed.

Changes

  • ChunkedGzipConnectionCloseInterceptor adds Connection: close to HTTP/1.1 requests that have both headers, so OkHttp does not pool the connection. The server closes that connection anyway, so no connection reuse is lost. It is a network interceptor so that it sees the headers that are actually sent, including when gzip is disabled with RemoveAcceptEncodingConfigurator.
  • Every request body is built by toRequestBody, so isOneShot() always reflects the handle's isResendable(), also inside a MultipartBody. OkHttp and RetryIOExceptionInterceptor then never resend a consumed body, and the real failure is reported instead of Stream closed. RetryableRequestBody is deprecated; RetryIOExceptionInterceptor still honors it for bodies that do not override isOneShot().
  • With digest authentication, a body-less ping takes the 401 challenge before the first one-shot request on a thread. This is now done in sendRequestOnce, so it covers every call path. If the server is still unavailable after the ping retries, the request fails with a FailedRetryException instead of sending a body that would be lost.

Tests

  • StreamingPartRequestTest (mock server): a streaming request after a rows request succeeds; the connection is kept when gzip is disabled; a failed streaming request is not resent; digest authentication pings only before the first streaming request; a streaming request is not sent when the pings find the server unavailable.
  • ChunkedGzipConnectionCloseInterceptorTest (mock server): an HTTP/1.1 connection is not reused, an HTTP/2 connection is.
  • StreamingOutputImplTest: isOneShot() for each handle type and for multipart bodies; a body that only implements the deprecated RetryableRequestBody is still not retried.

Notes

Ryan Dew and others added 2 commits September 23, 2026 21:10
…ndability

StreamingOutputImpl previously hardcoded isRetryable()=false / isOneShot()=true
for every wrapped OutputStreamSender, even when the originating write handle
was fully buffered and resendable (e.g. JacksonHandle, StringHandle). This
meant any request body built from such a handle -- including Row Manager plan
execution, document writes, and Data Services parameters -- was needlessly
blocked from being retried by RetryIOExceptionInterceptor after a transient
connection error, even though the content could safely be written again.

Thread each call site's already-known isResendable()/handleBase.isResendable()
value into StreamingOutputImpl instead of assuming non-resendable:
- writeDocumentImpl-style document writes
- putPostValueImpl
- postResource / putResource (used by RowManagerImpl for Optic plan execution)
- postIteratedResourceImpl (multipart-mixed eval/invoke style resources)
- addParts multipart builder
- makeRequestBodyForContent and BaseProxy's makeRequestBody (Data Services
  single-node parameters)

Call sites without an existing resendable concept (structured query search,
alert match) keep the prior conservative false/one-shot default via new 3-arg
doPost/doPut overloads, so behavior is unchanged where resendability was
never determined.

Added StreamingOutputImplTest cases covering: a resendable body being retried
after a simulated connection failure, and a non-resendable part inside a
MultipartBody still propagating isOneShot()=true so it is never retried.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change ignores legacy retry opt-outs and uploads one-shot bodies after unsuccessful preflight retries, risking resends of consumed content and ResourceNotResendableException failures.

2 open issues
What changed in this PR

This PR addresses connection reuse and one-shot request handling in the core Java client, aiming to prevent failed uploads after MarkLogic closes a gzip-enabled rows connection.

Changes:

  • Adds a network interceptor that requests connection closure for gzip responses with trailers.
  • Uses OkHttp’s isOneShot() contract for retry decisions and centralizes digest-authentication preflight pings.
  • Closes buffered multipart responses promptly and adds streaming-request regression tests.
File Description
marklogic-client-api/​src/​test/​java/​com/​marklogic/​client/​impl/​StreamingPartRequestTest.java Adds connection reuse, streaming failure, and digest-authentication tests.
marklogic-client-api/​src/​test/​java/​com/​marklogic/​client/​impl/​StreamingOutputImplTest.java Tests handle resendability and multipart retry behavior.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​StreamingOutputImpl.java Reports one-shot status from handle resendability.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​OkHttpServices.java Centralizes body construction and preflight pings; closes buffered responses.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​RetryIOExceptionInterceptor.java Uses one-shot status to bypass retries.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​RetryableRequestBody.java Deprecates the legacy retry contract.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​ChunkedGzipConnectionCloseInterceptor.java Adds the gzip/trailers connection-close workaround.

Review effort: Balanced

RequestBody requestBody = request.body();

if (request.body() instanceof RetryableRequestBody body && !body.isRetryable()) {
if (requestBody != null && requestBody.isOneShot()) {
pingDelay = pingServer(baseUri, "ping", 0);
}
if (pingDelay > 0) {
logger.warn("Server still unavailable after {} ping attempts before sending a one-shot request", maxPingRetries);
@prgs-rsingh
prgs-rsingh force-pushed the MLE-32953-connection-close branch from a41b4a6 to d100c10 Compare November 24, 2026 04:41
@prgs-rsingh prgs-rsingh changed the title Mle 32953 connection close MLE-32953 Send Connection: close on gzipped rows requests Nov 24, 2026
MarkLogic answers a request that has both "TE: trailers" and
"Accept-Encoding: gzip" (every /v1/rows call) with a chunked, gzipped
response marked "Connection: Keep-Alive", and then closes the
connection. OkHttp reuses that dead connection, so a following request
with a one-shot body (e.g. a Reader) fails and cannot be resent. A Data
Services call with a streaming parameter, made right after
RowManager.resultRows(), failed this way with "Stream closed".

- ChunkedGzipConnectionCloseInterceptor adds "Connection: close" to
  such requests on HTTP/1.1, so OkHttp does not pool the connection.
  This is a workaround until the server bug is fixed.
- Every request body is built by toRequestBody, so isOneShot() always
  reflects the handle's isResendable(), also inside a MultipartBody.
  OkHttp and RetryIOExceptionInterceptor then never resend a consumed
  body. RetryableRequestBody is deprecated; RetryIOExceptionInterceptor
  still honors it for bodies that do not override isOneShot().
- With digest authentication, a body-less ping takes the 401 challenge
  before the first one-shot request on a thread. This is now done in
  sendRequestOnce, so it covers every call path. If the server is still
  unavailable after the ping retries, the request fails with a
  FailedRetryException instead of sending a body that would be lost.

StreamingPartRequestTest and ChunkedGzipConnectionCloseInterceptorTest
use a mock server. StreamingOutputImplTest checks isOneShot() for each
handle type.
@prgs-rsingh
prgs-rsingh force-pushed the MLE-32953-connection-close branch from d100c10 to 10c78a5 Compare November 24, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants