use of io.grpc.xds.WeightedRandomPicker.WeightedChildPicker in project grpc-java by grpc.
the class WeightedTargetLoadBalancerTest method balancingStateUpdatedFromChildBalancers.
@Test
public void balancingStateUpdatedFromChildBalancers() {
Map<String, WeightedPolicySelection> targets = ImmutableMap.of(// {foo, 10, config0}
"target0", weightedLbConfig0, // {bar, 20, config1}
"target1", weightedLbConfig1, // {bar, 30, config2}
"target2", weightedLbConfig2, // {foo, 40, config3}
"target3", weightedLbConfig3);
weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
// Subchannels to be created for each child balancer.
final SubchannelPicker[] subchannelPickers = new SubchannelPicker[] { mock(SubchannelPicker.class), mock(SubchannelPicker.class), mock(SubchannelPicker.class), mock(SubchannelPicker.class) };
final SubchannelPicker[] failurePickers = new SubchannelPicker[] { new ErrorPicker(Status.CANCELLED), new ErrorPicker(Status.ABORTED), new ErrorPicker(Status.DATA_LOSS), new ErrorPicker(Status.DATA_LOSS) };
ArgumentCaptor<SubchannelPicker> pickerCaptor = ArgumentCaptor.forClass(null);
// One child balancer goes to TRANSIENT_FAILURE.
childHelpers.get(1).updateBalancingState(TRANSIENT_FAILURE, failurePickers[1]);
verify(helper, never()).updateBalancingState(eq(TRANSIENT_FAILURE), any(SubchannelPicker.class));
verify(helper, times(2)).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
// Another child balancer goes to READY.
childHelpers.get(2).updateBalancingState(READY, subchannelPickers[2]);
verify(helper).updateBalancingState(eq(READY), pickerCaptor.capture());
assertThat(pickerCaptor.getValue()).isInstanceOf(WeightedRandomPicker.class);
WeightedRandomPicker overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[2], subchannelPickers[2]));
// Another child balancer goes to READY.
childHelpers.get(3).updateBalancingState(READY, subchannelPickers[3]);
verify(helper, times(2)).updateBalancingState(eq(READY), pickerCaptor.capture());
overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[2], subchannelPickers[2]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
// Another child balancer goes to READY.
childHelpers.get(0).updateBalancingState(READY, subchannelPickers[0]);
verify(helper, times(3)).updateBalancingState(eq(READY), pickerCaptor.capture());
overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], subchannelPickers[0]), new WeightedChildPicker(weights[2], subchannelPickers[2]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
// One of READY child balancers goes to TRANSIENT_FAILURE.
childHelpers.get(2).updateBalancingState(TRANSIENT_FAILURE, failurePickers[2]);
verify(helper, times(4)).updateBalancingState(eq(READY), pickerCaptor.capture());
overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], subchannelPickers[0]), new WeightedChildPicker(weights[3], subchannelPickers[3]));
// All child balancers go to TRANSIENT_FAILURE.
childHelpers.get(3).updateBalancingState(TRANSIENT_FAILURE, failurePickers[3]);
childHelpers.get(0).updateBalancingState(TRANSIENT_FAILURE, failurePickers[0]);
verify(helper).updateBalancingState(eq(TRANSIENT_FAILURE), pickerCaptor.capture());
overallPicker = (WeightedRandomPicker) pickerCaptor.getValue();
assertThat(overallPicker.weightedChildPickers).containsExactly(new WeightedChildPicker(weights[0], failurePickers[0]), new WeightedChildPicker(weights[1], failurePickers[1]), new WeightedChildPicker(weights[2], failurePickers[2]), new WeightedChildPicker(weights[3], failurePickers[3]));
}
use of io.grpc.xds.WeightedRandomPicker.WeightedChildPicker in project grpc-java by grpc.
the class WeightedTargetLoadBalancer method updateOverallBalancingState.
private void updateOverallBalancingState() {
List<WeightedChildPicker> childPickers = new ArrayList<>();
ConnectivityState overallState = null;
List<WeightedChildPicker> errorPickers = new ArrayList<>();
for (String name : targets.keySet()) {
ChildHelper childHelper = childHelpers.get(name);
ConnectivityState childState = childHelper.currentState;
overallState = aggregateState(overallState, childState);
int weight = targets.get(name).weight;
if (READY == childState) {
childPickers.add(new WeightedChildPicker(weight, childHelper.currentPicker));
} else if (TRANSIENT_FAILURE == childState) {
errorPickers.add(new WeightedChildPicker(weight, childHelper.currentPicker));
}
}
SubchannelPicker picker;
if (childPickers.isEmpty()) {
if (overallState == TRANSIENT_FAILURE) {
picker = new WeightedRandomPicker(errorPickers);
} else {
picker = XdsSubchannelPickers.BUFFER_PICKER;
}
} else {
picker = new WeightedRandomPicker(childPickers);
}
if (overallState != null) {
helper.updateBalancingState(overallState, picker);
}
}
use of io.grpc.xds.WeightedRandomPicker.WeightedChildPicker in project grpc-java by grpc.
the class WeightedRandomPickerTest method pickWithFakeRandom.
@Test
public void pickWithFakeRandom() {
WeightedChildPicker weightedChildPicker0 = new WeightedChildPicker(0, childPicker0);
WeightedChildPicker weightedChildPicker1 = new WeightedChildPicker(15, childPicker1);
WeightedChildPicker weightedChildPicker2 = new WeightedChildPicker(0, childPicker2);
WeightedChildPicker weightedChildPicker3 = new WeightedChildPicker(10, childPicker3);
WeightedRandomPicker xdsPicker = new WeightedRandomPicker(Arrays.asList(weightedChildPicker0, weightedChildPicker1, weightedChildPicker2, weightedChildPicker3), fakeRandom);
fakeRandom.nextInt = 0;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult1);
assertThat(fakeRandom.bound).isEqualTo(25);
fakeRandom.nextInt = 1;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult1);
assertThat(fakeRandom.bound).isEqualTo(25);
fakeRandom.nextInt = 14;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult1);
assertThat(fakeRandom.bound).isEqualTo(25);
fakeRandom.nextInt = 15;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult3);
assertThat(fakeRandom.bound).isEqualTo(25);
fakeRandom.nextInt = 24;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult3);
assertThat(fakeRandom.bound).isEqualTo(25);
}
use of io.grpc.xds.WeightedRandomPicker.WeightedChildPicker in project grpc-java by grpc.
the class WeightedRandomPickerTest method allZeroWeights.
@Test
public void allZeroWeights() {
WeightedChildPicker weightedChildPicker0 = new WeightedChildPicker(0, childPicker0);
WeightedChildPicker weightedChildPicker1 = new WeightedChildPicker(0, childPicker1);
WeightedChildPicker weightedChildPicker2 = new WeightedChildPicker(0, childPicker2);
WeightedChildPicker weightedChildPicker3 = new WeightedChildPicker(0, childPicker3);
WeightedRandomPicker xdsPicker = new WeightedRandomPicker(Arrays.asList(weightedChildPicker0, weightedChildPicker1, weightedChildPicker2, weightedChildPicker3), fakeRandom);
fakeRandom.nextInt = 0;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult0);
assertThat(fakeRandom.bound).isEqualTo(4);
fakeRandom.nextInt = 1;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult1);
assertThat(fakeRandom.bound).isEqualTo(4);
fakeRandom.nextInt = 2;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult2);
assertThat(fakeRandom.bound).isEqualTo(4);
fakeRandom.nextInt = 3;
assertThat(xdsPicker.pickSubchannel(pickSubchannelArgs)).isSameInstanceAs(pickResult3);
assertThat(fakeRandom.bound).isEqualTo(4);
}
Aggregations