YARN-11366. Improve equals, hashCode(), toString() methods of the Federation Base Object. (#5096)

This commit is contained in:
slfan1989 2022-11-04 12:33:53 +08:00 committed by GitHub
parent e62ba16a02
commit b90dfdff3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 263 additions and 107 deletions

View File

@ -17,6 +17,8 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
@ -123,32 +125,42 @@ public static ApplicationHomeSubCluster newInstance(ApplicationId appId, long cr
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) {
return false; if (obj instanceof ApplicationHomeSubCluster) {
}
ApplicationHomeSubCluster other = (ApplicationHomeSubCluster) obj; ApplicationHomeSubCluster other = (ApplicationHomeSubCluster) obj;
if (!this.getApplicationId().equals(other.getApplicationId())) { return new EqualsBuilder()
return false; .append(this.getApplicationId(), other.getApplicationId())
.append(this.getHomeSubCluster(), other.getHomeSubCluster())
.isEquals();
} }
return this.getHomeSubCluster().equals(other.getHomeSubCluster());
return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return getApplicationId().hashCode() * 31 + getHomeSubCluster().hashCode(); return new HashCodeBuilder().
append(this.getApplicationId()).
append(this.getHomeSubCluster()).
append(this.getCreateTime()).toHashCode();
} }
@Override @Override
public String toString() { public String toString() {
return "ApplicationHomeSubCluster [getApplicationId()=" StringBuilder sb = new StringBuilder();
+ getApplicationId() + ", getHomeSubCluster()=" + getHomeSubCluster() sb.append("ApplicationHomeSubCluster: [")
+ "]"; .append("ApplicationId: ").append(getApplicationId()).append(", ")
.append("HomeSubCluster: ").append(getHomeSubCluster()).append(", ")
.append("CreateTime: ").append(getCreateTime()).append(", ")
.append("]");
return sb.toString();
} }
} }

View File

@ -94,23 +94,26 @@ public static ReservationHomeSubCluster newInstance(ReservationId resId,
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) {
return false;
}
ReservationHomeSubCluster other = (ReservationHomeSubCluster) obj;
if (obj instanceof ReservationHomeSubCluster) {
ReservationHomeSubCluster other = (ReservationHomeSubCluster) obj;
return new EqualsBuilder() return new EqualsBuilder()
.append(this.getReservationId(), other.getReservationId()) .append(this.getReservationId(), other.getReservationId())
.append(this.getHomeSubCluster(), other.getHomeSubCluster()) .append(this.getHomeSubCluster(), other.getHomeSubCluster())
.isEquals(); .isEquals();
} }
return false;
}
@Override @Override
public int hashCode() { public int hashCode() {
return new HashCodeBuilder(). return new HashCodeBuilder().
@ -121,9 +124,11 @@ public int hashCode() {
@Override @Override
public String toString() { public String toString() {
return "ReservationHomeSubCluster [getReservationId()=" StringBuilder sb = new StringBuilder();
+ getReservationId() + ", getApplicationHomeSubcluster()=" + getHomeSubCluster() sb.append("ReservationHomeSubCluster: [")
+ "]"; .append("ReservationId: ").append(getReservationId()).append(", ")
.append("HomeSubCluster: ").append(getHomeSubCluster())
.append("]");
return sb.toString();
} }
} }

View File

@ -114,20 +114,36 @@ public int hashCode() {
} }
@Override @Override
public boolean equals(Object right) { public boolean equals(Object obj) {
if (this == right) {
if (this == obj) {
return true; return true;
} }
if (right == null || getClass() != right.getClass()) { if (obj == null) {
return false; return false;
} }
RouterMasterKey r = (RouterMasterKey) right; if (obj instanceof RouterMasterKey) {
RouterMasterKey other = (RouterMasterKey) obj;
return new EqualsBuilder() return new EqualsBuilder()
.append(this.getKeyId().intValue(), r.getKeyId().intValue()) .append(this.getKeyId().intValue(), other.getKeyId().intValue())
.append(this.getExpiryDate().longValue(), this.getExpiryDate().longValue()) .append(this.getExpiryDate().longValue(), other.getExpiryDate().longValue())
.append(getKeyBytes().array(), r.getKeyBytes()) .append(this.getKeyBytes().array(), other.getKeyBytes())
.isEquals(); .isEquals();
} }
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("RouterMasterKey: [")
.append("KeyId: ").append(getKeyId()).append(", ")
.append("ExpiryDate: ").append(getExpiryDate()).append(", ")
.append("KeyBytes: ").append(getKeyBytes()).append(", ")
.append("]");
return sb.toString();
}
} }

