From feb5b9641dcd61781d94c37af267a068dc916e5c Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Mon, 1 Jul 2013 01:28:22 +0000 Subject: [PATCH] HDFS-4797. BlockScanInfo does not override equals(..) and hashCode() consistently. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1498202 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 3 ++ .../datanode/BlockPoolSliceScanner.java | 37 ++++++++++++------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index be81c83c81..cd8faef90e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -251,6 +251,9 @@ Release 2.2.0 - UNRELEASED BUG FIXES + HDFS-4797. BlockScanInfo does not override equals(..) and hashCode() + consistently. (szetszwo) + Release 2.1.1-beta - UNRELEASED INCOMPATIBLE CHANGES diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BlockPoolSliceScanner.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BlockPoolSliceScanner.java index 8a117546ff..6ae19d6b6e 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BlockPoolSliceScanner.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/BlockPoolSliceScanner.java @@ -24,6 +24,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Collections; +import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.Iterator; @@ -80,7 +81,7 @@ class BlockPoolSliceScanner { private final FsDatasetSpi dataset; private final SortedSet blockInfoSet - = new TreeSet(); + = new TreeSet(BlockScanInfo.LAST_SCAN_TIME_COMPARATOR); private final Map blockMap = new HashMap(); @@ -107,8 +108,20 @@ private static enum ScanType { NONE, } - static class BlockScanInfo implements Comparable { - Block block; + static class BlockScanInfo { + /** Compare the info by the last scan time. */ + static final Comparator LAST_SCAN_TIME_COMPARATOR + = new Comparator() { + + @Override + public int compare(BlockScanInfo left, BlockScanInfo right) { + final long l = left.lastScanTime; + final long r = right.lastScanTime; + return l < r? -1: l > r? 1: 0; + } + }; + + final Block block; long lastScanTime = 0; ScanType lastScanType = ScanType.NONE; boolean lastScanOk = true; @@ -123,22 +136,18 @@ public int hashCode() { } @Override - public boolean equals(Object other) { - return other instanceof BlockScanInfo && - compareTo((BlockScanInfo)other) == 0; + public boolean equals(Object that) { + if (this == that) { + return true; + } else if (that == null || !(that instanceof BlockScanInfo)) { + return false; + } + return block.equals(((BlockScanInfo)that).block); } long getLastScanTime() { return (lastScanType == ScanType.NONE) ? 0 : lastScanTime; } - - @Override - public int compareTo(BlockScanInfo other) { - long t1 = lastScanTime; - long t2 = other.lastScanTime; - return ( t1 < t2 ) ? -1 : - (( t1 > t2 ) ? 1 : block.compareTo(other.block)); - } } BlockPoolSliceScanner(String bpid, DataNode datanode,