From bf0a1d02d63b9a73cc864532d8d387cb336da038 Mon Sep 17 00:00:00 2001 From: tom lee Date: Thu, 11 Nov 2021 09:49:40 +0800 Subject: [PATCH 1/3] HADOOP-18003. Add a method appendIfAbsent for CallerContext --- .../org/apache/hadoop/ipc/CallerContext.java | 20 ++++++++++++ .../apache/hadoop/ipc/TestCallerContext.java | 31 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java index 378b83d13b0c7..9f9c9741f36bb 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc/CallerContext.java @@ -228,6 +228,26 @@ public Builder append(String key, String value) { return this; } + /** + * Append new field which contains key and value to the context + * if the key("key:") is absent. + * @param key the key of field. + * @param value the value of field. + * @return the builder. + */ + public Builder appendIfAbsent(String key, String value) { + if (sb.toString().contains(key + KEY_VALUE_SEPARATOR)) { + return this; + } + if (isValid(key) && isValid(value)) { + if (sb.length() > 0) { + sb.append(fieldSeparator); + } + sb.append(key).append(KEY_VALUE_SEPARATOR).append(value); + } + return this; + } + public CallerContext build() { return new CallerContext(this); } diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java index fc1057b9f766d..933defab020ac 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java @@ -42,6 +42,37 @@ public void testBuilderAppend() { builder.build().getContext()); } + @Test + public void testBuilderAppendIfAbsent() { + Configuration conf = new Configuration(); + conf.set(HADOOP_CALLER_CONTEXT_SEPARATOR_KEY, "$"); + CallerContext.Builder builder = new CallerContext.Builder(null, conf); + CallerContext context = builder.append("key1", "value1").build(); + Assert.assertEquals("key1:value1", + builder.build().getContext()); + + // Append an existed key. + builder.appendIfAbsent("key1", "value1"); + String[] items = context.getContext().split("\\$"); + Assert.assertEquals(1, items.length); + Assert.assertEquals("key1:value1", + builder.build().getContext()); + + // Append an absent key. + builder.appendIfAbsent("key2", "value2"); + String[] items2 = builder.build().getContext().split("\\$"); + Assert.assertEquals(2, items2.length); + Assert.assertEquals("key1:value1$key2:value2", + builder.build().getContext()); + + // Append a key that is a substring of an existing key. + builder.appendIfAbsent("key", "value"); + String[] items3 = builder.build().getContext().split("\\$"); + Assert.assertEquals(3, items3.length); + Assert.assertEquals("key1:value1$key2:value2$key:value", + builder.build().getContext()); + } + @Test(expected = IllegalArgumentException.class) public void testNewBuilder() { Configuration conf = new Configuration(); From e4b6b0383084d46965d725c7d1f9615d37fc6f79 Mon Sep 17 00:00:00 2001 From: tom lee Date: Fri, 12 Nov 2021 10:23:57 +0800 Subject: [PATCH 2/3] update the unit test --- .../test/java/org/apache/hadoop/ipc/TestCallerContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java index 933defab020ac..b2050fbf9a351 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java @@ -51,8 +51,8 @@ public void testBuilderAppendIfAbsent() { Assert.assertEquals("key1:value1", builder.build().getContext()); - // Append an existed key. - builder.appendIfAbsent("key1", "value1"); + // Append an existed key with different value. + builder.appendIfAbsent("key1", "value2"); String[] items = context.getContext().split("\\$"); Assert.assertEquals(1, items.length); Assert.assertEquals("key1:value1", From 989274af29497873f662bcf8c3fedab2cf0294b0 Mon Sep 17 00:00:00 2001 From: tom lee Date: Fri, 12 Nov 2021 10:32:29 +0800 Subject: [PATCH 3/3] update the unit test --- .../test/java/org/apache/hadoop/ipc/TestCallerContext.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java index b2050fbf9a351..bb4a119e7db29 100644 --- a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java +++ b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/ipc/TestCallerContext.java @@ -47,13 +47,13 @@ public void testBuilderAppendIfAbsent() { Configuration conf = new Configuration(); conf.set(HADOOP_CALLER_CONTEXT_SEPARATOR_KEY, "$"); CallerContext.Builder builder = new CallerContext.Builder(null, conf); - CallerContext context = builder.append("key1", "value1").build(); + builder.append("key1", "value1"); Assert.assertEquals("key1:value1", builder.build().getContext()); // Append an existed key with different value. builder.appendIfAbsent("key1", "value2"); - String[] items = context.getContext().split("\\$"); + String[] items = builder.build().getContext().split("\\$"); Assert.assertEquals(1, items.length); Assert.assertEquals("key1:value1", builder.build().getContext());