/** * Performs a simple round robin on the list of {@link ManagedChannel}s in the {@code channels} * list. * * @return A {@link ManagedChannel} that can be used for a single RPC call. */ private ManagedChannel getNextChannel() { return getChannel(indexTicker.getAndIncrement()); }
/** * Create a {@link ClientCall} on a Channel from the pool chosen in a round-robin fashion to the * remote operation specified by the given {@link MethodDescriptor}. The returned {@link * ClientCall} does not trigger any remote behavior until {@link * ClientCall#start(ClientCall.Listener, io.grpc.Metadata)} is invoked. */ @Override public <ReqT, RespT> ClientCall<ReqT, RespT> newCall( MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions) { return getNextChannel().newCall(methodDescriptor, callOptions); }
/** {@inheritDoc} */ @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long endTimeNanos = System.nanoTime() + unit.toNanos(timeout); for (ManagedChannel channel : channels) { long awaitTimeNanos = endTimeNanos - System.nanoTime(); if (awaitTimeNanos <= 0) { break; } channel.awaitTermination(awaitTimeNanos, TimeUnit.NANOSECONDS); } return isTerminated(); }
private TransportChannel createChannel() throws IOException { ManagedChannel outerChannel; if (poolSize == null || poolSize == 1) { outerChannel = createSingleChannel(); } else { ImmutableList.Builder<ManagedChannel> channels = ImmutableList.builder(); for (int i = 0; i < poolSize; i++) { channels.add(createSingleChannel()); } outerChannel = new ChannelPool(channels.build()); } return GrpcTransportChannel.create(outerChannel); }
@Test public void testAuthority() { ManagedChannel sub1 = Mockito.mock(ManagedChannel.class); ManagedChannel sub2 = Mockito.mock(ManagedChannel.class); Mockito.when(sub1.authority()).thenReturn("myAuth"); ChannelPool pool = new ChannelPool(Lists.newArrayList(sub1, sub2)); Truth.assertThat(pool.authority()).isEqualTo("myAuth"); }
@Override public void run() { for (int j = 0; j < numPerThread; j++) { pool.newCall(methodDescriptor, callOptions); } } });
private TransportChannel createChannel() throws IOException { ManagedChannel outerChannel; if (poolSize == null || poolSize == 1) { outerChannel = createSingleChannel(); } else { ImmutableList.Builder<ManagedChannel> channels = ImmutableList.builder(); for (int i = 0; i < poolSize; i++) { channels.add(createSingleChannel()); } outerChannel = new ChannelPool(channels.build()); } return GrpcTransportChannel.create(outerChannel); }
private void verifyTargetChannel( ChannelPool pool, List<ManagedChannel> channels, ManagedChannel targetChannel) { MethodDescriptor<Color, Money> methodDescriptor = FakeServiceGrpc.METHOD_RECOGNIZE; CallOptions callOptions = CallOptions.DEFAULT; @SuppressWarnings("unchecked") ClientCall<Color, Money> expectedClientCall = Mockito.mock(ClientCall.class); for (ManagedChannel channel : channels) { Mockito.reset(channel); } Mockito.doReturn(expectedClientCall).when(targetChannel).newCall(methodDescriptor, callOptions); ClientCall<Color, Money> actualCall = pool.newCall(methodDescriptor, callOptions); Truth.assertThat(actualCall).isSameAs(expectedClientCall); Mockito.verify(targetChannel, Mockito.times(1)).newCall(methodDescriptor, callOptions); for (ManagedChannel otherChannel : channels) { if (otherChannel != targetChannel) { Mockito.verify(otherChannel, Mockito.never()).newCall(methodDescriptor, callOptions); } } }
@Test public void testRoundRobin() { ManagedChannel sub1 = Mockito.mock(ManagedChannel.class); ManagedChannel sub2 = Mockito.mock(ManagedChannel.class); Mockito.when(sub1.authority()).thenReturn("myAuth"); ArrayList<ManagedChannel> channels = Lists.newArrayList(sub1, sub2); ChannelPool pool = new ChannelPool(channels); verifyTargetChannel(pool, channels, sub1); verifyTargetChannel(pool, channels, sub2); verifyTargetChannel(pool, channels, sub1); }
/** * Performs a simple round robin on the list of {@link ManagedChannel}s in the {@code channels} * list. * * @return A {@link ManagedChannel} that can be used for a single RPC call. */ private ManagedChannel getNextChannel() { return getChannel(indexTicker.getAndIncrement()); }
/** * Create a {@link ClientCall} on a Channel from the pool chosen in a round-robin fashion to the * remote operation specified by the given {@link MethodDescriptor}. The returned {@link * ClientCall} does not trigger any remote behavior until {@link * ClientCall#start(ClientCall.Listener, io.grpc.Metadata)} is invoked. */ @Override public <ReqT, RespT> ClientCall<ReqT, RespT> newCall( MethodDescriptor<ReqT, RespT> methodDescriptor, CallOptions callOptions) { return getNextChannel().newCall(methodDescriptor, callOptions); }
/** {@inheritDoc} */ @Override public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { long endTimeNanos = System.nanoTime() + unit.toNanos(timeout); for (ManagedChannel channel : channels) { long awaitTimeNanos = endTimeNanos - System.nanoTime(); if (awaitTimeNanos <= 0) { break; } channel.awaitTermination(awaitTimeNanos, TimeUnit.NANOSECONDS); } return isTerminated(); }
final ChannelPool pool = new ChannelPool(Arrays.asList(channels));
channel = ((ChannelPool) channel).getChannel(grpcContext.getChannelAffinity());
@Test public void testAffinity() { MethodDescriptor<Color, Money> descriptor = FakeServiceGrpc.METHOD_RECOGNIZE; @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall0 = Mockito.mock(ClientCall.class); @SuppressWarnings("unchecked") ClientCall<Color, Money> clientCall1 = Mockito.mock(ClientCall.class); ManagedChannel channel0 = Mockito.mock(ManagedChannel.class); ManagedChannel channel1 = Mockito.mock(ManagedChannel.class); Mockito.when(channel0.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall0); Mockito.when(channel1.newCall(Mockito.eq(descriptor), Mockito.<CallOptions>any())) .thenReturn(clientCall1); Channel pool = new ChannelPool(Arrays.asList(channel0, channel1)); GrpcCallContext context = GrpcCallContext.createDefault().withChannel(pool); ClientCall<Color, Money> gotCallA = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallB = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(0)); ClientCall<Color, Money> gotCallC = GrpcClientCalls.newCall(descriptor, context.withChannelAffinity(1)); assertThat(gotCallA).isSameAs(gotCallB); assertThat(gotCallA).isNotSameAs(gotCallC); }
channel = ((ChannelPool) channel).getChannel(grpcContext.getChannelAffinity());