core: Validate policy name in defaultLoadBalancingPolicy() immediately#12696
Open
manmohak07 wants to merge 3 commits intogrpc:masterfrom
Open
core: Validate policy name in defaultLoadBalancingPolicy() immediately#12696manmohak07 wants to merge 3 commits intogrpc:masterfrom
manmohak07 wants to merge 3 commits intogrpc:masterfrom
Conversation
Author
Hi, thanks for the input. With respect to what you said about the tests, I think we can make these changes:
@Test
public void defaultLoadBalancingPolicy_normal() {
LoadBalancerProvider mockProvider = mock(LoadBalancerProvider.class);
doReturn(true).when(mockProvider).isAvailable();
doReturn("magic_balancer").when(mockProvider).getPolicyName();
LoadBalancerRegistry.getDefaultRegistry().register(mockProvider);
try {
assertSame(builder, builder.defaultLoadBalancingPolicy("magic_balancer"));
assertEquals("magic_balancer", builder.defaultLbPolicy);
} finally {
LoadBalancerRegistry.getDefaultRegistry().deregister(mockProvider);
}
}
@Test
public void defaultLoadBalancingPolicy_unregisteredPolicy() {
assertThrows(
IllegalArgumentException.class,
() -> builder.defaultLoadBalancingPolicy("magic_balancer")
);
}I am not sure about the first point you mentioned. @shivaspeaks Could you please provide suggestions on the existing changes and new ones mentioned here? It would help a lot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #12695
Move the load-balancing policy name validation from the build phase
to the
defaultLoadBalancingPolicy()setter itself, so that aninvalid policy name throws
IllegalArgumentExceptionimmediatelyat the call site.
Problem
Currently, calling
.defaultLoadBalancingPolicy("round_robinn")(typo)stores the bad string. The error only surfaces later during
channel construction when
AutoConfiguredLoadBalancerFactorycreates aFixedPickerLoadBalancerProviderwithTRANSIENT_FAILUREstatus.Solution
The
defaultLoadBalancingPolicy()setter inManagedChannelImplBuildernow validates the provided policy name against
LoadBalancerRegistry.getDefaultRegistry()immediately. If no provideris found, it throws
IllegalArgumentExceptionwith a descriptivemessage.
Changes
ManagedChannelImplBuilder.java: Add registry lookup andvalidation in
defaultLoadBalancingPolicy()ManagedChannelBuilder.java: Update Javadoc to document thenew
IllegalArgumentExceptionbehaviorManagedChannelImplBuilderTest.java: Register a fakeLoadBalancerProviderfor test isolation and add test for unregisteredpolicy name