Skip to content

Commit

Permalink
Move customized equality comparison to WorkerServiceEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunxuTang committed Jan 31, 2024
1 parent ff94a20 commit da942d7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public void join(WorkerInfo workerInfo) throws IOException {
"Existing WorkerServiceEntity for path:%s corrupted",
pathOnRing));
}
if (existingEntity.get().customizedEquals(entity)) {
if (existingEntity.get().equalsIgnoringOptionalFields(entity)) {
// Same entity but potentially with new optional fields,
// update the original etcd-stored worker information
mAlluxioEtcdClient.createForPath(pathOnRing, Optional.of(serializedEntity));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,14 @@ public void deserialize(byte[] buf) {
}

/**
* A customized equality comparison which uses the customized comparison of WorkerNetAddress
* objects.
* A customized equality comparison which ignores optional fields such as mHttpServerPort in the
* WorkerNetAddress.
*
* @param o The object to be compared with this WorkerServiceEntity for customized equality
* @return true if the specified object is equal to this WorkerServiceEntity; false otherwise
* @param o The object to be compared with this WorkerServiceEntity for equality
* @return true if the specified object is equal to this WorkerServiceEntity by ignoring optional
* fields; false otherwise
*/
public boolean customizedEquals(Object o) {
public boolean equalsIgnoringOptionalFields(Object o) {
if (this == o) {
return true;
}
Expand All @@ -144,6 +145,16 @@ public boolean customizedEquals(Object o) {
WorkerServiceEntity anotherO = (WorkerServiceEntity) o;
return mIdentity.equals(anotherO.mIdentity)
&& getServiceEntityName().equals(anotherO.getServiceEntityName())
&& mAddress.customizedEquals(anotherO.getWorkerNetAddress());
&& equalsIgnoringHttpServerPort(anotherO.getWorkerNetAddress());
}

private boolean equalsIgnoringHttpServerPort(WorkerNetAddress other) {
return mAddress.getHost().equals(other.getHost())
&& mAddress.getContainerHost().equals(other.getContainerHost())
&& mAddress.getSecureRpcPort() == other.getSecureRpcPort()
&& mAddress.getRpcPort() == other.getRpcPort()
&& mAddress.getDataPort() == other.getDataPort()
&& mAddress.getWebPort() == other.getWebPort()
&& mAddress.getDomainSocketPath().equals(other.getDomainSocketPath());
}
}
24 changes: 0 additions & 24 deletions dora/core/common/src/main/java/alluxio/wire/WorkerNetAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,30 +264,6 @@ public int hashCode() {
mDomainSocketPath, mHttpServerPort);
}

/**
* A customized equality comparison which skips the comparison of optional fields, including
* mHttpServerPort, for backward compatibility.
*
* @param o The object to be compared with this WorkerNetAddress for customized equality
* @return true if the specified object is equal to this WorkerNetAddress; false otherwise
*/
public boolean customizedEquals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof WorkerNetAddress)) {
return false;
}
WorkerNetAddress that = (WorkerNetAddress) o;
return mHost.equals(that.mHost)
&& mContainerHost.equals(that.mContainerHost)
&& mSecureRpcPort == that.mSecureRpcPort
&& mRpcPort == that.mRpcPort
&& mDataPort == that.mDataPort
&& mWebPort == that.mWebPort
&& mDomainSocketPath.equals(that.mDomainSocketPath);
}

/**
* dump the main info of the WorkerNetAddress object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,32 @@ public void testSerializationWorkerServiceEntity() throws Exception {
deserialized.deserialize(jsonBytes);
Assert.assertEquals(deserialized, entity);
}

@Test
public void testEqualsIgnoringOptionalFields() throws Exception {
final WorkerNetAddress workerNetAddress1 = new WorkerNetAddress()
.setHost("worker1").setContainerHost("containerhostname1")
.setRpcPort(1000).setDataPort(1001).setWebPort(1011)
.setDomainSocketPath("/var/lib/domain.sock").setHttpServerPort(1021);
final WorkerNetAddress workerNetAddress2 = new WorkerNetAddress()
.setHost("worker1").setContainerHost("containerhostname1")
.setRpcPort(1000).setDataPort(1001).setWebPort(1011)
.setDomainSocketPath("/var/lib/domain.sock").setHttpServerPort(2021);
final WorkerNetAddress workerNetAddress3 = new WorkerNetAddress()
.setHost("worker3").setContainerHost("containerhostname1")
.setRpcPort(1000).setDataPort(1001).setWebPort(1011)
.setDomainSocketPath("/var/lib/domain.sock").setHttpServerPort(1021);
final WorkerIdentity identity = WorkerIdentity.fromProto(
alluxio.grpc.WorkerIdentity.newBuilder()
.setIdentifier(ByteString.copyFrom(Longs.toByteArray(1L)))
.setVersion(0)
.build());
WorkerServiceEntity entity1 = new WorkerServiceEntity(identity, workerNetAddress1);
WorkerServiceEntity entity2 = new WorkerServiceEntity(identity, workerNetAddress2);
WorkerServiceEntity entity3 = new WorkerServiceEntity(identity, workerNetAddress3);

Assert.assertTrue(entity1.equalsIgnoringOptionalFields(entity2));
Assert.assertFalse(entity2.equalsIgnoringOptionalFields(entity3));
Assert.assertFalse(entity3.equalsIgnoringOptionalFields(entity1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,43 +72,6 @@ public void copyConstructor() throws IllegalAccessException {
}
}

@Test
public void testCustomizedEquals() {
WorkerNetAddress workerNetAddress1 = new WorkerNetAddress()
.setHost("host")
.setContainerHost("container")
.setRpcPort(1)
.setDataPort(1)
.setNettyDataPort(1)
.setSecureRpcPort(1)
.setWebPort(1)
.setDomainSocketPath("path")
.setHttpServerPort(1);
WorkerNetAddress workerNetAddress2 = new WorkerNetAddress()
.setHost("host")
.setContainerHost("container")
.setRpcPort(1)
.setDataPort(1)
.setNettyDataPort(1)
.setSecureRpcPort(1)
.setWebPort(1)
.setDomainSocketPath("path")
.setHttpServerPort(2);
WorkerNetAddress workerNetAddress3 = new WorkerNetAddress()
.setHost("host2")
.setContainerHost("container")
.setRpcPort(1)
.setDataPort(1)
.setNettyDataPort(1)
.setSecureRpcPort(1)
.setWebPort(1)
.setDomainSocketPath("path")
.setHttpServerPort(1);
Assert.assertTrue(workerNetAddress1.customizedEquals(workerNetAddress2));
Assert.assertFalse(workerNetAddress1.customizedEquals(workerNetAddress3));
Assert.assertFalse(workerNetAddress2.customizedEquals(workerNetAddress3));
}

public void checkEquality(WorkerNetAddress a, WorkerNetAddress b) {
Assert.assertEquals(a.getHost(), b.getHost());
Assert.assertEquals(a.getRpcPort(), b.getRpcPort());
Expand Down

0 comments on commit da942d7

Please sign in to comment.