-
Notifications
You must be signed in to change notification settings - Fork 989
Optimistic Locking for Delete Operations #6747
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
anasatirbasa
wants to merge
20
commits into
aws:master
Choose a base branch
from
anasatirbasa:feature/optimistic-locking-for-delete-operations
base: master
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
20 commits
Select commit
Hold shift + click to select a range
a1f9cfd
Optimistic Locking for Delete Operations
anasatirbasa adb11e3
Merge branch 'master' into feature/optimistic-locking-for-delete-oper…
anasatirbasa c002f9e
Merge branch 'master' into feature/optimistic-locking-for-delete-oper…
anasatirbasa 5adbe18
Address PR feedback
anasatirbasa cc414f3
Address PR feedback
anasatirbasa 3a84c27
Address PR feedback
anasatirbasa fd025d1
Address PR feedback
anasatirbasa b151dfd
Address PR feedback
anasatirbasa e49d02a
Address PR feedback
anasatirbasa e28e78a
Address PR feedback
anasatirbasa 31e5aca
Address PR feedback
anasatirbasa 48deb28
Address PR feedback
anasatirbasa f925da3
Address PR feedback
anasatirbasa f241c81
Address PR feedback
anasatirbasa efa9521
Address PR feedback
anasatirbasa 2176326
Address PR feedback
anasatirbasa 1796ed7
Merge branch 'refs/heads/master' into feature/optimistic-locking-for-…
anasatirbasa f0c0da0
Added implementation with Boolean flag via @DynamoDbVersionAttribute …
anasatirbasa 6406ad0
Increased code coverage
anasatirbasa 49d2ede
Merge Optimistic Locking condition with existing condition expressions
anasatirbasa 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
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
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
165 changes: 165 additions & 0 deletions
165
.../main/java/software/amazon/awssdk/enhanced/dynamodb/internal/OptimisticLockingHelper.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,165 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.enhanced.dynamodb.internal; | ||
|
|
||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.keyRef; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.valueRef; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Optional; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.enhanced.dynamodb.Expression; | ||
| import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
| import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest; | ||
| import software.amazon.awssdk.enhanced.dynamodb.model.TransactDeleteItemEnhancedRequest; | ||
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
|
|
||
| /** | ||
| * Utility class for adding optimistic locking to DynamoDB delete operations. | ||
| * <p> | ||
| * Optimistic locking prevents concurrent modifications by checking that an item's version hasn't changed since it was last read. | ||
| * If the version has changed, the delete operation fails with a {@code ConditionalCheckFailedException}. | ||
| */ | ||
| @SdkInternalApi | ||
| public final class OptimisticLockingHelper { | ||
|
|
||
| private static final String CUSTOM_VERSION_METADATA_KEY = "VersionedRecordExtension:VersionAttribute"; | ||
| private static final String USE_VERSION_ON_DELETE_METADATA_KEY = "VersionedRecordExtension:UseVersionOnDelete"; | ||
|
|
||
| private OptimisticLockingHelper() { | ||
| } | ||
|
|
||
| /** | ||
| * Adds optimistic locking to a delete request. | ||
| * <p> | ||
| * If a condition expression is already set on the request builder, this method merges it with the optimistic locking | ||
| * condition using {@code AND}. | ||
| * | ||
| * @param requestBuilder the original delete request builder | ||
| * @param versionValue the expected version value | ||
| * @param versionAttributeName the version attribute name | ||
| * @return delete request with optimistic locking condition | ||
| */ | ||
| public static DeleteItemEnhancedRequest optimisticLocking(DeleteItemEnhancedRequest.Builder requestBuilder, | ||
| AttributeValue versionValue, String versionAttributeName) { | ||
|
|
||
| Expression mergedCondition = mergeConditions( | ||
| requestBuilder.build().conditionExpression(), | ||
| createVersionCondition(versionValue, versionAttributeName)); | ||
|
|
||
| return requestBuilder | ||
| .conditionExpression(mergedCondition) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Adds optimistic locking to a transactional delete request. | ||
| * <p> | ||
| * If a condition expression is already set on the request builder, this method merges it with the optimistic locking | ||
| * condition using {@code AND}. | ||
| * | ||
| * @param requestBuilder the original delete request builder | ||
| * @param versionValue the expected version value | ||
| * @param versionAttributeName the version attribute name | ||
| * @return transactional delete request with optimistic locking condition | ||
| */ | ||
| public static TransactDeleteItemEnhancedRequest optimisticLocking(TransactDeleteItemEnhancedRequest.Builder requestBuilder, | ||
| AttributeValue versionValue, String versionAttributeName) { | ||
|
|
||
| Expression mergedCondition = mergeConditions( | ||
| requestBuilder.build().conditionExpression(), | ||
| createVersionCondition(versionValue, versionAttributeName)); | ||
|
|
||
| return requestBuilder | ||
| .conditionExpression(mergedCondition) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Conditionally applies optimistic locking based on annotation setting. | ||
| * | ||
| * @param <T> the type of the item | ||
| * @param requestBuilder the delete request builder | ||
| * @param keyItem the item containing version information | ||
| * @param tableSchema the table schema | ||
| * @return delete request with optimistic locking if annotation enables it and version exists, otherwise original request | ||
| * @throws IllegalStateException if optimistic locking is enabled but the version attribute is null | ||
| */ | ||
| public static <T> DeleteItemEnhancedRequest conditionallyApplyOptimisticLocking( | ||
| DeleteItemEnhancedRequest.Builder requestBuilder, T keyItem, TableSchema<T> tableSchema) { | ||
|
|
||
| return getVersionAttributeName(tableSchema) | ||
| .map(versionAttributeName -> { | ||
| Boolean useVersionOnDelete = tableSchema.tableMetadata() | ||
| .customMetadataObject(USE_VERSION_ON_DELETE_METADATA_KEY, Boolean.class) | ||
| .orElse(false); | ||
|
|
||
| if (!useVersionOnDelete) { | ||
| return requestBuilder.build(); | ||
| } | ||
|
|
||
| AttributeValue version = tableSchema.attributeValue(keyItem, versionAttributeName); | ||
| if (version == null) { | ||
| throw new IllegalStateException( | ||
| "Optimistic locking is enabled for delete, but version attribute is null: " + versionAttributeName); | ||
| } | ||
| return optimisticLocking(requestBuilder, version, versionAttributeName); | ||
|
|
||
| }).orElseGet(requestBuilder::build); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a version condition expression. | ||
| * | ||
| * @param versionValue the expected version value | ||
| * @param versionAttributeName the version attribute name | ||
| * @return version check condition expression | ||
| * @throws IllegalArgumentException if {@code versionAttributeName} or {@code versionValue} are null or empty | ||
| */ | ||
| public static Expression createVersionCondition(AttributeValue versionValue, String versionAttributeName) { | ||
| if (versionAttributeName == null || versionAttributeName.trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Version attribute name must not be null or empty."); | ||
| } | ||
|
|
||
| if (versionValue == null || versionValue.n() == null || versionValue.n().trim().isEmpty()) { | ||
| throw new IllegalArgumentException("Version value must not be null or empty."); | ||
| } | ||
|
|
||
| String attributeKeyRef = keyRef(versionAttributeName); | ||
| String attributeValueRef = valueRef(versionAttributeName); | ||
|
|
||
| return Expression.builder() | ||
| .expression(String.format("%s = %s", attributeKeyRef, attributeValueRef)) | ||
| .expressionNames(Collections.singletonMap(attributeKeyRef, versionAttributeName)) | ||
| .expressionValues(Collections.singletonMap(attributeValueRef, versionValue)) | ||
| .build(); | ||
| } | ||
|
|
||
| public static Expression mergeConditions(Expression initialCondition, Expression optimisticLockingCondition) { | ||
| return Expression.join(initialCondition, optimisticLockingCondition, " AND "); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the version attribute name from table schema. | ||
| * | ||
| * @param <T> the type of the item | ||
| * @param tableSchema the table schema | ||
| * @return version attribute name if present, empty otherwise | ||
| */ | ||
| public static <T> Optional<String> getVersionAttributeName(TableSchema<T> tableSchema) { | ||
| return tableSchema.tableMetadata().customMetadataObject(CUSTOM_VERSION_METADATA_KEY, String.class); | ||
| } | ||
| } | ||
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
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.
Why are we checking the
versionAttributeNamebut notversionValue?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.
Added a check for "versionValue" also: