Skip to content

balancer: expose endpoint weight and hostname as experimental APIs#9074

Open
hpathak01 wants to merge 10 commits intogrpc:masterfrom
hpathak01:hpathak01/issue-8971-expose-xds-attr
Open

balancer: expose endpoint weight and hostname as experimental APIs#9074
hpathak01 wants to merge 10 commits intogrpc:masterfrom
hpathak01:hpathak01/issue-8971-expose-xds-attr

Conversation

@hpathak01
Copy link
Copy Markdown

@hpathak01 hpathak01 commented Apr 17, 2026

Description

This PR exposes two new experimental public APIs so that custom load balancing policies can access endpoint attributes that were previously only available via internal packages:

  • google.golang.org/grpc/balancer/weightSet, FromEndpoint, EndpointInfo
  • google.golang.org/grpc/balancer/hostnameSet, FromEndpoint, FromAddress

These attributes (especially weight) are required by custom balancers such as deterministic aperture / P2C that need EDS-provided endpoint details like weights etc.

Changes

  • New public package balancer/hostname with full experimental notices.
  • Moved internal/balancer/weight to public balancer/weight via git mv, added experimental notices.
  • Internal callers (ringhash, pickfirst, cdsbalancer, clusterimpl, xdsresource) updated to use the public packages directly.
  • Removed SetHostname and Hostname wrappers from xdsresource; callers now use hostname.Set and hostname.FromAddress directly.
  • Added table-driven unit tests for both new public packages.

Related Issues

Fixes #8971

Testing

  • All tests pass (go test -count=1 ./...)

Notes for Reviewers

  • The new APIs follow the exact same experimental pattern used by resolver/ringhash.
  • The "Dependency Changes" check fails because this PR intentionally adds two new public packages (balancer/weight and balancer/hostname).

RELEASE NOTES: none

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 17, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.09%. Comparing base (06fc26a) to head (f30c259).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9074      +/-   ##
==========================================
+ Coverage   80.52%   83.09%   +2.56%     
==========================================
  Files         413      414       +1     
  Lines       33543    33480      -63     
