Skip to content

Comments

unwrap ForwardingSubchannel during Picks#6

Open
shivaspeaks wants to merge 7 commits intomasterfrom
unwrap-forwarding-subchannel
Open

unwrap ForwardingSubchannel during Picks#6
shivaspeaks wants to merge 7 commits intomasterfrom
unwrap-forwarding-subchannel

Conversation

@shivaspeaks
Copy link
Owner

Unwap ForwardingSubchannels during picks (prerequisite to deleting Subchannel Attributes)

Some LB policies wrap the Subchannel (look for ForwardingSubchannel) to get events like shutdown(), but they don't un-wrap the subchannel when propagating the subchannel up the stack during picks. That means the identity of a subchannel is awkward, as you can't use object identity and instead need to store data inside the subchannel.

If the LB policies properly unwrap when doing picks, then if ManagedChannelImpl has Helper.createSubchannel() always return an instance of FooSubchannel, then during pickSubchannel() the subchannel should always be a FooSubchannel and the channel could blindly cast to FooSubchannel. Today we are using getInternalSubchannel() to unwrap.

So this task has two parts:

  1. Fixing any LB policy that wraps subchannels (search for ForwardingSubchannel)
  2. Adding an API to LB.PickResult to make it easier to unwrap without dropping data on the floor (like any tracer; this makes (1) less annoying)

@gemini-code-assist
Copy link

Summary of Changes

Hello @shivaspeaks, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a long-standing issue where load balancer policies would wrap Subchannel objects with ForwardingSubchannel but fail to unwrap them during the picking process. This behavior made it difficult to correctly identify the true subchannel and perform operations like casting. The changes introduce new API methods to PickResult for seamless subchannel replacement and stream tracer factory updates, and subsequently integrate these unwrapping mechanisms into various load balancer implementations. The overall goal is to streamline subchannel identity management and simplify future modifications related to subchannel attributes.

Highlights

  • New PickResult Methods: Two new methods, withSubchannelReplacement and withStreamTracerFactory, were added to LoadBalancer.PickResult to facilitate easier unwrapping of subchannels and updating stream tracer factories while preserving other pick result attributes like authority override.
  • Load Balancer Policy Updates: Several load balancer implementations, including HealthCheckingLoadBalancerFactory, HealthProducerHelper, OutlierDetectionLoadBalancer, ClusterImplLoadBalancer, WeightedRoundRobinLoadBalancer, and OrcaOobUtil, were updated to explicitly unwrap ForwardingSubchannel instances during the pickSubchannel process. This ensures that the underlying subchannel is exposed, addressing identity issues.
  • Refactored ClusterImplLoadBalancer: The anonymous ForwardingSubchannel implementation within ClusterImplLoadBalancer was refactored into a dedicated inner class, ClusterImplSubchannel, improving code readability and maintainability.
  • Test Coverage Expanded: New tests were added for the PickResult methods, and existing tests for OutlierDetectionLoadBalancer and WeightedRoundRobinLoadBalancer were updated to reflect the new unwrapping behavior and helper methods.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • api/src/main/java/io/grpc/LoadBalancer.java
    • Introduced withSubchannelReplacement(Subchannel subchannel) to PickResult for replacing the subchannel while retaining stream tracer factory and authority override.
    • Added withStreamTracerFactory(ClientStreamTracer.Factory streamTracerFactory) to PickResult for updating the stream tracer factory while retaining the subchannel and authority override.
  • api/src/test/java/io/grpc/LoadBalancerTest.java
    • Added unit tests for the new PickResult.withSubchannelReplacement method.
    • Added unit tests for the new PickResult.withStreamTracerFactory method.
  • services/src/main/java/io/grpc/protobuf/services/HealthCheckingLoadBalancerFactory.java
    • Implemented HealthCheckPicker to unwrap SubchannelImpl instances during pickSubchannel using withSubchannelReplacement.
  • util/src/main/java/io/grpc/util/HealthProducerHelper.java
    • Imported ConnectivityState.
    • Implemented HealthProducerPicker to unwrap HealthProducerSubchannel instances during pickSubchannel using withSubchannelReplacement.
  • util/src/main/java/io/grpc/util/OutlierDetectionLoadBalancer.java
    • Modified pickSubchannel to unwrap OutlierDetectionSubchannel instances using withSubchannelReplacement and withStreamTracerFactory.
  • util/src/test/java/io/grpc/util/OutlierDetectionLoadBalancerTest.java
    • Updated delegatePick test to directly use the unwrapped subchannel from pickResult.getSubchannel().
  • xds/src/main/java/io/grpc/xds/ClusterImplLoadBalancer.java
    • Refactored the anonymous ForwardingSubchannel into a named inner class ClusterImplSubchannel.
    • Modified pickSubchannel to unwrap ClusterImplSubchannel instances using withSubchannelReplacement.
    • Updated pickSubchannel to use withStreamTracerFactory when applying ORCA tracer factories.
  • xds/src/main/java/io/grpc/xds/WeightedRoundRobinLoadBalancer.java
    • Modified pickSubchannel to unwrap WrrSubchannel instances using withSubchannelReplacement and withStreamTracerFactory.
  • xds/src/main/java/io/grpc/xds/orca/OrcaOobUtil.java
    • Imported ConnectivityState, PickResult, PickSubchannelArgs, and SubchannelPicker.
    • Implemented OrcaOobPicker to unwrap SubchannelImpl instances during pickSubchannel using withSubchannelReplacement.
  • xds/src/test/java/io/grpc/xds/WeightedRoundRobinLoadBalancerTest.java
    • Added a helper method getWrrPicker to correctly retrieve the WeightedRoundRobinPicker by unwrapping OrcaOobPicker if present.
    • Updated various test methods (pickChildLbTF, wrrLifeCycle, enableOobLoadReportConfig, pickByWeight, blackoutPeriod, updateWeightTimer, weightExpired, rrFallback, unknownWeightIsAvgWeight, pickFromOtherThread) to use the new getWrrPicker helper.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@shivaspeaks
Copy link
Owner Author

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces helper methods to PickResult for easier creation of new results from existing ones, and then refactors several load balancer implementations to use these new methods for unwrapping ForwardingSubchannel instances. This is a good improvement that simplifies the code and makes the logic clearer. The changes are consistent across different load balancers. I've found a few minor areas for improvement, mainly around Javadoc clarity and small code simplifications, which are detailed in the comments. Overall, this is a solid refactoring.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great step towards simplifying the load balancing logic by introducing helper methods on PickResult and using them to consistently unwrap ForwardingSubchannel instances within the load balancer policies. The changes make the code cleaner and more maintainable. I have a couple of minor suggestions to further improve consistency across the codebase.

@shivaspeaks shivaspeaks force-pushed the unwrap-forwarding-subchannel branch from 6fc9752 to ac409fb Compare February 16, 2026 05:29
@shivaspeaks shivaspeaks changed the title unwrap Forwarding subchannel unwrap forwardingSubchannel Feb 16, 2026
@shivaspeaks shivaspeaks changed the title unwrap forwardingSubchannel unwrap ForwardingSubchannel Feb 16, 2026
@shivaspeaks shivaspeaks changed the title unwrap ForwardingSubchannel unwrap ForwardingSubchannel during Picks Feb 16, 2026
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.

1 participant