Skip to content

Commit

Permalink
[#4389] Fix hostname retrieval triggering possible DNS lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
Xylus committed Jul 31, 2018
1 parent 0bdabeb commit 39d72bf
Show file tree
Hide file tree
Showing 10 changed files with 525 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.nio.channels.SelectionKey;
import java.util.concurrent.CountDownLatch;

import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
import org.apache.thrift.TServiceClient;
Expand Down Expand Up @@ -79,7 +81,10 @@ public String echo(String message) throws TException {

@Override
public void verifyTraces(PluginTestVerifier verifier, String expectedMessage) throws Exception {
final InetSocketAddress actualServerAddress = this.environment.getServerAddress();
final InetSocketAddress socketAddress = this.environment.getServerAddress();
final String hostName = SocketAddressUtils.getHostNameFirst(socketAddress);
// refer to com.navercorp.pinpoint.plugin.thrift.ThriftUtils#getHostPort
final String remoteAddress = HostAndPort.toHostAndPortString(hostName, socketAddress.getPort());
// ********** Asynchronous Traces
// SpanEvent - Asynchronous Invocation
ExpectedTrace asyncInvocationTrace = event("ASYNC", "Asynchronous Invocation");
Expand All @@ -101,13 +106,13 @@ public void verifyTraces(PluginTestVerifier verifier, String expectedMessage) th
// ********** Root trace for Asynchronous traces
// SpanEvent - TAsyncClientManager.call
Method call = TAsyncClientManager.class.getDeclaredMethod("call", TAsyncMethodCall.class);
ExpectedAnnotation thriftUrl = Expectations.annotation("thrift.url", actualServerAddress.getHostName() + ":"
+ actualServerAddress.getPort() + "/com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo_call");
ExpectedAnnotation thriftUrl = Expectations.annotation("thrift.url",
remoteAddress + "/com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo_call");
ExpectedTrace callTrace = event("THRIFT_CLIENT", // ServiceType
call, // Method
null, // rpc
null, // endPoint
actualServerAddress.getHostName() + ":" + actualServerAddress.getPort(), // destinationId
remoteAddress, // destinationId
thriftUrl // Annotation("thrift.url")
);
verifier.verifyTrace(async(callTrace, asyncInvocationTrace, cleanUpAndFireCallbackTrace, receiveBaseTrace));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.net.InetSocketAddress;

import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
Expand Down Expand Up @@ -60,14 +61,17 @@ public final String echo(String message) throws TException {

@Override
public void verifyTraces(PluginTestVerifier verifier, String expectedMessage) throws Exception {
final InetSocketAddress actualServerAddress = this.environment.getServerAddress();
final InetSocketAddress socketAddress = this.environment.getServerAddress();
final String hostName = SocketAddressUtils.getHostNameFirst(socketAddress);
// refer to com.navercorp.pinpoint.plugin.thrift.ThriftUtils#getHostPort
final String remoteAddress = HostAndPort.toHostAndPortString(hostName, socketAddress.getPort());
// SpanEvent - TServiceClient.sendBase
Method sendBase = TServiceClient.class.getDeclaredMethod("sendBase", String.class, TBase.class);
// refer to com.navercorp.pinpoint.plugin.thrift.ThriftUtils#getClientServiceName
ExpectedAnnotation thriftUrl = Expectations.annotation("thrift.url", actualServerAddress.getHostName() + ":"
+ actualServerAddress.getPort() + "/com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo");
ExpectedAnnotation thriftArgs = Expectations.annotation("thrift.args", "echo_args(message:" + expectedMessage
+ ")");
ExpectedAnnotation thriftUrl = Expectations.annotation("thrift.url",
remoteAddress + "/com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo");
ExpectedAnnotation thriftArgs = Expectations.annotation("thrift.args",
"echo_args(message:" + expectedMessage + ")");

// SpanEvent - TServiceClient.receiveBase
Method receiveBase = TServiceClient.class.getDeclaredMethod("receiveBase", TBase.class, String.class);
Expand All @@ -78,7 +82,7 @@ public void verifyTraces(PluginTestVerifier verifier, String expectedMessage) th
sendBase, // Method
null, // rpc
null, // endPoint
HostAndPort.toHostAndPortString(actualServerAddress.getHostName(), actualServerAddress.getPort()), // destinationId
remoteAddress, // destinationId
thriftUrl, // Annotation("thrift.url")
thriftArgs), // Annotation("thrift.args")
event("THRIFT_CLIENT_INTERNAL", // ServiceType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.Method;
import java.net.InetSocketAddress;

import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import org.apache.thrift.TBaseAsyncProcessor;
import org.apache.thrift.TProcessor;
import org.apache.thrift.server.AbstractNonblockingServer;
Expand Down Expand Up @@ -49,15 +51,16 @@ protected AsyncEchoTestServer(T server, TestEnvironment environment) throws TTra

@Override
public void verifyServerTraces(PluginTestVerifier verifier) throws Exception {
final InetSocketAddress actualServerAddress = super.environment.getServerAddress();
final InetSocketAddress socketAddress = super.environment.getServerAddress();
final String address = SocketAddressUtils.getAddressFirst(socketAddress);
verifier.verifyTraceCount(2);
Method process = TBaseAsyncProcessor.class.getDeclaredMethod("process", AsyncFrameBuffer.class);
// RootSpan
verifier.verifyTrace(root("THRIFT_SERVER", // ServiceType,
"Thrift Server Invocation", // Method
"com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo", // rpc
actualServerAddress.getHostName() + ":" + actualServerAddress.getPort(), // endPoint
actualServerAddress.getHostName() // remoteAddress
HostAndPort.toHostAndPortString(address, socketAddress.getPort()), // endPoint
address // remoteAddress
));
// SpanEvent - TBaseAsyncProcessor.process
verifier.verifyTrace(event("THRIFT_SERVER_INTERNAL", process));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.Method;
import java.net.InetSocketAddress;

import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils;
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
import org.apache.thrift.TBaseProcessor;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TProtocol;
Expand Down Expand Up @@ -52,16 +54,17 @@ protected SyncEchoTestServer(T server, TestEnvironment environment) throws TTran

@Override
public void verifyServerTraces(PluginTestVerifier verifier) throws Exception {
final InetSocketAddress actualServerAddress = super.environment.getServerAddress();
final InetSocketAddress socketAddress = super.environment.getServerAddress();
final String address = SocketAddressUtils.getAddressFirst(socketAddress);
verifier.verifyTraceCount(2);
Method process = TBaseProcessor.class.getDeclaredMethod("process", TProtocol.class, TProtocol.class);
verifier.verifyDiscreteTrace(
// RootSpan - Thrift Server Invocation
root("THRIFT_SERVER", // ServiceType,
"Thrift Server Invocation", // Method
"com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo", // rpc
actualServerAddress.getHostName() + ":" + actualServerAddress.getPort(), // endPoint
actualServerAddress.getHostName()), // remoteAddress
HostAndPort.toHostAndPortString(address, socketAddress.getPort()), // endPoint
address), // remoteAddress
// SpanEvent - TBaseProcessor.process
event("THRIFT_SERVER_INTERNAL", process));
verifier.verifyTraceCount(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/*
* Copyright 2018 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.bootstrap.plugin.util;

import com.navercorp.pinpoint.bootstrap.logging.PLogger;
import com.navercorp.pinpoint.bootstrap.logging.PLoggerFactory;
import com.navercorp.pinpoint.common.util.JvmUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;

/**
* @author HyunGil Jeong
*/
public final class SocketAddressUtils {

private static final PLogger LOGGER = PLoggerFactory.getLogger(SocketAddressUtils.class);

// TODO JDK 7 InetSocketAddress.getHostString() - reflection not needed once we drop JDK 6 support.
private static final HostStringAccessor HOST_STRING_ACCESSOR = createHostStringAccessor();

private static HostStringAccessor createHostStringAccessor() {
try {
final Method m = InetSocketAddress.class.getDeclaredMethod("getHostString");
m.setAccessible(true);
return new ReflectiveHostStringAccessor(m);
} catch (NoSuchMethodException e) {
LOGGER.error("[{}] {} - getHostString() not present in class InetSocketAddress.",
JvmUtils.getType(), JvmUtils.getVersion());
throw new IllegalStateException("Unexpected InetSocketAddress class", e);
}
}

private interface HostStringAccessor {
String getHostString(InetSocketAddress inetSocketAddress);
}

private static class ReflectiveHostStringAccessor implements HostStringAccessor {

private final PLogger logger = PLoggerFactory.getLogger(this.getClass());

private final Method method;

private ReflectiveHostStringAccessor(Method method) {
if (method == null) {
throw new NullPointerException("method must not be null");
}
this.method = method;
}

@Override
public String getHostString(InetSocketAddress inetSocketAddress) {
try {
return (String) method.invoke(inetSocketAddress);
} catch (IllegalAccessException e) {
logger.error("[{}] {} - Cannot access method : {}",
JvmUtils.getType(), JvmUtils.getVersion(), method.getName(), e);
} catch (InvocationTargetException e) {
logger.error("[{}] {} - Error invoking method : {}",
JvmUtils.getType(), JvmUtils.getVersion(), method.getName(), e);
} catch (Exception e) {
logger.error("[{}] {} - Unexpected error retrieving hostString",
JvmUtils.getType(), JvmUtils.getVersion(), e);
}
return null;
}
}

private SocketAddressUtils() {}

/**
* Returns the hostname of the given {@link InetSocketAddress}, or the String form of the IP address
* if it does not have one. Returns {@code null} if neither can be retrieved.
*
* <p> This <b>does not</b> trigger a reverse DNS lookup if was created using an address and the hostname
* is not yet available.
*
* @param inetSocketAddress the socket address in which to retrieve the hostname from
* @return the hostname or the String representation of the address, or {@code null} if neither
* can be found.
*
* @see java.net.InetSocketAddress#getHostString()
*/
public static String getHostNameFirst(InetSocketAddress inetSocketAddress) {
if (inetSocketAddress == null) {
return null;
}
return HOST_STRING_ACCESSOR.getHostString(inetSocketAddress);
}

/**
* Returns the hostname of the given {@link InetSocketAddress}, or the String form of the IP address
* if it does not have one. Returns {@code defaultHostName} if neither can be retrieved.
*
* <p>This <b>does not</b> trigger a reverse DNS lookup if was created using an address and the hostname
* is not yet available.
*
* @param inetSocketAddress the socket address in which to retrieve the hostname from
* @param defaultHostName the value to return if neither the hostname nor the string form of the
* address can be found
* @return the hostname or the String representation of the address, or the {@code defaultHostName}
* if neither can be found.
*
* @see java.net.InetSocketAddress#getHostString()
*/
public static String getHostNameFirst(InetSocketAddress inetSocketAddress, String defaultHostName) {
String hostName = getHostNameFirst(inetSocketAddress);
if (hostName == null) {
return defaultHostName;
}
return hostName;
}

/**
* Returns the String form of the IP address of the given {@link InetSocketAddress}, or the hostname
* if it has not been resolved. Returns {@code null} if neither can be retrieved.
*
* <p>This <b>does not</b> trigger a DNS lookup if the address has not been resolved yet by simply
* returning the hostname.
*
* @param inetSocketAddress the socket address in which to retrieve the address from
* @return the String representation of the address or the hostname, or {@code null} if neither
* can be found.
*
* @see InetSocketAddress#isUnresolved()
*/
public static String getAddressFirst(InetSocketAddress inetSocketAddress) {
if (inetSocketAddress == null) {
return null;
}
InetAddress inetAddress = inetSocketAddress.getAddress();
if (inetAddress != null) {
return inetAddress.getHostAddress();
}
// This won't trigger a DNS lookup as if it got to here, the hostName should not be null on the basis
// that InetSocketAddress does not allow both address and hostName fields to be null.
return inetSocketAddress.getHostName();
}

/**
* Returns the String form of the IP address of the given {@link InetSocketAddress}, or the hostname
* if it has not been resolved. Returns {@code defaultAddress} if neither can be retrieved.
*
* <p>This <b>does not</b> trigger a DNS lookup if the address has not been resolved yet by simply
* returning the hostname.
*
* @param inetSocketAddress the socket address in which to retrieve the address from
* @param defaultAddress the value to return if neither the string form of the address nor the
* hostname can be found
* @return the String representation of the address or the hostname, or {@code defaultAddress} if neither
* can be found.
*
* @see InetSocketAddress#isUnresolved()
*/
public static String getAddressFirst(InetSocketAddress inetSocketAddress, String defaultAddress) {
String address = getAddressFirst(inetSocketAddress);
if (address == null) {
return defaultAddress;
}
return address;
}
}

0 comments on commit 39d72bf

Please sign in to comment.