Skip to content

Commit

Permalink
NIFI-12239 Add OS and Java details at startup in bootstrap log
Browse files Browse the repository at this point in the history
This closes #7891

Signed-off-by: David Handermann <exceptionfactory@apache.org>

(cherry picked from commit e4e2336)
  • Loading branch information
pvillard31 authored and exceptionfactory committed Oct 20, 2023
1 parent f4722f4 commit 17738df
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
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;
Expand All @@ -59,6 +60,7 @@
import java.util.Arrays;
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;
Expand All @@ -74,6 +76,9 @@
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
Expand Down Expand Up @@ -1172,13 +1177,22 @@ public void start(final boolean monitor) throws IOException {

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();
final String value = entry.getValue();

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");
}
}
}

Expand Down Expand Up @@ -1225,6 +1239,7 @@ public boolean accept(final File dir, final String filename) {
if (RuntimeVersionProvider.isMajorVersionDeprecated(javaMajorVersion)) {
deprecationLogger.warn("Support for Java {} is deprecated. Java {} is the minimum recommended version", javaMajorVersion, RuntimeVersionProvider.getMinimumMajorVersion());
}
defaultLogger.info(getPlatformDetails(minimumHeapSize, maximumHeapSize));

final StringBuilder classPathBuilder = new StringBuilder();
for (int i = 0; i < cpFiles.size(); i++) {
Expand Down Expand Up @@ -1277,6 +1292,7 @@ public boolean accept(final File dir, final String filename) {
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");
Expand Down Expand Up @@ -1434,6 +1450,37 @@ public boolean accept(final File dir, final String filename) {
}
}

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));
Expand Down

0 comments on commit 17738df

Please sign in to comment.