NIFI-12239 Add OS and Java details at startup in bootstrap log

This closes #7891

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
Pierre Villard 2023-10-17 17:40:44 +02:00 committed by exceptionfactory
parent f9615414bd
commit e4e2336ebe
No known key found for this signature in database
GPG Key ID: 29B6A52D2AAE8DBA
1 changed files with 47 additions and 2 deletions

View File

@ -37,6 +37,7 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -55,6 +56,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
@ -69,6 +71,9 @@ import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.MBeanServer;
import javax.management.ObjectName;
/**
* <p>
* The class which bootstraps Apache NiFi. This class looks for the
@ -1165,6 +1170,9 @@ public class RunNiFi {
nifiPropsFilename = nifiPropsFilename.trim();
String maximumHeapSize = null;
String minimumHeapSize = null;
final List<String> javaAdditionalArgs = new ArrayList<>();
for (final Map.Entry<String, String> entry : props.entrySet()) {
final String key = entry.getKey();
@ -1172,6 +1180,12 @@ public class RunNiFi {
if (key.startsWith("java.arg")) {
javaAdditionalArgs.add(value);
if (value.startsWith("-Xms")) {
minimumHeapSize = StringUtils.substringAfter(value, "-Xms");
}
if (value.startsWith("-Xmx")) {
maximumHeapSize = StringUtils.substringAfter(value, "-Xmx");
}
}
}
@ -1197,8 +1211,7 @@ public class RunNiFi {
cpFiles.add(file.getAbsolutePath());
}
String runtimeJavaVersion = System.getProperty("java.version");
defaultLogger.info("Runtime Java version: {}", runtimeJavaVersion);
defaultLogger.info(getPlatformDetails(minimumHeapSize, maximumHeapSize));
final StringBuilder classPathBuilder = new StringBuilder();
for (int i = 0; i < cpFiles.size(); i++) {
@ -1251,6 +1264,7 @@ public class RunNiFi {
cmd.add("-classpath");
cmd.add(classPath);
cmd.addAll(javaAdditionalArgs);
cmd.add("-Dnifi.properties.file.path=" + nifiPropsFilename);
cmd.add("-Dnifi.bootstrap.listen.port=" + listenPort);
cmd.add("-Dapp=NiFi");
@ -1392,6 +1406,37 @@ public class RunNiFi {
}
}
private String getPlatformDetails(final String minimumHeapSize, final String maximumHeapSize) {
final Map<String, String> details = new LinkedHashMap<String, String>(6);
details.put("javaVersion", System.getProperty("java.version"));
details.put("availableProcessors", Integer.toString(Runtime.getRuntime().availableProcessors()));
try {
final ObjectName osObjectName = ManagementFactory.getOperatingSystemMXBean().getObjectName();
final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
final Object maxOpenFileCount = mBeanServer.getAttribute(osObjectName, "MaxFileDescriptorCount");
if (maxOpenFileCount!= null) {
details.put("maxOpenFileDescriptors", String.valueOf(maxOpenFileCount));
}
final Object totalPhysicalMemory = mBeanServer.getAttribute(osObjectName, "TotalPhysicalMemorySize");
if (totalPhysicalMemory != null) {
details.put("totalPhysicalMemoryMB", String.valueOf(((Long) totalPhysicalMemory) / (1024*1024)));
}
} catch (final Throwable t) {
// Ignore. This will throw either ClassNotFound or NoClassDefFoundError if unavailable in this JVM.
}
if (minimumHeapSize != null) {
details.put("minimumHeapSize", minimumHeapSize);
}
if (maximumHeapSize != null) {
details.put("maximumHeapSize", maximumHeapSize);
}
return details.toString();
}
private void writeSensitiveKeyFile(Map<String, String> props, Path sensitiveKeyFile) throws IOException {
BufferedWriter sensitiveKeyWriter = Files.newBufferedWriter(sensitiveKeyFile, StandardCharsets.UTF_8);
sensitiveKeyWriter.write(props.get(NIFI_BOOTSTRAP_SENSITIVE_KEY));