Search in sources :

Example 6 with ApiCallContext

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) {
            }
        }
    };
}
Also used : SubscriberServiceClient(com.google.cloud.pubsublite.v1.SubscriberServiceClient) RoutingMetadata(com.google.cloud.pubsublite.internal.wire.RoutingMetadata) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Committer(com.google.cloud.pubsublite.internal.wire.Committer) Framework(com.google.cloud.pubsublite.internal.wire.PubsubContext.Framework) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) SubscriptionPath(com.google.cloud.pubsublite.SubscriptionPath) AckSetTrackerImpl(com.google.cloud.pubsublite.cloudpubsub.internal.AckSetTrackerImpl) MessageTransformer(com.google.cloud.pubsublite.MessageTransformer) CommitterSettings(com.google.cloud.pubsublite.internal.wire.CommitterSettings) ArrayList(java.util.ArrayList) PubsubMessage(com.google.pubsub.v1.PubsubMessage) PartitionSubscriberFactory(com.google.cloud.pubsublite.cloudpubsub.internal.PartitionSubscriberFactory) PartitionAssignmentServiceSettings(com.google.cloud.pubsublite.v1.PartitionAssignmentServiceSettings) ImmutableList(com.google.common.collect.ImmutableList) CredentialsProvider(com.google.api.gax.core.CredentialsProvider) AssigningSubscriber(com.google.cloud.pubsublite.cloudpubsub.internal.AssigningSubscriber) ApiService(com.google.api.core.ApiService) PubsubContext(com.google.cloud.pubsublite.internal.wire.PubsubContext) NamedTarget(com.google.cloud.pubsublite.proto.SeekRequest.NamedTarget) CursorServiceSettings(com.google.cloud.pubsublite.v1.CursorServiceSettings) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) SinglePartitionSubscriber(com.google.cloud.pubsublite.cloudpubsub.internal.SinglePartitionSubscriber) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) Partition(com.google.cloud.pubsublite.Partition) MultiPartitionSubscriber(com.google.cloud.pubsublite.cloudpubsub.internal.MultiPartitionSubscriber) SeekRequest(com.google.cloud.pubsublite.proto.SeekRequest) SubscriberServiceSettings(com.google.cloud.pubsublite.v1.SubscriberServiceSettings) PartitionAssignmentServiceClient(com.google.cloud.pubsublite.v1.PartitionAssignmentServiceClient) CursorServiceClient(com.google.cloud.pubsublite.v1.CursorServiceClient) AssignerSettings(com.google.cloud.pubsublite.internal.wire.AssignerSettings) ApiException(com.google.api.gax.rpc.ApiException) List(java.util.List) ServiceClients.addDefaultSettings(com.google.cloud.pubsublite.internal.wire.ServiceClients.addDefaultSettings) SubscriberBuilder(com.google.cloud.pubsublite.internal.wire.SubscriberBuilder) AssignerFactory(com.google.cloud.pubsublite.internal.wire.AssignerFactory) AutoValue(com.google.auto.value.AutoValue) ExtractStatus.toCanonical(com.google.cloud.pubsublite.internal.ExtractStatus.toCanonical) Optional(java.util.Optional) ApiServiceUtils.autoCloseableAsApiService(com.google.cloud.pubsublite.internal.wire.ApiServiceUtils.autoCloseableAsApiService) ServiceClients.getCallContext(com.google.cloud.pubsublite.internal.wire.ServiceClients.getCallContext) Partition(com.google.cloud.pubsublite.Partition) SubscriberBuilder(com.google.cloud.pubsublite.internal.wire.SubscriberBuilder) SubscriberServiceClient(com.google.cloud.pubsublite.v1.SubscriberServiceClient) CursorServiceClient(com.google.cloud.pubsublite.v1.CursorServiceClient) AckSetTrackerImpl(com.google.cloud.pubsublite.cloudpubsub.internal.AckSetTrackerImpl) SinglePartitionSubscriber(com.google.cloud.pubsublite.cloudpubsub.internal.SinglePartitionSubscriber) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Committer(com.google.cloud.pubsublite.internal.wire.Committer) PartitionSubscriberFactory(com.google.cloud.pubsublite.cloudpubsub.internal.PartitionSubscriberFactory)

Example 7 with ApiCallContext

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);
}
Also used : GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext) CallContextConfigurator(com.google.cloud.spanner.SpannerOptions.CallContextConfigurator) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) CallCredentials(io.grpc.CallCredentials) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with ApiCallContext

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);
}
Also used : TransportChannel(com.google.api.gax.rpc.TransportChannel) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 9 with ApiCallContext

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);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) MutateRowsBatchingDescriptor(com.google.cloud.bigtable.data.v2.stub.mutaterows.MutateRowsBatchingDescriptor) BatchingDescriptor(com.google.api.gax.batching.BatchingDescriptor) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) BatcherImpl(com.google.api.gax.batching.BatcherImpl) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(org.mockito.stubbing.Answer) MutateRowsResponse(com.google.bigtable.v2.MutateRowsResponse) MutateRowsRequest(com.google.bigtable.v2.MutateRowsRequest) Batcher(com.google.api.gax.batching.Batcher) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FlowController(com.google.api.gax.batching.FlowController) Test(org.junit.Test)

Example 10 with ApiCallContext

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);
}
Also used : ApiFuture(com.google.api.core.ApiFuture) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ApiFuture(com.google.api.core.ApiFuture) ArrayList(java.util.ArrayList) List(java.util.List) ApiCallContext(com.google.api.gax.rpc.ApiCallContext) Test(org.junit.Test)

Aggregations

ApiCallContext (com.google.api.gax.rpc.ApiCallContext)45 Test (org.junit.Test)31 List (java.util.List)16 ArrayList (java.util.ArrayList)11 ApiFuture (com.google.api.core.ApiFuture)10 ImmutableList (com.google.common.collect.ImmutableList)8 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 LabeledIntList (com.google.api.gax.rpc.testing.FakeBatchableApi.LabeledIntList)5 HashMap (java.util.HashMap)5 HttpHeaders (com.google.api.client.http.HttpHeaders)3 HttpResponseException (com.google.api.client.http.HttpResponseException)3 GrpcCallContext (com.google.api.gax.grpc.GrpcCallContext)3 ApiException (com.google.api.gax.rpc.ApiException)3 LongRunningClient (com.google.api.gax.rpc.LongRunningClient)3 StatusCode (com.google.api.gax.rpc.StatusCode)3 Code (com.google.api.gax.rpc.StatusCode.Code)3 StreamController (com.google.api.gax.rpc.StreamController)3 MutateRowsRequest (com.google.bigtable.v2.MutateRowsRequest)3 MutateRowsResponse (com.google.bigtable.v2.MutateRowsResponse)3