Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fibers – blocking is cheap in a Parallel Universe jPrime Stefan Minev

Similar presentations


Presentation on theme: "Fibers – blocking is cheap in a Parallel Universe jPrime Stefan Minev"— Presentation transcript:

1 Fibers – blocking is cheap in a Parallel Universe jPrime 2017 Stefan Minev

2 Stefan Minev Software Developer

3 Quasar -  library that provides high-performance lightweight threads and actors for the JVM
Quick overview Demo QA are during the demo and after it Private and confidential

4 At the beginning the threads were “Green”

5 What are fibers? Thread is a continuation scheduled to run on a CPU core at the appropriate time by a scheduler. Continuation is a program counter, marking a given point in the sequence of instructions, and a stack, storing the values of the variables. Quasar fibers are threads implemented in JVM bytecode rather than in the OS kernel.

6 Why fibers instead of OS threads?
Threads are expensive: Thread switching incurs significant performance penalty. The kernel schedules threads by using a general-purpose scheduling algorithm. We can have only few thousands of them. Quasar fibers are cheap Fibers put a far lesser burden on the CPU when task-switching. Fibers are scheduled at the application layer by a high-quality work-stealing scheduler that uses fork join pool. Ideal for transactions that block very often. We can have millions of them - an idle fiber occupies ~400 bytes of RAM.

7 How do fibers work? new Fiber<>((SuspendableRunnable) () -> { // Instrumentation here - set bytecode index on resume long mills = System.currentTimeMillis(); System.out.println( "before suspension: " + mills); // Instrumentation here - store variables Fiber.sleep(3000); // Instrumentation here - restore variables System.out.println("and now on resume: " + mills); }.start(); before suspension: and now on resume:

8 Identify suspendable methods to be instrumented
Throws SuspendExecution exception in a method signature annotation to a method List classes/methods in suspendables and suspendable-supers files Use the SuspendablesScanner Ant task to fill the suspendable-supers file Use the SuspendablesScanner Ant task in auto mode to fill suspendables and suspendable-supers files without manually marking suspendable methods at all. Could end up instrumenting more than necessary.

9 Ways to perform the bytecode instrumentation
Running the Instrumentation Java Agent (-javaagent:path-to-quasar-jar.jar as a JVM argument). This works with standalone Java applications and embedded Servlet containers. Ahead-of-Time (AOT) Instrumentation by running InstrumentationTask via Ant after compilation. Using QuasarWebAppClassLoader for standalone Servlet containers (Tomcat and Jetty).

10 Official caveats Runaway fibers – stuck in a loop or blocking the thread Thread locals in Fibers – works as expected Synchronized and blocking “Thread” calls – not allowed by default but could be handled

11 “Do not communicate by sharing memory; instead, share memory by communicating.”
Channels - queues used to pass messages between strands (fibers and threads). ch: = make(chan string) go func() { ch < -"ping" }() msg: = < -ch fmt.Println(msg) Channel<String> ch = Channels.newChannel(0); new Fiber( ()-> ch.send("ping") ).start(); String msg = ch.receive(); System.out.println(msg);

12 Quasar’s Actor System Quasar provides an implementation of the actor model practically identical to Erlang’s. Akka’s callback-based API vs Quasar’s fiber blocking API

13 Comsat – your universe implemented with fibers
Servlets – extending FiberHttpServlet instead of HttpServlet JAX-RS API –using the co.paralleluniverse.fibers.jersey.ServletContainer with enabled async support instead of the one provided by Jersey. HTTP clients - Apache Http Client, JAX-RS client, Retrofit and OkHttp DB access – JDBC, MongoDB, JDBI, JOOQ Dropwizard Spring – Spring Web MVC, Spring Boot, Spring Security Web Actors – it is a new API

14 Demo

15 Awesome! What’s the gain?
Go on with your easy to write synchronous code and benefit from the power and efficiency of the asynchronous programming. Just leave the complexity in a Parallel Universe! Private and confidential

16 So…is it production ready and should I use it?

17 Let’s make Java great again!
“Note: work is ongoing with the OpenJDK team that will allow to remove this restriction completely starting with a JDK9 version of Quasar: efficient, automatic runtime instrumentation will be performed at the bytecode level, that is for all code written in any JVM language, without need anymore for annotations nor instrumentation plugins.”

18 Questions?


Download ppt "Fibers – blocking is cheap in a Parallel Universe jPrime Stefan Minev"

Similar presentations


Ads by Google