View File

@ -17,6 +17,8 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
@ -78,19 +80,26 @@ public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) {
return false; if (obj instanceof SubClusterId) {
}
SubClusterId other = (SubClusterId) obj; SubClusterId other = (SubClusterId) obj;
return this.getId().equals(other.getId()); return new EqualsBuilder()
.append(this.getId(), other.getId())
.isEquals();
}
return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return getId().hashCode(); return new HashCodeBuilder().
append(this.getId()).
toHashCode();
} }
@Override @Override
@ -104,5 +113,4 @@ public String toString() {
sb.append(getId()); sb.append(getId());
return sb.toString(); return sb.toString();
} }
} }

View File

@ -18,6 +18,8 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
@ -58,18 +60,28 @@ public SubClusterId toId() {
} }
@Override @Override
public boolean equals(Object other) { public boolean equals(Object obj) {
if (other instanceof SubClusterIdInfo) {
if (((SubClusterIdInfo) other).id.equals(this.id)) { if (this == obj) {
return true; return true;
} }
if (obj == null) {
return false;
} }
if (obj instanceof SubClusterIdInfo) {
SubClusterIdInfo other = (SubClusterIdInfo) obj;
return new EqualsBuilder()
.append(this.id, other.id)
.isEquals();
}
return false; return false;
} }
@Override @Override
public int hashCode() { public int hashCode() {
return id.hashCode(); return new HashCodeBuilder().append(this.id).toHashCode();
} }
} }

View File

