Skip to content

Commit ca89288

Browse files
committed
Must synchronize entire complete-upload process
1 parent d92092f commit ca89288

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

test/fixtures/s3-fixture/src/main/java/fixture/s3/S3HttpHandler.java

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
import static org.elasticsearch.test.fixture.HttpHeaderParser.parseRangeHeader;
5353
import static org.junit.Assert.assertEquals;
5454
import static org.junit.Assert.assertNotNull;
55+
import static org.junit.Assert.assertNull;
5556
import static org.w3c.dom.Node.ELEMENT_NODE;
5657

5758
/**
@@ -187,39 +188,42 @@ public void handle(final HttpExchange exchange) throws IOException {
187188
}
188189

189190
} else if (request.isCompleteMultipartUploadRequest()) {
190-
final var upload = removeUpload(request.getQueryParamOnce("uploadId"));
191-
if (upload == null) {
192-
if (Randomness.get().nextBoolean()) {
193-
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
191+
final byte[] responseBody;
192+
synchronized (uploads) {
193+
final var upload = removeUpload(request.getQueryParamOnce("uploadId"));
194+
if (upload == null) {
195+
if (Randomness.get().nextBoolean()) {
196+
responseBody = null;
197+
} else {
198+
responseBody = """
199+
<?xml version="1.0" encoding="UTF-8"?>
200+
<Error>
201+
<Code>NoSuchUpload</Code>
202+
<Message>No such upload</Message>
203+
<RequestId>test-request-id</RequestId>
204+
<HostId>test-host-id</HostId>
205+
</Error>""".getBytes(StandardCharsets.UTF_8);
206+
}
194207
} else {
195-
byte[] response = ("""
196-
<?xml version="1.0" encoding="UTF-8"?>
197-
<Error>
198-
<Code>NoSuchUpload</Code>
199-
<Message>No such upload</Message>
200-
<RequestId>test-request-id</RequestId>
201-
<HostId>test-host-id</HostId>
202-
</Error>""").getBytes(StandardCharsets.UTF_8);
203-
exchange.getResponseHeaders().add("Content-Type", "application/xml");
204-
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
205-
exchange.getResponseBody().write(response);
208+
final var blobContents = upload.complete(extractPartEtags(Streams.readFully(exchange.getRequestBody())));
209+
blobs.put(request.path(), blobContents);
210+
responseBody = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
211+
+ "<CompleteMultipartUploadResult>\n"
212+
+ "<Bucket>"
213+
+ bucket
214+
+ "</Bucket>\n"
215+
+ "<Key>"
216+
+ request.path()
217+
+ "</Key>\n"
218+
+ "</CompleteMultipartUploadResult>").getBytes(StandardCharsets.UTF_8);
206219
}
220+
}
221+
if (responseBody == null) {
222+
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
207223
} else {
208-
final var blobContents = upload.complete(extractPartEtags(Streams.readFully(exchange.getRequestBody())));
209-
blobs.put(request.path(), blobContents);
210-
211-
byte[] response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
212-
+ "<CompleteMultipartUploadResult>\n"
213-
+ "<Bucket>"
214-
+ bucket
215-
+ "</Bucket>\n"
216-
+ "<Key>"
217-
+ request.path()
218-
+ "</Key>\n"
219-
+ "</CompleteMultipartUploadResult>").getBytes(StandardCharsets.UTF_8);
220224
exchange.getResponseHeaders().add("Content-Type", "application/xml");
221-
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
222-
exchange.getResponseBody().write(response);
225+
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), responseBody.length);
226+
exchange.getResponseBody().write(responseBody);
223227
}
224228
} else if (request.isAbortMultipartUploadRequest()) {
225229
final var upload = removeUpload(request.getQueryParamOnce("uploadId"));
@@ -524,7 +528,7 @@ private static HttpHeaderParser.Range parsePartRange(final HttpExchange exchange
524528
MultipartUpload putUpload(String path) {
525529
final var upload = new MultipartUpload(UUIDs.randomBase64UUID(), path);
526530
synchronized (uploads) {
527-
uploads.put(upload.getUploadId(), upload);
531+
assertNull("upload " + upload.getUploadId() + " should not exist", uploads.put(upload.getUploadId(), upload));
528532
return upload;
529533
}
530534
}

0 commit comments

Comments
 (0)