==========================================
+ Hits        27012    27819     +807     
+ Misses       4316     4236      -80     
+ Partials     2215     1425     -790     
Files with missing lines Coverage Δ
balancer/hostname/hostname.go 100.00% <100.00%> (ø)
balancer/pickfirst/pickfirst.go 89.28% <ø> (-0.48%) ⬇️
balancer/ringhash/ringhash.go 94.11% <ø> (ø)
balancer/weight/weight.go 100.00% <ø> (ø)
internal/xds/balancer/cdsbalancer/configbuilder.go 91.11% <100.00%> (ø)
internal/xds/balancer/clusterimpl/clusterimpl.go 88.25% <100.00%> (ø)
...nternal/xds/xdsclient/xdsresource/unmarshal_eds.go 96.00% <100.00%> (+4.22%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…rpc#8971)

- Add new public packages balancer/weight and balancer/hostname
  with Set/FromEndpoint and Set/Hostname/HostnameFromAddress helpers.
- Mark all new APIs as # Experimental with the standard notice.
- Update internal callers (ringhash, pickfirst, cdsbalancer, xdsresource)
  to use the public packages.
- Remove dead hostnameKeyType from xdsresource and delegate legacy
  Hostname() to the public API (fixes Authority_Rewrite_Mismatch test).
- Keep deprecated internal/balancer/weight package for transition.
- Add tests for the new public packages.
- Verified end-to-end with custom DAPerture balancer.
@hpathak01 hpathak01 force-pushed the hpathak01/issue-8971-expose-xds-attr branch from 7e7f078 to 5ed3447 Compare April 17, 2026 08:39
@hpathak01 hpathak01 marked this pull request as ready for review April 20, 2026 03:45
@easwars easwars added the Type: Feature New features or improvements in behavior label Apr 20, 2026
@easwars easwars added this to the 1.82 Release milestone Apr 20, 2026
@easwars easwars added the Area: Resolvers/Balancers Includes LB policy & NR APIs, resolver/balancer/picker wrappers, LB policy impls and utilities. label Apr 20, 2026
@easwars easwars changed the title [balancer] expose endpoint weight and hostname as experimental APIs (… balancer: expose endpoint weight and hostname as experimental APIs Apr 20, 2026
@easwars
Copy link
Copy Markdown
Contributor

easwars commented Apr 20, 2026

The "Dependency Changes" check fails because this PR intentionally adds two new public packages (balancer/weight and balancer/hostname).

Yes, that is expected.

@easwars
Copy link
Copy Markdown
Contributor

easwars commented Apr 20, 2026

@Pranjali-2501 : Could you please do a first pass.

Copy link
Copy Markdown
Contributor

@Pranjali-2501 Pranjali-2501 left a comment

Choose a reason for hiding this comment

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

Hi @hpathak01, thanks for raising the PR.
I have added some comments, please take a look.

Comment thread balancer/hostname/hostname.go Outdated
//
// Notice: This API is EXPERIMENTAL and may be changed or removed in a
// later release.
func Hostname(endpoint resolver.Endpoint) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it will be better to rename it as FromEndpoint.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Comment thread balancer/hostname/hostname.go Outdated
return h
}

// FromAddress returns the hostname attribute from a legacy
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need to export the function to retrieve hostname from address.attributes and address.BalancerAttributes.
Please remove this.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

"time"

"github.com/google/go-cmp/cmp"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Remove this new line.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

Comment thread internal/balancer/weight/weight.go Outdated
// Package weight contains utilities to manage endpoint weights. Weights are
// used by LB policies such as ringhash to distribute load across multiple
// endpoints.
//
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of putting a deprecated message and creating new files in balancer/weight/, use git move to move the files from internal/balancer/weight/weight.go to balancer/weight/weight.go.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done - i also added the "Experimental" comment because this is a newly-public API. is that fine?

Comment thread internal/balancer/weight/weight_test.go Outdated
*
*/

// Deprecated: use google.golang.org/grpc/balancer/weight instead.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same as above, use git move to move this file also.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

"time"

"github.com/google/go-cmp/cmp"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please remove this newline.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

}
endpoint.Attributes = endpoint.Attributes.WithValue(hostnameKeyType{}, hostname)
return endpoint
func SetHostname(endpoint resolver.Endpoint, h string) resolver.Endpoint {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need to call exported Get/Set function from another function. Directly use hostname.Set whenever required.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

// Address. If this attribute is not set, it returns the empty string.
// Hostname returns the hostname from the given legacy Address.
// If this attribute is not set, it returns the empty string.
func Hostname(addr resolver.Address) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please revert the changes made in this function. We have an exported function to set hostname in endpoint. And will keep the function internal to retrieve the hostname resolver.Address.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

hi @Pranjali-2501 reverting this function causes two issues:

  1. The original hostnameKeyType has been removed above, so it wouldn't compile.
  2. Even if we bring hostnameKeyType back, the test TestAuthorityOverridingWithTLS/Authority_Rewrite_Mismatch fails — because EDS unmarshal now writes hostname via hostname.Set (which uses the hostnameKey defined in balancer/hostname/hostname.go), but the original Hostname(addr) would read with its own internal key type, so the hostname is never found.

The current implementation delegates to hostname.FromEndpoint so both setter and getter use the same key. All tests pass. Does this approach work, or would you prefer a different solution?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMO, the best way will be to move this function to balancer/hostname/hostname.go, export it and name it FromAddress.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

// Address. If this attribute is not set, it returns the empty string.
// Hostname returns the hostname from the given legacy Address.
// If this attribute is not set, it returns the empty string.
func Hostname(addr resolver.Address) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

IMO, the best way will be to move this function to balancer/hostname/hostname.go, export it and name it FromAddress.

Comment thread internal/xds/xdsclient/xdsresource/unmarshal_eds_test.go Outdated
Comment thread internal/xds/xdsclient/xdsresource/unmarshal_eds.go Outdated
Comment thread balancer/hostname/hostname_test.go Outdated
@Pranjali-2501
Copy link
Copy Markdown
Contributor

@hpathak01
Copy link
Copy Markdown
Author

Copy link
Copy Markdown
Contributor

@Pranjali-2501 Pranjali-2501 left a comment

Choose a reason for hiding this comment

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

LGTM.

Comment thread balancer/hostname/hostname.go Outdated
return endpoint
}

// FromEndpoint returns the hostname attribute of endpoint. If this attribute is
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: wrap the comment in 80 columns.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

done

@Pranjali-2501 Pranjali-2501 assigned easwars and unassigned hpathak01 Apr 23, 2026
@Pranjali-2501 Pranjali-2501 requested a review from easwars April 23, 2026 05:04
@hpathak01
Copy link
Copy Markdown
Author

Hi @easwars , just checking in — I've addressed all the review comments. Would you have a chance to take a look when you get a moment? Thanks!

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

Labels

Area: Resolvers/Balancers Includes LB policy & NR APIs, resolver/balancer/picker wrappers, LB policy impls and utilities. Type: Feature New features or improvements in behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose custom metadata from EDS response to custom LB policies

3 participants