-
Notifications
You must be signed in to change notification settings - Fork 429
feat(idempotency): leverage new DynamoDB Failed conditional writes behavior with ReturnValuesOnConditionCheckFailure #3446
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
leandrodamascena
merged 24 commits into
aws-powertools:develop
from
dastra:feat/dynamodb-failed-conditional-writes
Jan 19, 2024
Merged
Changes from 4 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
92d21ec
feat: add ReturnValuesOnConditionCheckFailure to DynamoDB idempotency…
7a8267f
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
bd92f33
feat: add ReturnValuesOnConditionCheckFailure to DynamoDB idempotency…
0e01865
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 3a7e60a
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 5727c48
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena dd3707d
feat: add ReturnValuesOnConditionCheckFailure to DynamoDB idempotency…
6a67bde
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 887e2e9
Improving code readability
leandrodamascena 88ed83c
Reverting function
leandrodamascena d688322
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 7cb1b2c
Adding comments about some logic decisions
leandrodamascena efe99a2
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena d2798ad
Use DynamoDBPersistenceLayer passed in for test_idempotent_lambda_exp…
dastra a9a598c
Adding docs
leandrodamascena 296e9ee
wording
cb94a14
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 7c66456
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena deca935
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 488fc01
Adressing Ruben's feedback
leandrodamascena 1d6d944
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 7ec8343
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 85b829c
Adressing Ruben's feedback
leandrodamascena 19e042e
Merge branch 'develop' into feat/dynamodb-failed-conditional-writes
leandrodamascena 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
93 changes: 93 additions & 0 deletions
93
aws_lambda_powertools/utilities/idempotency/persistence/datarecord.py
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,93 @@ | ||
""" | ||
Data Class for idempotency records. | ||
""" | ||
|
||
import datetime | ||
import json | ||
import logging | ||
from types import MappingProxyType | ||
from typing import Optional | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
STATUS_CONSTANTS = MappingProxyType({"INPROGRESS": "INPROGRESS", "COMPLETED": "COMPLETED", "EXPIRED": "EXPIRED"}) | ||
|
||
|
||
class DataRecord: | ||
""" | ||
Data Class for idempotency records. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
idempotency_key: str, | ||
status: str = "", | ||
expiry_timestamp: Optional[int] = None, | ||
in_progress_expiry_timestamp: Optional[int] = None, | ||
response_data: str = "", | ||
payload_hash: str = "", | ||
) -> None: | ||
""" | ||
|
||
Parameters | ||
---------- | ||
idempotency_key: str | ||
hashed representation of the idempotent data | ||
status: str, optional | ||
status of the idempotent record | ||
expiry_timestamp: int, optional | ||
time before the record should expire, in seconds | ||
in_progress_expiry_timestamp: int, optional | ||
time before the record should expire while in the INPROGRESS state, in seconds | ||
payload_hash: str, optional | ||
hashed representation of payload | ||
response_data: str, optional | ||
response data from previous executions using the record | ||
""" | ||
self.idempotency_key = idempotency_key | ||
self.payload_hash = payload_hash | ||
self.expiry_timestamp = expiry_timestamp | ||
self.in_progress_expiry_timestamp = in_progress_expiry_timestamp | ||
self._status = status | ||
self.response_data = response_data | ||
|
||
@property | ||
def is_expired(self) -> bool: | ||
""" | ||
Check if data record is expired | ||
|
||
Returns | ||
------- | ||
bool | ||
Whether the record is currently expired or not | ||
""" | ||
return bool(self.expiry_timestamp and int(datetime.datetime.now().timestamp()) > self.expiry_timestamp) | ||
|
||
@property | ||
def status(self) -> str: | ||
""" | ||
Get status of data record | ||
|
||
Returns | ||
------- | ||
str | ||
""" | ||
if self.is_expired: | ||
return STATUS_CONSTANTS["EXPIRED"] | ||
if self._status in STATUS_CONSTANTS.values(): | ||
return self._status | ||
|
||
from aws_lambda_powertools.utilities.idempotency.exceptions import IdempotencyInvalidStatusError | ||
|
||
raise IdempotencyInvalidStatusError(self._status) | ||
|
||
def response_json_as_dict(self) -> Optional[dict]: | ||
""" | ||
Get response data deserialized to python dict | ||
|
||
Returns | ||
------- | ||
Optional[dict] | ||
previous response data deserialized | ||
""" | ||
return json.loads(self.response_data) if self.response_data else None |
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
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.