【问题标题】:Amazon Lambda timing out when attempting to initialize a client for DynamoDB尝试为 DynamoDB 初始化客户端时,Amazon Lambda 超时
【发布时间】:2018-03-25 18:06:48
【问题描述】:

我在 Amazon 的 Lambda 服务上上传了以下 Java 类:

public class DevicePutHandler implements RequestHandler<DeviceRequest, Device> {
    private static final Logger log = Logger.getLogger(DevicePutHandler.class);

    public Device handleRequest(DeviceRequest request, Context context) {
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.defaultClient();
        DynamoDBMapper mapper = new DynamoDBMapper(client);

        if (request == null) {
            log.info("The request had a value of null.");
            return null;
        }

        log.info("Retrieving device");
        Device deviceRetrieved = mapper.load(Device.class, request.getDeviceId());

        log.info("Updating device properties");
        deviceRetrieved.setBuilding(request.getBuilding());
        deviceRetrieved.setMotionPresent(request.getMotionPresent());
        mapper.save(deviceRetrieved);

        log.info("Updated device has been saved");
        return deviceRetrieved;
    }
}

我还有一个执行角色集,可以让我完全控制 DynamoDB。我的权限应该完全没问题,因为我在其他以这种方式使用 Lambda 和 DynamoDB 的项目中使用了完全相同的权限(唯一的区别是不同的请求类型)。

这个类的目的是让它被 API Gateway (API Gateway -> Lambda -> DynamoDB) 调用,但现在我只是想在 Lambda (Lambda -> DynamoDB) 上测试它。

作为参考,以防万一,这里是 DeviceRequest 类:

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({ "deviceId", "building", "motionPresent" })
public class DeviceRequest {

   @JsonProperty("deviceId")
   private String deviceId;
   @JsonProperty("building")
   private String building;
   @JsonProperty("motionPresent")
   private Boolean motionPresent;
   @JsonIgnore
   private Map<String, Object> additionalProperties = new HashMap<String, Object>();

   @JsonProperty("deviceId")
   public String getDeviceId() {
       return deviceId;
   }

   @JsonProperty("deviceId")
   public void setDeviceId(String deviceId) {
       this.deviceId = deviceId;
   }

   @JsonProperty("building")
   public String getBuilding() {
       return building;
   }

    @JsonProperty("building")
    public void setBuilding(String building) {
        this.building = building;
    }

    @JsonProperty("motionPresent")
    public Boolean getMotionPresent() {
        return motionPresent;
    }

    @JsonProperty("motionPresent")
        public void setMotionPresent(Boolean motionPresent) {
        this.motionPresent = motionPresent;
    }

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

}

这里是设备类:

@DynamoDBTable(tableName="DeviceTable")
public class Device {
    private String deviceID;
    private String building;
    private String queue;
    private boolean motionPresent;

    @DynamoDBHashKey(attributeName="Device ID")
    public String getDeviceID() {
        return deviceID;
    }

    public void setDeviceID(String deviceID) {
        this.deviceID = deviceID;
    }

    @DynamoDBAttribute(attributeName="Motion Present")
    public boolean getMotionPresent() {
        return motionPresent;
    }

    public void setMotionPresent(boolean motionPresent) {
        this.motionPresent = motionPresent;
    }

    @DynamoDBAttribute(attributeName="Building")
    public String getBuilding() {
        return this.building;
    }

    public void setBuilding(String building) {
        this.building = building;
    }

    @DynamoDBAttribute(attributeName="Queue")
    public String getQueue() {
        return this.queue;
    }

    public void setQueue(String queue) {
        this.queue = queue;
    }
}

这是我尝试用来测试 Lambda 的 JSON 输入:

{
  "deviceId": "test_device_name",
  "building": "building1",
  "motionPresent": false
}

不会引发任何异常(我尝试将其包装在 try/catch 块周围),并且 lambda 超时是唯一发生的事情。在初始化 DynamoDB 客户端之前,我一开始就尝试使用 log/print 语句来查看是否可以正确读取请求,并且它似乎可以正确解析 JSON 字段。我也把clientbuilder分离出来,发现builder对象是可以初始化的,但是超时来自builder调用build()做client的时候。

如果有人知道为什么会发生这种超时,请告诉我!

【问题讨论】:

  • 超过了 Lambda 超时?还是在没有对 DynamoDB 的出站 Internet 访问(或 VPC 终端节点)的 VPC 中运行?
  • 错误只是说“任务​​在 3.00 秒后超时”。如果我更改超时时间,它将一直运行,直到超过我设置的值。我没有在 VPC 中配置任何东西。

标签: java amazon-web-services amazon-dynamodb aws-lambda


【解决方案1】:

事实证明,通过增加超时时间和分配的内存,问题得到了解决。不知道为什么它会起作用,因为 lambda 总是表明它的内存使用量低于先前设置的限制,但是很好。希望未来亚马逊能够提供更好的错误反馈,指示 lambda 是否需要更多资源才能运行。

【讨论】:

  • 当你增加内存分配时,你也增加了分配给你的 lambda 的 CPU 量,这可能会导致你的 lambda 休眠一点。
  • 我遇到了与 urs 完全相同的问题。这是我的存储库github.com/HUAZHEYINy/DynamodbLockingDemo/blob/master/…。如果我增加内存的大小,那么它会成功执行。这应该是 aws lambda 方面的问题,他们应该提供更好的日志记录或解决问题。我在亚马逊工作,我可以在内部提出问题,稍后会更新这篇文章并提供更多信息
猜你喜欢
  • 1970-01-01
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多