liuml07 commented on a change in pull request #2327:
URL: https://github.com/apache/hadoop/pull/2327#discussion_r495375367



##########
File path: 
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java
##########
@@ -108,24 +117,86 @@ public String toString() {
   }
 
   /** The caller context builder. */
-  public static final class Builder {
-    private final String context;
+  public static final class Builder implements Configurable {

Review comment:
       I am not sure we need to implement the `Configurable` to make this 
flexible. How about:
   1. Add a new final `separator` field
   1. Add a new final `StringBuilder sb = new StringBuilder();` field and 
remove `context` variable
   1. Existing constructor `public Builder(String context)` will simply 
call`sb.append(context)` and assign `separator` filed with default value `,` 
aka `HADOOP_CALLER_CONTEXT_ITEM_SEPARATOR_DEFAULT`
   1. Create a new constructor `public Builder(Configuration conf)` which 
initializes the `separator` with the config, do not save the `conf` since we do 
not need it
   1. Add a new method `public append(String key, String value)` which calls 
`sb.append(key).append(separator).append(value)`
   1. Replace all places using `builder.context` with `builder.sb.toString()`, 
especially in `CallerContext(Builder b)` constructor.
   
   This seems very straightforward without losing configurability:
   1. it will not allow user to mix different separator when appending multiple 
key/values since it's final either from the default `,` or from Configuration
   1. no need to create a new string builder every time calling `append(key, 
value)`
   1. user does not need to call `builder.setConf(conf)`
   1. we do not  save `conf` by implementing `Configurable` interface so this 
builder lightweight
   
   Thoughts?
   

##########
File path: 
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java
##########
@@ -0,0 +1,47 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.hadoop.ipc;
+
+import org.apache.hadoop.conf.Configuration;
+import org.junit.Assert;
+import org.junit.Test;
+
+import static 
org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_CALLER_CONTEXT_ITEM_SEPARATOR_DEFAULT;
+import static 
org.apache.hadoop.fs.CommonConfigurationKeysPublic.HADOOP_CALLER_CONTEXT_ITEM_SEPARATOR_KEY;
+
+public class TestCallerContext {
+  @Test
+  public void testBuilderAppend() {
+    Configuration conf = new Configuration();
+    conf.set(HADOOP_CALLER_CONTEXT_ITEM_SEPARATOR_KEY, "$");
+    CallerContext.Builder builder = new CallerContext.Builder(null);
+    builder.setConf(conf);
+    CallerContext context =
+        builder.append("context1").append("context2").append("key3", "value3", 
":").build();
+    Assert.assertEquals(true,
+        context.getContext().contains("$"));
+    String[] items = context.getContext().split("\\$");
+    Assert.assertEquals(3, items.length);
+    Assert.assertEquals(true, items[2].equals("key3:value3"));
+
+    builder.append("$$");
+    Assert.assertEquals(true,

Review comment:
       User `assertEquals` to compare value, not compare the `true` with return 
value of `equals`.
   
   ```
   assertEquals("context1$context2$key3:value3$$$", 
builder.build().getContext());
   ```
   
   Simple reason: if it fails, the suggested way shows you expected value and 
real value of the context - which can help you debug.

##########
File path: 
hadoop-common-project/hadoop-common/src/main/resources/core-default.xml
##########
@@ -3826,6 +3826,13 @@
       in audit logs.
     </description>
   </property>
+  <property>
+    <name>hadoop.caller.context.item.separator</name>
+    <value>,</value>
+    <description>
+      The separator is for context which maybe contain many items.

Review comment:
       Add an example with two key/value items to make this embarrassingly 
clear?

##########
File path: 
hadoop-common-project/hadoop-common/src/main/resources/core-default.xml
##########
@@ -3826,6 +3826,13 @@
       in audit logs.
     </description>
   </property>
+  <property>
+    <name>hadoop.caller.context.item.separator</name>

Review comment:
       `hadoop.caller.context.separator` would just work for me. Shorter and 
easier to remember.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to