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());
}
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"));
}
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"));
}
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);
}
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();
}
Aggregations