Skip to content
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

fix: correct behaviour to avoid caching "INPROGRESS" records #295

Merged
merged 7 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def _save_to_cache(self, data_record: DataRecord):
def _retrieve_from_cache(self, idempotency_key: str):
cached_record = self._cache.get(idempotency_key)
if cached_record:
if not cached_record.is_expired:
if all((not cached_record.is_expired, not cached_record.status == "INPROGRESS")):
to-mc marked this conversation as resolved.
Show resolved Hide resolved
return cached_record
logger.debug(f"Removing expired local cache record for idempotency key: {idempotency_key}")
self._delete_from_cache(idempotency_key)
Expand Down Expand Up @@ -305,11 +305,6 @@ def save_inprogress(self, event: Dict[str, Any]) -> None:

self._put_record(data_record)

# This has to come after _put_record. If _put_record call raises ItemAlreadyExists we shouldn't populate the
# cache with an "INPROGRESS" record as we don't know the status in the data store at this point.
if self.use_local_cache:
self._save_to_cache(data_record)

def delete_record(self, event: Dict[str, Any], exception: Exception):
"""
Delete record from the persistence store
Expand Down Expand Up @@ -365,7 +360,9 @@ def get_record(self, event: Dict[str, Any]) -> DataRecord:

record = self._get_record(idempotency_key)

if self.use_local_cache:
# We can't cache "INPROGRESS" records as we have no way to reflect updates that can happen outside of the
# execution environment
if self.use_local_cache and not record.status == STATUS_CONSTANTS["INPROGRESS"]:
to-mc marked this conversation as resolved.
Show resolved Hide resolved
self._save_to_cache(data_record=record)

self._validate_payload(event, record)
Expand Down
57 changes: 30 additions & 27 deletions docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ title: Idempotency
description: Utility
---

This utility provides a simple solution to convert your Lambda functions into idempotent operations which are safe to
retry.
!!! attention
**This utility is currently in beta**. Please open an [issue in GitHub](https://github.com/awslabs/aws-lambda-powertools-python/issues/new/choose) for any bugs or feature requests.

The idempotency utility provides a simple solution to convert your Lambda functions into idempotent operations which
are safe to retry.

## Terminology

Expand All @@ -31,31 +34,31 @@ storage layer, so you'll need to create a table first.
> Example using AWS Serverless Application Model (SAM)

=== "template.yml"
```yaml
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
...
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref IdempotencyTable
IdempotencyTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
TableName: "IdempotencyTable"
TimeToLiveSpecification:
AttributeName: expiration
Enabled: true
```
```yaml
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: python3.8
...
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref IdempotencyTable
IdempotencyTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
BillingMode: PAY_PER_REQUEST
KeySchema:
- AttributeName: id
KeyType: HASH
TableName: "IdempotencyTable"
TimeToLiveSpecification:
AttributeName: expiration
Enabled: true
```

!!! warning
When using this utility with DynamoDB, your lambda responses must always be smaller than 400kb. Larger items cannot
Expand Down
23 changes: 12 additions & 11 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ def test_idempotent_lambda_in_progress_with_cache(

stubber.add_client_error("put_item", "ConditionalCheckFailedException")
stubber.add_response("get_item", ddb_response, expected_params)

stubber.add_client_error("put_item", "ConditionalCheckFailedException")
stubber.add_response("get_item", copy.deepcopy(ddb_response), copy.deepcopy(expected_params))

stubber.add_client_error("put_item", "ConditionalCheckFailedException")
stubber.add_response("get_item", copy.deepcopy(ddb_response), copy.deepcopy(expected_params))
stubber.activate()

@idempotent(persistence_store=persistence_store)
Expand All @@ -151,11 +157,8 @@ def lambda_handler(event, context):
assert retrieve_from_cache_spy.call_count == 2 * loops
retrieve_from_cache_spy.assert_called_with(idempotency_key=hashed_idempotency_key)

assert save_to_cache_spy.call_count == 1
first_call_args_data_record = save_to_cache_spy.call_args_list[0].kwargs["data_record"]
assert first_call_args_data_record.idempotency_key == hashed_idempotency_key
assert first_call_args_data_record.status == "INPROGRESS"
assert persistence_store._cache.get(hashed_idempotency_key)
save_to_cache_spy.assert_not_called()
assert persistence_store._cache.get(hashed_idempotency_key) is None

stubber.assert_no_pending_responses()
stubber.deactivate()
Expand Down Expand Up @@ -223,12 +226,10 @@ def lambda_handler(event, context):

lambda_handler(lambda_apigw_event, {})

assert retrieve_from_cache_spy.call_count == 1
assert save_to_cache_spy.call_count == 2
first_call_args, second_call_args = save_to_cache_spy.call_args_list
assert first_call_args.args[0].status == "INPROGRESS"
assert second_call_args.args[0].status == "COMPLETED"
assert persistence_store._cache.get(hashed_idempotency_key)
retrieve_from_cache_spy.assert_called_once()
save_to_cache_spy.assert_called_once()
assert save_to_cache_spy.call_args[0][0].status == "COMPLETED"
assert persistence_store._cache.get(hashed_idempotency_key).status == "COMPLETED"

# This lambda call should not call AWS API
lambda_handler(lambda_apigw_event, {})
Expand Down