Search in sources :

Example 11 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig 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]);
    }
}
Also used : Attributes(io.grpc.Attributes) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 12 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig 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);
}
Also used : WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Helper(io.grpc.LoadBalancer.Helper) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ConfigOrError(io.grpc.NameResolver.ConfigOrError) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig 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();
}
Also used : GracefulSwitchLoadBalancer(io.grpc.util.GracefulSwitchLoadBalancer) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)

Example 14 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method endpointAddressesAttachedWithClusterName.

@Test
public void endpointAddressesAttachedWithClusterName() {
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    // One locality with two endpoints.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    // Simulates leaf load balancer creating subchannels.
    CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
    Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_CLUSTER_NAME)).isEqualTo(CLUSTER);
    }
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) Test(org.junit.Test)

Example 15 with WeightedTargetConfig

use of io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig in project grpc-java by grpc.

the class ClusterImplLoadBalancerTest method subtest_endpointAddressesAttachedWithTlsConfig.

private void subtest_endpointAddressesAttachedWithTlsConfig(boolean enableSecurity) {
    UpstreamTlsContext upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe", true);
    LoadBalancerProvider weightedTargetProvider = new WeightedTargetLoadBalancerProvider();
    WeightedTargetConfig weightedTargetConfig = buildWeightedTargetConfig(ImmutableMap.of(locality, 10));
    ClusterImplConfig config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), upstreamTlsContext);
    // One locality with two endpoints.
    EquivalentAddressGroup endpoint1 = makeAddress("endpoint-addr1", locality);
    EquivalentAddressGroup endpoint2 = makeAddress("endpoint-addr2", locality);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    // one leaf balancer
    assertThat(downstreamBalancers).hasSize(1);
    FakeLoadBalancer leafBalancer = Iterables.getOnlyElement(downstreamBalancers);
    assertThat(leafBalancer.name).isEqualTo("round_robin");
    // Simulates leaf load balancer creating subchannels.
    CreateSubchannelArgs args = CreateSubchannelArgs.newBuilder().setAddresses(leafBalancer.addresses).build();
    Subchannel subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
        } else {
            assertThat(supplier).isNull();
        }
    }
    // Removes UpstreamTlsContext from the config.
    config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), null);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    assertThat(Iterables.getOnlyElement(downstreamBalancers)).isSameInstanceAs(leafBalancer);
    // creates new connections
    subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        assertThat(eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER)).isNull();
    }
    // Config with a new UpstreamTlsContext.
    upstreamTlsContext = CommonTlsContextTestsUtil.buildUpstreamTlsContext("google_cloud_private_spiffe1", true);
    config = new ClusterImplConfig(CLUSTER, EDS_SERVICE_NAME, LRS_SERVER_INFO, null, Collections.<DropOverload>emptyList(), new PolicySelection(weightedTargetProvider, weightedTargetConfig), upstreamTlsContext);
    deliverAddressesAndConfig(Arrays.asList(endpoint1, endpoint2), config);
    assertThat(Iterables.getOnlyElement(downstreamBalancers)).isSameInstanceAs(leafBalancer);
    // creates new connections
    subchannel = leafBalancer.helper.createSubchannel(args);
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.isShutdown()).isFalse();
            assertThat(supplier.getTlsContext()).isEqualTo(upstreamTlsContext);
        } else {
            assertThat(supplier).isNull();
        }
    }
    loadBalancer.shutdown();
    for (EquivalentAddressGroup eag : subchannel.getAllAddresses()) {
        SslContextProviderSupplier supplier = eag.getAttributes().get(InternalXdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
        if (enableSecurity) {
            assertThat(supplier.isShutdown()).isTrue();
        }
    }
    loadBalancer = null;
}
Also used : CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) UpstreamTlsContext(io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) DropOverload(io.grpc.xds.Endpoints.DropOverload) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) SslContextProviderSupplier(io.grpc.xds.internal.sds.SslContextProviderSupplier)

Aggregations

WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)17 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)15 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)13 Test (org.junit.Test)11 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)9 LoadBalancerProvider (io.grpc.LoadBalancerProvider)8 Subchannel (io.grpc.LoadBalancer.Subchannel)6 PickResult (io.grpc.LoadBalancer.PickResult)5 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)5 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)3 Metadata (io.grpc.Metadata)3 ClusterResolverConfig (io.grpc.xds.ClusterResolverLoadBalancerProvider.ClusterResolverConfig)3 LocalityLbEndpoints (io.grpc.xds.Endpoints.LocalityLbEndpoints)3 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)3 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 Helper (io.grpc.LoadBalancer.Helper)2