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() {
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) {
final Thread thread = entry.getKey();
logger.warn("Potentially blocked execution on network thread [{}] [{}] [{} milliseconds]: \n{}",
thread.getName(),
thread.getState(),
TimeUnit.NANOSECONDS.toMillis(elapsedTimeInNanos),
Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n")));
final String stackTrace =
Arrays.stream(thread.getStackTrace()).map(Object::toString).collect(Collectors.joining("\n"));
final Thread.State threadState = thread.getState();
if (blockedSinceInNanos == registry.get(thread)) {
logger.warn("Potentially blocked execution on network thread [{}] [{}] [{} milliseconds]: \n{}",
thread.getName(), threadState, TimeUnit.NANOSECONDS.toMillis(elapsedTimeInNanos), stackTrace);
}
}
}
if (stopped == false) {