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. 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.
This documentation provides guidance for automatic class priming in Powertools for AWS Lambda Java modules.
Classes are proactively loaded using Java runtime hooks which are part of the open source CRaC (Coordinated Restore at Checkpoint) project.
Implementations across the project use the beforeCheckpoint() hook, to prime Snapstart-enabled Java functions via Class Priming.
In order to generate the classloaded.txt file for a Java module in this project, follow these general steps.
-
Add Maven Profile
- Add maven test profile with the following VM argument for generating classes loaded files.
-Xlog:class+load=info:classesloaded.txt
- You can find an example of this in
generate-classesloaded-fileprofile in this pom.xml.
-
Generate classes loaded file
- Run tests with
-Pgenerate-classesloaded-fileprofile.
mvn -Pgenerate-classesloaded-file clean test- This will generate a file named
classesloaded.txtin the target directory of the module.
- Run tests with
-
Cleanup the file
- The classes loaded file generated in Step 2 has the format
[0.054s][info][class,load] java.lang.Object source: shared objects filebut we are only interested injava.lang.Object- the fully qualified class name. - To strip the lines to include only the fully qualified class name,
Use the following regex to replace with empty string.
^\[[\[\]0-9.a-z,]+(to replace the left part)( source: )[0-9a-z :/._$-]+(to replace the right part)
- The classes loaded file generated in Step 2 has the format
-
Add file to resources
- Move the cleaned-up file to the corresponding
src/main/resourcesdirectory of the module. See example.
- Move the cleaned-up file to the corresponding
-
Register and checkpoint
- A class, usually the entry point of the module, should register the CRaC resource in the constructor. Example
- Note that AspectJ aspect is not suitable for this purpose, as it does not work with CRaC.
- Add the
beforeCheckpoint()hook in the same class to invokeClassPreLoader.preloadClasses(). TheClassPreLoaderclass is implemented inpowertools-commonmodule. - This will ensure that the classes are already pre-loaded by the Snapstart RESTORE operation leading to a shorter INIT duration.
- A class, usually the entry point of the module, should register the CRaC resource in the constructor. Example
- This is a manual process at the moment, but it can be automated in the future.
classesloaded.txtfile 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 byClassPreLoader.preloadClasses(). AlsobeforeCheckpoint()hook is not time-sensitive, it only runs once when a new Lambda version gets published.
Working example is available in the powertools-metrics.