-
Notifications
You must be signed in to change notification settings - Fork 202
http-api: edit MultiMap.getKeys() to return a LinkedHashSet to prevent non-deterministic behaviors in MultiMap.hashCode() #3358
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hi @lawrencewang49, Interesting, I haven't seen |
|
@idelpivnitskiy Yes!
Running that command resulted in the following output for a few runs: and similar output for another run: The reason I believe that it's non-deterministic is because the hashCode() inconsistency arises from Equality checks remain valid since equals() doesn’t depend on key iteration order, only the hash codes vary. If |
|
Heh, that's a great catch! Thanks a lot for detailed explanation. |
Motivation The test headersWithSameNamesAndValuesShouldBeEquivalent included assertions comparing hashCode() values, resulting in failing assertions because the hashCode() implementation for AbstractHttpHeaders is non-deterministic, even for the same object. Modifications Removes all hashCode() assertions Result No failing flaky tests when running testing with nondex
…hset when getting keys to remove non-deterministic behaviors
fb5c1e1 to
6162a9d
Compare
Done! |
|
hi @idelpivnitskiy, I thought I left a comment already, but I guess I didn't. I just wanted to let you know that I resolved the import statement issues for the style checks. |
| } | ||
| // Overall iteration order does not need to be preserved. | ||
| final Set<K> names = new HashSet<>((int) (size() / .75), .75f); | ||
| final Set<K> names = new LinkedHashSet<>((int) (size() / .75), .75f); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know that this actually fixes our issue since LinkedHashSet provides insertion ordering, not total ordering. Being a hash-map like data structure, the size of the hash array can change the ordering of buckets and thus iteration order.
We could go two ways: either use something like a TreeSet, which will give us both a Set and the correct iteration order, or we could modify the hashCode() method. We could do the latter by changing the general algorithm from result = 31 * result + hash(newContributor) to simply result += hash(newContributor). This will fix our iteration order problem (since the sum will be the sum regardless of iteration order) but will reduce hashcode entropy, but maybe that's fine.
Motivation
headersWithSameNamesAndValuesShouldBeEquivalentinAbstractHttpHeadersTestincluded assertions onhashCode()that were failing unpredictably:headers1.hashCode() != headers1.hashCode()headers2.hashCode() != headers2.hashCode()headers1.hashCode() != headers2.hashCode()These failures occur because the hashCode() implementation is non-deterministic for these header objects. The assertions were invalid and caused unreliable test failures with nondex.
Modifications
Result