Repository navigation
fix(batch): use thread-safe collection in MultiThreadMDC
#2717
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
phipag
merged 2 commits into
aws-powertools:main
from
bubanko:fix/2712-multi-thread-mdc-concurrency-bug
Oct 29, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
101 changes: 101 additions & 0 deletions
101
...ch/src/test/java/software/amazon/lambda/powertools/batch/internal/MultiThreadMDCTest.java
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,101 @@ | ||
| /* | ||
| * Copyright 2023 Amazon.com, Inc. or its affiliates. | ||
| * Licensed under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| */ | ||
| package software.amazon.lambda.powertools.batch.internal; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
|
|
||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatNoException; | ||
|
|
||
| class MultiThreadMDCTest { | ||
|
|
||
| @Test | ||
| void copyMDCToThread_givenMdcNotCopiedToThread_shouldCopyMdcToThread() { | ||
| MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
| assertThat(multiThreadMDC.copyMDCToThread("test")).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void copyMDCToThread_givenMdcAlreadyCopiedToThread_shouldNotCopyMdcToThread() { | ||
| MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
| multiThreadMDC.copyMDCToThread("test"); | ||
| assertThat(multiThreadMDC.copyMDCToThread("test")).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void removeThread_givenMdcAlreadyCopiedToThread_shouldRemoveThread() { | ||
| MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
| multiThreadMDC.copyMDCToThread("test"); | ||
| assertThat(multiThreadMDC.removeThread("test")).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void removeThread_givenMdcNotCopiedToThread_shouldNotRemoveThread() { | ||
| MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
| assertThat(multiThreadMDC.removeThread("test")).isFalse(); | ||
| } | ||
|
|
||
| @Nested | ||
| class ConcurrencyTests { | ||
|
|
||
| private static final int REMOVE_THREAD_WORKERS_COUNT = 10; | ||
| private static final int COPY_MDC_TO_THREAD_ITERATIONS = 100000; | ||
| private static final String THREAD_NAME = "test"; | ||
|
|
||
| @Test | ||
| void shouldBeThreadSafe() { | ||
| MultiThreadMDC multiThreadMDC = new MultiThreadMDC(); | ||
| AtomicBoolean stopSignal = new AtomicBoolean(false); | ||
| ExecutorService executor = Executors.newFixedThreadPool(REMOVE_THREAD_WORKERS_COUNT + 1); | ||
|
Check failure on line 68 in powertools-batch/src/test/java/software/amazon/lambda/powertools/batch/internal/MultiThreadMDCTest.java
|
||
|
phipag marked this conversation as resolved.
|
||
| try { | ||
| List<CompletableFuture<Void>> futures = new ArrayList<>(); | ||
| for (int i = 0; i < REMOVE_THREAD_WORKERS_COUNT; i++) { | ||
| futures.add(CompletableFuture.runAsync(() -> threadRemover(multiThreadMDC, stopSignal), executor)); | ||
| } | ||
| futures.add(CompletableFuture.runAsync(() -> mdcCopier(multiThreadMDC, stopSignal), executor)); | ||
| assertThatNoException().isThrownBy(() -> futures.forEach(CompletableFuture::join)); | ||
| } finally { | ||
| executor.shutdown(); | ||
| } | ||
| } | ||
|
|
||
| private static void mdcCopier(MultiThreadMDC multiThreadMDC, AtomicBoolean stopSignal) { | ||
| try { | ||
| for (int i = 0; i < COPY_MDC_TO_THREAD_ITERATIONS; i++) { | ||
| multiThreadMDC.copyMDCToThread(THREAD_NAME); | ||
| } | ||
| } finally { | ||
| stopSignal.set(true); | ||
| } | ||
| } | ||
|
|
||
| private static void threadRemover(MultiThreadMDC multiThreadMDC, AtomicBoolean stopSignal) { | ||
| try { | ||
| while (!stopSignal.get()) { | ||
| multiThreadMDC.removeThread(THREAD_NAME); | ||
| } | ||
| } finally { | ||
| stopSignal.set(true); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.