Skip to content

Optimistic locking for delete scenario with TransactWriteItemsEnhancedRequest #6043

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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

roamariei
Copy link
Contributor

@roamariei roamariei commented Apr 17, 2025

Description

Optimistic locking while using DynamoDbEnhancedClient - DeleteItem with TransactWriteItemsEnhancedRequest

Motivation and Context

#2358

Modifications

  • Modified addDeleteItem method in TransactWriteItemsEnhancedRequest to invoke a new method, generateTransactDeleteItem, which includes an additional parameter: Map<String, AttributeValue> itemMap.
  • Redirected delete operations to an internal method that constructs the version check condition.
  • Added an internal BeforeDelete interface, similar to BeforeWrite, to handle version validation before execution. The implementation of this method was placed in VersionedRecordExtension and called from DeleteItemOperation.

Testing

Added unit tests for ChainExtension, VersionRecordTest and integration tests for delete scenario with matching version and with exception if the version mismatch.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@roamariei roamariei requested a review from a team as a code owner April 17, 2025 14:52
@roamariei roamariei force-pushed the bugifx/optimistic-locking-not-working-for-delete-operation-with-transactWriteItemsEnhancedRequest branch from 9d90f5d to 12779d9 Compare April 17, 2025 17:46
@roamariei roamariei changed the title updated transact delete item flow to check the version for optimistic… Optimistic locking for delete scenario with TransactWriteItemsEnhancedRequest Apr 17, 2025
@roamariei roamariei force-pushed the bugifx/optimistic-locking-not-working-for-delete-operation-with-transactWriteItemsEnhancedRequest branch from 4552122 to bbbca6e Compare April 23, 2025 12:47
@roamariei roamariei force-pushed the bugifx/optimistic-locking-not-working-for-delete-operation-with-transactWriteItemsEnhancedRequest branch from bbbca6e to b1fd95d Compare April 23, 2025 12:52
@joviegas
Copy link
Contributor

This is a breaking change right ?

What if currently user was using this API to delete any API m and such cases we will start getting exceptions ?

@anasatirbasa
Copy link

This is a breaking change right ?

What if currently user was using this API to delete any API m and such cases we will start getting exceptions ?

Hello, @joviegas

This is not a breaking change, as described in the Solution Proposal. A validation step was added to ensure that the version in the delete request matches the version stored in the database.

If the versions do not match, the operation now throws a TransactionCanceledException, which is the expected behavior for conditional operations in DefaultDynamoDbClient in case a condition (in one of the condition expressions) is not met. This scenario is covered by the integration test deleteItemWithTransactWrite_shouldFailIfVersionMismatch in CrudWithResponseIntegrationTest.java.

Before the changes from the current PR, the item would have been deleted even if the version did not match, and no exception was thrown. The new behavior enforces optimistic locking to improve data integrity.

@joviegas
Copy link
Contributor

This is a breaking change right ?
What if currently user was using this API to delete any API m and such cases we will start getting exceptions ?

Hello, @joviegas

This is not a breaking change, as described in the Solution Proposal. A validation step was added to ensure that the version in the delete request matches the version stored in the database.

If the versions do not match, the operation now throws a TransactionCanceledException, which is the expected behavior for conditional operations in DefaultDynamoDbClient in case a condition (in one of the condition expressions) is not met. This scenario is covered by the integration test deleteItemWithTransactWrite_shouldFailIfVersionMismatch in CrudWithResponseIntegrationTest.java.

Before the changes from the current PR, the item would have been deleted even if the version did not match, and no exception was thrown. The new behavior enforces optimistic locking to improve data integrity.

Right now, customers can delete items without worrying about versions, and it just works. But after our update, they're going to start getting TransactionCanceledException errors out of nowhere.

Think about it this way: let's say someone has code that tries to delete item X with version 1. They don't really care if the actual version is 2 or 3 - they just want the item gone. Today, that works fine. Tomorrow, after they upgrade, boom - exception thrown and their code breaks.

That's a classic breaking change. Their apps that work perfectly today will suddenly fail. They'll have to go in and modify their code to handle these new exceptions. And there's no way for them to get the old "just delete it" behavior back without changing their code.

Don't get me wrong - I totally get that this change makes things better from a data integrity standpoint with the optimistic locking. Some customers probably built their apps specifically counting on that "delete regardless of version" behavior.
We need to think of approach which is not breaking the existing customers. What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants