-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Introduce Interfaces to Support atomic conditional-writes #18064
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
x-INFiN1TY-x
wants to merge
5
commits into
opensearch-project:main
Choose a base branch
from
x-INFiN1TY-x:pr-interfaces
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99e6943
PR Interfaces: Add two new method signatures to BlobContainer
tqranjan 6461fe0
Javadoc update
tqranjan 96c0587
WIP: saving in-progress work on pr-interfaces
tqranjan 766204d
Javadoc update
tqranjan a928b49
Javadoc Update
tqranjan 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
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
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
server/src/main/java/org/opensearch/common/blobstore/ConditionalWrite.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 @@ | ||
|
|
||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.common.blobstore; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| /** | ||
| * Utility classes supporting conditional write operations on a {@link BlobContainer}. | ||
| * The main entry points are {@link ConditionalWriteOptions} for specifying conditions, and | ||
| * {@link ConditionalWriteResponse} for receiving the result of a conditional write. | ||
| */ | ||
| public final class ConditionalWrite { | ||
| private ConditionalWrite() {} | ||
|
|
||
| /** | ||
| * Encapsulates options controlling preconditions to be deployed when a blob is to be written to the remote store. | ||
| * Immutable and thread-safe. Use the provided static factory methods or the {@link Builder} | ||
| * to construct instances with the desired conditions. These options can be supplied to | ||
| * blob store write operations to enforce preconditions | ||
| * | ||
| */ | ||
| public static final class ConditionalWriteOptions { | ||
|
|
||
| private final boolean ifNotExists; | ||
| private final boolean ifMatch; | ||
| private final boolean ifUnmodifiedSince; | ||
| private final String versionIdentifier; | ||
| private final Instant unmodifiedSince; | ||
|
|
||
| private ConditionalWriteOptions(Builder builder) { | ||
| this.ifNotExists = builder.ifNotExists; | ||
| this.ifMatch = builder.ifMatch; | ||
| this.ifUnmodifiedSince = builder.ifUnmodifiedSince; | ||
| this.versionIdentifier = builder.versionIdentifier; | ||
| this.unmodifiedSince = builder.unmodifiedSince; | ||
| } | ||
|
|
||
| public static ConditionalWriteOptions none() { | ||
| return new Builder().build(); | ||
| } | ||
|
|
||
| public static ConditionalWriteOptions ifNotExists() { | ||
| return new Builder().setIfNotExists(true).build(); | ||
| } | ||
|
|
||
| public static ConditionalWriteOptions ifMatch(String versionIdentifier) { | ||
| return new Builder().setIfMatch(true).setVersionIdentifier(versionIdentifier).build(); | ||
| } | ||
|
|
||
| public static ConditionalWriteOptions ifUnmodifiedSince(Instant ts) { | ||
| return new Builder().setIfUnmodifiedSince(true).setUnmodifiedSince(ts).build(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a new {@link Builder} for constructing custom conditional write options. | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public boolean isIfNotExists() { | ||
| return ifNotExists; | ||
| } | ||
|
|
||
| public boolean isIfMatch() { | ||
| return ifMatch; | ||
| } | ||
|
|
||
| public boolean isIfUnmodifiedSince() { | ||
| return ifUnmodifiedSince; | ||
| } | ||
|
|
||
| public String getVersionIdentifier() { | ||
| return versionIdentifier; | ||
| } | ||
|
|
||
| public Instant getUnmodifiedSince() { | ||
| return unmodifiedSince; | ||
| } | ||
|
|
||
| /** | ||
| * Builder for {@link ConditionalWriteOptions}. | ||
| * Allows fine-grained construction of conditional write criteria. | ||
| */ | ||
| public static final class Builder { | ||
| private boolean ifNotExists = false; | ||
| private boolean ifMatch = false; | ||
| private boolean ifUnmodifiedSince = false; | ||
| private String versionIdentifier = null; | ||
| private Instant unmodifiedSince = null; | ||
|
|
||
| private Builder() {} | ||
|
|
||
| /** | ||
| * Sets the write to succeed only if the blob does not exist. | ||
| * @param flag true to enable this condition | ||
| * @return this builder | ||
| */ | ||
| public Builder setIfNotExists(boolean flag) { | ||
| this.ifNotExists = flag; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the write to succeed only if the blob matches the expected version. | ||
| * @param flag true to enable this condition | ||
| * @return this builder | ||
| */ | ||
| public Builder setIfMatch(boolean flag) { | ||
| this.ifMatch = flag; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the write to succeed only if the blob was not modified since a given instant. | ||
| * @param flag true to enable this condition | ||
| * @return this builder | ||
| */ | ||
| public Builder setIfUnmodifiedSince(boolean flag) { | ||
| this.ifUnmodifiedSince = flag; | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the timestamp before which the blob must remain unmodified. | ||
| * @param ts the instant to check | ||
| * @return this builder | ||
| */ | ||
| public Builder setUnmodifiedSince(Instant ts) { | ||
| this.unmodifiedSince = ts; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setVersionIdentifier(String versionIdentifier) { | ||
| this.versionIdentifier = versionIdentifier; | ||
| return this; | ||
| } | ||
|
|
||
| public ConditionalWriteOptions build() { | ||
| return new ConditionalWriteOptions(this); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * encapsulates the result of a conditional write operation. | ||
| * Contains the new version identifier (such as an ETag or version string) retrieved from the remote store | ||
| * after a successful write. | ||
| */ | ||
| public static final class ConditionalWriteResponse { | ||
| private final String newVersionIdentifier; | ||
|
|
||
| private ConditionalWriteResponse(String versionIdentifier) { | ||
| this.newVersionIdentifier = versionIdentifier; | ||
| } | ||
|
|
||
| public static ConditionalWriteResponse success(String versionIdentifier) { | ||
| return new ConditionalWriteResponse(versionIdentifier); | ||
| } | ||
|
|
||
| public String getVersionIdentifier() { | ||
| return newVersionIdentifier; | ||
| } | ||
| } | ||
| } | ||
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.
Can there be case where the conditionalWriteOptions has multiple conditions and are contradicting? If yes do we need to have validation for same?
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.
All the Concerned cloud storage providers support multiple conditional write options that can be employed concurrently, without needing to manually enforce checks for proper functioning, so we wouldn't require any validation for the same.
References :
AWS S3 conditional writes:
Azure Blob Storage conditional headers:
Google Cloud Storage generation preconditions: