Search in sources :

Example 21 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ManagedChannelImplTest method nameResolverReturnsEmptySubLists_optionallyAllowed.

@Test
public void nameResolverReturnsEmptySubLists_optionallyAllowed() throws Exception {
    when(mockLoadBalancer.canHandleEmptyAddressListFromNameResolution()).thenReturn(true);
    // Pass a FakeNameResolverFactory with an empty list and LB config
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).build();
    String rawLbConfig = "{ \"setting1\": \"high\" }";
    Object parsedLbConfig = new Object();
    Map<String, Object> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [ {\"mock_lb\": " + rawLbConfig + " } ] }");
    ManagedChannelServiceConfig parsedServiceConfig = createManagedChannelServiceConfig(rawServiceConfig, new PolicySelection(mockLoadBalancerProvider, parsedLbConfig));
    nameResolverFactory.nextConfigOrError.set(ConfigOrError.fromConfig(parsedServiceConfig));
    channelBuilder.nameResolverFactory(nameResolverFactory);
    createChannel();
    // LoadBalancer received the empty list and the LB config
    verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class));
    ArgumentCaptor<ResolvedAddresses> resultCaptor = ArgumentCaptor.forClass(ResolvedAddresses.class);
    verify(mockLoadBalancer).handleResolvedAddresses(resultCaptor.capture());
    assertThat(resultCaptor.getValue().getAddresses()).isEmpty();
    assertThat(resultCaptor.getValue().getLoadBalancingPolicyConfig()).isEqualTo(parsedLbConfig);
    // A no resolution retry
    assertEquals(0, timer.numPendingTasks(NAME_RESOLVER_REFRESH_TASK_FILTER));
}
Also used : Helper(io.grpc.LoadBalancer.Helper) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ResolvedAddresses(io.grpc.LoadBalancer.ResolvedAddresses) Test(org.junit.Test)

Example 22 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ManagedChannelImplTest method channelTracing_serviceConfigChange.

@Test
public void channelTracing_serviceConfigChange() throws Exception {
    timer.forwardNanos(1234);
    channelBuilder.maxTraceEvents(10);
    List<EquivalentAddressGroup> servers = new ArrayList<>();
    servers.add(new EquivalentAddressGroup(socketAddress));
    FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri).setServers(servers).build();
    channelBuilder.nameResolverFactory(nameResolverFactory);
    createChannel();
    int prevSize = getStats(channel).channelTrace.events.size();
    ManagedChannelServiceConfig mcsc1 = createManagedChannelServiceConfig(ImmutableMap.<String, Object>of(), new PolicySelection(mockLoadBalancerProvider, null));
    ResolutionResult resolutionResult1 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult1);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
    assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
    prevSize = getStats(channel).channelTrace.events.size();
    ResolutionResult resolutionResult2 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(mcsc1)).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult2);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize);
    prevSize = getStats(channel).channelTrace.events.size();
    timer.forwardNanos(1234);
    ResolutionResult resolutionResult3 = ResolutionResult.newBuilder().setAddresses(Collections.singletonList(new EquivalentAddressGroup(Arrays.asList(new SocketAddress() {
    }, new SocketAddress() {
    })))).setServiceConfig(ConfigOrError.fromConfig(ManagedChannelServiceConfig.empty())).build();
    nameResolverFactory.resolvers.get(0).listener.onResult(resolutionResult3);
    assertThat(getStats(channel).channelTrace.events).hasSize(prevSize + 1);
    assertThat(getStats(channel).channelTrace.events.get(prevSize)).isEqualTo(new ChannelTrace.Event.Builder().setDescription("Service config changed").setSeverity(ChannelTrace.Event.Severity.CT_INFO).setTimestampNanos(timer.getTicker().read()).build());
}
Also used : EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) ResolutionResult(io.grpc.NameResolver.ResolutionResult) ArrayList(java.util.ArrayList) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) Test(org.junit.Test)

Example 23 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class ClusterManagerLoadBalancerProviderTest method parseLoadBalancingConfig_valid.

