Search in sources :

Example 16 with LoadBalancerProvider

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

the class ClusterResolverLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new AssertionError(e);
        }
    });
    FakeClock fakeClock = new FakeClock();
    NameResolverRegistry nsRegistry = new NameResolverRegistry();
    NameResolver.Args args = NameResolver.Args.newBuilder().setDefaultPort(8080).setProxyDetector(GrpcUtil.NOOP_PROXY_DETECTOR).setSynchronizationContext(syncContext).setServiceConfigParser(mock(ServiceConfigParser.class)).setChannelLogger(mock(ChannelLogger.class)).build();
    when(helper.getNameResolverRegistry()).thenReturn(nsRegistry);
    when(helper.getNameResolverArgs()).thenReturn(args);
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    when(helper.getScheduledExecutorService()).thenReturn(fakeClock.getScheduledExecutorService());
    when(helper.getAuthority()).thenReturn("api.google.com");
    LoadBalancerProvider provider = new ClusterResolverLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(ClusterResolverLoadBalancer.class);
}
Also used : FakeClock(io.grpc.internal.FakeClock) LoadBalancer(io.grpc.LoadBalancer) NameResolverRegistry(io.grpc.NameResolverRegistry) Helper(io.grpc.LoadBalancer.Helper) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) ServiceConfigParser(io.grpc.NameResolver.ServiceConfigParser) NameResolver(io.grpc.NameResolver) Test(org.junit.Test)

Example 17 with LoadBalancerProvider

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

the class CdsLoadBalancerProviderTest method provided.

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

Example 18 with LoadBalancerProvider

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

the class CdsLoadBalancerProviderTest method providesLoadBalancer.

@Test
public void providesLoadBalancer() {
    Helper helper = mock(Helper.class);
    SynchronizationContext syncContext = new SynchronizationContext(new Thread.UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new AssertionError(e);
        }
    });
    when(helper.getSynchronizationContext()).thenReturn(syncContext);
    LoadBalancerProvider provider = new CdsLoadBalancerProvider();
    LoadBalancer loadBalancer = provider.newLoadBalancer(helper);
    assertThat(loadBalancer).isInstanceOf(CdsLoadBalancer2.class);
}
Also used : Helper(io.grpc.LoadBalancer.Helper) SynchronizationContext(io.grpc.SynchronizationContext) LoadBalancerProvider(io.grpc.LoadBalancerProvider) LoadBalancer(io.grpc.LoadBalancer) Test(org.junit.Test)

Example 19 with LoadBalancerProvider

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

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

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