Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion Priming.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ In order to generate the `classloaded.txt` file for a Java module in this projec
- `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.

## Reference Implementation
Working example is available in the [powertools-metrics](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java).
Working examples are available in:
- [powertools-metrics](powertools-metrics/src/main/java/software/amazon/lambda/powertools/metrics/MetricsFactory.java) - Automatic priming with `ClassPreLoader`
- [powertools-idempotency-dynamodb](powertools-idempotency/powertools-idempotency-dynamodb/src/main/java/software/amazon/lambda/powertools/idempotency/persistence/dynamodb/DynamoDBPersistenceStore.java) - Invoke priming + Automatic priming
34 changes: 34 additions & 0 deletions powertools-idempotency/powertools-idempotency-dynamodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
<groupId>org.crac</groupId>
<artifactId>crac</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.lambda</groupId>
<artifactId>powertools-common</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down Expand Up @@ -106,6 +110,24 @@
</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>
<!-- The AWS DynamoDBClient uses com.amazonaws.xray.interceptors.TracingInterceptor if available on the
Expand All @@ -118,6 +140,11 @@
<artifactId>aws-xray-recorder-sdk-aws-sdk-v2-instrumentor</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-subclass</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -138,6 +165,13 @@
</profile>
<profile>
<id>graalvm-native</id>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-subclass</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest;
import software.amazon.lambda.powertools.common.internal.ClassPreLoader;
import software.amazon.lambda.powertools.common.internal.UserAgentConfigurator;
import software.amazon.lambda.powertools.idempotency.Constants;
import software.amazon.lambda.powertools.idempotency.exceptions.IdempotencyItemAlreadyExistsException;
Expand Down Expand Up @@ -123,7 +124,16 @@ private DynamoDBPersistenceStore(String tableName,
*/
@Override
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
// Skip priming if DynamoDB client is not available
if (dynamoDbClient == null) {
return;
}

try {
// Automatic priming - preload classes from classesloaded.txt
ClassPreLoader.preloadClasses();

// Invoke priming - exercise DynamoDB SDK paths
String primingRecordKey = "__invoke_prime__";
Instant now = Instant.now();
long expiry = now.plus(3600, ChronoUnit.SECONDS).getEpochSecond();
Expand All @@ -137,8 +147,9 @@ public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throw
putRecord(primingDataRecord, Instant.now());
getRecord(primingRecordKey);
deleteRecord(primingRecordKey);
} catch (Exception unknown) {
// This is unexpected but we must continue without any interruption
} catch (Exception e) {
// Log the exception for debugging but continue to ensure checkpoint succeeds
LOG.warn("Priming failed during checkpoint, continuing anyway", e);
}
}

Expand Down
Loading
Loading