-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HDFS-16700. RBF: Record the real client IP carried by the Router in the NameNode log #4659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| import org.apache.commons.lang3.builder.HashCodeBuilder; | ||
| import org.apache.hadoop.classification.InterfaceAudience; | ||
| import org.apache.hadoop.classification.InterfaceStability; | ||
| import org.apache.hadoop.classification.VisibleForTesting; | ||
| import org.apache.hadoop.conf.Configuration; | ||
|
|
||
| import java.nio.charset.Charset; | ||
|
|
@@ -121,6 +122,51 @@ public String toString() { | |
| return str; | ||
| } | ||
|
|
||
| /** | ||
| * Try to obtain the value corresponding to the key by parsing the content. | ||
| * @param content the full content to be parsed. | ||
| * @param key trying to obtain the value of the key. | ||
| * @return the value corresponding to the key. | ||
| */ | ||
| @VisibleForTesting | ||
| public static String parseSpecialValue(String content, String key) { | ||
| int posn = content.indexOf(key); | ||
| if (posn != -1) { | ||
| posn += key.length(); | ||
| int end = content.indexOf(",", posn); | ||
| return end == -1 ? content.substring(posn) : content.substring(posn, end); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get client ip content from caller context. | ||
| * @param context The context here is obtained from outside. | ||
| * @return Filter the value carried by 'clientIp:' from the context. | ||
| * If not, return null. | ||
| */ | ||
| public static String getRealClientIp(String context) { | ||
| if (context != null && !context.equals("")) { | ||
This comment was marked as resolved.
Sorry, something went wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The string here is obtained from outside. |
||
| String ipKey = CLIENT_IP_STR + Builder.KEY_VALUE_SEPARATOR; | ||
| return parseSpecialValue(context, ipKey); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Get client port content from caller context. | ||
| * @param context The context here is obtained from outside. | ||
| * @return Filter the value carried by 'clientPort:' from the context. | ||
| * If not, return null. | ||
| */ | ||
| public static String getRealClientPort(String context) { | ||
| if (context != null && !context.equals("")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. !context.equals("") -> context.isContextValid()
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The string here is obtained from outside. |
||
| String portKey = CLIENT_PORT_STR + Builder.KEY_VALUE_SEPARATOR; | ||
| return parseSpecialValue(context, portKey); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** The caller context builder. */ | ||
| public static final class Builder { | ||
| public static final String KEY_VALUE_SEPARATOR = ":"; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,8 @@ | |
|
|
||
| package org.apache.hadoop.ipc; | ||
|
|
||
| import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_CALLER_CONTEXT_ENABLED_DEFAULT; | ||
| import static org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_CALLER_CONTEXT_ENABLED_KEY; | ||
| import static org.apache.hadoop.ipc.ProcessingDetails.Timing; | ||
| import static org.apache.hadoop.ipc.RpcConstants.AUTHORIZATION_FAILED_CALL_ID; | ||
| import static org.apache.hadoop.ipc.RpcConstants.CONNECTION_CONTEXT_CALL_ID; | ||
|
|
@@ -1110,6 +1112,10 @@ public void setDeferredResponse(Writable response) { | |
|
|
||
| public void setDeferredError(Throwable t) { | ||
| } | ||
|
|
||
| public CallerContext getCallerContext() { | ||
| return callerContext; | ||
| } | ||
| } | ||
|
|
||
| /** A RPC extended call queued for handling. */ | ||
|
|
@@ -1351,6 +1357,16 @@ private class ResponseParams { | |
|
|
||
| @Override | ||
| public String toString() { | ||
| boolean isCallerContextEnabled = conf.getBoolean( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this is a good idea to get the value from conf every time. And
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, the essence is to obtain the real client ip, and it is not a problem to use HADOOP_CALLER_CONTEXT_ENABLED_KEY. |
||
| HADOOP_CALLER_CONTEXT_ENABLED_KEY, | ||
| HADOOP_CALLER_CONTEXT_ENABLED_DEFAULT); | ||
| CallerContext context = getCallerContext(); | ||
| if (isCallerContextEnabled && context != null && context.isContextValid()) { | ||
| String cc = context.getContext(); | ||
goiri marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return super.toString() + " " + rpcRequest + " from " + connection + | ||
| ", client=" + CallerContext.getRealClientIp(cc) + ":" + | ||
| CallerContext.getRealClientPort(cc); | ||
| } | ||
| return super.toString() + " " + rpcRequest + " from " + connection; | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small javadoc with an example
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add some javadoc later.