@Test
public void parseLoadBalancingConfig_valid() throws IOException {
    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) {
            throw new UnsupportedOperationException("Should not be called");
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
            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) {
            throw new UnsupportedOperationException("Should not be called");
        }

        @Override
        public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawLoadBalancingPolicyConfig) {
            return ConfigOrError.fromConfig(barConfig);
        }
    };
    lbRegistry.register(lbProviderFoo);
    lbRegistry.register(lbProviderBar);
    String clusterManagerConfigJson = "{\n" + "  \"childPolicy\": {\n" + "    \"child1\": {\n" + "      \"lbPolicy\": [\n" + "        {\n" + "          \"foo_policy\": {" + "            \"config_name\": \"config_value\"\n" + "          }\n" + "        }\n" + "      ]\n" + "    },\n" + "    \"child2\": {\n" + "      \"lbPolicy\": [\n" + "        {\n" + "          \"bar_policy\": {}\n" + "        }, {\n" + "          \"unsupported\": {}\n" + "        }\n" + "      ]\n" + "    }\n" + "  }\n" + "}";
    @SuppressWarnings("unchecked") Map<String, ?> rawLbConfigMap = (Map<String, ?>) JsonParser.parse(clusterManagerConfigJson);
    ConfigOrError configOrError = provider.parseLoadBalancingPolicyConfig(rawLbConfigMap);
    assertThat(configOrError.getConfig()).isNotNull();
    ClusterManagerConfig config = (ClusterManagerConfig) configOrError.getConfig();
    assertThat(config.childPolicies).containsExactly("child1", new PolicySelection(lbProviderFoo, fooConfig), "child2", new PolicySelection(lbProviderBar, barConfig));
}
Also used : Helper(io.grpc.LoadBalancer.Helper) ClusterManagerConfig(io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Map(java.util.Map) Test(org.junit.Test)

Example 24 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection 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 25 with PolicySelection

use of io.grpc.internal.ServiceConfigUtil.PolicySelection in project grpc-java by grpc.

the class PriorityLoadBalancerTest method readyToConnectDoesNotFailOverButUpdatesPicker.

@Test
public void readyToConnectDoesNotFailOverButUpdatesPicker() {
    PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityLbConfig priorityLbConfig = new PriorityLbConfig(ImmutableMap.of("p0", priorityChildConfig0, "p1", priorityChildConfig1), ImmutableList.of("p0", "p1"));
    priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).build());
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
    Helper helper0 = Iterables.getOnlyElement(fooHelpers);
    // p0 gets READY.
    final Subchannel subchannel0 = mock(Subchannel.class);
    helper0.updateBalancingState(READY, new SubchannelPicker() {

        @Override
        public PickResult pickSubchannel(PickSubchannelArgs args) {
            return PickResult.withSubchannel(subchannel0);
        }
    });
    assertCurrentPickerPicksSubchannel(subchannel0);
    // p0 goes to CONNECTING
    helper0.updateBalancingState(IDLE, BUFFER_PICKER);
    assertCurrentPickerIsBufferPicker();
    // no failover happened
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
    // resolution update without priority change does not trigger failover
    Attributes.Key<String> fooKey = Attributes.Key.create("fooKey");
    priorityLb.handleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(ImmutableList.<EquivalentAddressGroup>of()).setLoadBalancingPolicyConfig(priorityLbConfig).setAttributes(Attributes.newBuilder().set(fooKey, "barVal").build()).build());
    assertCurrentPickerIsBufferPicker();
    // no failover happened
    assertThat(fooBalancers).hasSize(1);
    assertThat(fooHelpers).hasSize(1);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) Attributes(io.grpc.Attributes) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) Subchannel(io.grpc.LoadBalancer.Subchannel) PickResult(io.grpc.LoadBalancer.PickResult) PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) Test(org.junit.Test)

Aggregations

PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)36 Test (org.junit.Test)27 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)20 LoadBalancerProvider (io.grpc.LoadBalancerProvider)13 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)12 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)12 Helper (io.grpc.LoadBalancer.Helper)10 ConfigOrError (io.grpc.NameResolver.ConfigOrError)10 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)10 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)10 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)9 Subchannel (io.grpc.LoadBalancer.Subchannel)8 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)7 PickResult (io.grpc.LoadBalancer.PickResult)6 LoadBalancer (io.grpc.LoadBalancer)5 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 SocketAddress (java.net.SocketAddress)4 Map (java.util.Map)4 Attributes (io.grpc.Attributes)3