Skip to content

Commit

Permalink
Prepare platform specific jar files (#429)
Browse files Browse the repository at this point in the history
  • Loading branch information
smirnoal committed Jul 17, 2023
1 parent f6358f7 commit f9abdf5
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 116 deletions.
8 changes: 8 additions & 0 deletions aws-lambda-java-runtime-interface-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,19 @@ pr: test test-smoke
.PHONY: build
build:
mvn clean install
mvn install -P linux-x86_64
mvn install -P linux_musl-x86_64
mvn install -P linux-aarch64
mvn install -P linux_musl-aarch64

.PHONY: publish
publish:
./ric-dev-environment/publish_snapshot.sh

.PHONY: publish
test-publish:
./ric-dev-environment/test-platform-specific-jar-snapshot.sh

define HELP_MESSAGE

Usage: $ make [TARGETS]
Expand Down
27 changes: 27 additions & 0 deletions aws-lambda-java-runtime-interface-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ DOCKERHUB_PASSWORD=<dockerhub password>
```
Recommended way is to set the Docker Hub credentials in CodeBuild job by retrieving them from AWS Secrets Manager.

## Configuration
The `aws-lambda-java-runtime-interface-client` JAR is a large uber jar, which contains compiled C libraries
for x86_64 and aarch_64 for glibc and musl LIBC implementations. If the size is an issue, you can pick a smaller
platform-specific JAR by setting the `<classifier>`.
```
<!-- Platform-specific Linux x86_64 JAR -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
<version>2.3.2</version>
<classifier>linux-x86_64</classifier>
</dependency>
```

Available platform classifiers: `linux-x86_64`, `linux-aarch_64`, `linux_musl-aarch_64`, `linux_musl-x86_64`

The Lambda runtime interface client tries to load compatible library during execution, by unpacking it to a temporary
location `/tmp/.libaws-lambda-jni.so`.
If this behaviour is not desirable, it is possible to extract the `.so` files during build time and specify the location via
`com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI` system property, like
```
ENTRYPOINT [ "/usr/bin/java",
"-Dcom.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI=/function/libaws-lambda-jni.linux_x86_64.so"
"-cp", "./*",
"com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ]
```

## Security

If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue.
Expand Down
42 changes: 42 additions & 0 deletions aws-lambda-java-runtime-interface-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
separately from the Runtime Interface Client functionality until we figure something else out.
-->
<multiArch>true</multiArch>
<target_build_os/>
<target_build_arch/>
<ric.classifier/>
</properties>

<dependencies>
Expand Down Expand Up @@ -119,6 +122,8 @@
failonerror="true" logError="true">
<arg value="${project.build.directory}"/>
<arg value="${multiArch}"/>
<arg value="${target_build_os}"/>
<arg value="${target_build_arch}"/>
</exec>
</target>
</configuration>
Expand Down Expand Up @@ -147,6 +152,11 @@
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
<classifier>${ric.classifier}</classifier>
<includes>
<include>com/</include>
<include>jni/*${ric.classifier}.so</include>
</includes>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -288,5 +298,37 @@
</repository>
</distributionManagement>
</profile>
<profile>
<id>linux-x86_64</id>
<properties>
<target_build_os>linux</target_build_os>
<target_build_arch>x86_64</target_build_arch>
<ric.classifier>linux-x86_64</ric.classifier>
</properties>
</profile>
<profile>
<id>linux_musl-x86_64</id>
<properties>
<target_build_os>linux_musl</target_build_os>
<target_build_arch>x86_64</target_build_arch>
<ric.classifier>linux_musl-x86_64</ric.classifier>
</properties>
</profile>
<profile>
<id>linux-aarch64</id>
<properties>
<target_build_os>linux</target_build_os>
<target_build_arch>aarch_64</target_build_arch>
<ric.classifier>linux-aarch_64</ric.classifier>
</properties>
</profile>
<profile>
<id>linux_musl-aarch64</id>
<properties>
<target_build_os>linux_musl</target_build_os>
<target_build_arch>aarch_64</target_build_arch>
<ric.classifier>linux_musl-aarch_64</ric.classifier>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash -x

set -e

Expand All @@ -18,5 +18,33 @@ else
echo "Already -SNAPSHOT version"
fi

mvn -P ci-repo deploy --settings ric-dev-environment/settings.xml
mv pom.xml.versionsBackup pom.xml
CLASSIFIERS_ARRAY=("linux-x86_64" "linux_musl-x86_64" "linux-aarch_64" "linux_musl-aarch_64")

for str in "${CLASSIFIERS_ARRAY[@]}"; do
FILES="${FILES}target/aws-lambda-java-runtime-interface-client-$projectVersion-$str.jar,"
CLASSIFIERS="${CLASSIFIERS}${str},"
TYPES="${TYPES}jar,"
done

# remove the last ","
FILES=${FILES%?}
CLASSIFIERS=${CLASSIFIERS%?}
TYPES=${TYPES%?}

mvn -B -X -P ci-repo \
deploy:deploy-file \
-DgroupId=com.amazonaws \
-DartifactId=aws-lambda-java-runtime-interface-client \
-Dpackaging=jar \
-Dversion=$projectVersion \
-Dfile=./target/aws-lambda-java-runtime-interface-client-$projectVersion.jar \
-Dfiles=$FILES \
-Dclassifiers=$CLASSIFIERS \
-Dtypes=$TYPES \
-DpomFile=pom.xml \
-DrepositoryId=ci-repo -Durl=$MAVEN_REPO_URL \
--settings ric-dev-environment/settings.xml

if [ -f pom.xml.versionsBackup ]; then
mv pom.xml.versionsBackup pom.xml
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

projectVersion=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)


# test uber jar
mvn -B -X -P ci-repo \
dependency:get \
-DremoteRepositories=ci-repo::::$MAVEN_REPO_URL \
-Dartifact=com.amazonaws:aws-lambda-java-runtime-interface-client:${projectVersion}-SNAPSHOT \
-Dtransitive=false \
--settings ric-dev-environment/settings.xml


PLATFORM_ARRAY=("linux-x86_64" "linux_musl-x86_64" "linux-aarch_64" "linux_musl-aarch_64")

for classifier in "${PLATFORM_ARRAY[@]}"; do
# Test platform specific jar
mvn -B -P ci-repo \
dependency:get \
-DremoteRepositories=ci-repo::::$MAVEN_REPO_URL \
-Dartifact=com.amazonaws:aws-lambda-java-runtime-interface-client:${projectVersion}-SNAPSHOT:jar:${classifier} \
-Dtransitive=false \
--settings ric-dev-environment/settings.xml
done
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class ClasspathLoader {
private static final int CLASS_SUFFIX_LEN = ".class".length();

static {
// NativeClient loads a native library and crashes if loaded here so just exclude it
BLOCKLIST.add("com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient");
// Ignore module info class for serialization lib
BLOCKLIST.add("META-INF.versions.9.module-info");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -39,6 +37,7 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import static com.amazonaws.services.lambda.runtime.api.client.UserFault.*;
Expand Down Expand Up @@ -81,12 +80,10 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type
}
}
// else platform dependent (Android uses GSON but all other platforms use Jackson)
switch (platform) {
case ANDROID:
return GsonFactory.getInstance().getSerializer(type);
default:
return JacksonFactory.getInstance().getSerializer(type);
if (Objects.requireNonNull(platform) == Platform.ANDROID) {
return GsonFactory.getInstance().getSerializer(type);
}
return JacksonFactory.getInstance().getSerializer(type);
}

private static PojoSerializer<Object> getSerializerCached(Platform platform, Type type) {
Expand Down Expand Up @@ -710,14 +707,14 @@ private static Optional<LambdaRequestHandler> getHandlerFromOverload(Class<?> cl
}
}

private static final boolean isContext(Type t) {
private static boolean isContext(Type t) {
return Context.class.equals(t);
}

/**
* Returns true if the last type in params is a lambda context object interface (Context).
*/
private static final boolean lastParameterIsContext(Class<?>[] params) {
private static boolean lastParameterIsContext(Class<?>[] params) {
return params.length != 0 && isContext(params[params.length - 1]);
}

Expand Down Expand Up @@ -832,21 +829,6 @@ private static LambdaRequestHandler wrapPojoHandler(RequestHandler instance, Typ
));
}

private static String exceptionToString(Throwable t) {
StringWriter writer = new StringWriter(65536);
try (PrintWriter wrapped = new PrintWriter(writer)) {
t.printStackTrace(wrapped);
}
StringBuffer buffer = writer.getBuffer();
if (buffer.length() > 262144) {
final String extra = " Truncated by Lambda";
buffer.delete(262144, buffer.length());
buffer.append(extra);
}

return buffer.toString();
}

private static LambdaRequestHandler wrapRequestStreamHandler(final RequestStreamHandler handler) {
return new LambdaRequestHandler() {
private final ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,61 @@ public class InvocationRequest {
*/
private String cognitoIdentity;

/**
* An input stream of the invocation's request body.
*/
private InputStream stream;

private byte[] content;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getXrayTraceId() {
return xrayTraceId;
}

public void setXrayTraceId(String xrayTraceId) {
this.xrayTraceId = xrayTraceId;
}

public String getInvokedFunctionArn() {
return invokedFunctionArn;
}

public void setInvokedFunctionArn(String invokedFunctionArn) {
this.invokedFunctionArn = invokedFunctionArn;
}

public long getDeadlineTimeInMs() {
return deadlineTimeInMs;
}

public void setDeadlineTimeInMs(long deadlineTimeInMs) {
this.deadlineTimeInMs = deadlineTimeInMs;
}

public String getClientContext() {
return clientContext;
}

public void setClientContext(String clientContext) {
this.clientContext = clientContext;
}

public String getCognitoIdentity() {
return cognitoIdentity;
}

public void setCognitoIdentity(String cognitoIdentity) {
this.cognitoIdentity = cognitoIdentity;
}

public InputStream getContentAsStream() {
return new ByteArrayInputStream(content);
}

public void setContent(byte[] content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public LambdaRuntimeClient(String hostnamePort) {
this.hostname = parts[0];
this.port = Integer.parseInt(parts[1]);
this.invocationEndpoint = invocationEndpoint();
NativeClient.init();
}

public InvocationRequest waitForNextInvocation() {
Expand Down

0 comments on commit f9abdf5

Please sign in to comment.