Skip to content

Commit 021fcc6

Browse files
authored
HADOOP-18628. IPC Server Connection should log host name before returning VersionMismatch error (apache#5385)
Contributed by Viraj Jasani
1 parent fe0541b commit 021fcc6

File tree

3 files changed

+39
-12
lines changed

3 files changed

+39
-12
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/Server.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -1985,11 +1985,26 @@ public class Connection {
19851985
private long lastContact;
19861986
private int dataLength;
19871987
private Socket socket;
1988+
19881989
// Cache the remote host & port info so that even if the socket is
19891990
// disconnected, we can say where it used to connect to.
1990-
private String hostAddress;
1991-
private int remotePort;
1992-
private InetAddress addr;
1991+
1992+
/**
1993+
* Client Host IP address from where the socket connection is being established to the Server.
1994+
*/
1995+
private final String hostAddress;
1996+
/**
1997+
* Client remote port used for the given socket connection.
1998+
*/
1999+
private final int remotePort;
2000+
/**
2001+
* Address to which the socket is connected to.
2002+
*/
2003+
private final InetAddress addr;
2004+
/**
2005+
* Client Host address from where the socket connection is being established to the Server.
2006+
*/
2007+
private final String hostName;
19932008

19942009
IpcConnectionContextProto connectionContext;
19952010
String protocolName;
@@ -2033,8 +2048,12 @@ public Connection(SocketChannel channel, long lastContact,
20332048
this.isOnAuxiliaryPort = isOnAuxiliaryPort;
20342049
if (addr == null) {
20352050
this.hostAddress = "*Unknown*";
2051+
this.hostName = this.hostAddress;
20362052
} else {
2053+
// host IP address
20372054
this.hostAddress = addr.getHostAddress();
2055+
// host name for the IP address
2056+
this.hostName = addr.getHostName();
20382057
}
20392058
this.remotePort = socket.getPort();
20402059
this.responseQueue = new LinkedList<RpcCall>();
@@ -2050,7 +2069,7 @@ public Connection(SocketChannel channel, long lastContact,
20502069

20512070
@Override
20522071
public String toString() {
2053-
return getHostAddress() + ":" + remotePort;
2072+
return hostName + ":" + remotePort + " / " + hostAddress + ":" + remotePort;
20542073
}
20552074

20562075
boolean setShouldClose() {
@@ -2463,19 +2482,18 @@ public int readAndProcess() throws IOException, InterruptedException {
24632482
return -1;
24642483
}
24652484

2466-
if(!RpcConstants.HEADER.equals(dataLengthBuffer)) {
2467-
LOG.warn("Incorrect RPC Header length from {}:{} "
2468-
+ "expected length: {} got length: {}",
2469-
hostAddress, remotePort, RpcConstants.HEADER, dataLengthBuffer);
2485+
if (!RpcConstants.HEADER.equals(dataLengthBuffer)) {
2486+
LOG.warn("Incorrect RPC Header length from {}:{} / {}:{}. Expected: {}. Actual: {}",
2487+
hostName, remotePort, hostAddress, remotePort, RpcConstants.HEADER,
2488+
dataLengthBuffer);
24702489
setupBadVersionResponse(version);
24712490
return -1;
24722491
}
24732492
if (version != CURRENT_VERSION) {
24742493
//Warning is ok since this is not supposed to happen.
2475-
LOG.warn("Version mismatch from " +
2476-
hostAddress + ":" + remotePort +
2477-
" got version " + version +
2478-
" expected version " + CURRENT_VERSION);
2494+
LOG.warn("Version mismatch from {}:{} / {}:{}. "
2495+
+ "Expected version: {}. Actual version: {} ", hostName,
2496+
remotePort, hostAddress, remotePort, CURRENT_VERSION, version);
24792497
setupBadVersionResponse(version);
24802498
return -1;
24812499
}

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestIPC.java

+4
Original file line numberDiff line numberDiff line change
@@ -1168,6 +1168,10 @@ private static void callAndVerify(Server server, InetSocketAddress addr,
11681168

11691169
call(client, addr, serviceClass, conf);
11701170
Connection connection = server.getConnections()[0];
1171+
LOG.info("Connection is from: {}", connection);
1172+
assertEquals(
1173+
"Connection string representation should include both IP address and Host name", 2,
1174+
connection.toString().split(" / ").length);
11711175
int serviceClass2 = connection.getServiceClass();
11721176
assertFalse(noChanged ^ serviceClass == serviceClass2);
11731177
client.stop();

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestRPC.java

+5
Original file line numberDiff line numberDiff line change
@@ -1849,6 +1849,11 @@ public RpcStatusProto getRpcStatusProto() {
18491849
// if it wasn't fatal, verify there's only one open connection.
18501850
Connection[] conns = server.getConnections();
18511851
assertEquals(reqName, 1, conns.length);
1852+
String connectionInfo = conns[0].toString();
1853+
LOG.info("Connection is from: {}", connectionInfo);
1854+
assertEquals(
1855+
"Connection string representation should include both IP address and Host name", 2,
1856+
connectionInfo.split(" / ").length);
18521857
// verify whether the connection should have been reused.
18531858
if (isDisconnected) {
18541859
assertNotSame(reqName, lastConn, conns[0]);

0 commit comments

Comments
 (0)