Skip to content

Commit 0bad062

Browse files
committed
HDFS-16310. RBF: Add client port to CallerContext for Router
1 parent c9f95b0 commit 0bad062

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

hadoop-hdfs-project/hadoop-hdfs-rbf/src/main/java/org/apache/hadoop/hdfs/server/federation/router/RouterRpcClient.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ public class RouterRpcClient {
132132
Pattern.compile("\\tat (.*)\\.(.*)\\((.*):(\\d*)\\)");
133133

134134
private static final String CLIENT_IP_STR = "clientIp";
135+
private static final String CLIENT_PORT_STR = "clientPort";
135136

136137
/** Fairness manager to control handlers assigned per NS. */
137138
private RouterRpcFairnessPolicyController routerRpcFairnessPolicyController;
@@ -465,7 +466,7 @@ private Object invokeMethod(
465466
+ router.getRouterId());
466467
}
467468

468-
appendClientIpToCallerContextIfAbsent();
469+
appendClientIpPortToCallerContextIfAbsent();
469470

470471
Object ret = null;
471472
if (rpcMonitor != null) {
@@ -586,25 +587,20 @@ private Object invokeMethod(
586587

587588
/**
588589
* For tracking which is the actual client address.
589-
* It adds trace info "clientIp:ip" to caller context if it's absent.
590+
* It adds trace info "clientIp:ip" and "clientPort:port"
591+
* to caller context if they are absent.
590592
*/
591-
private void appendClientIpToCallerContextIfAbsent() {
592-
String clientIpInfo = CLIENT_IP_STR + ":" + Server.getRemoteAddress();
593-
final CallerContext ctx = CallerContext.getCurrent();
594-
if (isClientIpInfoAbsent(clientIpInfo, ctx)) {
595-
String origContext = ctx == null ? null : ctx.getContext();
596-
byte[] origSignature = ctx == null ? null : ctx.getSignature();
597-
CallerContext.setCurrent(
598-
new CallerContext.Builder(origContext, contextFieldSeparator)
599-
.append(clientIpInfo)
600-
.setSignature(origSignature)
601-
.build());
602-
}
603-
}
604-
605-
private boolean isClientIpInfoAbsent(String clientIpInfo, CallerContext ctx){
606-
return ctx == null || ctx.getContext() == null
607-
|| !ctx.getContext().contains(clientIpInfo);
593+
private void appendClientIpPortToCallerContextIfAbsent() {
594+
CallerContext ctx = CallerContext.getCurrent();
595+
String origContext = ctx == null ? null : ctx.getContext();
596+
byte[] origSignature = ctx == null ? null : ctx.getSignature();
597+
CallerContext.setCurrent(
598+
new CallerContext.Builder(origContext, contextFieldSeparator)
599+
.appendIfAbsent(CLIENT_IP_STR, Server.getRemoteAddress())
600+
.appendIfAbsent(CLIENT_PORT_STR,
601+
Integer.toString(Server.getRemotePort()))
602+
.setSignature(origSignature)
603+
.build());
608604
}
609605

610606
/**

hadoop-hdfs-project/hadoop-hdfs-rbf/src/test/java/org/apache/hadoop/hdfs/server/federation/router/TestRouterRpc.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,4 +1967,39 @@ public void testSetBalancerBandwidth() throws Exception {
19671967
return datanodes.get(0).getBalancerBandwidth() == newBandwidth;
19681968
}, 100, 60 * 1000);
19691969
}
1970+
1971+
@Test
1972+
public void testAddClientIpPortToCallerContext() throws IOException {
1973+
GenericTestUtils.LogCapturer auditLog =
1974+
GenericTestUtils.LogCapturer.captureLogs(FSNamesystem.auditLog);
1975+
1976+
// 1. ClientIp and ClientPort are not set on the client.
1977+
// Set client context.
1978+
CallerContext.setCurrent(
1979+
new CallerContext.Builder("clientContext").build());
1980+
1981+
// Create a directory via the router.
1982+
String dirPath = "/test";
1983+
routerProtocol.mkdirs(dirPath, new FsPermission("755"), false);
1984+
1985+
// The audit log should contains "clientIp:" and "clientPort:".
1986+
assertTrue(auditLog.getOutput().contains("clientIp:"));
1987+
assertTrue(auditLog.getOutput().contains("clientPort:"));
1988+
assertTrue(verifyFileExists(routerFS, dirPath));
1989+
auditLog.clearOutput();
1990+
1991+
// 2. ClientIp and ClientPort are set on the client.
1992+
// Reset client context.
1993+
CallerContext.setCurrent(
1994+
new CallerContext.Builder(
1995+
"clientContext,clientIp:1.1.1.1,clientPort:1234").build());
1996+
1997+
// Create a directory via the router.
1998+
routerProtocol.getFileInfo(dirPath);
1999+
2000+
// The audit log should contains the original clientIp and clientPort
2001+
// set by client.
2002+
assertTrue(auditLog.getOutput().contains("clientIp:1.1.1.1"));
2003+
assertTrue(auditLog.getOutput().contains("clientPort:1234"));
2004+
}
19702005
}

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ private void logAuditEvent(boolean succeeded,
462462

463463
private void appendClientPortToCallerContextIfAbsent() {
464464
final CallerContext ctx = CallerContext.getCurrent();
465-
if (isClientPortInfoAbsent(CLIENT_PORT_STR + ":" + Server.getRemotePort(),
466-
ctx)) {
465+
if (isClientPortInfoAbsent(ctx)) {
467466
String origContext = ctx == null ? null : ctx.getContext();
468467
byte[] origSignature = ctx == null ? null : ctx.getSignature();
469468
CallerContext.setCurrent(
@@ -474,9 +473,9 @@ private void appendClientPortToCallerContextIfAbsent() {
474473
}
475474
}
476475

477-
private boolean isClientPortInfoAbsent(String clientPortInfo, CallerContext ctx){
476+
private boolean isClientPortInfoAbsent(CallerContext ctx){
478477
return ctx == null || ctx.getContext() == null
479-
|| !ctx.getContext().contains(clientPortInfo);
478+
|| !ctx.getContext().contains(CLIENT_PORT_STR);
480479
}
481480

482481
/**

0 commit comments

Comments
 (0)