Slide 1

Slide 1 text

©2020 VMware, Inc. Spring Native Applications Combining Spring and GraalVM native image technology Martin Lippert Spring Tools Lead, MAPBU, VMware November 2020 (based on work by Sébastien Deleuze, Andy Clement, and others)

Slide 2

Slide 2 text

©2020 VMware, Inc. https://graalvm.org/: High-performance polyglot VM Basic idea: • One virtual machine that can execute all languages (Java, JavaScript, R, Ruby, Python, C, C++, Kotlin, Scala, etc.) • You can run all those languages without any boundaries between them 2 What is GraalVM ?

Slide 3

Slide 3 text

©2020 VMware, Inc. 3 GraalVM overview Java Scala Kotlin JavaScript Ruby R Python C++ WebAssembly OpenJDK NodeJS Oracle Database MySQL Native GraalVM

Slide 4

Slide 4 text

©2020 VMware, Inc. 4 GraalVM overview Java Scala Kotlin JavaScript Ruby R Python C++ WebAssembly OpenJDK NodeJS Oracle Database MySQL Native GraalVM 4 GraalVM overview This piece of the project is our focus here

Slide 5

Slide 5 text

©2020 VMware, Inc. 5 What is GraalVM Native Image technology? static analysis AOT compilation + snapshotting intermediate model Your Java code JDK SubstrateVM Binary Compiled Code Initial Heap

Slide 6

Slide 6 text

©2020 VMware, Inc. 6 Two main benefits Reduced memory consumption Instant startup

Slide 7

Slide 7 text

©2020 VMware, Inc. 7 Two main benefits Reduced memory consumption Instant startup Scale to zero You only pay when your application is used Serverless for any kind of workload including regular web applications Good fit with platforms like Knative 3x to 5x memory reduction (RSS) Cheaper cloud instances Critical with systems split into multiple microservices

Slide 8

Slide 8 text

©2020 VMware, Inc. 8 Trace-offs 0 20 40 60 80 100 Startup Memory footprint Latency / Throughput Maturity Build JVM Native

Slide 9

Slide 9 text

©2020 VMware, Inc. • Static analysis of your application from the main entry point • Configuration required for: • Reflection • Proxies • Resources • Classpath fixed at build time • No class lazy loading • Some code will run at build time 9 Key differences between JVM and Native

Slide 10

Slide 10 text

©2020 VMware, Inc. 10 Two main editions GraalVM Community Edition GraalVM Enterprise Edition

Slide 11

Slide 11 text

©2020 VMware, Inc. GraalVM native is a source of inspiration for the JVM ecosystem 11

Slide 12

Slide 12 text

©2020 VMware, Inc. An upcoming Java platform standard with Project Leyden 12

Slide 13

Slide 13 text

©2020 VMware, Inc. Still in “early adopter” mode but matures quickly 13

Slide 14

Slide 14 text

©2020 VMware, Inc. Spring Native Applications The path towards natively compiled Spring applications 14

Slide 15

Slide 15 text

©2020 VMware, Inc. Good fit with Spring Boot stand-alone deployment model 15

Slide 16

Slide 16 text

©2020 VMware, Inc. Compiling your application to a native executable requires configuration (reflection/proxy/resources) 16

Slide 17

Slide 17 text

©2020 VMware, Inc. The Spring team is doing most of the hard work 17

Slide 18

Slide 18 text

©2020 VMware, Inc. Our goal is to support compilation of existing or new Spring Boot applications to native executables. Unchanged. 18

Slide 19

Slide 19 text

©2020 VMware, Inc. “We are excited about the great partnership between the Spring and GraalVM engineering teams to support native ahead-of-time compilation for millions of Spring Boot applications. This is a game changer enabling low memory footprint and instant startup for these workloads.” Thomas Wuerthinger, GraalVM founder & project lead 19 Collaboration between the Spring and GraalVM teams

Slide 20

Slide 20 text

©2020 VMware, Inc. 20 Leverage the Spring Boot container image support > mvn spring-boot:build-image or > gradle bootBuildImage

Slide 21

Slide 21 text

©2020 VMware, Inc. 21 Using Paketo Buildpacks…

Slide 22

Slide 22 text

©2020 VMware, Inc. 22 … to create lightweight container images

Slide 23

Slide 23 text

©2020 VMware, Inc. How to get started 23

Slide 24

Slide 24 text

©2020 VMware, Inc. 24

Slide 25

Slide 25 text

©2020 VMware, Inc. org.springframework.boot spring-boot-starter-parent 2.4.0-RC1 25 Make sure to use Spring Boot 2.4 latest milestone

Slide 26

Slide 26 text

©2020 VMware, Inc. @SpringBootApplication(proxyBeanMethods = false) public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } 26 Update configuration to avoid using proxies

Slide 27

Slide 27 text

©2020 VMware, Inc. org.springframework.boot spring-boot-maven-plugin paketobuildpacks/builder:tiny 1 -Dspring.native.remove-yaml-support=true -Dspring.spel.ignore=true 27 Configure Maven plugin

Slide 28

Slide 28 text

©2020 VMware, Inc. org.springframework.experimental spring-graalvm-native 0.8.2 28 Add the spring-graalvm-native dependency

Slide 29

Slide 29 text

©2020 VMware, Inc. > mvn spring-boot:build-image Successfully built image 'docker.io/library/demo:0.0.1- SNAPSHOT' Total time: 60 s 29 Build the native application • No need for a GraalVM native local install • Build happens in a container • And produces a container

Slide 30

Slide 30 text

©2020 VMware, Inc. > docker run -p 8080:8080 docker.io/library/demo:0.0.1-SNAPSHOT . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: Started application in 0.05 seconds (JVM running for 0.009) 30 Run the native application

