Search in sources :

Example 16 with PolicySelection

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

the class PriorityLoadBalancerTest method raceBetweenShutdownAndChildLbBalancingStateUpdate.

@Test
public void raceBetweenShutdownAndChildLbBalancingStateUpdate() {
    PriorityChildConfig priorityChildConfig0 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), true);
    PriorityChildConfig priorityChildConfig1 = new PriorityChildConfig(new PolicySelection(fooLbProvider, new Object()), false);
    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());
    verify(helper).updateBalancingState(eq(CONNECTING), eq(BUFFER_PICKER));
    // LB shutdown and subchannel state change can happen simultaneously. If shutdown runs first,
    // any further balancing state update should be ignored.
    priorityLb.shutdown();
    // priority p0
    Helper priorityHelper0 = Iterables.getOnlyElement(fooHelpers);
    priorityHelper0.updateBalancingState(READY, mock(SubchannelPicker.class));
    verifyNoMoreInteractions(helper);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SubchannelPicker(io.grpc.LoadBalancer.SubchannelPicker) PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) PriorityLbConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) Test(org.junit.Test)

Example 17 with PolicySelection

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

the class ManagedChannelImplTest method badServiceConfigIsRecoverable.

@Test
public void badServiceConfigIsRecoverable() throws Exception {
    final List<EquivalentAddressGroup> addresses = ImmutableList.of(new EquivalentAddressGroup(new SocketAddress() {
    }));
    final class FakeNameResolver extends NameResolver {

        Listener2 listener;

        @Override
        public String getServiceAuthority() {
            return "also fake";
        }

        @Override
        public void start(Listener2 listener) {
            this.listener = listener;
            listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromError(Status.INTERNAL.withDescription("kaboom is invalid"))).build());
        }

        @Override
        public void shutdown() {
        }
    }
    final class FakeNameResolverFactory2 extends NameResolver.Factory {

        FakeNameResolver resolver;

        @Nullable
        @Override
        public NameResolver newNameResolver(URI targetUri, NameResolver.Args args) {
            return (resolver = new FakeNameResolver());
        }

        @Override
        public String getDefaultScheme() {
            return "fake";
        }
    }
    FakeNameResolverFactory2 factory = new FakeNameResolverFactory2();
    ManagedChannelImplBuilder customBuilder = new ManagedChannelImplBuilder(TARGET, new ClientTransportFactoryBuilder() {

        @Override
        public ClientTransportFactory buildClientTransportFactory() {
            return mockTransportFactory;
        }
    }, null);
    customBuilder.executorPool = executorPool;
    customBuilder.channelz = channelz;
    ManagedChannel mychannel = customBuilder.nameResolverFactory(factory).build();
    ClientCall<Void, Void> call1 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT);
    ListenableFuture<Void> future1 = ClientCalls.futureUnaryCall(call1, null);
    executor.runDueTasks();
    try {
        future1.get(1, TimeUnit.SECONDS);
        Assert.fail();
    } catch (ExecutionException e) {
        assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("kaboom");
    }
    // ok the service config is bad, let's fix it.
    Map<String, Object> rawServiceConfig = parseConfig("{\"loadBalancingConfig\": [{\"round_robin\": {}}]}");
    Object fakeLbConfig = new Object();
    PolicySelection lbConfigs = new PolicySelection(mockLoadBalancerProvider, fakeLbConfig);
    mockLoadBalancerProvider.parseLoadBalancingPolicyConfig(rawServiceConfig);
    ManagedChannelServiceConfig managedChannelServiceConfig = createManagedChannelServiceConfig(rawServiceConfig, lbConfigs);
    factory.resolver.listener.onResult(ResolutionResult.newBuilder().setAddresses(addresses).setServiceConfig(ConfigOrError.fromConfig(managedChannelServiceConfig)).build());
    ClientCall<Void, Void> call2 = mychannel.newCall(TestMethodDescriptors.voidMethod(), CallOptions.DEFAULT.withDeadlineAfter(5, TimeUnit.SECONDS));
    ListenableFuture<Void> future2 = ClientCalls.futureUnaryCall(call2, null);
    timer.forwardTime(1234, TimeUnit.SECONDS);
    executor.runDueTasks();
    try {
        future2.get();
        Assert.fail();
    } catch (ExecutionException e) {
        assertThat(Throwables.getStackTraceAsString(e.getCause())).contains("deadline");
    }
    mychannel.shutdownNow();
}
Also used : PickSubchannelArgs(io.grpc.LoadBalancer.PickSubchannelArgs) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) URI(java.net.URI) UnsupportedClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.UnsupportedClientTransportFactoryBuilder) ClientTransportFactoryBuilder(io.grpc.internal.ManagedChannelImplBuilder.ClientTransportFactoryBuilder) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) ManagedChannel(io.grpc.ManagedChannel) ProxiedSocketAddress(io.grpc.ProxiedSocketAddress) SocketAddress(java.net.SocketAddress) ExecutionException(java.util.concurrent.ExecutionException) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 18 with PolicySelection

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

