-
Notifications
You must be signed in to change notification settings - Fork 14.9k
KAFKA-16072: JUnit 5 extension to detect thread leak #15101
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
Closed
Closed
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3eaa3ab
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv cfd1716
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv 6387125
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv 5c707ad
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv 28c2626
Merge branch 'trunk' into KAFKA-16072
wernerdv 7035b90
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv 9c4a431
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv 5eedd69
Merge branch 'trunk' into KAFKA-16072
wernerdv 7460dad
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv ef44604
Merge branch 'trunk' into KAFKA-16072
wernerdv d26d948
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv ef15ae1
Merge branch 'trunk' into KAFKA-16072
wernerdv c7f58fc
Merge branch 'trunk' into KAFKA-16072
wernerdv 776da95
Merge branch 'trunk' into KAFKA-16072
wernerdv dc85ac5
Merge branch 'trunk' into KAFKA-16072
wernerdv 93b5985
KAFKA-16072: JUnit 5 extension to detect thread leak
wernerdv b1fb3ef
Merge branch 'trunk' into KAFKA-16072
wernerdv 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
84 changes: 84 additions & 0 deletions
84
core/src/test/java/kafka/test/junit/LeakTestingExtension.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,84 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 kafka.test.junit; | ||
|
|
||
| import kafka.utils.TestUtils; | ||
| import org.junit.jupiter.api.extension.AfterEachCallback; | ||
| import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
| import org.junit.jupiter.api.extension.ExtensionContext; | ||
| import org.junit.jupiter.api.extension.ExtensionContext.Namespace; | ||
| import org.junit.jupiter.api.extension.ExtensionContext.Store; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import scala.Tuple2; | ||
|
|
||
| import static org.apache.kafka.test.TestUtils.DEFAULT_MAX_WAIT_MS; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class LeakTestingExtension implements BeforeEachCallback, AfterEachCallback { | ||
| private static final Set<String> EXPECTED_THREAD_NAMES = new HashSet<>( | ||
| Arrays.asList( | ||
| "Attach Listener", "client-metrics-reaper", "executor-", "feature-zk-node-event-process-thread", | ||
| "ForkJoinPool", "JMX", "junit-", "metrics-meter-tick-thread", "pool-", "process reaper", "RMI", | ||
| "scala-" | ||
| ) | ||
| ); | ||
| private static final String THREADS_KEY = "threads"; | ||
|
|
||
| @Override | ||
| public void beforeEach(ExtensionContext context) { | ||
| getStore(context).put(THREADS_KEY, Thread.getAllStackTraces().keySet()); | ||
| } | ||
|
|
||
| @Override | ||
| @SuppressWarnings("unchecked") | ||
| public void afterEach(ExtensionContext context) { | ||
| Set<Thread> initialThreads = getStore(context).remove(THREADS_KEY, Set.class); | ||
| Tuple2<Set<Thread>, Object> unexpectedThreads = TestUtils.computeUntilTrue( | ||
| () -> unexpectedThreads(initialThreads), | ||
| DEFAULT_MAX_WAIT_MS, | ||
| 100L, | ||
| Set::isEmpty | ||
| ); | ||
|
|
||
| assertTrue(unexpectedThreads._1.isEmpty(), "Found unexpected threads after executing test: " + | ||
| unexpectedThreads._1.stream().map(Objects::toString).collect(Collectors.joining(", "))); | ||
| } | ||
|
|
||
| private Set<Thread> unexpectedThreads(Set<Thread> initialThreads) { | ||
| Set<Thread> threads = new HashSet<>(Thread.getAllStackTraces().keySet()); | ||
| return threads.stream() | ||
| .filter(t -> !initialThreads.contains(t)) | ||
| .filter(t -> { | ||
| for (String s: EXPECTED_THREAD_NAMES) { | ||
| if (t.getName().contains(s)) | ||
| return false; | ||
| } | ||
| return true; | ||
| }) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private Store getStore(ExtensionContext context) { | ||
| return context.getStore(Namespace.create(getClass(), context.getRequiredTestMethod())); | ||
| } | ||
| } | ||
16 changes: 16 additions & 0 deletions
16
core/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension
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,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| # contributor license agreements. See the NOTICE file distributed with | ||
| # this work for additional information regarding copyright ownership. | ||
| # The ASF licenses this file to You 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. | ||
|
|
||
| kafka.test.junit.LeakTestingExtension |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hy @gharris1727 @wernerdv
Can you please help me understand why it is expected to have kafka-scheduler or ExpirationReaper or ReplicaFetcherThread at the end or beginning of test? Isn't presence of these threads an indication of a thread leak? (for example, expiration reaper threads may mean that we don't close controllerAPIs correctly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I didn't notice that this is a set of threads that are allowed to leak, where the original TestUtils.verifyNoUnexpectedThreads was a set of threads which weren't allowed to leak. I assumed the mechanism was copy-pasted.
I don't know if this set needs to exist, especially now that the assertion is stateful. Perhaps we can empty this set completely to see what the effect is, and then add in what is necessary. I did see some flakiness with an early prototype of #14783 which sometimes detected the gradle runner opening sockets. Perhaps there are similar things which create background threads.