Skip to content

Commit cdb2f2c

Browse files
Ryan DewCopilot
andcommitted
MLE-32953 Make StreamingOutputImpl retry decision reflect handle resendability
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>
1 parent 3c26e65 commit cdb2f2c

3 files changed

Lines changed: 104 additions & 13 deletions

File tree

‎marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ private TemporalDescriptor putPostDocumentImpl(RequestLogger reqlog, String meth
12291229
MediaType mediaType = makeType(requestBldr.build().header(HEADER_CONTENT_TYPE));
12301230
if (value instanceof OutputStreamSender) {
12311231
StreamingOutputImpl sentStream =
1232-
new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType);
1232+
new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType, isResendable);
12331233
requestBldr =
12341234
("put".equals(method)) ?
12351235
requestBldr.put(sentStream) :
@@ -2457,7 +2457,7 @@ private void putPostValueImpl(RequestLogger reqlog, String method,
24572457
RequestBody sentValue;
24582458
if (nextValue instanceof OutputStreamSender) {
24592459
sentValue = new StreamingOutputImpl(
2460-
(OutputStreamSender) nextValue, reqlog, mediaType);
2460+
(OutputStreamSender) nextValue, reqlog, mediaType, handle != null && handle.isResendable());
24612461
} else {
24622462
if (reqlog != null && retryContext.getRetry() == 0) {
24632463
sentValue = new ObjectRequestBody(reqlog.copyContent(nextValue), mediaType);
@@ -2831,7 +2831,7 @@ public <R extends AbstractReadHandle> R putResource(RequestLogger reqlog,
28312831

28322832
Function<Request.Builder, Response> doPutFunction = new Function<Request.Builder, Response>() {
28332833
public Response apply(Request.Builder funcBuilder) {
2834-
return doPut(reqlog, funcBuilder, inputBase.sendContent());
2834+
return doPut(reqlog, funcBuilder, inputBase.sendContent(), isResendable);
28352835
}
28362836
};
28372837
Response response = sendRequestWithRetry(requestBldr, (transaction == null), doPutFunction, resendableConsumer);
@@ -2980,7 +2980,7 @@ public void accept(Boolean resendable) {
29802980
final Object value = inputBase == null ? null : inputBase.sendContent();
29812981
Function<Request.Builder, Response> doPostFunction = new Function<Request.Builder, Response>() {
29822982
public Response apply(Request.Builder funcBuilder) {
2983-
return doPost(reqlog, funcBuilder, value);
2983+
return doPost(reqlog, funcBuilder, value, isResendable);
29842984
}
29852985
};
29862986

@@ -3573,7 +3573,8 @@ private <U extends OkHttpResultIterator> U postIteratedResourceImpl(
35733573
Function<Request.Builder, Response> doPostFunction = requestBuilder -> doPost(
35743574
reqlog,
35753575
requestBuilder.header(HEADER_ACCEPT, multipartMixedWithBoundary()),
3576-
inputBase.sendContent()
3576+
inputBase.sendContent(),
3577+
isResendable
35773578
);
35783579

35793580
Response response = sendRequestWithRetry(requestBldr, (transaction == null), doPostFunction, resendableConsumer);
@@ -3746,13 +3747,18 @@ private Request.Builder makePutWebResource(String path,
37463747
}
37473748

37483749
private Response doPut(RequestLogger reqlog, Request.Builder requestBldr, Object value) {
3750+
// Default to non-resendable for callers that have not determined whether the content can be sent again.
3751+
return doPut(reqlog, requestBldr, value, false);
3752+
}
3753+
3754+
private Response doPut(RequestLogger reqlog, Request.Builder requestBldr, Object value, boolean isResendable) {
37493755
if (value == null) throw new IllegalArgumentException("Resource write with null value");
37503756

37513757
if (isFirstRequest() && isStreaming(value)) pingServerBeforeStreaming(0);
37523758

37533759
MediaType mediaType = makeType(requestBldr.build().header(HEADER_CONTENT_TYPE));
37543760
if (value instanceof OutputStreamSender) {
3755-
requestBldr = requestBldr.put(new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType));
3761+
requestBldr = requestBldr.put(new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType, isResendable));
37563762
} else {
37573763
if (reqlog != null) {
37583764
requestBldr = requestBldr.put(new ObjectRequestBody(reqlog.copyContent(value), mediaType));
@@ -3788,6 +3794,11 @@ private Request.Builder makePostWebResource(String path, RequestParameters param
37883794
}
37893795

37903796
private Response doPost(RequestLogger reqlog, Request.Builder requestBldr, Object value) {
3797+
// Default to non-resendable for callers that have not determined whether the content can be sent again.
3798+
return doPost(reqlog, requestBldr, value, false);
3799+
}
3800+
3801+
private Response doPost(RequestLogger reqlog, Request.Builder requestBldr, Object value, boolean isResendable) {
37913802
if (isFirstRequest() && isStreaming(value)) {
37923803
pingServerBeforeStreaming(0);
37933804
}
@@ -3799,7 +3810,7 @@ private Response doPost(RequestLogger reqlog, Request.Builder requestBldr, Objec
37993810
requestBldr = requestBldr.post((MultipartBody) value);
38003811
} else if (value instanceof OutputStreamSender) {
38013812
requestBldr = requestBldr
3802-
.post(new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType));
3813+
.post(new StreamingOutputImpl((OutputStreamSender) value, reqlog, mediaType, isResendable));
38033814
} else {
38043815
if (reqlog != null) {
38053816
requestBldr = requestBldr.post(new ObjectRequestBody(reqlog.copyContent(value), mediaType));
@@ -4003,7 +4014,7 @@ private <W extends AbstractWriteHandle> boolean addParts(
40034014
Part bodyPart = null;
40044015
if (value instanceof OutputStreamSender) {
40054016
bodyPart = Part.create(partHeaders.build(), new StreamingOutputImpl(
4006-
(OutputStreamSender) value, reqlog, mediaType));
4017+
(OutputStreamSender) value, reqlog, mediaType, handleBase.isResendable()));
40074018
} else {
40084019
if (reqlog != null) {
40094020
bodyPart = Part.create(partHeaders.build(), new ObjectRequestBody(reqlog.copyContent(value), mediaType));
@@ -5291,7 +5302,7 @@ public static RequestBody makeRequestBodyForContent(AbstractWriteHandle content)
52915302
(mimetype != null) ? mimetype : "application/x-unknown-content-type"
52925303
);
52935304
return (content instanceof OutputStreamSender) ?
5294-
new StreamingOutputImpl((OutputStreamSender) content, null, mediaType) :
5305+
new StreamingOutputImpl((OutputStreamSender) content, null, mediaType, handleBase.isResendable()) :
52955306
new ObjectRequestBody(HandleAccessor.sendContent(content), mediaType);
52965307
}
52975308

@@ -5513,7 +5524,7 @@ private RequestBody makeRequestBody(AbstractWriteHandle document) {
55135524
(mimetype != null) ? mimetype : "application/x-unknown-content-type"
55145525
);
55155526
return (document instanceof OutputStreamSender) ?
5516-
new StreamingOutputImpl((OutputStreamSender) document, null, mediaType) :
5527+
new StreamingOutputImpl((OutputStreamSender) document, null, mediaType, handleBase.isResendable()) :
55175528
new ObjectRequestBody(HandleAccessor.sendContent(document), mediaType);
55185529
}
55195530

‎marklogic-client-api/src/main/java/com/marklogic/client/impl/StreamingOutputImpl.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,27 @@ class StreamingOutputImpl extends RequestBody implements RetryableRequestBody {
1818
private OutputStreamSender handle;
1919
private RequestLogger logger;
2020
private MediaType contentType;
21+
private boolean resendable;
2122

23+
/**
24+
* Assumes the wrapped handle cannot be resent, which is the safe default for callers that have not determined
25+
* whether the underlying content can be sent again (e.g. because it is backed by a one-shot stream).
26+
*/
2227
StreamingOutputImpl(OutputStreamSender handle, RequestLogger logger, MediaType contentType) {
28+
this(handle, logger, contentType, false);
29+
}
30+
31+
/**
32+
* @param resendable whether the wrapped handle's content can be written again if the request must be retried;
33+
* should reflect the originating handle's own {@code isResendable()} value rather than
34+
* always assuming the content is one-shot.
35+
*/
36+
StreamingOutputImpl(OutputStreamSender handle, RequestLogger logger, MediaType contentType, boolean resendable) {
2337
super();
2438
this.handle = handle;
2539
this.logger = logger;
2640
this.contentType = contentType;
41+
this.resendable = resendable;
2742
}
2843

2944
@Override
@@ -50,8 +65,11 @@ public void writeTo(BufferedSink sink) throws IOException {
5065

5166
@Override
5267
public boolean isRetryable() {
53-
// Added in 8.0.0; streaming output cannot be retried as the stream is consumed on first write.
54-
return false;
68+
// Added in 8.0.0. Originally always returned false, on the assumption that the wrapped stream is consumed
69+
// on first write. That is not true for handles whose content is fully buffered (e.g. JacksonHandle,
70+
// StringHandle) and which report isResendable() == true; for those, the content can safely be written
71+
// again, so we defer to the resendable flag supplied by the caller instead of hardcoding false.
72+
return resendable;
5573
}
5674

5775
@Override
@@ -60,6 +78,6 @@ public boolean isOneShot() {
6078
// RequestBody (e.g. a MultipartBody part for a Data Services call), OkHttp and RetryIOExceptionInterceptor
6179
// both recognize the outer body as non-retryable too; isRetryable() above is only checked when this is the
6280
// top-level request body and would otherwise be bypassed for a nested streaming part.
63-
return true;
81+
return !resendable;
6482
}
6583
}

‎marklogic-client-api/src/test/java/com/marklogic/client/impl/StreamingOutputImplTest.java‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.util.concurrent.atomic.AtomicInteger;
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
2021
import static org.junit.jupiter.api.Assertions.assertThrows;
2122
import static org.junit.jupiter.api.Assertions.assertTrue;
2223

@@ -81,4 +82,65 @@ void multipartBodyWithInputStreamPartIsNotRetried() {
8182
assertThrows(IOException.class, () -> client.newCall(request).execute());
8283
assertEquals(1, attempts.get());
8384
}
85+
86+
@Test
87+
void resendableStreamingBodyIsRetried() {
88+
StreamingOutputImpl resendableBody = new StreamingOutputImpl(
89+
outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)),
90+
null,
91+
MediaType.get("application/xml"),
92+
true
93+
);
94+
assertTrue(resendableBody.isRetryable(),
95+
"A handle that reports isResendable() == true should allow its StreamingOutputImpl wrapper to be retried.");
96+
assertFalse(resendableBody.isOneShot(),
97+
"A resendable StreamingOutputImpl should not be marked one-shot, so OkHttp permits rewriting its body.");
98+
99+
AtomicInteger attempts = new AtomicInteger();
100+
OkHttpClient client = new OkHttpClient.Builder()
101+
.addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0))
102+
.addInterceptor(chain -> {
103+
if (attempts.incrementAndGet() < 2) {
104+
throw new IOException("unexpected end of stream");
105+
}
106+
return new okhttp3.Response.Builder()
107+
.request(chain.request())
108+
.protocol(okhttp3.Protocol.HTTP_1_1)
109+
.code(200)
110+
.message("OK")
111+
.body(okhttp3.ResponseBody.create("", null))
112+
.build();
113+
})
114+
.build();
115+
Request request = new Request.Builder()
116+
.url("http://localhost/")
117+
.post(resendableBody)
118+
.build();
119+
120+
try (okhttp3.Response response = client.newCall(request).execute()) {
121+
assertEquals(200, response.code());
122+
} catch (IOException e) {
123+
throw new RuntimeException(e);
124+
}
125+
assertEquals(2, attempts.get(),
126+
"The request should have been retried once after the first simulated connection failure.");
127+
}
128+
129+
@Test
130+
void nonResendableHandleInMultipartIsNotRetried() {
131+
// Mirrors how OkHttpServices wraps a non-resendable handle (e.g. ReaderHandle) in a Data Services multipart
132+
// request: the per-part resendable flag should propagate up through MultipartBody.isOneShot().
133+
StreamingOutputImpl nonResendablePart = new StreamingOutputImpl(
134+
outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)),
135+
null,
136+
MediaType.get("application/xml"),
137+
false
138+
);
139+
MultipartBody multipartBody = new MultipartBody.Builder()
140+
.setType(MultipartBody.FORM)
141+
.addFormDataPart("body", null, nonResendablePart)
142+
.build();
143+
assertTrue(multipartBody.isOneShot(),
144+
"A multipart request containing even one non-resendable part must remain one-shot overall.");
145+
}
84146
}

0 commit comments

Comments
 (0)