the class ClusterResolverLoadBalancer method generateEdsBasedPriorityChildConfigs.

/**
 * Generates configs to be used in the priority LB policy for priorities in an EDS cluster.
 *
 * <p>priority LB -> cluster_impl LB (one per priority) -> (weighted_target LB
 * -> round_robin / least_request_experimental (one per locality)) / ring_hash_experimental
 */
private static Map<String, PriorityChildConfig> generateEdsBasedPriorityChildConfigs(String cluster, @Nullable String edsServiceName, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext, PolicySelection endpointLbPolicy, LoadBalancerRegistry lbRegistry, Map<String, Map<Locality, Integer>> prioritizedLocalityWeights, List<DropOverload> dropOverloads) {
    Map<String, PriorityChildConfig> configs = new HashMap<>();
    for (String priority : prioritizedLocalityWeights.keySet()) {
        PolicySelection leafPolicy = endpointLbPolicy;
        // product of each endpoint's weight and the weight of the locality it belongs to.
        if (endpointLbPolicy.getProvider().getPolicyName().equals("round_robin") || endpointLbPolicy.getProvider().getPolicyName().equals("least_request_experimental")) {
            Map<Locality, Integer> localityWeights = prioritizedLocalityWeights.get(priority);
            Map<String, WeightedPolicySelection> targets = new HashMap<>();
            for (Locality locality : localityWeights.keySet()) {
                int weight = localityWeights.get(locality);
                WeightedPolicySelection target = new WeightedPolicySelection(weight, endpointLbPolicy);
                targets.put(localityName(locality), target);
            }
            LoadBalancerProvider weightedTargetLbProvider = lbRegistry.getProvider(WEIGHTED_TARGET_POLICY_NAME);
            WeightedTargetConfig weightedTargetConfig = new WeightedTargetConfig(Collections.unmodifiableMap(targets));
            leafPolicy = new PolicySelection(weightedTargetLbProvider, weightedTargetConfig);
        }
        ClusterImplConfig clusterImplConfig = new ClusterImplConfig(cluster, edsServiceName, lrsServerInfo, maxConcurrentRequests, dropOverloads, leafPolicy, tlsContext);
        LoadBalancerProvider clusterImplLbProvider = lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
        PolicySelection clusterImplPolicy = new PolicySelection(clusterImplLbProvider, clusterImplConfig);
        PriorityChildConfig priorityChildConfig = new PriorityChildConfig(clusterImplPolicy, true);
        configs.put(priority, priorityChildConfig);
    }
    return configs;
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) HashMap(java.util.HashMap) WeightedTargetConfig(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)

Example 19 with PolicySelection

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

the class ClusterResolverLoadBalancer method generateDnsBasedPriorityChildConfig.

