Pārlūkot izejas kodu

Merge -r 727190:727191 from trunk to branch-0.20 to fix HADOOP-4879.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/branches/branch-0.20@727193 13f79535-47bb-0310-9956-ffa450edef68
Arun Murthy 16 gadi atpakaļ
vecāks
revīzija
1da3851388

+ 4 - 0
CHANGES.txt

@@ -441,6 +441,10 @@ Release 0.20.0 - Unreleased
     HADOOP-4458. Add a test creating symlinks in the working directory.
     (Amareshwari Sriramadasu via cdouglas)
 
+    HADOOP-4879. Fix org.apache.hadoop.mapred.Counters to correctly define
+    Object.equals rather than depend on contentEquals api. (omalley via 
+    acmurthy)
+
 Release 0.19.1 - Unreleased
 
   IMPROVEMENTS

+ 23 - 9
src/mapred/org/apache/hadoop/mapred/Counters.java

@@ -211,13 +211,20 @@ public class Counters implements Writable, Iterable<Counters.Group> {
       buf.append(GROUP_CLOSE); // group end
       return buf.toString();
     }
-        
+
+    @Override
+    public int hashCode() {
+      return subcounters.hashCode();
+    }
+
     /** 
      * Checks for (content) equality of Groups
      */
-    synchronized boolean contentEquals(Group g) {
+    @Override
+    public synchronized boolean equals(Object obj) {
       boolean isEqual = false;
-      if (g != null) {
+      if (obj != null && obj instanceof Group) {
+        Group g = (Group) obj;
         if (size() == g.size()) {
           isEqual = true;
           for (Map.Entry<String, Counter> entry : subcounters.entrySet()) {
@@ -671,17 +678,24 @@ public class Counters implements Writable, Iterable<Counters.Group> {
     return StringUtils.unEscapeString(string, StringUtils.ESCAPE_CHAR, 
                                       charsToEscape);
   }
-  
-  synchronized boolean contentEquals(Counters counters) {
+
+  @Override 
+  public synchronized int hashCode() {
+    return counters.hashCode();
+  }
+
+  @Override
+  public synchronized boolean equals(Object obj) {
     boolean isEqual = false;
-    if (counters != null) {
-      if (size() == counters.size()) {
+    if (obj != null && obj instanceof Counters) {
+      Counters other = (Counters) obj;
+      if (size() == other.size()) {
         isEqual = true;
         for (Map.Entry<String, Group> entry : this.counters.entrySet()) {
           String key = entry.getKey();
           Group sourceGroup = entry.getValue();
-          Group targetGroup = counters.getGroup(key);
-          if (!sourceGroup.contentEquals(targetGroup)) {
+          Group targetGroup = other.getGroup(key);
+          if (!sourceGroup.equals(targetGroup)) {
             isEqual = false;
             break;
           }

+ 4 - 3
src/test/org/apache/hadoop/mapred/TestCounters.java

@@ -61,9 +61,10 @@ public class TestCounters extends TestCase {
     Counters recoveredCounter = 
       Counters.fromEscapedCompactString(compactEscapedString);
     // Check for recovery from string
-    assertTrue("Recovered counter does not match on content", 
-               counter.contentEquals(recoveredCounter));
-    
+    assertEquals("Recovered counter does not match on content", 
+                 counter, recoveredCounter);
+    assertEquals("recovered counter has wrong hash code",
+                 counter.hashCode(), recoveredCounter.hashCode());
   }
   
   public void testCounters() throws IOException {