Search in sources :

Example 11 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ClusterResolverLoadBalancerProviderTest method provided.

@Test
public void provided() {
    LoadBalancerProvider provider = LoadBalancerRegistry.getDefaultRegistry().getProvider(XdsLbPolicies.CLUSTER_RESOLVER_POLICY_NAME);
    assertThat(provider).isInstanceOf(ClusterResolverLoadBalancerProvider.class);
}
Also used : LoadBalancerProvider(io.grpc.LoadBalancerProvider) Test(org.junit.Test)

Example 12 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider in project grpc-java by grpc.

the class ServiceConfigUtil method selectLbPolicyFromList.

/**
 * Parses and selects a load balancing policy from a non-empty list of raw configs. If selection
 * is successful, the returned ConfigOrError object will include a {@link
 * ServiceConfigUtil.PolicySelection} as its config value.
 */
public static ConfigOrError selectLbPolicyFromList(List<LbConfig> lbConfigs, LoadBalancerRegistry lbRegistry) {
    List<String> policiesTried = new ArrayList<>();
    for (LbConfig lbConfig : lbConfigs) {
        String policy = lbConfig.getPolicyName();
        LoadBalancerProvider provider = lbRegistry.getProvider(policy);
        if (provider == null) {
            policiesTried.add(policy);
        } else {
            if (!policiesTried.isEmpty()) {
                Logger.getLogger(ServiceConfigUtil.class.getName()).log(Level.FINEST, "{0} specified by Service Config are not available", policiesTried);
            }
            ConfigOrError parsedLbPolicyConfig = provider.parseLoadBalancingPolicyConfig(lbConfig.getRawConfigValue());
            if (parsedLbPolicyConfig.getError() != null) {
                return parsedLbPolicyConfig;
            }
            return ConfigOrError.fromConfig(new PolicySelection(provider, parsedLbPolicyConfig.getConfig()));
        }
    }
    return ConfigOrError.fromError(Status.UNKNOWN.withDescription("None of " + policiesTried + " specified by Service Config are available."));
}
Also used : LoadBalancerProvider(io.grpc.LoadBalancerProvider) ArrayList(java.util.ArrayList) ConfigOrError(io.grpc.NameResolver.ConfigOrError)

Example 13 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider 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 14 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider 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 15 with LoadBalancerProvider

use of io.grpc.LoadBalancerProvider 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)

Aggregations

LoadBalancerProvider (io.grpc.LoadBalancerProvider)25 Test (org.junit.Test)17 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)11 WeightedPolicySelection (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedPolicySelection)9 ClusterImplConfig (io.grpc.xds.ClusterImplLoadBalancerProvider.ClusterImplConfig)8 WeightedTargetConfig (io.grpc.xds.WeightedTargetLoadBalancerProvider.WeightedTargetConfig)8 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)6 Subchannel (io.grpc.LoadBalancer.Subchannel)6 Helper (io.grpc.LoadBalancer.Helper)5 LoadBalancer (io.grpc.LoadBalancer)4 PickResult (io.grpc.LoadBalancer.PickResult)4 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)4 ClusterStats (io.grpc.xds.Stats.ClusterStats)4 ClientStreamTracer (io.grpc.ClientStreamTracer)3 Metadata (io.grpc.Metadata)3 ConfigOrError (io.grpc.NameResolver.ConfigOrError)3 Map (java.util.Map)3 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 SynchronizationContext (io.grpc.SynchronizationContext)2 ClusterManagerConfig (io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig)2