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

Closes gh-31572
This commit is contained in:
Andy Wilkinson 2022-07-20 11:53:12 +01:00
parent bcbe072d96
commit efdd844c7a
1 changed files with 32 additions and 2 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.nio.file.attribute.PosixFilePermission;
import java.util.Set; 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.Assert;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
@ -36,9 +40,13 @@ import org.springframework.util.ObjectUtils;
*/ */
public class ApplicationPid { public class ApplicationPid {
private static final Log logger = LogFactory.getLog(ApplicationPid.class);
private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE, private static final PosixFilePermission[] WRITE_PERMISSIONS = { PosixFilePermission.OWNER_WRITE,
PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE }; PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE };
private static final long JVM_NAME_RESOLVE_THRESHOLD = 200;
private final String pid; private final String pid;
public ApplicationPid() { public ApplicationPid() {
@ -51,7 +59,7 @@ public class ApplicationPid {
private String getPid() { private String getPid() {
try { try {
String jvmName = ManagementFactory.getRuntimeMXBean().getName(); String jvmName = resolveJvmName();
return jvmName.split("@")[0]; return jvmName.split("@")[0];
} }
catch (Throwable ex) { 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 @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == this) { if (obj == this) {