Skip to content

Commit

Permalink
NIFI-12358 Fixed NPE in HostHeaderHandler
Browse files Browse the repository at this point in the history
Fixed NPE in ThreadDumpTask and TrackedLeaderElectionManager

This closes #8032

Signed-off-by: David Handermann <exceptionfactory@apache.org>
  • Loading branch information
markap14 authored and exceptionfactory committed Nov 15, 2023
1 parent 685c686 commit b5c3e6f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

public class ThreadDumpTask implements DiagnosticTask {
@Override
Expand All @@ -37,7 +38,7 @@ public DiagnosticsDumpElement captureDump(boolean verbose) {

final List<ThreadInfo> sortedInfos = new ArrayList<>(infos.length);
Collections.addAll(sortedInfos, infos);
sortedInfos.sort(new Comparator<ThreadInfo>() {
sortedInfos.sort(new Comparator<>() {
@Override
public int compare(ThreadInfo o1, ThreadInfo o2) {
return o1.getThreadName().toLowerCase().compareTo(o2.getThreadName().toLowerCase());
Expand Down Expand Up @@ -69,15 +70,15 @@ public int compare(ThreadInfo o1, ThreadInfo o2) {
sb.append(" (in native code)");
}

if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) {
if (deadlockedThreadIds != null) {
for (final long id : deadlockedThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** DEADLOCKED THREAD **");
}
}
}

if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) {
if (monitorDeadlockThreadIds != null) {
for (final long id : monitorDeadlockThreadIds) {
if (id == info.getThreadId()) {
sb.append(" ** MONITOR-DEADLOCKED THREAD **");
Expand All @@ -91,7 +92,7 @@ public int compare(ThreadInfo o1, ThreadInfo o2) {

final MonitorInfo[] monitors = info.getLockedMonitors();
for (final MonitorInfo monitor : monitors) {
if (monitor.getLockedStackFrame().equals(element)) {
if (Objects.equals(monitor.getLockedStackFrame(), element)) {
sb.append("\n\t- waiting on ").append(monitor);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public Map<String, Integer> getLeadershipChangeCount(final long duration, final
final TimedBuffer<TimestampedLong> buffer = entry.getValue();

final TimestampedLong aggregateValue = buffer.getAggregateValue(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(duration, unit));
final int leadershipChanges = aggregateValue.getValue().intValue();
final int leadershipChanges = (aggregateValue == null) ? 0 : aggregateValue.getValue().intValue();
leadershipChangesPerRole.put(roleName, leadershipChanges);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
*/
package org.apache.nifi.web.server;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.nifi.util.NiFiProperties;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ScopedHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;
Expand All @@ -28,17 +40,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.nifi.util.NiFiProperties;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ScopedHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HostHeaderHandler extends ScopedHandler {
private static final Logger logger = LoggerFactory.getLogger(HostHeaderHandler.class);
Expand Down Expand Up @@ -233,7 +234,7 @@ public static List<String> generateDefaultHostnames(NiFiProperties niFiPropertie
try {
final int lambdaPort = serverPort;
List<String> customIPs = extractIPsFromNetworkInterfaces(niFiProperties);
customIPs.stream().forEach(ip -> {
customIPs.forEach(ip -> {
validHosts.add(ip);
validHosts.add(ip + ":" + lambdaPort);
});
Expand Down Expand Up @@ -284,16 +285,20 @@ static List<String> extractIPsFromNetworkInterfaces(NiFiProperties niFiPropertie
Map<String, String> networkInterfaces = niFiProperties.isHTTPSConfigured() ? niFiProperties.getHttpsNetworkInterfaces() : niFiProperties.getHttpNetworkInterfaces();
if (isNotDefined(networkInterfaces)) {
// No custom interfaces defined
return new ArrayList<>(0);
return List.of();
} else {
List<String> allIPAddresses = new ArrayList<>();
final List<String> allIPAddresses = new ArrayList<>();
for (Map.Entry<String, String> entry : networkInterfaces.entrySet()) {
final String networkInterfaceName = entry.getValue();
try {
NetworkInterface ni = NetworkInterface.getByName(networkInterfaceName);
List<String> ipAddresses = Collections.list(ni.getInetAddresses()).stream().map(inetAddress -> inetAddress.getHostAddress().toLowerCase()).collect(Collectors.toList());
logger.debug("Resolved the following IP addresses for network interface {}: {}", networkInterfaceName, StringUtils.join(ipAddresses, ", "));
allIPAddresses.addAll(ipAddresses);
final NetworkInterface ni = NetworkInterface.getByName(networkInterfaceName);
if (ni == null) {
logger.warn("Cannot resolve network interface named " + networkInterfaceName);
} else {
final List<String> ipAddresses = Collections.list(ni.getInetAddresses()).stream().map(inetAddress -> inetAddress.getHostAddress().toLowerCase()).collect(Collectors.toList());
logger.debug("Resolved the following IP addresses for network interface {}: {}", networkInterfaceName, StringUtils.join(ipAddresses, ", "));
allIPAddresses.addAll(ipAddresses);
}
} catch (SocketException e) {
logger.warn("Cannot resolve network interface named " + networkInterfaceName);
}
Expand Down

0 comments on commit b5c3e6f

Please sign in to comment.