Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
597d123
Automatic priming of powertools-metrics
subhash686 May 8, 2025
ec43605
Merge remote-tracking branch 'upstream/v2' into support_crac_on_power…
subhash686 May 8, 2025
91ee9df
Fixed resource name and added unit tests and javadoc comments
subhash686 May 14, 2025
e10a624
Invoke prime Dynamo persistent store
subhash686 May 17, 2025
747dad8
Merge branch 'v2' into support_crac_on_powertools_metrics
subhash686 May 28, 2025
c8d2404
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jun 12, 2025
7ba4cfa
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jun 13, 2025
8b2dba4
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jun 24, 2025
85513ea
Fixed Sonar issues
subhash686 Jul 2, 2025
e1906e9
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jul 7, 2025
0c80204
Update classesloaded.txt
subhash686 Jul 9, 2025
babd855
Merge branch 'main' into support_crac_on_powertools_metrics
phipag Jul 9, 2025
789564b
Moved priming to MetricsFactory and DynamoDBPersistenceStore, added P…
subhash686 Jul 22, 2025
4cebb6b
Update classesloaded.txt
subhash686 Jul 22, 2025
6336252
Update LambdaMetricsAspect.java
subhash686 Jul 22, 2025
27dad71
Update DynamoDBPersistenceStore.java
subhash686 Jul 22, 2025
685877e
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jul 22, 2025
ec37b49
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jul 28, 2025
a1e1545
Improved priming.md documentation and static initialization of classe…
subhash686 Jul 30, 2025
6261d06
Merge branch 'support_crac_on_powertools_metrics' of https://github.c…
subhash686 Jul 30, 2025
844dad7
Fixed a Sonarqube finding and added more unit tests to ensure all ava…
subhash686 Jul 31, 2025
9b6e5f9
Merge branch 'main' into support_crac_on_powertools_metrics
subhash686 Jul 31, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions Priming.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Automatic Priming for AWS Lambda Powertools Java

## Table of Contents
- [Overview](#overview)
- [Implementation Steps](#general-implementation-steps)
- [Known Issues](#known-issues-and-solutions)
- [Reference Implementation](#reference-implementation)

## Overview
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 AWS Lambda Powertools Java modules.


## Implementation Steps
Classes are proactively loaded using Java runtime hooks which are part of the open source CRaC (Coordinated Restore at Checkpoint) project.
This implementation uses this hook, called `beforeCheckpoint()`, to prime SnapStart-enabled Java functions via Class Priming.
In order to generate the `classloaded` files for Powertools java module, follow these general steps.

1. **Add Maven Profile**
- Add maven test profile with the following VM argument for generating classes loaded files.
```shell
-Xlog:class+load=info:classesloaded.txt
```
- You can find an example of this in profile `generate-classesloaded-file` in this [pom.xml](powertools-metrics/pom.xml).

2. **Generate classes loaded file**
- Run tests with `-Pgenerate-classesloaded-file` profile.
```shell
mvn -Pgenerate-classesloaded-file clean test
```
- This will generate a file named `classesloaded.txt` in the target directory of the module.

3. **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 file`
but we are only interested in `java.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)

4. **Place the file**
- Move the cleanedup file containing to the corresponding resources directory of the module. See [example](powertools-metrics/src/resources/classesloaded.txt).

5. **Register and checkpoint**
- Register the CRaC Resource in the constructor of the main class.
- Add the `beforeCheckpoint()` hook in the same class to invoke `ClassPreLoader.preloadClasses()`.
- This will ensure that the classes are loaded during the INIT phase of the Lambda function. See [example](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java)
## Known Issues
- This is a manual process at the moment, but it can be automated in the future.
- Because the file is generated while running tests, it includes test classes as well.
## Reference Implementation
Working example is available in the [powertools-metrics](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java).
4 changes: 0 additions & 4 deletions powertools-idempotency/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-common</artifactId>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.crac.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -47,7 +46,7 @@
* {@link software.amazon.lambda.powertools.idempotency.persistence.PersistenceStore}
* to store the result of previous calls.
*/
public class IdempotencyHandler implements Resource{
public class IdempotencyHandler {
private static final Logger LOG = LoggerFactory.getLogger(IdempotencyHandler.class);
private static final int MAX_RETRIES = 2;

Expand All @@ -64,28 +63,6 @@ public IdempotencyHandler(ProceedingJoinPoint pjp, String functionName, JsonNode
persistenceStore.configure(Idempotency.getInstance().getConfig(), functionName);
}

/**
* Primes the persistent store by invoking the get record method with a key that doesn't exist.
*
* @param context
* @throws Exception
*/
@Override
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
try {
persistenceStore.getRecord("__invoke_prime__");
} catch (IdempotencyItemNotFoundException keyNotFound) {
// This is expected, as we are priming the store
} catch (Exception unknown) {
// This is unexpected but we must continue without any interruption
}
}

@Override
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
// This is a no-op, as we don't need to do anything after restore
}

/**
* Main entry point for handling idempotent execution of a function.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package software.amazon.lambda.powertools.idempotency.persistence.dynamodb;

import org.crac.Core;
import org.crac.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
Expand All @@ -37,6 +39,7 @@
import software.amazon.lambda.powertools.idempotency.persistence.PersistenceStore;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -52,7 +55,7 @@
* DynamoDB version of the {@link PersistenceStore}. Will store idempotency data in DynamoDB.<br>
* Use the {@link Builder} to create a new instance.
*/
public class DynamoDBPersistenceStore extends BasePersistenceStore implements PersistenceStore {
public class DynamoDBPersistenceStore extends BasePersistenceStore implements PersistenceStore, Resource {

Check failure on line 58 in powertools-idempotency/powertools-idempotency-dynamodb/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/dynamodb/DynamoDBPersistenceStore.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

This class has only private constructors and may be final

Reports classes that may be made final because they cannot be extended from outside their compilation unit anyway. This is because all their constructors are private, so a subclass could not call the super constructor. ClassWithOnlyPrivateConstructorsShouldBeFinal (Priority: 1, Ruleset: Design) https://docs.pmd-code.org/snapshot/pmd_rules_java_design.html#classwithonlyprivateconstructorsshouldbefinal

public static final String IDEMPOTENCY = "idempotency";
private static final Logger LOG = LoggerFactory.getLogger(DynamoDBPersistenceStore.class);
Expand Down Expand Up @@ -109,6 +112,39 @@
this.dynamoDbClient = null;
}
}
Core.getGlobalContext().register(this);
}

/**
* Primes the persistent store by invoking the get record method with a key that doesn't exist.
*
* @param context
* @throws Exception
*/
@Override
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
try {
String primingRecordKey = "__invoke_prime__";
Instant now = Instant.now();
long expiry = now.plus(3600, ChronoUnit.SECONDS).getEpochSecond();
DataRecord primingDataRecord = new DataRecord(
primingRecordKey,
DataRecord.Status.COMPLETED,
expiry,
null, // no data
null // no validation
);
putRecord(primingDataRecord, Instant.now());
getRecord(primingRecordKey);
deleteRecord(primingRecordKey);
} catch (Exception unknown) {
// This is unexpected but we must continue without any interruption
}

Check failure on line 142 in powertools-idempotency/powertools-idempotency-dynamodb/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/dynamodb/DynamoDBPersistenceStore.java

View workflow job for this annotation

GitHub Actions / pmd_analyse

Avoid empty catch blocks

Empty Catch Block finds instances where an exception is caught, but nothing is done. In most circumstances, this swallows an exception which should either be acted on or reported. EmptyCatchBlock (Priority: 1, Ruleset: Error Prone) https://docs.pmd-code.org/snapshot/pmd_rules_java_errorprone.html#emptycatchblock
}

@Override
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
// This is a no-op, as we don't need to do anything after restore
}

