From 6ba99741086170b83c38d3e7e715d9e8046a1e00 Mon Sep 17 00:00:00 2001 From: Andrew Wang Date: Mon, 2 Jul 2018 12:02:19 +0200 Subject: [PATCH] HDFS-13703. Avoid allocation of CorruptedBlocks hashmap when no corrupted blocks are hit. Contributed by Todd Lipcon. --- .../java/org/apache/hadoop/hdfs/DFSInputStream.java | 2 +- .../java/org/apache/hadoop/hdfs/DFSUtilClient.java | 11 ++++++----- .../apache/hadoop/hdfs/server/datanode/DataNode.java | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java index 573b860fab1..1bdc50a5e27 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java @@ -1423,7 +1423,7 @@ public class DFSInputStream extends FSInputStream Map> corruptedBlockMap = corruptedBlocks.getCorruptionMap(); - if (corruptedBlockMap.isEmpty()) { + if (corruptedBlockMap == null) { return; } List reportList = new ArrayList<>(corruptedBlockMap.size()); diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java index 6c0b106ab21..313b973550c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java @@ -751,14 +751,14 @@ public class DFSUtilClient { public static class CorruptedBlocks { private Map> corruptionMap; - public CorruptedBlocks() { - this.corruptionMap = new HashMap<>(); - } - /** * Indicate a block replica on the specified datanode is corrupted */ public void addCorruptedBlock(ExtendedBlock blk, DatanodeInfo node) { + if (corruptionMap == null) { + corruptionMap = new HashMap<>(); + } + Set dnSet = corruptionMap.get(blk); if (dnSet == null) { dnSet = new HashSet<>(); @@ -770,7 +770,8 @@ public class DFSUtilClient { } /** - * @return the map that contains all the corruption entries. + * @return the map that contains all the corruption entries, or null if + * there were no corrupted entries */ public Map> getCorruptionMap() { return corruptionMap; diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java index 96b0f36f9e6..4baafb92847 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java @@ -1278,7 +1278,7 @@ public class DataNode extends ReconfigurableBase DFSUtilClient.CorruptedBlocks corruptedBlocks) throws IOException { Map> corruptionMap = corruptedBlocks.getCorruptionMap(); - if (!corruptionMap.isEmpty()) { + if (corruptionMap != null) { for (Map.Entry> entry : corruptionMap.entrySet()) { for (DatanodeInfo dnInfo : entry.getValue()) {