From 1bcfab8e7fd8562f1829ac484d2f6c91f7afe3d6 Mon Sep 17 00:00:00 2001 From: Haohui Mai Date: Thu, 4 Feb 2016 11:28:45 -0800 Subject: [PATCH] HDFS-9724. Degraded performance in WebHDFS listing as it does not reuse ObjectMapper. Contributed by Akira Ajisaka. --- .../hadoop/hdfs/web/WebHdfsFileSystem.java | 6 +++-- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 +++ .../org/apache/hadoop/hdfs/web/JsonUtil.java | 23 ++++++++++--------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java index d806d5544c..82cf65516a 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/web/WebHdfsFileSystem.java @@ -87,6 +87,7 @@ import org.apache.hadoop.util.Progressable; import org.apache.hadoop.util.StringUtils; import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.ObjectReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -128,6 +129,8 @@ public class WebHdfsFileSystem extends FileSystem private InetSocketAddress nnAddrs[]; private int currentNNAddrIndex; private boolean disallowFallbackToInsecureCluster; + private static final ObjectReader READER = + new ObjectMapper().reader(Map.class); /** * Return the protocol scheme for the FileSystem. @@ -361,8 +364,7 @@ private Path makeAbsolute(Path f) { + "\" (parsed=\"" + parsed + "\")"); } } - ObjectMapper mapper = new ObjectMapper(); - return mapper.reader(Map.class).readValue(in); + return READER.readValue(in); } finally { in.close(); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 846eb2b8b4..dc27f914ad 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -2775,6 +2775,9 @@ Release 2.7.3 - UNRELEASED HDFS-9730. Storage ID update does not happen when there is a layout change (Tsz Wo Nicholas Sze via kihwal) + HDFS-9724. Degraded performance in WebHDFS listing as it does not reuse + ObjectMapper. (Akira AJISAKA via wheat9) + Release 2.7.2 - 2016-01-25 INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/JsonUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/JsonUtil.java index 1f5eaf6cce..342f719872 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/JsonUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/web/JsonUtil.java @@ -38,6 +38,12 @@ public class JsonUtil { private static final Object[] EMPTY_OBJECT_ARRAY = {}; + // Reuse ObjectMapper instance for improving performance. + // ObjectMapper is thread safe as long as we always configure instance + // before use. We don't have a re-entrant call pattern in WebHDFS, + // so we just need to worry about thread-safety. + private static final ObjectMapper MAPPER = new ObjectMapper(); + /** Convert a token object to a Json string. */ public static String toJsonString(final Token token ) throws IOException { @@ -72,9 +78,8 @@ private static String toJsonString(final Class clazz, final Object value) { public static String toJsonString(final String key, final Object value) { final Map m = new TreeMap(); m.put(key, value); - ObjectMapper mapper = new ObjectMapper(); try { - return mapper.writeValueAsString(m); + return MAPPER.writeValueAsString(m); } catch (IOException ignored) { } return null; @@ -116,10 +121,9 @@ public static String toJsonString(final HdfsFileStatus status, m.put("fileId", status.getFileId()); m.put("childrenNum", status.getChildrenNum()); m.put("storagePolicy", status.getStoragePolicy()); - ObjectMapper mapper = new ObjectMapper(); try { return includeType ? - toJsonString(FileStatus.class, m) : mapper.writeValueAsString(m); + toJsonString(FileStatus.class, m) : MAPPER.writeValueAsString(m); } catch (IOException ignored) { } return null; @@ -331,9 +335,8 @@ public static String toJsonString(final AclStatus status) { new TreeMap>(); finalMap.put(AclStatus.class.getSimpleName(), m); - ObjectMapper mapper = new ObjectMapper(); try { - return mapper.writeValueAsString(finalMap); + return MAPPER.writeValueAsString(finalMap); } catch (IOException ignored) { } return null; @@ -371,8 +374,7 @@ public static String toJsonString(final List xAttrs, final XAttrCodec encoding) throws IOException { final Map finalMap = new TreeMap(); finalMap.put("XAttrs", toJsonArray(xAttrs, encoding)); - ObjectMapper mapper = new ObjectMapper(); - return mapper.writeValueAsString(finalMap); + return MAPPER.writeValueAsString(finalMap); } public static String toJsonString(final List xAttrs) @@ -381,11 +383,10 @@ public static String toJsonString(final List xAttrs) for (XAttr xAttr : xAttrs) { names.add(XAttrHelper.getPrefixedName(xAttr)); } - ObjectMapper mapper = new ObjectMapper(); - String ret = mapper.writeValueAsString(names); + String ret = MAPPER.writeValueAsString(names); final Map finalMap = new TreeMap(); finalMap.put("XAttrNames", ret); - return mapper.writeValueAsString(finalMap); + return MAPPER.writeValueAsString(finalMap); } }