@ -17,6 +17,8 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
@ -43,6 +45,7 @@ public abstract class SubClusterInfo {
@Private @Private
@Unstable @Unstable
@SuppressWarnings("checkstyle:ParameterNumber")
public static SubClusterInfo newInstance(SubClusterId subClusterId, public static SubClusterInfo newInstance(SubClusterId subClusterId,
String amRMServiceAddress, String clientRMServiceAddress, String amRMServiceAddress, String clientRMServiceAddress,
String rmAdminServiceAddress, String rmWebServiceAddress, String rmAdminServiceAddress, String rmWebServiceAddress,
@ -54,6 +57,7 @@ public static SubClusterInfo newInstance(SubClusterId subClusterId,
@Private @Private
@Unstable @Unstable
@SuppressWarnings("checkstyle:ParameterNumber")
public static SubClusterInfo newInstance(SubClusterId subClusterId, public static SubClusterInfo newInstance(SubClusterId subClusterId,
String amRMServiceAddress, String clientRMServiceAddress, String amRMServiceAddress, String clientRMServiceAddress,
String rmAdminServiceAddress, String rmWebServiceAddress, String rmAdminServiceAddress, String rmWebServiceAddress,
@ -252,48 +256,49 @@ public static SubClusterInfo newInstance(SubClusterId subClusterId,
@Override @Override
public String toString() { public String toString() {
return "SubClusterInfo [getSubClusterId() = " + getSubClusterId() StringBuilder sb = new StringBuilder();
+ ", getAMRMServiceAddress() = " + getAMRMServiceAddress() sb.append("SubClusterInfo: [")
+ ", getClientRMServiceAddress() = " + getClientRMServiceAddress() .append("SubClusterId: ").append(getSubClusterId()).append(", ")
+ ", getRMAdminServiceAddress() = " + getRMAdminServiceAddress() .append("AMRMServiceAddress: ").append(getAMRMServiceAddress()).append(", ")
+ ", getRMWebServiceAddress() = " + getRMWebServiceAddress() .append("ClientRMServiceAddress: ").append(getClientRMServiceAddress()).append(", ")
+ ", getState() = " + getState() + ", getLastStartTime() = " .append("RMAdminServiceAddress: ").append(getRMAdminServiceAddress()).append(", ")
+ getLastStartTime() + ", getCapability() = " + getCapability() + "]"; .append("RMWebServiceAddress: ").append(getRMWebServiceAddress()).append(", ")
.append("State: ").append(getState()).append(", ")
.append("LastStartTime: ").append(getLastStartTime()).append(", ")
.append("Capability: ").append(getCapability())
.append("]");
return sb.toString();
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) { if (getClass() != obj.getClass()) {
return false; return false;
} }
if (obj instanceof SubClusterInfo) {
SubClusterInfo other = (SubClusterInfo) obj; SubClusterInfo other = (SubClusterInfo) obj;
if (!this.getSubClusterId().equals(other.getSubClusterId())) { return new EqualsBuilder()
return false; .append(this.getSubClusterId(), other.getSubClusterId())
.append(this.getAMRMServiceAddress(), other.getAMRMServiceAddress())
.append(this.getClientRMServiceAddress(), other.getClientRMServiceAddress())
.append(this.getRMAdminServiceAddress(), other.getRMAdminServiceAddress())
.append(this.getRMWebServiceAddress(), other.getRMWebServiceAddress())
.append(this.getState(), other.getState())
.append(this.getLastStartTime(), other.getLastStartTime())
.isEquals();
} }
if (!this.getAMRMServiceAddress().equals(other.getAMRMServiceAddress())) {
return false; return false;
}
if (!this.getClientRMServiceAddress()
.equals(other.getClientRMServiceAddress())) {
return false;
}
if (!this.getRMAdminServiceAddress()
.equals(other.getRMAdminServiceAddress())) {
return false;
}
if (!this.getRMWebServiceAddress().equals(other.getRMWebServiceAddress())) {
return false;
}
if (!this.getState().equals(other.getState())) {
return false;
}
return this.getLastStartTime() == other.getLastStartTime();
// Capability and HeartBeat fields are not included as they are temporal // Capability and HeartBeat fields are not included as they are temporal
// (i.e. timestamps), so they change during the lifetime of the same // (i.e. timestamps), so they change during the lifetime of the same
// sub-cluster // sub-cluster
@ -301,23 +306,16 @@ public boolean equals(Object obj) {
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31;
int result = 1; return new HashCodeBuilder()
result = prime * result .append(this.getSubClusterId())
+ ((getSubClusterId() == null) ? 0 : getSubClusterId().hashCode()); .append(this.getAMRMServiceAddress())
result = prime * result + ((getAMRMServiceAddress() == null) ? 0 .append(this.getClientRMServiceAddress())
: getAMRMServiceAddress().hashCode()); .append(this.getRMAdminServiceAddress())
result = prime * result + ((getClientRMServiceAddress() == null) ? 0 .append(this.getRMWebServiceAddress())
: getClientRMServiceAddress().hashCode()); .append(this.getState())
result = prime * result + ((getRMAdminServiceAddress() == null) ? 0 .append(this.getLastStartTime())
: getRMAdminServiceAddress().hashCode()); .toHashCode();
result = prime * result + ((getRMWebServiceAddress() == null) ? 0
: getRMWebServiceAddress().hashCode());
result =
prime * result + ((getState() == null) ? 0 : getState().hashCode());
result = prime * result
+ (int) (getLastStartTime() ^ (getLastStartTime() >>> 32));
return result;
// Capability and HeartBeat fields are not included as they are temporal // Capability and HeartBeat fields are not included as they are temporal
// (i.e. timestamps), so they change during the lifetime of the same // (i.e. timestamps), so they change during the lifetime of the same
// sub-cluster // sub-cluster

View File

@ -18,6 +18,8 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.hadoop.classification.InterfaceAudience.Private; import org.apache.hadoop.classification.InterfaceAudience.Private;
import org.apache.hadoop.classification.InterfaceAudience.Public; import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Unstable; import org.apache.hadoop.classification.InterfaceStability.Unstable;
@ -127,36 +129,44 @@ public static SubClusterPolicyConfiguration newInstance(
@Override @Override
public int hashCode() { public int hashCode() {
return 31 * getParams().hashCode() + getType().hashCode(); return new HashCodeBuilder()
.append(this.getType())
.append(this.getQueue())
.append(this.getParams()).
toHashCode();
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) { if (this == obj) {
return true; return true;
} }
if (obj == null) { if (obj == null) {
return false; return false;
} }
if (getClass() != obj.getClass()) {
return false; if (obj instanceof SubClusterPolicyConfiguration) {
}
SubClusterPolicyConfiguration other = (SubClusterPolicyConfiguration) obj; SubClusterPolicyConfiguration other = (SubClusterPolicyConfiguration) obj;
if (!this.getType().equals(other.getType())) { return new EqualsBuilder()
return false; .append(this.getType(), other.getType())
.append(this.getQueue(), other.getQueue())
.append(this.getParams(), other.getParams())
.isEquals();
} }
if (!this.getParams().equals(other.getParams())) {
return false; return false;
} }
return true;
}
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(getType()) sb.append("SubClusterPolicyConfiguration: [")
.append(" : ") .append("Type: ").append(getType()).append(", ")
.append(getParams()); .append("Queue: ").append(getQueue()).append(", ")
.append("Params: ").append(getParams()).append(", ")
.append("]");
return sb.toString(); return sb.toString();
} }
} }

View File

@ -17,6 +17,7 @@
package org.apache.hadoop.yarn.server.federation.store.records; package org.apache.hadoop.yarn.server.federation.store.records;
import org.apache.hadoop.util.Time;
import org.apache.hadoop.yarn.api.BasePBImplRecordsTest; import org.apache.hadoop.yarn.api.BasePBImplRecordsTest;
import org.apache.hadoop.yarn.api.records.ApplicationId; import org.apache.hadoop.yarn.api.records.ApplicationId;
import org.apache.hadoop.yarn.api.records.ReservationId; import org.apache.hadoop.yarn.api.records.ReservationId;
@ -56,6 +57,7 @@
import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.RouterMasterKeyRequestProto; import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.RouterMasterKeyRequestProto;
import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.RouterMasterKeyResponseProto; import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.RouterMasterKeyResponseProto;
import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.ApplicationHomeSubClusterProto; import org.apache.hadoop.yarn.federation.proto.YarnServerFederationProtos.ApplicationHomeSubClusterProto;
import org.apache.hadoop.yarn.server.federation.policies.dao.WeightedPolicyInfo;
import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.AddApplicationHomeSubClusterRequestPBImpl; import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.AddApplicationHomeSubClusterRequestPBImpl;
import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.AddApplicationHomeSubClusterResponsePBImpl; import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.AddApplicationHomeSubClusterResponsePBImpl;
import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.DeleteApplicationHomeSubClusterRequestPBImpl; import org.apache.hadoop.yarn.server.federation.store.records.impl.pb.DeleteApplicationHomeSubClusterRequestPBImpl;
@ -97,6 +99,11 @@
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import java.nio.ByteBuffer;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
/** /**
* Test class for federation protocol records. * Test class for federation protocol records.
*/ */
@ -326,4 +333,92 @@ public void testGetReservationHomeSubClusterRequest() throws Exception {
validatePBImplRecord(GetReservationHomeSubClusterRequestPBImpl.class, validatePBImplRecord(GetReservationHomeSubClusterRequestPBImpl.class,
GetReservationHomeSubClusterRequestProto.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 testSubClusterPolicyConfigurationEqual() 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);
}
@Test
public void testSubClusterInfoEqual() throws Exception {
String scAmRMAddress = "5.6.7.8:5";
String scClientRMAddress = "5.6.7.8:6";
String scRmAdminAddress = "5.6.7.8:7";
String scWebAppAddress = "127.0.0.1:8080";
String capabilityJson = "-";
SubClusterInfo sc1 =
SubClusterInfo.newInstance(SubClusterId.newInstance("SC-1"),
scAmRMAddress, scClientRMAddress, scRmAdminAddress, scWebAppAddress,
SubClusterState.SC_RUNNING, Time.now(), capabilityJson);
SubClusterInfo sc2 =
SubClusterInfo.newInstance(SubClusterId.newInstance("SC-1"),
scAmRMAddress, scClientRMAddress, scRmAdminAddress, scWebAppAddress,
SubClusterState.SC_RUNNING, Time.now(), capabilityJson);
assertEquals(sc1, sc2);
}
} }