From 1035f550b9019ed7fe6479dc37a4858aafa33d12 Mon Sep 17 00:00:00 2001 From: Tsuyoshi Ozawa Date: Mon, 4 May 2015 15:02:21 +0900 Subject: [PATCH] MAPREDUCE-5905. CountersStrings.toEscapedCompactStrings outputs unnecessary null strings. Contributed by Akira AJISAKA. (cherry picked from commit 3ba18362f2a4b83635b89aa0adc5ebaf27d9ca83) --- hadoop-mapreduce-project/CHANGES.txt | 3 +++ .../hadoop/mapreduce/util/CountersStrings.java | 17 ++--------------- .../org/apache/hadoop/mapred/TestCounters.java | 3 +++ 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/hadoop-mapreduce-project/CHANGES.txt b/hadoop-mapreduce-project/CHANGES.txt index 963da70cd5c..84c5f854003 100644 --- a/hadoop-mapreduce-project/CHANGES.txt +++ b/hadoop-mapreduce-project/CHANGES.txt @@ -107,6 +107,9 @@ Release 2.8.0 - UNRELEASED MAPREDUCE-6345. Documentation fix for when CRLA is enabled for MRAppMaster logs. (Rohit Agarwal via gera) + MAPREDUCE-5905. CountersStrings.toEscapedCompactStrings outputs + unnecessary "null" strings. (Akira AJISAKA via ozawa) + Release 2.7.1 - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/util/CountersStrings.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/util/CountersStrings.java index ce799f5f2b6..ac16c127c29 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/util/CountersStrings.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/util/CountersStrings.java @@ -151,25 +151,12 @@ public class CountersStrings { public static , T extends AbstractCounters> String toEscapedCompactString(T counters) { - String[] groupsArray; - int length = 0; + StringBuilder builder = new StringBuilder(); synchronized(counters) { - groupsArray = new String[counters.countCounters()]; - int i = 0; - // First up, obtain the escaped string for each group so that we can - // determine the buffer length apriori. for (G group : counters) { - String escapedString = toEscapedCompactString(group); - groupsArray[i++] = escapedString; - length += escapedString.length(); + builder.append(toEscapedCompactString(group)); } } - - // Now construct the buffer - StringBuilder builder = new StringBuilder(length); - for (String group : groupsArray) { - builder.append(group); - } return builder.toString(); } diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapred/TestCounters.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapred/TestCounters.java index 46e7221a83b..5e2763e0036 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapred/TestCounters.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapred/TestCounters.java @@ -18,6 +18,7 @@ package org.apache.hadoop.mapred; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -84,6 +85,8 @@ public class TestCounters { */ private void testCounter(Counters counter) throws ParseException { String compactEscapedString = counter.makeEscapedCompactString(); + assertFalse("compactEscapedString should not contain null", + compactEscapedString.contains("null")); Counters recoveredCounter = Counters.fromEscapedCompactString(compactEscapedString);