SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
807 stars 171 forks source link

Use Virtual Threads threading model #474

Closed ujibang closed 7 months ago

ujibang commented 1 year ago

Overview

Java 21 introduces Virtual Threads, a feature detailed in JEP 444. These lightweight threads significantly boost performance and simplify concurrent programming.

RESTHeart 8 harnesses Executors.newVirtualThreadPerTaskExecutor() to manage requests handled by services marked as blocking (with RegisterPlugin(blocking = true) by default), efficiently processing blocking services.

Furthermore, RESTHeart 8 configures Undertow to use its ThreadAwareByteBufferPool for optimized resource allocation:

Rationale

Virtual threads are lightweight, high-throughput threads that simplify the development, maintenance, and monitoring of concurrent applications.

Configuration

The following options manage virtual threads:

# Specifies the initial number of platform carrier threads used for virtual threads in blocking operations.
# Recommended value: 1.5 * number of cores.
# If <= 0, it defaults to 1.5 times the system core count.
workers-scheduler-parallelism: 0

# Sets the maximum number of platform carrier threads for virtual threads in blocking operations.
workers-scheduler-max-pool-size: 256
ujibang commented 7 months ago

All blocking tasks are now dispatched to the VirtualThreadPerTaskExecutor, ensuring efficient task execution and concurrent programming.

However, a known limitation is with the Truffle JS engine, which currently lacks support for virtual threads. To mitigate this, the truffle-runtime has been excluded from the restheart.jar.