public static Builder builder() {
Expand Down
17 changes: 17 additions & 0 deletions powertools-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@
</dependencies>

<profiles>
<profile>
<id>generate-classesloaded-file</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Xlog:class+load=info:classesloaded.txt
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>generate-graalvm-files</id>
<dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

package software.amazon.lambda.powertools.metrics;

import org.crac.Core;
import org.crac.Resource;
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
import software.amazon.lambda.powertools.common.internal.LambdaConstants;
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
import software.amazon.lambda.powertools.metrics.model.DimensionSet;
Expand All @@ -23,11 +26,12 @@
/**
* Factory for accessing the singleton Metrics instance
*/
public final class MetricsFactory {
public final class MetricsFactory implements Resource {
private static MetricsProvider provider = new EmfMetricsProvider();
private static Metrics metrics;

private MetricsFactory() {
Core.getGlobalContext().register(this);
}

/**
Expand Down Expand Up @@ -68,4 +72,14 @@ public static synchronized void setMetricsProvider(MetricsProvider metricsProvid
// Reset the metrics instance so it will be recreated with the new provider
metrics = null;
}

@Override
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
ClassPreLoader.preloadClasses();
}

@Override
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
// No action needed after restore
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import org.crac.Core;
import org.crac.Resource;

import com.amazonaws.services.lambda.runtime.Context;

import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
import software.amazon.lambda.powertools.common.internal.LambdaConstants;
import software.amazon.lambda.powertools.common.internal.LambdaHandlerProcessor;
import software.amazon.lambda.powertools.metrics.FlushMetrics;
Expand All @@ -37,16 +33,12 @@
import software.amazon.lambda.powertools.metrics.model.DimensionSet;

@Aspect
public class LambdaMetricsAspect implements Resource {
public class LambdaMetricsAspect {
public static final String TRACE_ID_PROPERTY = "xray_trace_id";
public static final String REQUEST_ID_PROPERTY = "function_request_id";
private static final String SERVICE_DIMENSION = "Service";
private static final String FUNCTION_NAME_ENV_VAR = "POWERTOOLS_METRICS_FUNCTION_NAME";

public LambdaMetricsAspect() {
Core.getGlobalContext().register(this);
}

private String functionName(FlushMetrics metrics, Context context) {
if (!"".equals(metrics.functionName())) {
return metrics.functionName();
Expand Down Expand Up @@ -147,14 +139,4 @@ private void captureColdStartMetricIfEnabled(Context extractedContext, FlushMetr
metricsInstance.captureColdStartMetric(extractedContext, coldStartDimensions);
}
}

@Override
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
ClassPreLoader.preloadClasses();
}

@Override
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
// No action needed after restore
}
}
Loading
Loading