diff --git a/include/grpc/impl/codegen/grpc_types.h b/include/grpc/impl/codegen/grpc_types.h index dcaf807d0b47c..b681558e86cc9 100644 --- a/include/grpc/impl/codegen/grpc_types.h +++ b/include/grpc/impl/codegen/grpc_types.h @@ -310,6 +310,10 @@ typedef struct { "grpc.service_config_disable_resolution" /** LB policy name. */ #define GRPC_ARG_LB_POLICY_NAME "grpc.lb_policy_name" +/** Cap for ring size in the ring_hash LB policy. The min and max ring size + values set in the LB policy config will be capped to this value. + Default is 4096. */ +#define GRPC_ARG_RING_HASH_LB_RING_SIZE_CAP "grpc.lb.ring_hash.ring_size_cap" /** The grpc_socket_mutator instance that set the socket options. A pointer. */ #define GRPC_ARG_SOCKET_MUTATOR "grpc.socket_mutator" /** The grpc_socket_factory instance to create and bind sockets. A pointer. */ 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 d39e0a9ca415a..44e221c350a2d 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 @@ -129,6 +129,8 @@ class RingHashLbConfig : public LoadBalancingPolicy::Config { // ring_hash LB policy // +constexpr size_t kRingSizeCapDefault = 4096; + class RingHash : public LoadBalancingPolicy { public: explicit RingHash(Args args); @@ -518,8 +520,12 @@ RingHash::RingHashSubchannelList::RingHashSubchannelList( // weights aren't provided, all hosts should get an equal number of hashes. In // the case where this number exceeds the max_ring_size, it's scaled back down // to fit. - const size_t min_ring_size = policy->config_->min_ring_size(); - const size_t max_ring_size = policy->config_->max_ring_size(); + const size_t ring_size_cap = args.GetInt(GRPC_ARG_RING_HASH_LB_RING_SIZE_CAP) + .value_or(kRingSizeCapDefault); + const size_t min_ring_size = + std::min(policy->config_->min_ring_size(), ring_size_cap); + const size_t max_ring_size = + std::min(policy->config_->max_ring_size(), ring_size_cap); const double scale = std::min( std::ceil(min_normalized_weight * min_ring_size) / min_normalized_weight, static_cast(max_ring_size)); 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 ecb19c9963adc..711eb961c2d4e 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 @@ -35,7 +35,7 @@ UniqueTypeName RequestHashAttributeName(); // hash size validity. struct RingHashConfig { uint64_t min_ring_size = 1024; - uint64_t max_ring_size = 8388608; + uint64_t max_ring_size = 4096; static const JsonLoaderInterface* JsonLoader(const JsonArgs&); void JsonPostLoad(const Json& json, const JsonArgs&,