Download - Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Transcript
Page 1: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Zero Turnaround in Java

Watching the logs roll by…

Jevgeni Kabanov

Founder of ZeroTurnaround

Aranea and Squill Project Co-Founder

Speaker, Scientist, Engineer, Entrepreneur,…

Page 2: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Turnaround cycle

Make a change

Build, deploy,

wait

Check the

change

Page 3: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

DEMO: SPRING PETCLINIC

TURNAROUND

Page 4: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Outline

Turnaround – Why should you care?

Trimming Builds

Reloading Java Code with Class Loaders

HotSwap, JavaRebel and Beyond

Page 5: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

TURNAROUND – WHY

SHOULD YOU CARE?

Page 6: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Turnaround Cost

• Average turnaround is about 1 minute long

• Done about 5 times an hour

From over 15 projects and 150 people

• 8.3% of total coding time (1*5/60)

• 30 minutes a day (from 6 hours of coding a day)

• 2.5 hours a week

• Almost 3 work weeks a year

This sums up to

Page 7: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Working Memory

Programming is an exercise

of the working (short-term)

memory that holds the

current context

Questions:

How fast do you lose that

context?

How much time does context

recovery take?

Page 8: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Working Memory

52%

41%

24%

12%9%

6%

0%

10%

20%

30%

40%

50%

60%

3 6 9 12 15 18

Working memory degradation per second

Source: L. Peterson and M. Peterson “Short-Term Retention of Individual

Verbal Items.” Journal of Experimental Psychology, 1959.

Page 9: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Recovery time

The recovery time after a phone call is at least 15

minutes.

– Interrupts: Just a Minute Never Is, IEEE Software, 1998

The time it takes the employees to recover from an email

interrupt was found to be on average 64 seconds.

– Case Study: Evaluating the Effect of Email Interruptions within

the Workplace, EASE 2002

The recovery time for an instant message was estimated

to be between 11 and 25 seconds

– Instant Messaging Implications in the Transition from a Private

Consumer Activity to a Communication Tool for Business,

Software Quality Management, 2004

Page 10: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Some Conclusions

1. With the recovery time considered,

turnaround can easily cost more than

15% of coding time.

• ~ 4.5 hours a week, 5 work weeks a year

2. Every second counts! There is a

significant difference between a minute,

30, 15, 5 and 1 second pause!

Page 11: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Frustration

1. a user experiences a greater increase in anxiety when

a peripheral task interrupts her primary task than when

it does not

2. a user perceives an interrupted task to be

more difficult to complete than a non-interrupted task

– The Effects of Interruptions on Task Performance, Annoyance,

and Anxiety in the User Interface, IEEE Computer, 2006

Many programmers appear to be continually

frustrated in attempts to work. The so-called

"work -day" is made up largely of frustration time.

– Programmer performance and the effects of the workplace,

ICSE 1985

Page 12: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Worker’s IQ falls 10 points when distracted.

This drop in IQ is more than double the

drop seen after smoking marijuana. IQ

drop of 10 points is equivalent to missing

an entire night of sleep– Hewlett-Packard Press Release, Abuse of

technology can reduce UK workers' intelligence,

2005

Page 13: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

TRIMMING BUILDS

Page 14: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

A typical web application build

Package everything in a WAR/EAR

Package modules in JARs

Compile classes

Copy static resources

Resolve dependencies

Page 15: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Exploded layout

The project layout

exactly follows the

deployment layout

All resources are

edited in-place

without copying

Page 16: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Automatic building

Classes should be

compiled

automatically by the

IDE

The output should be

set directly to

WEB-INF/classes or

similar

Page 17: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Deployment by linking

The project is

deployed by

either pointing

the container

to it or creating

a symbolic link

in the

deployment

directory

• ln -s

• Symlinks can point to any file

Linux symbolic links

• Sysinternals junction utility on NTFS partitions

• Can only link to local directories and must be careful when deleting

