Skip to content

feat: support virtual threads#51

Open
devingryu wants to merge 4 commits into
astubbs:masterfrom
devingryu:features/enable-virtual-threads
Open

feat: support virtual threads#51
devingryu wants to merge 4 commits into
astubbs:masterfrom
devingryu:features/enable-virtual-threads

Conversation

@devingryu
Copy link
Copy Markdown

Copy of confluentinc#908


Adds option to use Virtual Threads for worker pool. (See also: #896)

  • VT support: Added the useVirtualThreads option and execution logic to support Java 21+ Virtual Threads (Project Loom).
  • VT Pinning prevention: Replace synchronization method from synchronized blocks to ReentrantLock to prevent VT pinning.
  • ExecutorService Abstraction: Generalized the return type of setupWorkerPool to ExecutorService and improved queue management and backpressure logic accordingly.

Concerns

  1. Test Environment Compatibility
    The newly added VT-related tests require JDK 21+ and are configured to skip execution using Assumptions if 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.

  2. Concurrency in isPoolQueueLow
    When VT mode is enabled, we cannot directly query the queue size, so WorkerManager.numberRecordsOutForProcessing is used to calculate the load. Please review if this approach creates any potential concurrency issues in a multi-threaded environment.

  3. Defensive Logic
    To ensure the stability of the existing logic, if VT is disabled (!useVirtualThreads), setupWorkerPool is enforced to return a ThreadPoolExecutor (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 inherit AbstractParallelEoSStreamProcessor to use custom Executors.

Checklist

  • Documentation (if applicable)
  • Changelog

* 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.
Copilot AI review requested due to automatic review settings June 4, 2026 00:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 useVirtualThreads option with JVM capability validation.
  • Updates worker pool abstractions from ThreadPoolExecutor to ExecutorService and adds VT-specific behavior.
  • Replaces synchronized blocks/methods with ReentrantLock in 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants