diff --git a/README.md b/README.md index 6bcdbdcd1..5d8fad4c3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ +# AWS Lambda Libs on CRaC + +This is a version of the original AWS Lambda Libraries with CRaC support added. + +To use, add next to the pom.xml: +``` + + io.github.crac.com.amazonaws + aws-lambda-java-runtime-interface-client + 2.4.1.CRAC.0 + +``` + # AWS Lambda Java Support Libraries Key libraries for running Java on the AWS Lambda platform. diff --git a/aws-lambda-java-runtime-interface-client/pom.xml b/aws-lambda-java-runtime-interface-client/pom.xml index fe299bdd1..787ab5c19 100644 --- a/aws-lambda-java-runtime-interface-client/pom.xml +++ b/aws-lambda-java-runtime-interface-client/pom.xml @@ -2,9 +2,9 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 - com.amazonaws + io.github.crac.com.amazonaws aws-lambda-java-runtime-interface-client - 2.4.1 + 2.4.1.CRAC.0 jar AWS Lambda Java Runtime Interface Client @@ -20,9 +20,15 @@ - https://github.com/aws/aws-lambda-java-libs.git + http://github.com/CRaC/aws-lambda-java-libs/tree/master + + antonkozlov + Anton Kozlov + https://github.com/AntonKozlov + akozlov@azul.com + AWS Lambda team Amazon Web Services @@ -60,6 +66,11 @@ aws-lambda-java-serialization 1.1.2 + + org.crac + crac + 1.4.0 + org.junit.jupiter @@ -280,11 +291,11 @@ nexus-staging-maven-plugin 1.6.3 true - - sonatype-nexus-staging - https://aws.oss.sonatype.org/ - false - + + ossrh + https://s01.oss.sonatype.org/ + false + diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java index 1d4d3aa59..e79ba6142 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java @@ -42,11 +42,11 @@ public LambdaRuntimeClient(String hostnamePort) { } public InvocationRequest waitForNextInvocation() { - return NativeClient.next(); + return NativeClient.nextWrapper(); } public void postInvocationResponse(String requestId, byte[] response) { - NativeClient.postInvocationResponse(requestId.getBytes(UTF_8), response); + NativeClient.postInvocationResponseWrapper(requestId.getBytes(UTF_8), response); } public void postInvocationError(String requestId, byte[] errorResponse, String errorType) throws IOException { diff --git a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/NativeClient.java b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/NativeClient.java index 82088c0f9..785b95864 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/NativeClient.java +++ b/aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/NativeClient.java @@ -2,9 +2,14 @@ package com.amazonaws.services.lambda.runtime.api.client.runtimeapi; +import org.crac.Context; +import org.crac.Resource; + import java.io.FileNotFoundException; import java.io.InputStream; +import java.lang.annotation.Native; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; @@ -15,12 +20,56 @@ * interactions with the Runtime API. */ class NativeClient { + static class CheckpointState implements Resource { + enum State { + WORKING, + SYNCING, + SYNCED, + }; + + State state = State.WORKING; + + private void waitFor(State targetState) { + while (state != targetState) { + try { + this.wait(); + } catch (InterruptedException interruptedException) { + } + } + } + + @Override + public synchronized void beforeCheckpoint(Context context) throws Exception { + state = State.SYNCING; + waitFor(State.SYNCED); + deinitializeClient(); + } + + @Override + public synchronized void afterRestore(Context context) throws Exception { + initUserAgent(); + state = State.WORKING; + this.notifyAll(); + } + + public synchronized void syncPoint() { + if (state == State.SYNCING) { + state = State.SYNCED; + this.notifyAll(); + } + waitFor(State.WORKING); + } + } + + static CheckpointState checkpointState = new CheckpointState(); + private static final String NATIVE_LIB_PATH = "/tmp/.libaws-lambda-jni.so"; public static final String NATIVE_CLIENT_JNI_PROPERTY = "com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI"; static void init() { loadJNILib(); initUserAgent(); + org.crac.Core.getGlobalContext().register(checkpointState); } private static void loadJNILib() { @@ -77,12 +126,23 @@ private static void initUserAgent() { NativeClient.class.getPackage().getImplementationVersion()); initializeClient(userAgent.getBytes()); + } static native void initializeClient(byte[] userAgent); - static native InvocationRequest next(); + private static native InvocationRequest next(); + + static InvocationRequest nextWrapper() { + return next(); + } - static native void postInvocationResponse(byte[] requestId, byte[] response); + private static native void postInvocationResponse(byte[] requestId, byte[] response); + + static void postInvocationResponseWrapper(byte[] requestId, byte[] response) { + postInvocationResponse(requestId, response); + checkpointState.syncPoint(); + } + static native void deinitializeClient(); } diff --git a/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.cpp b/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.cpp index 87fa9f028..240a8a795 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.cpp +++ b/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.cpp @@ -149,3 +149,9 @@ JNIEXPORT void JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_run throwLambdaRuntimeClientException(env, errorMessage, outcome.get_failure()); } } + +JNIEXPORT void JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient_deinitializeClient(JNIEnv *env, jobject thisObject) { + delete CLIENT; + CLIENT = NULL; +} + diff --git a/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.h b/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.h index 28a6f444a..47d1265df 100644 --- a/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.h +++ b/aws-lambda-java-runtime-interface-client/src/main/jni/com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient.h @@ -15,6 +15,9 @@ JNIEXPORT jobject JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_ JNIEXPORT void JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient_postInvocationResponse (JNIEnv *, jobject, jbyteArray, jbyteArray); +JNIEXPORT void JNICALL Java_com_amazonaws_services_lambda_runtime_api_client_runtimeapi_NativeClient_deinitializeClient + (JNIEnv *, jobject); + #ifdef __cplusplus } #endif