-
Notifications
You must be signed in to change notification settings - Fork 92
feat: Support CRaC and priming of powertools metrics and idempotency-dynamodb #1861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
597d123
ec43605
91ee9df
e10a624
747dad8
c8d2404
7ba4cfa
8b2dba4
85513ea
e1906e9
0c80204
babd855
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2023 Amazon.com, Inc. or its affiliates. | ||
* Licensed under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
package software.amazon.lambda.powertools.common.internal; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.util.Enumeration; | ||
|
||
/** | ||
* Used to preload classes to support automatic priming for SnapStart | ||
*/ | ||
public class ClassPreLoader { | ||
Check failure on line 28 in powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java
|
||
public static final String CLASSES_FILE = "classesloaded.txt"; | ||
|
||
private ClassPreLoader() { | ||
// Hide default constructor | ||
} | ||
/** | ||
* Initializes the classes listed in the classesloaded resource | ||
*/ | ||
public static void preloadClasses() { | ||
try { | ||
Enumeration<URL> files = ClassPreLoader.class.getClassLoader().getResources(CLASSES_FILE); | ||
// If there are multiple files, preload classes from all of them | ||
while (files.hasMoreElements()) { | ||
URL url = files.nextElement(); | ||
URLConnection conn = url.openConnection(); | ||
conn.setUseCaches(false); | ||
InputStream is = conn.getInputStream(); | ||
Check failure on line 45 in powertools-common/src/main/java/software/amazon/lambda/powertools/common/internal/ClassPreLoader.java
|
||
preloadClassesFromStream(is); | ||
} | ||
} catch (IOException ignored) { | ||
// No action is required if preloading fails for any reason | ||
} | ||
} | ||
|
||
/** | ||
* Loads the list of classes passed as a stream | ||
* | ||
* @param is | ||
*/ | ||
private static void preloadClassesFromStream(InputStream is) { | ||
try (is; | ||
InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); | ||
BufferedReader reader = new BufferedReader(isr)) { | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
int idx = line.indexOf('#'); | ||
if (idx != -1) { | ||
line = line.substring(0, idx); | ||
} | ||
final String className = line.stripTrailing(); | ||
if (!className.isBlank()) { | ||
Class.forName(className, true, ClassPreLoader.class.getClassLoader()); | ||
} | ||
} | ||
} catch (Exception ignored) { | ||
// No action is required if preloading fails for any reason | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package software.amazon.lambda.powertools.common.internal; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class ClassPreLoaderTest { | ||
|
||
@Test | ||
void preloadClasses_shouldIgnoreInvalidClassesAndLoadValidClasses() { | ||
// Verify that the missing class did not throw any exception | ||
assertDoesNotThrow(ClassPreLoader::preloadClasses); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
software.amazon.lambda.powertools.common.internal.NonExistingClass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
|
||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.crac.Resource; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -46,7 +47,7 @@ | |
* {@link software.amazon.lambda.powertools.idempotency.persistence.PersistenceStore} | ||
* to store the result of previous calls. | ||
*/ | ||
public class IdempotencyHandler { | ||
public class IdempotencyHandler implements Resource{ | ||
private static final Logger LOG = LoggerFactory.getLogger(IdempotencyHandler.class); | ||
private static final int MAX_RETRIES = 2; | ||
|
||
|
@@ -63,6 +64,28 @@ | |
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) { | ||
Check failure on line 79 in powertools-idempotency/powertools-idempotency-core/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java
|
||
// This is unexpected but we must continue without any interruption | ||
} | ||
Check failure on line 81 in powertools-idempotency/powertools-idempotency-core/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java
|
||
} | ||
|
||
@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 | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we move this to the DynamoDBPersistenceStore.java? It will not run when included here because this class is only used from the AspectJ Aspect. Also, don't forget to register the CraC resource in the constructor of the class. From the DynamoDBPersistenceStore we can then run some more logic such as createRecord, updateRecord, and getRecord: /**
* 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 {
System.out.println("beforeCheckpoint");
this.getRecord("__invoke_prime__");
// Add logic for createRecord, updateRecord here and catch exceptions
} catch (IdempotencyItemNotFoundException keyNotFound) {
// This is expected, as we are priming the store
System.out.println("beforeCheckpoint IdempotencyItemNotFoundException");
} catch (Exception unknown) {
// This is unexpected but we must continue without any interruption
System.out.println("beforeCheckpoint Exception");
}
}
@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
System.out.println("afterRestore");
} |
||
/** | ||
* Main entry point for handling idempotent execution of a function. | ||
* | ||
|
@@ -204,7 +227,7 @@ | |
Object response; | ||
try { | ||
response = pjp.proceed(pjp.getArgs()); | ||
} catch (Throwable handlerException) { | ||
Check failure on line 230 in powertools-idempotency/powertools-idempotency-core/src/main/java/software/amazon/lambda/powertools/idempotency/internal/IdempotencyHandler.java
|
||
// We need these nested blocks to preserve function's exception in case the persistence store operation | ||
// also raises an exception | ||
try { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also move this dependency to the dynamodb module for now.