Skip to content

Commit

Permalink
Use customized equality to compare etcd worker identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunxuTang committed Jan 31, 2024
1 parent 07c139e commit ff94a20
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ public void join(WorkerInfo workerInfo) throws IOException {
"Existing WorkerServiceEntity for path:%s corrupted",
pathOnRing));
}
if (existingEntity.get().equals(entity)) {
// Same entity, update the original etcd-stored worker information
if (existingEntity.get().customizedEquals(entity)) {
// Same entity but potentially with new optional fields,
// update the original etcd-stored worker information
mAlluxioEtcdClient.createForPath(pathOnRing, Optional.of(serializedEntity));
} else {
throw new AlreadyExistsException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ public boolean equals(Object o) {
// only the service name and the identity are the factors that define
// what a WorkerServiceEntity is. Any other is either ephemeral or supplementary data.
return mIdentity.equals(anotherO.mIdentity)
&& getServiceEntityName().equals(anotherO.getServiceEntityName())
&& mAddress.equals(anotherO.getWorkerNetAddress());
&& getServiceEntityName().equals(anotherO.getServiceEntityName());
}

@Override
Expand All @@ -127,4 +126,24 @@ public void deserialize(byte[] buf) {
.create();
gson.fromJson(new InputStreamReader(new ByteArrayInputStream(buf)), WorkerServiceEntity.class);
}

/**
* A customized equality comparison which uses the customized comparison of WorkerNetAddress
* objects.
*
* @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
*/
public boolean customizedEquals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
WorkerServiceEntity anotherO = (WorkerServiceEntity) o;
return mIdentity.equals(anotherO.mIdentity)
&& getServiceEntityName().equals(anotherO.getServiceEntityName())
&& mAddress.customizedEquals(anotherO.getWorkerNetAddress());
}
}
33 changes: 28 additions & 5 deletions dora/core/common/src/main/java/alluxio/wire/WorkerNetAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class WorkerNetAddress implements Serializable {
private String mDomainSocketPath = "";
@Expose
@com.google.gson.annotations.SerializedName("HttpServerPort")
// Optional field - skipped in the equality comparison
// Optional field - skipped in the customized equality comparison
private int mHttpServerPort;

/**
Expand Down Expand Up @@ -254,15 +254,38 @@ public boolean equals(Object o) {
&& mRpcPort == that.mRpcPort
&& mDataPort == that.mDataPort
&& mWebPort == that.mWebPort
&& mDomainSocketPath.equals(that.mDomainSocketPath);
// Skip the comparison of mHttpServerPort for backward compatibility
&& mDomainSocketPath.equals(that.mDomainSocketPath)
&& mHttpServerPort == that.mHttpServerPort;
}

@Override
public int hashCode() {
// Skip the mHttpServerPort for backward compatibility
return Objects.hashCode(mSecureRpcPort, mHost, mContainerHost, mDataPort, mRpcPort, mWebPort,
mDomainSocketPath);
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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,43 @@ 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 ff94a20

Please sign in to comment.