use of com.google.api.gax.rpc.ApiException in project divolte-collector by divolte.
the class GoogleCloudPubSubFlusherTest method testMessagesAreRetriedOnRetriableFailure.
@Test
public void testMessagesAreRetriedOnRetriableFailure() throws IOException {
// Simulate a failure on the first send that indicates a retry should succeed.
final Publisher publisher = mockPublisher.orElseThrow(IllegalStateException::new);
when(publisher.publish(any(PubsubMessage.class))).thenReturn(failedFuture(new ApiException("simulated transient failure", new IOException(), GrpcStatusCode.of(Status.Code.INTERNAL), true))).thenAnswer(invocationOnMock -> completedFuture(String.valueOf(messageIdCounter++)));
// Here we send the message.
processSingleMessage();
// Now we check the invocations…
verify(publisher, times(2)).publish(any(PubsubMessage.class));
verifyNoMoreInteractions(publisher);
}
use of com.google.api.gax.rpc.ApiException in project java-docs-samples by GoogleCloudPlatform.
the class PublisherExample method main.
/**
* Publish messages to a topic.
* @param args topic name, number of messages
*/
public static void main(String... args) throws Exception {
// topic id, eg. "my-topic"
String topicId = args[0];
int messageCount = Integer.parseInt(args[1]);
ProjectTopicName topicName = ProjectTopicName.of(PROJECT_ID, topicId);
Publisher publisher = null;
try {
// Create a publisher instance with default settings bound to the topic
publisher = Publisher.newBuilder(topicName).build();
for (int i = 0; i < messageCount; i++) {
String message = "message-" + i;
// convert message to bytes
ByteString data = ByteString.copyFromUtf8(message);
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
// schedule a message to be published, messages are automatically batched
ApiFuture<String> future = publisher.publish(pubsubMessage);
// add an asynchronous callback to handle success / failure
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
if (throwable instanceof ApiException) {
ApiException apiException = ((ApiException) throwable);
// details on the API exception
System.out.println(apiException.getStatusCode().getCode());
System.out.println(apiException.isRetryable());
}
System.out.println("Error publishing message : " + message);
}
@Override
public void onSuccess(String messageId) {
// Once published, returns server-assigned message ids (unique within the topic)
System.out.println(messageId);
}
});
}
} finally {
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
publisher.shutdown();
}
}
}
use of com.google.api.gax.rpc.ApiException in project beam by apache.
the class RpcQosTest method attemptEnforcesActiveStateToPerformOperations_maxAttemptsExhausted.
@Test
public void attemptEnforcesActiveStateToPerformOperations_maxAttemptsExhausted() throws InterruptedException {
RpcQosOptions rpcQosOptions = options.toBuilder().withMaxAttempts(1).unsafeBuild();
RpcQos qos = new RpcQosImpl(rpcQosOptions, random, sleeper, counterFactory, distributionFactory);
RpcReadAttempt readAttempt = qos.newReadAttempt(RPC_ATTEMPT_CONTEXT);
readAttempt.recordRequestStart(monotonicClock.instant());
readAttempt.recordRequestFailed(monotonicClock.instant());
try {
readAttempt.checkCanRetry(monotonicClock.instant(), RETRYABLE_ERROR);
fail("expected error to be re-thrown due to max attempts exhaustion");
} catch (ApiException e) {
// expected
}
try {
readAttempt.recordStreamValue(monotonicClock.instant());
fail("expected IllegalStateException due to attempt being in terminal state");
} catch (IllegalStateException e) {
// expected
}
verify(sleeper, times(0)).sleep(// happens in checkCanRetry when the backoff is checked
anyLong());
verify(counterThrottlingMs, times(0)).inc(anyLong());
verify(counterRpcFailures, times(1)).inc();
verify(counterRpcSuccesses, times(0)).inc();
verify(counterRpcStreamValueReceived, times(0)).inc();
}
use of com.google.api.gax.rpc.ApiException in project beam by apache.
the class RpcQosTest method attemptsExhaustCorrectly.
@Test
public void attemptsExhaustCorrectly() throws InterruptedException {
RpcQosOptions rpcQosOptions = options.toBuilder().withMaxAttempts(3).unsafeBuild();
RpcQos qos = new RpcQosImpl(rpcQosOptions, random, sleeper, counterFactory, distributionFactory);
RpcReadAttempt readAttempt = qos.newReadAttempt(RPC_ATTEMPT_CONTEXT);
// try 1
readAttempt.recordRequestStart(monotonicClock.instant());
readAttempt.recordRequestFailed(monotonicClock.instant());
readAttempt.checkCanRetry(monotonicClock.instant(), RETRYABLE_ERROR);
// try 2
readAttempt.recordRequestStart(monotonicClock.instant());
readAttempt.recordRequestFailed(monotonicClock.instant());
readAttempt.checkCanRetry(monotonicClock.instant(), RETRYABLE_ERROR);
// try 3
readAttempt.recordRequestStart(monotonicClock.instant());
readAttempt.recordRequestFailed(monotonicClock.instant());
try {
readAttempt.checkCanRetry(monotonicClock.instant(), RETRYABLE_ERROR);
fail("expected retry to be exhausted after third attempt");
} catch (ApiException e) {
assertSame(e, RETRYABLE_ERROR);
}
verify(counterThrottlingMs, times(0)).inc(anyLong());
verify(counterRpcFailures, times(3)).inc();
verify(counterRpcSuccesses, times(0)).inc();
verify(counterRpcStreamValueReceived, times(0)).inc();
}
use of com.google.api.gax.rpc.ApiException in project beam by apache.
the class RpcQosTest method attemptThrowsOnNonRetryableError.
@Test
public void attemptThrowsOnNonRetryableError() throws InterruptedException {
RpcQosOptions rpcQosOptions = options.toBuilder().withMaxAttempts(3).unsafeBuild();
RpcQos qos = new RpcQosImpl(rpcQosOptions, random, sleeper, counterFactory, distributionFactory);
RpcReadAttempt readAttempt = qos.newReadAttempt(RPC_ATTEMPT_CONTEXT);
readAttempt.recordRequestStart(monotonicClock.instant());
// try 1
readAttempt.recordRequestFailed(monotonicClock.instant());
try {
readAttempt.checkCanRetry(monotonicClock.instant(), NON_RETRYABLE_ERROR);
fail("expected non-retryable error to throw error on first occurrence");
} catch (ApiException e) {
assertSame(e, NON_RETRYABLE_ERROR);
}
verify(counterThrottlingMs, times(0)).inc(anyLong());
verify(counterRpcFailures, times(1)).inc();
verify(counterRpcSuccesses, times(0)).inc();
verify(counterRpcStreamValueReceived, times(0)).inc();
}
Aggregations