You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: Support CRaC priming of powertools metrics and idempotency-dynamodb (#1861)
* Automatic priming of powertools-metrics
* Fixed resource name and added unit tests and javadoc comments
* Invoke prime Dynamo persistent store
* Fixed Sonar issues
* Update classesloaded.txt
replaced build time class list with run time class list
* Moved priming to MetricsFactory and DynamoDBPersistenceStore, added Priming.md
* Update classesloaded.txt
classesloaded file generated using the new method
* Update LambdaMetricsAspect.java
Removed unused import
* Update DynamoDBPersistenceStore.java
Fixed variable naming Sonar issue
* Improved priming.md documentation and static initialization of classes using for priming. Also fixed an unrelated flaky unit test
* Fixed a Sonarqube finding and added more unit tests to ensure all available classes are loaded
---------
Co-authored-by: Philipp Page <[email protected]>
Priming is the process of preloading dependencies and initializing resources during the INIT phase, rather than during the INVOKE phase to further optimize startup performance with SnapStart.
11
+
This is required because Java frameworks that use dependency injection load classes into memory when these classes are explicitly invoked, which typically happens during Lambda’s INVOKE phase.
12
+
13
+
This documentation provides guidance for automatic class priming in Powertools for AWS Lambda Java modules.
14
+
15
+
16
+
## Implementation Steps
17
+
Classes are proactively loaded using Java runtime hooks which are part of the open source [CRaC (Coordinated Restore at Checkpoint) project](https://openjdk.org/projects/crac/).
18
+
Implementations across the project use the `beforeCheckpoint()` hook, to prime Snapstart-enabled Java functions via Class Priming.
19
+
In order to generate the `classloaded.txt` file for a Java module in this project, follow these general steps.
20
+
21
+
1.**Add Maven Profile**
22
+
- Add maven test profile with the following VM argument for generating classes loaded files.
23
+
```shell
24
+
-Xlog:class+load=info:classesloaded.txt
25
+
```
26
+
- You can find an example of this in`generate-classesloaded-file` profile in this [pom.xml](powertools-metrics/pom.xml).
27
+
28
+
2. **Generate classes loaded file**
29
+
- Run tests with `-Pgenerate-classesloaded-file` profile.
30
+
```shell
31
+
mvn -Pgenerate-classesloaded-file clean test
32
+
```
33
+
- This will generate a file named `classesloaded.txt`in the target directory of the module.
34
+
35
+
3. **Cleanup the file**
36
+
- The classes loaded file generated in Step 2 has the format
but we are only interested in`java.lang.Object` - the fully qualified class name.
39
+
- To strip the lines to include only the fully qualified class name,
40
+
Use the following regex to replace with empty string.
41
+
- `^\[[\[\]0-9.a-z,]+ ` (to replace the left part)
42
+
- `( source: )[0-9a-z :/._$-]+` (to replace the right part)
43
+
44
+
4. **Add file to resources**
45
+
- Move the cleaned-up file to the corresponding `src/main/resources` directory of the module. See [example](powertools-metrics/src/main/resources/classesloaded.txt).
46
+
47
+
5. **Register and checkpoint**
48
+
- A class, usually the entry point of the module, should register the CRaC resource in the constructor. [Example](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java)
49
+
- Note that AspectJ aspect is not suitable for this purpose, as it does not work with CRaC.
50
+
- Add the `beforeCheckpoint()` hook in the same class to invoke `ClassPreLoader.preloadClasses()`. The `ClassPreLoader` class is implemented in`powertools-common` module.
51
+
- This will ensure that the classes are already pre-loaded by the Snapstart RESTORE operation leading to a shorter INIT duration.
52
+
53
+
54
+
## Known Issues
55
+
- This is a manual process at the moment, but it can be automated in the future.
56
+
- `classesloaded.txt` file includes test classes as well because the file is generated while running tests. This is not a problem because all the classes that are not found are ignored by `ClassPreLoader.preloadClasses()`. Also `beforeCheckpoint()` hook is not time-sensitive, it only runs once when a new Lambda version gets published.
57
+
58
+
## Reference Implementation
59
+
Working example is available in the [powertools-metrics](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java).
Copy file name to clipboardExpand all lines: powertools-idempotency/powertools-idempotency-dynamodb/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/dynamodb/DynamoDBPersistenceStore.java
Copy file name to clipboardExpand all lines: powertools-logging/powertools-logging-logback/src/test/java/software/amazon/lambda/powertools/logging/internal/LambdaJsonEncoderTest.java
0 commit comments