You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-issues@hadoop.apache.org by GitBox <gi...@apache.org> on 2022/11/02 07:18:04 UTC

[GitHub] [hadoop] slfan1989 commented on a diff in pull request #5096: YARN-11366. Improve equals, hashCode(), toString() methods of the Federation Base Object.

slfan1989 commented on code in PR #5096:
URL: https://github.com/apache/hadoop/pull/5096#discussion_r1011297471


##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/main/java/org/apache/hadoop/yarn/server/federation/store/records/SubClusterPolicyConfiguration.java:
##########
@@ -127,36 +129,36 @@ public static SubClusterPolicyConfiguration newInstance(
 
   @Override
   public int hashCode() {
-    return 31 * getParams().hashCode() + getType().hashCode();
+    return new HashCodeBuilder().
+        append(this.getType()).
+        append(this.getQueue()).
+        append(this.getParams()).
+        toHashCode();
   }
 
   @Override
   public boolean equals(Object obj) {
+
     if (this == obj) {
       return true;
     }
-    if (obj == null) {
-      return false;
-    }
-    if (getClass() != obj.getClass()) {
-      return false;
-    }
-    SubClusterPolicyConfiguration other = (SubClusterPolicyConfiguration) obj;
-    if (!this.getType().equals(other.getType())) {
-      return false;
-    }
-    if (!this.getParams().equals(other.getParams())) {
+
+    if (obj == null || getClass() != obj.getClass()) {
       return false;
     }
-    return true;
+
+    SubClusterPolicyConfiguration right = (SubClusterPolicyConfiguration) obj;
+
+    return new EqualsBuilder()
+        .append(this.getType(), right.getType())
+        .append(this.getQueue(), right.getQueue())
+        .append(this.getParams(), right.getParams())
+        .isEquals();
   }
 
   @Override
   public String toString() {
-    StringBuilder sb = new StringBuilder();
-    sb.append(getType())
-        .append(" : ")
-        .append(getParams());
-    return sb.toString();
+    return "SubClusterPolicyConfiguration [getType() = " + getType() + ","
+        + "getQueue() = " + getQueue() + ",getParams() = " +  getParams() + "]";
   }
 }

Review Comment:
   Thank you very much for your help reviewing the code, I will fix it.



##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-common/src/test/java/org/apache/hadoop/yarn/server/federation/store/records/TestFederationProtocolRecords.java:
##########
@@ -326,4 +333,71 @@ public void testGetReservationHomeSubClusterRequest() throws Exception {
     validatePBImplRecord(GetReservationHomeSubClusterRequestPBImpl.class,
         GetReservationHomeSubClusterRequestProto.class);
   }
+
+  @Test
+  public void testValidateApplicationHomeSubClusterEqual() throws Exception {
+    long now = Time.now();
+
+    ApplicationId appId1 = ApplicationId.newInstance(now, 1);
+    SubClusterId subClusterId1 = SubClusterId.newInstance("SC-1");
+    ApplicationHomeSubCluster applicationHomeSubCluster1 =
+        ApplicationHomeSubCluster.newInstance(appId1, subClusterId1);
+
+    ApplicationId appId2 = ApplicationId.newInstance(now, 1);
+    SubClusterId subClusterId2 = SubClusterId.newInstance("SC-1");
+    ApplicationHomeSubCluster applicationHomeSubCluster2 =
+        ApplicationHomeSubCluster.newInstance(appId2, subClusterId2);
+
+    assertEquals(applicationHomeSubCluster1, applicationHomeSubCluster2);
+  }
+
+  @Test
+  public void testValidateReservationHomeSubClusterEqual() throws Exception {
+    long now = Time.now();
+
+    ReservationId reservationId1 = ReservationId.newInstance(now, 1);
+    SubClusterId subClusterId1 = SubClusterId.newInstance("SC-1");
+    ReservationHomeSubCluster reservationHomeSubCluster1 =
+        ReservationHomeSubCluster.newInstance(reservationId1, subClusterId1);
+
+    ReservationId reservationId2 = ReservationId.newInstance(now, 1);
+    SubClusterId subClusterId2 = SubClusterId.newInstance("SC-1");
+    ReservationHomeSubCluster reservationHomeSubCluster2 =
+        ReservationHomeSubCluster.newInstance(reservationId2, subClusterId2);
+
+    assertEquals(reservationHomeSubCluster1, reservationHomeSubCluster2);
+  }
+
+  @Test
+  public void testSubClusterIdEqual() throws Exception {
+    SubClusterId subClusterId1 = SubClusterId.newInstance("SC-1");
+    SubClusterId subClusterId2 = SubClusterId.newInstance("SC-1");
+    assertEquals(subClusterId1, subClusterId2);
+  }
+
+  @Test
+  public void testSubClusterIdInfoEqual() throws Exception {
+    SubClusterIdInfo subClusterIdInfo1 = new SubClusterIdInfo("SC-1");
+    SubClusterIdInfo subClusterIdInfo2 = new SubClusterIdInfo("SC-1");
+    assertEquals(subClusterIdInfo1, subClusterIdInfo2);
+  }
+
+  @Test
+  public void testSubClusterPolicyConfiguration() throws Exception {
+
+    String queue1 = "queue1";
+    WeightedPolicyInfo policyInfo1 = mock(WeightedPolicyInfo.class);
+    ByteBuffer buf1 = policyInfo1.toByteBuffer();
+    SubClusterPolicyConfiguration configuration1 = SubClusterPolicyConfiguration
+        .newInstance(queue1, policyInfo1.getClass().getCanonicalName(), buf1);
+
+    String queue2 = "queue1";
+    WeightedPolicyInfo policyInfo2 = mock(WeightedPolicyInfo.class);
+    ByteBuffer buf2 = policyInfo1.toByteBuffer();
+    SubClusterPolicyConfiguration configuration2 = SubClusterPolicyConfiguration
+        .newInstance(queue2, policyInfo2.getClass().getCanonicalName(), buf2);
+
+    assertEquals(configuration1, configuration2);
+  }
+
 }

Review Comment:
   Thank you very much for your help reviewing the code, I will fix it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: common-issues-unsubscribe@hadoop.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-issues-help@hadoop.apache.org