【问题标题】:AWS Lambda function is reading records from kinesis stream infinitelyAWS Lambda 函数正在无限地从 kinesis 流中读取记录
【发布时间】:2016-03-04 18:17:22
【问题描述】:

我有一个带有一个分片的运动流和一个用 python 编写的 lambda 函数。我将 kinesis 流添加为批处理大小为 5 的事件源。我向 kinesis 添加了数百条记录,并且 lambda 函数被正确调用和执行。但是对于最后 3 条记录,即使函数返回成功,lambda 函数也会被无限调用。

Lambda 函数:

from __future__ import print_function

import base64
import json
import urllib2
import json

print('Loading function')

def is_valid_url(url):
    try:
        urllib2.urlopen(url)
        print("Valid URL ...")
        return True         # URL Exist
    except ValueError, ex:
        print("URL is not formatted...")
        return False        # URL not well formatted
    except urllib2.URLError, ex:
        print("Invalid URL ...")
        return False        # URL don't seem to be alive


def lambda_handler(event, context):     
    for record in event['Records']:
        # Kinesis data is base64 encoded so decode here
        payload = base64.b64decode(record['kinesis']['data'])
        params = json.loads(payload)
        print("Decoded payload: " + payload  + " : " +     str(is_valid_url(params['url'])) + " : " + str(len(event['Records'])))
    return 'Successfully processed {} records.'.format(len(event['Records']))

````

当我查看云观察日志时

  START RequestId: d6033244-1c43-40ea-8886-f38b8c48daa3 Version:   $LATEST 
  Loading function 
  Valid URL ... 
  Decoded payload: { "url": "https://google.com" }
  Valid URL ... 
  Decoded payload: { "url": "https://google.com" }
  Valid URL ... 
  Decoded payload: { "url": "https://google.com" }
  Valid URL ...  
  Decoded payload: { "url": "https://google.com" }
  END RequestId: d6033244-1c43-40ea-8886-f38b8c48daa3 
  REPORT RequestId: d6033244-1c43-40ea-8886-f38b8c48daa3    Duration: 3003.00 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 10 MB   
  2016-03-04T17:32:01.030Z d6033244-1c43-40ea-8886-f38b8c48daa3 Task timed out after 3.00 seconds

我无法弄清楚 lambda 函数发生了什么。有人可以提供一些有关此错误的见解。

【问题讨论】:

  • 关于在每批中仅读取 5 个事件的评论:您将很快开始累积处理延迟,因为您每秒可以将 1000 个事件写入分片,并且应该计划在每个分片中处理所有事件第二。

标签: amazon-web-services amazon-kinesis aws-lambda


【解决方案1】:

由于您的函数超时,Lambda 将运行视为错误。 Kinesis 的错误处理策略是重试记录,直到它脱离 Trim Horizo​​n(通常为 24 小时),因此您的函数将重试 24 小时或直到它不超时。

根据您发布的内容,我无法判断您的功能为何超时。一个快速的解决方法是简单地增加 Lambda 控制台上的超时值(在配置选项卡上的高级下)

【讨论】:

  • 由于某种原因,ping 某些 url 需要超过 3 秒的时间,因此增加超时效果很好。感谢您的回复
猜你喜欢
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-02
  • 2018-09-20
相关资源
最近更新 更多