247 points · by SemanticStrengh · 4y ago · 212 comments
Achieving 5M persistent connections with Project Loom virtual threads - https://news.ycombinator.com/item?id=31214253 - April 2022 (142 comments)
"Whereas the OS can support up to a few thousand active threads, the Java runtime can support millions of virtual threads. Every unit of concurrency in the application domain can be represented by its own thread, making programming concurrent applications easier. Forget about thread-pools, just spawn a new thread, one per task. You’ve already spawned a new virtual thread to handle an incoming HTTP request, but now, in the course of handling the request, you want to simultaneously query a database and issue outgoing requests to three other services? No problem — spawn more threads. You need to wait for something to happen without wasting precious resources? Forget about callbacks or reactive stream chaining — just block. Write straightforward, boring code. All the benefits threads give us — control flow, exception context, debugging flow, profiling organization — are preserved by virtual threads; only the runtime cost in footprint and performance is gone. There is no loss in flexibility compared to asynchronous programming because, as we’ll see, we have not ceded fine-grained control over scheduling."
This seems so obvious in hindsight as the "right way". I wonder why we went down that whole async/await craze with so many languages?
We addressed this, albeit very briefly, in the Alternatives section of the JEP: https://openjdk.java.net/jeps/425
There are multiple reasons:
1. Languages that don't already have threads have an implicit assumption built into all existing code that program state cannot change concurrently. This is why scheduling points need to be marked explicitly, and why adding threads — whether user-mode or OS — might break existing code in some very tricky ways. That's the case of JavaScript.
2. Some languages target an IR without control over its backend, making an efficient implementation of user-mode threads difficult, if not impossible. async/await requires only changes to the frontend compiler. That's the case of Kotlin, and, perhaps to a lesser extent, Rust.
3. Some languages have technical features that make implementing user-mode threads efficiently more difficult than in others. Pointers into the stack and careful control over memory allocation make this more challenging in languages like C++ and Rust than in Java.
As an aside, Ron had a JUG talk 6 months ago which I found really helpful where he went into more detail about why they chose this approach: https://youtu.be/KmMU5Y_r0Uk (27m20s mark, and from 2m50 there’s a more general introduction to Loom).
I’m sure there are other videos/papers as well, but this was a pretty good overview of Java vs other languages’ approach to async.
For example if I want to implement a "sleep" with async/await I probably only need to store the wake time as state, if I want to make a virtual thread to do the same I likely need to allocate a large stack just in case I use it.
Of course this can be mitigated with stack caching, small stacks, segmented stacks or other tricks. But doing this is still more expensive than knowing how much "stack" you need up-front and allocating only that.
At the cost of breaking : conceptual model of concurrency ; debugging ; performance analysis ; tracing ; logging
but yeah...great stuff.
While the "stack is optimally sized" argument exists, it might not always be true: E.g. implementations could require far more memory being required for the "virtual" stack than what is actually required due to implementation challenges. That for example applies in various situations in Rust. Then a more classical stackless implementation which allocates state for each callback on the heap (like if you manually write boost asio code) which have quite some allocation and memcpy churn. And besides that a "virtual stack" might be more fragmented and less cache friendly than a contiguous stack, which also impacts efficiency.
The programmers should be seen as “users” of the language.
What you give here is a list of excuses on why system X doesn’t do what is best for its users.
For languages like Python for example, this is a big issue, and reason why alternative concurrency patterns to async/await haven't made much progress.
Targeting a specific IR is an implementation detail.
Having explicit pointers and thread are not mutually exclusive concepts. It’s the implementation details that make them difficult.
HN can be so annoying sometimes
Well, ironically, the exception that applies to all languages being: "does my code still work?" choices... which is what pron was addressing.
And, to be fair, languages like Erlang and Go went this route LONG ago.
So did java! It originally used green threads, before switching to exclusively kernel threads.
For single-threaded event loops because most of those languages did not want to put the concept of thread safety onto the developer (e.g. JS and Python). And even the ones that do concern the user w/ thread safety don't have an intermediate representation and VM to automatically sequence instructions (e.g. Rust).
rust's rfc explains some of the drawbacks: https://github.com/rust-lang/rfcs/blob/master/text/0230-remo... (which basically explains why a preview took until jdk 19)
// Edit: also:
void handle(Request request, Response response) {
var url1 = ...
var url2 = ...
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
var future1 = executor.submit(() -> fetchURL(url1));
var future2 = executor.submit(() -> fetchURL(url2));
response.send(future1.get() + future2.get());
} catch (ExecutionException | InterruptedException e) {
response.fail(e);
}
}
is not better or worse than: async Task<IActionResult> Handle()
{
var url1 = ...
var url2 = ...
try {
var requestTask1 = FetchURL(url1);
var requestTask2 = FetchURL(url2);
return Ok((await requestTask1) + (await requestTask2));
}
catch (Exception ex)
{
return BadRequest();
}
}
of course one uses "colored" functions the other not, but both have one thing in common: a bad programmer can make serious mistakes with both.It is better for two reasons:
1. Virtual threads are threads as far as the Java platform is concerned, meaning you get the same troubleshooting (stack traces), debugging, and profiling support by the runtime and its tools as for platform (OS-backed) threads.
2. There is no split-world of APIs, with separate and largely incompatible flavours, that need to be developed for two constructs that are semantically (almost) equivalent.
You are correct, however, that implementing user-mode threads is harder than implementing async/await. While the latter requires only changes to the frontend compiler (until you want to add tooling support), the former requires deep changes to the backend.
I never had any problem with debugging and profiling Futures/async/await. Neither in Rust/C#/Scala. And stack traces like Exceptions were never a problem, in fact go panics were way more troublesome... (and still are, most often you dont run into them...) Most of the time depending how "concurrent" your program is, it also makes just no sense to debug them anyway, if you deal with tons of concurrent code you should learn to make good log statements, it's bread and butter, especially once your in production.
> 2. There is no split-world of APIs, with separate and largely incompatible flavours, that need to be developed for two constructs that are semantically (almost) equivalent.
well yeah... but the question is why do you try to hide it? that programmers don't see that the code might run concurrent? most goroutine bugs I've seen happened because of the hidden concurrency. and it's odd that an important concept gets hidden, for the sake of split-world apis. heck thats exactly why there is a type system, to express the meaning of code. with green threads it's not really obvious on first sight what the code exactly expresses.
(P.S.: I switched pretty early in my career to Scala, now to Rust/Golang/C# so maybe I'm more biased when it comes to a strong type system and I prefer a strong typesystem with all it's quirks, i've seen nasty bugs with goroutines and with async/await, so I doubt there is any real world benefit over the two worlds)
Hindsight? There are many of us that have been advocating against the async/await madness.
I said it before and I will say it again. Async is today what OOP used to be in the nineties.
But I am a Java pleb.
I don't think you could, for example, use this model of many virtual threads with a traditional GUI framework as designs are still predominately single threaded by nature.
Said another way, cooperative scheduling has its place and will continue to.
I'm excited to see some threaded UI designs shake out of this, though.
Well no, not _all_ of the complexities because you have await and explicit yielding. It's much easier to pass data from one asynchronous task to another using await than it is to, say, manually code the locks or state machine necessary for the message passing and/or callbacks.
you can do that as a design choice in languages that aren't exclusively single threaded.
Async/await is in many single and multi-threaded languages so I'm not clear on what you're saying.
Funny, I have never heard anyone describe threads as the thing that made concurrent programming easier, or more threads would make it easier.
What makes concurrent programming easier is thread interaction models that make it hard to lock the process.
async\await, give you concurrency without the need to for the developer to explicitly write lock code. Much easier than having to manually handle that interaction.
async\await in no way relieve the developer from needing to worry about locks. If you have shared mutable memory, you have locks. If you aren't considering that, then you've got broken code.
> What makes concurrent programming easier is thread interaction models that make it hard to lock the process.
Yes, the way you do that is by focusing on message passing and immutable data when you can get away with it and, ideally, prebuilt thread safe datastructures when you can't.
What async\await buys is lightweight threading when lightweight threading isn't available. It allows you to have millions of concurrent processes running at the same time. HOWEVER, the cost of this is the "colored function" problem. You HAVE to mark up your code to let the compiler/framework know "this is code that can block and thus needs to be able to give up control and resume". This problem is difficult because you can't simply call an async function from a non-async function. You have to do some wrapping/juggling to make everything play nice.
The reason lightweight threading is nice is because you no longer have the colored function problem. From any reference or context you can say `CompletableFuture.supplyAsync(()->calculateValue())` and have a new concurrent action spawned. If that action does IO or whatever, it doesn't hog a thread, it just yields (like async/await does) and lets another task move forward.
The only reason for async/await is OS threads are expensive and have pretty large negative implications on operating systems.
This isn't really true. For example, UI code often uses async/await for the concurrency but a single UI thread to prevent locking. Because this design is only concurrent and not parallel you don't need to worry about thread safety while still getting a way to yield execution to other code.
The huge difference is that futures (and thus async/await) change the primary concurrency concept you think about into "wait until this value is available" rather than managing the locks and shared memory yourself.
The latter is still available (and still as footgunny as ever) but aren't as much of a problem when better options are more ergonomic.
Futures also compose far better than threads do, especially when you want to wait for "any of X" rather than "all of X".
My theory on is that it evolved as a consequence of people trying to solve problems on a level they were familiar and comfortable with. E.g. let's say there were C and C++ application programmers who wanted to scale their applications to handle more clients than a OS supported at that point in time. Their solution was to use event-driven OS APIs, and build abstractions on top of that (libuv, boost) that they found comfortable to use. Then we went one level further, and compiler and programming language experts got aware about the problem, which tried to solve it with language extensions (async/await).
I fully confess I'm guilty of this too, by having used/promoted/evolved various async frameworks in the last 10 years. It's a super interesting problem to work on, a bit on the research side, and it feels fulfilling to design some elegant solutions on top of the "i have to async paradigms" problem.
But ultimately the compiler magic on top of callbacks on top of event-driven OS APIs workarounds definitely doesn't seem to be best solution for regular application developers, since the abstractions are very leaky and now application developers need to be aware about how all those things work and work together.
I really like the Loom solution and look forward to it, since it means the application space doesn't have to deal with colored functions anymore. But ultimately I'm wondering in the meantime whether we should rather try to find an OS level fix for those OS level problems instead of trying to work around them on a higher level. There's probably 100x the amount of code written on async runtimes than for the kernel schedulers and IO subsystems themselves, which feels wrong.
https://github.com/python-greenlet/greenlet
has been available for quite some time in Python (gevent probably being its most used flavor).
Note that it it actually predates the async/await approach which was incorporated into the Python language (so, in Python it was implemented as a third-party library -- even async/await had an implementation based on Python 2 using yield and some decorators: https://pypi.org/project/trollius/).
I really do think Python should have instead adopted gevent as it's async approach instead of asyncio and async/await etc.
Does anyone know how Project Loom has gotten around these constraints?
[0] http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2018/p136...
One big (and IMHO the only) advantage of the async/await model is that the stack size can be bounded. But if you control the VM like in the Loom case, you can expand and shrink stacks as needed, so it is much less of an issue.
That’s basically what async swift does?
It is also orthogonal to async/await.
Async/await is a mechanism a programming language uses to allow you to write straight line code without having to write blocking code. You could resolve that by creating a hulking thread, but that just means you need to await a thread result :)
Also JS isn’t going anywhere, and it seems unlikely it will ever get multiple execution threads
How many servers have a spare half-terabyte of RAM to do that though?
((512 * 1024 * 1_000_000) / 1024 / 1024 / 1024 = ~500)
Secondly, you don't need 512kb per thread. The kernel will only use pages that are actually used, so by default new threads will use 4kb.
[1] https://man7.org/linux/man-pages/man3/pthread_create.3.html
Because it's a useful abstraction for managing N concurrent activities. In the threaded world, it's known as fork-join.
I really think C# & Python will be jealous of the languages mentioned and puts them in an odd spot from language design perspective.
https://github.com/python-greenlet/greenlet
has been available for quite some time in Python (gevent probably being its most used flavor).
i.e.: just because it's not in the standard library doesn't mean it's not available (so, you can actually choose whether you'd like to use async/await or greenlets).
I must say that the main usage I personally had of greenlets didn't have it in mind initially (it was used in an existing application where making the coloring wasn't really feasible as it was a huge app already and adopting green threads was much less work).
https://www.reddit.com/r/rust/comments/7x0icm/regarding_gree...
https://github.com/rust-lang/rfcs/blob/master/text/0230-remo...
> Initially, Rust supported only the green threading model. Later, native threading was added and ultimately became the default.
Although the coloring problem is only a problem if red functions are harder to use. Not sure marking all functions with "suspend" would make it any worse, besides infecting the codebase.
https://elizarov.medium.com/how-do-you-color-your-functions-...
In theory, such a system could be implemented on C# with the same sort of gotchas as Java virtual threads. I don't think there's a fundamental design conflict.
https://github.com/openjdk/jdk/pull/8166
1,140 files, (+98,553 −9,862 LOC) and those lines of code are mostly horribly fiddly low level assembly/compiler hacking. This is partly why it took years of development.
Most people don't realize this, especially because in the later stages of course many others contributed, but Loom is in some sense one man's journey. Before he worked at Oracle Ron Pressler spent years writing a library called Quasar which implemented fibers on top of the JVM using bytecode rewriting and some low level hackery with internal APIs. At some point he became available for hiring and Oracle brought him on board, as by that point he was not only an expert in fiber implementations but also the JVM. That was the genesis of Loom.
Something I've learned from following the intricacies of VM development is that what we get is very much a result of hidden human stories as well as technical decisions. Features happen or don't happen on the basis of who was available to be hired at the time, as much as cold calculations of performance impacts. In turn that depends heavily on the vagaries of personal lives. The skills needed to do this work aren't that easy to find on the open market and training takes a long time.
For other VM implementors to do this, and realistically only .NET has the sort of languages where it makes sense (JS doesn't), well, it'd take a long time even if they start today and there's no guarantee of success.
There are also advantages in debug ability and performance tracing for threads.
It would be nice to have (virtual) threads that are lightweight and efficient enough that we did not need async for writing highly concurrent software.
I wonder if 20 years from now, programmers will look at async, kind of like current programmers look at segmented memory or manual register allocation —- something that was necessary for performance in a bygone era, but is now not needed.
If anything, this is yet another example of the decades how old concepts still take to become widespread across all major mainstream languages.
How does it know that an operation is blocking on I/O? Is this limited to stuff baked into the language or standard library?
What is the mechanism of suspension and resumption? They mention elsewhere that any platform thread could pick up any virtual thread so I assume they must be storing the stack somewhere. Is there a cost transferring stacks on virtual threads across platform threads? Does this introduce new security implications if there are less OS level restrictions on memory access between platform threads?
What happens in cases where an application has user defined platform threads? How does the system determine what platform threads are available to the virtual threading system?
I think this is probably the right decision for Java and I agree with all of their motivations. I personally like the explicitness of async/await, however I assume Java devs are very familiar with threading in general. I believe this allows an easier path to migrating existing Java code.
Most libraries use the I/O primitives from the platform standard libraries, so the behaviour is going to trickle out from there. If you aren't using those libraries, the only other way to do I/O would be to use native code such as via JNI, and the runtime would schedule that on a thread pool and so it would tie up an OS thread for the duration of a function invocation.
It will be interesting to see how much of a coloring problem this creates, if any. I guess the great thing about having it be standardised and baked into the language / VM is it will quickly become de facto best practice to make any such library code compliant with virtual threads. And for all its various downsides, the java ecosystem has always leaned away from native code and treated it as a last resort rather than leaning into it like Python etc. So likely it will not be a huge issue the way the coloring problem is in the async/await situation.
You shouldn't need to "transfer" the stack. There isn't really a "platform stack", that is just whatever your stack pointer is pointing at. So it is perfectly fine to allocate many stacks and switch between them within one OS thread just by changing the stack pointer.
Of course if you are allocating a "full stack" then the main question is what is the point? IIUC the biggest cost of threads is the memory allocated to the stack. So unless you are doing something clever you don't get much benefit. There are many approaches here but I guess it is up to the runtime to pick one.
Virtual threads are built on top of underlying delimited one shot continuations, and those store the stack. A large part of the engineering effort has been in making this as efficient as possible.
> Is there a cost transferring stacks on virtual threads across platform threads?
A stack always has to be at least partially copied to the carrier thread’s stack, and it doesn’t really matter which OS thread that is. I say partially because initially only the current stack frame will be copied as most threads will have a deep stack compared to the number of frames active in any operation likely to yield.
> Does this introduce new security implications if there are less OS level restrictions on memory access between platform threads?
The security model remains intact. A virtual thread performing some privileged operation will be privileged no matter which OS thread it is run on, and one which is not privileged will not be no matter the OS thread it runs on.
> What happens in cases where an application has user defined platform threads? How does the system determine what platform threads are available to the virtual threading system?
Virtual threads don’t just run on a random OS level thread. They run on a scheduler (commonly a fork join pool) and are effectively a series of tasks fed to that scheduler.
https://cr.openjdk.java.net/~rpressler/loom/loom/sol1_part1....
"All Your Blocking Are Belong to Us"
Not sure how updated the document is. But the section title says it all. (It's a reference to a meme.)
That is, should i organise my code so i create a single executor and use it across many operations, or is it okay to create and destroy executors all over the place?
And are the executors threadsafe - both for use from virtual threads, and from native ones?
As an aside, these are things which is important to know, but which are very rarely documented. We had some headaches a while ago because we wrote code which treated the new HttpClient as lightweight when it is very much not.
Executors tend to be units of management (await()/shutdown()) etc. and monitoring, so that'll be your main reason to use more than just one for everything.
The good news is that long term supported releases got more frequent in recent times and the next one is due in ... September 2023. So there hopefully won't be any delays for people who have to wait for an LTS release.
1. Promise constructor (to turn an existing callback-based or async operation into one that can be blocked with Loom). JS example:
await new Promise(resolve => someApiThatTakesACallback(resolve));
2. Methods like Promise.all() and Promise.any() to run multiple async operations concurrently. JS example: const [result1, result2] = await Promise.all([doSomething1(), doSomething2()]);1. Same as with normal threads:
CompletableFuture<Foo> future = new CompletableFuture<>();
someApiThatTakesACallback(future::complete);
Foo result = future.get(); // this can take a timeout
2. If you mean that the operations are blocking and should be called in parallel using virtual threads, the equivalent of Promise.any:: ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
Future<Foo> future1 = executor.submit(this::doSomething1);
Future<Bar> future2 = executor.submit(this::doSomething2);
Foo result1 = future1.get();
Bar result2 = future2.get();
There is a version of that using ExecutorService::invokeAll, but i'm not sure it's any more concise, because you still need to unpack the futures one by one.For Promise.any:
ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();
Foo result = executor.invokeAny(List.of(this::doSomething1, this::doSomething2));
The asymmetry between invokeAny and invokeAll here is slightly grating, but makes sense.Note that in both cases, you probably already have an executor lying around that you can use for this.
I was expecting something like call/cc, or `suspend(Cancellable)Coroutine` in Kotlin. But judging from the list of preview items[1] it seems the only way is to have the callback write the result to a Future and blocking wait on it.
2.1 Promise.all() can be done with `.map(future => future.get())` But it is a bit more complicated than that. [2]
2.2 Promise.any()
This is somewhat relevant http://mail.openjdk.java.net/pipermail/loom-dev/2020-Februar...
[1] https://download.java.net/java/early_access/loom/docs/api/pr...
[2] https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutin... "This function is not equivalent to deferreds.map { it.await() } which fails only when it sequentially gets to wait for the failing deferred, while this awaitAll fails immediately as soon as any of the deferreds fail."
This is overkill for a single task, but for your second example you could do a an invokeAny with something like this:
try (var scope = new StructuredTaskScope.ShutdownOnSuccess<String>()) {
scope.fork(() -> doSomething1());
scope.fork(() -> doSomething2());
scope.join(); // or if you wanted a 5 second timeout .joinUntil(Instant.now().plusSeconds(5))
String result = scope.result(); // This will return the result of either doSomething1() or doSomething2(), whichever won the race to finish first.
...
}
https://download.java.net/java/early_access/loom/docs/api/jd...For the second one, you can use an implementation of ExecutorService[1], which has the methods invokeAll and invokeAny.
Futures in Java are similar to Promises in Javascript, in that they represent a calculation that might not be completed yet.
1. https://docs.oracle.com/en/java/javase/17/docs/api/java.base... 2. https://docs.oracle.com/en/java/javase/17/docs/api/java.base...
`ExecutorService#submit` works with the latter.
Not even close. The entire mobile space — Android and iOS combined — is drastically smaller than just Java alone (not counting Android). You can confirm this by going to job websites that allow you to analyse their postings. Java, together with its sister leading languages, JavaScript and Python, is so popular that it's in a completely different ballgame than most languages.
> Virtual threads are a lightweight implementation of threads that is provided by the JDK rather than the OS. They are a form of user-mode threads, which have been successful in other multithreaded languages (e.g., goroutines in Go and processes in Erlang). User-mode threads even featured as so-called "green threads" in early versions of Java, when OS threads were not yet mature and widespread. However, Java's green threads all shared one OS thread (M:1 scheduling) and were eventually outperformed by platform threads, implemented as wrappers for OS threads (1:1 scheduling). Virtual threads employ M:N scheduling, where a large number (M) of virtual threads is scheduled to run on a smaller number (N) of OS threads
I genuinely don’t know as I have never actually seen them being used, but they always seem to be described as a “lightweight” thread
Or as syntactic sugar for interacting with such a threadpool?
I guess I still haven‘t understood what this gives you as opposed to submitting lambdas to a threadpool. Besides maybe more usefull threadlocals.
Curious to see how Loom concurrency compares to FP languages like Erlang.
The JDK doesn't have this because many of the concerns involved are outside its remit. Java does not define a project layout, a build process, or a way to acquire or distribute libraries.
But the Java world does have Gradle and Maven, which are, for better or for worse, the options to satisfy abledon's desire.
e.g. to make a new console app.... ```
c#
dotnet new console
java
go install extra maven program (if your team uses maven)...
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
```