3 * Copyright 2018 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 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H
22 #include <grpc/support/port_platform.h>
24 #include "src/core/ext/filters/client_channel/lb_policy.h"
25 #include "src/core/ext/filters/client_channel/lb_policy_factory.h"
26 #include "src/core/ext/filters/client_channel/resolver.h"
27 #include "src/core/lib/channel/channel_args.h"
28 #include "src/core/lib/channel/channel_stack.h"
29 #include "src/core/lib/debug/trace.h"
30 #include "src/core/lib/gprpp/inlined_vector.h"
31 #include "src/core/lib/gprpp/orphanable.h"
32 #include "src/core/lib/iomgr/call_combiner.h"
33 #include "src/core/lib/iomgr/closure.h"
34 #include "src/core/lib/iomgr/polling_entity.h"
35 #include "src/core/lib/iomgr/pollset_set.h"
36 #include "src/core/lib/transport/connectivity_state.h"
37 #include "src/core/lib/transport/metadata_batch.h"
41 // An LB policy that wraps a resolver and a child LB policy to make use
42 // of the addresses returned by the resolver.
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.
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.
52 class ResolvingLoadBalancingPolicy : public LoadBalancingPolicy {
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.
60 typedef bool (*ProcessResolverResultCallback)(
61 void* user_data, const Resolver::Result& result,
62 const char** lb_policy_name,
63 RefCountedPtr<LoadBalancingPolicy::Config>* lb_policy_config,
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.
67 ResolvingLoadBalancingPolicy(
68 Args args, TraceFlag* tracer, UniquePtr<char> target_uri,
69 ProcessResolverResultCallback process_resolver_result,
70 void* process_resolver_result_user_data);
72 virtual const char* name() const override { return "resolving_lb"; }
74 // No-op -- should never get updates from the channel.
75 // TODO(roth): Need to support updating child LB policy's config for xds
77 void UpdateLocked(UpdateArgs args) override {}
79 void ExitIdleLocked() override;
81 void ResetBackoffLocked() override;
84 using TraceStringVector = InlinedVector<char*, 3>;
86 class ResolverResultHandler;
87 class ResolvingControlHelper;
89 ~ResolvingLoadBalancingPolicy();
91 void ShutdownLocked() override;
93 void OnResolverError(grpc_error* error);
94 void CreateOrUpdateLbPolicyLocked(
95 const char* lb_policy_name,
96 RefCountedPtr<LoadBalancingPolicy::Config> lb_policy_config,
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);
107 // Passed in from caller at construction time.
109 UniquePtr<char> target_uri_;
110 ProcessResolverResultCallback process_resolver_result_ = nullptr;
111 void* process_resolver_result_user_data_ = nullptr;
112 UniquePtr<char> child_policy_name_;
113 RefCountedPtr<LoadBalancingPolicy::Config> child_lb_config_;
115 // Resolver and associated state.
116 OrphanablePtr<Resolver> resolver_;
117 bool previous_resolution_contained_addresses_ = false;
120 OrphanablePtr<LoadBalancingPolicy> lb_policy_;
121 OrphanablePtr<LoadBalancingPolicy> pending_lb_policy_;
124 } // namespace grpc_core
126 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_RESOLVING_LB_POLICY_H */