Skip to content
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

io.grpc.Attributes - reference vs. object equality #3857

Closed
nicktrav opened this issue Dec 11, 2017 · 4 comments
Closed

io.grpc.Attributes - reference vs. object equality #3857

nicktrav opened this issue Dec 11, 2017 · 4 comments
Labels
Milestone

Comments

@nicktrav
Copy link
Contributor

io.grpc.Attributes was modified in 291f170 to use an IdentityMap to store the underlying attributes. This map uses reference equality in place of the object equality that was used in prior versions, when the class used HashMap.

This is more of a question around the expected behavior of Attributes#equals and the notion of "attribute equality" as opposed to a bug or an issue. For context, we have some tests internally that are failing in 1.8 as they were written with object equality in mind.

For example, the following fails in 1.8 but passes in prior versions, which upon immediate inspection wasn't intuitive, as InetSocketAddress#equals returns true when comparing the two different objects:

  @Test
  public void equals() {
    Attributes.Key<InetSocketAddress> attr = Attributes.Key.of("foo");

    InetSocketAddress addr1 = new InetSocketAddress(80);
    Attributes attrs1 = Attributes.newBuilder()
        .set(attr, addr1)
        .build();

    InetSocketAddress addr2 = new InetSocketAddress(80);
    Attributes attrs2 = Attributes.newBuilder()
        .set(attr, addr2)
        .build();

    assertEquals(addr1, addr2);   // passes
    assertEquals(attrs1, attrs2); // fails
  }

I'm interested in the motivation for the change in semantics (talking with @lukaszx0 in person, he recalls some talk about this previously). If reference equality was the intention, perhaps we could tighten up the javadoc a little to make it more explicit that this class now uses reference equality as opposed to object equality?

Or, perhaps this wasn't the original intention and it would be possible to revert to using a HashMap, although the benchmarks seem to indicate this would be slightly slower than the current impl.

@carl-mastrangelo
Copy link
Contributor

I don't believe that side effect was intended, only the keys were supposed to be compared by reference equality. We could change it back, because it seems like a legit use case.

Do you mainly use it in tests or in prod code?

@ejona86
Copy link
Member

ejona86 commented Dec 11, 2017

We should change it back. Yeah, we should use object-equality for the values themselves.

@ejona86 ejona86 added bug and removed question labels Dec 11, 2017
@ejona86 ejona86 added this to the Next milestone Dec 11, 2017
carl-mastrangelo added a commit to carl-mastrangelo/grpc-java that referenced this issue Dec 11, 2017
carl-mastrangelo added a commit to carl-mastrangelo/grpc-java that referenced this issue Dec 11, 2017
@nicktrav
Copy link
Contributor Author

@carl-mastrangelo @ejona86 - from what I can tell we're only relying on this in tests right now.

Thanks for the prompt reply / fix nonetheless! 🙏

@nicktrav nicktrav reopened this Dec 11, 2017
@nicktrav
Copy link
Contributor Author

(ignore my trigger finger on the close button)

carl-mastrangelo added a commit that referenced this issue Dec 12, 2017
@ejona86 ejona86 modified the milestones: Next, 1.9 Dec 18, 2017
@lock lock bot locked as resolved and limited conversation to collaborators Sep 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants