Java Code Examples for io.grpc.LoadBalancer.Subchannel#shutdown()
The following examples show how to use
io.grpc.LoadBalancer.Subchannel#shutdown() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CachedSubchannelPool.java From grpc-nebula-java with Apache License 2.0 | 6 votes |
@Override public void returnSubchannel(Subchannel subchannel) { CacheEntry prev = cache.get(subchannel.getAddresses()); if (prev != null) { // Returning the same Subchannel twice has no effect. // Returning a different Subchannel for an already cached EAG will cause the // latter Subchannel to be shutdown immediately. if (prev.subchannel != subchannel) { subchannel.shutdown(); } return; } final ShutdownSubchannelTask shutdownTask = new ShutdownSubchannelTask(subchannel); ScheduledHandle shutdownTimer = helper.getSynchronizationContext().schedule( shutdownTask, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS, helper.getScheduledExecutorService()); CacheEntry entry = new CacheEntry(subchannel, shutdownTimer); cache.put(subchannel.getAddresses(), entry); }
Example 2
Source File: CachedSubchannelPool.java From grpc-java with Apache License 2.0 | 6 votes |
@Override public void returnSubchannel(Subchannel subchannel, ConnectivityStateInfo lastKnownState) { CacheEntry prev = cache.get(subchannel.getAddresses()); if (prev != null) { // Returning the same Subchannel twice has no effect. // Returning a different Subchannel for an already cached EAG will cause the // latter Subchannel to be shutdown immediately. if (prev.subchannel != subchannel) { subchannel.shutdown(); } return; } final ShutdownSubchannelTask shutdownTask = new ShutdownSubchannelTask(subchannel); ScheduledHandle shutdownTimer = helper.getSynchronizationContext().schedule( shutdownTask, SHUTDOWN_TIMEOUT_MS, TimeUnit.MILLISECONDS, helper.getScheduledExecutorService()); CacheEntry entry = new CacheEntry(subchannel, shutdownTimer, lastKnownState); cache.put(subchannel.getAddresses(), entry); }
Example 3
Source File: RoundRobinLoadBalancer.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
private void shutdownSubchannel(Subchannel subchannel) { subchannel.shutdown(); getSubchannelStateInfoRef(subchannel).value = ConnectivityStateInfo.forNonError(SHUTDOWN); if (stickinessState != null) { stickinessState.remove(subchannel); } }
Example 4
Source File: ManagedChannelImplTest.java From grpc-nebula-java with Apache License 2.0 | 5 votes |
@Test public void subchannelsNoConnectionShutdown() { createChannel(); Subchannel sub1 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY); Subchannel sub2 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY); channel.shutdown(); verify(mockLoadBalancer).shutdown(); sub1.shutdown(); assertFalse(channel.isTerminated()); sub2.shutdown(); assertTrue(channel.isTerminated()); verify(mockTransportFactory, never()) .newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class)); }
Example 5
Source File: ManagedChannelImplTest.java From grpc-java with Apache License 2.0 | 5 votes |
@Deprecated @Test @SuppressWarnings("deprecation") public void createSubchannel_old_propagateSubchannelStatesToOldApi() { createChannel(); Subchannel subchannel = helper.createSubchannel(addressGroup, Attributes.EMPTY); subchannel.requestConnection(); verify(mockTransportFactory) .newClientTransport( any(SocketAddress.class), any(ClientTransportOptions.class), any(ChannelLogger.class)); verify(mockLoadBalancer).handleSubchannelState( same(subchannel), eq(ConnectivityStateInfo.forNonError(CONNECTING))); MockClientTransportInfo transportInfo = transports.poll(); transportInfo.listener.transportReady(); verify(mockLoadBalancer).handleSubchannelState( same(subchannel), eq(ConnectivityStateInfo.forNonError(READY))); channel.shutdown(); verify(mockLoadBalancer).shutdown(); subchannel.shutdown(); verify(mockLoadBalancer, atLeast(0)).canHandleEmptyAddressListFromNameResolution(); verify(mockLoadBalancer, atLeast(0)).handleNameResolutionError(any(Status.class)); // handleSubchannelState() should not be called after shutdown() verifyNoMoreInteractions(mockLoadBalancer); }
Example 6
Source File: ManagedChannelImplTest.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Test public void callOptionsExecutor() { Metadata headers = new Metadata(); ClientStream mockStream = mock(ClientStream.class); FakeClock callExecutor = new FakeClock(); createChannel(); // Start a call with a call executor CallOptions options = CallOptions.DEFAULT.withExecutor(callExecutor.getScheduledExecutorService()); ClientCall<String, Integer> call = channel.newCall(method, options); call.start(mockCallListener, headers); // Make the transport available Subchannel subchannel = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY); verify(mockTransportFactory, never()) .newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class)); subchannel.requestConnection(); verify(mockTransportFactory) .newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class)); MockClientTransportInfo transportInfo = transports.poll(); ConnectionClientTransport mockTransport = transportInfo.transport; ManagedClientTransport.Listener transportListener = transportInfo.listener; when(mockTransport.newStream(same(method), same(headers), any(CallOptions.class))) .thenReturn(mockStream); transportListener.transportReady(); when(mockPicker.pickSubchannel(any(PickSubchannelArgs.class))) .thenReturn(PickResult.withSubchannel(subchannel)); assertEquals(0, callExecutor.numPendingTasks()); helper.updateBalancingState(READY, mockPicker); // Real streams are started in the call executor if they were previously buffered. assertEquals(1, callExecutor.runDueTasks()); verify(mockTransport).newStream(same(method), same(headers), same(options)); verify(mockStream).start(streamListenerCaptor.capture()); // Call listener callbacks are also run in the call executor ClientStreamListener streamListener = streamListenerCaptor.getValue(); Metadata trailers = new Metadata(); assertEquals(0, callExecutor.numPendingTasks()); streamListener.closed(Status.CANCELLED, trailers); verify(mockCallListener, never()).onClose(same(Status.CANCELLED), same(trailers)); assertEquals(1, callExecutor.runDueTasks()); verify(mockCallListener).onClose(same(Status.CANCELLED), same(trailers)); transportListener.transportShutdown(Status.UNAVAILABLE); transportListener.transportTerminated(); // Clean up as much as possible to allow the channel to terminate. subchannel.shutdown(); timer.forwardNanos( TimeUnit.SECONDS.toNanos(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS)); }
Example 7
Source File: ManagedChannelImplTest.java From grpc-nebula-java with Apache License 2.0 | 4 votes |
@Test public void subchannels() { createChannel(); // createSubchannel() always return a new Subchannel Attributes attrs1 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr1").build(); Attributes attrs2 = Attributes.newBuilder().set(SUBCHANNEL_ATTR_KEY, "attr2").build(); Subchannel sub1 = createSubchannelSafely(helper, addressGroup, attrs1); Subchannel sub2 = createSubchannelSafely(helper, addressGroup, attrs2); assertNotSame(sub1, sub2); assertNotSame(attrs1, attrs2); assertSame(attrs1, sub1.getAttributes()); assertSame(attrs2, sub2.getAttributes()); assertSame(addressGroup, sub1.getAddresses()); assertSame(addressGroup, sub2.getAddresses()); // requestConnection() verify(mockTransportFactory, never()) .newClientTransport(any(SocketAddress.class), any(ClientTransportOptions.class)); sub1.requestConnection(); verify(mockTransportFactory).newClientTransport(socketAddress, clientTransportOptions); MockClientTransportInfo transportInfo1 = transports.poll(); assertNotNull(transportInfo1); sub2.requestConnection(); verify(mockTransportFactory, times(2)) .newClientTransport(socketAddress, clientTransportOptions); MockClientTransportInfo transportInfo2 = transports.poll(); assertNotNull(transportInfo2); sub1.requestConnection(); sub2.requestConnection(); verify(mockTransportFactory, times(2)) .newClientTransport(socketAddress, clientTransportOptions); // shutdown() has a delay sub1.shutdown(); timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS - 1, TimeUnit.SECONDS); sub1.shutdown(); verify(transportInfo1.transport, never()).shutdown(any(Status.class)); timer.forwardTime(1, TimeUnit.SECONDS); verify(transportInfo1.transport).shutdown(same(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_STATUS)); // ... but not after Channel is terminating verify(mockLoadBalancer, never()).shutdown(); channel.shutdown(); verify(mockLoadBalancer).shutdown(); verify(transportInfo2.transport, never()).shutdown(any(Status.class)); sub2.shutdown(); verify(transportInfo2.transport).shutdown(same(ManagedChannelImpl.SHUTDOWN_STATUS)); // Cleanup transportInfo1.listener.transportShutdown(Status.UNAVAILABLE); transportInfo1.listener.transportTerminated(); transportInfo2.listener.transportShutdown(Status.UNAVAILABLE); transportInfo2.listener.transportTerminated(); timer.forwardTime(ManagedChannelImpl.SUBCHANNEL_SHUTDOWN_DELAY_SECONDS, TimeUnit.SECONDS); }
Example 8
Source File: ManagedChannelImplTest.java From grpc-java with Apache License 2.0 | 4 votes |
@Test public void noMoreCallbackAfterLoadBalancerShutdown() { FakeNameResolverFactory nameResolverFactory = new FakeNameResolverFactory.Builder(expectedUri) .setServers(Collections.singletonList(new EquivalentAddressGroup(socketAddress))) .build(); channelBuilder.nameResolverFactory(nameResolverFactory); Status resolutionError = Status.UNAVAILABLE.withDescription("Resolution failed"); createChannel(); FakeNameResolverFactory.FakeNameResolver resolver = nameResolverFactory.resolvers.get(0); verify(mockLoadBalancerProvider).newLoadBalancer(any(Helper.class)); verify(mockLoadBalancer).handleResolvedAddresses(resolvedAddressCaptor.capture()); assertThat(resolvedAddressCaptor.getValue().getAddresses()).containsExactly(addressGroup); SubchannelStateListener stateListener1 = mock(SubchannelStateListener.class); SubchannelStateListener stateListener2 = mock(SubchannelStateListener.class); Subchannel subchannel1 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, stateListener1); Subchannel subchannel2 = createSubchannelSafely(helper, addressGroup, Attributes.EMPTY, stateListener2); requestConnectionSafely(helper, subchannel1); requestConnectionSafely(helper, subchannel2); verify(mockTransportFactory, times(2)) .newClientTransport( any(SocketAddress.class), any(ClientTransportOptions.class), any(ChannelLogger.class)); MockClientTransportInfo transportInfo1 = transports.poll(); MockClientTransportInfo transportInfo2 = transports.poll(); // LoadBalancer receives all sorts of callbacks transportInfo1.listener.transportReady(); verify(stateListener1, times(2)).onSubchannelState(stateInfoCaptor.capture()); assertSame(CONNECTING, stateInfoCaptor.getAllValues().get(0).getState()); assertSame(READY, stateInfoCaptor.getAllValues().get(1).getState()); verify(stateListener2).onSubchannelState(stateInfoCaptor.capture()); assertSame(CONNECTING, stateInfoCaptor.getValue().getState()); resolver.listener.onError(resolutionError); verify(mockLoadBalancer).handleNameResolutionError(resolutionError); verifyNoMoreInteractions(mockLoadBalancer); channel.shutdown(); verify(mockLoadBalancer).shutdown(); verifyNoMoreInteractions(stateListener1, stateListener2); // LoadBalancer will normally shutdown all subchannels subchannel1.shutdown(); subchannel2.shutdown(); // Since subchannels are shutdown, SubchannelStateListeners will only get SHUTDOWN regardless of // the transport states. transportInfo1.listener.transportShutdown(Status.UNAVAILABLE); transportInfo2.listener.transportReady(); verify(stateListener1).onSubchannelState(ConnectivityStateInfo.forNonError(SHUTDOWN)); verify(stateListener2).onSubchannelState(ConnectivityStateInfo.forNonError(SHUTDOWN)); verifyNoMoreInteractions(stateListener1, stateListener2); // No more callback should be delivered to LoadBalancer after it's shut down resolver.listener.onError(resolutionError); resolver.resolved(); verifyNoMoreInteractions(mockLoadBalancer); }