Skip to content

Commit b7699ef

Browse files
authored
LB policy API: use UniqueTypeName for call attributes (grpc#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 <[email protected]>
1 parent 8558f46 commit b7699ef

File tree

10 files changed

+64
-37
lines changed

10 files changed

+64
-37
lines changed

BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,7 @@ grpc_cc_library(
28072807
"ref_counted_ptr",
28082808
"service_config_parser",
28092809
"slice_refcount",
2810+
"unique_type_name",
28102811
],
28112812
)
28122813

@@ -4153,6 +4154,7 @@ grpc_cc_library(
41534154
"ref_counted_ptr",
41544155
"server_address",
41554156
"sockaddr_utils",
4157+
"unique_type_name",
41564158
],
41574159
)
41584160

@@ -4704,6 +4706,9 @@ grpc_cc_library(
47044706
hdrs = [
47054707
"src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h",
47064708
],
4709+
external_deps = [
4710+
"absl/strings",
4711+
],
47074712
language = "c++",
47084713
)
47094714

@@ -4749,6 +4754,7 @@ grpc_cc_library(
47494754
"ref_counted_ptr",
47504755
"server_address",
47514756
"time",
4757+
"unique_type_name",
47524758
"uri_parser",
47534759
"useful",
47544760
],

src/core/ext/filters/client_channel/client_channel.cc

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,25 +2540,16 @@ class ClientChannel::LoadBalancedCall::Metadata
25402540
// ClientChannel::LoadBalancedCall::LbCallState
25412541
//
25422542

2543-
class ClientChannel::LoadBalancedCall::LbCallState
2544-
: public LoadBalancingPolicy::CallState {
2545-
public:
2546-
explicit LbCallState(LoadBalancedCall* lb_call) : lb_call_(lb_call) {}
2547-
2548-
void* Alloc(size_t size) override { return lb_call_->arena_->Alloc(size); }
2549-
2550-
absl::string_view ExperimentalGetCallAttribute(const char* key) override {
2551-
auto* service_config_call_data = static_cast<ServiceConfigCallData*>(
2552-
lb_call_->call_context_[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value);
2553-
auto& call_attributes = service_config_call_data->call_attributes();
2554-
auto it = call_attributes.find(key);
2555-
if (it == call_attributes.end()) return absl::string_view();
2556-
return it->second;
2557-
}
2558-
2559-
private:
2560-
LoadBalancedCall* lb_call_;
2561-
};
2543+
absl::string_view
2544+
ClientChannel::LoadBalancedCall::LbCallState::GetCallAttribute(
2545+
UniqueTypeName type) {
2546+
auto* service_config_call_data = static_cast<ServiceConfigCallData*>(
2547+
lb_call_->call_context_[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value);
2548+
auto& call_attributes = service_config_call_data->call_attributes();
2549+
auto it = call_attributes.find(type);
2550+
if (it == call_attributes.end()) return absl::string_view();
2551+
return it->second;
2552+
}
25622553

25632554
//
25642555
// ClientChannel::LoadBalancedCall::BackendMetricAccessor

src/core/ext/filters/client_channel/client_channel.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "absl/base/thread_annotations.h"
3232
#include "absl/status/status.h"
33+
#include "absl/strings/string_view.h"
3334
#include "absl/types/optional.h"
3435

3536
#include <grpc/impl/codegen/connectivity_state.h>
@@ -54,6 +55,7 @@
5455
#include "src/core/lib/gprpp/ref_counted_ptr.h"
5556
#include "src/core/lib/gprpp/sync.h"
5657
#include "src/core/lib/gprpp/time.h"
58+
#include "src/core/lib/gprpp/unique_type_name.h"
5759
#include "src/core/lib/iomgr/call_combiner.h"
5860
#include "src/core/lib/iomgr/closure.h"
5961
#include "src/core/lib/iomgr/error.h"
@@ -382,6 +384,20 @@ class ClientChannel {
382384
class ClientChannel::LoadBalancedCall
383385
: public InternallyRefCounted<LoadBalancedCall, kUnrefCallDtor> {
384386
public:
387+
class LbCallState : public LoadBalancingPolicy::CallState {
388+
public:
389+
explicit LbCallState(LoadBalancedCall* lb_call) : lb_call_(lb_call) {}
390+
391+
void* Alloc(size_t size) override { return lb_call_->arena_->Alloc(size); }
392+
393+
// Internal API to allow first-party LB policies to access per-call
394+
// attributes set by the ConfigSelector.
395+
absl::string_view GetCallAttribute(UniqueTypeName type);
396+
397+
private:
398+
LoadBalancedCall* lb_call_;
399+
};
400+
385401
// If on_call_destruction_complete is non-null, then it will be
386402
// invoked once the LoadBalancedCall is completely destroyed.
387403
// If it is null, then the caller is responsible for checking whether
@@ -417,7 +433,6 @@ class ClientChannel::LoadBalancedCall
417433
private:
418434
class LbQueuedCallCanceller;
419435
class Metadata;
420-
class LbCallState;
421436
class BackendMetricAccessor;
422437

423438
// Returns the index into pending_batches_ to be used for batch.

src/core/ext/filters/client_channel/lb_policy.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,6 @@ class LoadBalancingPolicy : public InternallyRefCounted<LoadBalancingPolicy> {
106106
/// It is more efficient to use this than to allocate memory directly
107107
/// for allocations that need to be made on a per-call basis.
108108
virtual void* Alloc(size_t size) = 0;
109-
110-
/// EXPERIMENTAL API.
111-
/// Returns the value of the call attribute \a key.
112-
/// Keys are static strings, so an attribute can be accessed by an LB
113-
/// policy implementation only if it knows about the internal key.
114-
/// Returns a null string_view if key not found.
115-
virtual absl::string_view ExperimentalGetCallAttribute(const char* key) = 0;
116109
};
117110

118111
/// Interface for accessing metadata.

src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.cc

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@
3838
#include "absl/strings/string_view.h"
3939
#include "absl/types/optional.h"
4040

41+
#include "src/core/lib/gprpp/unique_type_name.h"
42+
4143
#define XXH_INLINE_ALL
4244
#include "xxhash.h"
4345

4446
#include <grpc/impl/codegen/connectivity_state.h>
4547
#include <grpc/impl/codegen/grpc_types.h>
4648
#include <grpc/support/log.h>
4749

50+
#include "src/core/ext/filters/client_channel/client_channel.h"
4851
#include "src/core/ext/filters/client_channel/lb_policy.h"
4952
#include "src/core/ext/filters/client_channel/lb_policy/subchannel_list.h"
5053
#include "src/core/ext/filters/client_channel/lb_policy_factory.h"
@@ -67,9 +70,13 @@
6770

6871
namespace grpc_core {
6972

70-
const char* kRequestRingHashAttribute = "request_ring_hash";
7173
TraceFlag grpc_lb_ring_hash_trace(false, "ring_hash_lb");
7274

75+
UniqueTypeName RequestHashAttributeName() {
76+
static UniqueTypeName::Factory kFactory("request_hash");
77+
return kFactory.Create();
78+
}
79+
7380
// Helper Parser method
7481
void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size,
7582
size_t* max_ring_size,
@@ -443,8 +450,9 @@ RingHash::Ring::Ring(RingHash* parent,
443450
//
444451

445452
RingHash::PickResult RingHash::Picker::Pick(PickArgs args) {
446-
auto hash =
447-
args.call_state->ExperimentalGetCallAttribute(kRequestRingHashAttribute);
453+
auto* call_state = static_cast<ClientChannel::LoadBalancedCall::LbCallState*>(
454+
args.call_state);
455+
auto hash = call_state->GetCallAttribute(RequestHashAttributeName());
448456
uint64_t h;
449457
if (!absl::SimpleAtoi(hash, &h)) {
450458
return PickResult::Fail(

src/core/ext/filters/client_channel/lb_policy/ring_hash/ring_hash.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323

2424
#include <vector>
2525

26+
#include "src/core/lib/gprpp/unique_type_name.h"
2627
#include "src/core/lib/iomgr/error.h"
2728
#include "src/core/lib/json/json.h"
2829

2930
namespace grpc_core {
30-
extern const char* kRequestRingHashAttribute;
31+
32+
UniqueTypeName RequestHashAttributeName();
3133

3234
// Helper Parsing method to parse ring hash policy configs; for example, ring
3335
// hash size validity.
3436
void ParseRingHashLbConfig(const Json& json, size_t* min_ring_size,
3537
size_t* max_ring_size,
3638
std::vector<grpc_error_handle>* error_list);
39+
3740
} // namespace grpc_core
3841

3942
#endif // GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_LB_POLICY_RING_HASH_RING_HASH_H

src/core/ext/filters/client_channel/lb_policy/xds/xds_cluster_manager.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <grpc/impl/codegen/grpc_types.h>
3737
#include <grpc/support/log.h>
3838

39+
#include "src/core/ext/filters/client_channel/client_channel.h"
3940
#include "src/core/ext/filters/client_channel/lb_policy.h"
4041
#include "src/core/ext/filters/client_channel/lb_policy/child_policy_handler.h"
4142
#include "src/core/ext/filters/client_channel/lb_policy_factory.h"
@@ -228,8 +229,10 @@ class XdsClusterManagerLb : public LoadBalancingPolicy {
228229

229230
XdsClusterManagerLb::PickResult XdsClusterManagerLb::ClusterPicker::Pick(
230231
PickArgs args) {
232+
auto* call_state = static_cast<ClientChannel::LoadBalancedCall::LbCallState*>(
233+
args.call_state);
231234
auto cluster_name =
232-
args.call_state->ExperimentalGetCallAttribute(kXdsClusterAttribute);
235+
call_state->GetCallAttribute(XdsClusterAttributeTypeName());
233236
auto it = cluster_map_.find(cluster_name);
234237
if (it != cluster_map_.end()) {
235238
return it->second->Pick(args);

src/core/ext/filters/client_channel/resolver/xds/xds_resolver.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
#include "absl/types/variant.h"
4646
#include "re2/re2.h"
4747

48+
#include "src/core/lib/gprpp/unique_type_name.h"
49+
4850
#define XXH_INLINE_ALL
4951
#include "xxhash.h"
5052

@@ -94,7 +96,10 @@ namespace grpc_core {
9496

9597
TraceFlag grpc_xds_resolver_trace(false, "xds_resolver");
9698

97-
const char* kXdsClusterAttribute = "xds_cluster_name";
99+
UniqueTypeName XdsClusterAttributeTypeName() {
100+
static UniqueTypeName::Factory kFactory("xds_cluster_name");
101+
return kFactory.Create();
102+
}
98103

99104
namespace {
100105

@@ -764,13 +769,13 @@ ConfigSelector::CallConfig XdsResolver::XdsConfigSelector::GetCallConfig(
764769
method_config->GetMethodParsedConfigVector(grpc_empty_slice());
765770
call_config.service_config = std::move(method_config);
766771
}
767-
call_config.call_attributes[kXdsClusterAttribute] = it->first;
772+
call_config.call_attributes[XdsClusterAttributeTypeName()] = it->first;
768773
std::string hash_string = absl::StrCat(hash.value());
769774
char* hash_value =
770775
static_cast<char*>(args.arena->Alloc(hash_string.size() + 1));
771776
memcpy(hash_value, hash_string.c_str(), hash_string.size());
772777
hash_value[hash_string.size()] = '\0';
773-
call_config.call_attributes[kRequestRingHashAttribute] = hash_value;
778+
call_config.call_attributes[RequestHashAttributeName()] = hash_value;
774779
call_config.call_dispatch_controller =
775780
args.arena->New<XdsCallDispatchController>(it->second->Ref());
776781
return call_config;

src/core/ext/filters/client_channel/resolver/xds/xds_resolver.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
#include <grpc/support/port_platform.h>
2121

22+
#include "src/core/lib/gprpp/unique_type_name.h"
23+
2224
namespace grpc_core {
2325

24-
extern const char* kXdsClusterAttribute;
26+
UniqueTypeName XdsClusterAttributeTypeName();
2527

2628
} // namespace grpc_core
2729

src/core/lib/service_config/service_config_call_data.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "absl/strings/string_view.h"
2929

3030
#include "src/core/lib/gprpp/ref_counted_ptr.h"
31+
#include "src/core/lib/gprpp/unique_type_name.h"
3132
#include "src/core/lib/service_config/service_config.h"
3233
#include "src/core/lib/service_config/service_config_parser.h"
3334

@@ -39,7 +40,7 @@ namespace grpc_core {
3940
/// easily access method and global parameters for the call.
4041
class ServiceConfigCallData {
4142
public:
42-
using CallAttributes = std::map<const char*, absl::string_view>;
43+
using CallAttributes = std::map<UniqueTypeName, absl::string_view>;
4344

4445
ServiceConfigCallData() : method_configs_(nullptr) {}
4546

0 commit comments

Comments
 (0)