| 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 |
#ifndef GRPC_TEST_CPP_END2END_XDS_XDS_SERVER_H |
| 18 |
#define GRPC_TEST_CPP_END2END_XDS_XDS_SERVER_H |
| 19 |
|
| 20 |
#include <deque> |
| 21 |
#include <set> |
| 22 |
#include <string> |
| 23 |
#include <thread> |
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include <gmock/gmock.h> |
| 27 |
#include <gtest/gtest.h> |
| 28 |
|
| 29 |
#include "absl/types/optional.h" |
| 30 |
|
| 31 |
#include <grpc/support/log.h> |
| 32 |
|
| 33 |
#include "src/core/lib/address_utils/parse_address.h" |
| 34 |
#include "src/core/lib/gprpp/sync.h" |
| 35 |
#include "src/proto/grpc/testing/xds/v3/ads.grpc.pb.h" |
| 36 |
#include "src/proto/grpc/testing/xds/v3/cluster.grpc.pb.h" |
| 37 |
#include "src/proto/grpc/testing/xds/v3/discovery.grpc.pb.h" |
| 38 |
#include "src/proto/grpc/testing/xds/v3/discovery.pb.h" |
| 39 |
#include "src/proto/grpc/testing/xds/v3/endpoint.grpc.pb.h" |
| 40 |
#include "src/proto/grpc/testing/xds/v3/listener.grpc.pb.h" |
| 41 |
#include "src/proto/grpc/testing/xds/v3/lrs.grpc.pb.h" |
| 42 |
#include "src/proto/grpc/testing/xds/v3/route.grpc.pb.h" |
| 43 |
#include "test/core/util/test_config.h" |
| 44 |
#include "test/cpp/end2end/counted_service.h" |
| 45 |
|
| 46 |
namespace grpc { |
| 47 |
namespace testing { |
| 48 |
|
| 49 |
constexpr char kLdsTypeUrl[] = |
| 50 |
"type.googleapis.com/envoy.config.listener.v3.Listener"; |
| 51 |
constexpr char kRdsTypeUrl[] = |
| 52 |
"type.googleapis.com/envoy.config.route.v3.RouteConfiguration"; |
| 53 |
constexpr char kCdsTypeUrl[] = |
| 54 |
"type.googleapis.com/envoy.config.cluster.v3.Cluster"; |
| 55 |
constexpr char kEdsTypeUrl[] = |
| 56 |
"type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment"; |
| 57 |
|
| 58 |
// An ADS service implementation. |
| 59 |
class AdsServiceImpl |
| 60 |
: public CountedService< |
| 61 |
::envoy::service::discovery::v3::AggregatedDiscoveryService::Service>, |
| 62 |
public std::enable_shared_from_this<AdsServiceImpl> { |
| 63 |
public: |
| 64 |
// State for a given xDS resource type. |
| 65 |
struct ResponseState { |
| 66 |
enum State { |
| 67 |
ACKED, // ACK received. |
| 68 |
NACKED, // NACK received; error_message will contain the error. |
| 69 |
}; |
| 70 |
State state = ACKED; |
| 71 |
std::string error_message; |
| 72 |
}; |
| 73 |
|
| 74 |
AdsServiceImpl() {} |
| 75 |
|
| 76 |
void set_wrap_resources(bool wrap_resources) { |
| 77 |
grpc_core::MutexLock lock(&ads_mu_); |
| 78 |
wrap_resources_ = wrap_resources; |
| 79 |
} |
| 80 |
|
| 81 |
void set_inject_bad_resources_for_resource_type(const std::string& type_url) { |
| 82 |
grpc_core::MutexLock lock(&ads_mu_); |
| 83 |
inject_bad_resources_for_resource_type_ = type_url; |
| 84 |
} |
| 85 |
|
| 86 |
// Sets a resource to a particular value, overwriting any previous value. |
| 87 |
void SetResource(google::protobuf::Any resource, const std::string& type_url, |
| 88 |
const std::string& name); |
| 89 |
|
| 90 |
// Removes a resource from the server's state. |
| 91 |
void UnsetResource(const std::string& type_url, const std::string& name); |
| 92 |
|
| 93 |
void SetLdsResource(const ::envoy::config::listener::v3::Listener& listener) { |
| 94 |
google::protobuf::Any resource; |
| 95 |
resource.PackFrom(listener); |
| 96 |
SetResource(std::move(resource), kLdsTypeUrl, listener.name()); |
| 97 |
} |
| 98 |
|
| 99 |
void SetRdsResource( |
| 100 |
const ::envoy::config::route::v3::RouteConfiguration& route) { |
| 101 |
google::protobuf::Any resource; |
| 102 |
resource.PackFrom(route); |
| 103 |
SetResource(std::move(resource), kRdsTypeUrl, route.name()); |
| 104 |
} |
| 105 |
|
| 106 |
void SetCdsResource(const ::envoy::config::cluster::v3::Cluster& cluster) { |
| 107 |
google::protobuf::Any resource; |
| 108 |
resource.PackFrom(cluster); |
| 109 |
SetResource(std::move(resource), kCdsTypeUrl, cluster.name()); |
| 110 |
} |
| 111 |
|
| 112 |
void SetEdsResource( |
| 113 |
const ::envoy::config::endpoint::v3::ClusterLoadAssignment& assignment) { |
| 114 |
google::protobuf::Any resource; |
| 115 |
resource.PackFrom(assignment); |
| 116 |
SetResource(std::move(resource), kEdsTypeUrl, assignment.cluster_name()); |
| 117 |
} |
| 118 |
|
| 119 |
// Tells the server to ignore requests from the client for a given |
| 120 |
// resource type. |
| 121 |
void IgnoreResourceType(const std::string& type_url) { |
| 122 |
grpc_core::MutexLock lock(&ads_mu_); |
| 123 |
resource_types_to_ignore_.emplace(type_url); |
| 124 |
} |
| 125 |
|
| 126 |
// Sets the minimum version that the server will accept for a given |
| 127 |
// resource type. Will cause a gmock expectation failure if we see a |
| 128 |
// lower version. |
| 129 |
void SetResourceMinVersion(const std::string& type_url, int version) { |
| 130 |
grpc_core::MutexLock lock(&ads_mu_); |
| 131 |
resource_type_min_versions_[type_url] = version; |
| 132 |
} |
| 133 |
|
| 134 |
// Get the list of response state for each resource type. |
| 135 |
// TODO(roth): Consider adding an absl::Notification-based mechanism |
| 136 |
// here to avoid the need for tests to poll the response state. |
| 137 |
absl::optional<ResponseState> GetResponseState(const std::string& type_url) { |
| 138 |
grpc_core::MutexLock lock(&ads_mu_); |
| 139 |
if (resource_type_response_state_[type_url].empty()) { |
| 140 |
return absl::nullopt; |
| 141 |
} |
| 142 |
auto response = resource_type_response_state_[type_url].front(); |
| 143 |
resource_type_response_state_[type_url].pop_front(); |
| 144 |
return response; |
| 145 |
} |
| 146 |
absl::optional<ResponseState> lds_response_state() { |
| 147 |
return GetResponseState(kLdsTypeUrl); |
| 148 |
} |
| 149 |
absl::optional<ResponseState> rds_response_state() { |
| 150 |
return GetResponseState(kRdsTypeUrl); |
| 151 |
} |
| 152 |
absl::optional<ResponseState> cds_response_state() { |
| 153 |
return GetResponseState(kCdsTypeUrl); |
| 154 |
} |
| 155 |
absl::optional<ResponseState> eds_response_state() { |
| 156 |
return GetResponseState(kEdsTypeUrl); |
| 157 |
} |
| 158 |
|
| 159 |
// Starts the service. |
| 160 |
void Start(); |
| 161 |
|
| 162 |
// Shuts down the service. |
| 163 |
void Shutdown(); |
| 164 |
|
| 165 |
// Returns the peer names of clients currently connected to the service. |
| 166 |
std::set<std::string> clients() { |
| 167 |
grpc_core::MutexLock lock(&clients_mu_); |
| 168 |
return clients_; |
| 169 |
} |
| 170 |
|
| 171 |
void ForceADSFailure(Status status) { |
| 172 |
grpc_core::MutexLock lock(&ads_mu_); |
| 173 |
forced_ads_failure_ = std::move(status); |
| 174 |
} |
| 175 |
|
| 176 |
private: |
| 177 |
// A queue of resource type/name pairs that have changed since the client |
| 178 |
// subscribed to them. |
| 179 |
using UpdateQueue = std::deque< |
| 180 |
std::pair<std::string /* type url */, std::string /* resource name */>>; |
| 181 |
|
| 182 |
// A struct representing a client's subscription to a particular resource. |
| 183 |
struct SubscriptionState { |
| 184 |
// The queue upon which to place updates when the resource is updated. |
| 185 |
UpdateQueue* update_queue; |
| 186 |
}; |
| 187 |
|
| 188 |
// A struct representing the a client's subscription to all the resources. |
| 189 |
using SubscriptionNameMap = |
| 190 |
std::map<std::string /* resource_name */, SubscriptionState>; |
| 191 |
using SubscriptionMap = |
| 192 |
std::map<std::string /* type_url */, SubscriptionNameMap>; |
| 193 |
|
| 194 |
// Sent state for a given resource type. |
| 195 |
struct SentState { |
| 196 |
int nonce = 0; |
| 197 |
int resource_type_version = 0; |
| 198 |
}; |
| 199 |
|
| 200 |
// A struct representing the current state for an individual resource. |
| 201 |
struct ResourceState { |
| 202 |
// The resource itself, if present. |
| 203 |
absl::optional<google::protobuf::Any> resource; |
| 204 |
// The resource type version that this resource was last updated in. |
| 205 |
int resource_type_version = 0; |
| 206 |
// A list of subscriptions to this resource. |
| 207 |
std::set<SubscriptionState*> subscriptions; |
| 208 |
}; |
| 209 |
|
| 210 |
// The current state for all individual resources of a given type. |
| 211 |
using ResourceNameMap = |
| 212 |
std::map<std::string /* resource_name */, ResourceState>; |
| 213 |
|
| 214 |
struct ResourceTypeState { |
| 215 |
int resource_type_version = 0; |
| 216 |
ResourceNameMap resource_name_map; |
| 217 |
}; |
| 218 |
|
| 219 |
using ResourceMap = std::map<std::string /* type_url */, ResourceTypeState>; |
| 220 |
|
| 221 |
using DiscoveryRequest = ::envoy::service::discovery::v3::DiscoveryRequest; |
| 222 |
using DiscoveryResponse = ::envoy::service::discovery::v3::DiscoveryResponse; |
| 223 |
using Stream = ServerReaderWriter<DiscoveryResponse, DiscoveryRequest>; |
| 224 |
|
| 225 |
Status StreamAggregatedResources(ServerContext* context, |
| 226 |
Stream* stream) override { |
| 227 |
gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources starts", this); |
| 228 |
{ |
| 229 |
grpc_core::MutexLock lock(&ads_mu_); |
| 230 |
if (forced_ads_failure_.has_value()) { |
| 231 |
gpr_log(GPR_INFO, |
| 232 |
"ADS[%p]: StreamAggregatedResources forcing early failure " |
| 233 |
"with status code: %d, message: %s", |
| 234 |
this, forced_ads_failure_.value().error_code(), |
| 235 |
forced_ads_failure_.value().error_message().c_str()); |
| 236 |
return forced_ads_failure_.value(); |
| 237 |
} |
| 238 |
} |
| 239 |
AddClient(context->peer()); |
| 240 |
// Take a reference of the AdsServiceImpl object, which will go |
| 241 |
// out of scope when this request handler returns. This ensures |
| 242 |
// that the parent won't be destroyed until this stream is complete. |
| 243 |
std::shared_ptr<AdsServiceImpl> ads_service_impl = shared_from_this(); |
| 244 |
// Resources (type/name pairs) that have changed since the client |
| 245 |
// subscribed to them. |
| 246 |
UpdateQueue update_queue; |
| 247 |
// Resources that the client will be subscribed to keyed by resource type |
| 248 |
// url. |
| 249 |
SubscriptionMap subscription_map; |
| 250 |
// Sent state for each resource type. |
| 251 |
std::map<std::string /*type_url*/, SentState> sent_state_map; |
| 252 |
// Spawn a thread to read requests from the stream. |
| 253 |
// Requests will be delivered to this thread in a queue. |
| 254 |
std::deque<DiscoveryRequest> requests; |
| 255 |
bool stream_closed = false; |
| 256 |
std::thread reader(std::bind(&AdsServiceImpl::BlockingRead, this, stream, |
| 257 |
&requests, &stream_closed)); |
| 258 |
// Main loop to process requests and updates. |
| 259 |
while (true) { |
| 260 |
// Boolean to keep track if the loop received any work to do: a |
| 261 |
// request or an update; regardless whether a response was actually |
| 262 |
// sent out. |
| 263 |
bool did_work = false; |
| 264 |
// Look for new requests and decide what to handle. |
| 265 |
absl::optional<DiscoveryResponse> response; |
| 266 |
{ |
| 267 |
grpc_core::MutexLock lock(&ads_mu_); |
| 268 |
// If the stream has been closed or our parent is being shut |
| 269 |
// down, stop immediately. |
| 270 |
if (stream_closed || ads_done_) break; |
| 271 |
// Otherwise, see if there's a request to read from the queue. |
| 272 |
if (!requests.empty()) { |
| 273 |
DiscoveryRequest request = std::move(requests.front()); |
| 274 |
requests.pop_front(); |
| 275 |
did_work = true; |
| 276 |
gpr_log(GPR_INFO, |
| 277 |
"ADS[%p]: Received request for type %s with content %s", this, |
| 278 |
request.type_url().c_str(), request.DebugString().c_str()); |
| 279 |
SentState& sent_state = sent_state_map[request.type_url()]; |
| 280 |
// Process request. |
| 281 |
ProcessRequest(request, &update_queue, &subscription_map, &sent_state, |
| 282 |
&response); |
| 283 |
} |
| 284 |
} |
| 285 |
if (response.has_value()) { |
| 286 |
gpr_log(GPR_INFO, "ADS[%p]: Sending response: %s", this, |
| 287 |
response->DebugString().c_str()); |
| 288 |
stream->Write(response.value()); |
| 289 |
} |
| 290 |
response.reset(); |
| 291 |
// Look for updates and decide what to handle. |
| 292 |
{ |
| 293 |
grpc_core::MutexLock lock(&ads_mu_); |
| 294 |
if (!update_queue.empty()) { |
| 295 |
const std::string resource_type = |
| 296 |
std::move(update_queue.front().first); |
| 297 |
const std::string resource_name = |
| 298 |
std::move(update_queue.front().second); |
| 299 |
update_queue.pop_front(); |
| 300 |
did_work = true; |
| 301 |
SentState& sent_state = sent_state_map[resource_type]; |
| 302 |
ProcessUpdate(resource_type, resource_name, &subscription_map, |
| 303 |
&sent_state, &response); |
| 304 |
} |
| 305 |
} |
| 306 |
if (response.has_value()) { |
| 307 |
gpr_log(GPR_INFO, "ADS[%p]: Sending update response: %s", this, |
| 308 |
response->DebugString().c_str()); |
| 309 |
stream->Write(response.value()); |
| 310 |
} |
| 311 |
{ |
| 312 |
grpc_core::MutexLock lock(&ads_mu_); |
| 313 |
if (ads_done_) { |
| 314 |
break; |
| 315 |
} |
| 316 |
} |
| 317 |
// If we didn't find anything to do, delay before the next loop |
| 318 |
// iteration; otherwise, check whether we should exit and then |
| 319 |
// immediately continue. |
| 320 |
gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(did_work ? 0 : 10)); |
| 321 |
} |
| 322 |
// Done with main loop. Clean up before returning. |
| 323 |
// Join reader thread. |
| 324 |
reader.join(); |
| 325 |
// Clean up any subscriptions that were still active when the call |
| 326 |
// finished. |
| 327 |
{ |
| 328 |
grpc_core::MutexLock lock(&ads_mu_); |
| 329 |
for (auto& p : subscription_map) { |
| 330 |
const std::string& type_url = p.first; |
| 331 |
SubscriptionNameMap& subscription_name_map = p.second; |
| 332 |
for (auto& q : subscription_name_map) { |
| 333 |
const std::string& resource_name = q.first; |
| 334 |
SubscriptionState& subscription_state = q.second; |
| 335 |
ResourceNameMap& resource_name_map = |
| 336 |
resource_map_[type_url].resource_name_map; |
| 337 |
ResourceState& resource_state = resource_name_map[resource_name]; |
| 338 |
resource_state.subscriptions.erase(&subscription_state); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources done", this); |
| 343 |
RemoveClient(context->peer()); |
| 344 |
return Status::OK; |
| 345 |
} |
| 346 |
|
| 347 |
// Processes a response read from the client. |
| 348 |
// Populates response if needed. |
| 349 |
void ProcessRequest(const DiscoveryRequest& request, |
| 350 |
UpdateQueue* update_queue, |
| 351 |
SubscriptionMap* subscription_map, SentState* sent_state, |
| 352 |
absl::optional<DiscoveryResponse>* response) |
| 353 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(ads_mu_) { |
| 354 |
// Check the nonce sent by the client, if any. |
| 355 |
// (This will be absent on the first request on a stream.) |
| 356 |
if (request.response_nonce().empty()) { |
| 357 |
int client_resource_type_version = 0; |
| 358 |
if (!request.version_info().empty()) { |
| 359 |
GPR_ASSERT(absl::SimpleAtoi(request.version_info(), |
| 360 |
&client_resource_type_version)); |
| 361 |
} |
| 362 |
EXPECT_GE(client_resource_type_version, |
| 363 |
resource_type_min_versions_[request.type_url()]) |
| 364 |
<< "resource_type: " << request.type_url(); |
| 365 |
} else { |
| 366 |
int client_nonce; |
| 367 |
GPR_ASSERT(absl::SimpleAtoi(request.response_nonce(), &client_nonce)); |
| 368 |
// Check for ACK or NACK. |
| 369 |
ResponseState response_state; |
| 370 |
if (!request.has_error_detail()) { |
| 371 |
response_state.state = ResponseState::ACKED; |
| 372 |
gpr_log(GPR_INFO, "ADS[%p]: client ACKed resource_type=%s version=%s", |
| 373 |
this, request.type_url().c_str(), |
| 374 |
request.version_info().c_str()); |
| 375 |
} else { |
| 376 |
response_state.state = ResponseState::NACKED; |
| 377 |
EXPECT_EQ(request.error_detail().code(), GRPC_STATUS_INVALID_ARGUMENT); |
| 378 |
response_state.error_message = request.error_detail().message(); |
| 379 |
gpr_log(GPR_INFO, |
| 380 |
"ADS[%p]: client NACKed resource_type=%s version=%s: %s", this, |
| 381 |
request.type_url().c_str(), request.version_info().c_str(), |
| 382 |
response_state.error_message.c_str()); |
| 383 |
} |
| 384 |
resource_type_response_state_[request.type_url()].emplace_back( |
| 385 |
std::move(response_state)); |
| 386 |
// Ignore requests with stale nonces. |
| 387 |
if (client_nonce < sent_state->nonce) return; |
| 388 |
} |
| 389 |
// Ignore resource types as requested by tests. |
| 390 |
if (resource_types_to_ignore_.find(request.type_url()) != |
| 391 |
resource_types_to_ignore_.end()) { |
| 392 |
return; |
| 393 |
} |
| 394 |
// Inject bad resources if needed. |
| 395 |
if (inject_bad_resources_for_resource_type_ == request.type_url()) { |
| 396 |
response->emplace(); |
| 397 |
// Unparseable Resource wrapper. |
| 398 |
auto* resource = (*response)->add_resources(); |
| 399 |
resource->set_type_url( |
| 400 |
"type.googleapis.com/envoy.service.discovery.v3.Resource"); |
| 401 |
resource->set_value(std::string("\0", 1)); |
| 402 |
// Unparseable resource within Resource wrapper. |
| 403 |
envoy::service::discovery::v3::Resource resource_wrapper; |
| 404 |
resource_wrapper.set_name("foo"); |
| 405 |
resource = resource_wrapper.mutable_resource(); |
| 406 |
resource->set_type_url(request.type_url()); |
| 407 |
resource->set_value(std::string("\0", 1)); |
| 408 |
(*response)->add_resources()->PackFrom(resource_wrapper); |
| 409 |
} |
| 410 |
// Look at all the resource names in the request. |
| 411 |
auto& subscription_name_map = (*subscription_map)[request.type_url()]; |
| 412 |
auto& resource_type_state = resource_map_[request.type_url()]; |
| 413 |
auto& resource_name_map = resource_type_state.resource_name_map; |
| 414 |
std::set<std::string> resources_in_current_request; |
| 415 |
std::set<std::string> resources_added_to_response; |
| 416 |
for (const std::string& resource_name : request.resource_names()) { |
| 417 |
resources_in_current_request.emplace(resource_name); |
| 418 |
auto& subscription_state = subscription_name_map[resource_name]; |
| 419 |
auto& resource_state = resource_name_map[resource_name]; |
| 420 |
// Subscribe if needed. |
| 421 |
// Send the resource in the response if either (a) this is |
| 422 |
// a new subscription or (b) there is an updated version of |
| 423 |
// this resource to send. |
| 424 |
if (MaybeSubscribe(request.type_url(), resource_name, &subscription_state, |
| 425 |
&resource_state, update_queue) || |
| 426 |
ClientNeedsResourceUpdate(resource_type_state, resource_state, |
| 427 |
sent_state->resource_type_version)) { |
| 428 |
gpr_log(GPR_INFO, "ADS[%p]: Sending update for type=%s name=%s", this, |
| 429 |
request.type_url().c_str(), resource_name.c_str()); |
| 430 |
resources_added_to_response.emplace(resource_name); |
| 431 |
if (!response->has_value()) response->emplace(); |
| 432 |
if (resource_state.resource.has_value()) { |
| 433 |
auto* resource = (*response)->add_resources(); |
| 434 |
resource->CopyFrom(resource_state.resource.value()); |
| 435 |
if (wrap_resources_) { |
| 436 |
envoy::service::discovery::v3::Resource resource_wrapper; |
| 437 |
*resource_wrapper.mutable_resource() = std::move(*resource); |
| 438 |
resource->PackFrom(resource_wrapper); |
| 439 |
} |
| 440 |
} |
| 441 |
} else { |
| 442 |
gpr_log(GPR_INFO, |
| 443 |
"ADS[%p]: client does not need update for type=%s name=%s", |
| 444 |
this, request.type_url().c_str(), resource_name.c_str()); |
| 445 |
} |
| 446 |
} |
| 447 |
// Process unsubscriptions for any resource no longer |
| 448 |
// present in the request's resource list. |
| 449 |
ProcessUnsubscriptions(request.type_url(), resources_in_current_request, |
| 450 |
&subscription_name_map, &resource_name_map); |
| 451 |
// Construct response if needed. |
| 452 |
if (!resources_added_to_response.empty()) { |
| 453 |
CompleteBuildingDiscoveryResponse( |
| 454 |
request.type_url(), resource_type_state.resource_type_version, |
| 455 |
subscription_name_map, resources_added_to_response, sent_state, |
| 456 |
&response->value()); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
// Processes a resource update from the test. |
| 461 |
// Populates response if needed. |
| 462 |
void ProcessUpdate(const std::string& resource_type, |
| 463 |
const std::string& resource_name, |
| 464 |
SubscriptionMap* subscription_map, SentState* sent_state, |
| 465 |
absl::optional<DiscoveryResponse>* response) |
| 466 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(ads_mu_) { |
| 467 |
gpr_log(GPR_INFO, "ADS[%p]: Received update for type=%s name=%s", this, |
| 468 |
resource_type.c_str(), resource_name.c_str()); |
| 469 |
auto& subscription_name_map = (*subscription_map)[resource_type]; |
| 470 |
auto& resource_type_state = resource_map_[resource_type]; |
| 471 |
auto& resource_name_map = resource_type_state.resource_name_map; |
| 472 |
auto it = subscription_name_map.find(resource_name); |
| 473 |
if (it != subscription_name_map.end()) { |
| 474 |
ResourceState& resource_state = resource_name_map[resource_name]; |
| 475 |
if (ClientNeedsResourceUpdate(resource_type_state, resource_state, |
| 476 |
sent_state->resource_type_version)) { |
| 477 |
gpr_log(GPR_INFO, "ADS[%p]: Sending update for type=%s name=%s", this, |
| 478 |
resource_type.c_str(), resource_name.c_str()); |
| 479 |
response->emplace(); |
| 480 |
if (resource_state.resource.has_value()) { |
| 481 |
auto* resource = (*response)->add_resources(); |
| 482 |
resource->CopyFrom(resource_state.resource.value()); |
| 483 |
} |
| 484 |
CompleteBuildingDiscoveryResponse( |
| 485 |
resource_type, resource_type_state.resource_type_version, |
| 486 |
subscription_name_map, {resource_name}, sent_state, |
| 487 |
&response->value()); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
// Starting a thread to do blocking read on the stream until cancel. |
| 493 |
void BlockingRead(Stream* stream, std::deque<DiscoveryRequest>* requests, |
| 494 |
bool* stream_closed) { |
| 495 |
DiscoveryRequest request; |
| 496 |
bool seen_first_request = false; |
| 497 |
while (stream->Read(&request)) { |
| 498 |
if (!seen_first_request) { |
| 499 |
EXPECT_TRUE(request.has_node()); |
| 500 |
EXPECT_THAT(request.node().client_features(), |
| 501 |
::testing::UnorderedElementsAre( |
| 502 |
"envoy.lb.does_not_support_overprovisioning", |
| 503 |
"xds.config.resource-in-sotw")); |
| 504 |
seen_first_request = true; |
| 505 |
} |
| 506 |
{ |
| 507 |
grpc_core::MutexLock lock(&ads_mu_); |
| 508 |
requests->emplace_back(std::move(request)); |
| 509 |
} |
| 510 |
} |
| 511 |
gpr_log(GPR_INFO, "ADS[%p]: Null read, stream closed", this); |
| 512 |
grpc_core::MutexLock lock(&ads_mu_); |
| 513 |
*stream_closed = true; |
| 514 |
} |
| 515 |
|
| 516 |
// Completing the building a DiscoveryResponse by adding common information |
| 517 |
// for all resources and by adding all subscribed resources for LDS and CDS. |
| 518 |
void CompleteBuildingDiscoveryResponse( |
| 519 |
const std::string& resource_type, const int version, |
| 520 |
const SubscriptionNameMap& subscription_name_map, |
| 521 |
const std::set<std::string>& resources_added_to_response, |
| 522 |
SentState* sent_state, DiscoveryResponse* response) |
| 523 |
ABSL_EXCLUSIVE_LOCKS_REQUIRED(ads_mu_) { |
| 524 |
response->set_type_url(resource_type); |
| 525 |
response->set_version_info(std::to_string(version)); |
| 526 |
response->set_nonce(std::to_string(++sent_state->nonce)); |
| 527 |
if (resource_type == kLdsTypeUrl || resource_type == kCdsTypeUrl) { |
| 528 |
// For LDS and CDS we must send back all subscribed resources |
| 529 |
// (even the unchanged ones) |
| 530 |
for (const auto& p : subscription_name_map) { |
| 531 |
const std::string& resource_name = p.first; |
| 532 |
if (resources_added_to_response.find(resource_name) == |
| 533 |
resources_added_to_response.end()) { |
| 534 |
ResourceNameMap& resource_name_map = |
| 535 |
resource_map_[resource_type].resource_name_map; |
| 536 |
const ResourceState& resource_state = |
| 537 |
resource_name_map[resource_name]; |
| 538 |
if (resource_state.resource.has_value()) { |
| 539 |
auto* resource = response->add_resources(); |
| 540 |
resource->CopyFrom(resource_state.resource.value()); |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
sent_state->resource_type_version = version; |
| 546 |
} |
| 547 |
|
| 548 |
// Checks whether the client needs to receive a newer version of |
| 549 |
// the resource. |
| 550 |
static bool ClientNeedsResourceUpdate( |
| 551 |
const ResourceTypeState& resource_type_state, |
| 552 |
const ResourceState& resource_state, int client_resource_type_version); |
| 553 |
|
| 554 |
// Subscribes to a resource if not already subscribed: |
| 555 |
// 1. Sets the update_queue field in subscription_state. |
| 556 |
// 2. Adds subscription_state to resource_state->subscriptions. |
| 557 |
bool MaybeSubscribe(const std::string& resource_type, |
| 558 |
const std::string& resource_name, |
| 559 |
SubscriptionState* subscription_state, |
| 560 |
ResourceState* resource_state, UpdateQueue* update_queue); |
| 561 |
|
| 562 |
// Removes subscriptions for resources no longer present in the |
| 563 |
// current request. |
| 564 |
void ProcessUnsubscriptions( |
| 565 |
const std::string& resource_type, |
| 566 |
const std::set<std::string>& resources_in_current_request, |
| 567 |
SubscriptionNameMap* subscription_name_map, |
| 568 |
ResourceNameMap* resource_name_map); |
| 569 |
|
| 570 |
void AddClient(const std::string& client) { |
| 571 |
grpc_core::MutexLock lock(&clients_mu_); |
| 572 |
clients_.insert(client); |
| 573 |
} |
| 574 |
|
| 575 |
void RemoveClient(const std::string& client) { |
| 576 |
grpc_core::MutexLock lock(&clients_mu_); |
| 577 |
clients_.erase(client); |
| 578 |
} |
| 579 |
|
| 580 |
grpc_core::CondVar ads_cond_; |
| 581 |
grpc_core::Mutex ads_mu_; |
| 582 |
bool ads_done_ ABSL_GUARDED_BY(ads_mu_) = false; |
| 583 |
std::map<std::string /* type_url */, std::deque<ResponseState>> |
| 584 |
resource_type_response_state_ ABSL_GUARDED_BY(ads_mu_); |
| 585 |
std::set<std::string /*resource_type*/> resource_types_to_ignore_ |
| 586 |
ABSL_GUARDED_BY(ads_mu_); |
| 587 |
std::map<std::string /*resource_type*/, int> resource_type_min_versions_ |
| 588 |
ABSL_GUARDED_BY(ads_mu_); |
| 589 |
// An instance data member containing the current state of all resources. |
| 590 |
// Note that an entry will exist whenever either of the following is true: |
| 591 |
// - The resource exists (i.e., has been created by SetResource() and has not |
| 592 |
// yet been destroyed by UnsetResource()). |
| 593 |
// - There is at least one subscription for the resource. |
| 594 |
ResourceMap resource_map_ ABSL_GUARDED_BY(ads_mu_); |
| 595 |
absl::optional<Status> forced_ads_failure_ ABSL_GUARDED_BY(ads_mu_); |
| 596 |
bool wrap_resources_ ABSL_GUARDED_BY(ads_mu_) = false; |
| 597 |
std::string inject_bad_resources_for_resource_type_ ABSL_GUARDED_BY(ads_mu_); |
| 598 |
|
| 599 |
grpc_core::Mutex clients_mu_; |
| 600 |
std::set<std::string> clients_ ABSL_GUARDED_BY(clients_mu_); |
| 601 |
}; |
| 602 |
|
| 603 |
// An LRS service implementation. |
| 604 |
class LrsServiceImpl |
| 605 |
: public CountedService< |
| 606 |
::envoy::service::load_stats::v3::LoadReportingService::Service>, |
| 607 |
public std::enable_shared_from_this<LrsServiceImpl> { |
| 608 |
public: |
| 609 |
// Stats reported by client. |
| 610 |
class ClientStats { |
| 611 |
public: |
| 612 |
// Stats for a given locality. |
| 613 |
struct LocalityStats { |
| 614 |
LocalityStats() {} |
| 615 |
|
| 616 |
// Converts from proto message class. |
| 617 |
explicit LocalityStats( |
| 618 |
const ::envoy::config::endpoint::v3::UpstreamLocalityStats& |
| 619 |
upstream_locality_stats) |
| 620 |
: total_successful_requests( |
| 621 |
upstream_locality_stats.total_successful_requests()), |
| 622 |
total_requests_in_progress( |
| 623 |
upstream_locality_stats.total_requests_in_progress()), |
| 624 |
total_error_requests( |
| 625 |
upstream_locality_stats.total_error_requests()), |
| 626 |
total_issued_requests( |
| 627 |
upstream_locality_stats.total_issued_requests()) {} |
| 628 |
|
| 629 |
LocalityStats& operator+=(const LocalityStats& other) { |
| 630 |
total_successful_requests += other.total_successful_requests; |
| 631 |
total_requests_in_progress += other.total_requests_in_progress; |
| 632 |
total_error_requests += other.total_error_requests; |
| 633 |
total_issued_requests += other.total_issued_requests; |
| 634 |
return *this; |
| 635 |
} |
| 636 |
|
| 637 |
uint64_t total_successful_requests = 0; |
| 638 |
uint64_t total_requests_in_progress = 0; |
| 639 |
uint64_t total_error_requests = 0; |
| 640 |
uint64_t total_issued_requests = 0; |
| 641 |
}; |
| 642 |
|
| 643 |
ClientStats() {} |
| 644 |
|
| 645 |
// Converts from proto message class. |
| 646 |
explicit ClientStats( |
| 647 |
const ::envoy::config::endpoint::v3::ClusterStats& cluster_stats) |
| 648 |
: cluster_name_(cluster_stats.cluster_name()), |
| 649 |
eds_service_name_(cluster_stats.cluster_service_name()), |
| 650 |
total_dropped_requests_(cluster_stats.total_dropped_requests()) { |
| 651 |
for (const auto& input_locality_stats : |
| 652 |
cluster_stats.upstream_locality_stats()) { |
| 653 |
locality_stats_.emplace(input_locality_stats.locality().sub_zone(), |
| 654 |
LocalityStats(input_locality_stats)); |
| 655 |
} |
| 656 |
for (const auto& input_dropped_requests : |
| 657 |
cluster_stats.dropped_requests()) { |
| 658 |
dropped_requests_.emplace(input_dropped_requests.category(), |
| 659 |
input_dropped_requests.dropped_count()); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
const std::string& cluster_name() const { return cluster_name_; } |
| 664 |
const std::string& eds_service_name() const { return eds_service_name_; } |
| 665 |
|
| 666 |
const std::map<std::string, LocalityStats>& locality_stats() const { |
| 667 |
return locality_stats_; |
| 668 |
} |
| 669 |
|
| 670 |
uint64_t total_successful_requests() const; |
| 671 |
uint64_t total_requests_in_progress() const; |
| 672 |
uint64_t total_error_requests() const; |
| 673 |
uint64_t total_issued_requests() const; |
| 674 |
|
| 675 |
uint64_t total_dropped_requests() const { return total_dropped_requests_; } |
| 676 |
|
| 677 |
uint64_t dropped_requests(const std::string& category) const; |
| 678 |
|
| 679 |
ClientStats& operator+=(const ClientStats& other); |
| 680 |
|
| 681 |
private: |
| 682 |
std::string cluster_name_; |
| 683 |
std::string eds_service_name_; |
| 684 |
std::map<std::string, LocalityStats> locality_stats_; |
| 685 |
uint64_t total_dropped_requests_ = 0; |
| 686 |
std::map<std::string, uint64_t> dropped_requests_; |
| 687 |
}; |
| 688 |
|
| 689 |
LrsServiceImpl(int client_load_reporting_interval_seconds, |
| 690 |
std::set<std::string> cluster_names) |
| 691 |
: client_load_reporting_interval_seconds_( |
| 692 |
client_load_reporting_interval_seconds), |
| 693 |
cluster_names_(std::move(cluster_names)) {} |
| 694 |
|
| 695 |
// Must be called before the LRS call is started. |
| 696 |
void set_send_all_clusters(bool send_all_clusters) { |
| 697 |
send_all_clusters_ = send_all_clusters; |
| 698 |
} |
| 699 |
void set_cluster_names(const std::set<std::string>& cluster_names) { |
| 700 |
cluster_names_ = cluster_names; |
| 701 |
} |
| 702 |
|
| 703 |
void Start() ABSL_LOCKS_EXCLUDED(lrs_mu_, load_report_mu_); |
| 704 |
|
| 705 |
void Shutdown(); |
| 706 |
|
| 707 |
std::vector<ClientStats> WaitForLoadReport(); |
| 708 |
|
| 709 |
private: |
| 710 |
using LoadStatsRequest = ::envoy::service::load_stats::v3::LoadStatsRequest; |
| 711 |
using LoadStatsResponse = ::envoy::service::load_stats::v3::LoadStatsResponse; |
| 712 |
using Stream = ServerReaderWriter<LoadStatsResponse, LoadStatsRequest>; |
| 713 |
|
| 714 |
Status StreamLoadStats(ServerContext* /*context*/, Stream* stream) override { |
| 715 |
gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats starts", this); |
| 716 |
EXPECT_GT(client_load_reporting_interval_seconds_, 0); |
| 717 |
// Take a reference of the LrsServiceImpl object, reference will go |
| 718 |
// out of scope after this method exits. |
| 719 |
std::shared_ptr<LrsServiceImpl> lrs_service_impl = shared_from_this(); |
| 720 |
// Read initial request. |
| 721 |
LoadStatsRequest request; |
| 722 |
if (stream->Read(&request)) { |
| 723 |
IncreaseRequestCount(); |
| 724 |
// Verify client features. |
| 725 |
EXPECT_THAT(request.node().client_features(), |
| 726 |
::testing::Contains("envoy.lrs.supports_send_all_clusters")); |
| 727 |
// Send initial response. |
| 728 |
LoadStatsResponse response; |
| 729 |
if (send_all_clusters_) { |
| 730 |
response.set_send_all_clusters(true); |
| 731 |
} else { |
| 732 |
for (const std::string& cluster_name : cluster_names_) { |
| 733 |
response.add_clusters(cluster_name); |
| 734 |
} |
| 735 |
} |
| 736 |
response.mutable_load_reporting_interval()->set_seconds( |
| 737 |
client_load_reporting_interval_seconds_ * |
| 738 |
grpc_test_slowdown_factor()); |
| 739 |
stream->Write(response); |
| 740 |
IncreaseResponseCount(); |
| 741 |
// Wait for report. |
| 742 |
request.Clear(); |
| 743 |
while (stream->Read(&request)) { |
| 744 |
gpr_log(GPR_INFO, "LRS[%p]: received client load report message: %s", |
| 745 |
this, request.DebugString().c_str()); |
| 746 |
std::vector<ClientStats> stats; |
| 747 |
for (const auto& cluster_stats : request.cluster_stats()) { |
| 748 |
stats.emplace_back(cluster_stats); |
| 749 |
} |
| 750 |
grpc_core::MutexLock lock(&load_report_mu_); |
| 751 |
result_queue_.emplace_back(std::move(stats)); |
| 752 |
if (load_report_cond_ != nullptr) { |
| 753 |
load_report_cond_->Signal(); |
| 754 |
} |
| 755 |
} |
| 756 |
// Wait until notified done. |
| 757 |
grpc_core::MutexLock lock(&lrs_mu_); |
| 758 |
while (!lrs_done_) { |
| 759 |
lrs_cv_.Wait(&lrs_mu_); |
| 760 |
} |
| 761 |
} |
| 762 |
gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats done", this); |
| 763 |
return Status::OK; |
| 764 |
} |
| 765 |
|
| 766 |
const int client_load_reporting_interval_seconds_; |
| 767 |
bool send_all_clusters_ = false; |
| 768 |
std::set<std::string> cluster_names_; |
| 769 |
|
| 770 |
grpc_core::CondVar lrs_cv_; |
| 771 |
grpc_core::Mutex lrs_mu_; |
| 772 |
bool lrs_done_ ABSL_GUARDED_BY(lrs_mu_) = false; |
| 773 |
|
| 774 |
grpc_core::Mutex load_report_mu_; |
| 775 |
grpc_core::CondVar* load_report_cond_ ABSL_GUARDED_BY(load_report_mu_) = |
| 776 |
nullptr; |
| 777 |
std::deque<std::vector<ClientStats>> result_queue_ |
| 778 |
ABSL_GUARDED_BY(load_report_mu_); |
| 779 |
}; |
| 780 |
|
| 781 |
} // namespace testing |
| 782 |
} // namespace grpc |
| 783 |
|
| 784 |
#endif // GRPC_TEST_CPP_END2END_XDS_XDS_SERVER_H |