LB policy API: use UniqueTypeName for call attributes (#29747)

* implement UniqueTypeName API

* convert security code to use UniqueTypeName

* change subchannel data producer API to use UniqueTypeName

* sanitize

* add missing build dep

* fix credentials_test

* fix certificate_provider_store_test

* fix tls_security_connector_test

* attempt to fix windows build

* avoid unnecessary allocation

* work around MSVC 2017 bug

* sanity

* change factory to not be templated

* fix sanity

* fix bug in chttp2 connector that used server creds instead of channel creds

* add missing build dep

* LB policy API: use UniqueTypeName for call attributes

* Automated change: Fix sanity tests

* add missing build deps

* simplify API

* update to use new API

* add missing BUILD dep

* add comment

Co-authored-by: markdroth <markdroth@users.noreply.github.com>
pull/29842/head
Mark D. Roth 3 years ago committed by GitHub
parent 8558f46d35
commit b7699ef787
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      BUILD
  2. 29
      src/core/ext/filters/client_channel/client_channel.cc
  3. 17
      src/core/ext/filters/client_channel/client_channel.h
  4. 7
      src/core/ext/filters/client_channel/lb_policy.h
  5. 14
      src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc
  6. 5
      src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h
  7. 5
      src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc
  8. 11
      src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc
  9. 4
      src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h
  10. 3
      src/core/lib/service_config/service_config_call_data.h

@ -2807,6 +2807,7 @@ grpc_cc_library(
"ref_counted_ptr", "ref_counted_ptr",
"service_config_parser", "service_config_parser",
"slice_refcount", "slice_refcount",
"unique_type_name",
], ],
) )
@ -4153,6 +4154,7 @@ grpc_cc_library(
"ref_counted_ptr", "ref_counted_ptr",
"server_address", "server_address",
"sockaddr_utils", "sockaddr_utils",
"unique_type_name",
], ],
) )
@ -4704,6 +4706,9 @@ grpc_cc_library(
hdrs = [ hdrs = [
"src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h", "src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h",
], ],
external_deps = [
"absl/strings",
],
language = "c++", language = "c++",
) )
@ -4749,6 +4754,7 @@ grpc_cc_library(
"ref_counted_ptr", "ref_counted_ptr",
"server_address", "server_address",
"time", "time",
"unique_type_name",
"uri_parser", "uri_parser",
"useful", "useful",
], ],

