New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
xds: implement WeightedTargetLoadBalancer #6731
Conversation
dc8da34
to
829babc
Compare
829babc
to
2cf623e
Compare
xds/src/main/java/io/grpc/xds/WeightedTargetLoadBalancerProvider.java
Outdated
Show resolved
Hide resolved
xds/src/main/java/io/grpc/xds/WeightedTargetLoadBalancerProvider.java
Outdated
Show resolved
Hide resolved
xds/src/main/java/io/grpc/xds/WeightedTargetLoadBalancerProvider.java
Outdated
Show resolved
Hide resolved
xds/src/main/java/io/grpc/xds/WeightedTargetLoadBalancerProvider.java
Outdated
Show resolved
Hide resolved
} | ||
|
||
@Nullable | ||
private ConnectivityState aggregateState( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to make a utility method for this, as it is used in a couple of places (for any load balancers that contain a bunch of child balancers).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is related to #6650. Might do it after the design in general is clear.
@@ -62,12 +62,12 @@ public String toString() { | |||
} | |||
} | |||
|
|||
InterLocalityPicker(List<WeightedChildPicker> weightedChildPickers) { | |||
WeightedRandomPicker(List<WeightedChildPicker> weightedChildPickers) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class structure does not help with testing at all, as in order to provide your own random number generator in test, you have to implement a fake WeightedPickerFactory
instance whose picker(...)
method creates a fake SubchannelPicker
. So everything about picking is fake. This tests nothing.
Instead, a better strategy is to not have this constructor at all. You implementation code should always call the constructor with injectable random number generator. In that way, your test code is still executing the real WeightedRandomPicker
but only with the random number generator being faked out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WeightedPickerFactory
does not claim to test anything that WeightedRandomPicker
does. What WeightedRandomPicker
does is well-tested in its own test with a fake random. Using WeightedPickerFactory
in LocalityStoreTest
/WeightedTargetLoadBalancerTest
is to avoid re-testing WeightedRandomPicker
. Passing a fake random instead of the factory toLocalityStore
/WeightedTargetLoadBalancer
in test, you need recalculate the relation between the weights and the fake random number, that's basically following the implementation details of WeightedRandomPicker
.
We have argued about this a lot of times. When class A
uses class B
where B
is well-tested with its own BTest
. Then ATest
will just use BFactory
. BFactory
does not claim to test anything of B
. ATest
does not claim to test any implementation detail encapsulated in B
Using B
's constructor arg instead of BFactory
in ATest
, you are retesting B
again.
Now I revised the class not to use a factory, but I'm still not going to retest the well-encapsulated and well-tested class before EdsLoadBalancer
is refactored to use WeightedTargetLoadBalancer
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test strategy is poor. It should be used only when the subcomponent B
is heavy (i.e., small interface with large implementation). WeightedRandomPicker
is completely an internal implementation of WeightedTargetLoadBalancer
/LocalityStore
, even though you write it as a modularized subcomponent. It's better even if WeightedRandomPicker
is inlined in WeightedTargetLoadBalancer
/LocalityStore
. But in terms of code reuse, put it into a modularized class. Its implementation is not complex (even somewhat trivial), it's just using a random number generator to choose something in an input list. It is more like a utility. We are not keenly interested in testing the isolated behaviors of this sub-component. Instead, we are more interested in the real output of WeigtedTargetLoadBalancer
/LocalityStore
, which is the content of picker that eventually propagated to Channel (i.e., helper.updateBalancingState(State, Picker)
).
The above is just my opinion. I am not going to block you on tests, as long as the class structure is obviously inappropriate. But really, existing tests test barely little real things...
WeightedTargetLoadBalancer(Helper helper) { | ||
this( | ||
checkNotNull(helper, "helper"), | ||
WeightedRandomPicker.RANDOM_PICKER_FACTORY); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks bad... You should rather call constructor to create a factory or call the factory class to get the instance. At least, not something that is not the factory itself.
5a7e732
to
ca310f1
Compare
ca310f1
to
9b4ed3d
Compare
InterLocalityPicker
to reuse theRandomWeightedPicker
.