KAFKA-7885: TopologyDescription violates equals-hashCode contract.#6210
KAFKA-7885: TopologyDescription violates equals-hashCode contract.#6210mjsax merged 5 commits intoapache:trunkfrom
Conversation
810f7ec to
82b7136
Compare
mjsax
left a comment
There was a problem hiding this comment.
Nice catch! And thanks for the PR!
One nit. Overall LGTM.
Call for second review @guozhangwang @bbejeck @vvcephei @ableegoldman
|
Hi @jCalamari , thanks for this PR and the Jira. I'm in favor of the change, but I'd like to make sure I understand the bug report... Looking at the diff here, I don't see how the prior code was in violation of the contract. Can you elaborate, please? Thanks, |
|
Hi John, many thanks for reviewing the PR. This test line fails on latest apache/trunk branch:
whereas this succeeds:
As per Java documentation, this is equlas-hashcode contract violation:
Also, I don't mind changing the test to make violation more obvious, any suggestions? Thanks, |
|
Hi @jCalamari , Thanks for the response! Sorry for saying that your test doesn't test the contract. I see now that it does. I'd misread it. It surprised me that the two description instances would be considered equal before your change, since when I dug into it, and it turns out that two Therefore, no only are the two topologies in your test I couldn't believe it, so I tried it: Of course, these two topologies will still have different |
|
Hi @vvcephei , very interesting finding! I can think of the following solutions, based on your feedback:
|
|
:) This makes me wonder how many others have already walked this path and chosen option 3 ;) I'll leave the choice between 1 and 2 up to you. I think being able to compare topologies for equality could be useful, but it could also be misleading. For example, you can alter the function passed to a At the least, we should document it, so the next person doesn't fall in the same pit. Actually, we can do better than that, if we just throw an exception inside of Thanks again for bringing this up! |
There was a problem hiding this comment.
@jCalamari thanks for the patch!
Having thought on this some more, while in the strictest of terms this PR is an improvement, it makes more sense to implement the changes found here and here to make for a complete change, so maybe we should not merge until all pieces are in place.
|
Hi @bbejeck , I completely agree with you, I might give it a go over this weekend. Many thanks |
|
@jCalamari Can you update the Jira tickets with the new finding (or create new ones)? |
|
Hi @vvcephei , @mjsax Advantages:
Disadvantages:
Throwing an exception inside of |
|
I think we should do the "right thing". Easier code does not seem to be a good argument here. |
|
@mjsax Shall we close this PR and open new one, once I figure out the "right thing"? |
|
Hi @jCalamari , That's a clever solution, but I have to agree with @mjsax . Another advantage of the toString approach is that, if they look the same, they are the same. But on the disadvantage side, it's generally an anti-pattern to incorporate the output of I believe this is generally interpreted as meaning that To be fair, this anti-pattern is in wide use, including in the Java core, see Duration, for example, where you're encouraged to Another reason not to implement equality/hashcode with toString is that, users can trivially do the same thing on the "outside" of the library, by just using I've previously sanity-checked the idea of throwing exceptions like One alternative (which would also resolve KAFKA-7885) is just to remove the equals/hashcode implementations. Then, TopologyDescription would fall back to the default (correct) implementation of identity equality/hashcode. |
I would just update this PR. There is not much to "figure out" IMHO -- we should add |
2406f52 to
56fff1b
Compare
|
I have removed default hashCode/equals implementations from
Any thoughts? |
|
Hey, @jCalamari , That's a conundrum. It would be fine, IMHO, to modify the test logic and the Streams logic to get it to come out the same. But I understand this might not be as easy as it sounds. I had a (probably bad) idea that in those cases, maybe we can just split the string on So, I tried this for the tests that are still failing: private void verify(final TopologyDescription actual, final InternalTopologyBuilder.TopologyDescription expected) {
final String[] actuals = actual.toString().split("\n");
Arrays.sort(actuals);
final String[] expecteds = expected.toString().split("\n");
Arrays.sort(expecteds);
assertThat(actuals, equalTo(expecteds));
}Funny enough, I still get a couple of test failures, and it looks like there's either a flaw in the tests, or a bug in Streams: |
|
@jCalamari -- Just cycling back to this PR -- any update on this? We recently changes some I still think, we should not remove |
…Code contract" This reverts commit 56fff1b.
|
Hey @mjsax Mind having a quick look at the PR again? Many thanks |
|
jCalamari @PeterFras Sorry that I dropped this PR. Just came across this again. LGTM. Thanks a lot for your contribution! Btw: if this happens in the future (ie, your PR gets no attention), feel free to ping us on a weekly basis to get our attention back. Kafka is a very busy project and if you don't "fight" for attention, it can easily happen that a PR sits there for many month... :( |
* 'trunk' of github.com:apache/kafka: (28 commits) MINOR: cleanup RocksDBStore tests (apache#8510) KAFKA-9818: Fix flaky test in RecordCollectorTest (apache#8507) MINOR: reduce impact of trace logging in replica hot path (apache#8468) KAFKA-6145: KIP-441: Add test scenarios to ensure rebalance convergence (apache#8475) KAFKA-9881: Convert integration test to verify measurements from RocksDB to unit test (apache#8501) MINOR: improve test coverage for dynamic LogConfig(s) (apache#7616) MINOR: Switch order of sections on tumbling and hopping windows in streams doc. Tumbling windows are defined as "special case of hopping time windows" - but hopping windows currently only explained later in the docs. (apache#8505) KAFKA-9819: Fix flaky test in StoreChangelogReaderTest (apache#8488) HOTFIX: fix active task process ratio metric recording KAFKA-9796; Ensure broker shutdown is not stuck when Acceptor is waiting on connection queue (apache#8448) MINOR: Use streaming iterator with decompression buffer when building offset map (apache#8494) Add log message in release.py (apache#8461) KAFKA-9854 Re-authenticating causes mismatched parse of response (apache#8471) KAFKA-9838; Add log concurrency test and fix minor race condition (apache#8476) KAFKA-9703; Free up compression buffer after splitting a large batch KAFKA-9779: Add Stream system test for 2.5 release (apache#8378) KAFKA-7885: TopologyDescription violates equals-hashCode contract. (apache#6210) MINOR: KafkaApis#handleOffsetDeleteRequest does not group result correctly (apache#8485) HOTFIX: don't close or wipe out someone else's state (apache#8478) MINOR: add process(Test)Messages to the README (apache#8480) ...
Overwrote hash code implementation in StaticTopicNameExtractor.
Also overwrote
equalsto pass checkstyle.Added a unit test confirming equals-hashCode contract.