use of com.google.api.gax.rpc.ApiCallContext in project java-pubsublite by googleapis.
the class SubscriberSettings method getPartitionSubscriberFactory.
PartitionSubscriberFactory getPartitionSubscriberFactory() {
SubscriberServiceClient client = newSubscriberServiceClient();
CursorServiceClient cursorClient = newCursorServiceClient();
return new PartitionSubscriberFactory() {
@Override
public Subscriber newSubscriber(Partition partition) {
SubscriberBuilder.Builder wireSubscriberBuilder = SubscriberBuilder.newBuilder().setPartition(partition).setSubscriptionPath(subscriptionPath()).setStreamFactory(responseStream -> {
ApiCallContext context = getCallContext(PubsubContext.of(framework()), RoutingMetadata.of(subscriptionPath(), partition));
return client.subscribeCallable().splitCall(responseStream, context);
}).setInitialLocation(SeekRequest.newBuilder().setNamedTarget(NamedTarget.COMMITTED_CURSOR).build());
Committer wireCommitter = CommitterSettings.newBuilder().setSubscriptionPath(subscriptionPath()).setPartition(partition).setStreamFactory(responseStream -> cursorClient.streamingCommitCursorCallable().splitCall(responseStream)).build().instantiate();
return new SinglePartitionSubscriber(receiver(), MessageTransforms.addIdCpsSubscribeTransformer(partition, transformer().orElse(MessageTransforms.toCpsSubscribeTransformer())), new AckSetTrackerImpl(wireCommitter), nackHandler().orElse(new NackHandler() {
}), (messageConsumer, resetHandler) -> wireSubscriberBuilder.setMessageConsumer(messageConsumer).setResetHandler(resetHandler).build(), perPartitionFlowControlSettings());
}
@Override
public void close() {
try (SubscriberServiceClient c1 = client;
CursorServiceClient c2 = cursorClient) {
}
}
};
}
use of com.google.api.gax.rpc.ApiCallContext in project java-spanner by googleapis.
the class GapicSpannerRpc method newCallContext.
@VisibleForTesting
<ReqT, RespT> GrpcCallContext newCallContext(@Nullable Map<Option, ?> options, String resource, ReqT request, MethodDescriptor<ReqT, RespT> method) {
GrpcCallContext context = GrpcCallContext.createDefault();
if (options != null) {
context = context.withChannelAffinity(Option.CHANNEL_HINT.getLong(options).intValue());
}
context = context.withExtraHeaders(metadataProvider.newExtraHeaders(resource, projectName));
if (callCredentialsProvider != null) {
CallCredentials callCredentials = callCredentialsProvider.getCallCredentials();
if (callCredentials != null) {
context = context.withCallOptions(context.getCallOptions().withCallCredentials(callCredentials));
}
}
context = context.withStreamWaitTimeout(waitTimeout).withStreamIdleTimeout(idleTimeout);
CallContextConfigurator configurator = SpannerOptions.CALL_CONTEXT_CONFIGURATOR_KEY.get();
ApiCallContext apiCallContextFromContext = null;
if (configurator != null) {
apiCallContextFromContext = configurator.configure(context, request, method);
}
return (GrpcCallContext) context.merge(apiCallContextFromContext);
}
use of com.google.api.gax.rpc.ApiCallContext in project spring-cloud-gcp by GoogleCloudPlatform.
the class DefaultPublisherFactoryTests method setUp.
@BeforeEach
public void setUp() throws IOException {
factory = new DefaultPublisherFactory(() -> "projectId");
factory.setCredentialsProvider(NoCredentialsProvider.create());
TransportChannelProvider mockChannelProvider = mock(TransportChannelProvider.class);
TransportChannel mockTransportChannel = mock(TransportChannel.class);
when(mockChannelProvider.getTransportChannel()).thenReturn(mockTransportChannel);
ApiCallContext mockContext = mock(ApiCallContext.class);
when(mockTransportChannel.getEmptyCallContext()).thenReturn(mockContext);
when(mockContext.withTransportChannel(any())).thenReturn(mockContext);
factory.setChannelProvider(mockChannelProvider);
}
use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.
the class MetricsTracerTest method testBatchMutateRowsThrottledTime.
@Test
public void testBatchMutateRowsThrottledTime() throws Exception {
FlowController flowController = Mockito.mock(FlowController.class);
BatchingDescriptor batchingDescriptor = Mockito.mock(MutateRowsBatchingDescriptor.class);
// Mock throttling
final long throttled = 50;
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(throttled);
return null;
}
}).when(flowController).reserve(any(Long.class), any(Long.class));
when(flowController.getMaxElementCountLimit()).thenReturn(null);
when(flowController.getMaxRequestBytesLimit()).thenReturn(null);
when(batchingDescriptor.countBytes(any())).thenReturn(1l);
when(batchingDescriptor.newRequestBuilder(any())).thenCallRealMethod();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) {
@SuppressWarnings("unchecked") StreamObserver<MutateRowsResponse> observer = (StreamObserver<MutateRowsResponse>) invocation.getArguments()[1];
observer.onNext(MutateRowsResponse.getDefaultInstance());
observer.onCompleted();
return null;
}
}).when(mockService).mutateRows(any(MutateRowsRequest.class), any());
ApiCallContext defaultContext = GrpcCallContext.createDefault();
Batcher batcher = new BatcherImpl(batchingDescriptor, stub.bulkMutateRowsCallable().withDefaultCallContext(defaultContext), BulkMutation.create(TABLE_ID), settings.getStubSettings().bulkMutateRowsSettings().getBatchingSettings(), Executors.newSingleThreadScheduledExecutor(), flowController, defaultContext);
batcher.add(RowMutationEntry.create("key"));
batcher.sendOutstanding();
Thread.sleep(100);
long throttledTimeMetric = StatsTestUtils.getAggregationValueAsLong(localStats, RpcViewConstants.BIGTABLE_BATCH_THROTTLED_TIME_VIEW, ImmutableMap.of(RpcMeasureConstants.BIGTABLE_OP, TagValue.create("Bigtable.MutateRows")), PROJECT_ID, INSTANCE_ID, APP_PROFILE_ID);
assertThat(throttledTimeMetric).isAtLeast(throttled);
}
use of com.google.api.gax.rpc.ApiCallContext in project java-bigtable by googleapis.
the class DynamicFlowControlCallableTest method testDecreasingThresholdsCantGoOverLimit.
@Test
public void testDecreasingThresholdsCantGoOverLimit() throws Exception {
// set adjusting intervals to 0 so the thresholds can keep getting updated
callableToTest = new DynamicFlowControlCallable(innerCallable, flowController, stats, TARGET_LATENCY_MS, 0);
Map<String, List<String>> extraHeaders = new HashMap<>();
extraHeaders.put(LATENCY_HEADER, Arrays.asList(String.valueOf(TARGET_LATENCY_MS * 4)));
ApiCallContext newContext = context.withExtraHeaders(extraHeaders);
List<Future> futures = new ArrayList<>();
for (int i = 0; i < 3; i++) {
ApiFuture future = callableToTest.futureCall(request, newContext);
futures.add(future);
}
for (Future f : futures) {
f.get();
}
long expectedStep = Math.round(MAX_ELEMENT * DynamicFlowControlCallable.VERY_HIGH_LATENCY_DECREASE_CONCURRENCY_RATE) * 3;
assertThat(INITIAL_ELEMENT - expectedStep).isLessThan(MIN_ELEMENT);
assertThat(flowController.getCurrentElementCountLimit()).isEqualTo(MIN_ELEMENT);
}
Aggregations