Skip to content

Commit 045bcc4

Browse files
committed
Revert "Wait until response body or error body received to process request (#4786)"
This reverts commit 0980e94.
1 parent cb20642 commit 045bcc4

File tree

4 files changed

+20
-198
lines changed

4 files changed

+20
-198
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
<rxjava.version>2.2.21</rxjava.version>
120120
<commons-codec.verion>1.15</commons-codec.verion>
121121
<jmh.version>1.29</jmh.version>
122-
<awscrt.version>0.29.2</awscrt.version>
122+
<awscrt.version>0.29.1</awscrt.version>
123123

124124
<!--Test dependencies -->
125125
<junit5.version>5.10.0</junit5.version>

services/s3/src/main/java/software/amazon/awssdk/services/s3/internal/crt/S3CrtResponseHandlerAdapter.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ public final class S3CrtResponseHandlerAdapter implements S3MetaRequestResponseH
4545

4646
private final SimplePublisher<ByteBuffer> responsePublisher = new SimplePublisher<>();
4747

48-
private final SdkHttpResponse.Builder initialHeadersResponse = SdkHttpResponse.builder();
48+
private final SdkHttpResponse.Builder respBuilder = SdkHttpResponse.builder();
4949
private volatile S3MetaRequest metaRequest;
5050

5151
private final PublisherListener<S3MetaRequestProgress> progressListener;
5252

