[common] DefaultValueRecordBatch and DefaultKvRecordBatch override equals() without hashCode()
Search before asking
- [x] I searched in the issues and found nothing similar.
Fluss version
main (development)
Please describe the bug 🐞
Summary
DefaultValueRecordBatch and DefaultKvRecordBatch both override equals() with value
semantics but do not override hashCode(). Both classes extend Object (they only
implements their respective interfaces) and neither equals() delegates to
super.equals(), so hashCode() is the identity hash from Object.
Two batches with identical bytes are therefore equals() but return different hash codes,
breaking the general contract of Object.hashCode():
"If two objects are equal according to the
equals(Object)method, then calling thehashCodemethod on each of the two objects must produce the same integer result." —java.lang.Object.hashCode(), Java SE 11 API
DefaultValueRecordBatch — value-based equals() at
DefaultValueRecordBatch.java:122,
no hashCode() anywhere in the class:
@Override
public boolean equals(Object o) {
...
DefaultValueRecordBatch that = (DefaultValueRecordBatch) o;
int sizeInBytes = sizeInBytes();
return sizeInBytes == that.sizeInBytes()
&& segment.equalTo(that.segment, position, that.position, sizeInBytes);
}
DefaultKvRecordBatch — the same equals() shape at
DefaultKvRecordBatch.java:209,
also with no hashCode().
Why this looks like an oversight rather than a deliberate choice
The third batch implementation in the same package, DefaultLogRecordBatch, has the
identical equals() shape and overrides hashCode() consistently with it
(DefaultLogRecordBatch.java:316):
@Override
public int hashCode() {
return MurmurHashUtils.hashBytes(segment, position, sizeInBytes());
}
All three classes are memory-segment-backed record batches in
org.apache.fluss.record, all three compare the same byte range in equals(), and only
one of the three hashes it. The Checkstyle EqualsHashCode module is not enabled in
tools/maven/checkstyle.xml, which is why CI does not flag this today.
Impact
Any HashSet/HashMap keyed on these batches would treat byte-identical batches as
distinct keys, so contains()/get() would miss. Nothing does that today: the only
collections involved are Map<TableBucket, KvRecordBatch> (e.g.
ServerRpcMessageUtils#getPutKvData, ReplicaManager#putRecordsToKv), where the batch is
the value type and its hashCode() is never consulted. So this is a latent correctness
trap for future callers rather than an active failure — but changing it now is free,
whereas finding it later means debugging a silent hash miss.
There is also a smaller, immediate diagnostic cost. Neither class overrides toString(),
so Object.toString() renders them as getClass().getName() + "@" + Integer.toHexString(hashCode()). With an identity hash, the hex suffix carries no
information about content: two equal batches print different suffixes. This is visible in
the assertion failure in #3877, where the message is
expected: org.apache.fluss.record.DefaultValueRecordBatch@60eebc2e
but was: org.apache.fluss.record.DefaultValueRecordBatch@1408822
Those two hex values do not tell a reader whether the contents actually differ. Once
hashCode() is content-derived, differing suffixes do correspond to differing bytes.
To be explicit: this is not a fix for the flakiness in #3877 — the reason the two batches genuinely differ there is a separate question, and this change does not address it.
Solution
Override hashCode() in both classes, mirroring DefaultLogRecordBatch exactly so the
three sibling implementations stay consistent:
@Override
public int hashCode() {
return MurmurHashUtils.hashBytes(segment, position, sizeInBytes());
}
equals() compares the byte range [position, position + sizeInBytes()), and
MurmurHashUtils.hashBytes(segment, position, sizeInBytes()) hashes exactly that range, so
the two are consistent by construction.
For scope: I swept src/main/java across the repo for classes that override
equals(Object) without hashCode(). Besides these two there are five more where the
superclass is Object and so the contract is genuinely violated —
TieringSourceEnumeratorState, RemoteLogManifestHandle, and the three nested
IsrState implementations. A further group (WriteResultForBucket and subclasses, the
ModifyColumn* classes) inherits a hashCode() from a base class that hashes a subset of
the fields compared by equals(); that is legal, only a weaker hash, so I have left it
alone. I would rather keep this issue to the two record-batch classes so the PR stays to a
single module and a single concern, and follow up on the other five separately.
Are you willing to submit a PR?
- [x] I'm willing to submit a PR!