Skip to content

Commit f9abdf5

Browse files
authored
Prepare platform specific jar files (#429)
1 parent f6358f7 commit f9abdf5

File tree

15 files changed

+466
-116
lines changed

15 files changed

+466
-116
lines changed

aws-lambda-java-runtime-interface-client/Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@ pr: test test-smoke
4545
.PHONY: build
4646
build:
4747
mvn clean install
48+
mvn install -P linux-x86_64
49+
mvn install -P linux_musl-x86_64
50+
mvn install -P linux-aarch64
51+
mvn install -P linux_musl-aarch64
4852

4953
.PHONY: publish
5054
publish:
5155
./ric-dev-environment/publish_snapshot.sh
5256

57+
.PHONY: publish
58+
test-publish:
59+
./ric-dev-environment/test-platform-specific-jar-snapshot.sh
60+
5361
define HELP_MESSAGE
5462

5563
Usage: $ make [TARGETS]

aws-lambda-java-runtime-interface-client/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,33 @@ DOCKERHUB_PASSWORD=<dockerhub password>
151151
```
152152
Recommended way is to set the Docker Hub credentials in CodeBuild job by retrieving them from AWS Secrets Manager.
153153

154+
## Configuration
155+
The `aws-lambda-java-runtime-interface-client` JAR is a large uber jar, which contains compiled C libraries
156+
for x86_64 and aarch_64 for glibc and musl LIBC implementations. If the size is an issue, you can pick a smaller
157+
platform-specific JAR by setting the `<classifier>`.
158+
```
159+
<!-- Platform-specific Linux x86_64 JAR -->
160+
<dependency>
161+
<groupId>com.amazonaws</groupId>
162+
<artifactId>aws-lambda-java-runtime-interface-client</artifactId>
163+
<version>2.3.2</version>
164+
<classifier>linux-x86_64</classifier>
165+
</dependency>
166+
```
167+
168+
Available platform classifiers: `linux-x86_64`, `linux-aarch_64`, `linux_musl-aarch_64`, `linux_musl-x86_64`
169+
170+
The Lambda runtime interface client tries to load compatible library during execution, by unpacking it to a temporary
171+
location `/tmp/.libaws-lambda-jni.so`.
172+
If this behaviour is not desirable, it is possible to extract the `.so` files during build time and specify the location via
173+
`com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI` system property, like
174+
```
175+
ENTRYPOINT [ "/usr/bin/java",
176+
"-Dcom.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient.JNI=/function/libaws-lambda-jni.linux_x86_64.so"
177+
"-cp", "./*",
178+
"com.amazonaws.services.lambda.runtime.api.client.AWSLambda" ]
179+
```
180+
154181
## Security
155182

156183
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.

aws-lambda-java-runtime-interface-client/pom.xml

+42
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
separately from the Runtime Interface Client functionality until we figure something else out.
4545
-->
4646
<multiArch>true</multiArch>
47+
<target_build_os/>
48+
<target_build_arch/>
49+
<ric.classifier/>
4750
</properties>
4851

4952
<dependencies>
@@ -119,6 +122,8 @@
119122
failonerror="true" logError="true">
120123
<arg value="${project.build.directory}"/>
121124
<arg value="${multiArch}"/>
125+
<arg value="${target_build_os}"/>
126+
<arg value="${target_build_arch}"/>
122127
</exec>
123128
</target>
124129
</configuration>
@@ -147,6 +152,11 @@
147152
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
148153
</manifest>
149154
</archive>
155+
<classifier>${ric.classifier}</classifier>
156+
<includes>
157+
<include>com/</include>
158+
<include>jni/*${ric.classifier}.so</include>
159+
</includes>
150160
</configuration>
151161
</plugin>
152162
<plugin>
@@ -288,5 +298,37 @@
288298
</repository>
289299
</distributionManagement>
290300
</profile>
301+
<profile>
302+
<id>linux-x86_64</id>
303+
<properties>
304+
<target_build_os>linux</target_build_os>
305+
<target_build_arch>x86_64</target_build_arch>
306+
<ric.classifier>linux-x86_64</ric.classifier>
307+
</properties>
308+
</profile>
309+
<profile>
310+
<id>linux_musl-x86_64</id>
311+
<properties>
312+
<target_build_os>linux_musl</target_build_os>
313+
<target_build_arch>x86_64</target_build_arch>
314+
<ric.classifier>linux_musl-x86_64</ric.classifier>
315+
</properties>
316+
</profile>
317+
<profile>
318+
<id>linux-aarch64</id>
319+
<properties>
320+
<target_build_os>linux</target_build_os>
321+
<target_build_arch>aarch_64</target_build_arch>
322+
<ric.classifier>linux-aarch_64</ric.classifier>
323+
</properties>
324+
</profile>
325+
<profile>
326+
<id>linux_musl-aarch64</id>
327+
<properties>
328+
<target_build_os>linux_musl</target_build_os>
329+
<target_build_arch>aarch_64</target_build_arch>
330+
<ric.classifier>linux_musl-aarch_64</ric.classifier>
331+
</properties>
332+
</profile>
291333
</profiles>
292334
</project>

aws-lambda-java-runtime-interface-client/ric-dev-environment/publish_snapshot.sh

+31-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/bin/bash -x
22

33
set -e
44

@@ -18,5 +18,33 @@ else
1818
echo "Already -SNAPSHOT version"
1919
fi
2020

21-
mvn -P ci-repo deploy --settings ric-dev-environment/settings.xml
22-
mv pom.xml.versionsBackup pom.xml
21+
CLASSIFIERS_ARRAY=("linux-x86_64" "linux_musl-x86_64" "linux-aarch_64" "linux_musl-aarch_64")
22+
23+
for str in "${CLASSIFIERS_ARRAY[@]}"; do
24+
FILES="${FILES}target/aws-lambda-java-runtime-interface-client-$projectVersion-$str.jar,"
25+
CLASSIFIERS="${CLASSIFIERS}${str},"
26+
TYPES="${TYPES}jar,"
27+
done
28+
29+
# remove the last ","
30+
FILES=${FILES%?}
31+
CLASSIFIERS=${CLASSIFIERS%?}
32+
TYPES=${TYPES%?}
33+
34+
mvn -B -X -P ci-repo \
35+
deploy:deploy-file \
36+
-DgroupId=com.amazonaws \
37+
-DartifactId=aws-lambda-java-runtime-interface-client \
38+
-Dpackaging=jar \
39+
-Dversion=$projectVersion \
40+
-Dfile=./target/aws-lambda-java-runtime-interface-client-$projectVersion.jar \
41+
-Dfiles=$FILES \
42+
-Dclassifiers=$CLASSIFIERS \
43+
-Dtypes=$TYPES \
44+
-DpomFile=pom.xml \
45+
-DrepositoryId=ci-repo -Durl=$MAVEN_REPO_URL \
46+
--settings ric-dev-environment/settings.xml
47+
48+
if [ -f pom.xml.versionsBackup ]; then
49+
mv pom.xml.versionsBackup pom.xml
50+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
projectVersion=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
6+
7+
8+
# test uber jar
9+
mvn -B -X -P ci-repo \
10+
dependency:get \
11+
-DremoteRepositories=ci-repo::::$MAVEN_REPO_URL \
12+
-Dartifact=com.amazonaws:aws-lambda-java-runtime-interface-client:${projectVersion}-SNAPSHOT \
13+
-Dtransitive=false \
14+
--settings ric-dev-environment/settings.xml
15+
16+
17+
PLATFORM_ARRAY=("linux-x86_64" "linux_musl-x86_64" "linux-aarch_64" "linux_musl-aarch_64")
18+
19+
for classifier in "${PLATFORM_ARRAY[@]}"; do
20+
# Test platform specific jar
21+
mvn -B -P ci-repo \
22+
dependency:get \
23+
-DremoteRepositories=ci-repo::::$MAVEN_REPO_URL \
24+
-Dartifact=com.amazonaws:aws-lambda-java-runtime-interface-client:${projectVersion}-SNAPSHOT:jar:${classifier} \
25+
-Dtransitive=false \
26+
--settings ric-dev-environment/settings.xml
27+
done

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/ClasspathLoader.java

-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class ClasspathLoader {
2424
private static final int CLASS_SUFFIX_LEN = ".class".length();
2525

2626
static {
27-
// NativeClient loads a native library and crashes if loaded here so just exclude it
28-
BLOCKLIST.add("com.amazonaws.services.lambda.runtime.api.client.runtimeapi.NativeClient");
2927
// Ignore module info class for serialization lib
3028
BLOCKLIST.add("META-INF.versions.9.module-info");
3129
}

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/EventHandlerLoader.java

+6-24
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
27-
import java.io.PrintWriter;
28-
import java.io.StringWriter;
2927
import java.lang.reflect.Constructor;
3028
import java.lang.reflect.InvocationTargetException;
3129
import java.lang.reflect.Method;
@@ -39,6 +37,7 @@
3937
import java.util.HashMap;
4038
import java.util.LinkedList;
4139
import java.util.Map;
40+
import java.util.Objects;
4241
import java.util.Optional;
4342

4443
import static com.amazonaws.services.lambda.runtime.api.client.UserFault.*;
@@ -81,12 +80,10 @@ private static PojoSerializer<Object> getSerializer(Platform platform, Type type
8180
}
8281
}
8382
// else platform dependent (Android uses GSON but all other platforms use Jackson)
84-
switch (platform) {
85-
case ANDROID:
86-
return GsonFactory.getInstance().getSerializer(type);
87-
default:
88-
return JacksonFactory.getInstance().getSerializer(type);
83+
if (Objects.requireNonNull(platform) == Platform.ANDROID) {
84+
return GsonFactory.getInstance().getSerializer(type);
8985
}
86+
return JacksonFactory.getInstance().getSerializer(type);
9087
}
9188

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

713-
private static final boolean isContext(Type t) {
710+
private static boolean isContext(Type t) {
714711
return Context.class.equals(t);
715712
}
716713

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

@@ -832,21 +829,6 @@ private static LambdaRequestHandler wrapPojoHandler(RequestHandler instance, Typ
832829
));
833830
}
834831

835-
private static String exceptionToString(Throwable t) {
836-
StringWriter writer = new StringWriter(65536);
837-
try (PrintWriter wrapped = new PrintWriter(writer)) {
838-
t.printStackTrace(wrapped);
839-
}
840-
StringBuffer buffer = writer.getBuffer();
841-
if (buffer.length() > 262144) {
842-
final String extra = " Truncated by Lambda";
843-
buffer.delete(262144, buffer.length());
844-
buffer.append(extra);
845-
}
846-
847-
return buffer.toString();
848-
}
849-
850832
private static LambdaRequestHandler wrapRequestStreamHandler(final RequestStreamHandler handler) {
851833
return new LambdaRequestHandler() {
852834
private final ByteArrayOutputStream output = new ByteArrayOutputStream(1024);

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/InvocationRequest.java

+27-5
Original file line numberDiff line numberDiff line change
@@ -41,39 +41,61 @@ public class InvocationRequest {
4141
*/
4242
private String cognitoIdentity;
4343

44-
/**
45-
* An input stream of the invocation's request body.
46-
*/
47-
private InputStream stream;
48-
4944
private byte[] content;
5045

5146
public String getId() {
5247
return id;
5348
}
5449

50+
public void setId(String id) {
51+
this.id = id;
52+
}
53+
5554
public String getXrayTraceId() {
5655
return xrayTraceId;
5756
}
5857

58+
public void setXrayTraceId(String xrayTraceId) {
59+
this.xrayTraceId = xrayTraceId;
60+
}
61+
5962
public String getInvokedFunctionArn() {
6063
return invokedFunctionArn;
6164
}
6265

66+
public void setInvokedFunctionArn(String invokedFunctionArn) {
67+
this.invokedFunctionArn = invokedFunctionArn;
68+
}
69+
6370
public long getDeadlineTimeInMs() {
6471
return deadlineTimeInMs;
6572
}
6673

74+
public void setDeadlineTimeInMs(long deadlineTimeInMs) {
75+
this.deadlineTimeInMs = deadlineTimeInMs;
76+
}
77+
6778
public String getClientContext() {
6879
return clientContext;
6980
}
7081

82+
public void setClientContext(String clientContext) {
83+
this.clientContext = clientContext;
84+
}
85+
7186
public String getCognitoIdentity() {
7287
return cognitoIdentity;
7388
}
7489

90+
public void setCognitoIdentity(String cognitoIdentity) {
91+
this.cognitoIdentity = cognitoIdentity;
92+
}
93+
7594
public InputStream getContentAsStream() {
7695
return new ByteArrayInputStream(content);
7796
}
7897

98+
public void setContent(byte[] content) {
99+
this.content = content;
100+
}
79101
}

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/runtimeapi/LambdaRuntimeClient.java

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public LambdaRuntimeClient(String hostnamePort) {
3838
this.hostname = parts[0];
3939
this.port = Integer.parseInt(parts[1]);
4040
this.invocationEndpoint = invocationEndpoint();
41+
NativeClient.init();
4142
}
4243

4344
public InvocationRequest waitForNextInvocation() {

0 commit comments

Comments
 (0)