-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Resolve GCS-fixture batch-delete inconsistencies #139166
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
Open
mhl-b
wants to merge
2
commits into
elastic:main
Choose a base branch
from
mhl-b:gcs-batch-fixture-fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+369
−209
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
253 changes: 253 additions & 0 deletions
253
test/fixtures/gcs-fixture/src/main/java/fixture/gcs/MultipartContent.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,253 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package fixture.gcs; | ||
|
|
||
| import com.sun.net.httpserver.HttpExchange; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.regex.Pattern; | ||
| import java.util.zip.GZIPInputStream; | ||
|
|
||
| /** | ||
| * Multipart content | ||
| * | ||
| * Every part has own headers and content. Parts are separated by dash-boundary(--boundary) delimiter, | ||
| * and boundary is defined in the HTTP header Content-Type, | ||
| * like this {@code multipart/related; boundary=__END_OF_PART__4914cd49-4065-44f6-9846-ce805fe1e77f__}. | ||
| * Last part, close-delimiter, is dashed from both sides {@code --boundary--}. | ||
| * Part headers are separated from the content by double CRLF. | ||
| * More details here <a href=https://www.rfc-editor.org/rfc/rfc2046.html#page-19>rfc2046</a>. | ||
| * | ||
| * <pre> | ||
| * {@code | ||
| * --boundary CRLF // with headers and content | ||
| * header: value CRLF | ||
| * header: value CRLF | ||
| * CRLF | ||
| * content CRLF // no headers | ||
| * --boundary CRLF | ||
| * CRLF | ||
| * content CRLF | ||
| * --boundary CRLF // no headers and no content | ||
| * CRLF | ||
| * --boundary-- | ||
| * } | ||
| * </pre> | ||
| */ | ||
| public class MultipartContent { | ||
| static final byte[] CRLF = new byte[] { '\r', '\n' }; | ||
| static final byte[] DOUBLE_DASH = new byte[] { '-', '-' }; | ||
|
|
||
| record Part(Map<String, String> headers, BytesReference content) { | ||
| public static Part of(Map<String, String> headers, String content) { | ||
| return new Part(headers, new BytesArray(content)); | ||
| } | ||
| } | ||
|
|
||
| static class Writer { | ||
| private final OutputStream output; | ||
| private final byte[] boundary; | ||
| private boolean done; | ||
|
|
||
| Writer(String boundary, OutputStream output) { | ||
| this.output = output; | ||
| this.boundary = boundary.getBytes(StandardCharsets.UTF_8); | ||
| this.done = false; | ||
| } | ||
|
|
||
| void write(Part part) throws IOException { | ||
| if (done) { | ||
| throw new IllegalStateException("cannot write part after close-delimiter"); | ||
| } | ||
| output.write(DOUBLE_DASH); | ||
| output.write(boundary); | ||
| output.write(CRLF); | ||
| for (var header : part.headers.entrySet()) { | ||
| output.write(header.getKey().getBytes(StandardCharsets.UTF_8)); | ||
| output.write(": ".getBytes(StandardCharsets.UTF_8)); | ||
| output.write(header.getValue().getBytes(StandardCharsets.UTF_8)); | ||
| output.write(CRLF); | ||
| } | ||
| output.write(CRLF); | ||
| if (part.content != null && part.content.length() > 0) { | ||
| var ref = part.content.toBytesRef(); | ||
| output.write(ref.bytes, ref.offset, ref.length); | ||
| output.write(CRLF); | ||
| } | ||
| } | ||
|
|
||
| void end() throws IOException { | ||
| output.write(DOUBLE_DASH); | ||
| output.write(boundary); | ||
| output.write(DOUBLE_DASH); | ||
| output.close(); | ||
| done = true; | ||
| } | ||
| } | ||
|
|
||
| static class Reader implements Iterator<Part> { | ||
|
|
||
| static final Pattern BOUNDARY_HEADER_PATTERN = Pattern.compile("multipart/\\w+; boundary=\\\"?(.*)\\\"?"); | ||
|
|
||
| private final InputStream input; | ||
| private final byte[] delimiter; | ||
| private boolean done; | ||
|
|
||
| Reader(byte[] delimiter, InputStream input) { | ||
| this.input = input; | ||
| this.delimiter = delimiter; | ||
| } | ||
|
|
||
| public static Reader readGzipStream(HttpExchange exchange, InputStream gzipInput) throws IOException { | ||
| return readGzipStream(getBoundary(exchange), gzipInput); | ||
| } | ||
|
|
||
| public static Reader readGzipStream(String boundary, InputStream gzipInput) throws IOException { | ||
| return readStream(boundary, new GZIPInputStream(gzipInput)); | ||
| } | ||
|
|
||
| public static String getBoundary(HttpExchange exchange) { | ||
| var m = BOUNDARY_HEADER_PATTERN.matcher(exchange.getRequestHeaders().getFirst("Content-Type")); | ||
| if (m.matches() == false) { | ||
| throw new IllegalStateException("boundary header is not present"); | ||
| } | ||
| return m.group(1); | ||
| } | ||
|
|
||
| // for testing | ||
| static Reader readStream(String boundary, InputStream input) throws IOException { | ||
| byte[] dashBoundary = ("--" + boundary).getBytes(); | ||
| // read first boundary | ||
| skipUntilDelimiter(input, dashBoundary); | ||
| if (readCloseDelimiterOrCRLF(input)) { | ||
| throw new IllegalStateException("multipart content must have at least one part"); | ||
| } | ||
| var delimiter = ("\r\n--" + boundary).getBytes(); | ||
| return new Reader(delimiter, input); | ||
| } | ||
|
|
||
| /** | ||
| * Must call after reading body-part-delimiter to see if there are more parts. | ||
| * If there are no parts, a closing double dash is expected, otherwise CRLF. | ||
| */ | ||
| static boolean readCloseDelimiterOrCRLF(InputStream is) throws IOException { | ||
| var d1 = is.read(); | ||
| var d2 = is.read(); | ||
| if (d1 == '-' && d2 == '-') { | ||
| return true; | ||
| } else if (d1 == '\r' && d2 == '\n') { | ||
| return false; | ||
| } else { | ||
| throw new IllegalStateException("expect '--' or CRLF, got " + d1 + " " + d2); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Read bytes from stream into buffer until reach given delimiter. The delimiter is consumed too. | ||
| */ | ||
| static BytesReference readUntilDelimiter(InputStream is, byte[] delimiter) throws IOException { | ||
| assert delimiter.length > 0; | ||
| var out = new ByteArrayOutputStream(1024); | ||
| var delimiterMatchLen = 0; | ||
| while (true) { | ||
| var c = is.read(); | ||
| if (c == -1) { | ||
| throw new IllegalStateException("expected delimiter, but reached end of stream "); | ||
| } | ||
| var b = (byte) c; | ||
| out.write(b); | ||
| if (delimiter[delimiterMatchLen] == b) { | ||
| delimiterMatchLen++; | ||
| if (delimiterMatchLen >= delimiter.length) { | ||
| var bytes = out.toByteArray(); | ||
| return new BytesArray(bytes, 0, bytes.length - delimiter.length); | ||
| } | ||
| } else { | ||
| if (delimiter[0] == b) { | ||
| delimiterMatchLen = 1; | ||
| } else { | ||
| delimiterMatchLen = 0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Discard bytes from stream until reach given delimiter. The delimiter is consumed too. | ||
| */ | ||
| static void skipUntilDelimiter(InputStream is, byte[] delimiter) throws IOException { | ||
| assert delimiter.length > 0; | ||
| var delimiterMatchLen = 0; | ||
| while (true) { | ||
| var c = is.read(); | ||
| if (c == -1) { | ||
| throw new IllegalStateException("expected delimiter, but reached end of stream "); | ||
| } | ||
| var b = (byte) c; | ||
| if (delimiter[delimiterMatchLen] == b) { | ||
| delimiterMatchLen++; | ||
| if (delimiterMatchLen >= delimiter.length) { | ||
| return; | ||
| } | ||
| } else { | ||
| if (delimiter[0] == b) { | ||
| delimiterMatchLen = 1; | ||
| } else { | ||
| delimiterMatchLen = 0; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasNext() { | ||
| return done == false; | ||
| } | ||
|
|
||
| @Override | ||
| public Part next() { | ||
| if (done) { | ||
| return null; | ||
| } | ||
| try { | ||
| final var partBytes = readUntilDelimiter(input, delimiter); | ||
| done = readCloseDelimiterOrCRLF(input); | ||
| if (partBytes.length() == 0) { | ||
| return new Part(Map.of(), BytesArray.EMPTY); | ||
| } | ||
|
|
||
| var partStream = partBytes.streamInput(); | ||
|
|
||
| final var headers = new HashMap<String, String>(); | ||
| BytesReference headerLine; | ||
| while ((headerLine = readUntilDelimiter(partStream, CRLF)).length() > 0) { | ||
| final var split = headerLine.utf8ToString().split(": ", 2); | ||
| headers.put(split[0].toLowerCase(Locale.ROOT), split[1]); | ||
| } | ||
|
|
||
| return new Part(headers, new BytesArray(partStream.readAllBytes())); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
I wonder if we should
maybeDieOnAnotherThreadif we see what we consider to be a malformed request here. If we just throw an exception here it might retry around the issue somehow and we don't know we've got some issue with our stub.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.
Thank you, I will add it to the following PR.