Search in sources :

Example 16 with LoadBalancer

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

the class WeightedTargetLoadBalancer method shutdown.

@Override
public void shutdown() {
    logger.log(XdsLogLevel.INFO, "Shutdown");
    for (LoadBalancer childBalancer : childBalancers.values()) {
        childBalancer.shutdown();
    }
    childBalancers.clear();
}
Also used : LoadBalancer(io.grpc.LoadBalancer) GracefulSwitchLoadBalancer(io.grpc.util.GracefulSwitchLoadBalancer)

Example 17 with LoadBalancer

use of io.grpc.LoadBalancer 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 18 with LoadBalancer

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

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

the class AutoConfiguredLoadBalancerFactoryTest method handleResolvedAddressGroups_keepOldBalancer.

@Test
public void handleResolvedAddressGroups_keepOldBalancer() {
    final List<EquivalentAddressGroup> servers = Collections.singletonList(new EquivalentAddressGroup(new SocketAddress() {
    }));
    Helper helper = new TestHelper() {

        @Override
        public Subchannel createSubchannel(CreateSubchannelArgs args) {
            assertThat(args.getAddresses()).isEqualTo(servers);
            return new TestSubchannel(args);
        }
    };
    AutoConfiguredLoadBalancer lb = lbf.newLoadBalancer(helper);
    LoadBalancer oldDelegate = lb.getDelegate();
    Status handleResult = lb.tryHandleResolvedAddresses(ResolvedAddresses.newBuilder().setAddresses(servers).setAttributes(Attributes.EMPTY).setLoadBalancingPolicyConfig(null).build());
    assertThat(handleResult.getCode()).isEqualTo(Status.Code.OK);
    assertThat(lb.getDelegate()).isSameInstanceAs(oldDelegate);
}
Also used : ForwardingLoadBalancerHelper(io.grpc.util.ForwardingLoadBalancerHelper) Helper(io.grpc.LoadBalancer.Helper) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) Status(io.grpc.Status) CreateSubchannelArgs(io.grpc.LoadBalancer.CreateSubchannelArgs) EquivalentAddressGroup(io.grpc.EquivalentAddressGroup) AutoConfiguredLoadBalancer(io.grpc.internal.AutoConfiguredLoadBalancerFactory.AutoConfiguredLoadBalancer) LoadBalancer(io.grpc.LoadBalancer) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.Test)

Example 20 with LoadBalancer

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

LoadBalancer (io.grpc.LoadBalancer)27 Test (org.junit.Test)22 Helper (io.grpc.LoadBalancer.Helper)20 SubchannelPicker (io.grpc.LoadBalancer.SubchannelPicker)13 EquivalentAddressGroup (io.grpc.EquivalentAddressGroup)7 PolicySelection (io.grpc.internal.ServiceConfigUtil.PolicySelection)5 ResolvedAddresses (io.grpc.LoadBalancer.ResolvedAddresses)4 LoadBalancerProvider (io.grpc.LoadBalancerProvider)4 Status (io.grpc.Status)4 PriorityLbConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig)4 PriorityChildConfig (io.grpc.xds.PriorityLoadBalancerProvider.PriorityLbConfig.PriorityChildConfig)4 Attributes (io.grpc.Attributes)2 CreateSubchannelArgs (io.grpc.LoadBalancer.CreateSubchannelArgs)2 PickResult (io.grpc.LoadBalancer.PickResult)2 PickSubchannelArgs (io.grpc.LoadBalancer.PickSubchannelArgs)2 SynchronizationContext (io.grpc.SynchronizationContext)2 GracefulSwitchLoadBalancer (io.grpc.util.GracefulSwitchLoadBalancer)2 ErrorPicker (io.grpc.xds.XdsSubchannelPickers.ErrorPicker)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2