Search in sources :

Example 1 with DeleteMessageBatchResult

use of com.amazonaws.services.sqs.model.DeleteMessageBatchResult in project conductor by Netflix.

the class SQSObservableQueue method delete.

private List<String> delete(List<Message> messages) {
    if (messages == null || messages.isEmpty()) {
        return null;
    }
    DeleteMessageBatchRequest batch = new DeleteMessageBatchRequest().withQueueUrl(queueURL);
    List<DeleteMessageBatchRequestEntry> entries = batch.getEntries();
    messages.forEach(m -> entries.add(new DeleteMessageBatchRequestEntry().withId(m.getId()).withReceiptHandle(m.getReceipt())));
    DeleteMessageBatchResult result = client.deleteMessageBatch(batch);
    List<String> failures = result.getFailed().stream().map(BatchResultErrorEntry::getId).collect(Collectors.toList());
    logger.debug("Failed to delete messages from queue: {}: {}", queueName, failures);
    return failures;
}
Also used : DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) DeleteMessageBatchResult(com.amazonaws.services.sqs.model.DeleteMessageBatchResult) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)

Example 2 with DeleteMessageBatchResult

use of com.amazonaws.services.sqs.model.DeleteMessageBatchResult in project beam by apache.

the class SqsUnboundedReader method deleteBatch.

/**
 * delete the provided {@code messageIds} from SQS, blocking until all of the messages are
 * deleted.
 *
 * <p>CAUTION: May be invoked from a separate thread.
 *
 * <p>CAUTION: Retains {@code messageIds}.
 */
private void deleteBatch(List<String> messageIds) throws IOException {
    int retries = 0;
    List<String> errorMessages = new ArrayList<>();
    Map<String, String> pendingReceipts = IntStream.range(0, messageIds.size()).boxed().filter(i -> inFlight.containsKey(messageIds.get(i))).collect(toMap(Object::toString, i -> inFlight.get(messageIds.get(i)).receiptHandle));
    while (!pendingReceipts.isEmpty()) {
        if (retries >= BATCH_OPERATION_MAX_RETIRES) {
            throw new IOException("Failed to delete " + pendingReceipts.size() + " messages after " + retries + " retries: " + String.join(", ", errorMessages));
        }
        List<DeleteMessageBatchRequestEntry> entries = pendingReceipts.entrySet().stream().map(r -> new DeleteMessageBatchRequestEntry(r.getKey(), r.getValue())).collect(Collectors.toList());
        DeleteMessageBatchResult result = sqsClient.deleteMessageBatch(source.getRead().queueUrl(), entries);
        // Retry errors except invalid handles
        Set<BatchResultErrorEntry> retryErrors = result.getFailed().stream().filter(e -> !e.getCode().equals("ReceiptHandleIsInvalid")).collect(Collectors.toSet());
        pendingReceipts.keySet().retainAll(retryErrors.stream().map(BatchResultErrorEntry::getId).collect(Collectors.toSet()));
        errorMessages = retryErrors.stream().map(BatchResultErrorEntry::getMessage).collect(Collectors.toList());
        retries += 1;
    }
    deletedIds.add(messageIds);
}
Also used : AmazonSQSClientBuilder(com.amazonaws.services.sqs.AmazonSQSClientBuilder) Arrays(java.util.Arrays) MessageSystemAttributeName(com.amazonaws.services.sqs.model.MessageSystemAttributeName) UnboundedSource(org.apache.beam.sdk.io.UnboundedSource) LoggerFactory(org.slf4j.LoggerFactory) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) BatchResultErrorEntry(com.amazonaws.services.sqs.model.BatchResultErrorEntry) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) ChangeMessageVisibilityBatchResult(com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchResult) Collectors.toMap(java.util.stream.Collectors.toMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) GetQueueAttributesRequest(com.amazonaws.services.sqs.model.GetQueueAttributesRequest) Min(org.apache.beam.sdk.transforms.Min) Set(java.util.Set) Sum(org.apache.beam.sdk.transforms.Sum) Collectors(java.util.stream.Collectors) MovingFunction(org.apache.beam.sdk.util.MovingFunction) ChangeMessageVisibilityBatchRequestEntry(com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequestEntry) Objects(java.util.Objects) List(java.util.List) Max(org.apache.beam.sdk.transforms.Max) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) Queue(java.util.Queue) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) IntStream(java.util.stream.IntStream) MessageAttributeValue(com.amazonaws.services.sqs.model.MessageAttributeValue) Combine(org.apache.beam.sdk.transforms.Combine) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Duration(org.joda.time.Duration) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BucketingFunction(org.apache.beam.sdk.util.BucketingFunction) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) DeleteMessageBatchResult(com.amazonaws.services.sqs.model.DeleteMessageBatchResult) EvictingQueue(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.EvictingQueue) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) NoSuchElementException(java.util.NoSuchElementException) QueueAttributeName(com.amazonaws.services.sqs.model.QueueAttributeName) Logger(org.slf4j.Logger) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) IOException(java.io.IOException) Message(com.amazonaws.services.sqs.model.Message) CheckpointMark(org.apache.beam.sdk.io.UnboundedSource.CheckpointMark) Preconditions.checkState(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkState) BoundedWindow(org.apache.beam.sdk.transforms.windowing.BoundedWindow) Instant(org.joda.time.Instant) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) DeleteMessageBatchResult(com.amazonaws.services.sqs.model.DeleteMessageBatchResult) ArrayList(java.util.ArrayList) BatchResultErrorEntry(com.amazonaws.services.sqs.model.BatchResultErrorEntry) IOException(java.io.IOException) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)

