This is an automated email from the ASF dual-hosted git repository.
exceptionfactory pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi.git
The following commit(s) were added to refs/heads/main by this push:
new cb70f4ceeaa NIFI-16618 Avoid reverse DNS lookups in cluster protocol
listener (#11958)
cb70f4ceeaa is described below
commit cb70f4ceeaa0fb37c5524a9be8742cb41d72160d
Author: Jim Halfpenny <[email protected]>
AuthorDate: Tue Dec 1 02:48:26 2026 +0000
NIFI-16618 Avoid reverse DNS lookups in cluster protocol listener (#11958)
- Log the remote IP address of inbound cluster protocol connections instead
of resolving it with InetAddress.getHostName().
- The reverse lookup ran before every request was read, so slow PTR
resolution delayed heartbeats and other protocol messages.
Signed-off-by: David Handermann <[email protected]>
---
.../cluster/protocol/impl/SocketProtocolListener.java | 2 +-
.../java/org/apache/nifi/io/socket/SocketListener.java | 2 +-
.../main/java/org/apache/nifi/io/socket/SocketUtils.java | 16 ++++++++++++++++
.../java/org/apache/nifi/io/socket/SocketUtilsTest.java | 13 +++++++++++++
4 files changed, 31 insertions(+), 2 deletions(-)
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/protocol/impl/SocketProtocolListener.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/protocol/impl/SocketProtocolListener.java
index 2b7afba5146..5ccb5219612 100644
---
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/protocol/impl/SocketProtocolListener.java
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/cluster/protocol/impl/SocketProtocolListener.java
@@ -133,7 +133,7 @@ public class SocketProtocolListener extends SocketListener
implements ProtocolLi
String hostname = null;
try {
final StopWatch stopWatch = new StopWatch(true);
- hostname = socket.getInetAddress().getHostName();
+ hostname = SocketUtils.getRemoteHostString(socket);
final String requestId = UUID.randomUUID().toString();
logger.debug("Received request {} from {}", requestId, hostname);
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketListener.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketListener.java
index 04682c9f3b7..449010627f4 100644
---
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketListener.java
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketListener.java
@@ -113,7 +113,7 @@ public abstract class SocketListener {
SocketUtils.closeQuietly(socket);
continue;
} catch (final SocketException se) {
- logger.warn("Failed to communicate with {}", (socket
== null ? "Unknown Host" : socket.getInetAddress().getHostName()), se);
+ logger.warn("Failed to communicate with {}", (socket
== null ? "Unknown Host" : SocketUtils.getRemoteHostString(socket)), se);
SocketUtils.closeQuietly(socket);
continue;
} catch (final Throwable t12) {
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
index 901c20abefc..96fd93c274c 100644
---
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/main/java/org/apache/nifi/io/socket/SocketUtils.java
@@ -25,6 +25,7 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
+import java.net.SocketAddress;
import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
@@ -205,6 +206,21 @@ public final class SocketUtils {
}
}
+ /**
+ * Returns the remote host of the given socket for logging without
performing a reverse DNS lookup.
+ * Unlike {@link java.net.InetAddress#getHostName()}, this returns the IP
address literal when no hostname is known.
+ *
+ * @param socket the connected socket
+ * @return the remote hostname if already known, otherwise the IP address
literal
+ */
+ public static String getRemoteHostString(final Socket socket) {
+ final SocketAddress remoteAddress = socket.getRemoteSocketAddress();
+ if (remoteAddress instanceof final InetSocketAddress
inetSocketAddress) {
+ return inetSocketAddress.getHostString();
+ }
+ return String.valueOf(remoteAddress);
+ }
+
/**
* Returns {@code true} if this exception is due to a TLS problem (either
directly or because of its cause, if present). Traverses the cause chain
recursively.
*
diff --git
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
index bceac03fde3..4605f3ca089 100644
---
a/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
+++
b/nifi-framework-bundle/nifi-framework/nifi-framework-cluster-protocol/src/test/java/org/apache/nifi/io/socket/SocketUtilsTest.java
@@ -101,6 +101,19 @@ class SocketUtilsTest {
assertThrows(SSLHandshakeException.class, () ->
connectAndHandshake(keyPair, certificate));
}
+ @Test
+ void testGetRemoteHostStringAcceptedSocket() throws IOException {
+ final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
+ try (
+ ServerSocket serverSocket = new ServerSocket(0, 1,
loopbackAddress);
+ Socket clientSocket = new Socket(loopbackAddress,
serverSocket.getLocalPort());
+ Socket acceptedSocket = serverSocket.accept()
+ ) {
+ assertTrue(clientSocket.isConnected());
+ assertEquals(loopbackAddress.getHostAddress(),
SocketUtils.getRemoteHostString(acceptedSocket));
+ }
+ }
+
@Test
void testCreateSocketConnected() throws IOException {
try (ServerSocket serverSocket = new ServerSocket(0, 1,
InetAddress.getLoopbackAddress())) {