GRPC Core  9.0.0
resolving_lb_policy.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H
21 
23 
38 
39 namespace grpc_core {
40 
41 // An LB policy that wraps a resolver and a child LB policy to make use
42 // of the addresses returned by the resolver.
43 //
44 // When used in the client_channel code, the resolver will attempt to
45 // fetch the service config, and the child LB policy name and config
46 // will be determined based on the service config.
47 //
48 // When used in an LB policy implementation that needs to do another
49 // round of resolution before creating a child policy, the resolver does
50 // not fetch the service config, and the caller must pre-determine the
51 // child LB policy and config to use.
53  public:
54  // Synchronous callback that takes the resolver result and sets
55  // lb_policy_name and lb_policy_config to point to the right data.
56  // Returns true if the service config has changed since the last result.
57  // If the returned service_config_error is not none and lb_policy_name is
58  // empty, it means that we don't have a valid service config to use, and we
59  // should set the channel to be in TRANSIENT_FAILURE.
61  void* user_data, const Resolver::Result& result,
62  const char** lb_policy_name,
64  grpc_error** service_config_error);
65  // If error is set when this returns, then construction failed, and
66  // the caller may not use the new object.
68  Args args, TraceFlag* tracer, grpc_core::UniquePtr<char> target_uri,
69  ProcessResolverResultCallback process_resolver_result,
70  void* process_resolver_result_user_data);
71 
72  virtual const char* name() const override { return "resolving_lb"; }
73 
74  // No-op -- should never get updates from the channel.
75  // TODO(roth): Need to support updating child LB policy's config for xds
76  // use case.
77  void UpdateLocked(UpdateArgs /*args*/) override {}
78 
79  void ExitIdleLocked() override;
80 
81  void ResetBackoffLocked() override;
82 
83  private:
84  using TraceStringVector = InlinedVector<char*, 3>;
85 
86  class ResolverResultHandler;
87  class ResolvingControlHelper;
88 
90 
91  void ShutdownLocked() override;
92 
93  void OnResolverError(grpc_error* error);
94  void CreateOrUpdateLbPolicyLocked(
95  const char* lb_policy_name,
97  Resolver::Result result, TraceStringVector* trace_strings);
98  OrphanablePtr<LoadBalancingPolicy> CreateLbPolicyLocked(
99  const char* lb_policy_name, const grpc_channel_args& args,
100  TraceStringVector* trace_strings);
101  void MaybeAddTraceMessagesForAddressChangesLocked(
102  bool resolution_contains_addresses, TraceStringVector* trace_strings);
103  void ConcatenateAndAddChannelTraceLocked(
104  TraceStringVector* trace_strings) const;
105  void OnResolverResultChangedLocked(Resolver::Result result);
106 
107  // Passed in from caller at construction time.
108  TraceFlag* tracer_;
109  grpc_core::UniquePtr<char> target_uri_;
110  ProcessResolverResultCallback process_resolver_result_ = nullptr;
111  void* process_resolver_result_user_data_ = nullptr;
112  grpc_core::UniquePtr<char> child_policy_name_;
114 
115  // Resolver and associated state.
116  OrphanablePtr<Resolver> resolver_;
117  bool previous_resolution_contained_addresses_ = false;
118 
119  // Child LB policy.
121  OrphanablePtr<LoadBalancingPolicy> pending_lb_policy_;
122 };
123 
124 } // namespace grpc_core
125 
126 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H */
Definition: inlined_vector.h:60
Interface for load balancing policies.
Definition: lb_policy.h:81
Definition: ref_counted_ptr.h:35
Definition: resolving_lb_policy.h:52
void UpdateLocked(UpdateArgs) override
Updates the policy with new data from the resolver.
Definition: resolving_lb_policy.h:77
ResolvingLoadBalancingPolicy(Args args, TraceFlag *tracer, grpc_core::UniquePtr< char > target_uri, ProcessResolverResultCallback process_resolver_result, void *process_resolver_result_user_data)
Definition: resolving_lb_policy.cc:179
void ResetBackoffLocked() override
Resets connection backoff.
Definition: resolving_lb_policy.cc:239
virtual const char * name() const override
Returns the name of the LB policy.
Definition: resolving_lb_policy.h:72
void ExitIdleLocked() override
Tries to enter a READY connectivity state.
Definition: resolving_lb_policy.cc:232
bool(* ProcessResolverResultCallback)(void *user_data, const Resolver::Result &result, const char **lb_policy_name, RefCountedPtr< LoadBalancingPolicy::Config > *lb_policy_config, grpc_error **service_config_error)
Definition: resolving_lb_policy.h:60
Definition: trace.h:61
Round Robin Policy.
Definition: backend_metric.cc:24
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
std::unique_ptr< T, Deleter > OrphanablePtr
Definition: orphanable.h:68
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Args used to instantiate an LB policy.
Definition: lb_policy.h:311
Data passed to the UpdateLocked() method when new addresses and config are available.
Definition: lb_policy.h:295
Results returned by the resolver.
Definition: resolver.h:52
Definition: error_internal.h:39