简体   繁体   English

AWS Lambda 和 SQS:故障报告

[英]AWS Lambda and SQS: failure reporting

It's more of a "best practice" or "am I doing it right" question, but I can't find the answer anywhere on the Internet.这更像是一个“最佳实践”或“我做得对吗”的问题,但我在互联网上的任何地方都找不到答案。 I'm preparing an AWS Lambda (Java), which will be triggered by SQS events.我正在准备一个 AWS Lambda (Java),它将由 SQS 事件触发。 It will receive up to 10 events and process them.它将接收多达 10 个事件并进行处理。 If some of the events will not be processed correctly (dependent on external services), it will remove processed events from the SQS queue.如果某些事件无法正确处理(依赖于外部服务),它将从 SQS 队列中删除已处理的事件。 Nothing very fancy really:) Question is - how should I finish the execution of the lambda in case of failure in the processing of the batch (either partial or full).真的没什么特别的:)问题是 - 如果批处理(部分或全部)失败,我应该如何完成 lambda 的执行。

Code for Lambda handler looks like this: Lambda 处理程序的代码如下所示:

public class LambdaHandler implements RequestHandler<SQSEvent, Void> {

    private final CopierComponent copierComponent;

    public LambdaHandler() {
        this.copierComponent = DaggerCopierComponent.builder().build();
    }

    @Override
    public Void handleRequest(SQSEvent sqsEvent, Context context) {
        context.getLogger().log("entering the function");
        copierComponent.ldpDynamoToEsCopier().processMessages(sqsEvent);
        return null;
    }
}

Questions:问题:

  1. In case of success, LambdaHandler will just return null.如果成功,LambdaHandler 将只返回 null。 Should it return anything else?它应该返回其他东西吗? Some kind of LambdaResponse with code 200?某种带有代码 200 的 LambdaResponse? Some examples propose String "200 OK", but does it really matter?一些例子提出了字符串“200 OK”,但这真的很重要吗?

  2. In case of failure, processMessages will first remove processed messages from SQS, then raise an exception (BatchProcessingFailure).如果失败, processMessages将首先从 SQS 中删除已处理的消息,然后引发异常 (BatchProcessingFailure)。 SQS will not receive any response, so after visibility timeout, it will return not processed messages back to the queue (there is a DLQ configured as well). SQS 不会收到任何响应,因此在可见性超时后,它会将未处理的消息返回到队列(也配置了 DLQ)。

I don't like this method though.不过我不喜欢这种方法。 Is there some other method of returning some value or finishing lambda with a failed state?是否有其他方法可以返回一些值或用失败的 state 完成 lambda? Some kind of LambdaResponse with code 500, that would inform SQS to not to remove messages and return them to queue?某种代码为 500 的 LambdaResponse,它会通知 SQS 不要删除消息并将它们返回到队列中?

  1. No, you don't need to return anything special.不,您不需要返回任何特别的东西。 The returned data is only relevant for other Lambda integrations, eg in combination with API Gateway.返回的数据仅与其他 Lambda 集成相关,例如与 API 网关结合使用。

  2. I think there's no better way than throwing an exception in case of an error.我认为没有比在发生错误时抛出异常更好的方法了。 From the AWS docs:来自 AWS 文档:

If your function successfully processes the batch, Lambda deletes the messages from the queue.如果您的 function 成功处理了批次,Lambda 将从队列中删除消息。 If your function is throttled, returns an error, or doesn't respond, the message becomes visible again.如果您的 function 受到限制、返回错误或没有响应,则该消息将再次可见。 All messages in a failed batch return to the queue, so your function code must be able to process the same message multiple times without side effects.失败批次中的所有消息都返回队列,因此您的 function 代码必须能够多次处理同一消息而不会产生副作用。

Source: Using AWS Lambda with Amazon SQS来源:将 AWS Lambda 与 Amazon SQS 结合使用

My suggestions for you in case you don't want to reprocess messages multiple times:如果您不想多次重新处理消息,我对您的建议:

  1. Delete all successful messages (immediately) from the queue.从队列中删除所有成功的消息(立即)。
  2. If one or more messages fail, throw an exception in your Lambda function.如果一条或多条消息失败,请在 Lambda function 中抛出异常。 If you don't throw an exception, the Lambda service thinks everything went fine and deletes all messages of the batch from the queue - this is not what you want.如果您不抛出异常,Lambda 服务会认为一切正常并从队列中删除该批次的所有消息 - 这不是您想要的。

There seems to be support now for partial failures when consuming SQS messages from Lambda: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting在使用来自 Lambda 的 SQS 消息时,现在似乎支持部分故障: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#services-sqs-batchfailurereporting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM