3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/filters/client_channel/resolving_lb_policy.h"
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/string_util.h>
32 #include <grpc/support/sync.h>
34 #include "src/core/ext/filters/client_channel/backup_poller.h"
35 #include "src/core/ext/filters/client_channel/http_connect_handshaker.h"
36 #include "src/core/ext/filters/client_channel/lb_policy_registry.h"
37 #include "src/core/ext/filters/client_channel/proxy_mapper_registry.h"
38 #include "src/core/ext/filters/client_channel/resolver_registry.h"
39 #include "src/core/ext/filters/client_channel/retry_throttle.h"
40 #include "src/core/ext/filters/client_channel/server_address.h"
41 #include "src/core/ext/filters/client_channel/service_config.h"
42 #include "src/core/ext/filters/client_channel/subchannel.h"
43 #include "src/core/ext/filters/deadline/deadline_filter.h"
44 #include "src/core/lib/backoff/backoff.h"
45 #include "src/core/lib/channel/channel_args.h"
46 #include "src/core/lib/channel/connected_channel.h"
47 #include "src/core/lib/channel/status_util.h"
48 #include "src/core/lib/gpr/string.h"
49 #include "src/core/lib/gprpp/inlined_vector.h"
50 #include "src/core/lib/gprpp/manual_constructor.h"
51 #include "src/core/lib/gprpp/sync.h"
52 #include "src/core/lib/iomgr/combiner.h"
53 #include "src/core/lib/iomgr/iomgr.h"
54 #include "src/core/lib/iomgr/polling_entity.h"
55 #include "src/core/lib/profiling/timers.h"
56 #include "src/core/lib/slice/slice_internal.h"
57 #include "src/core/lib/slice/slice_string_helpers.h"
58 #include "src/core/lib/surface/channel.h"
59 #include "src/core/lib/transport/connectivity_state.h"
60 #include "src/core/lib/transport/error_utils.h"
61 #include "src/core/lib/transport/metadata.h"
62 #include "src/core/lib/transport/metadata_batch.h"
63 #include "src/core/lib/transport/static_metadata.h"
64 #include "src/core/lib/transport/status_metadata.h"
69 // ResolvingLoadBalancingPolicy::ResolverResultHandler
72 class ResolvingLoadBalancingPolicy::ResolverResultHandler
73 : public Resolver::ResultHandler {
75 explicit ResolverResultHandler(
76 RefCountedPtr<ResolvingLoadBalancingPolicy> parent)
77 : parent_(std::move(parent)) {}
79 ~ResolverResultHandler() {
80 if (GRPC_TRACE_FLAG_ENABLED(*(parent_->tracer_))) {
81 gpr_log(GPR_INFO, "resolving_lb=%p: resolver shutdown complete",
86 void ReturnResult(Resolver::Result result) override {
87 parent_->OnResolverResultChangedLocked(std::move(result));
90 void ReturnError(grpc_error* error) override {
91 parent_->OnResolverError(error);
95 RefCountedPtr<ResolvingLoadBalancingPolicy> parent_;
99 // ResolvingLoadBalancingPolicy::ResolvingControlHelper
102 class ResolvingLoadBalancingPolicy::ResolvingControlHelper
103 : public LoadBalancingPolicy::ChannelControlHelper {
105 explicit ResolvingControlHelper(
106 RefCountedPtr<ResolvingLoadBalancingPolicy> parent)
107 : parent_(std::move(parent)) {}
109 RefCountedPtr<SubchannelInterface> CreateSubchannel(
110 const grpc_channel_args& args) override {
111 if (parent_->resolver_ == nullptr) return nullptr; // Shutting down.
112 if (!CalledByCurrentChild() && !CalledByPendingChild()) return nullptr;
113 return parent_->channel_control_helper()->CreateSubchannel(args);
116 grpc_channel* CreateChannel(const char* target,
117 const grpc_channel_args& args) override {
118 if (parent_->resolver_ == nullptr) return nullptr; // Shutting down.
119 if (!CalledByCurrentChild() && !CalledByPendingChild()) return nullptr;
120 return parent_->channel_control_helper()->CreateChannel(target, args);
123 void UpdateState(grpc_connectivity_state state,
124 UniquePtr<SubchannelPicker> picker) override {
125 if (parent_->resolver_ == nullptr) return; // Shutting down.
126 // If this request is from the pending child policy, ignore it until
127 // it reports READY, at which point we swap it into place.
128 if (CalledByPendingChild()) {
129 if (GRPC_TRACE_FLAG_ENABLED(*(parent_->tracer_))) {
131 "resolving_lb=%p helper=%p: pending child policy %p reports "
133 parent_.get(), this, child_,
134 grpc_connectivity_state_name(state));
136 if (state != GRPC_CHANNEL_READY) return;
137 grpc_pollset_set_del_pollset_set(
138 parent_->lb_policy_->interested_parties(),
139 parent_->interested_parties());
140 parent_->lb_policy_ = std::move(parent_->pending_lb_policy_);
141 } else if (!CalledByCurrentChild()) {
142 // This request is from an outdated child, so ignore it.
145 parent_->channel_control_helper()->UpdateState(state, std::move(picker));
148 void RequestReresolution() override {
149 // If there is a pending child policy, ignore re-resolution requests
150 // from the current child policy (or any outdated child).
151 if (parent_->pending_lb_policy_ != nullptr && !CalledByPendingChild()) {
154 if (GRPC_TRACE_FLAG_ENABLED(*(parent_->tracer_))) {
155 gpr_log(GPR_INFO, "resolving_lb=%p: started name re-resolving",
158 if (parent_->resolver_ != nullptr) {
159 parent_->resolver_->RequestReresolutionLocked();
163 void set_child(LoadBalancingPolicy* child) { child_ = child; }
166 bool CalledByPendingChild() const {
167 GPR_ASSERT(child_ != nullptr);
168 return child_ == parent_->pending_lb_policy_.get();
171 bool CalledByCurrentChild() const {
172 GPR_ASSERT(child_ != nullptr);
173 return child_ == parent_->lb_policy_.get();
176 RefCountedPtr<ResolvingLoadBalancingPolicy> parent_;
177 LoadBalancingPolicy* child_ = nullptr;
181 // ResolvingLoadBalancingPolicy
184 ResolvingLoadBalancingPolicy::ResolvingLoadBalancingPolicy(
185 Args args, TraceFlag* tracer, UniquePtr<char> target_uri,
186 UniquePtr<char> child_policy_name,
187 RefCountedPtr<LoadBalancingPolicy::Config> child_lb_config,
189 : LoadBalancingPolicy(std::move(args)),
191 target_uri_(std::move(target_uri)),
192 child_policy_name_(std::move(child_policy_name)),
193 child_lb_config_(std::move(child_lb_config)) {
194 GPR_ASSERT(child_policy_name_ != nullptr);
195 // Don't fetch service config, since this ctor is for use in nested LB
196 // policies, not at the top level, and we only fetch the service
197 // config at the top level.
198 grpc_arg arg = grpc_channel_arg_integer_create(
199 const_cast<char*>(GRPC_ARG_SERVICE_CONFIG_DISABLE_RESOLUTION), 0);
200 grpc_channel_args* new_args =
201 grpc_channel_args_copy_and_add(args.args, &arg, 1);
202 *error = Init(*new_args);
203 grpc_channel_args_destroy(new_args);
206 ResolvingLoadBalancingPolicy::ResolvingLoadBalancingPolicy(
207 Args args, TraceFlag* tracer, UniquePtr<char> target_uri,
208 ProcessResolverResultCallback process_resolver_result,
209 void* process_resolver_result_user_data, grpc_error** error)
210 : LoadBalancingPolicy(std::move(args)),
212 target_uri_(std::move(target_uri)),
213 process_resolver_result_(process_resolver_result),
214 process_resolver_result_user_data_(process_resolver_result_user_data) {
215 GPR_ASSERT(process_resolver_result != nullptr);
216 *error = Init(*args.args);
219 grpc_error* ResolvingLoadBalancingPolicy::Init(const grpc_channel_args& args) {
220 resolver_ = ResolverRegistry::CreateResolver(
221 target_uri_.get(), &args, interested_parties(), combiner(),
222 UniquePtr<Resolver::ResultHandler>(New<ResolverResultHandler>(Ref())));
223 if (resolver_ == nullptr) {
224 return GRPC_ERROR_CREATE_FROM_STATIC_STRING("resolver creation failed");
226 // Return our picker to the channel.
227 channel_control_helper()->UpdateState(
228 GRPC_CHANNEL_IDLE, UniquePtr<SubchannelPicker>(New<QueuePicker>(Ref())));
229 return GRPC_ERROR_NONE;
232 ResolvingLoadBalancingPolicy::~ResolvingLoadBalancingPolicy() {
233 GPR_ASSERT(resolver_ == nullptr);
234 GPR_ASSERT(lb_policy_ == nullptr);
237 void ResolvingLoadBalancingPolicy::ShutdownLocked() {
238 if (resolver_ != nullptr) {
240 if (lb_policy_ != nullptr) {
241 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
242 gpr_log(GPR_INFO, "resolving_lb=%p: shutting down lb_policy=%p", this,
245 grpc_pollset_set_del_pollset_set(lb_policy_->interested_parties(),
246 interested_parties());
249 if (pending_lb_policy_ != nullptr) {
250 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
251 gpr_log(GPR_INFO, "resolving_lb=%p: shutting down pending lb_policy=%p",
252 this, pending_lb_policy_.get());
254 grpc_pollset_set_del_pollset_set(pending_lb_policy_->interested_parties(),
255 interested_parties());
256 pending_lb_policy_.reset();
261 void ResolvingLoadBalancingPolicy::ExitIdleLocked() {
262 if (lb_policy_ != nullptr) {
263 lb_policy_->ExitIdleLocked();
264 if (pending_lb_policy_ != nullptr) pending_lb_policy_->ExitIdleLocked();
266 if (!started_resolving_ && resolver_ != nullptr) {
267 StartResolvingLocked();
272 void ResolvingLoadBalancingPolicy::ResetBackoffLocked() {
273 if (resolver_ != nullptr) {
274 resolver_->ResetBackoffLocked();
275 resolver_->RequestReresolutionLocked();
277 if (lb_policy_ != nullptr) lb_policy_->ResetBackoffLocked();
278 if (pending_lb_policy_ != nullptr) pending_lb_policy_->ResetBackoffLocked();
281 void ResolvingLoadBalancingPolicy::StartResolvingLocked() {
282 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
283 gpr_log(GPR_INFO, "resolving_lb=%p: starting name resolution", this);
285 GPR_ASSERT(!started_resolving_);
286 started_resolving_ = true;
287 channel_control_helper()->UpdateState(
288 GRPC_CHANNEL_CONNECTING,
289 UniquePtr<SubchannelPicker>(New<QueuePicker>(Ref())));
290 resolver_->StartLocked();
293 void ResolvingLoadBalancingPolicy::OnResolverError(grpc_error* error) {
294 if (resolver_ == nullptr) {
295 GRPC_ERROR_UNREF(error);
298 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
299 gpr_log(GPR_INFO, "resolving_lb=%p: resolver transient failure: %s", this,
300 grpc_error_string(error));
302 // If we already have an LB policy from a previous resolution
303 // result, then we continue to let it set the connectivity state.
304 // Otherwise, we go into TRANSIENT_FAILURE.
305 if (lb_policy_ == nullptr) {
306 grpc_error* state_error = GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(
307 "Resolver transient failure", &error, 1);
308 channel_control_helper()->UpdateState(
309 GRPC_CHANNEL_TRANSIENT_FAILURE,
310 UniquePtr<SubchannelPicker>(New<TransientFailurePicker>(state_error)));
312 GRPC_ERROR_UNREF(error);
315 void ResolvingLoadBalancingPolicy::CreateOrUpdateLbPolicyLocked(
316 const char* lb_policy_name,
317 RefCountedPtr<LoadBalancingPolicy::Config> lb_policy_config,
318 Resolver::Result result, TraceStringVector* trace_strings) {
319 // If the child policy name changes, we need to create a new child
320 // policy. When this happens, we leave child_policy_ as-is and store
321 // the new child policy in pending_child_policy_. Once the new child
322 // policy transitions into state READY, we swap it into child_policy_,
323 // replacing the original child policy. So pending_child_policy_ is
324 // non-null only between when we apply an update that changes the child
325 // policy name and when the new child reports state READY.
327 // Updates can arrive at any point during this transition. We always
328 // apply updates relative to the most recently created child policy,
329 // even if the most recent one is still in pending_child_policy_. This
330 // is true both when applying the updates to an existing child policy
331 // and when determining whether we need to create a new policy.
333 // As a result of this, there are several cases to consider here:
335 // 1. We have no existing child policy (i.e., we have started up but
336 // have not yet received a serverlist from the balancer or gone
337 // into fallback mode; in this case, both child_policy_ and
338 // pending_child_policy_ are null). In this case, we create a
339 // new child policy and store it in child_policy_.
341 // 2. We have an existing child policy and have no pending child policy
342 // from a previous update (i.e., either there has not been a
343 // previous update that changed the policy name, or we have already
344 // finished swapping in the new policy; in this case, child_policy_
345 // is non-null but pending_child_policy_ is null). In this case:
346 // a. If child_policy_->name() equals child_policy_name, then we
347 // update the existing child policy.
348 // b. If child_policy_->name() does not equal child_policy_name,
349 // we create a new policy. The policy will be stored in
350 // pending_child_policy_ and will later be swapped into
351 // child_policy_ by the helper when the new child transitions
354 // 3. We have an existing child policy and have a pending child policy
355 // from a previous update (i.e., a previous update set
356 // pending_child_policy_ as per case 2b above and that policy has
357 // not yet transitioned into state READY and been swapped into
358 // child_policy_; in this case, both child_policy_ and
359 // pending_child_policy_ are non-null). In this case:
360 // a. If pending_child_policy_->name() equals child_policy_name,
361 // then we update the existing pending child policy.
362 // b. If pending_child_policy->name() does not equal
363 // child_policy_name, then we create a new policy. The new
364 // policy is stored in pending_child_policy_ (replacing the one
365 // that was there before, which will be immediately shut down)
366 // and will later be swapped into child_policy_ by the helper
367 // when the new child transitions into state READY.
368 const bool create_policy =
370 lb_policy_ == nullptr ||
372 (pending_lb_policy_ == nullptr &&
373 strcmp(lb_policy_->name(), lb_policy_name) != 0) ||
375 (pending_lb_policy_ != nullptr &&
376 strcmp(pending_lb_policy_->name(), lb_policy_name) != 0);
377 LoadBalancingPolicy* policy_to_update = nullptr;
379 // Cases 1, 2b, and 3b: create a new child policy.
380 // If lb_policy_ is null, we set it (case 1), else we set
381 // pending_lb_policy_ (cases 2b and 3b).
382 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
383 gpr_log(GPR_INFO, "resolving_lb=%p: Creating new %schild policy %s", this,
384 lb_policy_ == nullptr ? "" : "pending ", lb_policy_name);
386 auto& lb_policy = lb_policy_ == nullptr ? lb_policy_ : pending_lb_policy_;
388 CreateLbPolicyLocked(lb_policy_name, *result.args, trace_strings);
389 policy_to_update = lb_policy.get();
391 // Cases 2a and 3a: update an existing policy.
392 // If we have a pending child policy, send the update to the pending
393 // policy (case 3a), else send it to the current policy (case 2a).
394 policy_to_update = pending_lb_policy_ != nullptr ? pending_lb_policy_.get()
397 GPR_ASSERT(policy_to_update != nullptr);
398 // Update the policy.
399 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
400 gpr_log(GPR_INFO, "resolving_lb=%p: Updating %schild policy %p", this,
401 policy_to_update == pending_lb_policy_.get() ? "pending " : "",
404 UpdateArgs update_args;
405 update_args.addresses = std::move(result.addresses);
406 update_args.config = std::move(lb_policy_config);
407 // TODO(roth): Once channel args is converted to C++, use std::move() here.
408 update_args.args = result.args;
409 result.args = nullptr;
410 policy_to_update->UpdateLocked(std::move(update_args));
413 // Creates a new LB policy.
414 // Updates trace_strings to indicate what was done.
415 OrphanablePtr<LoadBalancingPolicy>
416 ResolvingLoadBalancingPolicy::CreateLbPolicyLocked(
417 const char* lb_policy_name, const grpc_channel_args& args,
418 TraceStringVector* trace_strings) {
419 ResolvingControlHelper* helper = New<ResolvingControlHelper>(Ref());
420 LoadBalancingPolicy::Args lb_policy_args;
421 lb_policy_args.combiner = combiner();
422 lb_policy_args.channel_control_helper =
423 UniquePtr<ChannelControlHelper>(helper);
424 lb_policy_args.args = &args;
425 OrphanablePtr<LoadBalancingPolicy> lb_policy =
426 LoadBalancingPolicyRegistry::CreateLoadBalancingPolicy(
427 lb_policy_name, std::move(lb_policy_args));
428 if (GPR_UNLIKELY(lb_policy == nullptr)) {
429 gpr_log(GPR_ERROR, "could not create LB policy \"%s\"", lb_policy_name);
431 gpr_asprintf(&str, "Could not create LB policy \"%s\"", lb_policy_name);
432 trace_strings->push_back(str);
435 helper->set_child(lb_policy.get());
436 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
437 gpr_log(GPR_INFO, "resolving_lb=%p: created new LB policy \"%s\" (%p)",
438 this, lb_policy_name, lb_policy.get());
441 gpr_asprintf(&str, "Created new LB policy \"%s\"", lb_policy_name);
442 trace_strings->push_back(str);
443 grpc_pollset_set_add_pollset_set(lb_policy->interested_parties(),
444 interested_parties());
448 void ResolvingLoadBalancingPolicy::MaybeAddTraceMessagesForAddressChangesLocked(
449 bool resolution_contains_addresses, TraceStringVector* trace_strings) {
450 if (!resolution_contains_addresses &&
451 previous_resolution_contained_addresses_) {
452 trace_strings->push_back(gpr_strdup("Address list became empty"));
453 } else if (resolution_contains_addresses &&
454 !previous_resolution_contained_addresses_) {
455 trace_strings->push_back(gpr_strdup("Address list became non-empty"));
457 previous_resolution_contained_addresses_ = resolution_contains_addresses;
460 void ResolvingLoadBalancingPolicy::ConcatenateAndAddChannelTraceLocked(
461 TraceStringVector* trace_strings) const {
462 if (!trace_strings->empty()) {
465 gpr_strvec_add(&v, gpr_strdup("Resolution event: "));
467 for (size_t i = 0; i < trace_strings->size(); ++i) {
468 if (!is_first) gpr_strvec_add(&v, gpr_strdup(", "));
470 gpr_strvec_add(&v, (*trace_strings)[i]);
473 UniquePtr<char> message(gpr_strvec_flatten(&v, &len));
474 channel_control_helper()->AddTraceEvent(ChannelControlHelper::TRACE_INFO,
476 gpr_strvec_destroy(&v);
480 void ResolvingLoadBalancingPolicy::OnResolverResultChangedLocked(
481 Resolver::Result result) {
482 // Handle race conditions.
483 if (resolver_ == nullptr) return;
484 if (GRPC_TRACE_FLAG_ENABLED(*tracer_)) {
485 gpr_log(GPR_INFO, "resolving_lb=%p: got resolver result", this);
487 // We only want to trace the address resolution in the follow cases:
488 // (a) Address resolution resulted in service config change.
489 // (b) Address resolution that causes number of backends to go from
491 // (c) Address resolution that causes number of backends to go from
493 // (d) Address resolution that causes a new LB policy to be created.
495 // We track a list of strings to eventually be concatenated and traced.
496 TraceStringVector trace_strings;
497 const bool resolution_contains_addresses = result.addresses.size() > 0;
498 // Process the resolver result.
499 const char* lb_policy_name = nullptr;
500 RefCountedPtr<LoadBalancingPolicy::Config> lb_policy_config;
501 bool service_config_changed = false;
502 char* service_config_error_string = nullptr;
503 if (process_resolver_result_ != nullptr) {
504 grpc_error* service_config_error = GRPC_ERROR_NONE;
505 service_config_changed = process_resolver_result_(
506 process_resolver_result_user_data_, result, &lb_policy_name,
507 &lb_policy_config, &service_config_error);
508 if (service_config_error != GRPC_ERROR_NONE) {
509 service_config_error_string =
510 gpr_strdup(grpc_error_string(service_config_error));
511 if (lb_policy_name == nullptr) {
512 // Use an empty lb_policy_name as an indicator that we received an
513 // invalid service config and we don't have a fallback service config.
514 OnResolverError(service_config_error);
516 GRPC_ERROR_UNREF(service_config_error);
520 lb_policy_name = child_policy_name_.get();
521 lb_policy_config = child_lb_config_;
523 if (lb_policy_name != nullptr) {
524 // Create or update LB policy, as needed.
525 CreateOrUpdateLbPolicyLocked(lb_policy_name, lb_policy_config,
526 std::move(result), &trace_strings);
528 // Add channel trace event.
529 if (service_config_changed) {
530 // TODO(ncteisen): might be worth somehow including a snippet of the
531 // config in the trace, at the risk of bloating the trace logs.
532 trace_strings.push_back(gpr_strdup("Service config changed"));
534 if (service_config_error_string != nullptr) {
535 trace_strings.push_back(service_config_error_string);
536 service_config_error_string = nullptr;
538 MaybeAddTraceMessagesForAddressChangesLocked(resolution_contains_addresses,
540 ConcatenateAndAddChannelTraceLocked(&trace_strings);
543 } // namespace grpc_core