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

Fix issue: CallOptions is not thread-safe. #9689

Merged
merged 9 commits into from Nov 17, 2022

Conversation

pandaapo
Copy link
Contributor

Fixes #9658

Although CallOptions is annotated by @Immutable, its fields are not final. So it's not truly immutable, namely not thread-safe.

This PR add final to all fields of CallOptions and remove @Immutable. Using internal builder class to keep flexibility of constructing CallOptions .

@sanjaypujare sanjaypujare added the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 14, 2022
@grpc-kokoro grpc-kokoro removed the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 14, 2022
Copy link
Contributor

@sanjaypujare sanjaypujare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

couple of comments added

api/src/main/java/io/grpc/CallOptions.java Outdated Show resolved Hide resolved
api/src/main/java/io/grpc/CallOptions.java Outdated Show resolved Hide resolved
api/src/main/java/io/grpc/CallOptions.java Outdated Show resolved Hide resolved
Copy link
Contributor

@sanjaypujare sanjaypujare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but I would like to see my last comment addressed

@sanjaypujare sanjaypujare added the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 16, 2022
@grpc-kokoro grpc-kokoro removed the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 16, 2022
@sanjaypujare sanjaypujare added the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 17, 2022
@grpc-kokoro grpc-kokoro removed the kokoro:run Add this label to a PR to tell Kokoro the code is safe and tests can be run label Nov 17, 2022
@pandaapo
Copy link
Contributor Author

@sanjaypujare @ejona86 Could you help to merge this PR now?

@sanjaypujare
Copy link
Contributor

sanjaypujare commented Nov 17, 2022

@sanjaypujare @ejona86 Could you help to merge this PR now?

Couple of things.

  • there is a test failure

image

The details are here https://source.cloud.google.com/results/invocations/ac617d67-b1bf-41b5-8cb4-3de99df7c53b/targets/github%2Fgrpc-java%2Fapi%2Fbuild%2Ftest-results%2Ftest%2FTEST-io.grpc.CallOptionsTest/tests and this shows up as

expected : 59.980091551s from now
but was : 59.979571156s from now
outside tolerance in ns: 10000000
at io.grpc.CallOptionsTest.withDeadlineAfter(CallOptionsTest.java:141)

Most probably not related to this change but can you look into it?

  • @ejona86 needs to approve. I will then merge

@pandaapo
Copy link
Contributor Author

  • there is a test failure
image

The details are here https://source.cloud.google.com/results/invocations/ac617d67-b1bf-41b5-8cb4-3de99df7c53b/targets/github%2Fgrpc-java%2Fapi%2Fbuild%2Ftest-results%2Ftest%2FTEST-io.grpc.CallOptionsTest/tests and this shows up as

expected : 59.980091551s from now but was : 59.979571156s from now outside tolerance in ns: 10000000 at io.grpc.CallOptionsTest.withDeadlineAfter(CallOptionsTest.java:141)

Most probably not be related to this change but can you look into it?

I noticed this. I want to fix it but my OS is windows. I can not reproduce this failure.

@sanjaypujare
Copy link
Contributor

I noticed this. I want to fix it but my OS is windows. I can not reproduce this failure.

This could just be a flake because of its nature but it happens to be in CallOptionsTest which is the part you touched. Not sure what you were planning to fix.

Let me rerun the tests and see what happens.

@sanjaypujare sanjaypujare added the kokoro:force-run Add this label to a PR to tell Kokoro to re-run all tests. Not generally necessary label Nov 17, 2022
@grpc-kokoro grpc-kokoro removed the kokoro:force-run Add this label to a PR to tell Kokoro to re-run all tests. Not generally necessary label Nov 17, 2022
String authority;
CallCredentials credentials;
String compressorName;
Object[][] customOptions = new Object[0][2];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this object will only be used once, this will create it all the time. This and the emptyList() below are just for DEFAULT, so we should just make a static method to configure the default builder, or use a static block. I'll do that as a follow-up.

The optimizer should optimize these away, but it would still be there on startup and cause a non-trivial constructor. So it isn't a big deal, but still worth doing.

For reference, it'll look something like this:

static {
  Builder b = new Builder();
  b.customOptions = new Object[0][2];
  b.streamTracerFactories = Collections.emptyList();
  DEFAULT = b.build();
}

// or

public static final CallOptions DEFAULT = newDefault();

private static CallOptions newDefault() {
  Builder b = new Builder();
  b.customOptions = new Object[0][2];
  b.streamTracerFactories = Collections.emptyList();
  return b.build();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The optimizer should optimize these away, but it would still be there on startup and cause a non-trivial constructor.

Thanks for your work. I am curious about this. Does you mean that the java optimizer can optimize Builder to make customOptions will not be created all the time even if without modifying my Builder?

@ejona86 ejona86 merged commit 1b94f48 into grpc:master Nov 17, 2022
@ejona86
Copy link
Member

ejona86 commented Nov 17, 2022

@pandaapo, thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CallOptions is not thread-safe
4 participants