Windows symbolic links

Page 18: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

A typical web application build

Package everything in a WAR/EAR

Package modules in JARs

Compile classes

Copy static resources

Resolve dependencies

Page 19: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Bootstrapping Builds

Can’t always use exploded layout

Instead:

Build the WAR/EAR

Unzip it to a temp directory

Remove some of the folders/jars and symlink them to the project

folders

Set the project to build automatically

Easy to automate with a bootstrapping script

Save on copying resources and packaging classes

Page 20: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

RELOADING CODE

Page 21: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Reloading Code

Objects & Class Loaders

Deployment, OSGi & etc

JVM Dynamic languages

Page 22: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Reloading an Object

MyObject

MyObject.class

OldClassLoader NewClassLoader

MyObject.class

MyObjectRecreate the object

Page 23: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Twin Class Loader

JVM

Classes

Libraries

OldClassLoader NewClassLoader

Objects

and Code

Classes

Libraries

Objects

and Code

Page 24: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Twin Class Issues

• instanceof returns false

• Casting throws an exception

New objects are not instances of

old classes

• Can get an IllegalAccessExceptionwhen calling a perfectly legal method

New classes are not members of

the old packages

• If you hold a reference to any object in the old classloader you will hold all old classes (including their static fields)

Memory leaks are easy

Page 25: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Web Deployment

Classes

Libraries

OldClassLoader NewClassLoader

Sevlet New

Classes

New

Libraries

Sevlet

Session Session

init()

App

State

App

State

Serialize/deserialize

Page 26: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Web Deployment

• Every deployed application gets a dedicated class loader

Class loader scope

• Application state is recovered by reinitialization

• Session state is (optionally) serialized and deserialized in the new class loader

State recreation

• Application reinitialization time, typically around one minute

Reloading time

• Leaks memory

• Lazy caches need to be warmed up every timeProblems

Page 27: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

OSGi

Frameworks that implement the OSGi standard

provide an environment for the modularization of

applications into smaller bundles. [Wikipedia]

Page 28: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

OSGi Redeployment

Classes

Libraries

OldClassLoader NewClassLoader

Bundle New

Classes

New

Libraries

Bundle

start()

Module

State

Module

State

Page 29: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

OSGi

• Dedicated class loader per application moduleClass loader scope

• Module state is recovered by reinitializationState recreation

• Module reinitialization time, usually less than whole application reinitializationReloading time

• Applications must be designed with OSGi in mind

• Overhead interface definitions

• Module export interfaces cannot be changed without redeploying the application

Problems

Page 30: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Fine-grained Class Loaders

Wrap a class loader around components

Tapestry 5

RIFE

Very fast reloading

Few classes at a time

Components managed by the framework are usually easy to

recreate

Page 31: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Old Component

ClassLoader

New Component

ClassLoader

Class Object

Component State

New

Class

New

Object

Page 32: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Fine-grained Class Loaders

• Class loader per component/serviceClass loader

scope

• State restored by framework (component/service recreated)

State recreation

• (Almost) InstantReloading time

• Only managed components can be reloaded

• Managed components referring unmanaged code can be a problem (twin class issues)

Problems

Page 33: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Some Conclusions

Recreating the state is the breaking point of reloading a

class

Coarse-grained class loaders take too much time to

recreate the state

Fine-grained class loaders exhibit the twin class

problem and are not universally applicable

Both are useful, but only partial solutions to the zero

turnaround problem

Page 34: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Dynamic Languages

Class-based

languages have same

limitations as Java

Groovy

Jython

Non-class based

languages can have

better support

JRuby

Clojure

Page 35: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

HOTSWAP AND JAVAREBEL

Page 36: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

HotSwap

MyObject

MyObject.class

OldClassLoader

Code

101000101

100010010

Debugger

HotSwap

New Code

111000100

101010010

New Code

111000100

101010010

User saves class

from IDE

