Skip to content

feat: use route hash_policy for weighted cluster as well - #41244

Merged
wbpcode merged 7 commits into
envoyproxy:mainfrom
tanujd11:feat/use-hash-policy-in-weighted-cluster
Sep 29, 2025
Merged

wbpcode merged 7 commits into
envoyproxy:mainfrom
tanujd11:feat/use-hash-policy-in-weighted-cluster

Conversation

@tanujd11

Copy link
Copy Markdown
Member

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:]

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@repokitteh-read-only

Copy link
Copy Markdown

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/).
envoyproxy/api-shepherds assignee is @wbpcode
CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

🐱

Caused by: #41244 was synchronize by tanujd11.

see: more, trace.

Comment on lines +553 to +558

// 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +307 to +321
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you could do this at pickWeightedCluster, just like the header_name field.

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, Tanuj. Only some minor comments.

Comment on lines +207 to +211
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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wbpcode wbpcode left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. The Envoy's CI is unfriendly for developers who are not very familiar with it. So, I add some suggestions to help address some problems. You can apply it directly if you think it's fine.

Comment on lines +210 to +223
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);
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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_;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const bool use_hash_policy_;
const bool use_hash_policy_{};

Comment thread changelogs/current.yaml
Comment on lines +220 to +229
- 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Envoy's strict format validation dislike trailing white space.

Signed-off-by: tanujd11 <[email protected]>
Signed-off-by: tanujd11 <[email protected]>
@tanujd11

Copy link
Copy Markdown
Member Author

/retest

@wbpcode wbpcode left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support sticky_session when using weighted_clusters

2 participants