Skip to content

Maintenance: Make Parameters utility thread-safe #2283

@phipag

Description

@phipag

Summary

Customers can use the parameters utility from multi-threaded context. For instance, when using the Batch utility using parallel processing.

This can cause a race condition in the underlying data structure that was created to support the fluent API (https://docs.aws.amazon.com/powertools/java/latest/utilities/parameters/#fluent-api).

Example:

Thread 1: provider.withMaxAge(60, SECONDS).get("key1")
Thread 2: provider.withMaxAge(10, SECONDS).get("key2")

.withMaxAge and also .withTransformation internally set a instance variable on the cache manager (or transformation manager). This is problematic because in the example above thread 2 could set the max age before thread 1 has actually cached "key1" with the correct age.

We should fix such race-conditions and mark the BaseProvider as @ThreadSafe.

Why is this needed?

Thread-safety is a general expectation that is also fulfilled by AWS SDKs. Hence, we should keep the same quality bar for Powertools utilities.

Which area does this relate to?

Parameters

Solution

When implementing this solution we need to make sure that the cached values are still accessible across threads because this is the whole purpose of the cache.

Acknowledgment

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Pending review

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions