This is an automated email from the ASF dual-hosted git repository.
zaynt4606 pushed a commit to branch branch-0.7
in repository https://gitbox.apache.org/repos/asf/celeborn.git
The following commit(s) were added to refs/heads/branch-0.7 by this push:
new 854bc98f2a [CELEBORN-2507] Fix hashCode/equals contract violation in
CelebornCRC32, CommitMetadata and protocol messages
854bc98f2a is described below
commit 854bc98f2a78f1a5a0b643f716c09254c1f7b4c6
Author: yew1eb <[email protected]>
AuthorDate: Thu Nov 5 01:22:47 2026 +0800
[CELEBORN-2507] Fix hashCode/equals contract violation in CelebornCRC32,
CommitMetadata and protocol messages
### What changes were proposed in this pull request?
Fix the `hashCode`/`equals` contract violations in `CelebornCRC32` and
`CommitMetadata`, and align the hash calculations of two protocol message
classes with their siblings:
- `CelebornCRC32`: hash the checksum value instead of the `AtomicInteger`
holder.
- `CommitMetadata`: hash `bytes.get()` instead of the `AtomicLong` holder.
- `ReadData` / `SubPartitionReadData`: use `Objects.hash(<fields>, body())`
like sibling protocol messages instead of incorporating `super.hashCode()`.
Add contract regression tests for the two value types and hash-calculation
tests for the two message classes.
### Why are the changes needed?
A repo-wide audit of the Java `equals`/`hashCode` implementations (659
files, 86 `equals(Object)` / 86 `hashCode()` pairs) showed that all of them
carry `Override` and none is missing its counterpart, so there is no
missing-annotation problem. Two of these classes, however, return different
hash codes for equal instances:
- `CelebornCRC32.equals` compares `current.get()` while `hashCode` returned
`Objects.hashCode(current)`; `AtomicInteger` does not override `hashCode()`, so
that is the identity hash of the holder object.
- `CommitMetadata.hashCode` returned `Objects.hash(bytes, crc)` with
`bytes` an `AtomicLong` (identity hash) and `crc` the already-broken
`CelebornCRC32`.
Measured before the fix:
```
CelebornCRC32 equals=true hashEqual=false (1030870354 vs 485815673)
CommitMetadata equals=true hashEqual=false (199780859 vs -276161396)
```
Using either value type as a `HashSet` element or `HashMap` key could cause
lookups by an equal-but-distinct instance to fail. Current production uses keep
`CommitMetadata` as map values and `CelebornCRC32` as a field, so this fixes a
latent bug. Checksum computation, wire encoding and persisted primitive values
are unchanged.
For `ReadData` and `SubPartitionReadData`, the change aligns hash
calculations with sibling protocol messages. `ReadData.equals(Object)` calls
`Object.equals(Object)` through `super.equals(o)`, rather than the overloaded
`Message.equals(Message)` helper, so distinct message instances remain unequal
via `equals(Object)`. Their equality semantics remain unchanged.
### Does this PR resolve a correctness bug?
- [ ] Yes
### Does this PR introduce _any_ user-facing change?
- [ ] Yes
### How was this patch tested?
New JUnit 4 tests in `CelebornCRC32Test` and `CommitMetadataTest` verify
that equal instances share a hash code, hashing uses the values rather than
holder identities, and an equal-but-distinct instance is found in a `HashSet`.
The new `ReadDataSuiteJ` checks hash composition for both message classes and
verifies that distinct `ReadData` instances remain unequal.
They fail before the fix and pass after (verified by stashing the
main-source changes):
```
[ERROR] Tests run: 28, Failures: 7 (before the fix)
[INFO] Tests run: 28, Failures: 0 (after the fix)
```
Commands:
```
build/mvn spotless:apply -pl common
build/mvn spotless:check -pl common
build/mvn -pl common test
-Dtest='CelebornCRC32Test,CommitMetadataTest,ReadDataSuiteJ'
-Dsurefire.failIfNoSpecifiedTests=false
```
Closes #3903 from yew1eb/celeborn-2507.
Authored-by: yew1eb <[email protected]>
Signed-off-by: zhengtao <[email protected]>
AI-Contributed/Feature: 0/9
AI-Contributed/UT: 0/148
(cherry picked from commit 3e841843b5d5fb8da7ea5535cc0d4f5490321afa)
Signed-off-by: zhengtao <[email protected]>
---
.../org/apache/celeborn/common/CelebornCRC32.java | 3 +-
.../org/apache/celeborn/common/CommitMetadata.java | 2 +-
.../celeborn/common/network/protocol/ReadData.java | 2 +-
.../network/protocol/SubPartitionReadData.java | 2 +-
.../apache/celeborn/common/CelebornCRC32Test.java | 43 ++++++++++++++++
.../apache/celeborn/common/CommitMetadataTest.java | 48 ++++++++++++++++++
.../common/network/protocol/ReadDataSuiteJ.java | 57 ++++++++++++++++++++++
7 files changed, 152 insertions(+), 5 deletions(-)
diff --git a/common/src/main/java/org/apache/celeborn/common/CelebornCRC32.java
b/common/src/main/java/org/apache/celeborn/common/CelebornCRC32.java
index 87997e3a01..f248fc327d 100644
--- a/common/src/main/java/org/apache/celeborn/common/CelebornCRC32.java
+++ b/common/src/main/java/org/apache/celeborn/common/CelebornCRC32.java
@@ -18,7 +18,6 @@
package org.apache.celeborn.common;
import java.nio.ByteBuffer;
-import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.CRC32;
@@ -108,6 +107,6 @@ public class CelebornCRC32 {
@Override
public int hashCode() {
- return Objects.hashCode(current);
+ return Integer.hashCode(current.get());
}
}
diff --git
a/common/src/main/java/org/apache/celeborn/common/CommitMetadata.java
b/common/src/main/java/org/apache/celeborn/common/CommitMetadata.java
index 814d944baa..f3701862f3 100644
--- a/common/src/main/java/org/apache/celeborn/common/CommitMetadata.java
+++ b/common/src/main/java/org/apache/celeborn/common/CommitMetadata.java
@@ -95,6 +95,6 @@ public class CommitMetadata {
@Override
public int hashCode() {
- return Objects.hash(bytes, crc);
+ return Objects.hash(bytes.get(), crc);
}
}
diff --git
a/common/src/main/java/org/apache/celeborn/common/network/protocol/ReadData.java
b/common/src/main/java/org/apache/celeborn/common/network/protocol/ReadData.java
index 1be7b46ff8..43f3a3d290 100644
---
a/common/src/main/java/org/apache/celeborn/common/network/protocol/ReadData.java
+++
b/common/src/main/java/org/apache/celeborn/common/network/protocol/ReadData.java
@@ -62,7 +62,7 @@ public class ReadData extends RequestMessage {
@Override
public int hashCode() {
- return Objects.hash(streamId, super.hashCode());
+ return Objects.hash(streamId, body());
}
@Override
diff --git
a/common/src/main/java/org/apache/celeborn/common/network/protocol/SubPartitionReadData.java
b/common/src/main/java/org/apache/celeborn/common/network/protocol/SubPartitionReadData.java
index 11a13118d1..ef0bf6c0c6 100644
---
a/common/src/main/java/org/apache/celeborn/common/network/protocol/SubPartitionReadData.java
+++
b/common/src/main/java/org/apache/celeborn/common/network/protocol/SubPartitionReadData.java
@@ -67,7 +67,7 @@ public class SubPartitionReadData extends ReadData {
@Override
public int hashCode() {
- return Objects.hash(streamId, subPartitionId, super.hashCode());
+ return Objects.hash(streamId, subPartitionId, body());
}
@Override
diff --git
a/common/src/test/java/org/apache/celeborn/common/CelebornCRC32Test.java
b/common/src/test/java/org/apache/celeborn/common/CelebornCRC32Test.java
index c5bcc2b0ae..e5c645ff29 100644
--- a/common/src/test/java/org/apache/celeborn/common/CelebornCRC32Test.java
+++ b/common/src/test/java/org/apache/celeborn/common/CelebornCRC32Test.java
@@ -19,8 +19,11 @@ package org.apache.celeborn.common;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Set;
import org.junit.Test;
@@ -112,4 +115,44 @@ public class CelebornCRC32Test {
CelebornCRC32 crc = new CelebornCRC32(123456789);
assertEquals("CelebornCRC32{current=123456789}", crc.toString());
}
+
+ @Test
+ public void testEqualsComparesChecksumValue() {
+ CelebornCRC32 withChecksum = new CelebornCRC32();
+ withChecksum.addChecksum(123456789);
+ CelebornCRC32 sameChecksum = new CelebornCRC32();
+ sameChecksum.addChecksum(123456789);
+
+ assertEquals(withChecksum, sameChecksum);
+ assertNotEquals(withChecksum, new CelebornCRC32());
+ }
+
+ @Test
+ public void testHashCodeIsSameForEqualInstances() {
+ CelebornCRC32 withChecksum = new CelebornCRC32();
+ withChecksum.addChecksum(123456789);
+ CelebornCRC32 sameChecksum = new CelebornCRC32();
+ sameChecksum.addChecksum(123456789);
+
+ assertEquals(withChecksum, sameChecksum);
+ assertEquals(withChecksum.hashCode(), sameChecksum.hashCode());
+ }
+
+ @Test
+ public void testHashCodeIsHashOfChecksumValue() {
+ CelebornCRC32 crc = new CelebornCRC32(123456789);
+ assertEquals(Integer.hashCode(123456789), crc.hashCode());
+ }
+
+ @Test
+ public void testHashCodeAndEqualsWorkInSet() {
+ CelebornCRC32 withChecksum = new CelebornCRC32();
+ withChecksum.addChecksum(123456789);
+ CelebornCRC32 sameChecksum = new CelebornCRC32();
+ sameChecksum.addChecksum(123456789);
+
+ Set<CelebornCRC32> checksums = new HashSet<>();
+ checksums.add(withChecksum);
+ assertTrue(checksums.contains(sameChecksum));
+ }
}
diff --git
a/common/src/test/java/org/apache/celeborn/common/CommitMetadataTest.java
b/common/src/test/java/org/apache/celeborn/common/CommitMetadataTest.java
index a228a796cc..c080842086 100644
--- a/common/src/test/java/org/apache/celeborn/common/CommitMetadataTest.java
+++ b/common/src/test/java/org/apache/celeborn/common/CommitMetadataTest.java
@@ -22,6 +22,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.nio.ByteBuffer;
+import java.util.HashSet;
+import java.util.Set;
import org.junit.Assert;
import org.junit.Test;
@@ -156,4 +158,50 @@ public class CommitMetadataTest {
Assert.assertFalse(CommitMetadata.checkCommitMetadata(expected,
actualNonMatchingChecksumOnly));
Assert.assertFalse(CommitMetadata.checkCommitMetadata(expected,
actualNonMatching));
}
+
+ @Test
+ public void testEqualsComparesBytesAndChecksum() {
+ byte[] data = "testdata".getBytes();
+ CommitMetadata first = new CommitMetadata();
+ first.addDataWithOffsetAndLength(data, 0, data.length);
+ CommitMetadata second = new CommitMetadata();
+ second.addDataWithOffsetAndLength(data, 0, data.length);
+
+ Assert.assertEquals(first, second);
+ Assert.assertNotEquals(first, new CommitMetadata());
+ }
+
+ @Test
+ public void testHashCodeIsSameForEqualInstances() {
+ byte[] data = "testdata".getBytes();
+ CommitMetadata first = new CommitMetadata();
+ first.addDataWithOffsetAndLength(data, 0, data.length);
+ CommitMetadata second = new CommitMetadata();
+ second.addDataWithOffsetAndLength(data, 0, data.length);
+
+ Assert.assertEquals(first, second);
+ Assert.assertEquals(first.hashCode(), second.hashCode());
+ }
+
+ @Test
+ public void testHashCodeDiffersWhenChecksumDiffers() {
+ CommitMetadata first = new
CommitMetadata(CelebornCRC32.compute("testdata".getBytes()), 8);
+ CommitMetadata second = new
CommitMetadata(CelebornCRC32.compute("foo".getBytes()), 8);
+
+ Assert.assertNotEquals(first, second);
+ Assert.assertNotEquals(first.hashCode(), second.hashCode());
+ }
+
+ @Test
+ public void testHashCodeAndEqualsWorkInSet() {
+ byte[] data = "testdata".getBytes();
+ CommitMetadata first = new CommitMetadata();
+ first.addDataWithOffsetAndLength(data, 0, data.length);
+ CommitMetadata second = new CommitMetadata();
+ second.addDataWithOffsetAndLength(data, 0, data.length);
+
+ Set<CommitMetadata> metadata = new HashSet<>();
+ metadata.add(first);
+ assertTrue(metadata.contains(second));
+ }
}
diff --git
a/common/src/test/java/org/apache/celeborn/common/network/protocol/ReadDataSuiteJ.java
b/common/src/test/java/org/apache/celeborn/common/network/protocol/ReadDataSuiteJ.java
new file mode 100644
index 0000000000..8c3d2f402f
--- /dev/null
+++
b/common/src/test/java/org/apache/celeborn/common/network/protocol/ReadDataSuiteJ.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.celeborn.common.network.protocol;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+
+import java.util.Objects;
+
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Test;
+
+public class ReadDataSuiteJ {
+
+ private static ByteBuf body() {
+ return Unpooled.wrappedBuffer("testdata".getBytes());
+ }
+
+ @Test
+ public void testReadDataHashCodeIsDerivedFromStreamIdAndBody() {
+ ReadData readData = new ReadData(1L, body());
+ assertEquals(Objects.hash(1L, readData.body()), readData.hashCode());
+ // stable across calls, i.e. it does not depend on the identity of the
message itself
+ assertEquals(readData.hashCode(), readData.hashCode());
+ }
+
+ @Test
+ public void
testSubPartitionReadDataHashCodeIsDerivedFromStreamIdSubPartitionIdAndBody() {
+ SubPartitionReadData readData = new SubPartitionReadData(1L, 2, body());
+ assertEquals(Objects.hash(1L, 2, readData.body()), readData.hashCode());
+ assertEquals(readData.hashCode(), readData.hashCode());
+ }
+
+ @Test
+ public void testReadDataWithDifferentBodyAreNotEqual() {
+ ReadData first = new ReadData(1L, body());
+ ReadData second = new ReadData(1L, body());
+ assertEquals(first, first);
+ assertNotEquals(first, second);
+ }
+}