/**
 * Generates the config to be used in the priority LB policy for the single priority of
 * logical DNS cluster.
 *
 * <p>priority LB -> cluster_impl LB (single hardcoded priority) -> pick_first
 */
private static PriorityChildConfig generateDnsBasedPriorityChildConfig(String cluster, @Nullable ServerInfo lrsServerInfo, @Nullable Long maxConcurrentRequests, @Nullable UpstreamTlsContext tlsContext, LoadBalancerRegistry lbRegistry, List<DropOverload> dropOverloads) {
    // Override endpoint-level LB policy with pick_first for logical DNS cluster.
    PolicySelection endpointLbPolicy = new PolicySelection(lbRegistry.getProvider("pick_first"), null);
    ClusterImplConfig clusterImplConfig = new ClusterImplConfig(cluster, null, lrsServerInfo, maxConcurrentRequests, dropOverloads, endpointLbPolicy, tlsContext);
    LoadBalancerProvider clusterImplLbProvider = lbRegistry.getProvider(XdsLbPolicies.CLUSTER_IMPL_POLICY_NAME);
    PolicySelection clusterImplPolicy = new PolicySelection(clusterImplLbProvider, clusterImplConfig);
    return new PriorityChildConfig(clusterImplPolicy, false);
}
Also used : PriorityChildConfig(io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) WeightedPolicySelection(io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) ClusterImplConfig(io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)

Example 20 with PolicySelection

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

the class ClusterManagerLoadBalancerProvider method parseLoadBalancingPolicyConfig.

@Override
public ConfigOrError parseLoadBalancingPolicyConfig(Map<String, ?> rawConfig) {
    Map<String, PolicySelection> parsedChildPolicies = new LinkedHashMap<>();
    try {
        Map<String, ?> childPolicies = JsonUtil.getObject(rawConfig, "childPolicy");
        if (childPolicies == null || childPolicies.isEmpty()) {
            return ConfigOrError.fromError(Status.INTERNAL.withDescription("No child policy provided for cluster_manager LB policy: " + rawConfig));
        }
        for (String name : childPolicies.keySet()) {
            Map<String, ?> childPolicy = JsonUtil.getObject(childPolicies, name);
            if (childPolicy == null) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config for child " + name + " in cluster_manager LB policy: " + rawConfig));
            }
            List<LbConfig> childConfigCandidates = ServiceConfigUtil.unwrapLoadBalancingConfigList(JsonUtil.getListOfObjects(childPolicy, "lbPolicy"));
            if (childConfigCandidates == null || childConfigCandidates.isEmpty()) {
                return ConfigOrError.fromError(Status.INTERNAL.withDescription("No config specified for child " + name + " in cluster_manager Lb policy: " + rawConfig));
            }
            LoadBalancerRegistry registry = lbRegistry != null ? lbRegistry : LoadBalancerRegistry.getDefaultRegistry();
            ConfigOrError selectedConfig = ServiceConfigUtil.selectLbPolicyFromList(childConfigCandidates, registry);
            if (selectedConfig.getError() != null) {
                Status error = selectedConfig.getError();
                return ConfigOrError.fromError(Status.INTERNAL.withCause(error.getCause()).withDescription(error.getDescription()).augmentDescription("Failed to select config for child " + name));
            }
            parsedChildPolicies.put(name, (PolicySelection) selectedConfig.getConfig());
        }
    } catch (RuntimeException e) {
        return ConfigOrError.fromError(Status.fromThrowable(e).withDescription("Failed to parse cluster_manager LB config: " + rawConfig));
    }
    return ConfigOrError.fromConfig(new ClusterManagerConfig(parsedChildPolicies));
}
Also used : Status(io.grpc.Status) LbConfig(io.grpc.internal.ServiceConfigUtil.LbConfig) ConfigOrError(io.grpc.NameResolver.ConfigOrError) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LoadBalancerRegistry(io.grpc.LoadBalancerRegistry) LinkedHashMap(java.util.LinkedHashMap)

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