diff --git a/BUILD b/BUILD index 315271cd692..d6089ed4eaa 100644 --- a/BUILD +++ b/BUILD @@ -2807,6 +2807,7 @@ grpc_cc_library( "ref_counted_ptr", "service_config_parser", "slice_refcount", + "unique_type_name", ], ) @@ -4153,6 +4154,7 @@ grpc_cc_library( "ref_counted_ptr", "server_address", "sockaddr_utils", + "unique_type_name", ], ) @@ -4704,6 +4706,9 @@ grpc_cc_library( hdrs = [ "src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h", ], + external_deps = [ + "absl/strings", + ], language = "c++", ) @@ -4749,6 +4754,7 @@ grpc_cc_library( "ref_counted_ptr", "server_address", "time", + "unique_type_name", "uri_parser", "useful", ], diff --git a/src/core/ext/filters/client_channel/client_channel.cc b/src/core/ext/filters/client_channel/client_channel.cc index a8eb032800e..86cbb058fba 100644 --- a/src/core/ext/filters/client_channel/client_channel.cc +++ b/src/core/ext/filters/client_channel/client_channel.cc @@ -2540,25 +2540,16 @@ class ClientChannel::LoadBalancedCall::Metadata // ClientChannel::LoadBalancedCall::LbCallState // -class ClientChannel::LoadBalancedCall::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); } - - absl::string_view ExperimentalGetCallAttribute(const char* key) override { - auto* service_config_call_data = static_cast( - 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_; -}; +absl::string_view +ClientChannel::LoadBalancedCall::LbCallState::GetCallAttribute( + UniqueTypeName type) { + auto* service_config_call_data = static_cast( + 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(type); + if (it == call_attributes.end()) return absl::string_view(); + return it->second; +} // // ClientChannel::LoadBalancedCall::BackendMetricAccessor diff --git a/src/core/ext/filters/client_channel/client_channel.h b/src/core/ext/filters/client_channel/client_channel.h index c18eabfd27a..16efd351547 100644 --- a/src/core/ext/filters/client_channel/client_channel.h +++ b/src/core/ext/filters/client_channel/client_channel.h @@ -30,6 +30,7 @@ #include "absl/base/thread_annotations.h" #include "absl/status/status.h" +#include "absl/strings/string_view.h" #include "absl/types/optional.h" #include @@ -54,6 +55,7 @@ #include "src/core/lib/gprpp/ref_counted_ptr.h" #include "src/core/lib/gprpp/sync.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/closure.h" #include "src/core/lib/iomgr/error.h" @@ -382,6 +384,20 @@ class ClientChannel { class ClientChannel::LoadBalancedCall : public InternallyRefCounted { 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 // invoked once the LoadBalancedCall is completely destroyed. // If it is null, then the caller is responsible for checking whether @@ -417,7 +433,6 @@ class ClientChannel::LoadBalancedCall private: class LbQueuedCallCanceller; class Metadata; - class LbCallState; class BackendMetricAccessor; // Returns the index into pending_batches_ to be used for batch. diff --git a/src/core/ext/filters/client_channel/lb_policy.h b/src/core/ext/filters/client_channel/lb_policy.h index 3cd542b50f6..ba054540c58 100644 --- a/src/core/ext/filters/client_channel/lb_policy.h +++ b/src/core/ext/filters/client_channel/lb_policy.h @@ -106,13 +106,6 @@ class LoadBalancingPolicy : public InternallyRefCounted { /// It is more efficient to use this than to allocate memory directly /// for allocations that need to be made on a per-call basis. 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. diff --git a/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc b/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc index 94f0b1863ba..ae0f074c960 100644 --- a/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc +++ b/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc @@ -38,6 +38,8 @@ #include "absl/strings/string_view.h" #include "absl/types/optional.h" +#include "src/core/lib/gprpp/unique_type_name.h" + #define XXH_INLINE_ALL #include "xxhash.h" @@ -45,6 +47,7 @@ #include #include +#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/subchannel_list.h" #include "src/core/ext/filters/client_channel/lb_policy_factory.h" @@ -67,9 +70,13 @@ namespace grpc_core { -const char* kRequestRingHashAttribute = "request_ring_hash"; TraceFlag grpc_lb_ring_hash_trace(false, "ring_hash_lb"); +UniqueTypeName RequestHashAttributeName() { + static UniqueTypeName::Factory kFactory("request_hash"); + return kFactory.Create(); +} + // Helper Parser method void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size, size_t* max_ring_size, @@ -443,8 +450,9 @@ RingHash::Ring::Ring(RingHash* parent, // RingHash::PickResult RingHash::Picker::Pick(PickArgs args) { - auto hash = - args.call_state->ExperimentalGetCallAttribute(kRequestRingHashAttribute); + auto* call_state = static_cast( + args.call_state); + auto hash = call_state->GetCallAttribute(RequestHashAttributeName()); uint64_t h; if (!absl::SimpleAtoi(hash, &h)) { return PickResult::Fail( diff --git a/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h b/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h index 43bb39e94db..fdc0b10278b 100644 --- a/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h +++ b/src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h @@ -23,17 +23,20 @@ #include +#include "src/core/lib/gprpp/unique_type_name.h" #include "src/core/lib/iomgr/error.h" #include "src/core/lib/json/json.h" namespace grpc_core { -extern const char* kRequestRingHashAttribute; + +UniqueTypeName RequestHashAttributeName(); // Helper Parsing method to parse ring hash policy configs; for example, ring // hash size validity. void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size, size_t* max_ring_size, std::vector* error_list); + } // namespace grpc_core #endif // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_RING_HASH_RING_HASH_H diff --git a/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc b/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc index 40e8abacb93..ad50eede9fe 100644 --- a/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc +++ b/src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc @@ -36,6 +36,7 @@ #include #include +#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/child_policy_handler.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( PickArgs args) { + auto* call_state = static_cast( + args.call_state); auto cluster_name = - args.call_state->ExperimentalGetCallAttribute(kXdsClusterAttribute); + call_state->GetCallAttribute(XdsClusterAttributeTypeName()); auto it = cluster_map_.find(cluster_name); if (it != cluster_map_.end()) { return it->second->Pick(args); diff --git a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc index 7dfe637d61c..bfbb0c3c13c 100644 --- a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc +++ b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc @@ -45,6 +45,8 @@ #include "absl/types/variant.h" #include "re2/re2.h" +#include "src/core/lib/gprpp/unique_type_name.h" + #define XXH_INLINE_ALL #include "xxhash.h" @@ -94,7 +96,10 @@ namespace grpc_core { 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 { @@ -764,13 +769,13 @@ ConfigSelector::CallConfig XdsResolver::XdsConfigSelector::GetCallConfig( method_config->GetMethodParsedConfigVector(grpc_empty_slice()); 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()); char* hash_value = static_cast(args.arena->Alloc(hash_string.size() + 1)); memcpy(hash_value, hash_string.c_str(), hash_string.size()); 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 = args.arena->New(it->second->Ref()); return call_config; diff --git a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h index 72a80a97e1f..09889e4f73a 100644 --- a/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h +++ b/src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h @@ -19,9 +19,11 @@ #include +#include "src/core/lib/gprpp/unique_type_name.h" + namespace grpc_core { -extern const char* kXdsClusterAttribute; +UniqueTypeName XdsClusterAttributeTypeName(); } // namespace grpc_core diff --git a/src/core/lib/service_config/service_config_call_data.h b/src/core/lib/service_config/service_config_call_data.h index f60342ec25e..f222846d742 100644 --- a/src/core/lib/service_config/service_config_call_data.h +++ b/src/core/lib/service_config/service_config_call_data.h @@ -28,6 +28,7 @@ #include "absl/strings/string_view.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_parser.h" @@ -39,7 +40,7 @@ namespace grpc_core { /// easily access method and global parameters for the call. class ServiceConfigCallData { public: - using CallAttributes = std::map; + using CallAttributes = std::map; ServiceConfigCallData() : method_configs_(nullptr) {}