Log a warning if determining the app's PID takes too long

Closes gh-31572
pull/32521/head
Andy Wilkinson 2 years ago
parent bcbe072d96
commit efdd844c7a

@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,6 +25,10 @@ import java.nio.file.Files;
import java.nio.file.attribute.PosixFilePermission;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
@ -36,9 +40,13 @@ import org.springframework.util.ObjectUtils;
*/
public class ApplicationPid {
private static final Log logger = LogFactory.getLog(ApplicationPid.class);
private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE };
private static final long JVM_NAME_RESOLVE_THRESHOLD = 200;
private final String pid;
public ApplicationPid() {
@ -51,7 +59,7 @@ public class ApplicationPid {
private String getPid() {
try {
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
String jvmName = resolveJvmName();
return jvmName.split("@")[0];
}
catch (Throwable ex) {
@ -59,6 +67,28 @@ public class ApplicationPid {
}
}
private String resolveJvmName() {
long startTime = System.currentTimeMillis();
String jvmName = ManagementFactory.getRuntimeMXBean().getName();
long elapsed = System.currentTimeMillis() - startTime;
if (elapsed > JVM_NAME_RESOLVE_THRESHOLD) {
logger.warn(LogMessage.of(() -> {
StringBuilder warning = new StringBuilder();
warning.append("ManagementFactory.getRuntimeMXBean().getName() took ");
warning.append(elapsed);
warning.append(" milliseconds to respond.");
warning.append(" This may be due to slow host name resolution.");
warning.append(" Please verify your network configuration");
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
warning.append(" (macOS machines may need to add entries to /etc/hosts)");
}
warning.append(".");
return warning;
}));
}
return jvmName;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {

Loading…
Cancel
Save