Debian Codegraph @

test/cpp/end2end/xds/xds_server.cc

raw
1 //
2 // Copyright 2017 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 #include "test/cpp/end2end/xds/xds_server.h"
18
19 #include <deque>
20 #include <set>
21 #include <string>
22 #include <thread>
23 #include <vector>
24
25 #include <gmock/gmock.h>
26 #include <gtest/gtest.h>
27
28 #include "absl/types/optional.h"
29
30 #include <grpc/support/log.h>
31
32 #include "src/core/lib/address_utils/parse_address.h"
33 #include "src/core/lib/gprpp/sync.h"
34 #include "src/proto/grpc/testing/xds/v3/ads.grpc.pb.h"
35 #include "src/proto/grpc/testing/xds/v3/discovery.grpc.pb.h"
36 #include "src/proto/grpc/testing/xds/v3/lrs.grpc.pb.h"
37
38 namespace grpc {
39 namespace testing {
40
41 //
42 // AdsServiceImpl
43 //
44
45 void AdsServiceImpl::SetResource(google::protobuf::Any resource,
46 const std::string& type_url,
47 const std::string& name) {
48 grpc_core::MutexLock lock(&ads_mu_);
49 ResourceTypeState& resource_type_state = resource_map_[type_url];
50 ++resource_type_state.resource_type_version;
51 ResourceState& resource_state = resource_type_state.resource_name_map[name];
52 resource_state.resource_type_version =
53 resource_type_state.resource_type_version;
54 resource_state.resource = std::move(resource);
55 gpr_log(GPR_INFO,
56 "ADS[%p]: Updating %s resource %s; resource_type_version now %u",
57 this, type_url.c_str(), name.c_str(),
58 resource_type_state.resource_type_version);
59 for (SubscriptionState* subscription : resource_state.subscriptions) {
60 subscription->update_queue->emplace_back(type_url, name);
61 }
62 }
63
64 void AdsServiceImpl::UnsetResource(const std::string& type_url,
65 const std::string& name) {
66 grpc_core::MutexLock lock(&ads_mu_);
67 ResourceTypeState& resource_type_state = resource_map_[type_url];
68 ++resource_type_state.resource_type_version;
69 ResourceState& resource_state = resource_type_state.resource_name_map[name];
70 resource_state.resource_type_version =
71 resource_type_state.resource_type_version;
72 resource_state.resource.reset();
73 gpr_log(GPR_INFO,
74 "ADS[%p]: Unsetting %s resource %s; resource_type_version now %u",
75 this, type_url.c_str(), name.c_str(),
76 resource_type_state.resource_type_version);
77 for (SubscriptionState* subscription : resource_state.subscriptions) {
78 subscription->update_queue->emplace_back(type_url, name);
79 }
80 }
81
82 // Checks whether the client needs to receive a newer version of
83 // the resource.
84 bool AdsServiceImpl::ClientNeedsResourceUpdate(
85 const ResourceTypeState& resource_type_state,
86 const ResourceState& resource_state, int client_resource_type_version) {
87 return client_resource_type_version <
88 resource_type_state.resource_type_version &&
89 resource_state.resource_type_version <=
90 resource_type_state.resource_type_version;
91 }
92
93 // Subscribes to a resource if not already subscribed:
94 // 1. Sets the update_queue field in subscription_state.
95 // 2. Adds subscription_state to resource_state->subscriptions.
96 bool AdsServiceImpl::MaybeSubscribe(const std::string& resource_type,
97 const std::string& resource_name,
98 SubscriptionState* subscription_state,
99 ResourceState* resource_state,
100 UpdateQueue* update_queue) {
101 // The update_queue will be null if we were not previously subscribed.
102 if (subscription_state->update_queue != nullptr) return false;
103 subscription_state->update_queue = update_queue;
104 resource_state->subscriptions.emplace(subscription_state);
105 gpr_log(GPR_INFO, "ADS[%p]: subscribe to resource type %s name %s state %p",
106 this, resource_type.c_str(), resource_name.c_str(),
107 &subscription_state);
108 return true;
109 }
110
111 // Removes subscriptions for resources no longer present in the
112 // current request.
113 void AdsServiceImpl::ProcessUnsubscriptions(
114 const std::string& resource_type,
115 const std::set<std::string>& resources_in_current_request,
116 SubscriptionNameMap* subscription_name_map,
117 ResourceNameMap* resource_name_map) {
118 for (auto it = subscription_name_map->begin();
119 it != subscription_name_map->end();) {
120 const std::string& resource_name = it->first;
121 SubscriptionState& subscription_state = it->second;
122 if (resources_in_current_request.find(resource_name) !=
123 resources_in_current_request.end()) {
124 ++it;
125 continue;
126 }
127 gpr_log(GPR_INFO, "ADS[%p]: Unsubscribe to type=%s name=%s state=%p", this,
128 resource_type.c_str(), resource_name.c_str(), &subscription_state);
129 auto resource_it = resource_name_map->find(resource_name);
130 GPR_ASSERT(resource_it != resource_name_map->end());
131 auto& resource_state = resource_it->second;
132 resource_state.subscriptions.erase(&subscription_state);
133 if (resource_state.subscriptions.empty() &&
134 !resource_state.resource.has_value()) {
135 resource_name_map->erase(resource_it);
136 }
137 it = subscription_name_map->erase(it);
138 }
139 }
140
141 void AdsServiceImpl::Start() {
142 grpc_core::MutexLock lock(&ads_mu_);
143 ads_done_ = false;
144 }
145
146 void AdsServiceImpl::Shutdown() {
147 {
148 grpc_core::MutexLock lock(&ads_mu_);
149 if (!ads_done_) {
150 ads_done_ = true;
151 ads_cond_.SignalAll();
152 }
153 resource_type_response_state_.clear();
154 }
155 gpr_log(GPR_INFO, "ADS[%p]: shut down", this);
156 }
157
158 //
159 // LrsServiceImpl::ClientStats
160 //
161
162 uint64_t LrsServiceImpl::ClientStats::total_successful_requests() const {
163 uint64_t sum = 0;
164 for (auto& p : locality_stats_) {
165 sum += p.second.total_successful_requests;
166 }
167 return sum;
168 }
169
170 uint64_t LrsServiceImpl::ClientStats::total_requests_in_progress() const {
171 uint64_t sum = 0;
172 for (auto& p : locality_stats_) {
173 sum += p.second.total_requests_in_progress;
174 }
175 return sum;
176 }
177
178 uint64_t LrsServiceImpl::ClientStats::total_error_requests() const {
179 uint64_t sum = 0;
180 for (auto& p : locality_stats_) {
181 sum += p.second.total_error_requests;
182 }
183 return sum;
184 }
185
186 uint64_t LrsServiceImpl::ClientStats::total_issued_requests() const {
187 uint64_t sum = 0;
188 for (auto& p : locality_stats_) {
189 sum += p.second.total_issued_requests;
190 }
191 return sum;
192 }
193
194 uint64_t LrsServiceImpl::ClientStats::dropped_requests(
195 const std::string& category) const {
196 auto iter = dropped_requests_.find(category);
197 GPR_ASSERT(iter != dropped_requests_.end());
198 return iter->second;
199 }
200
201 LrsServiceImpl::ClientStats& LrsServiceImpl::ClientStats::operator+=(
202 const ClientStats& other) {
203 for (const auto& p : other.locality_stats_) {
204 locality_stats_[p.first] += p.second;
205 }
206 total_dropped_requests_ += other.total_dropped_requests_;
207 for (const auto& p : other.dropped_requests_) {
208 dropped_requests_[p.first] += p.second;
209 }
210 return *this;
211 }
212
213 //
214 // LrsServiceImpl
215 //
216
217 void LrsServiceImpl::Start() {
218 {
219 grpc_core::MutexLock lock(&lrs_mu_);
220 lrs_done_ = false;
221 }
222 {
223 grpc_core::MutexLock lock(&load_report_mu_);
224 result_queue_.clear();
225 }
226 }
227
228 void LrsServiceImpl::Shutdown() {
229 {
230 grpc_core::MutexLock lock(&lrs_mu_);
231 if (!lrs_done_) {
232 lrs_done_ = true;
233 lrs_cv_.SignalAll();
234 }
235 }
236 gpr_log(GPR_INFO, "LRS[%p]: shut down", this);
237 }
238
239 std::vector<LrsServiceImpl::ClientStats> LrsServiceImpl::WaitForLoadReport() {
240 grpc_core::MutexLock lock(&load_report_mu_);
241 grpc_core::CondVar cv;
242 if (result_queue_.empty()) {
243 load_report_cond_ = &cv;
244 while (result_queue_.empty()) {
245 cv.Wait(&load_report_mu_);
246 }
247 load_report_cond_ = nullptr;
248 }
249 std::vector<ClientStats> result = std::move(result_queue_.front());
250 result_queue_.pop_front();
251 return result;
252 }
253
254 } // namespace testing
255 } // namespace grpc