Want to do the impossible with Java? Lets do it!

Preview:

DESCRIPTION

Nigel Daniels presentation at JAXConf Santa Clara 2013 Want the impossible from Java? Lets do it! Love Java but find the JVM doesn’t let you do what you want, or maybe you have never considered what could be done if you could change the machine itself? After all doing that means writing a custom JVM, right? That was true, until now!

Citation preview

Nigel Daniels | Waratek Ltd.

Want to do the impossible with Java? Let‘s do it!

Waratek Ltd © 2013

Nigel Daniels• Wide range of tech roles including robotics,

process control, network management, 3D visualization, media and enterprise software.

• 8 years with IBM developing, delivering and managing WebSphere related products.

• Now freelance consulting on software product management and delivery.

• Consulting Product Manager with Waratek for 1½ years.

Introduction

Waratek Ltd © 2013

Java Virtualization Interface

• A pure-Java hypervisor inside the JVM.• Makes JVM multitenant, creates Java Virtual

Containers (JVCs).• Simple example:

• chroot for a JVC, written in pure-Java.• Complex example:

• Virtualise webapps running on Apache Tomcat et al.• Currently on Waratek’s CloudVM for Java.• V 1.0 – OSS License, includes the virtualization

driver for Apache Tomcat.

Introduction

Waratek Ltd © 2013

What will we cover?

• Java virtualization, an introduction• Key concepts• Object != Object• Exchanging objects• Creating virtual environments

• API overview• Demo

Introduction

Waratek Ltd © 2013

Java virtualization,an introduction

Waratek Ltd © 2013

Java virtualization, an introduction

Host Hardware

Hypervisor

Guest OS

Type 1 virtualization

Hardware

Host OS

Hypervisor

Guest OS

Type 2 virtualization

Waratek Ltd © 2013

Java virtualization, an introduction

Hypervisor

Host Hardware

Hypervisor(Virtual Machine Monitor)

Is it sensitive?

No

Emulate instruction

Yes

Trap x86instruction

Call

Guest OS

Waratek Ltd © 2013

Java virtualization, an introduction

Trap-and-emulating x86 instructions doesn’t work for Java, all JVM instructions are non-sensitive.

But what about a call like:

java.lang.System.exit(int)

JavaEE and every other Java application framework uses co-operative multi-tenancy…

So how do we provide fully isolated virtualization for Java?

but the last time co-operative multi-tenancy was used was in Windows 3.1 and Mac OS 9 in the 1990’s!

Waratek Ltd © 2013

Java virtualization, an introduction

Host JVM

Guest Application (JVC)

Hypervisor

JVM Hypervisor(Java Virtual Machine Monitor)

Is type virtualized?

No

Emulate type

accessYes

Exit transition

Call

With a type-1 virtual machine monitor for the JVM

All in pure Java!

Waratek Ltd © 2013

Key concepts

Waratek Ltd © 2013

Object != Object

Waratek Ltd © 2013

Key concepts, Object != Object

Host JVM

Hypervisor

DOM-0

JVC-1 JVC-2 JVC-3

Waratek Ltd © 2013

Key concepts, Object != Object

Host JVM

Hypervisor

DOM-0

JVC-1 JVC-2 JVC-3

java.util.HashMap java.util.HashMap java.util.HashMap

java.util.HashMap

Waratek Ltd © 2013

Key concepts, Object != Object

Host JVM

Hypervisor

DOM-0

JVC-1 JVC-2 JVC-3

Real BCL

Virtual BCL-A Virtual BCL-B Virtual BCL-C

Waratek Ltd © 2013

Key concepts, Object != Object

Host JVM

Hypervisor

DOM-0

JVC-1 JVC-2 JVC-3

Real BCL

Virtual BCL-A Virtual BCL-B Virtual BCL-C

define()

java.util.HashMap

JIT emitted byte[]

HashMap

define()

HashMap′

define()

HashMap′′

define()

HashMap′′′

Waratek Ltd © 2013

Exchanging objects

Waratek Ltd © 2013

Hypervisor

JVMM-1 JVMM-2 JVMM-3

JVMM-0

Key concepts, exchanging objects

Host JVM

DOM-0

JVC-1 JVC-2 JVC-3

RBCL

VBCL-A VBCL-B VBCL-C

Waratek Ltd © 2013

Hypervisor

JVMM-1 JVMM-2 JVMM-3

JVMM-0

Key concepts, exchanging objects

Host JVM

DOM-0

JVC-1 JVC-2 JVC-3

exit()

ObjectA

ObjectB

Waratek Ltd © 2013

Hypervisor

JVMM-1 JVMM-2 JVMM-3

JVMM-0

Key concepts, exchanging objects

Host JVM

DOM-0

JVC-1 JVC-2 JVC-3

entry()

ObjectC

ObjectB

Waratek Ltd © 2013

Hypervisor

JVMM-1 JVMM-2 JVMM-3

JVMM-0

Key concepts, exchanging objects

Host JVM

DOM-0

JVC-1 JVC-2 JVC-3

entry()

ObjectCObjectA

exit()

Waratek Ltd © 2013

DOM-0

Key concepts, exchanging objects

JVC-1 JVC-2

Waratek Ltd © 2013

Creating virtual environments

Waratek Ltd © 2013

Hypervisor

