GRPC Core  9.0.0
xds_api.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_XDS_XDS_API_H
20 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_API_H
21 
23 
24 #include <stdint.h>
25 
26 #include <grpc/slice_buffer.h>
27 
31 
32 namespace grpc_core {
33 
35  public:
36  struct LocalityMap {
37  struct Locality {
38  bool operator==(const Locality& other) const {
39  return *name == *other.name && serverlist == other.serverlist &&
40  lb_weight == other.lb_weight && priority == other.priority;
41  }
42 
43  // This comparator only compares the locality names.
44  struct Less {
45  bool operator()(const Locality& lhs, const Locality& rhs) const {
46  return XdsLocalityName::Less()(lhs.name, rhs.name);
47  }
48  };
49 
52  uint32_t lb_weight;
53  uint32_t priority;
54  };
55 
56  bool Contains(const RefCountedPtr<XdsLocalityName>& name) const {
57  return localities.find(name) != localities.end();
58  }
59 
60  size_t size() const { return localities.size(); }
61 
62  std::map<RefCountedPtr<XdsLocalityName>, Locality, XdsLocalityName::Less>
64  };
65 
66  bool operator==(const XdsPriorityListUpdate& other) const;
67  bool operator!=(const XdsPriorityListUpdate& other) const {
68  return !(*this == other);
69  }
70 
71  void Add(LocalityMap::Locality locality);
72 
73  const LocalityMap* Find(uint32_t priority) const;
74 
75  bool Contains(uint32_t priority) const {
76  return priority < priorities_.size();
77  }
78  bool Contains(const RefCountedPtr<XdsLocalityName>& name);
79 
80  bool empty() const { return priorities_.empty(); }
81  size_t size() const { return priorities_.size(); }
82 
83  // Callers should make sure the priority list is non-empty.
84  uint32_t LowestPriority() const {
85  return static_cast<uint32_t>(priorities_.size()) - 1;
86  }
87 
88  private:
90 };
91 
92 // There are two phases of accessing this class's content:
93 // 1. to initialize in the control plane combiner;
94 // 2. to use in the data plane combiner.
95 // So no additional synchronization is needed.
96 class XdsDropConfig : public RefCounted<XdsDropConfig> {
97  public:
98  struct DropCategory {
99  bool operator==(const DropCategory& other) const {
100  return strcmp(name.get(), other.name.get()) == 0 &&
102  }
103 
105  const uint32_t parts_per_million;
106  };
107 
109 
111  uint32_t parts_per_million) {
112  drop_category_list_.emplace_back(
113  DropCategory{std::move(name), parts_per_million});
114  }
115 
116  // The only method invoked from the data plane combiner.
117  bool ShouldDrop(const grpc_core::UniquePtr<char>** category_name) const;
118 
120  return drop_category_list_;
121  }
122 
123  bool operator==(const XdsDropConfig& other) const {
124  return drop_category_list_ == other.drop_category_list_;
125  }
126  bool operator!=(const XdsDropConfig& other) const {
127  return !(*this == other);
128  }
129 
130  private:
131  DropCategoryList drop_category_list_;
132 };
133 
134 struct EdsUpdate {
137  bool drop_all = false;
138 };
139 
140 struct CdsUpdate {
141  // The name to use in the EDS request.
142  // If null, the cluster name will be used.
144  // The LRS server to use for load reporting.
145  // If null, load reporting will be disabled.
146  // If set to the empty string, will use the same server we obtained
147  // the CDS data from.
149 };
150 
151 // Creates an EDS request querying \a service_name.
152 grpc_slice XdsEdsRequestCreateAndEncode(const char* server_name,
153  const XdsBootstrap::Node* node,
154  const char* build_version);
155 
156 // Parses the EDS response and returns the args to update locality map. If there
157 // is any error, the output update is invalid.
158 grpc_error* XdsEdsResponseDecodeAndParse(const grpc_slice& encoded_response,
159  EdsUpdate* update);
160 
161 // Creates an LRS request querying \a server_name.
162 grpc_slice XdsLrsRequestCreateAndEncode(const char* server_name,
163  const XdsBootstrap::Node* node,
164  const char* build_version);
165 
166 // Creates an LRS request sending client-side load reports. If all the counters
167 // in \a client_stats are zero, returns empty slice.
168 grpc_slice XdsLrsRequestCreateAndEncode(const char* server_name,
169  XdsClientStats* client_stats);
170 
171 // Parses the LRS response and returns \a cluster_name and \a
172 // load_reporting_interval for client-side load reporting. If there is any
173 // error, the output config is invalid.
175  const grpc_slice& encoded_response,
176  grpc_core::UniquePtr<char>* cluster_name,
177  grpc_millis* load_reporting_interval);
178 
179 } // namespace grpc_core
180 
181 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_XDS_XDS_API_H */
void emplace_back(Args &&... args)
Definition: inlined_vector.h:145
Definition: ref_counted.h:248
Definition: ref_counted_ptr.h:35
Definition: xds_client_stats.h:93
Definition: xds_api.h:96
bool ShouldDrop(const grpc_core::UniquePtr< char > **category_name) const
Definition: xds_api.cc:91
void AddCategory(grpc_core::UniquePtr< char > name, uint32_t parts_per_million)
Definition: xds_api.h:110
bool operator!=(const XdsDropConfig &other) const
Definition: xds_api.h:126
InlinedVector< DropCategory, 2 > DropCategoryList
Definition: xds_api.h:108
const DropCategoryList & drop_category_list() const
Definition: xds_api.h:119
bool operator==(const XdsDropConfig &other) const
Definition: xds_api.h:123
Definition: xds_api.h:34
bool Contains(uint32_t priority) const
Definition: xds_api.h:75
uint32_t LowestPriority() const
Definition: xds_api.h:84
bool operator==(const XdsPriorityListUpdate &other) const
Definition: xds_api.cc:56
const LocalityMap * Find(uint32_t priority) const
Definition: xds_api.cc:76
bool operator!=(const XdsPriorityListUpdate &other) const
Definition: xds_api.h:67
size_t size() const
Definition: xds_api.h:81
void Add(LocalityMap::Locality locality)
Definition: xds_api.cc:67
bool empty() const
Definition: xds_api.h:80
int64_t grpc_millis
Definition: exec_ctx.h:35
Round Robin Policy.
Definition: backend_metric.cc:24
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: memory.h:45
grpc_slice XdsEdsRequestCreateAndEncode(const char *server_name, const XdsBootstrap::Node *node, const char *build_version)
Definition: xds_api.cc:202
grpc_error * XdsEdsResponseDecodeAndParse(const grpc_slice &encoded_response, EdsUpdate *update)
Definition: xds_api.cc:347
grpc_slice XdsLrsRequestCreateAndEncode(const char *server_name, const XdsBootstrap::Node *node, const char *build_version)
Definition: xds_api.cc:428
grpc_error * XdsLrsResponseDecodeAndParse(const grpc_slice &encoded_response, grpc_core::UniquePtr< char > *cluster_name, grpc_millis *load_reporting_interval)
Definition: xds_api.cc:545
Definition: xds_api.h:140
grpc_core::UniquePtr< char > lrs_load_reporting_server_name
Definition: xds_api.h:148
grpc_core::UniquePtr< char > eds_service_name
Definition: xds_api.h:143
Definition: xds_api.h:134
bool drop_all
Definition: xds_api.h:137
XdsPriorityListUpdate priority_list_update
Definition: xds_api.h:135
RefCountedPtr< XdsDropConfig > drop_config
Definition: xds_api.h:136
Definition: xds_bootstrap.h:47
bool operator==(const DropCategory &other) const
Definition: xds_api.h:99
grpc_core::UniquePtr< char > name
Definition: xds_api.h:104
const uint32_t parts_per_million
Definition: xds_api.h:105
Definition: xds_client_stats.h:38
bool operator()(const Locality &lhs, const Locality &rhs) const
Definition: xds_api.h:45
bool operator==(const Locality &other) const
Definition: xds_api.h:38
RefCountedPtr< XdsLocalityName > name
Definition: xds_api.h:50
ServerAddressList serverlist
Definition: xds_api.h:51
size_t size() const
Definition: xds_api.h:60
std::map< RefCountedPtr< XdsLocalityName >, Locality, XdsLocalityName::Less > localities
Definition: xds_api.h:63
bool Contains(const RefCountedPtr< XdsLocalityName > &name) const
Definition: xds_api.h:56
Definition: error_internal.h:39
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60