Repository navigation
feat(client): add retransmitRequestBodies(false) to stop re-sending a request that was already sent #175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+227
−2
Merged
feat(client): add retransmitRequestBodies(false) to stop re-sending a request that was already sent #175
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2f185b
fix(client): make request bodies one-shot
mp-orkes 4a0e1d2
test: reproduce same-route REFUSED_STREAM retransmission against a si…
mp-orkes 547d598
test: update the class javadoc to match what it now covers
mp-orkes 717ba84
fix(client): default retransmitRequestBodies to true, fix test coverage
mp-orkes 759a8d3
fix(client): address review on one-shot default flip
mp-orkes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
...client/src/test/java/com/netflix/conductor/client/http/RequestBodyRetransmissionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| * Copyright 2026 Conductor Authors. | ||
| * <p> | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * <p> | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * <p> | ||
| * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations under the License. | ||
| */ | ||
| package com.netflix.conductor.client.http; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InterruptedIOException; | ||
| import java.net.InetAddress; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import com.netflix.conductor.client.exception.ConductorClientException; | ||
|
|
||
| import okhttp3.Dns; | ||
| import okhttp3.RequestBody; | ||
| import okhttp3.mockwebserver.MockResponse; | ||
| import okhttp3.mockwebserver.MockWebServer; | ||
| import okhttp3.mockwebserver.SocketPolicy; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| /** | ||
| * A request body that is not one-shot can be sent twice: OkHttp retransmits it after a | ||
| * recoverable connection failure, and the caller is then told about the second attempt rather | ||
| * than the first. For a non-idempotent call that means the work happened and the error says it | ||
| * did not. {@code retransmitRequestBodies(false)} opts in to the one-shot protection. | ||
| * | ||
| * <p>The decisive pair below makes one successful call first, so the connection is pooled, then | ||
| * severs the next one: a connection taken from the pool retries on its own address, which is the | ||
| * real-world shape this SDK must handle. The remaining tests pin the one-shot contract directly | ||
| * and check that a failure before the request is sent still falls back to another route - the | ||
| * behaviour that must not be lost in exchange. | ||
| */ | ||
| class RequestBodyRetransmissionTest { | ||
|
|
||
| private MockWebServer server; | ||
| private final List<ConductorClient> clients = new ArrayList<>(); | ||
|
|
||
| @BeforeEach | ||
| void setUp() throws IOException { | ||
| server = new MockWebServer(); | ||
| server.start(InetAddress.getByName("127.0.0.1"), 0); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() throws IOException { | ||
| // Pools are per-client here, but evict explicitly so no pooled connection outlives its server. | ||
| clients.forEach(c -> c.okHttpClient.connectionPool().evictAll()); | ||
| server.shutdown(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("default (retransmitRequestBodies true): a body severed on a pooled connection " | ||
| + "is retransmitted and the call succeeds") | ||
| void retransmitEnabled_bodyIsRetransmittedOnPooledConnection_callSucceeds() { | ||
| server.enqueue(new MockResponse().setBody("{}")); // warm-up: pools the connection | ||
| server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST)); | ||
| server.enqueue(new MockResponse().setBody("{}")); // served only if retransmitted | ||
|
|
||
| var client = newClient(ConductorClient.builder().basePath(basePath()).retransmitRequestBodies(true)); | ||
|
|
||
| // Must run to completion (response body drained) or the connection is never pooled and the retry never fires. | ||
| assertDoesNotThrow(() -> client.execute(warmupRequest())); | ||
|
|
||
| assertDoesNotThrow(() -> client.execute(postRequest()), | ||
| "retransmission must hide the severed attempt from the caller"); | ||
| assertEquals(3, server.getRequestCount(), "warm-up + severed attempt + retransmit"); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("opt-in (retransmitRequestBodies(false)): a body severed on a pooled connection " | ||
| + "is not retransmitted and the call fails") | ||
| void optIn_bodyIsNotRetransmittedOnPooledConnection_callFails() { | ||
| server.enqueue(new MockResponse().setBody("{}")); // warm-up: pools the connection | ||
| server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AFTER_REQUEST)); | ||
| server.enqueue(new MockResponse().setBody("{}")); // must never be reached | ||
|
|
||
| var client = newClient(ConductorClient.builder().basePath(basePath()).retransmitRequestBodies(false)); | ||
|
|
||
| // Must run to completion (response body drained) or the connection is never pooled and the retry never fires. | ||
| assertDoesNotThrow(() -> client.execute(warmupRequest())); | ||
|
|
||
| ConductorClientException e = assertThrows(ConductorClientException.class, () -> client.execute(postRequest())); | ||
|
|
||
| assertEquals(2, server.getRequestCount(), "warm-up + severed attempt, no retransmit"); | ||
| assertInstanceOf(IOException.class, e.getCause(), "got: " + e.getCause()); | ||
| assertFalse(e.getCause() instanceof InterruptedIOException, | ||
| "must be the raw severed-socket failure, not a call timeout masquerading as it: " + e.getCause()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("contract pin: the built request body is one-shot only when opted in " | ||
| + "(retransmitRequestBodies(false))") | ||
| void requestBody_isOneShot_onlyWhenOptedIn() { | ||
| var defaultClient = newClient(ConductorClient.builder().basePath(basePath())); | ||
| var optInClient = newClient(ConductorClient.builder().basePath(basePath()).retransmitRequestBodies(false)); | ||
|
|
||
| assertFalse(builtRequestBody(defaultClient).isOneShot()); | ||
| assertTrue(builtRequestBody(optInClient).isOneShot()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("a pre-send connection failure still falls back to another route, even for a " | ||
| + "one-shot POST body") | ||
| void preSendConnectFailure_fallsBackToAnotherRoute() { | ||
| server.enqueue(new MockResponse().setBody("{}")); | ||
|
|
||
| Dns twoRouteDns = hostname -> List.of( | ||
| InetAddress.getByName("127.0.0.2"), InetAddress.getByName("127.0.0.1")); | ||
|
|
||
| var client = newClient( | ||
| ConductorClient.builder() | ||
| .basePath("http://multi-route.invalid:" + server.getPort() + "/api") | ||
| .retransmitRequestBodies(false) | ||
| .connectTimeout(250) | ||
| .configureOkHttp(b -> b.dns(twoRouteDns))); | ||
|
|
||
| assertDoesNotThrow(() -> client.execute(postRequest()), | ||
| "a one-shot POST must still fall back pre-send: recover() never consults " | ||
| + "requestIsOneShot before the body starts sending"); | ||
| assertEquals(1, server.getRequestCount(), "request must have reached the server via the fallback route"); | ||
| } | ||
|
|
||
| private ConductorClient newClient(ConductorClient.Builder<?> builder) { | ||
| ConductorClient client = new ConductorClient(builder); | ||
| clients.add(client); | ||
| return client; | ||
| } | ||
|
|
||
| private String basePath() { | ||
| return "http://127.0.0.1:" + server.getPort() + "/api"; | ||
| } | ||
|
|
||
| private static ConductorClientRequest warmupRequest() { | ||
| return ConductorClientRequest.builder() | ||
| .method(ConductorClientRequest.Method.GET) | ||
| .path("/workflow") | ||
| .build(); | ||
| } | ||
|
|
||
| private static ConductorClientRequest postRequest() { | ||
| return ConductorClientRequest.builder() | ||
| .method(ConductorClientRequest.Method.POST) | ||
| .path("/workflow") | ||
| .body("{\"name\":\"test\"}") | ||
| .build(); | ||
| } | ||
|
|
||
| private static RequestBody builtRequestBody(ConductorClient client) { | ||
| return client.buildRequest("POST", "/workflow", List.of(), List.of(), Map.of(), "{\"name\":\"test\"}").body(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
relates to orkes-io/jepsen-conductor#6