| 1 |
// |
| 2 |
// Copyright 2018 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 <grpc/support/port_platform.h> |
| 18 |
|
| 19 |
#include "src/core/ext/xds/xds_api.h" |
| 20 |
|
| 21 |
#include <stdint.h> |
| 22 |
#include <stdlib.h> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <set> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "absl/strings/str_cat.h" |
| 30 |
#include "absl/strings/strip.h" |
| 31 |
#include "envoy/config/core/v3/base.upb.h" |
| 32 |
#include "envoy/config/endpoint/v3/load_report.upb.h" |
| 33 |
#include "envoy/service/discovery/v3/discovery.upb.h" |
| 34 |
#include "envoy/service/discovery/v3/discovery.upbdefs.h" |
| 35 |
#include "envoy/service/load_stats/v3/lrs.upb.h" |
| 36 |
#include "envoy/service/load_stats/v3/lrs.upbdefs.h" |
| 37 |
#include "envoy/service/status/v3/csds.upb.h" |
| 38 |
#include "google/protobuf/any.upb.h" |
| 39 |
#include "google/protobuf/duration.upb.h" |
| 40 |
#include "google/protobuf/struct.upb.h" |
| 41 |
#include "google/protobuf/timestamp.upb.h" |
| 42 |
#include "google/rpc/status.upb.h" |
| 43 |
#include "upb/def.h" |
| 44 |
#include "upb/text_encode.h" |
| 45 |
#include "upb/upb.h" |
| 46 |
#include "upb/upb.hpp" |
| 47 |
|
| 48 |
#include <grpc/grpc.h> |
| 49 |
#include <grpc/impl/codegen/gpr_types.h> |
| 50 |
#include <grpc/status.h> |
| 51 |
#include <grpc/support/log.h> |
| 52 |
|
| 53 |
#include "src/core/ext/xds/upb_utils.h" |
| 54 |
#include "src/core/ext/xds/xds_client.h" |
| 55 |
#include "src/core/lib/json/json.h" |
| 56 |
|
| 57 |
// IWYU pragma: no_include "upb/msg_internal.h" |
| 58 |
|
| 59 |
namespace grpc_core { |
| 60 |
|
| 61 |
// If gRPC is built with -DGRPC_XDS_USER_AGENT_NAME_SUFFIX="...", that string |
| 62 |
// will be appended to the user agent name reported to the xDS server. |
| 63 |
#ifdef GRPC_XDS_USER_AGENT_NAME_SUFFIX |
| 64 |
#define GRPC_XDS_USER_AGENT_NAME_SUFFIX_STRING \ |
| 65 |
" " GRPC_XDS_USER_AGENT_NAME_SUFFIX |
| 66 |
#else |
| 67 |
#define GRPC_XDS_USER_AGENT_NAME_SUFFIX_STRING "" |
| 68 |
#endif |
| 69 |
|
| 70 |
// If gRPC is built with -DGRPC_XDS_USER_AGENT_VERSION_SUFFIX="...", that string |
| 71 |
// will be appended to the user agent version reported to the xDS server. |
| 72 |
#ifdef GRPC_XDS_USER_AGENT_VERSION_SUFFIX |
| 73 |
#define GRPC_XDS_USER_AGENT_VERSION_SUFFIX_STRING \ |
| 74 |
" " GRPC_XDS_USER_AGENT_VERSION_SUFFIX |
| 75 |
#else |
| 76 |
#define GRPC_XDS_USER_AGENT_VERSION_SUFFIX_STRING "" |
| 77 |
#endif |
| 78 |
|
| 79 |
XdsApi::XdsApi(XdsClient* client, TraceFlag* tracer, |
| 80 |
const XdsBootstrap::Node* node, upb::SymbolTable* symtab) |
| 81 |
: client_(client), |
| 82 |
tracer_(tracer), |
| 83 |
node_(node), |
| 84 |
symtab_(symtab), |
| 85 |
user_agent_name_(absl::StrCat("gRPC C-core ", GPR_PLATFORM_STRING, |
| 86 |
GRPC_XDS_USER_AGENT_NAME_SUFFIX_STRING)), |
| 87 |
user_agent_version_( |
| 88 |
absl::StrCat("C-core ", grpc_version_string(), |
| 89 |
GRPC_XDS_USER_AGENT_NAME_SUFFIX_STRING, |
| 90 |
GRPC_XDS_USER_AGENT_VERSION_SUFFIX_STRING)) {} |
| 91 |
|
| 92 |
namespace { |
| 93 |
|
| 94 |
struct XdsApiContext { |
| 95 |
XdsClient* client; |
| 96 |
TraceFlag* tracer; |
| 97 |
upb_DefPool* symtab; |
| 98 |
upb_Arena* arena; |
| 99 |
}; |
| 100 |
|
| 101 |
void PopulateMetadataValue(const XdsApiContext& context, |
| 102 |
google_protobuf_Value* value_pb, const Json& value); |
| 103 |
|
| 104 |
void PopulateListValue(const XdsApiContext& context, |
| 105 |
google_protobuf_ListValue* list_value, |
| 106 |
const Json::Array& values) { |
| 107 |
for (const auto& value : values) { |
| 108 |
auto* value_pb = |
| 109 |
google_protobuf_ListValue_add_values(list_value, context.arena); |
| 110 |
PopulateMetadataValue(context, value_pb, value); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void PopulateMetadata(const XdsApiContext& context, |
| 115 |
google_protobuf_Struct* metadata_pb, |
| 116 |
const Json::Object& metadata) { |
| 117 |
for (const auto& p : metadata) { |
| 118 |
google_protobuf_Value* value = google_protobuf_Value_new(context.arena); |
| 119 |
PopulateMetadataValue(context, value, p.second); |
| 120 |
google_protobuf_Struct_fields_set( |
| 121 |
metadata_pb, StdStringToUpbString(p.first), value, context.arena); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void PopulateMetadataValue(const XdsApiContext& context, |
| 126 |
google_protobuf_Value* value_pb, const Json& value) { |
| 127 |
switch (value.type()) { |
| 128 |
case Json::Type::JSON_NULL: |
| 129 |
google_protobuf_Value_set_null_value(value_pb, 0); |
| 130 |
break; |
| 131 |
case Json::Type::NUMBER: |
| 132 |
google_protobuf_Value_set_number_value( |
| 133 |
value_pb, strtod(value.string_value().c_str(), nullptr)); |
| 134 |
break; |
| 135 |
case Json::Type::STRING: |
| 136 |
google_protobuf_Value_set_string_value( |
| 137 |
value_pb, StdStringToUpbString(value.string_value())); |
| 138 |
break; |
| 139 |
case Json::Type::JSON_TRUE: |
| 140 |
google_protobuf_Value_set_bool_value(value_pb, true); |
| 141 |
break; |
| 142 |
case Json::Type::JSON_FALSE: |
| 143 |
google_protobuf_Value_set_bool_value(value_pb, false); |
| 144 |
break; |
| 145 |
case Json::Type::OBJECT: { |
| 146 |
google_protobuf_Struct* struct_value = |
| 147 |
google_protobuf_Value_mutable_struct_value(value_pb, context.arena); |
| 148 |
PopulateMetadata(context, struct_value, value.object_value()); |
| 149 |
break; |
| 150 |
} |
| 151 |
case Json::Type::ARRAY: { |
| 152 |
google_protobuf_ListValue* list_value = |
| 153 |
google_protobuf_Value_mutable_list_value(value_pb, context.arena); |
| 154 |
PopulateListValue(context, list_value, value.array_value()); |
| 155 |
break; |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void PopulateNode(const XdsApiContext& context, const XdsBootstrap::Node* node, |
| 161 |
const std::string& user_agent_name, |
| 162 |
const std::string& user_agent_version, |
| 163 |
envoy_config_core_v3_Node* node_msg) { |
| 164 |
if (node != nullptr) { |
| 165 |
if (!node->id().empty()) { |
| 166 |
envoy_config_core_v3_Node_set_id(node_msg, |
| 167 |
StdStringToUpbString(node->id())); |
| 168 |
} |
| 169 |
if (!node->cluster().empty()) { |
| 170 |
envoy_config_core_v3_Node_set_cluster( |
| 171 |
node_msg, StdStringToUpbString(node->cluster())); |
| 172 |
} |
| 173 |
if (!node->metadata().empty()) { |
| 174 |
google_protobuf_Struct* metadata = |
| 175 |
envoy_config_core_v3_Node_mutable_metadata(node_msg, context.arena); |
| 176 |
PopulateMetadata(context, metadata, node->metadata()); |
| 177 |
} |
| 178 |
if (!node->locality_region().empty() || !node->locality_zone().empty() || |
| 179 |
!node->locality_sub_zone().empty()) { |
| 180 |
envoy_config_core_v3_Locality* locality = |
| 181 |
envoy_config_core_v3_Node_mutable_locality(node_msg, context.arena); |
| 182 |
if (!node->locality_region().empty()) { |
| 183 |
envoy_config_core_v3_Locality_set_region( |
| 184 |
locality, StdStringToUpbString(node->locality_region())); |
| 185 |
} |
| 186 |
if (!node->locality_zone().empty()) { |
| 187 |
envoy_config_core_v3_Locality_set_zone( |
| 188 |
locality, StdStringToUpbString(node->locality_zone())); |
| 189 |
} |
| 190 |
if (!node->locality_sub_zone().empty()) { |
| 191 |
envoy_config_core_v3_Locality_set_sub_zone( |
| 192 |
locality, StdStringToUpbString(node->locality_sub_zone())); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
envoy_config_core_v3_Node_set_user_agent_name( |
| 197 |
node_msg, StdStringToUpbString(user_agent_name)); |
| 198 |
envoy_config_core_v3_Node_set_user_agent_version( |
| 199 |
node_msg, StdStringToUpbString(user_agent_version)); |
| 200 |
envoy_config_core_v3_Node_add_client_features( |
| 201 |
node_msg, |
| 202 |
upb_StringView_FromString("envoy.lb.does_not_support_overprovisioning"), |
| 203 |
context.arena); |
| 204 |
} |
| 205 |
|
| 206 |
void MaybeLogDiscoveryRequest( |
| 207 |
const XdsApiContext& context, |
| 208 |
const envoy_service_discovery_v3_DiscoveryRequest* request) { |
| 209 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer) && |
| 210 |
gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) { |
| 211 |
const upb_MessageDef* msg_type = |
| 212 |
envoy_service_discovery_v3_DiscoveryRequest_getmsgdef(context.symtab); |
| 213 |
char buf[10240]; |
| 214 |
upb_TextEncode(request, msg_type, nullptr, 0, buf, sizeof(buf)); |
| 215 |
gpr_log(GPR_DEBUG, "[xds_client %p] constructed ADS request: %s", |
| 216 |
context.client, buf); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
std::string SerializeDiscoveryRequest( |
| 221 |
const XdsApiContext& context, |
| 222 |
envoy_service_discovery_v3_DiscoveryRequest* request) { |
| 223 |
size_t output_length; |
| 224 |
char* output = envoy_service_discovery_v3_DiscoveryRequest_serialize( |
| 225 |
request, context.arena, &output_length); |
| 226 |
return std::string(output, output_length); |
| 227 |
} |
| 228 |
|
| 229 |
} // namespace |
| 230 |
|
| 231 |
std::string XdsApi::CreateAdsRequest( |
| 232 |
absl::string_view type_url, absl::string_view version, |
| 233 |
absl::string_view nonce, const std::vector<std::string>& resource_names, |
| 234 |
absl::Status status, bool populate_node) { |
| 235 |
upb::Arena arena; |
| 236 |
const XdsApiContext context = {client_, tracer_, symtab_->ptr(), arena.ptr()}; |
| 237 |
// Create a request. |
| 238 |
envoy_service_discovery_v3_DiscoveryRequest* request = |
| 239 |
envoy_service_discovery_v3_DiscoveryRequest_new(arena.ptr()); |
| 240 |
// Set type_url. |
| 241 |
std::string type_url_str = absl::StrCat("type.googleapis.com/", type_url); |
| 242 |
envoy_service_discovery_v3_DiscoveryRequest_set_type_url( |
| 243 |
request, StdStringToUpbString(type_url_str)); |
| 244 |
// Set version_info. |
| 245 |
if (!version.empty()) { |
| 246 |
envoy_service_discovery_v3_DiscoveryRequest_set_version_info( |
| 247 |
request, StdStringToUpbString(version)); |
| 248 |
} |
| 249 |
// Set nonce. |
| 250 |
if (!nonce.empty()) { |
| 251 |
envoy_service_discovery_v3_DiscoveryRequest_set_response_nonce( |
| 252 |
request, StdStringToUpbString(nonce)); |
| 253 |
} |
| 254 |
// Set error_detail if it's a NACK. |
| 255 |
std::string error_string_storage; |
| 256 |
if (!status.ok()) { |
| 257 |
google_rpc_Status* error_detail = |
| 258 |
envoy_service_discovery_v3_DiscoveryRequest_mutable_error_detail( |
| 259 |
request, arena.ptr()); |
| 260 |
// Hard-code INVALID_ARGUMENT as the status code. |
| 261 |
// TODO(roth): If at some point we decide we care about this value, |
| 262 |
// we could attach a status code to the individual errors where we |
| 263 |
// generate them in the parsing code, and then use that here. |
| 264 |
google_rpc_Status_set_code(error_detail, GRPC_STATUS_INVALID_ARGUMENT); |
| 265 |
// Error description comes from the status that was passed in. |
| 266 |
error_string_storage = std::string(status.message()); |
| 267 |
upb_StringView error_description = |
| 268 |
StdStringToUpbString(error_string_storage); |
| 269 |
google_rpc_Status_set_message(error_detail, error_description); |
| 270 |
} |
| 271 |
// Populate node. |
| 272 |
if (populate_node) { |
| 273 |
envoy_config_core_v3_Node* node_msg = |
| 274 |
envoy_service_discovery_v3_DiscoveryRequest_mutable_node(request, |
| 275 |
arena.ptr()); |
| 276 |
PopulateNode(context, node_, user_agent_name_, user_agent_version_, |
| 277 |
node_msg); |
| 278 |
envoy_config_core_v3_Node_add_client_features( |
| 279 |
node_msg, upb_StringView_FromString("xds.config.resource-in-sotw"), |
| 280 |
context.arena); |
| 281 |
} |
| 282 |
// Add resource_names. |
| 283 |
for (const std::string& resource_name : resource_names) { |
| 284 |
envoy_service_discovery_v3_DiscoveryRequest_add_resource_names( |
| 285 |
request, StdStringToUpbString(resource_name), arena.ptr()); |
| 286 |
} |
| 287 |
MaybeLogDiscoveryRequest(context, request); |
| 288 |
return SerializeDiscoveryRequest(context, request); |
| 289 |
} |
| 290 |
|
| 291 |
namespace { |
| 292 |
|
| 293 |
void MaybeLogDiscoveryResponse( |
| 294 |
const XdsApiContext& context, |
| 295 |
const envoy_service_discovery_v3_DiscoveryResponse* response) { |
| 296 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer) && |
| 297 |
gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) { |
| 298 |
const upb_MessageDef* msg_type = |
| 299 |
envoy_service_discovery_v3_DiscoveryResponse_getmsgdef(context.symtab); |
| 300 |
char buf[10240]; |
| 301 |
upb_TextEncode(response, msg_type, nullptr, 0, buf, sizeof(buf)); |
| 302 |
gpr_log(GPR_DEBUG, "[xds_client %p] received response: %s", context.client, |
| 303 |
buf); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
} // namespace |
| 308 |
|
| 309 |
absl::Status XdsApi::ParseAdsResponse(absl::string_view encoded_response, |
| 310 |
AdsResponseParserInterface* parser) { |
| 311 |
upb::Arena arena; |
| 312 |
const XdsApiContext context = {client_, tracer_, symtab_->ptr(), arena.ptr()}; |
| 313 |
// Decode the response. |
| 314 |
const envoy_service_discovery_v3_DiscoveryResponse* response = |
| 315 |
envoy_service_discovery_v3_DiscoveryResponse_parse( |
| 316 |
encoded_response.data(), encoded_response.size(), arena.ptr()); |
| 317 |
// If decoding fails, report a fatal error and return. |
| 318 |
if (response == nullptr) { |
| 319 |
return absl::InvalidArgumentError("Can't decode DiscoveryResponse."); |
| 320 |
} |
| 321 |
MaybeLogDiscoveryResponse(context, response); |
| 322 |
// Report the type_url, version, nonce, and number of resources to the parser. |
| 323 |
AdsResponseParserInterface::AdsResponseFields fields; |
| 324 |
fields.type_url = std::string(absl::StripPrefix( |
| 325 |
UpbStringToAbsl( |
| 326 |
envoy_service_discovery_v3_DiscoveryResponse_type_url(response)), |
| 327 |
"type.googleapis.com/")); |
| 328 |
fields.version = UpbStringToStdString( |
| 329 |
envoy_service_discovery_v3_DiscoveryResponse_version_info(response)); |
| 330 |
fields.nonce = UpbStringToStdString( |
| 331 |
envoy_service_discovery_v3_DiscoveryResponse_nonce(response)); |
| 332 |
size_t num_resources; |
| 333 |
const google_protobuf_Any* const* resources = |
| 334 |
envoy_service_discovery_v3_DiscoveryResponse_resources(response, |
| 335 |
&num_resources); |
| 336 |
fields.num_resources = num_resources; |
| 337 |
absl::Status status = parser->ProcessAdsResponseFields(std::move(fields)); |
| 338 |
if (!status.ok()) return status; |
| 339 |
// Process each resource. |
| 340 |
for (size_t i = 0; i < num_resources; ++i) { |
| 341 |
absl::string_view type_url = absl::StripPrefix( |
| 342 |
UpbStringToAbsl(google_protobuf_Any_type_url(resources[i])), |
| 343 |
"type.googleapis.com/"); |
| 344 |
absl::string_view serialized_resource = |
| 345 |
UpbStringToAbsl(google_protobuf_Any_value(resources[i])); |
| 346 |
// Unwrap Resource messages, if so wrapped. |
| 347 |
absl::string_view resource_name; |
| 348 |
if (type_url == "envoy.service.discovery.v3.Resource") { |
| 349 |
const auto* resource_wrapper = envoy_service_discovery_v3_Resource_parse( |
| 350 |
serialized_resource.data(), serialized_resource.size(), arena.ptr()); |
| 351 |
if (resource_wrapper == nullptr) { |
| 352 |
parser->ResourceWrapperParsingFailed(i); |
| 353 |
continue; |
| 354 |
} |
| 355 |
const auto* resource = |
| 356 |
envoy_service_discovery_v3_Resource_resource(resource_wrapper); |
| 357 |
type_url = absl::StripPrefix( |
| 358 |
UpbStringToAbsl(google_protobuf_Any_type_url(resource)), |
| 359 |
"type.googleapis.com/"); |
| 360 |
serialized_resource = |
| 361 |
UpbStringToAbsl(google_protobuf_Any_value(resource)); |
| 362 |
resource_name = UpbStringToAbsl( |
| 363 |
envoy_service_discovery_v3_Resource_name(resource_wrapper)); |
| 364 |
} |
| 365 |
parser->ParseResource(context.arena, i, type_url, resource_name, |
| 366 |
serialized_resource); |
| 367 |
} |
| 368 |
return absl::OkStatus(); |
| 369 |
} |
| 370 |
|
| 371 |
namespace { |
| 372 |
|
| 373 |
void MaybeLogLrsRequest( |
| 374 |
const XdsApiContext& context, |
| 375 |
const envoy_service_load_stats_v3_LoadStatsRequest* request) { |
| 376 |
if (GRPC_TRACE_FLAG_ENABLED(*context.tracer) && |
| 377 |
gpr_should_log(GPR_LOG_SEVERITY_DEBUG)) { |
| 378 |
const upb_MessageDef* msg_type = |
| 379 |
envoy_service_load_stats_v3_LoadStatsRequest_getmsgdef(context.symtab); |
| 380 |
char buf[10240]; |
| 381 |
upb_TextEncode(request, msg_type, nullptr, 0, buf, sizeof(buf)); |
| 382 |
gpr_log(GPR_DEBUG, "[xds_client %p] constructed LRS request: %s", |
| 383 |
context.client, buf); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
std::string SerializeLrsRequest( |
| 388 |
const XdsApiContext& context, |
| 389 |
const envoy_service_load_stats_v3_LoadStatsRequest* request) { |
| 390 |
size_t output_length; |
| 391 |
char* output = envoy_service_load_stats_v3_LoadStatsRequest_serialize( |
| 392 |
request, context.arena, &output_length); |
| 393 |
return std::string(output, output_length); |
| 394 |
} |
| 395 |
|
| 396 |
} // namespace |
| 397 |
|
| 398 |
std::string XdsApi::CreateLrsInitialRequest() { |
| 399 |
upb::Arena arena; |
| 400 |
const XdsApiContext context = {client_, tracer_, symtab_->ptr(), arena.ptr()}; |
| 401 |
// Create a request. |
| 402 |
envoy_service_load_stats_v3_LoadStatsRequest* request = |
| 403 |
envoy_service_load_stats_v3_LoadStatsRequest_new(arena.ptr()); |
| 404 |
// Populate node. |
| 405 |
envoy_config_core_v3_Node* node_msg = |
| 406 |
envoy_service_load_stats_v3_LoadStatsRequest_mutable_node(request, |
| 407 |
arena.ptr()); |
| 408 |
PopulateNode(context, node_, user_agent_name_, user_agent_version_, node_msg); |
| 409 |
envoy_config_core_v3_Node_add_client_features( |
| 410 |
node_msg, |
| 411 |
upb_StringView_FromString("envoy.lrs.supports_send_all_clusters"), |
| 412 |
arena.ptr()); |
| 413 |
MaybeLogLrsRequest(context, request); |
| 414 |
return SerializeLrsRequest(context, request); |
| 415 |
} |
| 416 |
|
| 417 |
namespace { |
| 418 |
|
| 419 |
void LocalityStatsPopulate( |
| 420 |
const XdsApiContext& context, |
| 421 |
envoy_config_endpoint_v3_UpstreamLocalityStats* output, |
| 422 |
const XdsLocalityName& locality_name, |
| 423 |
const XdsClusterLocalityStats::Snapshot& snapshot) { |
| 424 |
// Set locality. |
| 425 |
envoy_config_core_v3_Locality* locality = |
| 426 |
envoy_config_endpoint_v3_UpstreamLocalityStats_mutable_locality( |
| 427 |
output, context.arena); |
| 428 |
if (!locality_name.region().empty()) { |
| 429 |
envoy_config_core_v3_Locality_set_region( |
| 430 |
locality, StdStringToUpbString(locality_name.region())); |
| 431 |
} |
| 432 |
if (!locality_name.zone().empty()) { |
| 433 |
envoy_config_core_v3_Locality_set_zone( |
| 434 |
locality, StdStringToUpbString(locality_name.zone())); |
| 435 |
} |
| 436 |
if (!locality_name.sub_zone().empty()) { |
| 437 |
envoy_config_core_v3_Locality_set_sub_zone( |
| 438 |
locality, StdStringToUpbString(locality_name.sub_zone())); |
| 439 |
} |
| 440 |
// Set total counts. |
| 441 |
envoy_config_endpoint_v3_UpstreamLocalityStats_set_total_successful_requests( |
| 442 |
output, snapshot.total_successful_requests); |
| 443 |
envoy_config_endpoint_v3_UpstreamLocalityStats_set_total_requests_in_progress( |
| 444 |
output, snapshot.total_requests_in_progress); |
| 445 |
envoy_config_endpoint_v3_UpstreamLocalityStats_set_total_error_requests( |
| 446 |
output, snapshot.total_error_requests); |
| 447 |
envoy_config_endpoint_v3_UpstreamLocalityStats_set_total_issued_requests( |
| 448 |
output, snapshot.total_issued_requests); |
| 449 |
// Add backend metrics. |
| 450 |
for (const auto& p : snapshot.backend_metrics) { |
| 451 |
const std::string& metric_name = p.first; |
| 452 |
const XdsClusterLocalityStats::BackendMetric& metric_value = p.second; |
| 453 |
envoy_config_endpoint_v3_EndpointLoadMetricStats* load_metric = |
| 454 |
envoy_config_endpoint_v3_UpstreamLocalityStats_add_load_metric_stats( |
| 455 |
output, context.arena); |
| 456 |
envoy_config_endpoint_v3_EndpointLoadMetricStats_set_metric_name( |
| 457 |
load_metric, StdStringToUpbString(metric_name)); |
| 458 |
envoy_config_endpoint_v3_EndpointLoadMetricStats_set_num_requests_finished_with_metric( |
| 459 |
load_metric, metric_value.num_requests_finished_with_metric); |
| 460 |
envoy_config_endpoint_v3_EndpointLoadMetricStats_set_total_metric_value( |
| 461 |
load_metric, metric_value.total_metric_value); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
} // namespace |
| 466 |
|
| 467 |
std::string XdsApi::CreateLrsRequest( |
| 468 |
ClusterLoadReportMap cluster_load_report_map) { |
| 469 |
upb::Arena arena; |
| 470 |
const XdsApiContext context = {client_, tracer_, symtab_->ptr(), arena.ptr()}; |
| 471 |
// Create a request. |
| 472 |
envoy_service_load_stats_v3_LoadStatsRequest* request = |
| 473 |
envoy_service_load_stats_v3_LoadStatsRequest_new(arena.ptr()); |
| 474 |
for (auto& p : cluster_load_report_map) { |
| 475 |
const std::string& cluster_name = p.first.first; |
| 476 |
const std::string& eds_service_name = p.first.second; |
| 477 |
const ClusterLoadReport& load_report = p.second; |
| 478 |
// Add cluster stats. |
| 479 |
envoy_config_endpoint_v3_ClusterStats* cluster_stats = |
| 480 |
envoy_service_load_stats_v3_LoadStatsRequest_add_cluster_stats( |
| 481 |
request, arena.ptr()); |
| 482 |
// Set the cluster name. |
| 483 |
envoy_config_endpoint_v3_ClusterStats_set_cluster_name( |
| 484 |
cluster_stats, StdStringToUpbString(cluster_name)); |
| 485 |
// Set EDS service name, if non-empty. |
| 486 |
if (!eds_service_name.empty()) { |
| 487 |
envoy_config_endpoint_v3_ClusterStats_set_cluster_service_name( |
| 488 |
cluster_stats, StdStringToUpbString(eds_service_name)); |
| 489 |
} |
| 490 |
// Add locality stats. |
| 491 |
for (const auto& p : load_report.locality_stats) { |
| 492 |
const XdsLocalityName& locality_name = *p.first; |
| 493 |
const auto& snapshot = p.second; |
| 494 |
envoy_config_endpoint_v3_UpstreamLocalityStats* locality_stats = |
| 495 |
envoy_config_endpoint_v3_ClusterStats_add_upstream_locality_stats( |
| 496 |
cluster_stats, arena.ptr()); |
| 497 |
LocalityStatsPopulate(context, locality_stats, locality_name, snapshot); |
| 498 |
} |
| 499 |
// Add dropped requests. |
| 500 |
uint64_t total_dropped_requests = 0; |
| 501 |
for (const auto& p : load_report.dropped_requests.categorized_drops) { |
| 502 |
const std::string& category = p.first; |
| 503 |
const uint64_t count = p.second; |
| 504 |
envoy_config_endpoint_v3_ClusterStats_DroppedRequests* dropped_requests = |
| 505 |
envoy_config_endpoint_v3_ClusterStats_add_dropped_requests( |
| 506 |
cluster_stats, arena.ptr()); |
| 507 |
envoy_config_endpoint_v3_ClusterStats_DroppedRequests_set_category( |
| 508 |
dropped_requests, StdStringToUpbString(category)); |
| 509 |
envoy_config_endpoint_v3_ClusterStats_DroppedRequests_set_dropped_count( |
| 510 |
dropped_requests, count); |
| 511 |
total_dropped_requests += count; |
| 512 |
} |
| 513 |
total_dropped_requests += load_report.dropped_requests.uncategorized_drops; |
| 514 |
// Set total dropped requests. |
| 515 |
envoy_config_endpoint_v3_ClusterStats_set_total_dropped_requests( |
| 516 |
cluster_stats, total_dropped_requests); |
| 517 |
// Set real load report interval. |
| 518 |
gpr_timespec timespec = load_report.load_report_interval.as_timespec(); |
| 519 |
google_protobuf_Duration* load_report_interval = |
| 520 |
envoy_config_endpoint_v3_ClusterStats_mutable_load_report_interval( |
| 521 |
cluster_stats, arena.ptr()); |
| 522 |
google_protobuf_Duration_set_seconds(load_report_interval, timespec.tv_sec); |
| 523 |
google_protobuf_Duration_set_nanos(load_report_interval, timespec.tv_nsec); |
| 524 |
} |
| 525 |
MaybeLogLrsRequest(context, request); |
| 526 |
return SerializeLrsRequest(context, request); |
| 527 |
} |
| 528 |
|
| 529 |
absl::Status XdsApi::ParseLrsResponse(absl::string_view encoded_response, |
| 530 |
bool* send_all_clusters, |
| 531 |
std::set<std::string>* cluster_names, |
| 532 |
Duration* load_reporting_interval) { |
| 533 |
upb::Arena arena; |
| 534 |
// Decode the response. |
| 535 |
const envoy_service_load_stats_v3_LoadStatsResponse* decoded_response = |
| 536 |
envoy_service_load_stats_v3_LoadStatsResponse_parse( |
| 537 |
encoded_response.data(), encoded_response.size(), arena.ptr()); |
| 538 |
// Parse the response. |
| 539 |
if (decoded_response == nullptr) { |
| 540 |
return absl::UnavailableError("Can't decode response."); |
| 541 |
} |
| 542 |
// Check send_all_clusters. |
| 543 |
if (envoy_service_load_stats_v3_LoadStatsResponse_send_all_clusters( |
| 544 |
decoded_response)) { |
| 545 |
*send_all_clusters = true; |
| 546 |
} else { |
| 547 |
// Store the cluster names. |
| 548 |
size_t size; |
| 549 |
const upb_StringView* clusters = |
| 550 |
envoy_service_load_stats_v3_LoadStatsResponse_clusters(decoded_response, |
| 551 |
&size); |
| 552 |
for (size_t i = 0; i < size; ++i) { |
| 553 |
cluster_names->emplace(UpbStringToStdString(clusters[i])); |
| 554 |
} |
| 555 |
} |
| 556 |
// Get the load report interval. |
| 557 |
const google_protobuf_Duration* load_reporting_interval_duration = |
| 558 |
envoy_service_load_stats_v3_LoadStatsResponse_load_reporting_interval( |
| 559 |
decoded_response); |
| 560 |
*load_reporting_interval = Duration::FromSecondsAndNanoseconds( |
| 561 |
google_protobuf_Duration_seconds(load_reporting_interval_duration), |
| 562 |
google_protobuf_Duration_nanos(load_reporting_interval_duration)); |
| 563 |
return absl::OkStatus(); |
| 564 |
} |
| 565 |
|
| 566 |
namespace { |
| 567 |
|
| 568 |
google_protobuf_Timestamp* EncodeTimestamp(const XdsApiContext& context, |
| 569 |
Timestamp value) { |
| 570 |
google_protobuf_Timestamp* timestamp = |
| 571 |
google_protobuf_Timestamp_new(context.arena); |
| 572 |
gpr_timespec timespec = value.as_timespec(GPR_CLOCK_REALTIME); |
| 573 |
google_protobuf_Timestamp_set_seconds(timestamp, timespec.tv_sec); |
| 574 |
google_protobuf_Timestamp_set_nanos(timestamp, timespec.tv_nsec); |
| 575 |
return timestamp; |
| 576 |
} |
| 577 |
|
| 578 |
} // namespace |
| 579 |
|
| 580 |
std::string XdsApi::AssembleClientConfig( |
| 581 |
const ResourceTypeMetadataMap& resource_type_metadata_map) { |
| 582 |
upb::Arena arena; |
| 583 |
// Create the ClientConfig for resource metadata from XdsClient |
| 584 |
auto* client_config = envoy_service_status_v3_ClientConfig_new(arena.ptr()); |
| 585 |
// Fill-in the node information |
| 586 |
auto* node = envoy_service_status_v3_ClientConfig_mutable_node(client_config, |
| 587 |
arena.ptr()); |
| 588 |
const XdsApiContext context = {client_, tracer_, symtab_->ptr(), arena.ptr()}; |
| 589 |
PopulateNode(context, node_, user_agent_name_, user_agent_version_, node); |
| 590 |
// Dump each resource. |
| 591 |
std::vector<std::string> type_url_storage; |
| 592 |
for (const auto& p : resource_type_metadata_map) { |
| 593 |
absl::string_view type_url = p.first; |
| 594 |
const ResourceMetadataMap& resource_metadata_map = p.second; |
| 595 |
type_url_storage.emplace_back( |
| 596 |
absl::StrCat("type.googleapis.com/", type_url)); |
| 597 |
for (const auto& q : resource_metadata_map) { |
| 598 |
absl::string_view resource_name = q.first; |
| 599 |
const ResourceMetadata& metadata = *q.second; |
| 600 |
auto* entry = |
| 601 |
envoy_service_status_v3_ClientConfig_add_generic_xds_configs( |
| 602 |
client_config, context.arena); |
| 603 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_type_url( |
| 604 |
entry, StdStringToUpbString(type_url_storage.back())); |
| 605 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_name( |
| 606 |
entry, StdStringToUpbString(resource_name)); |
| 607 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_client_status( |
| 608 |
entry, metadata.client_status); |
| 609 |
if (!metadata.serialized_proto.empty()) { |
| 610 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_version_info( |
| 611 |
entry, StdStringToUpbString(metadata.version)); |
| 612 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_last_updated( |
| 613 |
entry, EncodeTimestamp(context, metadata.update_time)); |
| 614 |
auto* any_field = |
| 615 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_mutable_xds_config( |
| 616 |
entry, context.arena); |
| 617 |
google_protobuf_Any_set_type_url( |
| 618 |
any_field, StdStringToUpbString(type_url_storage.back())); |
| 619 |
google_protobuf_Any_set_value( |
| 620 |
any_field, StdStringToUpbString(metadata.serialized_proto)); |
| 621 |
} |
| 622 |
if (metadata.client_status == XdsApi::ResourceMetadata::NACKED) { |
| 623 |
auto* update_failure_state = |
| 624 |
envoy_admin_v3_UpdateFailureState_new(context.arena); |
| 625 |
envoy_admin_v3_UpdateFailureState_set_details( |
| 626 |
update_failure_state, |
| 627 |
StdStringToUpbString(metadata.failed_details)); |
| 628 |
envoy_admin_v3_UpdateFailureState_set_version_info( |
| 629 |
update_failure_state, |
| 630 |
StdStringToUpbString(metadata.failed_version)); |
| 631 |
envoy_admin_v3_UpdateFailureState_set_last_update_attempt( |
| 632 |
update_failure_state, |
| 633 |
EncodeTimestamp(context, metadata.failed_update_time)); |
| 634 |
envoy_service_status_v3_ClientConfig_GenericXdsConfig_set_error_state( |
| 635 |
entry, update_failure_state); |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
// Serialize the upb message to bytes |
| 640 |
size_t output_length; |
| 641 |
char* output = envoy_service_status_v3_ClientConfig_serialize( |
| 642 |
client_config, arena.ptr(), &output_length); |
| 643 |
return std::string(output, output_length); |
| 644 |
} |
| 645 |
|
| 646 |
} // namespace grpc_core |