Search in sources :

Example 1 with ClusterManagerConfig

use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig in project grpc-java by grpc.

the class ClusterManagerLoadBalancerTest method buildConfig.

private ClusterManagerConfig buildConfig(Map<String, String> childPolicies) {
    Map<String, PolicySelection> childPolicySelections = new LinkedHashMap<>();
    for (String name : childPolicies.keySet()) {
        String childPolicyName = childPolicies.get(name);
        Object childConfig = lbConfigInventory.get(name);
        PolicySelection policy = new PolicySelection(new FakeLoadBalancerProvider(childPolicyName), childConfig);
        childPolicySelections.put(name, policy);
    }
    return new ClusterManagerConfig(childPolicySelections);
}
Also used : ClusterManagerConfig(io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with ClusterManagerConfig

use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig 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 3 with ClusterManagerConfig

use of io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig in project grpc-java by grpc.

the class ClusterManagerLoadBalancer method handleResolvedAddresses.

@Override
public void handleResolvedAddresses(ResolvedAddresses resolvedAddresses) {
    logger.log(XdsLogLevel.DEBUG, "Received resolution result: {0}", resolvedAddresses);
    ClusterManagerConfig config = (ClusterManagerConfig) resolvedAddresses.getLoadBalancingPolicyConfig();
    Map<String, PolicySelection> newChildPolicies = config.childPolicies;
    logger.log(XdsLogLevel.INFO, "Received cluster_manager lb config: child names={0}", newChildPolicies.keySet());
    for (Map.Entry<String, PolicySelection> entry : newChildPolicies.entrySet()) {
        final String name = entry.getKey();
        LoadBalancerProvider childPolicyProvider = entry.getValue().getProvider();
        Object childConfig = entry.getValue().getConfig();
        if (!childLbStates.containsKey(name)) {
            childLbStates.put(name, new ChildLbState(name, childPolicyProvider));
        } else {
            childLbStates.get(name).reactivate(childPolicyProvider);
        }
        LoadBalancer childLb = childLbStates.get(name).lb;
        ResolvedAddresses childAddresses = resolvedAddresses.toBuilder().setLoadBalancingPolicyConfig(childConfig).build();
        childLb.handleResolvedAddresses(childAddresses);
    }
    for (String name : childLbStates.keySet()) {
        if (!newChildPolicies.containsKey(name)) {
            childLbStates.get(name).deactivate();
        }
    }
    // Must update channel picker before return so that new RPCs will not be routed to deleted
    // clusters and resolver can remove them in service config.
    updateOverallBalancingState();
}
Also used : ClusterManagerConfig(io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) GracefulSwitchLoadBalancer(io.grpc.util.GracefulSwitchLoadBalancer) PolicySelection(io.grpc.internal.ServiceConfigUtil.PolicySelection) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)3 ClusterManagerConfig (io.grpc.xds.ClusterManagerLoadBalancerProvider.ClusterManagerConfig)3 LoadBalancerProvider (io.grpc.LoadBalancerProvider)2 Map (java.util.Map)2 LoadBalancer (io.grpc.LoadBalancer)1 Helper (io.grpc.LoadBalancer.Helper)1 ConfigOrError (io.grpc.NameResolver.ConfigOrError)1 GracefulSwitchLoadBalancer (io.grpc.util.GracefulSwitchLoadBalancer)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.Test)1