Slide 31

Slide 31 text

©2020 VMware, Inc. You can also build a native executable without using containers 31

Slide 32

Slide 32 text

©2020 VMware, Inc. org.graalvm.nativeimage native-image-maven-plugin 20.2.0 com.example.demo.DemoApplication -Dspring.native.remove-yaml-support=true -Dspring.spel.ignore=true native-image package org.springframework.boot spring-boot-maven-plugin 32 Configure Maven plugin in a native profile

Slide 33

Slide 33 text

©2020 VMware, Inc. > mvn -Pnative clean package Total time: 60 s 33 Build the native application • Need for a GraalVM native local install • Build happens directly on your host (Linux, Mac and Windows are supported) • Produces a native executable • No cross compilation

Slide 34

Slide 34 text

©2020 VMware, Inc. > target/com.example.demo.demoapplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: Started application in 0.05 seconds (JVM running for 0.009) 34 Run the native application

Slide 35

Slide 35 text

©2020 VMware, Inc. 35 Petclinic Sample On the JDK Native application petclinic- jdbc Build: Memory(RSS): Startup time: 9s 417M 2.6s Build: Memory(RSS): Startup time: 194s 101M 0.158s +2050% -75% -94%

Slide 36

Slide 36 text

©2020 VMware, Inc. 36 More progress being made… Sample SpringOne 2019 SpringOne 2020 commandlinerunner Build: Exec. size: Memory(RSS): 90s 48M 29M Build: Exec. size: Memory(RSS): 50s 22M 24M -45% -55% -17% webflux-netty Build: Exec. size: Memory(RSS): 193s 81M 95M Build: Exec. size: Memory(RSS): 79s 43M 47M -60% -47% -50% webmvc-tomcat Build: Exec size: Memory(RSS): 203s 105M 70M Build: Exec. size: Memory(RSS): 67s 42M 51M -67% -60% -27%

Slide 37

Slide 37 text

©2020 VMware, Inc. You can specify additional native configuration 37

Slide 38

Slide 38 text

©2020 VMware, Inc. • Create a META-INF/native-image/resource-config.json file • Wildcard patterns can be used • Similar config for reflection and proxies • See GraalVM documentation for more details 38 For example additional resources { "resources": [ { "pattern": "myresource.*" } ], "bundles": [ { "name": "your.pkg.Bundle" } ] }

Slide 39

Slide 39 text

©2020 VMware, Inc. • An agent can assist in generating these files, available with GraalVM java -Dorg.graalvm.nativeimage.imagecode=agent -agentlib:native-image-agent=config-output-dir=META-INF/native-image Demo • Agent has improved greatly during 2020, thanks to the GraalVM team • Run application with agent attached to produce .json files • Exercise all code paths (manually or via tests) - producing (merging) many of these collected configurations 39 Tracing agent to collect necessary information

Slide 40

Slide 40 text

©2020 VMware, Inc. Where are we? 40

Slide 41

Slide 41 text

©2020 VMware, Inc. 41 Timeline GraalVM fixes and improvements We are here

Slide 42

Slide 42 text

©2020 VMware, Inc. 42 Timeline GraalVM fixes and improvements Incubating support in spring-graalvm- native We are here

Slide 43

Slide 43 text

©2020 VMware, Inc. 43 Incubator for GraalVM native support in Spring https://github.com/spring-projects-experimental/spring-graalvm-native

Slide 44

Slide 44 text

©2020 VMware, Inc. • Incubating Spring Boot native application support • It includes a plugin for the GraalVM native image builder • Analyses the Spring Boot application at build time • Computes the most optimal native image configuration • Challenge is doing that with static analysis • Also perform some build time transformation for: • Optimized footprint • Compatibility 44 spring-graalvm-native

Slide 45

Slide 45 text

©2020 VMware, Inc. • GraalVM 20.2 baseline • Use Spring Boot 2.4.0 latest milestone (2.4.0-RC1) • Significant footprint improvements • Wider range of supported technology • New experimental functional mode • Spring Boot lightweight container integration • Maven • Gradle 45 We have just released 0.8.2

Slide 46

Slide 46 text

©2020 VMware, Inc. • Actuator • Cache • Data JPA, MongoDB, R2DBC, Redis • JDBC • Logging (Logback) • Thymeleaf • Validation • Web (Spring MVC with Tomcat) • Webflux (Netty) • Wavefront • Spring Cloud Function 46 Starters supported in 0.8.x

Slide 47

Slide 47 text

©2020 VMware, Inc. 47 Reference documentation available

Slide 48

Slide 48 text

©2020 VMware, Inc. 48 Timeline GraalVM fixes and improvements Spring Boot support Incubating support in spring-graalvm- native We are here

Slide 49

Slide 49 text

©2020 VMware, Inc. The road ahead 49

Slide 50

Slide 50 text

©2020 VMware, Inc. • First release with beta support • Spring Boot 2.4.0 and GraalVM 20.3 baseline • Subset of starters supported • Breaking change will happen (with upgrade paths) • Wider support • Spring Security • Spring Batch • Development-oriented container image 50 spring-graalvm-native 0.9.0 (December 2020)

Slide 51

Slide 51 text

©2020 VMware, Inc. More starters supported in upcoming 0.9.x releases 51

Slide 52

Slide 52 text

©2020 VMware, Inc. Spring Boot 3, based on Spring Framework 6, is expected to provide first-class support for native application deployment, as well as an optimized footprint on the JVM. 52

Slide 53

Slide 53 text

©2020 VMware, Inc. Thank You Martin Lippert @martinlippert lippertm@vmware.com