Page 37: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

HotSwap

Updates classes and objects

• Almost instantly

• Can be attached remotely

Very limited

• Only updates method bodies, no new fields, methods or classes

• Needs a debugger session running, slow and prone to error

Page 38: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel Approach

JVM

Reloading “Interpreter”

JavaRebel

Agent

Classes Libraries

ClassLoader ClassLoader ClassLoader

Objects and Code

Page 39: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel

MyObject

MyObject.class

OldClassLoader

Code

101000101

100010010 New Code

111000100

101010010

New Code

111000100

101010010

JavaRebel

agent

MyObject.class file

changed

Page 40: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel Features

HotSwap JavaRebel

Changing method bodies + +

Adding/removing methods - +

Adding/removing constructors - +

Adding/removing fields - +

Adding/removing classes - +

Adding/removing annotations - +

Replacing superclass - -

Adding/removing

implemented interfaces- -

Page 41: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel Installation

-noverify -javaagent:/path/to/javarebel.jar

Enables the JavaRebel agent

All *.class files in the classpath will be monitored for changes

automatically

(Optional) -Drebel.dirs=folder1,folder2,…

Specifies IDE output folders or just class folders

Can deploy a WAR/EAR and still get instant updates to code

Page 42: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

DEMO: PETCLINIC WITH

JAVAREBEL

Page 43: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel

Just works

• No configuration necessary!

• Runs on all JVMs starting with 1.4

• Supports all major containers

• Supports standalone Java applications and OSGi

Seamlessly

• Changes are visible in reflection

• Serialization works as usual

• Dynamic proxies work as usual

Page 44: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel

Commercial tool, free

30 day trial

No free/open source

analogs

Get it from:www.zeroturnaround.com

or just google “javarebel”

Personal license:

Commercial license:

~ $10

Page 45: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel History

JavaRebel 1.0 released in December, 2007

Today over 10 000 licensed users

Some of our customers:

LinkedIn

Turner

Roche

Logica

Disney.com

Page 46: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

AND BEYOND

Page 47: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Classes

JavaRebel

MyObject

MyObject.class

OldClassLoader

Code

101000101

100010010 New Code

111000100

101010010

New Code

111000100

101010010

JavaRebel

agent

MyObject.class file

changed

Configuration

(XML, annotations, …)

Fra

mew

ork

Configuration changed

Page 48: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Types of Configuration

• EJB 2.0/3.0

• Spring

• Guice

Service Glue

• Struts 1.0/2.0

• Stripes

• Spring MVC

Web Controller

• Hibernate

• TopLink

• JPAORM

Page 49: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel Plugins

• Plugins are found and started from classpath

• Javassist support allows patching framework classes

• API to react on class reloads

Open Source JavaRebel SDK

• Adding/removing beans dependencies via setters/fields

• Adding new beans via XML or annotations

• Adding new MVC Controllers and Handlers

Spring Plugin

Page 50: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

DEMO: PETCLINIC WITH

JAVAREBEL SPRING PLUGIN

Page 51: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

JavaRebel 2.0

• Available: Spring, Guice, Struts 2, Tapestry 4

• Coming: Stripes, Wicket, Struts 1, …

Embedded plugins

• All the benefits of exploded development with unexploded one

• Automatically maps propagates class and resource updates to the deployed application

• Will need some user help to configure

Virtual Classpath

• Instant automatic production server updates and rollbacks with a press of a button

• Tools for update verification

Production support

Page 52: Zero Turnaround in Java - Jfokus · 2019-09-11 · JVM Reloading “Interpreter ... Builds should be as slim as possible, symlink is your best friend Code reloading is a complicated

Take Away

Every next second spent on turnaround costs more!

Builds should be as slim as possible, symlink is your

best friend

Code reloading is a complicated problem with

HotSwap, OSGi and framework support being the best

partial solutions available for free

JavaRebel solves the turnaround problem for peanuts :)