@ -2540,25 +2540,16 @@ class ClientChannel::LoadBalancedCall::Metadata
// ClientChannel::LoadBalancedCall::LbCallState // ClientChannel::LoadBalancedCall::LbCallState
// //
class ClientChannel::LoadBalancedCall::LbCallState absl::string_view
: public LoadBalancingPolicy::CallState { ClientChannel::LoadBalancedCall::LbCallState::GetCallAttribute(
public: UniqueTypeName type) {
explicit LbCallState(LoadBalancedCall* lb_call) : lb_call_(lb_call) {} auto* service_config_call_data = static_cast<ServiceConfigCallData*>(
lb_call_->call_context_[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value);
void* Alloc(size_t size) override { return lb_call_->arena_->Alloc(size); } auto& call_attributes = service_config_call_data->call_attributes();
auto it = call_attributes.find(type);
absl::string_view ExperimentalGetCallAttribute(const char* key) override { if (it == call_attributes.end()) return absl::string_view();
auto* service_config_call_data = static_cast<ServiceConfigCallData*>( return it->second;
lb_call_->call_context_[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value); }
auto& call_attributes = service_config_call_data->call_attributes();
auto it = call_attributes.find(key);
if (it == call_attributes.end()) return absl::string_view();
return it->second;
}
private:
LoadBalancedCall* lb_call_;
};
// //
// ClientChannel::LoadBalancedCall::BackendMetricAccessor // ClientChannel::LoadBalancedCall::BackendMetricAccessor

@ -30,6 +30,7 @@
#include "absl/base/thread_annotations.h" #include "absl/base/thread_annotations.h"
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include <grpc/impl/codegen/connectivity_state.h> #include <grpc/impl/codegen/connectivity_state.h>
@ -54,6 +55,7 @@
#include "src/core/lib/gprpp/ref_counted_ptr.h" #include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/sync.h" #include "src/core/lib/gprpp/sync.h"
#include "src/core/lib/gprpp/time.h" #include "src/core/lib/gprpp/time.h"
#include "src/core/lib/gprpp/unique_type_name.h"
#include "src/core/lib/iomgr/call_combiner.h" #include "src/core/lib/iomgr/call_combiner.h"
#include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/closure.h"
#include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/error.h"
@ -382,6 +384,20 @@ class ClientChannel {
class ClientChannel::LoadBalancedCall class ClientChannel::LoadBalancedCall
: public InternallyRefCounted<LoadBalancedCall, kUnrefCallDtor> { : public InternallyRefCounted<LoadBalancedCall, kUnrefCallDtor> {
public: public:
class LbCallState : public LoadBalancingPolicy::CallState {
public:
explicit LbCallState(LoadBalancedCall* lb_call) : lb_call_(lb_call) {}
void* Alloc(size_t size) override { return lb_call_->arena_->Alloc(size); }
// Internal API to allow first-party LB policies to access per-call
// attributes set by the ConfigSelector.
absl::string_view GetCallAttribute(UniqueTypeName type);
private:
LoadBalancedCall* lb_call_;
};
// If on_call_destruction_complete is non-null, then it will be // If on_call_destruction_complete is non-null, then it will be
// invoked once the LoadBalancedCall is completely destroyed. // invoked once the LoadBalancedCall is completely destroyed.
// If it is null, then the caller is responsible for checking whether // If it is null, then the caller is responsible for checking whether
@ -417,7 +433,6 @@ class ClientChannel::LoadBalancedCall
private: private:
class LbQueuedCallCanceller; class LbQueuedCallCanceller;
class Metadata; class Metadata;
class LbCallState;
class BackendMetricAccessor; class BackendMetricAccessor;
// Returns the index into pending_batches_ to be used for batch. // Returns the index into pending_batches_ to be used for batch.

@ -106,13 +106,6 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
/// It is more efficient to use this than to allocate memory directly /// It is more efficient to use this than to allocate memory directly
/// for allocations that need to be made on a per-call basis. /// for allocations that need to be made on a per-call basis.
virtual void* Alloc(size_t size) = 0; virtual void* Alloc(size_t size) = 0;
/// EXPERIMENTAL API.
/// Returns the value of the call attribute \a key.
/// Keys are static strings, so an attribute can be accessed by an LB
/// policy implementation only if it knows about the internal key.
/// Returns a null string_view if key not found.
virtual absl::string_view ExperimentalGetCallAttribute(const char* key) = 0;
}; };
/// Interface for accessing metadata. /// Interface for accessing metadata.

@ -38,6 +38,8 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/types/optional.h" #include "absl/types/optional.h"
#include "src/core/lib/gprpp/unique_type_name.h"
#define XXH_INLINE_ALL #define XXH_INLINE_ALL
#include "xxhash.h" #include "xxhash.h"
@ -45,6 +47,7 @@
#include <grpc/impl/codegen/grpc_types.h> #include <grpc/impl/codegen/grpc_types.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/client_channel.h"
#include "src/core/ext/filters/client_channel/lb_policy.h" #include "src/core/ext/filters/client_channel/lb_policy.h"
#include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h" #include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h"
#include "src/core/ext/filters/client_channel/lb_policy_factory.h" #include "src/core/ext/filters/client_channel/lb_policy_factory.h"
@ -67,9 +70,13 @@
namespace grpc_core { namespace grpc_core {
const char* kRequestRingHashAttribute = "request_ring_hash";
TraceFlag grpc_lb_ring_hash_trace(false, "ring_hash_lb"); TraceFlag grpc_lb_ring_hash_trace(false, "ring_hash_lb");
UniqueTypeName RequestHashAttributeName() {
static UniqueTypeName::Factory kFactory("request_hash");
return kFactory.Create();
}
// Helper Parser method // Helper Parser method
void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size, void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size,
size_t* max_ring_size, size_t* max_ring_size,
@ -443,8 +450,9 @@ RingHash::Ring::Ring(RingHash* parent,
// //
RingHash::PickResult RingHash::Picker::Pick(PickArgs args) { RingHash::PickResult RingHash::Picker::Pick(PickArgs args) {
auto hash = auto* call_state = static_cast<ClientChannel::LoadBalancedCall::LbCallState*>(
args.call_state->ExperimentalGetCallAttribute(kRequestRingHashAttribute); args.call_state);
auto hash = call_state->GetCallAttribute(RequestHashAttributeName());
uint64_t h; uint64_t h;
if (!absl::SimpleAtoi(hash, &h)) { if (!absl::SimpleAtoi(hash, &h)) {
return PickResult::Fail( return PickResult::Fail(

@ -23,17 +23,20 @@
#include <vector> #include <vector>
#include "src/core/lib/gprpp/unique_type_name.h"
#include "src/core/lib/iomgr/error.h" #include "src/core/lib/iomgr/error.h"
#include "src/core/lib/json/json.h" #include "src/core/lib/json/json.h"
namespace grpc_core { namespace grpc_core {
extern const char* kRequestRingHashAttribute;
UniqueTypeName RequestHashAttributeName();
// Helper Parsing method to parse ring hash policy configs; for example, ring // Helper Parsing method to parse ring hash policy configs; for example, ring
// hash size validity. // hash size validity.
void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size, void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size,
size_t* max_ring_size, size_t* max_ring_size,
std::vector<grpc_error_handle>* error_list); std::vector<grpc_error_handle>* error_list);
} // namespace grpc_core } // namespace grpc_core
#endif // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_RING_HASH_RING_HASH_H #endif // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_RING_HASH_RING_HASH_H

@ -36,6 +36,7 @@
#include <grpc/impl/codegen/grpc_types.h> #include <grpc/impl/codegen/grpc_types.h>
#include <grpc/support/log.h> #include <grpc/support/log.h>
#include "src/core/ext/filters/client_channel/client_channel.h"
#include "src/core/ext/filters/client_channel/lb_policy.h" #include "src/core/ext/filters/client_channel/lb_policy.h"
#include "src/core/ext/filters/client_channel/lb_policy/child_policy_handler.h" #include "src/core/ext/filters/client_channel/lb_policy/child_policy_handler.h"
#include "src/core/ext/filters/client_channel/lb_policy_factory.h" #include "src/core/ext/filters/client_channel/lb_policy_factory.h"
@ -228,8 +229,10 @@ class XdsClusterManagerLb : public LoadBalancingPolicy {
XdsClusterManagerLb::PickResult XdsClusterManagerLb::ClusterPicker::Pick( XdsClusterManagerLb::PickResult XdsClusterManagerLb::ClusterPicker::Pick(
PickArgs args) { PickArgs args) {
auto* call_state = static_cast<ClientChannel::LoadBalancedCall::LbCallState*>(
args.call_state);
auto cluster_name = auto cluster_name =
args.call_state->ExperimentalGetCallAttribute(kXdsClusterAttribute); call_state->GetCallAttribute(XdsClusterAttributeTypeName());
auto it = cluster_map_.find(cluster_name); auto it = cluster_map_.find(cluster_name);
if (it != cluster_map_.end()) { if (it != cluster_map_.end()) {
return it->second->Pick(args); return it->second->Pick(args);

@ -45,6 +45,8 @@
#include "absl/types/variant.h" #include "absl/types/variant.h"
#include "re2/re2.h" #include "re2/re2.h"
#include "src/core/lib/gprpp/unique_type_name.h"
#define XXH_INLINE_ALL #define XXH_INLINE_ALL
#include "xxhash.h" #include "xxhash.h"
@ -94,7 +96,10 @@ namespace grpc_core {
TraceFlag grpc_xds_resolver_trace(false, "xds_resolver"); TraceFlag grpc_xds_resolver_trace(false, "xds_resolver");
const char* kXdsClusterAttribute = "xds_cluster_name"; UniqueTypeName XdsClusterAttributeTypeName() {
static UniqueTypeName::Factory kFactory("xds_cluster_name");
return kFactory.Create();
}
namespace { namespace {
@ -764,13 +769,13 @@ ConfigSelector::CallConfig XdsResolver::XdsConfigSelector::GetCallConfig(
method_config->GetMethodParsedConfigVector(grpc_empty_slice()); method_config->GetMethodParsedConfigVector(grpc_empty_slice());
call_config.service_config = std::move(method_config); call_config.service_config = std::move(method_config);
} }
call_config.call_attributes[kXdsClusterAttribute] = it->first; call_config.call_attributes[XdsClusterAttributeTypeName()] = it->first;
std::string hash_string = absl::StrCat(hash.value()); std::string hash_string = absl::StrCat(hash.value());
char* hash_value = char* hash_value =
static_cast<char*>(args.arena->Alloc(hash_string.size() + 1)); static_cast<char*>(args.arena->Alloc(hash_string.size() + 1));
memcpy(hash_value, hash_string.c_str(), hash_string.size()); memcpy(hash_value, hash_string.c_str(), hash_string.size());
hash_value[hash_string.size()] = '\0'; hash_value[hash_string.size()] = '\0';
call_config.call_attributes[kRequestRingHashAttribute] = hash_value; call_config.call_attributes[RequestHashAttributeName()] = hash_value;
call_config.call_dispatch_controller = call_config.call_dispatch_controller =
args.arena->New<XdsCallDispatchController>(it->second->Ref()); args.arena->New<XdsCallDispatchController>(it->second->Ref());
return call_config; return call_config;

@ -19,9 +19,11 @@
#include <grpc/support/port_platform.h> #include <grpc/support/port_platform.h>
#include "src/core/lib/gprpp/unique_type_name.h"
namespace grpc_core { namespace grpc_core {
extern const char* kXdsClusterAttribute; UniqueTypeName XdsClusterAttributeTypeName();
} // namespace grpc_core } // namespace grpc_core

@ -28,6 +28,7 @@
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h" #include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/gprpp/unique_type_name.h"
#include "src/core/lib/service_config/service_config.h" #include "src/core/lib/service_config/service_config.h"
#include "src/core/lib/service_config/service_config_parser.h" #include "src/core/lib/service_config/service_config_parser.h"
@ -39,7 +40,7 @@ namespace grpc_core {
/// easily access method and global parameters for the call. /// easily access method and global parameters for the call.
class ServiceConfigCallData { class ServiceConfigCallData {
public: public:
using CallAttributes = std::map<const char*, absl::string_view>; using CallAttributes = std::map<UniqueTypeName, absl::string_view>;
ServiceConfigCallData() : method_configs_(nullptr) {} ServiceConfigCallData() : method_configs_(nullptr) {}

Loading…
Cancel
Save