grpc-java
grpc-java copied to clipboard
grpclb: pick_first delegation
Summary of Changes This pull request refactors the grpclb load balancer's PICK_FIRST mode to delegate its logic to a standard pick_first load balancing policy.
The key changes are as follows:
grpclb/build.gradle
Added dependency on grpc-util module to access ForwardingLoadBalancerHelper
grpclb/src/main/java/io/grpc/grpclb/GrpclbState.java
- New imports: LoadBalancer, LoadBalancerProvider, LoadBalancerRegistry, ResolvedAddresses, FixedResultPicker, ForwardingLoadBalancerHelper
- New fields for PICK_FIRST delegation:
- pickFirstLbProvider - Provider for creating child pick_first LB
- pickFirstLb - The child LoadBalancer instance
- pickFirstLbState / pickFirstLbPicker - Track child LB's state and picker
- currentPickFirstLoadRecorder - Load recorder for token attachment
- Key behavioral changes:
- updateServerList() PICK_FIRST case: Instead of creating a single subchannel, it now:
- Creates the child pick_first LB once and then updates it with new addresses on subsequent updates.
- Passes addresses to child LB via acceptResolvedAddresses()
- maybeUpdatePicker() PICK_FIRST case: Uses child LB's state and picker wrapped with ChildLbPickerEntry
- RoundRobinEntry.picked() signature change: Changed from picked(Metadata) to picked(PickSubchannelArgs) to allow child picker delegation
- New ChildLbPickerEntry class: Wraps child LB's picker and attaches TokenAttachingTracerFactory for token propagation
- New PickFirstLbHelper class: Forwarding helper that intercepts updateBalancingState() to store child state and trigger grpclb picker updates
- Updated shutdown(), requestConnection(), maybeUseFallbackBackends(): Handle the new child LB delegation model
- updateServerList() PICK_FIRST case: Instead of creating a single subchannel, it now:
grpclb/src/test/java/io/grpc/grpclb/GrpclbLoadBalancerTest.java
- Updated tests to reflect the new delegation behavior:
- Initial state is now CONNECTING (not IDLE) since standard pick_first eagerly connects
- Tests now verify the child LB is created only once and then handles address updates internally.
- Adjusted verification expectations for the new flow
- Key Behavioral Changes:
- Delegation to child LB: The grpclb state no longer directly manages subchannels for PICK_FIRST mode; the child pick_first LB handles this internally.