cask icon indicating copy to clipboard operation
cask copied to clipboard

Use Java Virtual Threads (Loom) (500USD Bounty)

Open sorumehta opened this issue 2 years ago • 10 comments

Spawn a virtual thread for each incoming request, which would enable asynchronous behaviour and increase performance of the library. The new Java virtual threads are lightweight and can replace the traditional Java threads. Given that Cask does not have any strong async/concurrency model of its own, leveraging the new JVM feature would be really nice.

Haoyi: To incentivize contribution, I'm putting a 500USD bounty on resolving this ticket. This is payable via bank transfer, and at my discretion in case of ambiguity. The acceptance criteria is a PR implementing support for virtual threads, tests exercising them, and performance benchmarks (ad-hoc is OK) demonstrating a performance improvement (e.g. in high concurrency use cases) vs normal threads in Java 21+

sorumehta avatar Sep 20 '23 14:09 sorumehta

I can create a PR for this if this seems like a good idea to @lihaoyi

sorumehta avatar Sep 22 '23 03:09 sorumehta

Go for it, though note we'll need some compatibility story for Java <21

lihaoyi avatar Sep 22 '23 04:09 lihaoyi

Is this something that https://github.com/com-lihaoyi/cask/pull/74 unblocks?

lloydmeta avatar Jan 14 '24 05:01 lloydmeta

I would recommend against this until JAVA-23 is released, du to the "pinning issue", effectively making Virtual Threads useless if your code/libs uses synchronized a lot. https://www.infoworld.com/article/3713220/java-virtual-threads-hit-with-pinning-issue.html

andreak avatar May 15 '24 07:05 andreak

Doing it opt-in should be fine. We can run benchmarks etc. without synchronized to see the benefit. That way we can be ready on day one when Java 23 lands with a fix to the thread pinning issue.

lihaoyi avatar May 15 '24 10:05 lihaoyi

I have done this at $work for all my services, with Java 21, it works fine, no need to delay to Java 23.

He-Pin avatar May 19 '24 07:05 He-Pin

If there are issues because of undertow, you may consider switching to Helidon, which is the new web server from Oracle, built for working with virtual threads.

https://helidon.io

https://github.com/helidon-io/helidon/tree/main/webserver

https://central.sonatype.com/artifact/io.helidon.webserver/helidon-webserver/overview

https://javadoc.io/doc/io.helidon.webserver/helidon-webserver/

sideeffffect avatar Jul 22 '24 17:07 sideeffffect

FYI for anyone reading, this bounty is still open in case anyone else wants to take a crack at this

lihaoyi avatar Sep 15 '24 09:09 lihaoyi

@lihaoyi Since I started this issue, I am going to finish it too. :)

sorumehta avatar Sep 19 '24 15:09 sorumehta

while building cask, twirl throws java.lang.reflect.InvocationTargetException

sorumehta avatar Sep 23 '24 10:09 sorumehta

while building cask, twirl throws java.lang.reflect.InvocationTargetException

I get that too when building on JDK >= 21. Maybe the newer 2.0.x works better.

jodersky avatar Nov 05 '24 13:11 jodersky

Looks like it wasn't twirl afterall (probably, just a cached coincidence when I switched JDK version). See #154.

jodersky avatar Nov 27 '24 12:11 jodersky

@sorumehta @jodersky I bought a MacBook for this :) .

He-Pin avatar Jan 11 '25 19:01 He-Pin

@sideeffffect As all the dispatching layers are done in cask, I think Netty/Vert.x is an option, too?

He-Pin avatar Jan 11 '25 19:01 He-Pin

@sorumehta BTW, I was superized by the performance, which is quite good, at least many systems only have < 1qps.

He-Pin avatar Jan 19 '25 08:01 He-Pin