Refactor code for gracefully switching child LB policies.#22101
Refactor code for gracefully switching child LB policies.#22101markdroth merged 1 commit intogrpc:masterfrom
Conversation
f105e86 to
07d42d8
Compare
|
Sorry, had to do a force-push to bring in some changes. The last commit is still what's new here. |
c0e4a3c to
7ca20bd
Compare
|
I've merged in the changes that this depended on, so this PR now contains only its own changes. Feel free to review. Thanks! |
| @@ -0,0 +1,277 @@ | |||
| // | |||
| // Copyright 2018 gRPC authors. | |||
There was a problem hiding this comment.
This is basically moving code from another file, and the code was originally written in 2018, so I'm going to keep that date.
| @@ -0,0 +1,66 @@ | |||
| // | |||
| // Copyright 2018 gRPC authors. | |||
There was a problem hiding this comment.
The code was originally written in 2018.
| } | ||
| auto& lb_policy = | ||
| child_policy_ == nullptr ? child_policy_ : pending_child_policy_; | ||
| lb_policy = CreateChildPolicy(child_policy_name, *args.args); |
There was a problem hiding this comment.
Why would we want to dereference the pointer here and then take address again in this function? Can we pass const grpc_channel_args* instead?
There was a problem hiding this comment.
The handing of grpc_channel_args is inconsistent all across our codebase, unfortunately. It's one of our oldest types, long predating the conversion from C to C++, so most of our older interfaces take it as a pointer. However, the C++ convention is to pass read-only arguments as const references, so that's what we do in newer code.
At some point, I would like to find time to convert grpc_channel_args to C++ and fix all of our code to use a more idiomatic interface. But until then, these inconsistencies will have to remain.
There was a problem hiding this comment.
Sounds reasonable. Is this convention a gRPC thing or it's in the cpp style? I searched through cppstype a bit but did not find any reference
There was a problem hiding this comment.
It is in the style guide, but confusingly, it's buried in the middle of a section about output parameters, even though it's talking about input parameters:
https://google.github.io/styleguide/cppguide.html#Output_Parameters
"Input parameters are usually values or const references"
| parent_->child_policy_->interested_parties(), | ||
| parent_->interested_parties()); | ||
| parent_->child_policy_ = std::move(parent_->pending_child_policy_); | ||
| } else if (!CalledByCurrentChild()) { |
There was a problem hiding this comment.
This is very nit, but would it be a little bit less awkward if you change the negative condition to a positive condition? When I read this line I was like "why would we even care if it's not a pending and not a current child", then after 3 seconds it became "oh yea we do not care about this case indeed". Changing to a positive condition would make it flow easier I guess.
There was a problem hiding this comment.
Can you give an example of what you mean by "a positive condition"? I'm not sure what you mean.
Note that I can't just change this to check that it is from the current child and then move line 67 inside of the body here, because we want line 67 to run even if the case of a pending child that has just gone READY and been swapped into being the current child.
There was a problem hiding this comment.
Note that I can't just change this to check that it is from the current child
That was what I meant. And I see the point here; you are right.
| parent_->pending_child_policy_ != nullptr | ||
| ? parent_->pending_child_policy_.get() | ||
| : parent_->child_policy_.get(); | ||
| if (child_ != latest_child_policy) return; |
There was a problem hiding this comment.
Could you explain why it's only the latest child policy that can trigger a reresolution? Maybe with a line of comment.
| if (GRPC_TRACE_FLAG_ENABLED(grpc_lb_glb_trace)) { | ||
| gpr_log(GPR_INFO, "[grpclb %p] Created new child policy %s (%p)", this, | ||
| name, lb_policy.get()); | ||
| gpr_log(GPR_INFO, "[grpclb %p] Created new child policy (%p)", this, |
There was a problem hiding this comment.
Since we know it's a child policy handler that got created, maybe we can log "Created new child policy handler" to tell things apart better. But current state is also fine too. Ditto to the gpr_log a few lines above.
| {"round_robin", Json::Object()}, | ||
| }}; | ||
| } else { | ||
| child_policy_config_json = it->second; |
There was a problem hiding this comment.
This could be a big copy. Consider use pointer for child_policy_config_json?
| {"round_robin", Json::Object()}, | ||
| }}; | ||
| } else { | ||
| child_policy_json = it->second; |
There was a problem hiding this comment.
Ditto. Can we use pointer instead to avoid possible copy?
| OrphanablePtr<LoadBalancingPolicy> CreateLbPolicyLocked( | ||
| const char* lb_policy_name, const grpc_channel_args& args, | ||
| TraceStringVector* trace_strings); | ||
| const grpc_channel_args& args); |
There was a problem hiding this comment.
Ditto. Use pointer instead of reference here?
There was a problem hiding this comment.
See my reply elsewhere. This is the normal C++ convention for passing a read-only parameter.
a64b259 to
19e28b1
Compare
Refactor code for gracefully switching child LB policies.
@muxi, this will probably be useful to you in the RLS policy you're implementing.