Skip to content

Commit e10a624

Browse files
committed
Invoke prime Dynamo persistent store
1 parent 91ee9df commit e10a624

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

powertools-idempotency/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
<groupId>software.amazon.lambda</groupId>
4949
<artifactId>powertools-common</artifactId>
5050
</dependency>
51+
<dependency>
52+
<groupId>org.crac</groupId>
53+
<artifactId>crac</artifactId>
54+
</dependency>
5155
<!-- Test dependencies -->
5256
<dependency>
5357
<groupId>org.junit.jupiter</groupId>

powertools-idempotency/powertools-idempotency-core/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.aspectj.lang.ProceedingJoinPoint;
2525
import org.aspectj.lang.reflect.MethodSignature;
26+
import org.crac.Resource;
2627
import org.slf4j.Logger;
2728
import org.slf4j.LoggerFactory;
2829

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

@@ -63,6 +64,27 @@ public IdempotencyHandler(ProceedingJoinPoint pjp, String functionName, JsonNode
6364
persistenceStore.configure(Idempotency.getInstance().getConfig(), functionName);
6465
}
6566

67+
/**
68+
* Primes the persistent store by invoking the get record method with a key that doesn't exist.
69+
*
70+
* @param context
71+
* @throws Exception
72+
*/
73+
@Override
74+
public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
75+
try {
76+
persistenceStore.getRecord("__invoke_prime__");
77+
} catch (IdempotencyItemNotFoundException keyNotFound) {
78+
// This is expected, as we are priming the store
79+
} catch (Exception unknown) {
80+
// This is unexpected but we must continue without any interruption
81+
}
82+
}
83+
84+
@Override
85+
public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
86+
}
87+
6688
/**
6789
* Main entry point for handling idempotent execution of a function.
6890
*

0 commit comments

Comments
 (0)