api: fix bugs of missing to copy customOptions when converting to a new builder, also trash hashCode/equals#5647
Conversation
|
|
||
| private <T> Builder copyCustomOptions(Object[][] options) { | ||
| customOptions = new Object[options.length][2]; | ||
| System.arraycopy(customOptions, 0, options, 0, options.length); |
There was a problem hiding this comment.
No. Options are intended to be mutable. So keys and values are compared by identity.
There was a problem hiding this comment.
I'm confused. You used deepEquals for comparison which is not compared by identity. And here, coping builder is not for comparison.
There was a problem hiding this comment.
What does deep copy even mean here? The keys are immutable and the objects are arbitrary. How could we do a deep copy?
There was a problem hiding this comment.
@dapengzhang0
In equals method, I should do unordered identity-based equal instead of deepEquals. Keys and values should be used as identity-based.
There was a problem hiding this comment.
I can agree using shallow copy for copying builder, but equals and hashCode should also be shallow for the same reason.
There was a problem hiding this comment.
ejona86
What does deep copy even mean here? The keys are immutable and the objects are arbitrary. How could we do a deep copy?
I was just glancing at the code and noticed the inconsistency between copy and equals/hashCode, didn't know about the mutability detail.
There was a problem hiding this comment.
I don't understand what equals has to do with copy. We can use equals() even if we don't deep copy.
Keys should be identity-based. It is unclear if values should. I'd have expected to choose identity if we don't care about equals for custom options and use equals otherwise. This is why I hate defining equals/hashCode...
There was a problem hiding this comment.
lol. This is very subtle. Strings are always identity-based if we use with literals, while boxed numbers are not. This might bring us some trouble when using this. But I would agree that leaving values identity-based is better.
There was a problem hiding this comment.
Okay. I'm not hearing anything a reason for using identity for values. Let's use equals().
I'm still +1 for not defining equals though. This doesn't seem like something that anyone would even use during tests. With the stateListener, it is highly, highly unlikely to be equal with another instance.
| return Objects.equal(addrs, that.addrs) && Objects.equal(attrs, that.attrs) | ||
| && Objects.equal(stateListener, that.stateListener); | ||
| && Objects.equal(stateListener, that.stateListener) | ||
| && Arrays.deepEquals(customOptions, that.customOptions); |
There was a problem hiding this comment.
It's unclear what equality really means, but depending on the use-case the order that custom options were set should not matter, but here it does. (see also: hash code)
There was a problem hiding this comment.
Huh. Shouldn't this also apply for EquivalentAddressGroup?
There was a problem hiding this comment.
Order matters for sockets in EAG. It doesn't matter in Attributes. And in attributes we ignore order.
It looks like we've not defined equals for CallOptions, not Context.
b8707d8 to
e756ea8
Compare
| return false; | ||
| } | ||
| if (customOptions.length > 0) { | ||
| Map<Object, Object> thisOptionMap = new HashMap<>(); |
There was a problem hiding this comment.
Set<?> thisOptionSet = new HashSet<>(Arrays.asList((Object[]) customOptions));
Set<?> thatOptionSet = new HashSet<>(Arrays.asList((Object[]) that.customOptions));
might be better?
There was a problem hiding this comment.
Never mind, this is not the intended behavior.
…Buider, hashCode, equals
e756ea8 to
56a3967
Compare
|
@voidzcy, please avoid force-pushing during review, as it means we can't see what changed since we looked at it last. |
ejona86
left a comment
There was a problem hiding this comment.
Discussing with @zhangkun83, and we can't come up with a time equals/hashCode would actually be useful, even in a test. Unless there's a good reason to have them, we should trash equals/hashCode instead of "fixing" them.
75387f8 to
e079575
Compare
e079575 to
ae85e50
Compare
ejona86
left a comment
There was a problem hiding this comment.
Note: I only realized now that we are using a 2d array for options. Although it doesn't matter, generally we would prefer a 1d array. I may now better understand comments from @dapengzhang0 about deep copies.
ae85e50 to
8de6443
Compare
This is the second (last) thing I missed in #5640 .