Example 3 with DeleteMessageBatchResult

use of com.amazonaws.services.sqs.model.DeleteMessageBatchResult in project hudi by apache.

the class CloudObjectsSelector method deleteBatchOfMessages.

/**
 * Delete batch of messages from queue.
 */
protected void deleteBatchOfMessages(AmazonSQS sqs, String queueUrl, List<Message> messagesToBeDeleted) {
    DeleteMessageBatchRequest deleteBatchReq = new DeleteMessageBatchRequest().withQueueUrl(queueUrl);
    List<DeleteMessageBatchRequestEntry> deleteEntries = deleteBatchReq.getEntries();
    for (Message message : messagesToBeDeleted) {
        deleteEntries.add(new DeleteMessageBatchRequestEntry().withId(message.getMessageId()).withReceiptHandle(message.getReceiptHandle()));
    }
    DeleteMessageBatchResult deleteResult = sqs.deleteMessageBatch(deleteBatchReq);
    List<String> deleteFailures = deleteResult.getFailed().stream().map(BatchResultErrorEntry::getId).collect(Collectors.toList());
    if (!deleteFailures.isEmpty()) {
        log.warn("Failed to delete " + deleteFailures.size() + " messages out of " + deleteEntries.size() + " from queue.");
    } else {
        log.info("Successfully deleted " + deleteEntries.size() + " messages from queue.");
    }
}
Also used : DeleteMessageBatchRequest(com.amazonaws.services.sqs.model.DeleteMessageBatchRequest) Message(com.amazonaws.services.sqs.model.Message) DeleteMessageBatchResult(com.amazonaws.services.sqs.model.DeleteMessageBatchResult) DeleteMessageBatchRequestEntry(com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)

Aggregations

DeleteMessageBatchRequestEntry (com.amazonaws.services.sqs.model.DeleteMessageBatchRequestEntry)3 DeleteMessageBatchResult (com.amazonaws.services.sqs.model.DeleteMessageBatchResult)3 DeleteMessageBatchRequest (com.amazonaws.services.sqs.model.DeleteMessageBatchRequest)2 Message (com.amazonaws.services.sqs.model.Message)2 AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)1 AmazonSQSClientBuilder (com.amazonaws.services.sqs.AmazonSQSClientBuilder)1 BatchResultErrorEntry (com.amazonaws.services.sqs.model.BatchResultErrorEntry)1 ChangeMessageVisibilityBatchRequestEntry (com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchRequestEntry)1 ChangeMessageVisibilityBatchResult (com.amazonaws.services.sqs.model.ChangeMessageVisibilityBatchResult)1 GetQueueAttributesRequest (com.amazonaws.services.sqs.model.GetQueueAttributesRequest)1 MessageAttributeValue (com.amazonaws.services.sqs.model.MessageAttributeValue)1 MessageSystemAttributeName (com.amazonaws.services.sqs.model.MessageSystemAttributeName)1 QueueAttributeName (com.amazonaws.services.sqs.model.QueueAttributeName)1 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)1 ReceiveMessageResult (com.amazonaws.services.sqs.model.ReceiveMessageResult)1 IOException (java.io.IOException)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1