简体   繁体   English

AWS Lambda - Java 静态初始化

[英]AWS Lambda - Java static initialization

Inside AWS lambda function (written in Java) I want to use AsyncHttpClient ( https://github.com/AsyncHttpClient/async-http-client ).在 AWS lambda 函数(用 Java 编写)中,我想使用 AsyncHttpClient( https://github.com/AsyncHttpClient/async-http-client )。 Unfortunately it takes around 500 ms to create an instance of this object.. (but I still like it, please don't advice me to change the http client).不幸的是,创建这个对象的实例需要大约 500 毫秒。(但我仍然喜欢它,请不要建议我更改 http 客户端)。

I was considering creating AsyncHttpClient in static initialization block.我正在考虑在静态初始化块中创建 AsyncHttpClient。 So maybe it will be executed once by AWS and than the snapshot would be cloned for the every AWS Lambda execution.所以它可能会被 AWS 执行一次,然后快照会在每次 AWS Lambda 执行时被克隆。 Am I correct?我对么?

When static block are executed in AWS Lambda?什么时候在 AWS Lambda 中执行静态块?

  • Once and than the snapshot is cloned to every AWS Lambda invocation一次快照被克隆到每个 AWS Lambda 调用
  • or static block is executed for every Lambda execution separately (and it will not help to put creation of Http Client there)或为每个 Lambda 执行分别执行静态块(将 Http Client 的创建放在那里无济于事)

Thank you for help谢谢你的帮助

There is no "snapshot" taken of your Lambda execution environment, ever. 您的Lambda执行环境从未有过任何快照。 There is however a concept of container reuse. 但是,有一个容器重用的概念。 A static initialization block will be called when the function runs for the first time in a new container, and each subsequent Lambda execution that is sent to that container will be able to skip the initialization step. 当函数在新容器中首次运行时,将调用静态初始化块,并且发送到该容器的每个后续Lambda执行将能够跳过初始化步骤。 Each time Lambda spins up a new container for your Lambda function that initialization work will need to happen again. 每次Lambda为Lambda函数启动一个新容器时,都需要再次进行初始化工作。

I suggest reading this post on the AWS blog about Lambda container reuse. 我建议在AWS博客上阅读有关Lambda容器重用的文章。

As Mark B explained, there is no such thing as a 'snapshot'. 正如Mark B解释的那样,没有“快照”之类的东西。

AWS starts an execution context the first time your Lambda is called and then reuses it for the next requests. 首次调用您的Lambda时,AWS启动执行上下文,然后将其重用于下一个请求。 However, this is not guaranteed. 但是,这不能保证。 AWS may shut down this context at any time, or create others to scale your Lambda in case your of heavy load. AWS可以随时关闭此上下文,或者创建其他文件来扩展Lambda,以防您负担沉重。

An execution context consists of the container, the JVM, and a Singleton instance of the Java class where your handler function is defined. 执行上下文由容器,JVM和Java类的Singleton实例组成,其中定义了处理程序函数。

Therefore, I would not recommend doing any "one-time" initialization in a static block, but instead in the constructor of your class. 因此,我不建议在静态块中进行任何“一次性”初始化,而建议在类的构造函数中进行。 This will greatly improve the testability of your code. 这将大大提高代码的可测试性。

You can configure AWS Snapstart which will save the state of your lambda after init.您可以配置AWS Snapstart ,它将在初始化后保存您的 lambda 的状态。 I just saw a 1/6 reduced cold start times on a simple java lambda.我刚刚看到一个简单的 Java lambda 冷启动时间减少了 1/6。

From AWS Documentation Linked above: "Lambda SnapStart for Java can improve startup performance for latency-sensitive applications by up to 10x at no extra cost, typically with no changes to your function code. The largest contributor to startup latency (often referred to as cold start time) is the time that Lambda spends initializing the function, which includes loading the function's code, starting the runtime, and initializing the function code.来自上面链接的 AWS 文档:“Lambda SnapStart for Java 可以将延迟敏感应用程序的启动性能提高多达 10 倍,无需额外费用,通常无需更改您的功能代码。启动延迟的最大贡献者(通常称为冷开始时间)是 Lambda 初始化函数所用的时间,包括加载函数代码、启动运行时和初始化函数代码。

With SnapStart, Lambda initializes your function when you publish a function version.借助 SnapStart,Lambda 会在您发布函数版本时初始化您的函数。 Lambda takes a Firecracker microVM snapshot of the memory and disk state of the initialized execution environment, encrypts the snapshot, and caches it for low-latency access. Lambda 获取初始化执行环境的内存和磁盘状态的 Firecracker microVM 快照,加密快照,并将其缓存以实现低延迟访问。 When you invoke the function version for the first time, and as the invocations scale up, Lambda resumes new execution environments from the cached snapshot instead of initializing them from scratch, improving startup latency."当你第一次调用函数版本时,随着调用的增加,Lambda 会从缓存的快照中恢复新的执行环境,而不是从头开始初始化它们,从而改善启动延迟。”

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

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