From fcbe93c43a2b16cb3c0f7a8176a47fd1b2975267 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 7 Sep 2022 10:25:18 +1000 Subject: [PATCH 1/3] Update Dockerfile-11 and pom.xml --- jvm-jersey/Dockerfile-11 | 2 +- jvm-jersey/pom.xml | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/jvm-jersey/Dockerfile-11 b/jvm-jersey/Dockerfile-11 index 37353798..1483b499 100644 --- a/jvm-jersey/Dockerfile-11 +++ b/jvm-jersey/Dockerfile-11 @@ -11,5 +11,5 @@ RUN mvn package FROM openjdk:11-jre COPY --from=BUILD /usr/src/myapp/target/env-jvm-jersey-0.0.1.jar /app.jar -ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar --server.port=8888 +ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar 8888 EXPOSE 8888 diff --git a/jvm-jersey/pom.xml b/jvm-jersey/pom.xml index f0dd2833..9f65a8d9 100644 --- a/jvm-jersey/pom.xml +++ b/jvm-jersey/pom.xml @@ -38,6 +38,11 @@ jersey-media-json-jackson 2.34 + + org.glassfish.jersey.inject + jersey-hk2 + 2.34 + javax.xml jaxb-api From f0fbac262aa7bdfdf18bcadd2a5a1080702d8c0e Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Wed, 7 Sep 2022 18:44:54 +1000 Subject: [PATCH 2/3] Update deserialiser to ignore unknown properties --- jvm-jersey/Dockerfile-11 | 2 +- jvm-jersey/pom.xml | 17 ++++++----------- .../java/io/fission/FunctionLoadRequest.java | 2 +- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/jvm-jersey/Dockerfile-11 b/jvm-jersey/Dockerfile-11 index 1483b499..6704b5a4 100644 --- a/jvm-jersey/Dockerfile-11 +++ b/jvm-jersey/Dockerfile-11 @@ -10,6 +10,6 @@ COPY src /usr/src/myapp/src/ RUN mvn package FROM openjdk:11-jre -COPY --from=BUILD /usr/src/myapp/target/env-jvm-jersey-0.0.1.jar /app.jar +COPY --from=BUILD /usr/src/myapp/target/env-jvm-jersey-0.0.2.jar /app.jar ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar 8888 EXPOSE 8888 diff --git a/jvm-jersey/pom.xml b/jvm-jersey/pom.xml index 9f65a8d9..aff4ee12 100644 --- a/jvm-jersey/pom.xml +++ b/jvm-jersey/pom.xml @@ -5,11 +5,11 @@ io.fission env-jvm-jersey - 0.0.1 + 0.0.2 - 1.6 - 1.6 + 1.8 + 1.8 @@ -39,20 +39,15 @@ 2.34 - org.glassfish.jersey.inject - jersey-hk2 - 2.34 + org.glassfish.jersey.inject + jersey-hk2 + 2.34 javax.xml jaxb-api 2.1 - - org.codehaus.jackson - jackson-core-asl - 1.9.13 - diff --git a/jvm-jersey/src/main/java/io/fission/FunctionLoadRequest.java b/jvm-jersey/src/main/java/io/fission/FunctionLoadRequest.java index 02d3e65a..c17f92fd 100644 --- a/jvm-jersey/src/main/java/io/fission/FunctionLoadRequest.java +++ b/jvm-jersey/src/main/java/io/fission/FunctionLoadRequest.java @@ -1,6 +1,6 @@ package io.fission; -import org.codehaus.jackson.annotate.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown = true) public class FunctionLoadRequest { From ed9b2568ef21cda8905310551bdfec777496290a Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 13 Sep 2022 13:44:13 +1000 Subject: [PATCH 3/3] Include java 11 version of spring boot JVM loader --- jvm-11/Dockerfile | 16 ++++++ jvm-11/Makefile | 6 ++ jvm-11/README.md | 70 ++++++++++++++++++++++++ jvm/Dockerfile-11 | 16 ++++++ jvm/pom.xml | 9 +-- jvm/src/main/java/io/fission/Server.java | 22 ++------ 6 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 jvm-11/Dockerfile create mode 100644 jvm-11/Makefile create mode 100644 jvm-11/README.md create mode 100644 jvm/Dockerfile-11 diff --git a/jvm-11/Dockerfile b/jvm-11/Dockerfile new file mode 100644 index 00000000..9fec881a --- /dev/null +++ b/jvm-11/Dockerfile @@ -0,0 +1,16 @@ +FROM maven:3.6-jdk-11 as BUILD +WORKDIR /usr/src/myapp/ + +# To reuse the build cache, here we split maven dependency +# download and package into two RUN commands to avoid cache invalidation. +COPY pom.xml . +RUN mvn dependency:go-offline + +COPY src /usr/src/myapp/src/ +RUN mvn package + +FROM openjdk:11-jre +VOLUME /tmp +COPY --from=BUILD /usr/src/myapp/target/env-java-0.0.2-SNAPSHOT.jar /app.jar +ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar --server.port=8888 +EXPOSE 8888 diff --git a/jvm-11/Makefile b/jvm-11/Makefile new file mode 100644 index 00000000..c8350993 --- /dev/null +++ b/jvm-11/Makefile @@ -0,0 +1,6 @@ +-include ../rules.mk + +.PHONY: all +all: jvm-builder jvm-env-img + +jvm-env-img: Dockerfile \ No newline at end of file diff --git a/jvm-11/README.md b/jvm-11/README.md new file mode 100644 index 00000000..96b0cdea --- /dev/null +++ b/jvm-11/README.md @@ -0,0 +1,70 @@ +# Fission: Java and JVM Environment + +This is the JVM environment for Fission. + +It's a Docker image containing a OpenJDK8 runtime, along with a +dynamic loader. A few dependencies are included in the +pom.xml file. + +Looking for ready-to-run examples? See the [JVM examples directory](../../examples/jvm). + +## Customizing this image + +To add package dependencies, edit pom.xml to add what you +need, and rebuild this image (instructions below). + +## Rebuilding and pushing the image + +You'll need access to a Docker registry to push the image: you can +sign up for Docker hub at hub.docker.com, or use registries from +gcr.io, quay.io, etc. Let's assume you're using a docker hub account +called USER. Build and push the image to the the registry: + +``` + docker build -t USER/jvm-env . && docker push USER/jvm-env +``` + +## Using the image in fission + +You can add this customized image to fission with "fission env +create": + +``` + fission env create --name jvm --image USER/jvm-env +``` + +Or, if you already have an environment, you can update its image: + +``` + fission env update --name jvm --image USER/jvm-env +``` + +After this, fission functions that have the env parameter set to the +same environment name as this command will use this environment. + +## Web Server Framework + +JVM environment uses Tomcat HTTP server by default as it is included in spring web. You can choose to use jetty or undertow by changing the dependency in pom.xml file as shown below. + +``` + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + org.springframework.boot + spring-boot-starter-jetty + +``` + +## Java and JVM builder + +JVM environment builder is based on OpenJDK8 and Maven 3.5.4 version. The default build command runs `mvn clean package` and uses the target/*with-dependencies.jar file for function. The default build command can be overridden as long as the uber jar file is copied to ${DEPLOY_PKG}. diff --git a/jvm/Dockerfile-11 b/jvm/Dockerfile-11 new file mode 100644 index 00000000..9fec881a --- /dev/null +++ b/jvm/Dockerfile-11 @@ -0,0 +1,16 @@ +FROM maven:3.6-jdk-11 as BUILD +WORKDIR /usr/src/myapp/ + +# To reuse the build cache, here we split maven dependency +# download and package into two RUN commands to avoid cache invalidation. +COPY pom.xml . +RUN mvn dependency:go-offline + +COPY src /usr/src/myapp/src/ +RUN mvn package + +FROM openjdk:11-jre +VOLUME /tmp +COPY --from=BUILD /usr/src/myapp/target/env-java-0.0.2-SNAPSHOT.jar /app.jar +ENTRYPOINT java ${JVM_OPTS} -Djava.security.egd=file:/dev/./urandom -jar /app.jar --server.port=8888 +EXPOSE 8888 diff --git a/jvm/pom.xml b/jvm/pom.xml index 458404fc..9d3e9c5f 100644 --- a/jvm/pom.xml +++ b/jvm/pom.xml @@ -6,12 +6,12 @@ io.fission env-java - 0.0.1-SNAPSHOT + 0.0.2-SNAPSHOT org.springframework.boot spring-boot-starter-parent - 2.0.1.RELEASE + 2.7.3 @@ -24,12 +24,7 @@ fission-java-core 0.0.2-SNAPSHOT - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - - diff --git a/jvm/src/main/java/io/fission/Server.java b/jvm/src/main/java/io/fission/Server.java index 3d752279..6afb0fff 100644 --- a/jvm/src/main/java/io/fission/Server.java +++ b/jvm/src/main/java/io/fission/Server.java @@ -63,7 +63,8 @@ ResponseEntity specialize(@RequestBody FunctionLoadRequest req) { // TODO Check if the classloading can be improved for ex. use something like: // Thread.currentThread().setContextClassLoader(cl); - if (this.getClass().getClassLoader() == null) { + URLClassLoader currentLoader = (URLClassLoader) this.getClass().getClassLoader(); + if (currentLoader == null ) { // this.getClass().getClassLoader() == null) { cl = URLClassLoader.newInstance(urls); } else { cl = URLClassLoader.newInstance(urls, this.getClass().getClassLoader()); @@ -72,21 +73,7 @@ ResponseEntity specialize(@RequestBody FunctionLoadRequest req) { if (cl == null) { return ResponseEntity.status(500).body("Failed to initialize the classloader"); } - - // Load all dependent classes from libraries etc. - while (e.hasMoreElements()) { - JarEntry je = e.nextElement(); - if (je.isDirectory() || !je.getName().endsWith(".class")) { - continue; - } - String className = je.getName().substring(0, je.getName().length() - CLASS_LENGTH); - className = className.replace('/', '.'); - cl.loadClass(className); - } - - // Instantiate the function class - fn = (Function) cl.loadClass(entryPoint).newInstance(); - + fn = (Function)Class.forName(entryPoint, true, cl).newInstance(); } catch (MalformedURLException e) { e.printStackTrace(); return ResponseEntity.badRequest().body("Error loading the Function class file"); @@ -102,6 +89,9 @@ ResponseEntity specialize(@RequestBody FunctionLoadRequest req) { } catch (IOException e) { e.printStackTrace(); return ResponseEntity.badRequest().body("Error reading the JAR file"); + } catch (NoClassDefFoundError e) { + e.printStackTrace(); + return ResponseEntity.badRequest().body("Class Dependency Missing"); } finally { try { // cl.close();