JVMM-1 JVMM-2 JVMM-3

JVMM-0

Key concepts, creating virtual environments

Host JVM

DOM-0

JVC-1 JVC-2 JVC-3

HostEnv-0

GuestEnv-1 GuestEnv-2 GuestEnv-3

Waratek Ltd © 2013

JVMM-1

Key concepts, creating virtual environments

JVC-1

GuestEnv-1

MonitorEnv

RuntimeSystemEnv

FileSystemEnvNetworkSystemEn

v

Waratek Ltd © 2013

Java Virtualization Interface API

Waratek Ltd © 2013

Java Virtualization Interface API

Waratek Ltd © 2013

Java Virtualization Interface API

Entry Point

Waratek Ltd © 2013

package org.apache.catalina.startup;…public class BootstrapEnv extends HostEnv

When the hypervisor starts a particular sequence is used to determine if a driver is to be loaded:

1. Is the class provided an instance of HostEnv?2. Else append “Env” to the fully qualified class name. Is there a

matching class?3. Else call the standard main entry point for the class.

The JVI drivers entry point replaces the standard call to public static void main (String[] args).

JVI API – BootstrapEnv

Waratek Ltd © 2013

public void start( String[] args, Class mainClass ) { // Step 1) tell the world we're loading… … // Step 2) load our managed entry transitions. … // Step 3) start Tomcat proper super.start( args, mainClass ); }

JVI API – BootstrapEnv

Waratek Ltd © 2013

public ClassLoaderEnv virtualize( final ClassLoader cl ) { // Load our privileged types … // Is this class loader a child of a virtualized // class loader? … else if( cl.getClass().getName().equals( "org.apache.catalina.loader.WebappClassLoader")) { // This is a webapp class loader, virtualise this! … tomcat_driver.ServletMonitorEnv me = new tomcat_driver.ServletMonitorEnv(this, name, cl); ClassLoaderEnv webappClassLoaderEnv = me.startJVC(); … return webappClassLoaderEnv; } … }

JVI API – BootstrapEnv

Waratek Ltd © 2013

DOM-0 typespace

JVC-1 typespace

VCL

RCL

Stub Class

Java Virtualization Interface API

Waratek Ltd © 2013

Java Virtualization Interface API

JVC Entry Point

NB. This may be renamed marshalling on release

Waratek Ltd © 2013

public ClassLoaderEnv startJVC() { // Load our managed exit transitions … // Start the JVC super.start(); // Setup the virtualized web app class loader environment … return webappClassLoaderEnv; }

JVI API – MonitorEnv

Waratek Ltd © 2013

DOM-0 typespace

JVC-1 typespace

Java Virtualization Interface API

Waratek Ltd © 2013

public ClassLoaderEnv virtualize( final ClassLoader cl ) { // Load our privileged types if ( privTypes == null &&cl.getClass().getName().equals("org.apache.catalina.loader.StandardClassLoader”)) { privTypes = new TypeSet( cl ); try { privTypes.loadPackages( new String[]{ "javax.servlet.", "javax.el." }, true, new String[] { "javax.servlet.jsp.el.ImplicitObjectELResolver$Scope”}, ); privTypes.loadClass( "org.apache.tomcat.InstanceManager" ); privTypes.loadClass( "java.io.Writer" ); … // that’s all the classes, so seal it now. privTypes.seal(); } catch( ClassNotFoundException e ) { throw new EnvironmentError( e ); } }

JVI API – BootstrapEnv

Waratek Ltd © 2013

@MemberExit(type={ "javax/servlet/ServletRequest", "javax/servlet/ServletRequestWrapper" }, name="getLocale", descriptor="()Ljava/util/Locale;", conversion={ "java.util.Locale" } ) public static Locale getLocaleX( ServletRequest req ) { return req.getLocale(); }

Java Virtualization Interface API – Exit/Entry

As the hypervisor loads the JVI driver the following sequence takes place for each member:

1. Is the member a privileged type (in the intersection)?2. Is there a matching description of the member as a Member

Entry or Exit?3. Failing ether of these throw an error while loading the driver.

Here we can completely override the function without sub-classing, this gives us true polymorphism! We can make our Local anywhere.

Waratek Ltd © 2013

DEMO

Waratek Ltd © 2013

What have we covered?• Java virtualization is type 1 virtualization, and

is pure Java.• This requires typespace virtualization.• Virtual machine monitors marshal the types

and handle the transitions (unless the type is privileged).

• The API allows us entry points into the VMMs.• We can now extend and control the entire JVM

image!

Summary

Waratek Ltd © 2013

References

Waratek Ltd © 2013

Virtualisation references:https://en.wikipedia.org/wiki/Virtual_machine_monitorhttps://en.wikipedia.org/wiki/Popek_and_Goldberg_virtualization_requirementshttps://en.wikipedia.org/wiki/X86_virtualization

JVI an Introduction (Part I)http://www.waratek.com/blog/june-2013/open-source-api-for-java-virtualization

General information about the Waratek CloudVM:http://www.waratek.com

Elasticat (JVI implemented for TomCat):http://www.waratek.com/product/elasticat

JVI source code and Java doc (coming soon):https://github.com/waratek

Waratek Ltd © 2013

Thank You

nigel.daniels@waratek.com

Questions?