Skip to content

Conversation

@markdroth
Copy link
Member

No description provided.

@markdroth markdroth added the release notes: no Indicates if PR should not be in release notes label Jun 4, 2021
@markdroth markdroth requested a review from apolcyn June 4, 2021 22:27
// Construct bootstrap JSON.
Json::Object node = {
{"id", "C2P"},
{"id", absl::StrCat("C2P-", rand())},
Copy link
Contributor

Choose a reason for hiding this comment

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

Even though we use rand() in a lot of other places in core, it doesn't seem completely correct to use it given:

  • AFAIK rand() isn't generally guaranteed to be thread safe
  • it's not guaranteed to have been seeded

The backoff code uses it's own random number generator which I believe solves this - this seems like the correct approach to me, any reason not to do what the backoff code is doing instead of using rand() ?

Copy link
Member Author

Choose a reason for hiding this comment

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

See discussion in #17891. I think rand() is sufficient for what we need here.

Copy link
Contributor

@apolcyn apolcyn Jun 4, 2021

Choose a reason for hiding this comment

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

The broken case I can imagine here binaries which aren't calling srand at the top of main. They would all generate the same random number in their node IDs and so defeat the purpose of the random number.

Another alternative to rand may be to use the absl random library, which takes care of seeding.

That said, since this is used in many other places, this is a wider discussion from this PR so doesn't need to block

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, actually, I think you're right: having every client report the same random number will kind of defeat the purpose here. Our other uses of rand() inside of C-core are not visible outside of our code, so it doesn't matter as much if there's a little bit of determinism.

Unfortunately, we can't yet use the absl random library, as per https://github.com/grpc/grpc/blob/master/third_party/ABSEIL_MANUAL.md. I've asked @veblush to look into what's blocking us from using it. If we can get that resolved quickly, I can change this code to use that.

Copy link
Member Author

Choose a reason for hiding this comment

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

For now, I've changed this to use the C++ random library. PTAL.

@markdroth markdroth merged commit 7879498 into grpc:master Jun 8, 2021
@markdroth markdroth deleted the c2p_resolver_node_id_random branch June 8, 2021 22:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: no Indicates if PR should not be in release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants