Search in sources :

Example 11 with ApiException

use of com.google.api.gax.rpc.ApiException in project beam by apache.

the class SubscriptionPartitionProcessorImplTest method subscriberFailureFails.

@Test
public void subscriberFailureFails() throws Exception {
    when(tracker.currentRestriction()).thenReturn(initialRange());
    doAnswer((Answer<Void>) args -> {
        subscriber.fail(new CheckedApiException(Code.OUT_OF_RANGE));
        return null;
    }).when(subscriber).awaitRunning();
    ApiException e = assertThrows(// Longer wait is needed due to listener asynchrony, but should never wait this long.
    ApiException.class, () -> processor.runFor(Duration.standardMinutes(2)));
    assertEquals(Code.OUT_OF_RANGE, e.getStatusCode().getCode());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) FlowControlRequest(com.google.cloud.pubsublite.proto.FlowControlRequest) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) ProcessContinuation(org.apache.beam.sdk.transforms.DoFn.ProcessContinuation) Function(java.util.function.Function) Mockito.doThrow(org.mockito.Mockito.doThrow) Answer(org.mockito.stubbing.Answer) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Spy(org.mockito.Spy) Timeout(org.junit.rules.Timeout) OutputReceiver(org.apache.beam.sdk.transforms.DoFn.OutputReceiver) RestrictionTracker(org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker) Before(org.junit.Before) InOrder(org.mockito.InOrder) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) UnitTestExamples.example(com.google.cloud.pubsublite.internal.testing.UnitTestExamples.example) Assert.assertNotNull(org.junit.Assert.assertNotNull) Offset(com.google.cloud.pubsublite.Offset) DEFAULT_FLOW_CONTROL(org.apache.beam.sdk.io.gcp.pubsublite.SubscriberOptions.DEFAULT_FLOW_CONTROL) Mockito.times(org.mockito.Mockito.times) Timestamps(com.google.protobuf.util.Timestamps) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) JUnit4(org.junit.runners.JUnit4) Subscriber(com.google.cloud.pubsublite.internal.wire.Subscriber) ApiException(com.google.api.gax.rpc.ApiException) Mockito.verify(org.mockito.Mockito.verify) SettableApiFuture(com.google.api.core.SettableApiFuture) Consumer(java.util.function.Consumer) List(java.util.List) FakeApiService(com.google.cloud.pubsublite.internal.testing.FakeApiService) Rule(org.junit.Rule) Code(com.google.api.gax.rpc.StatusCode.Code) Assert.assertFalse(org.junit.Assert.assertFalse) Mockito.inOrder(org.mockito.Mockito.inOrder) Instant(org.joda.time.Instant) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) Cursor(com.google.cloud.pubsublite.proto.Cursor) OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) SystemExecutors(com.google.cloud.pubsublite.internal.wire.SystemExecutors) Assert.assertEquals(org.junit.Assert.assertEquals) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 12 with ApiException

use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.

the class PubSubAdminTests method testGetSubscription_notFound.

@Test
public void testGetSubscription_notFound() {
    when(this.mockSubscriptionAdminClient.getSubscription(any(ProjectSubscriptionName.class))).thenThrow(new ApiException(null, GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), false));
    assertThat(new PubSubAdmin(() -> "test-project", this.mockTopicAdminClient, this.mockSubscriptionAdminClient).getSubscription("fooSubscription")).isNull();
    verify(this.mockSubscriptionAdminClient).getSubscription(ProjectSubscriptionName.of("test-project", "fooSubscription"));
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 13 with ApiException

use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.

the class PubSubAdminTests method testGetSubscription_serviceDown.

@Test
public void testGetSubscription_serviceDown() {
    when(this.mockSubscriptionAdminClient.getSubscription(any(ProjectSubscriptionName.class))).thenThrow(new ApiException(null, GrpcStatusCode.of(io.grpc.Status.Code.UNAVAILABLE), false));
    PubSubAdmin psa = new PubSubAdmin(() -> "test-project", this.mockTopicAdminClient, this.mockSubscriptionAdminClient);
    assertThatExceptionOfType(ApiException.class).isThrownBy(() -> psa.getSubscription("fooSubscription"));
    verify(this.mockSubscriptionAdminClient).getSubscription(ProjectSubscriptionName.of("test-project", "fooSubscription"));
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 14 with ApiException

use of com.google.api.gax.rpc.ApiException in project spring-cloud-gcp by spring-cloud.

the class PubSubHealthIndicatorTests method healthUpFor403.

@Test
public void healthUpFor403() throws Exception {
    when(pubSubTemplate.pull(anyString(), anyInt(), anyBoolean())).thenThrow(new ApiException(new IllegalStateException("Illegal State"), GrpcStatusCode.of(Code.PERMISSION_DENIED), false));
    PubSubHealthIndicator healthIndicator = new PubSubHealthIndicator(pubSubTemplate);
    assertThat(healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}
Also used : PubSubHealthIndicator(org.springframework.cloud.gcp.autoconfigure.pubsub.health.PubSubHealthIndicator) ApiException(com.google.api.gax.rpc.ApiException) Test(org.junit.Test)

Example 15 with ApiException

use of com.google.api.gax.rpc.ApiException in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusher method sendBatch.

@Override
protected ImmutableList<PubsubMessage> sendBatch(final List<PubsubMessage> batch) throws InterruptedException {
    // For Pub/Sub we assume the following:
    // - Batching behaviour is set to flush everything ASAP.
    // - Retry behaviour will retry indefinitely, so long as it seems likely to succeed.
    // First start sending the messages.
    // (This will serialize them, determine the partition and then assign them to a per-partition buffer.)
    final int batchSize = batch.size();
    final List<ApiFuture<String>> sendResults = batch.stream().map(publisher::publish).collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
    // At this point the messages are in flight, and we assume being flushed.
    // When they eventually complete, each message can be in one of several states:
    // - Completed.
    // - An error occurred, but a retry may succeed.
    // - A fatal error occurred.
    final ImmutableList.Builder<PubsubMessage> remaining = ImmutableList.builder();
    for (int i = 0; i < batchSize; ++i) {
        final ApiFuture<String> pendingResult = sendResults.get(i);
        try {
            final String messageId = pendingResult.get();
            if (logger.isDebugEnabled()) {
                final PubsubMessage message = batch.get(i);
                logger.debug("Finished sending event (partyId={}) to Pub/Sub: messageId = {}", message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID), messageId);
            }
        } catch (final ExecutionException e) {
            final PubsubMessage message = batch.get(i);
            // The Pub/Sub publisher internally has a retry policy, but outside that we also
            // retry indefinitely unless it's a cause that we don't understand.
            final Throwable cause = e.getCause();
            if (cause instanceof ApiException) {
                final ApiException apiException = (ApiException) cause;
                if (apiException.isRetryable()) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Transient error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; retrying.", cause);
                    }
                    remaining.add(message);
                } else {
                    logger.warn("Permanent error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
                }
            } else {
                logger.error("Unknown error sending event (partyId=" + message.getAttributesOrThrow(MESSAGE_ATTRIBUTE_PARTYID) + ") to Pub/Sub; abandoning.", cause);
            }
        }
    }
    return remaining.build();
}
Also used : ApiFuture(com.google.api.core.ApiFuture) ImmutableList(com.google.common.collect.ImmutableList) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) PubsubMessage(com.google.pubsub.v1.PubsubMessage) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

ApiException (com.google.api.gax.rpc.ApiException)30 Test (org.junit.Test)21 IOException (java.io.IOException)5 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 StatusCode (com.google.api.gax.rpc.StatusCode)3 Publisher (com.google.cloud.pubsub.v1.Publisher)3 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)3 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)3 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)3 RpcReadAttempt (org.apache.beam.sdk.io.gcp.firestore.RpcQos.RpcReadAttempt)3 Code (com.google.api.gax.rpc.StatusCode.Code)2 Offset (com.google.cloud.pubsublite.Offset)2 Write (com.google.firestore.v1.Write)2 ByteString (com.google.protobuf.ByteString)2 TopicName (com.google.pubsub.v1.TopicName)2 Span (io.opencensus.trace.Span)2 FirestoreProtoHelpers.newWrite (org.apache.beam.sdk.io.gcp.firestore.FirestoreProtoHelpers.newWrite)2 WriteElement (org.apache.beam.sdk.io.gcp.firestore.FirestoreV1WriteFn.WriteElement)2 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)2 Instant (org.joda.time.Instant)2