-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add full conditional write support to S3 test fixture #138542
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
DaveCTurner
merged 8 commits into
elastic:main
from
DaveCTurner:2025/11/24/s3-fixture-conditional-writes
Nov 26, 2025
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
258602c
Add full conditional write support to S3 test fixture
DaveCTurner 8b14e66
Merge branch 'main' into 2025/11/24/s3-fixture-conditional-writes
DaveCTurner 3b4477f
Rework to return 404 and 409 too
DaveCTurner 5a9eace
Fix comment
DaveCTurner 86363d1
Accept 404 for abort-on-cleanup response
DaveCTurner 185a32e
Merge branch 'main' into 2025/11/24/s3-fixture-conditional-writes
DaveCTurner 0030552
Merge branch 'main' into 2025/11/24/s3-fixture-conditional-writes
DaveCTurner 5c0542c
Merge branch 'main' into 2025/11/24/s3-fixture-conditional-writes
DaveCTurner 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.ConcurrentMap; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
|
|
@@ -248,6 +249,9 @@ public void handle(final HttpExchange exchange) throws IOException { | |
| if (isProtectOverwrite(exchange)) { | ||
| throw new AssertionError("If-None-Match: * header is not supported here"); | ||
| } | ||
| if (getRequiredExistingETag(exchange) != null) { | ||
| throw new AssertionError("If-Match: * header is not supported here"); | ||
| } | ||
|
|
||
| var sourceBlob = blobs.get(copySource); | ||
| if (sourceBlob == null) { | ||
|
|
@@ -412,10 +416,24 @@ public void handle(final HttpExchange exchange) throws IOException { | |
| private boolean updateBlobContents(HttpExchange exchange, String path, BytesReference newContents) { | ||
| if (isProtectOverwrite(exchange)) { | ||
| return blobs.putIfAbsent(path, newContents) == null; | ||
| } else { | ||
| blobs.put(path, newContents); | ||
| return true; | ||
| } | ||
|
|
||
| final var requireExistingETag = getRequiredExistingETag(exchange); | ||
| if (requireExistingETag != null) { | ||
| final var success = new AtomicBoolean(true); | ||
| blobs.compute(path, (ignoredPath, existingContents) -> { | ||
| if (existingContents != null && requireExistingETag.equals(getEtagFromContents(existingContents))) { | ||
| return newContents; | ||
| } | ||
|
|
||
| success.set(false); | ||
| return existingContents; | ||
| }); | ||
| return success.get(); | ||
| } | ||
|
|
||
| blobs.put(path, newContents); | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -594,6 +612,9 @@ private static boolean isProtectOverwrite(final HttpExchange exchange) { | |
| return false; | ||
| } | ||
|
|
||
| if (exchange.getRequestHeaders().get("If-Match") != null) { | ||
| throw new AssertionError("Handling both If-None-Match and If-Match headers is not supported"); | ||
| } | ||
| if (ifNoneMatch.size() != 1) { | ||
| throw new AssertionError("multiple If-None-Match headers found: " + ifNoneMatch); | ||
| } | ||
|
|
@@ -605,6 +626,29 @@ private static boolean isProtectOverwrite(final HttpExchange exchange) { | |
| throw new AssertionError("invalid If-None-Match header: " + ifNoneMatch); | ||
| } | ||
|
|
||
| @Nullable // if no If-Match header found | ||
| private static String getRequiredExistingETag(final HttpExchange exchange) { | ||
| final var ifMatch = exchange.getRequestHeaders().get("If-Match"); | ||
|
|
||
| if (ifMatch == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (exchange.getRequestHeaders().get("If-None-Match") != null) { | ||
| throw new AssertionError("Handling both If-None-Match and If-Match headers is not supported"); | ||
| } | ||
|
|
||
| final var iterator = ifMatch.iterator(); | ||
| if (iterator.hasNext()) { | ||
| final var result = iterator.next(); | ||
| if (iterator.hasNext() == false) { | ||
| return result; | ||
| } | ||
| } | ||
|
Comment on lines
+651
to
+657
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think it should be |
||
|
|
||
| throw new AssertionError("multiple If-Match headers found: " + ifMatch); | ||
| } | ||
|
|
||
| MultipartUpload putUpload(String path) { | ||
| final var upload = new MultipartUpload(UUIDs.randomBase64UUID(), path); | ||
| synchronized (uploads) { | ||
|
|
||
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
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.
If there is no existing object and it's
If-Matchit should return 404. I assume in this case it would return 412 precondition failed.https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html#conditional-error-response
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.
Maybe a test for If-Match for non-existing object.
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.
Ah thanks, well spotted. Bit of an odd choice tbh but I'll try and find a way to match that behaviour.