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 
22 #include <grpc/support/port_platform.h>
23 
24 #include "absl/container/inlined_vector.h"
25 
26 #include "src/core/ext/filters/client_channel/lb_policy.h"
27 #include "src/core/ext/filters/client_channel/lb_policy_factory.h"
28 #include "src/core/ext/filters/client_channel/resolver.h"
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/channel/channel_stack.h"
31 #include "src/core/lib/debug/trace.h"
32 #include "src/core/lib/gprpp/orphanable.h"
33 #include "src/core/lib/iomgr/call_combiner.h"
34 #include "src/core/lib/iomgr/closure.h"
35 #include "src/core/lib/iomgr/polling_entity.h"
36 #include "src/core/lib/iomgr/pollset_set.h"
37 #include "src/core/lib/transport/connectivity_state.h"
38 #include "src/core/lib/transport/metadata_batch.h"
39 
40 namespace grpc_core {
41 
42 // An LB policy that wraps a resolver and a child LB policy to make use
43 // of the addresses returned by the resolver.
44 //
45 // When used in the client_channel code, the resolver will attempt to
46 // fetch the service config, and the child LB policy name and config
47 // will be determined based on the service config.
48 //
49 // When used in an LB policy implementation that needs to do another
50 // round of resolution before creating a child policy, the resolver does
51 // not fetch the service config, and the caller must pre-determine the
52 // child LB policy and config to use.
53 class ResolvingLoadBalancingPolicy : public LoadBalancingPolicy {
54  public:
55   // Synchronous callback that takes the resolver result and sets
56   // lb_policy_config to point to the right data.
57   // Returns true if the service config has changed since the last result.
58   // If the returned no_valid_service_config is true, that means that we
59   // don't have a valid service config to use, and we should set the channel
60   // to be in TRANSIENT_FAILURE.
61   typedef bool (*ProcessResolverResultCallback)(
62       void* user_data, const Resolver::Result& result,
63       RefCountedPtr<LoadBalancingPolicy::Config>* lb_policy_config,
64       grpc_error** service_config_error, bool* no_valid_service_config);
65   // If error is set when this returns, then construction failed, and
66   // the caller may not use the new object.
67   ResolvingLoadBalancingPolicy(
68       Args args, TraceFlag* tracer, grpc_core::UniquePtr<char> target_uri,
69       ProcessResolverResultCallback process_resolver_result,
70       void* process_resolver_result_user_data);
71 
name()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.
UpdateLocked(UpdateArgs)77   void UpdateLocked(UpdateArgs /*args*/) override {}
78 
79   void ExitIdleLocked() override;
80 
81   void ResetBackoffLocked() override;
82 
83  private:
84   using TraceStringVector = absl::InlinedVector<const char*, 3>;
85 
86   class ResolverResultHandler;
87   class ResolvingControlHelper;
88 
89   ~ResolvingLoadBalancingPolicy();
90 
91   void ShutdownLocked() override;
92 
93   void OnResolverError(grpc_error* error);
94   void CreateOrUpdateLbPolicyLocked(
95       RefCountedPtr<LoadBalancingPolicy::Config> lb_policy_config,
96       Resolver::Result result);
97   OrphanablePtr<LoadBalancingPolicy> CreateLbPolicyLocked(
98       const grpc_channel_args& args);
99   void MaybeAddTraceMessagesForAddressChangesLocked(
100       bool resolution_contains_addresses, TraceStringVector* trace_strings);
101   void ConcatenateAndAddChannelTraceLocked(
102       const TraceStringVector& trace_strings) const;
103   void OnResolverResultChangedLocked(Resolver::Result result);
104 
105   // Passed in from caller at construction time.
106   TraceFlag* tracer_;
107   grpc_core::UniquePtr<char> target_uri_;
108   ProcessResolverResultCallback process_resolver_result_ = nullptr;
109   void* process_resolver_result_user_data_ = nullptr;
110   grpc_core::UniquePtr<char> child_policy_name_;
111   RefCountedPtr<LoadBalancingPolicy::Config> child_lb_config_;
112 
113   // Resolver and associated state.
114   OrphanablePtr<Resolver> resolver_;
115   bool previous_resolution_contained_addresses_ = false;
116 
117   // Child LB policy.
118   OrphanablePtr<LoadBalancingPolicy> lb_policy_;
119 };
120 
121 }  // namespace grpc_core
122 
123 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H */
124