From 225aa72f5c72b813a2e16cd67455f381fad846c9 Mon Sep 17 00:00:00 2001 From: Anton Kozlov Date: Tue, 9 Nov 2021 22:27:41 +0300 Subject: [PATCH 1/3] CRaC: initial version --- .../pom.xml | 13 ++ .../runtimeapi/LambdaRuntimeClient.java | 4 +- .../api/client/runtimeapi/NativeClient.java | 114 +++++++++++++++--- ...ime_api_client_runtimeapi_NativeClient.cpp | 6 + ...ntime_api_client_runtimeapi_NativeClient.h | 3 + 5 files changed, 121 insertions(+), 19 deletions(-) diff --git a/aws-lambda-java-runtime-interface-client/pom.xml b/aws-lambda-java-runtime-interface-client/pom.xml index 54d2c06db..01206a7ca 100644 --- a/aws-lambda-java-runtime-interface-client/pom.xml +++ b/aws-lambda-java-runtime-interface-client/pom.xml @@ -35,12 +35,25 @@ 1.8 + + + github + https://maven.pkg.github.com/crac/org.crac + + + + com.amazonaws aws-lambda-java-core 1.2.0 + + org.crac + crac + 999-SNAPSHOT + com.amazonaws aws-lambda-java-serialization 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 05aa50a1b..b623bdb91 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 @@ -38,11 +38,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 f3cda8bcf..de6f75fd4 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,8 +2,13 @@ package com.amazonaws.services.lambda.runtime.api.client.runtimeapi; +import org.crac.Context; +import org.crac.Resource; + 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; @@ -14,14 +19,71 @@ class NativeClient { private static final String nativeLibPath = "/tmp/.aws-lambda-runtime-interface-client"; private static final String[] libsToTry = { - "/aws-lambda-runtime-interface-client.glibc.so", - "/aws-lambda-runtime-interface-client.musl.so", + "aws-lambda-runtime-interface-client.glibc.so", + "aws-lambda-runtime-interface-client.musl.so", }; private static final Throwable[] exceptions = new Throwable[libsToTry.length]; + + 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 { + initializeNativeClient(); + 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(); static { - boolean loaded = false; - for (int i = 0; !loaded && i < libsToTry.length; ++i) { - try (InputStream lib = NativeClient.class.getResourceAsStream(libsToTry[i])) { + boolean loaded = false; + String basestr = System.getProperty("com.amazonaws.services.lambda.runtime.api.client.NativeClient.libsBase", "/"); + Path base = Paths.get(basestr); + for (int i = 0; !loaded && i < libsToTry.length; ++i) { + Path p = base.resolve(libsToTry[i]); + if (Files.exists(p)) { + try { + System.load(p.toString()); + loaded = true; + } catch (UnsatisfiedLinkError e) { + exceptions[i] = e; + } catch (Exception e) { + exceptions[i] = e; + } + } + if (!loaded && exceptions[i] == null) { + try (InputStream lib = NativeClient.class.getResourceAsStream("/" + libsToTry[i])) { Files.copy(lib, Paths.get(nativeLibPath), StandardCopyOption.REPLACE_EXISTING); System.load(nativeLibPath); loaded = true; @@ -31,23 +93,41 @@ class NativeClient { exceptions[i] = e; } } - if (!loaded) { - for (int i = 0; i < libsToTry.length; ++i) { - System.err.printf("Failed to load the native runtime interface client library %s. Exception: %s\n", libsToTry[i], exceptions[i].getMessage()); - } - System.exit(-1); + } + + if (!loaded) { + for (int i = 0; i < libsToTry.length; ++i) { + System.err.print(exceptions[i]); + System.err.printf("Failed to load the native runtime interface client library %s. Exception: %s\n", libsToTry[i], exceptions[i].getMessage()); } - String userAgent = String.format( - "aws-lambda-java/%s-%s" , - System.getProperty("java.vendor.version"), - NativeClient.class.getPackage().getImplementationVersion()); - initializeClient(userAgent.getBytes()); + System.exit(-1); + } + initializeNativeClient(); + org.crac.Core.getGlobalContext().register(checkpointState); + } + + private static void initializeNativeClient() { + String userAgent = String.format( + "aws-lambda-java/%s-%s" , + System.getProperty("java.vendor.version"), + 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(); + } + + private static native void postInvocationResponse(byte[] requestId, byte[] response); - 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 c6a1dabcf..61f8e79a8 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 @@ -126,3 +126,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 From 06684f5a659d76c56a8667d230d54e86f8935364 Mon Sep 17 00:00:00 2001 From: Anton Kozlov Date: Tue, 15 Mar 2022 12:55:06 +0300 Subject: [PATCH 2/3] Update for io.github.crac --- .../pom.xml | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/aws-lambda-java-runtime-interface-client/pom.xml b/aws-lambda-java-runtime-interface-client/pom.xml index 01206a7ca..9a26caac7 100644 --- a/aws-lambda-java-runtime-interface-client/pom.xml +++ b/aws-lambda-java-runtime-interface-client/pom.xml @@ -2,7 +2,7 @@ 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 1.0.0 jar @@ -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 @@ -35,14 +41,6 @@ 1.8 - - - github - https://maven.pkg.github.com/crac/org.crac - - - - com.amazonaws @@ -50,9 +48,9 @@ 1.2.0 - org.crac - crac - 999-SNAPSHOT + io.github.crac + org-crac + 0.1.0 com.amazonaws @@ -255,8 +253,8 @@ 1.6.3 true - sonatype-nexus-staging - https://aws.oss.sonatype.org/ + ossrh + https://s01.oss.sonatype.org/ false From 1c7176a7a5e6002cd84fd82704c942c7f0014869 Mon Sep 17 00:00:00 2001 From: Anton Kozlov Date: Mon, 25 Apr 2022 21:46:33 +0300 Subject: [PATCH 3/3] README: add CRaC note --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 8925d3826..92688a4b9 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 + 1.0.0 + +``` + # AWS Lambda Java Support Libraries Interface definitions for Java code running on the AWS Lambda platform.