use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection in project grpc-java by grpc.
the class WeightedTargetLoadBalancerTest method handleResolvedAddresses.
@Test
public void handleResolvedAddresses() {
ArgumentCaptor<ResolvedAddresses> resolvedAddressesCaptor = ArgumentCaptor.forClass(null);
Attributes.Key<Object> fakeKey = Attributes.Key.create("fake_key");
Object fakeValue = new Object();
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);
EquivalentAddressGroup eag0 = new EquivalentAddressGroup(socketAddresses[0]);
eag0 = AddressFilter.setPathFilter(eag0, ImmutableList.of("target0"));
EquivalentAddressGroup eag1 = new EquivalentAddressGroup(socketAddresses[1]);
eag1 = AddressFilter.setPathFilter(eag1, ImmutableList.of("target1"));
EquivalentAddressGroup eag2 = new EquivalentAddressGroup(socketAddresses[2]);
eag2 = AddressFilter.setPathFilter(eag2, ImmutableList.of("target2"));
EquivalentAddressGroup eag3 = new EquivalentAddressGroup(socketAddresses[3]);
eag3 = AddressFilter.setPathFilter(eag3, ImmutableList.of("target3"));
weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.of(eag0, eag1, eag2, eag3)).setAttributes(Attributes.newBuilder().set(fakeKey, fakeValue).build()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(targets)).build());
verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
assertThat(childBalancers).hasSize(4);
assertThat(childHelpers).hasSize(4);
assertThat(fooLbCreated).isEqualTo(2);
assertThat(barLbCreated).isEqualTo(2);
for (int i = 0; i < childBalancers.size(); i++) {
verify(childBalancers.get(i)).handleResolvedAddresses(resolvedAddressesCaptor.capture());
ResolvedAddresses resolvedAddresses = resolvedAddressesCaptor.getValue();
assertThat(resolvedAddresses.getLoadBalancingPolicyConfig()).isEqualTo(configs[i]);
assertThat(resolvedAddresses.getAttributes().get(fakeKey)).isEqualTo(fakeValue);
assertThat(Iterables.getOnlyElement(resolvedAddresses.getAddresses()).getAddresses()).containsExactly(socketAddresses[i]);
}
// Update new weighted target config for a typical workflow.
// target0 removed. target1, target2, target3 changed weight and config. target4 added.
int[] newWeights = new int[] { 11, 22, 33, 44 };
Object[] newConfigs = new Object[] { "newConfig1", "newConfig2", "newConfig3", "newConfig4" };
Map<String, WeightedPolicySelection> newTargets = ImmutableMap.of("target1", new WeightedPolicySelection(newWeights[0], new PolicySelection(barLbProvider, newConfigs[0])), "target2", new WeightedPolicySelection(newWeights[1], new PolicySelection(barLbProvider, newConfigs[1])), "target3", new WeightedPolicySelection(newWeights[2], new PolicySelection(fooLbProvider, newConfigs[2])), "target4", new WeightedPolicySelection(newWeights[3], new PolicySelection(fooLbProvider, newConfigs[3])));
weightedTargetLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(new WeightedTargetConfig(newTargets)).build());
verify(helper, atLeast(2)).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
assertThat(childBalancers).hasSize(5);
assertThat(childHelpers).hasSize(5);
// One more foo LB created for target4
assertThat(fooLbCreated).isEqualTo(3);
assertThat(barLbCreated).isEqualTo(2);
verify(childBalancers.get(0)).shutdown();
for (int i = 1; i < childBalancers.size(); i++) {
verify(childBalancers.get(i), atLeastOnce()).handleResolvedAddresses(resolvedAddressesCaptor.capture());
assertThat(resolvedAddressesCaptor.getValue().getLoadBalancingPolicyConfig()).isEqualTo(newConfigs[i - 1]);
}
}
use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection in project grpc-java by grpc.
the class WeightedTargetLoadBalancerProviderTest method parseWeightedTargetConfig.
@Test
public void parseWeightedTargetConfig() throws Exception {
LoadBalancerRegistry lbRegistry = new LoadBalancerRegistry();
WeightedTargetLoadBalancerProvider weightedTargetLoadBalancerProvider = new WeightedTargetLoadBalancerProvider(lbRegistry);
final Object fooConfig = new Object();
LoadBalancerProvider lbProviderFoo = new LoadBalancerProvider() {
@Override
public boolean isAvailable() {
return true;
}
@Override
public int getPriority() {
return 5;
}
@Override
public String getPolicyName() {
return "foo_policy";
}
@Override
public LoadBalancer newLoadBalancer(Helper helper) {
return mock(LoadBalancer.class);
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
return ConfigOrError.fromConfig(fooConfig);
}
};
final Object barConfig = new Object();
LoadBalancerProvider lbProviderBar = new LoadBalancerProvider() {
@Override
public boolean isAvailable() {
return true;
}
@Override
public int getPriority() {
return 5;
}
@Override
public String getPolicyName() {
return "bar_policy";
}
@Override
public LoadBalancer newLoadBalancer(Helper helper) {
return mock(LoadBalancer.class);
}
@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
return ConfigOrError.fromConfig(barConfig);
}
};
lbRegistry.register(lbProviderFoo);
lbRegistry.register(lbProviderBar);
String weightedTargetConfigJson = ("{" + " 'targets' : {" + " 'target_1' : {" + " 'weight' : 10," + " 'childPolicy' : [" + " {'unsupported_policy' : {}}," + " {'foo_policy' : {}}" + " ]" + " }," + " 'target_2' : {" + " 'weight' : 20," + " 'childPolicy' : [" + " {'unsupported_policy' : {}}," + " {'bar_policy' : {}}" + " ]" + " }" + " }" + "}").replace('\'', '"');
@SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(weightedTargetConfigJson);
ConfigOrError parsedConfig = weightedTargetLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
ConfigOrError expectedConfig = ConfigOrError.fromConfig(new WeightedTargetConfig(ImmutableMap.of("target_1", new WeightedPolicySelection(10, new PolicySelection(lbProviderFoo, fooConfig)), "target_2", new WeightedPolicySelection(20, new PolicySelection(lbProviderBar, barConfig)))));
assertThat(parsedConfig).isEqualTo(expectedConfig);
}
use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection in project grpc-java by grpc.
the class WeightedTargetLoadBalancer method handleResolvedAddresses.
@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
Object lbConfig = resolvedAddresses.getLoadBalancingPolicyConfig();
checkNotNull(lbConfig, "missing weighted_target lb config");
WeightedTargetConfig weightedTargetConfig = (WeightedTargetConfig) lbConfig;
Map<String, WeightedPolicySelection> newTargets = weightedTargetConfig.targets;
for (String targetName : newTargets.keySet()) {
WeightedPolicySelection weightedChildLbConfig = newTargets.get(targetName);
if (!targets.containsKey(targetName)) {
ChildHelper childHelper = new ChildHelper(targetName);
GracefulSwitchLoadBalancer childBalancer = new GracefulSwitchLoadBalancer(childHelper);
childBalancer.switchTo(weightedChildLbConfig.policySelection.getProvider());
childHelpers.put(targetName, childHelper);
childBalancers.put(targetName, childBalancer);
} else if (!weightedChildLbConfig.policySelection.getProvider().equals(targets.get(targetName).policySelection.getProvider())) {
childBalancers.get(targetName).switchTo(weightedChildLbConfig.policySelection.getProvider());
}
}
targets = newTargets;
for (String targetName : targets.keySet()) {
childBalancers.get(targetName).handleResolvedAddresses(resolvedAddresses.toBuilder().setAddresses(AddressFilter.filter(resolvedAddresses.getAddresses(), targetName)).setLoadBalancingPolicyConfig(targets.get(targetName).policySelection.getConfig()).build());
}
// TODO(zdapeng): cache removed target for 15 minutes.
for (String targetName : childBalancers.keySet()) {
if (!targets.containsKey(targetName)) {
childBalancers.get(targetName).shutdown();
}
}
childBalancers.keySet().retainAll(targets.keySet());
childHelpers.keySet().retainAll(targets.keySet());
updateOverallBalancingState();
}
use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection in project grpc-java by grpc.
the class ClusterResolverLoadBalancerTest method onlyEdsClusters_receivedEndpoints.
@Test
public void onlyEdsClusters_receivedEndpoints() {
ClusterResolverConfig config = new ClusterResolverConfig(Arrays.asList(edsDiscoveryMechanism1, edsDiscoveryMechanism2), roundRobin);
deliverLbConfig(config);
assertThat(xdsClient.watchers.keySet()).containsExactly(EDS_SERVICE_NAME1, EDS_SERVICE_NAME2);
assertThat(childBalancers).isEmpty();
// CLUSTER1 has priority 1 (priority3), which has locality 2, which has endpoint3.
// CLUSTER2 has priority 1 (priority1) and 2 (priority2); priority1 has locality1,
// which has endpoint1 and endpoint2; priority2 has locality3, which has endpoint4.
EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr-1");
EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr-2");
EquivalentAddressGroup endpoint3 = makeAddress("endpoint-addr-3");
EquivalentAddressGroup endpoint4 = makeAddress("endpoint-addr-4");
LocalityLbEndpoints localityLbEndpoints1 = LocalityLbEndpoints.create(Arrays.asList(LbEndpoint.create(endpoint1, 100, true), LbEndpoint.create(endpoint2, 100, true)), 70, /* localityWeight */
1);
LocalityLbEndpoints localityLbEndpoints2 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint3, 100, true)), 10, /* localityWeight */
1);
LocalityLbEndpoints localityLbEndpoints3 = LocalityLbEndpoints.create(Collections.singletonList(LbEndpoint.create(endpoint4, 100, true)), 20, /* localityWeight */
2);
String priority1 = CLUSTER2 + "[priority1]";
String priority2 = CLUSTER2 + "[priority2]";
String priority3 = CLUSTER1 + "[priority1]";
// CLUSTER2: locality1 with priority 1 and locality3 with priority 2.
xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME2, ImmutableMap.of(locality1, localityLbEndpoints1, locality3, localityLbEndpoints3));
// not created until all clusters resolved
assertThat(childBalancers).isEmpty();
// CLUSTER1: locality2 with priority 1.
xdsClient.deliverClusterLoadAssignment(EDS_SERVICE_NAME1, Collections.singletonMap(locality2, localityLbEndpoints2));
// Endpoints of all clusters have been resolved.
assertThat(childBalancers).hasSize(1);
FakeLoadBalancer childBalancer = Iterables.getOnlyElement(childBalancers);
assertThat(childBalancer.name).isEqualTo(PRIORITY_POLICY_NAME);
PriorityLbConfig priorityLbConfig = (PriorityLbConfig) childBalancer.config;
assertThat(priorityLbConfig.priorities).containsExactly(priority3, priority1, priority2).inOrder();
PriorityChildConfig priorityChildConfig1 = priorityLbConfig.childConfigs.get(priority1);
assertThat(priorityChildConfig1.ignoreReresolution).isTrue();
assertThat(priorityChildConfig1.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig clusterImplConfig1 = (ClusterImplConfig) priorityChildConfig1.policySelection.getConfig();
assertClusterImplConfig(clusterImplConfig1, CLUSTER2, EDS_SERVICE_NAME2, LRS_SERVER_INFO, 200L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
WeightedTargetConfig weightedTargetConfig1 = (WeightedTargetConfig) clusterImplConfig1.childPolicy.getConfig();
assertThat(weightedTargetConfig1.targets.keySet()).containsExactly(locality1.toString());
WeightedPolicySelection target1 = weightedTargetConfig1.targets.get(locality1.toString());
assertThat(target1.weight).isEqualTo(70);
assertThat(target1.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
PriorityChildConfig priorityChildConfig2 = priorityLbConfig.childConfigs.get(priority2);
assertThat(priorityChildConfig2.ignoreReresolution).isTrue();
assertThat(priorityChildConfig2.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig clusterImplConfig2 = (ClusterImplConfig) priorityChildConfig2.policySelection.getConfig();
assertClusterImplConfig(clusterImplConfig2, CLUSTER2, EDS_SERVICE_NAME2, LRS_SERVER_INFO, 200L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
WeightedTargetConfig weightedTargetConfig2 = (WeightedTargetConfig) clusterImplConfig2.childPolicy.getConfig();
assertThat(weightedTargetConfig2.targets.keySet()).containsExactly(locality3.toString());
WeightedPolicySelection target2 = weightedTargetConfig2.targets.get(locality3.toString());
assertThat(target2.weight).isEqualTo(20);
assertThat(target2.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
List<EquivalentAddressGroup> priorityAddrs1 = AddressFilter.filter(childBalancer.addresses, priority1);
assertThat(priorityAddrs1).hasSize(2);
assertAddressesEqual(Arrays.asList(endpoint1, endpoint2), priorityAddrs1);
List<EquivalentAddressGroup> priorityAddrs2 = AddressFilter.filter(childBalancer.addresses, priority2);
assertThat(priorityAddrs2).hasSize(1);
assertAddressesEqual(Collections.singletonList(endpoint4), priorityAddrs2);
PriorityChildConfig priorityChildConfig3 = priorityLbConfig.childConfigs.get(priority3);
assertThat(priorityChildConfig3.ignoreReresolution).isTrue();
assertThat(priorityChildConfig3.policySelection.getProvider().getPolicyName()).isEqualTo(CLUSTER_IMPL_POLICY_NAME);
ClusterImplConfig clusterImplConfig3 = (ClusterImplConfig) priorityChildConfig3.policySelection.getConfig();
assertClusterImplConfig(clusterImplConfig3, CLUSTER1, EDS_SERVICE_NAME1, LRS_SERVER_INFO, 100L, tlsContext, Collections.<DropOverload>emptyList(), WEIGHTED_TARGET_POLICY_NAME);
WeightedTargetConfig weightedTargetConfig3 = (WeightedTargetConfig) clusterImplConfig3.childPolicy.getConfig();
assertThat(weightedTargetConfig3.targets.keySet()).containsExactly(locality2.toString());
WeightedPolicySelection target3 = weightedTargetConfig3.targets.get(locality2.toString());
assertThat(target3.weight).isEqualTo(10);
assertThat(target3.policySelection.getProvider().getPolicyName()).isEqualTo("round_robin");
List<EquivalentAddressGroup> priorityAddrs3 = AddressFilter.filter(childBalancer.addresses, priority3);
assertThat(priorityAddrs3).hasSize(1);
assertAddressesEqual(Collections.singletonList(endpoint3), priorityAddrs3);
}
Aggregations