[JAVA-4649] Support Virtual Threads Created: 14/Jun/22  Updated: 05/Oct/23  Resolved: 30/Sep/23

Status: Closed
Project: Java Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 4.11.0

Type: Epic Priority: Major - P3
Reporter: Ashni Mehta Assignee: Valentin Kavalenka
Resolution: Done Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Documented
Quarter: FY24Q3
Backwards Compatibility: Fully Compatible
Documentation Changes: Needed
Documentation Changes Summary: We should mention in What's new that we updated the codebase to avoid unnecessary pinning of virtual threads (https://openjdk.org/jeps/444) and to preserve interrupted status of a thread, as the latter matters for structured concurrency (https://openjdk.org/jeps/453) where it is used for cancellation.
Start date:
End date:
Calendar Time: 7 weeks, 4 days
Scope Cost Estimate: 4
Cost to Date: 5
Final Cost Estimate: 5
Cost Threshold %: 100
Detailed Project Statuses:

Engineer(s): Valentin Kavalenka

2023-09-29: Updated end date to 2023-10-06

  • Everything is in review. Added a week to the end date for reviews!

 

2023-09-15: Set target date to 2023-09-29

  • Remaining: 
    • Handle interrupts and replace uninterruptible Lock
  • Valentin has been doing some work for 4.11 release this week leading up to .local and will pick this back up
  • Bumped up Final Cost Est by a week

2023-09-01: Set target date to 2023-09-08

  • Work is proceeding
  • Remaining: 
    • Fix handling of InterruptedExceptions/SocketExceptions
    • Investigation of ThreadLocalRandom's interaction w/ virtual threads

2023-08-18: Set target date to 2023-09-01

  • Work started
  • Removal of synchronized blocks and methods in production code in progress

 Description   

Summary

Virtual threads (https://openjdk.org/jeps/444) support is GA in Java 21 . We should make any necessary improvements to the Java driver to allow our users to make use of these new features.

Who is the affected end user?

Any MongoDB Java driver applications using virtual threads in Java 21+.

How does this affect the end user?

Users can currently choose to use Future.cancel or ExecutorService.shutdownNow to attempt to cancel or shut down threads. With structured concurrency, any fork (including a transitive fork) can cause threads of all other forks still active to be interrupted. This means that forks should be responsive to interruption.

How likely is it that this problem or use case will occur?

As more users upgrade to Java 21, they may need us to handle interruptions differently. 

If the problem does occur, what are the consequences and how severe are they?

If a socket operation is interrupted, it will be treated as a a network error. However, it should be treated as an InterruptedException. (JAVA-4646)

Additionally, it is possible to create a scenario in which all virtual threads are starved of carrier threads due to the fact that all available carrier threads are pinned by synchronized blocks. (JAVA-4642)

Is this issue urgent?

Java 21 was released in September 2023. It is an LTS release. While generally users are slow to upgrade production apps to the latest JDK, adoption if newer JDKs is accelerating and we expect users to try this feature out as soon as it's available. Also, many application servers (Helidon, Spring Boot, Weblogic, Quarkus) either already do or will soon be able to operate in a mode where all requests are handled on virtual threads, which means that anyone using those servers with MongoDB will easily be able to create applications which are executing Java driver code on those virtual threads.

Is this ticket required by a downstream team?

No 

Is this ticket only for tests?

No.

Cast of Characters

Engineering Lead: jeff.yemin@mongodb.com
Product Owner: ashni.mehta@mongodb.com



 Comments   
Comment by Valentin Kavalenka [ 06/Sep/23 ]

We use FileInputStream/FileOutputStream in the legacy com.mongodb.gridfs, and those do not support interruptible IO. We could have replaced them with FileChannel, which is an InterruptibleChannel, but I don't think we should spend resources on that.

Generated at Thu Feb 08 09:02:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.