GRPC Core  9.0.0
xds_client.h
Go to the documentation of this file.
1 //
2 // Copyright 2019 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_CLIENT_H
18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_CLIENT_H
19 
21 
22 #include <set>
23 
28 #include "src/core/lib/gprpp/map.h"
35 
36 namespace grpc_core {
37 
38 extern TraceFlag xds_client_trace;
39 
40 class XdsClient : public InternallyRefCounted<XdsClient> {
41  public:
42  // Service config watcher interface. Implemented by callers.
44  public:
45  virtual ~ServiceConfigWatcherInterface() = default;
46 
47  virtual void OnServiceConfigChanged(
48  RefCountedPtr<ServiceConfig> service_config) = 0;
49 
50  virtual void OnError(grpc_error* error) = 0;
51  };
52 
53  // Cluster data watcher interface. Implemented by callers.
55  public:
56  virtual ~ClusterWatcherInterface() = default;
57 
58  virtual void OnClusterChanged(CdsUpdate cluster_data) = 0;
59 
60  virtual void OnError(grpc_error* error) = 0;
61  };
62 
63  // Endpoint data watcher interface. Implemented by callers.
65  public:
66  virtual ~EndpointWatcherInterface() = default;
67 
68  virtual void OnEndpointChanged(EdsUpdate update) = 0;
69 
70  virtual void OnError(grpc_error* error) = 0;
71  };
72 
73  // If *error is not GRPC_ERROR_NONE after construction, then there was
74  // an error initializing the client.
75  XdsClient(Combiner* combiner, grpc_pollset_set* interested_parties,
76  StringView server_name,
77  std::unique_ptr<ServiceConfigWatcherInterface> watcher,
78  const grpc_channel_args& channel_args, grpc_error** error);
79  ~XdsClient();
80 
81  void Orphan() override;
82 
83  // Start and cancel cluster data watch for a cluster.
84  // The XdsClient takes ownership of the watcher, but the caller may
85  // keep a raw pointer to the watcher, which may be used only for
86  // cancellation. (Because the caller does not own the watcher, the
87  // pointer must not be used for any other purpose.)
88  void WatchClusterData(StringView cluster,
89  std::unique_ptr<ClusterWatcherInterface> watcher);
91  ClusterWatcherInterface* watcher);
92 
93  // Start and cancel endpoint data watch for a cluster.
94  // The XdsClient takes ownership of the watcher, but the caller may
95  // keep a raw pointer to the watcher, which may be used only for
96  // cancellation. (Because the caller does not own the watcher, the
97  // pointer must not be used for any other purpose.)
98  void WatchEndpointData(StringView cluster,
99  std::unique_ptr<EndpointWatcherInterface> watcher);
100  void CancelEndpointDataWatch(StringView cluster,
101  EndpointWatcherInterface* watcher);
102 
103  // Adds and removes client stats for cluster.
104  void AddClientStats(StringView lrs_server, StringView cluster,
105  XdsClientStats* client_stats);
106  void RemoveClientStats(StringView lrs_server, StringView cluster,
107  XdsClientStats* client_stats);
108 
109  // Resets connection backoff state.
110  void ResetBackoff();
111 
112  // Helpers for encoding the XdsClient object in channel args.
113  grpc_arg MakeChannelArg() const;
115  const grpc_channel_args& args);
116 
117  private:
118  // Contains a channel to the xds server and all the data related to the
119  // channel. Holds a ref to the xds client object.
120  // TODO(roth): This is separate from the XdsClient object because it was
121  // originally designed to be able to swap itself out in case the
122  // balancer name changed. Now that the balancer name is going to be
123  // coming from the bootstrap file, we don't really need this level of
124  // indirection unless we decide to support watching the bootstrap file
125  // for changes. At some point, if we decide that we're never going to
126  // need to do that, then we can eliminate this class and move its
127  // contents directly into the XdsClient class.
128  class ChannelState : public InternallyRefCounted<ChannelState> {
129  public:
130  template <typename T>
131  class RetryableCall;
132 
133  class AdsCallState;
134  class LrsCallState;
135 
136  ChannelState(RefCountedPtr<XdsClient> xds_client,
137  const grpc_channel_args& args);
138  ~ChannelState();
139 
140  void Orphan() override;
141 
142  grpc_channel* channel() const { return channel_; }
143  XdsClient* xds_client() const { return xds_client_.get(); }
144  AdsCallState* ads_calld() const;
145  LrsCallState* lrs_calld() const;
146 
147  void MaybeStartAdsCall();
148  void StopAdsCall();
149 
150  void MaybeStartLrsCall();
151  void StopLrsCall();
152 
153  bool HasActiveAdsCall() const;
154 
155  void StartConnectivityWatchLocked();
156  void CancelConnectivityWatchLocked();
157 
158  private:
159  class StateWatcher;
160 
161  // The owning xds client.
162  RefCountedPtr<XdsClient> xds_client_;
163 
164  // The channel and its status.
165  grpc_channel* channel_;
166  bool shutting_down_ = false;
167  StateWatcher* watcher_ = nullptr;
168 
169  // The retryable XDS calls.
170  OrphanablePtr<RetryableCall<AdsCallState>> ads_calld_;
171  OrphanablePtr<RetryableCall<LrsCallState>> lrs_calld_;
172  };
173 
174  struct ClusterState {
175  std::map<ClusterWatcherInterface*, std::unique_ptr<ClusterWatcherInterface>>
176  cluster_watchers;
177  std::map<EndpointWatcherInterface*,
178  std::unique_ptr<EndpointWatcherInterface>>
179  endpoint_watchers;
180  std::set<XdsClientStats*> client_stats;
181  // The latest data seen from EDS.
182  EdsUpdate eds_update;
183  };
184 
185  // Sends an error notification to all watchers.
186  void NotifyOnError(grpc_error* error);
187 
188  // TODO(juanlishen): Once we implement LDS support, this can be a
189  // normal method instead of a closure callback.
190  static void NotifyOnServiceConfig(void* arg, grpc_error* error);
191 
192  // Channel arg vtable functions.
193  static void* ChannelArgCopy(void* p);
194  static void ChannelArgDestroy(void* p);
195  static int ChannelArgCmp(void* p, void* q);
196 
197  static const grpc_arg_pointer_vtable kXdsClientVtable;
198 
199  grpc_core::UniquePtr<char> build_version_;
200 
201  Combiner* combiner_;
202  grpc_pollset_set* interested_parties_;
203 
204  std::unique_ptr<XdsBootstrap> bootstrap_;
205 
206  grpc_core::UniquePtr<char> server_name_;
207  std::unique_ptr<ServiceConfigWatcherInterface> service_config_watcher_;
208  // TODO(juanlishen): Once we implement LDS support, this will no
209  // longer be needed.
210  grpc_closure service_config_notify_;
211 
212  // The channel for communicating with the xds server.
213  OrphanablePtr<ChannelState> chand_;
214 
215  // TODO(juanlishen): As part of adding CDS support, replace
216  // cluster_state_ with a map keyed by cluster name, so that we can
217  // support multiple clusters for both CDS and EDS.
218  ClusterState cluster_state_;
219  // Map<StringView /*cluster*/, ClusterState, StringLess> clusters_;
220 
221  bool shutting_down_ = false;
222 };
223 
224 } // namespace grpc_core
225 
226 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_CLIENT_H */
Definition: combiner.h:33
Definition: orphanable.h:77
Definition: ref_counted_ptr.h:35
Definition: string_view.h:69
virtual void OnClusterChanged(CdsUpdate cluster_data)=0
virtual void OnError(grpc_error *error)=0
virtual void OnEndpointChanged(EdsUpdate update)=0
virtual void OnError(grpc_error *error)=0
virtual void OnServiceConfigChanged(RefCountedPtr< ServiceConfig > service_config)=0
virtual void OnError(grpc_error *error)=0
Definition: xds_client.h:40
void Orphan() override
Definition: xds_client.cc:1286
void WatchClusterData(StringView cluster, std::unique_ptr< ClusterWatcherInterface > watcher)
Definition: xds_client.cc:1292
void CancelClusterDataWatch(StringView cluster, ClusterWatcherInterface *watcher)
Definition: xds_client.cc:1304
XdsClient(Combiner *combiner, grpc_pollset_set *interested_parties, StringView server_name, std::unique_ptr< ServiceConfigWatcherInterface > watcher, const grpc_channel_args &channel_args, grpc_error **error)
Definition: xds_client.cc:1252
static RefCountedPtr< XdsClient > GetFromChannelArgs(const grpc_channel_args &args)
Definition: xds_client.cc:1432
void WatchEndpointData(StringView cluster, std::unique_ptr< EndpointWatcherInterface > watcher)
Definition: xds_client.cc:1315
void AddClientStats(StringView lrs_server, StringView cluster, XdsClientStats *client_stats)
Definition: xds_client.cc:1338
void CancelEndpointDataWatch(StringView cluster, EndpointWatcherInterface *watcher)
Definition: xds_client.cc:1327
grpc_arg MakeChannelArg() const
Definition: xds_client.cc:1426
void RemoveClientStats(StringView lrs_server, StringView cluster, XdsClientStats *client_stats)
Definition: xds_client.cc:1347
void ResetBackoff()
Definition: xds_client.cc:1364
~XdsClient()
Definition: xds_client.cc:1284
Definition: xds_client_stats.h:93
Round Robin Policy.
Definition: backend_metric.cc:24
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
TraceFlag xds_client_trace
struct grpc_pollset_set grpc_pollset_set
Definition: pollset_set.h:31
Definition: grpc_types.h:85
A single argument...
Definition: grpc_types.h:103
An array of arguments that can be passed around.
Definition: grpc_types.h:132
Definition: channel.h:66
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: xds_api.h:140
Definition: xds_api.h:134
Definition: error_internal.h:39