Skip to content

Commit a8177ad

Browse files
fix(idempotency): warn when a persistence layer is reused with a different config (#8664)
Co-authored-by: Leandro Damascena <lcdama@amazon.pt>
1 parent d07a9c2 commit a8177ad

3 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎aws_lambda_powertools/utilities/idempotency/persistence/base.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
DataRecord,
3333
)
3434
from aws_lambda_powertools.utilities.jmespath_utils import PowertoolsFunctions
35+
from aws_lambda_powertools.warnings import PowertoolsUserWarning
3536

3637
if TYPE_CHECKING:
3738
from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig
@@ -85,6 +86,7 @@ def __init__(self):
8586
self.expires_after_seconds: int = 60 * 60 # 1 hour default
8687
self.use_local_cache = False
8788
self.hash_function = hashlib.md5
89+
self._config_settings: tuple | None = None
8890

8991
def configure(
9092
self,
@@ -108,10 +110,31 @@ def configure(
108110
key_prefix or f"{os.getenv(constants.LAMBDA_FUNCTION_NAME_ENV, 'test-func')}.{function_name or ''}"
109111
)
110112

113+
# Settings this layer applies from the config. lambda_context and response_hook are read per call instead.
114+
config_settings = (
115+
config.event_key_jmespath,
116+
config.payload_validation_jmespath,
117+
config.jmespath_options,
118+
config.raise_on_no_idempotency_key,
119+
config.expires_after_seconds,
120+
config.use_local_cache,
121+
config.local_cache_max_items,
122+
config.hash_function,
123+
)
124+
111125
if self.configured:
112126
# Prevent being reconfigured multiple times
127+
if config_settings != self._config_settings:
128+
warnings.warn(
129+
f"This persistence layer is already configured with a different IdempotencyConfig, so "
130+
f"'{self.function_name}' will use the settings of the first one. "
131+
"Use a separate persistence layer instance for each IdempotencyConfig.",
132+
category=PowertoolsUserWarning,
133+
stacklevel=2,
134+
)
113135
return
114136
self.configured = True
137+
self._config_settings = config_settings
115138

116139
self.event_key_jmespath = config.event_key_jmespath
117140
if config.event_key_jmespath:

‎docs/utilities/idempotency.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,9 @@ You can override and further extend idempotency behavior via **`IdempotencyConfi
875875
| **hash_function** | `md5` | Function to use for calculating hashes, as provided by [hashlib](https://docs.python.org/3/library/hashlib.html){target="_blank" rel="nofollow"} in the standard library. |
876876
| **response_hook** | `None` | Function to use for processing the stored Idempotent response. This function hook is called when an existing idempotent response is found. See [Manipulating The Idempotent Response](idempotency.md#manipulating-the-idempotent-response) |
877877

878+
!!! warning "Use one persistence layer instance per `IdempotencyConfig`"
879+
A persistence layer applies the settings of the first `IdempotencyConfig` it is used with. If functions with different configs share the same persistence layer instance, the later ones use the first config's settings, and we emit a `PowertoolsUserWarning`.
880+
878881
### Handling concurrent executions with the same payload
879882

880883
This utility will raise an **`IdempotencyAlreadyInProgressError`** exception if you receive **multiple invocations with the same payload while the first invocation hasn't completed yet**.

‎tests/functional/idempotency/_boto3/test_idempotency.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,6 +1299,33 @@ def test_jmespath_with_powertools_json(
12991299
assert result == "test-func.handler#" + persistence_store._generate_hash(expected_value)
13001300

13011301

1302+
def test_configure_warns_when_reused_with_a_different_config(persistence_store: DynamoDBPersistenceLayer):
1303+
# GIVEN a persistence layer already configured with one IdempotencyConfig
1304+
persistence_store.configure(IdempotencyConfig(event_key_jmespath="order_id"), "charge")
1305+
1306+
# WHEN it's configured again with a config whose settings differ
1307+
with pytest.warns(PowertoolsUserWarning, match="Use a separate persistence layer instance for each"):
1308+
persistence_store.configure(IdempotencyConfig(event_key_jmespath="user_id"), "notify")
1309+
1310+
# THEN the settings of the first config are kept
1311+
assert persistence_store.event_key_jmespath == "order_id"
1312+
1313+
1314+
def test_configure_does_not_warn_when_reused_with_the_same_settings(persistence_store: DynamoDBPersistenceLayer):
1315+
# GIVEN a persistence layer configured by a function with a shared IdempotencyConfig
1316+
shared_config = IdempotencyConfig(event_key_jmespath="order_id")
1317+
persistence_store.configure(shared_config, "charge")
1318+
1319+
# WHEN another function uses the same config, and a new config with the same settings is passed,
1320+
# as @idempotent does per call when no config is given
1321+
with warnings.catch_warnings():
1322+
warnings.simplefilter("error", PowertoolsUserWarning)
1323+
persistence_store.configure(shared_config, "notify")
1324+
persistence_store.configure(IdempotencyConfig(event_key_jmespath="order_id"), "notify")
1325+
1326+
# THEN no warning is emitted
1327+
1328+
13021329
@pytest.mark.parametrize("config_with_jmespath_options", ["powertools_json(data).payload"], indirect=True)
13031330
def test_custom_jmespath_function_overrides_builtin_functions(
13041331
config_with_jmespath_options: IdempotencyConfig,

0 commit comments

Comments
 (0)