feat: support virtual threads#51
Open
devingryu wants to merge 4 commits into
Open
Conversation
* Added boolean useVirtualThreads in ParallelConsumerOptions * Generalize workerThreadPool logics to handle Virtual Threads * Added tests
* prevents virtual threads pinning
* Bypasses `DynamicLoadFactor` in `getQueueTargetLoaded()` when using VT.
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds optional Virtual Thread (JDK 21+) execution for the worker pool and refactors several synchronization points from synchronized to ReentrantLock, along with new tests and release notes updates.
Changes:
- Introduces
useVirtualThreadsoption with JVM capability validation. - Updates worker pool abstractions from
ThreadPoolExecutortoExecutorServiceand adds VT-specific behavior. - Replaces
synchronizedblocks/methods withReentrantLockin metrics, producer transaction begin, load factor stepping, and supplier memoization.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| parallel-consumer-vertx/src/main/java/io/confluent/parallelconsumer/vertx/VertxParallelEoSStreamProcessor.java | Adapts override signature to ExecutorService to match new worker pool abstraction. |
| parallel-consumer-core/src/test/java/io/confluent/parallelconsumer/AbstractParallelEoSStreamProcessorConfigurationTest.java | Adds VT-focused tests and refactors module setup for tests. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/metrics/PCMetrics.java | Switches close/remove locking from synchronized to ReentrantLock. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/internal/ProducerManager.java | Replaces synchronized transaction-begin guard with an explicit lock. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/internal/ExternalEngine.java | Adapts override signature to ExecutorService. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/internal/DynamicLoadFactor.java | Refactors stepping synchronization to ReentrantLock. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/internal/AbstractParallelEoSStreamProcessor.java | Adds VT worker pool creation and generalizes worker pool type; introduces lock-based commit coordination and VT-safe pool stats/queue handling. |
| parallel-consumer-core/src/main/java/io/confluent/parallelconsumer/ParallelConsumerOptions.java | Adds useVirtualThreads flag + validation for VT support. |
| parallel-consumer-core/src/main/java/io/confluent/csid/utils/SupplierUtils.java | Refactors memoization locking from synchronized to ReentrantLock. |
| README.adoc | Notes the new VT feature and lock refactor in release notes. |
| CHANGELOG.adoc | Notes the new VT feature and lock refactor in changelog. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+304
to
+314
| workerThreadPool = SupplierUtils.memoize(() -> { | ||
| ExecutorService executor = setupWorkerPool(newOptions.getMaxConcurrency()); | ||
|
|
||
| if (!options.isUseVirtualThreads() && !(executor instanceof ThreadPoolExecutor)) { | ||
| throw new IllegalStateException( | ||
| "When Virtual Threads are disabled, the worker pool must be an instance of ThreadPoolExecutor. " + | ||
| "If you are using a custom ExecutorService, make sure it extends ThreadPoolExecutor." | ||
| ); | ||
| } | ||
| return executor; | ||
| }); |
Comment on lines
+361
to
+377
| protected ExecutorService setupWorkerPool(int poolSize) { | ||
| if (options.isUseVirtualThreads()) { | ||
| try { | ||
| // Thread.ofVirtual().name("pc-vt-").factory() | ||
| Object builder = Class.forName("java.lang.Thread").getMethod("ofVirtual").invoke(null); | ||
| Class<?> builderClass = Class.forName("java.lang.Thread$Builder"); | ||
| builderClass.getMethod("name", String.class, long.class).invoke(builder, "pc-vt-", 0); | ||
| ThreadFactory factory = (ThreadFactory) builderClass.getMethod("factory").invoke(builder); | ||
|
|
||
| // Executors.newThreadPerTaskExecutor(factory) | ||
| return (ExecutorService) java.util.concurrent.Executors.class | ||
| .getMethod("newThreadPerTaskExecutor", ThreadFactory.class) | ||
| .invoke(null, factory); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException("Virtual threads not supported on this JVM", e); | ||
| } | ||
| } |
Comment on lines
+473
to
485
| private void validateVirtualThreadsSupport() { | ||
| if (useVirtualThreads) { | ||
| try { | ||
| // Thread.ofVirtual(), Executors.newThreadPerTaskExecutor(ThreadFactory) | ||
| Class.forName("java.lang.Thread").getMethod("ofVirtual"); | ||
| java.util.concurrent.Executors.class.getMethod("newThreadPerTaskExecutor", java.util.concurrent.ThreadFactory.class); | ||
| } catch (NoSuchMethodException | ClassNotFoundException e) { | ||
| throw new UnsupportedOperationException( | ||
| "useVirtualThreads is enabled, but the current JVM does not support Virtual Threads or the required ExecutorService." | ||
| ); | ||
| } | ||
| } | ||
| } |
Comment on lines
+234
to
243
| public void removeMeter(Meter meter) { | ||
| lock.lock(); | ||
| try { | ||
| if (meter != null) { | ||
| removeMeter(meter.getId()); | ||
| } | ||
| } finally { | ||
| lock.unlock(); | ||
| } | ||
| } |
Comment on lines
+203
to
+212
| @Test | ||
| void testVirtualThreadActivationAndConcurrencyLimit() throws Exception { | ||
| Assumptions.assumeTrue(isVirtualThreadsSupported(), "JDK 21+ required for Virtual Threads"); | ||
|
|
||
| int maxConcurrency = 10; | ||
| var vtOptions = ParallelConsumerOptions.<String, String>builder() | ||
| .consumer(consumer) | ||
| .useVirtualThreads(true) | ||
| .maxConcurrency(maxConcurrency) | ||
| .build(); |
Comment on lines
+142
to
+149
| private boolean isVirtualThreadsSupported() { | ||
| try { | ||
| Class.forName("java.lang.Thread").getMethod("ofVirtual"); | ||
| return true; | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Copy of confluentinc#908
Adds option to use Virtual Threads for worker pool. (See also: #896)
useVirtualThreadsoption and execution logic to support Java 21+ Virtual Threads (Project Loom).synchronizedblocks toReentrantLockto prevent VT pinning.setupWorkerPooltoExecutorServiceand improved queue management and backpressure logic accordingly.Concerns
Test Environment Compatibility
The newly added VT-related tests require JDK 21+ and are configured to skip execution using
Assumptionsif the environment does not support it. It seems that current CI environment is based on JDK 17, if so the tests I added will be skipped in CI.Concurrency in
isPoolQueueLowWhen VT mode is enabled, we cannot directly query the queue size, so
WorkerManager.numberRecordsOutForProcessingis used to calculate the load. Please review if this approach creates any potential concurrency issues in a multi-threaded environment.Defensive Logic
To ensure the stability of the existing logic, if VT is disabled (
!useVirtualThreads),setupWorkerPoolis enforced to return aThreadPoolExecutor(throwing an exception otherwise), and type checks have been added. I am concerned this might introduce a breaking change (compatibility issue) for existing users who inheritAbstractParallelEoSStreamProcessorto use custom Executors.Checklist