Skip to content

Commit

Permalink
Native source sync at revision @c7cb8a8
Browse files Browse the repository at this point in the history
  • Loading branch information
dennycd authored and HannahShiSFB committed Dec 28, 2024
1 parent c7cb8a8 commit 32c56c6
Show file tree
Hide file tree
Showing 13 changed files with 641 additions and 763 deletions.
2 changes: 1 addition & 1 deletion gRPC-C++.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Pod::Spec.new do |s|
# build.
'USE_HEADERMAP' => 'NO',
'ALWAYS_SEARCH_USER_PATHS' => 'NO',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
}

s.libraries = 'c++'
Expand Down
2 changes: 1 addition & 1 deletion gRPC-Core.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ Pod::Spec.new do |s|
'ALWAYS_SEARCH_USER_PATHS' => 'NO',
'GCC_PREPROCESSOR_DEFINITIONS' => '"$(inherited)" "COCOAPODS=1"',
'CLANG_WARN_STRICT_PROTOTYPES' => 'NO',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
}

s.default_subspecs = 'Interface', 'Implementation'
Expand Down
2 changes: 1 addition & 1 deletion gRPC-ProtoRPC.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,6 @@ Pod::Spec.new do |s|
# This is needed by all pods that depend on gRPC-RxLibrary:
'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES',
'CLANG_WARN_STRICT_PROTOTYPES' => 'NO',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
}
end
2 changes: 1 addition & 1 deletion gRPC-RxLibrary.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ Pod::Spec.new do |s|

s.pod_target_xcconfig = {
'CLANG_WARN_STRICT_PROTOTYPES' => 'NO',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
}
end
2 changes: 1 addition & 1 deletion gRPC.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Pod::Spec.new do |s|
# This is needed by all pods that depend on gRPC-RxLibrary:
'CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES' => 'YES',
'CLANG_WARN_STRICT_PROTOTYPES' => 'NO',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++14',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17',
}

