Prevent Confusing Blocked Thread Warnings in MockNioTransport (#44356) (#44376)

* Prevent Confusing Blocked Thread Warnings in MockNioTransport

* We can run into a race where the stacktrace collection and subsequent logging happens after the thread has already unblocked thus logging a confusing stacktrace of wherever the transport thread was after it became unblocked
* Fixed this by comparing whether or not the recorded timestamp is still the same before and after the stacktrace was recorded and not logging if it already changed
This commit is contained in:
Armin Braun 2019-07-16 04:40:50 +02:00 committed by GitHub
parent e0b82e92f3
commit 099d52f3b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 6 deletions

View File

@ -384,14 +384,17 @@ public class MockNioTransport extends TcpTransport {
private void logLongRunningExecutions() { private void logLongRunningExecutions() {
for (Map.Entry<Thread, Long> entry : registry.entrySet()) { for (Map.Entry<Thread, Long> entry : registry.entrySet()) {
final long elapsedTimeInNanos = threadPool.relativeTimeInNanos() - entry.getValue(); final Long blockedSinceInNanos = entry.getValue();
final long elapsedTimeInNanos = threadPool.relativeTimeInNanos() - blockedSinceInNanos;
if (elapsedTimeInNanos > warnThreshold) { if (elapsedTimeInNanos > warnThreshold) {
final Thread thread = entry.getKey(); final Thread thread = entry.getKey();
logger.warn("Potentially blocked execution on network thread [{}] [{}] [{} milliseconds]: \n{}", final String stackTrace =
thread.getName(), Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n"));
thread.getState(), final Thread.State threadState = thread.getState();
TimeUnit.NANOSECONDS.toMillis(elapsedTimeInNanos), if (blockedSinceInNanos == registry.get(thread)) {
Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n"))); logger.warn("Potentially blocked execution on network thread [{}] [{}] [{} milliseconds]: \n{}",
thread.getName(), threadState, TimeUnit.NANOSECONDS.toMillis(elapsedTimeInNanos), stackTrace);
}
} }
} }
if (stopped == false) { if (stopped == false) {