Repository navigation
feat: use route hash_policy for weighted cluster as well - #41244
Conversation
Signed-off-by: tanujd11 <[email protected]>
wbpcode
left a comment
There was a problem hiding this comment.
Thanks so much for the contribution. But this is a very big behavior change anyway. Could you add a google.protobuf.BoolValue use_hash_policy in the WeightedCluster to enable this feature? Thanks.
Signed-off-by: tanujd11 <[email protected]>
|
CC @envoyproxy/api-shepherds: Your approval is needed for changes made to |
|
|
||
| // When set to true, enables the use of route-level hash policies for weighted cluster selection. | ||
| // This ensures that requests with the same hash value (e.g., same session ID, user ID, etc.) | ||
| // will consistently be routed to the same weighted cluster. If not set or set to false, | ||
| // weighted cluster selection will use the standard weighted selection behavior. | ||
| google.protobuf.BoolValue use_hash_policy = 5; |
There was a problem hiding this comment.
I think this should be one of the random_value_specifier. we have supported the header_name there, now we use a new way to get the random value.
And it would be better to tell in the comment that
When set to true, the hash policies will be used to generate the random value for weighted cluster selection.
This could ensure consistent cluster picking across multiple proxy levels for weighted traffic.
| absl::optional<uint64_t> hash_value; | ||
|
|
||
| // Only use hash policy if explicitly enabled via use_hash_policy field | ||
| if (use_hash_policy_) { | ||
| const auto* route_hash_policy = parent->hashPolicy(); | ||
| if (route_hash_policy != nullptr) { | ||
| hash_value = | ||
| route_hash_policy->generateHash(OptRef<const Http::RequestHeaderMap>(headers), | ||
| OptRef<const StreamInfo::StreamInfo>(stream_info), nullptr); | ||
| } | ||
| } | ||
|
|
||
| const uint64_t selection_value = hash_value.has_value() ? hash_value.value() : random; | ||
|
|
||
| return pickWeightedCluster(std::move(parent), headers, selection_value); |
There was a problem hiding this comment.
I think you could do this at pickWeightedCluster, just like the header_name field.
Signed-off-by: tanujd11 <[email protected]>
wbpcode
left a comment
There was a problem hiding this comment.
Thanks for the update, Tanuj. Only some minor comments.
| WeightedClusterSpecifierPlugin::pickWeightedCluster(RouteEntryAndRouteConstSharedPtr parent, | ||
| const Http::RequestHeaderMap& headers, | ||
| const uint64_t random_value) const { | ||
| const uint64_t random_value, | ||
| const StreamInfo::StreamInfo* stream_info) const { |
There was a problem hiding this comment.
RouteConstSharedPtr
WeightedClusterSpecifierPlugin::pickWeightedCluster(RouteEntryAndRouteConstSharedPtr parent,
const Http::RequestHeaderMap& headers,
const StreamInfo::StreamInfo& stream_info
const uint64_t random_value) const {
// ....
}
Reference always over raw pointer if possible.
Signed-off-by: tanujd11 <[email protected]>
| RouteConstSharedPtr WeightedClusterSpecifierPlugin::pickWeightedCluster( | ||
| RouteEntryAndRouteConstSharedPtr parent, const Http::RequestHeaderMap& headers, | ||
| const StreamInfo::StreamInfo* stream_info, const uint64_t random_value) const { | ||
| absl::optional<uint64_t> hash_value; | ||
|
|
||
| // Only use hash policy if explicitly enabled via use_hash_policy field | ||
| if (use_hash_policy_ && stream_info != nullptr) { | ||
| const auto* route_hash_policy = parent->hashPolicy(); | ||
| if (route_hash_policy != nullptr) { | ||
| hash_value = route_hash_policy->generateHash( | ||
| OptRef<const Http::RequestHeaderMap>(headers), | ||
| OptRef<const StreamInfo::StreamInfo>(*stream_info), nullptr); | ||
| } | ||
| } |
There was a problem hiding this comment.
| RouteConstSharedPtr WeightedClusterSpecifierPlugin::pickWeightedCluster( | |
| RouteEntryAndRouteConstSharedPtr parent, const Http::RequestHeaderMap& headers, | |
| const StreamInfo::StreamInfo* stream_info, const uint64_t random_value) const { | |
| absl::optional<uint64_t> hash_value; | |
| // Only use hash policy if explicitly enabled via use_hash_policy field | |
| if (use_hash_policy_ && stream_info != nullptr) { | |
| const auto* route_hash_policy = parent->hashPolicy(); | |
| if (route_hash_policy != nullptr) { | |
| hash_value = route_hash_policy->generateHash( | |
| OptRef<const Http::RequestHeaderMap>(headers), | |
| OptRef<const StreamInfo::StreamInfo>(*stream_info), nullptr); | |
| } | |
| } | |
| RouteConstSharedPtr WeightedClusterSpecifierPlugin::pickWeightedCluster( | |
| RouteEntryAndRouteConstSharedPtr parent, const Http::RequestHeaderMap& headers, | |
| const StreamInfo::StreamInfo& stream_info, const uint64_t random_value) const { | |
| absl::optional<uint64_t> hash_value; | |
| // Only use hash policy if explicitly enabled via use_hash_policy field | |
| if (use_hash_policy_) { | |
| const auto* route_hash_policy = parent->hashPolicy(); | |
| if (route_hash_policy != nullptr) { | |
| hash_value = route_hash_policy->generateHash( | |
| OptRef<const Http::RequestHeaderMap>(headers), | |
| OptRef<const StreamInfo::StreamInfo>(stream_info), nullptr); | |
| } | |
| } |
const StreamInfo::StreamInfo& over const StreamInfo::StreamInfo* if possible.
| const StreamInfo::StreamInfo& stream_info, | ||
| uint64_t random) const { | ||
| return pickWeightedCluster(std::move(parent), headers, random); | ||
| return pickWeightedCluster(std::move(parent), headers, random, &stream_info); |
There was a problem hiding this comment.
| return pickWeightedCluster(std::move(parent), headers, random, &stream_info); | |
| return pickWeightedCluster(std::move(parent), headers, stream_info, random); |
| private: | ||
| RouteConstSharedPtr pickWeightedCluster(RouteEntryAndRouteConstSharedPtr parent, | ||
| const Http::RequestHeaderMap& headers, | ||
| const StreamInfo::StreamInfo* stream_info, |
There was a problem hiding this comment.
| const StreamInfo::StreamInfo* stream_info, | |
| const StreamInfo::StreamInfo& stream_info, |
| Runtime::Loader& loader_; | ||
| const Http::LowerCaseString random_value_header_; | ||
| const std::string runtime_key_prefix_; | ||
| const bool use_hash_policy_; |
There was a problem hiding this comment.
| const bool use_hash_policy_; | |
| const bool use_hash_policy_{}; |
| - area: router | ||
| change: | | ||
| Added :ref:`use_hash_policy <envoy_v3_api_field_config.route.v3.WeightedCluster.use_hash_policy>` | ||
| field to :ref:`WeightedCluster <envoy_v3_api_msg_config.route.v3.WeightedCluster>` to enable | ||
| route-level hash policies for weighted cluster selection. When set to ``true``, | ||
| the existing route-level :ref:`hash_policy <envoy_v3_api_field_config.route.v3.RouteAction.hash_policy>` | ||
| will be used for consistent hashing between weighted clusters, ensuring that requests | ||
| with the same hash value (e.g., same session ID, user ID, etc.) will consistently be | ||
| routed to the same weighted cluster, enabling session affinity and consistent load | ||
| balancing behavior. |
There was a problem hiding this comment.
| - area: router | |
| change: | | |
| Added :ref:`use_hash_policy <envoy_v3_api_field_config.route.v3.WeightedCluster.use_hash_policy>` | |
| field to :ref:`WeightedCluster <envoy_v3_api_msg_config.route.v3.WeightedCluster>` to enable | |
| route-level hash policies for weighted cluster selection. When set to ``true``, | |
| the existing route-level :ref:`hash_policy <envoy_v3_api_field_config.route.v3.RouteAction.hash_policy>` | |
| will be used for consistent hashing between weighted clusters, ensuring that requests | |
| with the same hash value (e.g., same session ID, user ID, etc.) will consistently be | |
| routed to the same weighted cluster, enabling session affinity and consistent load | |
| balancing behavior. | |
| - area: router | |
| change: | | |
| Added :ref:`use_hash_policy <envoy_v3_api_field_config.route.v3.WeightedCluster.use_hash_policy>` | |
| field to :ref:`WeightedCluster <envoy_v3_api_msg_config.route.v3.WeightedCluster>` to enable | |
| route-level hash policies for weighted cluster selection. When set to ``true``, | |
| the existing route-level :ref:`hash_policy | |
| <envoy_v3_api_field_config.route.v3.RouteAction.hash_policy>` | |
| will be used for consistent hashing between weighted clusters, ensuring that requests | |
| with the same hash value (e.g., same session ID, user ID, etc.) will consistently be | |
| routed to the same weighted cluster, enabling session affinity and consistent load | |
| balancing behavior. |
There was a problem hiding this comment.
Envoy's strict format validation dislike trailing white space.
Signed-off-by: tanujd11 <[email protected]>
Signed-off-by: tanujd11 <[email protected]>
Signed-off-by: tanujd11 <[email protected]>
|
/retest |
Commit Message: weighted clusters: use route hash_policy for consistent hashing b/w clusters.
Additional Description: Fixes #29027. Currently the route hash_policy is only used with single cluster. This PR allows us to use it with multiple weighted clusters.
Risk Level: Low
Testing: Manual and Integration Test
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional API Considerations:]