53-
private volatile boolean responseHandlingInitiated;
54-
5553
public S3CrtResponseHandlerAdapter(CompletableFuture<Void> executeFuture,
5654
SdkAsyncHttpResponseHandler responseHandler,
5755
PublisherListener<S3MetaRequestProgress> progressListener) {
@@ -62,17 +60,17 @@ public S3CrtResponseHandlerAdapter(CompletableFuture<Void> executeFuture,
6260

6361
@Override
6462
public void onResponseHeaders(int statusCode, HttpHeader[] headers) {
65-
// Note, we cannot call responseHandler.onHeaders() here because the response status code and headers may not represent
66-
// whether the request has succeeded or not (e.g. if this is for a HeadObject call that CRT calls under the hood). We
67-
// need to rely on onResponseBody/onFinished being called to determine this.
68-
populateSdkHttpResponse(initialHeadersResponse, statusCode, headers);
63+
for (HttpHeader h : headers) {
64+
respBuilder.appendHeader(h.getName(), h.getValue());
65+
}
66+
67+
respBuilder.statusCode(statusCode);
68+
responseHandler.onHeaders(respBuilder.build());
69+
responseHandler.onStream(responsePublisher);
6970
}
7071

7172
@Override
7273
public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
73-
// See reasoning in onResponseHeaders for why we call this here and not there.
74-
initiateResponseHandling(initialHeadersResponse.build());
75-
7674
if (bodyBytesIn == null) {
7775
failResponseHandlerAndFuture(new IllegalStateException("ByteBuffer delivered is null"));
7876
return 0;
@@ -100,10 +98,6 @@ public void onFinished(S3FinishedResponseContext context) {
10098
if (crtCode != CRT.AWS_CRT_SUCCESS) {
10199
handleError(context);
102100
} else {
103-
// onResponseBody() is not invoked for responses with no content, so we may not have invoked
104-
// SdkAsyncHttpResponseHandler#onHeaders yet.
105-
// See also reasoning in onResponseHeaders for why we call this here and not there.
106-
initiateResponseHandling(initialHeadersResponse.build());
107101
onSuccessfulResponseComplete();
108102
}
109103
}
@@ -133,14 +127,10 @@ public void cancelRequest() {
133127

134128
private void handleError(S3FinishedResponseContext context) {
135129
int crtCode = context.getErrorCode();
136-
HttpHeader[] headers = context.getErrorHeaders();
137130
int responseStatus = context.getResponseStatus();
138131
byte[] errorPayload = context.getErrorPayload();
139132

140133
if (isErrorResponse(responseStatus) && errorPayload != null) {
141-
SdkHttpResponse.Builder errorResponse = populateSdkHttpResponse(SdkHttpResponse.builder(),
142-
responseStatus, headers);
143-
initiateResponseHandling(errorResponse.build());
144134
onErrorResponseComplete(errorPayload);
145135
} else {
146136
Throwable cause = context.getCause();
@@ -152,14 +142,6 @@ private void handleError(S3FinishedResponseContext context) {
152142
}
153143
}
154144

155-
private void initiateResponseHandling(SdkHttpResponse response) {
156-
if (!responseHandlingInitiated) {
157-
responseHandlingInitiated = true;
158-
responseHandler.onHeaders(response);
159-
responseHandler.onStream(responsePublisher);
160-
}
161-
}
162-
163145
private void onErrorResponseComplete(byte[] errorPayload) {
164146
responsePublisher.send(ByteBuffer.wrap(errorPayload))
165147
.thenRun(responsePublisher::complete)
@@ -194,17 +176,6 @@ public void onProgress(S3MetaRequestProgress progress) {
194176
this.progressListener.subscriberOnNext(progress);
195177
}
196178

197-
private static SdkHttpResponse.Builder populateSdkHttpResponse(SdkHttpResponse.Builder respBuilder,
198-
int statusCode, HttpHeader[] headers) {
199-
if (headers != null) {
200-
for (HttpHeader h : headers) {
201-
respBuilder.appendHeader(h.getName(), h.getValue());
202-
}
203-
}
204-
respBuilder.statusCode(statusCode);
205-
return respBuilder;
206-
}
207-
208179
private static class NoOpPublisherListener implements PublisherListener<S3MetaRequestProgress> {
209180
}
210181
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/crt/CrtDownloadErrorTest.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/crt/S3CrtResponseHandlerAdapterTest.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2121
import static org.mockito.ArgumentMatchers.any;
22-
import static org.mockito.Mockito.spy;
2322
import static org.mockito.Mockito.times;
2423
import static org.mockito.Mockito.verify;
2524
import static org.mockito.Mockito.when;
@@ -31,6 +30,7 @@
3130
import org.junit.Before;
3231
import org.junit.Test;
3332
import org.junit.runner.RunWith;
33+
import org.mockito.ArgumentCaptor;
3434
import org.mockito.Mock;
3535
import org.mockito.Mockito;
3636
import org.mockito.junit.MockitoJUnitRunner;
@@ -59,7 +59,7 @@ public class S3CrtResponseHandlerAdapterTest {
5959
@Before
6060
public void setup() {
6161
future = new CompletableFuture<>();
62-
sdkResponseHandler = spy(new TestResponseHandler());
62+
sdkResponseHandler = new TestResponseHandler();
6363
responseHandlerAdapter = new S3CrtResponseHandlerAdapter(future,
6464
sdkResponseHandler,
6565
null);
@@ -75,20 +75,17 @@ public void successfulResponse_shouldCompleteFutureSuccessfully() throws Excepti
7575
int statusCode = 200;
7676
responseHandlerAdapter.onResponseHeaders(statusCode, httpHeaders);
7777

78-
stubOnResponseBody();
79-
80-
responseHandlerAdapter.onFinished(stubResponseContext(0, 0, null));
81-
future.get(5, TimeUnit.SECONDS);
82-
8378
SdkHttpResponse actualSdkHttpResponse = sdkResponseHandler.sdkHttpResponse;
8479
assertThat(actualSdkHttpResponse.statusCode()).isEqualTo(statusCode);
8580
assertThat(actualSdkHttpResponse.firstMatchingHeader("foo")).contains("1");
8681
assertThat(actualSdkHttpResponse.firstMatchingHeader("bar")).contains("2");
82+
stubOnResponseBody();
8783

84+
responseHandlerAdapter.onFinished(stubResponseContext(0, 0, null));
85+
future.get(5, TimeUnit.SECONDS);
8886
assertThat(future).isCompleted();
8987
verify(s3MetaRequest, times(2)).incrementReadWindow(11L);
9088
verify(s3MetaRequest).close();
91-
verify(sdkResponseHandler).onHeaders(any(SdkHttpResponse.class));
9289
}
9390

9491
@Test
@@ -106,25 +103,24 @@ public void nullByteBuffer_shouldCompleteFutureExceptionally() {
106103
+ "null");
107104
assertThat(future).isCompletedExceptionally();
108105
verify(s3MetaRequest).close();
109-
verify(sdkResponseHandler).onHeaders(any(SdkHttpResponse.class));
110106
}
111107

112108
@Test
113109
public void errorResponse_shouldCompleteFutureSuccessfully() {
114110
int statusCode = 400;
115111
responseHandlerAdapter.onResponseHeaders(statusCode, new HttpHeader[0]);
116112

117-
byte[] errorPayload = "errorResponse".getBytes(StandardCharsets.UTF_8);
118-
stubOnResponseBody();
119-
responseHandlerAdapter.onFinished(stubResponseContext(1, statusCode, errorPayload));
120-
121113
SdkHttpResponse actualSdkHttpResponse = sdkResponseHandler.sdkHttpResponse;
122114
assertThat(actualSdkHttpResponse.statusCode()).isEqualTo(400);
123115
assertThat(actualSdkHttpResponse.headers()).isEmpty();
124116

117+
byte[] errorPayload = "errorResponse".getBytes(StandardCharsets.UTF_8);
118+
stubOnResponseBody();
119+
120+
responseHandlerAdapter.onFinished(stubResponseContext(1, statusCode, errorPayload));
121+
125122
assertThat(future).isCompleted();
126123
verify(s3MetaRequest).close();
127-
verify(sdkResponseHandler).onHeaders(any(SdkHttpResponse.class));
128124
}
129125

130126
@Test
@@ -168,7 +164,7 @@ private void stubOnResponseBody() {
168164
responseHandlerAdapter.onResponseBody(ByteBuffer.wrap("helloworld2".getBytes()), 1, 2);
169165
}
170166

171-
private static class TestResponseHandler implements SdkAsyncHttpResponseHandler {
167+
private static final class TestResponseHandler implements SdkAsyncHttpResponseHandler {
172168
private SdkHttpResponse sdkHttpResponse;
173169
private Throwable error;
174170
@Override

0 commit comments

Comments
 (0)