Java env alpha (#656)
This change adds support for JVM based environment for running Java functions. This is alpha release of JVM environment and might undergo changes.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
.springBeans
|
||||
.project
|
||||
.mvn
|
||||
.settings
|
||||
.classpath
|
||||
target/
|
||||
bin/
|
||||
@@ -0,0 +1,10 @@
|
||||
FROM maven:3.5-jdk-8 as BUILD
|
||||
COPY src /usr/src/myapp/src
|
||||
COPY pom.xml /usr/src/myapp
|
||||
RUN mvn -f /usr/src/myapp/pom.xml clean package
|
||||
|
||||
FROM openjdk:8-jdk-alpine
|
||||
VOLUME /tmp
|
||||
COPY --from=BUILD /usr/src/myapp/target/env-java-0.0.1-SNAPSHOT.jar /app.jar
|
||||
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar","--server.port=8888"]
|
||||
EXPOSE 8888
|
||||
@@ -0,0 +1,46 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>io.fission</groupId>
|
||||
<artifactId>env-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.fission</groupId>
|
||||
<artifactId>fission-java-core</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<!-- Adding Sonatype repository to pull snapshots -->
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>fission-java-core</id>
|
||||
<name>fission-java-core-snapshot</name>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</project>
|
||||
@@ -0,0 +1,32 @@
|
||||
package io.fission;
|
||||
|
||||
public class FunctionLoadRequest {
|
||||
|
||||
private String filepath;
|
||||
private String functionName;
|
||||
private String url;
|
||||
|
||||
String getFilepath() {
|
||||
return filepath;
|
||||
}
|
||||
|
||||
void setFilepath(String filepath) {
|
||||
this.filepath = filepath;
|
||||
}
|
||||
|
||||
String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getFunctionName() {
|
||||
return functionName;
|
||||
}
|
||||
|
||||
public void setFunctionName(String functionName) {
|
||||
this.functionName = functionName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package io.fission;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Enumeration;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.boot.*;
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
import org.springframework.http.RequestEntity;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import io.fission.Function;
|
||||
|
||||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
public class Server {
|
||||
|
||||
private Function fn;
|
||||
|
||||
private static final int CLASS_LENGTH = 6;
|
||||
|
||||
private static Logger logger = Logger.getGlobal();
|
||||
|
||||
@RequestMapping(value = "/", method = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
|
||||
RequestMethod.PUT })
|
||||
ResponseEntity<Object> home(RequestEntity<?> req) {
|
||||
if (fn == null) {
|
||||
return ResponseEntity.badRequest().body("Container not specialized");
|
||||
} else {
|
||||
return ((ResponseEntity<Object>) ((Function) fn).call(req, null));
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(path = "/v2/specialize", consumes = "application/json")
|
||||
ResponseEntity<String> specialize(@RequestBody FunctionLoadRequest req) {
|
||||
long startTime = System.nanoTime();
|
||||
File file = new File(req.getFilepath());
|
||||
if (!file.exists()) {
|
||||
return ResponseEntity.badRequest().body("/userfunc/user not found");
|
||||
}
|
||||
|
||||
String entryPoint = req.getFunctionName();
|
||||
logger.log(Level.INFO, "Entrypoint class:" + entryPoint);
|
||||
if (entryPoint == null) {
|
||||
return ResponseEntity.badRequest().body("Entrypoint class is missing in the function");
|
||||
}
|
||||
|
||||
JarFile jarFile = null;
|
||||
ClassLoader cl = null;
|
||||
try {
|
||||
|
||||
jarFile = new JarFile(file);
|
||||
Enumeration<JarEntry> e = jarFile.entries();
|
||||
URL[] urls = { new URL("jar:file:" + file + "!/") };
|
||||
|
||||
// TODO Check if the classloading can be improved for ex. use something like:
|
||||
// Thread.currentThread().setContextClassLoader(cl);
|
||||
if (this.getClass().getClassLoader() == null) {
|
||||
cl = URLClassLoader.newInstance(urls);
|
||||
} else {
|
||||
cl = URLClassLoader.newInstance(urls, this.getClass().getClassLoader());
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error loading the Function class file");
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error loading Function or dependent class");
|
||||
} catch (InstantiationException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error creating a new instance of function class");
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error creating a new instance of function class");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error reading the JAR file");
|
||||
} finally {
|
||||
try {
|
||||
// cl.close();
|
||||
jarFile.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return ResponseEntity.badRequest().body("Error closing the file while loading the class");
|
||||
}
|
||||
}
|
||||
long elapsedTime = System.nanoTime() - startTime;
|
||||
logger.log(Level.INFO, "Specialize call done in: " + elapsedTime / 1000000 + " ms");
|
||||
return ResponseEntity.ok("Done");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(Server.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user