s.ios.deployment_target = '11.0'
Expand Down
2 changes: 2 additions & 0 deletions src/core/lib/promise/activity.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ namespace grpc_core {
///////////////////////////////////////////////////////////////////////////////
// GLOBALS

#if !defined(_WIN32) || !defined(_DLL)
thread_local Activity* Activity::g_current_activity_{nullptr};
#endif

namespace promise_detail {

Expand Down
25 changes: 18 additions & 7 deletions src/core/lib/promise/activity.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class Activity : public Orphanable {
// locked
// - back up that assertion with a runtime check in debug builds (it's
// prohibitively expensive in non-debug builds)
static Activity* current() { return g_current_activity_; }
static Activity* current() { return current_ref(); }

// Produce an activity-owning Waker. The produced waker will keep the activity
// alive until it's awoken or dropped.
Expand All @@ -232,17 +232,16 @@ class Activity : public Orphanable {
protected:
// Check if this activity is the current activity executing on the current
// thread.
bool is_current() const { return this == g_current_activity_; }
bool is_current() const { return this == current(); }
// Check if there is an activity executing on the current thread.
static bool have_current() { return g_current_activity_ != nullptr; }
static bool have_current() { return current() != nullptr; }
// Set the current activity at construction, clean it up at destruction.
class ScopedActivity {
public:
explicit ScopedActivity(Activity* activity)
: prior_activity_(g_current_activity_) {
g_current_activity_ = activity;
explicit ScopedActivity(Activity* activity) : prior_activity_(current()) {
current_ref() = activity;
}
~ScopedActivity() { g_current_activity_ = prior_activity_; }
~ScopedActivity() { current_ref() = prior_activity_; }
ScopedActivity(const ScopedActivity&) = delete;
ScopedActivity& operator=(const ScopedActivity&) = delete;

Expand All @@ -251,9 +250,21 @@ class Activity : public Orphanable {
};

private:
static Activity*& current_ref() {
#if !defined(_WIN32) || !defined(_DLL)
return g_current_activity_;
#else
// Set during RunLoop to the Activity that's executing.
// Being set implies that mu_ is held.
static thread_local Activity* current_activity;
return current_activity;
#endif
}
#if !defined(_WIN32) || !defined(_DLL)
// Set during RunLoop to the Activity that's executing.
// Being set implies that mu_ is held.
static thread_local Activity* g_current_activity_;
#endif
};

// Owned pointer to one Activity.
Expand Down
2 changes: 0 additions & 2 deletions src/core/plugin_registry/grpc_plugin_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ extern void RegisterOutlierDetectionLbPolicy(
CoreConfiguration::Builder* builder);
extern void RegisterWeightedTargetLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterPickFirstLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterRingHashLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterRoundRobinLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterWeightedRoundRobinLbPolicy(
CoreConfiguration::Builder* builder);
Expand Down Expand Up @@ -103,7 +102,6 @@ void BuildCoreConfiguration(CoreConfiguration::Builder* builder) {
RegisterWeightedTargetLbPolicy(builder);
RegisterPickFirstLbPolicy(builder);
RegisterRoundRobinLbPolicy(builder);
RegisterRingHashLbPolicy(builder);
RegisterWeightedRoundRobinLbPolicy(builder);
BuildClientChannelConfiguration(builder);
SecurityRegisterHandshakerFactories(builder);
Expand Down
2 changes: 2 additions & 0 deletions src/core/plugin_registry/grpc_plugin_registry_extra.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ extern void RegisterCdsLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterXdsOverrideHostLbPolicy(
CoreConfiguration::Builder* builder);
extern void RegisterXdsWrrLocalityLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterRingHashLbPolicy(CoreConfiguration::Builder* builder);
extern void RegisterFileWatcherCertificateProvider(
CoreConfiguration::Builder* builder);
extern void RegisterXdsHttpProxyMapper(CoreConfiguration::Builder* builder);
Expand All @@ -59,6 +60,7 @@ void RegisterExtraFilters(CoreConfiguration::Builder* builder) {
RegisterCdsLbPolicy(builder);
RegisterXdsOverrideHostLbPolicy(builder);
RegisterXdsWrrLocalityLbPolicy(builder);
RegisterRingHashLbPolicy(builder);
RegisterFileWatcherCertificateProvider(builder);
RegisterXdsHttpProxyMapper(builder);
#endif
Expand Down
223 changes: 0 additions & 223 deletions src/core/xds/xds_client/xds_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,16 @@

#include "src/core/xds/xds_client/xds_api.h"

#include <grpc/status.h>
#include <grpc/support/port_platform.h>
#include <grpc/support/time.h>
#include <stdint.h>
#include <stdlib.h>

#include <set>
#include <string>
#include <vector>

#include "absl/log/log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/strip.h"
#include "envoy/config/core/v3/base.upb.h"
#include "envoy/config/endpoint/v3/load_report.upb.h"
#include "envoy/service/discovery/v3/discovery.upb.h"
#include "envoy/service/discovery/v3/discovery.upbdefs.h"
#include "envoy/service/load_stats/v3/lrs.upb.h"
#include "envoy/service/load_stats/v3/lrs.upbdefs.h"
#include "envoy/service/status/v3/csds.upb.h"
#include "google/protobuf/any.upb.h"
#include "google/protobuf/duration.upb.h"
#include "google/protobuf/struct.upb.h"
#include "google/protobuf/timestamp.upb.h"
#include "google/rpc/status.upb.h"
#include "src/core/util/json/json.h"
#include "src/core/util/upb_utils.h"
#include "src/core/xds/xds_client/xds_client.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.hpp"
#include "upb/reflection/def.h"
#include "upb/text/encode.h"

// IWYU pragma: no_include "upb/msg_internal.h"

namespace grpc_core {

XdsApi::XdsApi(XdsClient* client, TraceFlag* tracer,
const XdsBootstrap::Node* node, upb::DefPool* def_pool,
std::string user_agent_name, std::string user_agent_version)
: client_(client),
tracer_(tracer),
node_(node),
def_pool_(def_pool),
user_agent_name_(std::move(user_agent_name)),
user_agent_version_(std::move(user_agent_version)) {}

namespace {

struct XdsApiContext {
XdsClient* client;
TraceFlag* tracer;
upb_DefPool* def_pool;
upb_Arena* arena;
};

void PopulateMetadataValue(google_protobuf_Value* value_pb, const Json& value,
upb_Arena* arena);

Expand Down Expand Up @@ -125,37 +79,8 @@ void PopulateMetadataValue(google_protobuf_Value* value_pb, const Json& value,
}
}

void MaybeLogDiscoveryRequest(
const XdsApiContext& context,
const envoy_service_discovery_v3_DiscoveryRequest* request) {
if (GRPC_TRACE_FLAG_ENABLED_OBJ(*context.tracer) && ABSL_VLOG_IS_ON(2)) {
const upb_MessageDef* msg_type =
envoy_service_discovery_v3_DiscoveryRequest_getmsgdef(context.def_pool);
char buf[10240];
upb_TextEncode(reinterpret_cast<const upb_Message*>(request), msg_type,
nullptr, 0, buf, sizeof(buf));
VLOG(2) << "[xds_client " << context.client
<< "] constructed ADS request: " << buf;
}
}

std::string SerializeDiscoveryRequest(
const XdsApiContext& context,
envoy_service_discovery_v3_DiscoveryRequest* request) {
size_t output_length;
char* output = envoy_service_discovery_v3_DiscoveryRequest_serialize(
request, context.arena, &output_length);
return std::string(output, output_length);
}

} // namespace

void XdsApi::PopulateNode(envoy_config_core_v3_Node* node_msg,
upb_Arena* arena) {
PopulateXdsNode(node_, user_agent_name_, user_agent_version_, node_msg,
arena);
}

void PopulateXdsNode(const XdsBootstrap::Node* node,
absl::string_view user_agent_name,
absl::string_view user_agent_version,
Expand Down Expand Up @@ -202,152 +127,4 @@ void PopulateXdsNode(const XdsBootstrap::Node* node,
arena);
}

std::string XdsApi::CreateAdsRequest(
absl::string_view type_url, absl::string_view version,
absl::string_view nonce, const std::vector<std::string>& resource_names,
absl::Status status, bool populate_node) {
upb::Arena arena;
const XdsApiContext context = {client_, tracer_, def_pool_->ptr(),
arena.ptr()};
// Create a request.
envoy_service_discovery_v3_DiscoveryRequest* request =
envoy_service_discovery_v3_DiscoveryRequest_new(arena.ptr());
// Set type_url.
std::string type_url_str = absl::StrCat("type.googleapis.com/", type_url);
envoy_service_discovery_v3_DiscoveryRequest_set_type_url(
request, StdStringToUpbString(type_url_str));
// Set version_info.
if (!version.empty()) {
envoy_service_discovery_v3_DiscoveryRequest_set_version_info(
request, StdStringToUpbString(version));
}
// Set nonce.
if (!nonce.empty()) {
envoy_service_discovery_v3_DiscoveryRequest_set_response_nonce(
request, StdStringToUpbString(nonce));
}
// Set error_detail if it's a NACK.
std::string error_string_storage;
if (!status.ok()) {
google_rpc_Status* error_detail =
envoy_service_discovery_v3_DiscoveryRequest_mutable_error_detail(
request, arena.ptr());
// Hard-code INVALID_ARGUMENT as the status code.
// TODO(roth): If at some point we decide we care about this value,
// we could attach a status code to the individual errors where we
// generate them in the parsing code, and then use that here.
google_rpc_Status_set_code(error_detail, GRPC_STATUS_INVALID_ARGUMENT);
// Error description comes from the status that was passed in.
error_string_storage = std::string(status.message());
upb_StringView error_description =
StdStringToUpbString(error_string_storage);
google_rpc_Status_set_message(error_detail, error_description);
}
// Populate node.
if (populate_node) {
envoy_config_core_v3_Node* node_msg =
envoy_service_discovery_v3_DiscoveryRequest_mutable_node(request,
arena.ptr());
PopulateNode(node_msg, arena.ptr());
envoy_config_core_v3_Node_add_client_features(
node_msg, upb_StringView_FromString("xds.config.resource-in-sotw"),
context.arena);
}
// Add resource_names.
for (const std::string& resource_name : resource_names) {
envoy_service_discovery_v3_DiscoveryRequest_add_resource_names(
request, StdStringToUpbString(resource_name), arena.ptr());
}
MaybeLogDiscoveryRequest(context, request);
return SerializeDiscoveryRequest(context, request);
}

namespace {

void MaybeLogDiscoveryResponse(
const XdsApiContext& context,
const envoy_service_discovery_v3_DiscoveryResponse* response) {
if (GRPC_TRACE_FLAG_ENABLED_OBJ(*context.tracer) && ABSL_VLOG_IS_ON(2)) {
const upb_MessageDef* msg_type =
envoy_service_discovery_v3_DiscoveryResponse_getmsgdef(
context.def_pool);
char buf[10240];
upb_TextEncode(reinterpret_cast<const upb_Message*>(response), msg_type,
nullptr, 0, buf, sizeof(buf));
VLOG(2) << "[xds_client " << context.client
<< "] received response: " << buf;
}
}

} // namespace

absl::Status XdsApi::ParseAdsResponse(absl::string_view encoded_response,
AdsResponseParserInterface* parser) {
upb::Arena arena;
const XdsApiContext context = {client_, tracer_, def_pool_->ptr(),
arena.ptr()};
// Decode the response.
const envoy_service_discovery_v3_DiscoveryResponse* response =
envoy_service_discovery_v3_DiscoveryResponse_parse(
encoded_response.data(), encoded_response.size(), arena.ptr());
// If decoding fails, report a fatal error and return.
if (response == nullptr) {
return absl::InvalidArgumentError("Can't decode DiscoveryResponse.");
}
MaybeLogDiscoveryResponse(context, response);
// Report the type_url, version, nonce, and number of resources to the parser.
AdsResponseParserInterface::AdsResponseFields fields;
fields.type_url = std::string(absl::StripPrefix(
UpbStringToAbsl(
envoy_service_discovery_v3_DiscoveryResponse_type_url(response)),
"type.googleapis.com/"));
fields.version = UpbStringToStdString(
envoy_service_discovery_v3_DiscoveryResponse_version_info(response));
fields.nonce = UpbStringToStdString(
envoy_service_discovery_v3_DiscoveryResponse_nonce(response));
size_t num_resources;
const google_protobuf_Any* const* resources =
envoy_service_discovery_v3_DiscoveryResponse_resources(response,
&num_resources);
fields.num_resources = num_resources;
absl::Status status = parser->ProcessAdsResponseFields(std::move(fields));
if (!status.ok()) return status;
// Process each resource.
for (size_t i = 0; i < num_resources; ++i) {
absl::string_view type_url = absl::StripPrefix(
UpbStringToAbsl(google_protobuf_Any_type_url(resources[i])),
"type.googleapis.com/");
absl::string_view serialized_resource =
UpbStringToAbsl(google_protobuf_Any_value(resources[i]));
// Unwrap Resource messages, if so wrapped.
absl::string_view resource_name;
if (type_url == "envoy.service.discovery.v3.Resource") {
const auto* resource_wrapper = envoy_service_discovery_v3_Resource_parse(
serialized_resource.data(), serialized_resource.size(), arena.ptr());
if (resource_wrapper == nullptr) {
parser->ResourceWrapperParsingFailed(
i, "Can't decode Resource proto wrapper");
continue;
}
const auto* resource =
envoy_service_discovery_v3_Resource_resource(resource_wrapper);
if (resource == nullptr) {
parser->ResourceWrapperParsingFailed(
i, "No resource present in Resource proto wrapper");
continue;
}
type_url = absl::StripPrefix(
UpbStringToAbsl(google_protobuf_Any_type_url(resource)),
"type.googleapis.com/");
serialized_resource =
UpbStringToAbsl(google_protobuf_Any_value(resource));
resource_name = UpbStringToAbsl(
envoy_service_discovery_v3_Resource_name(resource_wrapper));
}
parser->ParseResource(context.arena, i, type_url, resource_name,
serialized_resource);
}
return absl::OkStatus();
}

} // namespace grpc_core
Loading

0 comments on commit 32c56c6

Please sign in to comment.