Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce Java 21 Virtual Thread Pinning in IO operations #2553

Merged
merged 2 commits into from
May 30, 2024

Conversation

BenWhitehead
Copy link
Collaborator

Setup a small workload using virtual threads and turned on pinning detection (-Djdk.tracePinnedThreads=full), and run various methods for uploading and downloading object data.

I then reduced the pinning sights to a minimized list (grep 'monitor' run.log | grep 'com.google.cloud.storage' | sort -u)

    com.google.cloud.storage.BaseStorageReadChannel.read(BaseStorageReadChannel.java:105) <== monitors:1
    com.google.cloud.storage.BlobWriteSessions$WritableByteChannelSessionAdapter.open(BlobWriteSessions.java:46) <== monitors:1
    com.google.cloud.storage.LazyWriteChannel.getSession(LazyWriteChannel.java:59) <== monitors:1
    com.google.cloud.storage.ParallelCompositeUploadWritableByteChannel.close(ParallelCompositeUploadWritableByteChannel.java:268) <== monitors:1
    com.google.cloud.storage.ParallelCompositeUploadWritableByteChannel.write(ParallelCompositeUploadWritableByteChannel.java:178) <== monitors:1
    com.google.cloud.storage.StorageByteChannels$SynchronizedBufferedWritableByteChannel.close(StorageByteChannels.java:119) <== monitors:1
    com.google.cloud.storage.StorageByteChannels$SynchronizedBufferedWritableByteChannel.write(StorageByteChannels.java:109) <== monitors:1
    com.google.cloud.storage.StorageByteChannels$SynchronizedUnbufferedReadableByteChannel.read(StorageByteChannels.java:139) <== monitors:1

To avoid pinning during IO operations, uses of synchronization have been replaced with ReenterantLock. There were a few synchronization sites that were also guarded by an outer operations synchronization, these too have been updated to use ReenterantLock.

We stick with synchronized for init related operations. In the case of creating a WriteChannel, the ability to open depends on the resumable session being created successfully, which requires a network request. Our objects in this area are not written in a top to bottom async friendly way to remove the synchronization at this time. Given these operations are performed at most once per WriteChannel impact is much less drastic compared to write(ByteBuffer) and close().

After this change, I ran the same workload and we are left with only the two following pins detected

    com.google.cloud.storage.BlobWriteSessions$WritableByteChannelSessionAdapter.open(BlobWriteSessions.java:46) <== monitors:1
    com.google.cloud.storage.LazyWriteChannel.getSession(LazyWriteChannel.java:59) <== monitors:1

Some investigation into if/how this type of pinning detection can be introduced to integration suite still needs to happen. Superficially, I'm not sure if it's practical to get JUnit4 to run everything in virtual threads, we might need to depend on some other CI external validation.

Part of work needed for #2308

@BenWhitehead BenWhitehead added do not merge Indicates a pull request not ready for merge, due to either quality or timing. owlbot:ignore instruct owl-bot to ignore a PR labels May 17, 2024
@BenWhitehead BenWhitehead requested a review from a team as a code owner May 17, 2024 17:56
@product-auto-label product-auto-label bot added size: l Pull request size is large. api: storage Issues related to the googleapis/java-storage API. labels May 17, 2024
@BenWhitehead BenWhitehead removed the do not merge Indicates a pull request not ready for merge, due to either quality or timing. label May 22, 2024
Copy link
Contributor

@JesseLovelace JesseLovelace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@BenWhitehead BenWhitehead merged commit 498fd0b into main May 30, 2024
23 checks passed
@BenWhitehead BenWhitehead deleted the java21-reduce-io-pinning branch May 30, 2024 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: storage Issues related to the googleapis/java-storage API. owlbot:ignore instruct owl-bot to ignore a PR size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants