static <RequestT, ResponseT> BatchingCreateResult<RequestT, ResponseT> batchingImpl( UnaryCallable<RequestT, ResponseT> innerCallable, BatchingCallSettings<RequestT, ResponseT> batchingCallSettings, ClientContext clientContext) { BatcherFactory<RequestT, ResponseT> batcherFactory = new BatcherFactory<>( batchingCallSettings.getBatchingDescriptor(), batchingCallSettings.getBatchingSettings(), clientContext.getExecutor(), batchingCallSettings.getFlowController()); UnaryCallable<RequestT, ResponseT> callable = new BatchingCallable<>( innerCallable, batchingCallSettings.getBatchingDescriptor(), batcherFactory); return new BatchingCreateResult<>(batcherFactory, callable); }
static <RequestT, ResponseT> BatchingCreateResult<RequestT, ResponseT> batchingImpl( UnaryCallable<RequestT, ResponseT> innerCallable, BatchingCallSettings<RequestT, ResponseT> batchingCallSettings, ClientContext clientContext) { BatcherFactory<RequestT, ResponseT> batcherFactory = new BatcherFactory<>( batchingCallSettings.getBatchingDescriptor(), batchingCallSettings.getBatchingSettings(), clientContext.getExecutor(), batchingCallSettings.getFlowController()); UnaryCallable<RequestT, ResponseT> callable = new BatchingCallable<>( innerCallable, batchingCallSettings.getBatchingDescriptor(), batcherFactory); return new BatchingCreateResult<>(batcherFactory, callable); }
@Test public void testEmptyBuilder() { @SuppressWarnings("unchecked") BatchingDescriptor<Integer, Integer> batchingDescriptor = Mockito.mock(BatchingDescriptor.class); BatchingCallSettings.Builder<Integer, Integer> builder = BatchingCallSettings.newBuilder(batchingDescriptor); Truth.assertThat(builder.getBatchingDescriptor()).isSameAs(batchingDescriptor); Truth.assertThat(builder.getBatchingSettings()).isNull(); Truth.assertThat(builder.getFlowController()).isNull(); Truth.assertThat(builder.getRetryableCodes().size()).isEqualTo(0); Truth.assertThat(builder.getRetrySettings()).isNotNull(); BatchingSettings batchingSettings = BatchingSettings.newBuilder().setElementCountThreshold(1L).build(); builder.setBatchingSettings(batchingSettings); BatchingCallSettings settings = builder.build(); Truth.assertThat(settings.getBatchingDescriptor()).isSameAs(batchingDescriptor); Truth.assertThat(settings.getBatchingSettings()).isSameAs(batchingSettings); Truth.assertThat(settings.getFlowController()).isNotNull(); Truth.assertThat(settings.getRetryableCodes().size()).isEqualTo(0); Truth.assertThat(settings.getRetrySettings()).isNotNull(); }
/** * Create a callable object that represents a batching API method. Designed for use by generated * code. * * @param grpcCallSettings the gRPC call settings * @param batchingCallSettings {@link BatchingCallSettings} to configure the batching related * settings with. * @param clientContext {@link ClientContext} to use to connect to the service. * @return {@link UnaryCallable} callable object. */ @BetaApi("The surface for batching is not stable yet and may change in the future.") public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable( GrpcCallSettings<RequestT, ResponseT> grpcCallSettings, BatchingCallSettings<RequestT, ResponseT> batchingCallSettings, ClientContext clientContext) { UnaryCallable<RequestT, ResponseT> baseCallable = createBaseUnaryCallable(grpcCallSettings, batchingCallSettings, clientContext); // NOTE: Since batching happens asynchronously and the outermost callable simply buffers the // request. Tracing will only start on the inner callable that accepts the batch. UnaryCallable<RequestT, ResponseT> tracedCallable = new TracedBatchingCallable<>( baseCallable, clientContext.getTracerFactory(), getSpanName(grpcCallSettings.getMethodDescriptor()), batchingCallSettings.getBatchingDescriptor()); UnaryCallable<RequestT, ResponseT> batchingCallable = Callables.batching(tracedCallable, batchingCallSettings, clientContext); return batchingCallable.withDefaultCallContext(clientContext.getDefaultCallContext()); }
@Test public void testBuilder() { @SuppressWarnings("unchecked") BatchingDescriptor<Integer, Integer> batchingDescriptor = Mockito.mock(BatchingDescriptor.class); BatchingCallSettings.Builder<Integer, Integer> builder = BatchingCallSettings.newBuilder(batchingDescriptor); BatchingSettings batchingSettings = BatchingSettings.newBuilder().setElementCountThreshold(1L).build(); FlowController flowController = Mockito.mock(FlowController.class); Set<StatusCode.Code> retryCodes = Sets.newHashSet(Code.UNAVAILABLE); RetrySettings retrySettings = RetrySettings.newBuilder().build(); builder.setBatchingSettings(batchingSettings); builder.setFlowController(flowController); builder.setRetryableCodes(retryCodes); builder.setRetrySettings(retrySettings); Truth.assertThat(builder.getBatchingDescriptor()).isSameAs(batchingDescriptor); Truth.assertThat(builder.getBatchingSettings()).isSameAs(batchingSettings); Truth.assertThat(builder.getFlowController()).isSameAs(flowController); Truth.assertThat(builder.getRetryableCodes().size()).isEqualTo(1); Truth.assertThat(builder.getRetrySettings()).isSameAs(retrySettings); BatchingCallSettings settings = builder.build(); Truth.assertThat(settings.getBatchingDescriptor()).isSameAs(batchingDescriptor); Truth.assertThat(settings.getBatchingSettings()).isSameAs(batchingSettings); Truth.assertThat(settings.getFlowController()).isSameAs(flowController); Truth.assertThat(settings.getRetryableCodes().size()).isEqualTo(1); Truth.assertThat(settings.getRetrySettings()).isSameAs(retrySettings); }