From 62abafaa73f34347a14daf613189467a2e2c0c0c Mon Sep 17 00:00:00 2001 From: Matt Pavlovich Date: Sat, 14 Nov 2026 12:53:48 -0600 Subject: [PATCH] Virtual Thread cleanups: one thread per task runner and client-side fixes One long-lived virtual thread per TaskRunner that parks between work, replacing the thread per wakeup submission, with termination logged on unexpected errors. No-arg shutdown() waits for the running iteration, matching the pooled and dedicated runners. Client-side fixes: load the java21 classes through this artifact's own class loader so client factories work without setThreadClassLoader, honor the system property in task runner selection the same as in executor setup, create the failover reconnect task on first use so the virtual thread setting reaches it, propagate the connection factory flag to the failover transport, and keep the task runner settings across serialization and JNDI. Tests: task runner behavior (thread identity, wakeup delivery, shutdown bounds and contract, failure logging, thread release) run through both the factory flag and the system property, the client configuration precedence matrix, session dispatch and failover reconnect on both thread types. --- .../activemq/ActiveMQConnectionFactory.java | 14 + .../activemq/thread/TaskRunnerFactory.java | 21 +- .../transport/failover/FailoverTransport.java | 108 ++++- .../thread/VirtualThreadTaskRunner.java | 172 ++++---- .../ActiveMQConnectionFactoryTest.java | 21 + .../VirtualThreadConfigurationMatrixTest.java | 189 ++++++++ .../VirtualThreadSessionTaskRunnerTest.java | 318 +++++++++++++ .../thread/VirtualThreadTaskRunnerTest.java | 417 ++++++++++++++++++ 8 files changed, 1142 insertions(+), 118 deletions(-) create mode 100644 activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadConfigurationMatrixTest.java create mode 100644 activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadSessionTaskRunnerTest.java create mode 100644 activemq-unit-tests/src/test/java21/org/apache/activemq/thread/VirtualThreadTaskRunnerTest.java diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnectionFactory.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnectionFactory.java index 93cfb20d309..4eb75fc798a 100644 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnectionFactory.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnectionFactory.java @@ -43,6 +43,7 @@ import org.apache.activemq.transport.Transport; import org.apache.activemq.transport.TransportFactory; import org.apache.activemq.transport.TransportListener; +import org.apache.activemq.transport.failover.FailoverTransport; import org.apache.activemq.util.*; import org.apache.activemq.util.URISupport.CompositeData; import org.slf4j.Logger; @@ -392,6 +393,17 @@ protected ActiveMQConnection createActiveMQConnection(String userName, String pa configureConnection(connection); + // The failover reconnect task is created on first use, so the connection level + // virtual thread preference has to reach the transport before it starts. A value + // set on the failover URI itself takes precedence and is left alone, and a factory + // left at false says nothing, so the system property default still applies. + if (isUseVirtualThreadTaskRunner()) { + FailoverTransport failover = transport.narrow(FailoverTransport.class); + if (failover != null && failover.getUseVirtualThreadTaskRunner() == null) { + failover.setUseVirtualThreadTaskRunner(true); + } + } + transport.start(); if (clientID != null) { @@ -906,6 +918,8 @@ public void populateProperties(Properties props) { props.setProperty("transactedIndividualAck", Boolean.toString(isTransactedIndividualAck())); props.setProperty("nonBlockingRedelivery", Boolean.toString(isNonBlockingRedelivery())); props.setProperty("maxThreadPoolSize", Integer.toString(getMaxThreadPoolSize())); + props.setProperty("useDedicatedTaskRunner", Boolean.toString(isUseDedicatedTaskRunner())); + props.setProperty("useVirtualThreadTaskRunner", Boolean.toString(isUseVirtualThreadTaskRunner())); props.setProperty("nestedMapAndListEnabled", Boolean.toString(isNestedMapAndListEnabled())); props.setProperty("consumerFailoverRedeliveryWaitPeriod", Long.toString(getConsumerFailoverRedeliveryWaitPeriod())); props.setProperty("rmIdFromConnectionId", Boolean.toString(isRmIdFromConnectionId())); diff --git a/activemq-client/src/main/java/org/apache/activemq/thread/TaskRunnerFactory.java b/activemq-client/src/main/java/org/apache/activemq/thread/TaskRunnerFactory.java index 28f834efcbe..904f4b64d71 100644 --- a/activemq-client/src/main/java/org/apache/activemq/thread/TaskRunnerFactory.java +++ b/activemq-client/src/main/java/org/apache/activemq/thread/TaskRunnerFactory.java @@ -58,6 +58,9 @@ public class TaskRunnerFactory implements Executor { private final AtomicBoolean initDone = new AtomicBoolean(false); private int maxThreadPoolSize = getDefaultMaximumPoolSize(); private RejectedExecutionHandler rejectedTaskHandler = null; + /** system property that turns on virtual thread task runners for every factory that is not told otherwise */ + public static final String USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY = "org.apache.activemq.UseVirtualThreadTaskRunner"; + private ClassLoader threadClassLoader; // [AMQ-9394] Virtual Thread support @@ -106,7 +109,7 @@ public void init() { synchronized(this) { //need to recheck if initDone is true under the lock if (!initDone.get()) { - if (virtualThreadTaskRunner || "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.UseVirtualThreadTaskRunner")) ) { + if (virtualThreadTaskRunner || isVirtualThreadTaskRunnerDefault()) { executorRef.compareAndSet(null, createVirtualThreadExecutor()); } else if (dedicatedTaskRunner || "true".equalsIgnoreCase(System.getProperty("org.apache.activemq.UseDedicatedTaskRunner"))) { executorRef.set(null); @@ -172,7 +175,9 @@ public TaskRunner createTaskRunner(Task task, String name) { init(); ExecutorService executor = executorRef.get(); if (executor != null) { - if(isVirtualThreadTaskRunner()) { + // match init(): the system property enables virtual threads for + // factories that are not configured otherwise + if (isVirtualThreadTaskRunner() || isVirtualThreadTaskRunnerDefault()) { return createVirtualThreadTaskRunner(executor, task, maxIterationsPerRun); } else { return new PooledTaskRunner(executor, task, maxIterationsPerRun); @@ -239,11 +244,14 @@ public void uncaughtException(final Thread t, final Throwable e) { return rc; } + // The JDK 21 classes ship in this artifact under META-INF/versions/21, so they are + // loaded through this class's own loader. threadClassLoader is the context loader + // for spawned threads and is unset on the client, where it would mean the bootstrap loader. protected ExecutorService createVirtualThreadExecutor() { assertJDK21VirtualThreadSupport(); try { - Class virtualThreadExecutorClass = Class.forName("org.apache.activemq.thread.VirtualThreadExecutor", false, threadClassLoader); + Class virtualThreadExecutorClass = Class.forName("org.apache.activemq.thread.VirtualThreadExecutor", false, TaskRunnerFactory.class.getClassLoader()); Method method = virtualThreadExecutorClass.getMethod("createVirtualThreadExecutorService", String.class, AtomicLong.class, Logger.class); Object result = method.invoke(null, name, id, LOG); if(!ExecutorService.class.isAssignableFrom(result.getClass())) { @@ -261,7 +269,7 @@ protected TaskRunner createVirtualThreadTaskRunner(Executor executor, Task task, assertJDK21VirtualThreadSupport(); try { - Class virtualThreadTaskRunnerClass = Class.forName("org.apache.activemq.thread.VirtualThreadTaskRunner", false, threadClassLoader); + Class virtualThreadTaskRunnerClass = Class.forName("org.apache.activemq.thread.VirtualThreadTaskRunner", false, TaskRunnerFactory.class.getClassLoader()); Constructor constructor = virtualThreadTaskRunnerClass.getConstructor(Executor.class, Task.class, Integer.TYPE); Object result = constructor.newInstance(executor, task, maxIterations); if(!TaskRunner.class.isAssignableFrom(result.getClass())) { @@ -274,6 +282,11 @@ protected TaskRunner createVirtualThreadTaskRunner(Executor executor, Task task, } } + /** the process wide default from {@link #USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY} */ + public static boolean isVirtualThreadTaskRunnerDefault() { + return "true".equalsIgnoreCase(System.getProperty(USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY)); + } + private void assertJDK21VirtualThreadSupport() { if(!(Runtime.version().feature() >= 21)) { LOG.error("Virtual Thread support requires JDK 21 or higher"); diff --git a/activemq-client/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java b/activemq-client/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java index 5be8cf3b5ce..c3186076fbe 100644 --- a/activemq-client/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java +++ b/activemq-client/src/main/java/org/apache/activemq/transport/failover/FailoverTransport.java @@ -92,8 +92,25 @@ public class FailoverTransport implements CompositeTransport { private URI connectedTransportURI; private URI failedConnectTransportURI; private final AtomicReference connectedTransport = new AtomicReference(); - private final TaskRunnerFactory reconnectTaskFactory; - private final TaskRunner reconnectTask; + private final Object reconnectTaskMutex = new Object(); + private TaskRunnerFactory reconnectTaskFactory; + private volatile TaskRunner reconnectTask; + // returned for wakeups that arrive after stop() so no new task is created + private static final TaskRunner DISPOSED_TASK = new TaskRunner() { + @Override + public void wakeup() { + } + + @Override + public void shutdown(long timeout) { + } + + @Override + public void shutdown() { + } + }; + // null until set by a failover URI option; the connection factory fills it in only then + private Boolean useVirtualThreadTaskRunner; private volatile boolean started; private long initialReconnectDelay = DEFAULT_INITIAL_RECONNECT_DELAY; private long maxReconnectDelay = 1000 * 30; @@ -140,10 +157,29 @@ public FailoverTransport() { public FailoverTransport(SslContext sslContext) { this.sslContext = sslContext; stateTracker.setTrackTransactions(true); - // Setup a task that is used to reconnect the a connection async. - reconnectTaskFactory = new TaskRunnerFactory(); - reconnectTaskFactory.init(); - reconnectTask = reconnectTaskFactory.createTaskRunner(new Task() { + } + + /** + * The reconnect task is built on first use rather than in the constructor so + * that URI options and the owning connection factory can decide whether it + * runs on a virtual thread before any thread is created. + */ + private TaskRunner getReconnectTask() { + TaskRunner task = reconnectTask; + if (task == null) { + synchronized (reconnectTaskMutex) { + task = reconnectTask; + if (task == null) { + // stop() sets disposed before it takes this mutex for its + // shutdown pass, so a task created here after that pass + // would never be shut down + if (disposed) { + return DISPOSED_TASK; + } + reconnectTaskFactory = new TaskRunnerFactory("ActiveMQ Task", Thread.NORM_PRIORITY, true, 1000, + false, isUseVirtualThreadTaskRunner()); + reconnectTaskFactory.init(); + task = reconnectTaskFactory.createTaskRunner(new Task() { @Override public boolean iterate() { boolean result = false; @@ -162,10 +198,7 @@ public boolean iterate() { if (priorityBackup && !connectedToPriority) { try { doDelay(); - if (reconnectTask == null) { - return true; - } - reconnectTask.wakeup(); + getReconnectTask().wakeup(); } catch (InterruptedException e) { LOG.debug("Reconnect task has been interrupted.", e); } @@ -174,10 +207,7 @@ public boolean iterate() { // build backups on the next iteration buildBackup = true; try { - if (reconnectTask == null) { - return true; - } - reconnectTask.wakeup(); + getReconnectTask().wakeup(); } catch (InterruptedException e) { LOG.debug("Reconnect task has been interrupted.", e); } @@ -186,6 +216,11 @@ public boolean iterate() { } }, "ActiveMQ Failover Worker: " + System.identityHashCode(this)); + reconnectTask = task; + } + } + } + return task; } private void processCommand(Object incoming) { @@ -298,7 +333,7 @@ public final void handleTransportFailure(Transport failed, IOException e) throws transportListener.transportInterupted(); } - reconnectTask.wakeup(); + getReconnectTask().wakeup(); } else if (!isDisposed()) { propagateFailureToExceptionListener(e); } @@ -408,8 +443,20 @@ public void stop() throws Exception { sleepMutex.notifyAll(); } } finally { - reconnectTask.shutdown(); - reconnectTaskFactory.shutdownNow(); + // Take the references under the mutex but shut down outside it: shutdown() + // waits for a running iteration, and that iteration may call getReconnectTask() + TaskRunner taskToStop; + TaskRunnerFactory factoryToStop; + synchronized (reconnectTaskMutex) { + taskToStop = reconnectTask; + factoryToStop = reconnectTaskFactory; + } + if (taskToStop != null) { + taskToStop.shutdown(); + } + if (factoryToStop != null) { + factoryToStop.shutdownNow(); + } } synchronized(backupMutex) { @@ -796,7 +843,7 @@ public void reconnect(boolean rebalance) { } LOG.debug("Waking up reconnect task"); try { - reconnectTask.wakeup(); + getReconnectTask().wakeup(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } @@ -1449,6 +1496,31 @@ private URI addExtraQueryOptions(URI uri) { return uri; } + /** + * The effective setting: an explicit value from the URI or the connection + * factory, otherwise the process wide default from the + * {@code org.apache.activemq.UseVirtualThreadTaskRunner} system property. + */ + public boolean isUseVirtualThreadTaskRunner() { + return useVirtualThreadTaskRunner != null ? useVirtualThreadTaskRunner : TaskRunnerFactory.isVirtualThreadTaskRunnerDefault(); + } + + /** the raw setting, null when neither a URI option nor the connection factory has set it */ + public Boolean getUseVirtualThreadTaskRunner() { + return useVirtualThreadTaskRunner; + } + + /** + * Run the reconnect task on a virtual thread (JDK 21+). A value given as a + * failover URI option is final; the owning connection factory turns this on + * when its own useVirtualThreadTaskRunner is set and the URI said nothing; + * left unset, the system property default applies. Takes effect only before + * the transport starts. + */ + public void setUseVirtualThreadTaskRunner(Boolean useVirtualThreadTaskRunner) { + this.useVirtualThreadTaskRunner = useVirtualThreadTaskRunner; + } + public void setNestedExtraQueryOptions(String nestedExtraQueryOptions) { this.nestedExtraQueryOptions = nestedExtraQueryOptions; } diff --git a/activemq-client/src/main/java21/org/apache/activemq/thread/VirtualThreadTaskRunner.java b/activemq-client/src/main/java21/org/apache/activemq/thread/VirtualThreadTaskRunner.java index 0b58657e7ea..4ba3ee86471 100644 --- a/activemq-client/src/main/java21/org/apache/activemq/thread/VirtualThreadTaskRunner.java +++ b/activemq-client/src/main/java21/org/apache/activemq/thread/VirtualThreadTaskRunner.java @@ -18,33 +18,44 @@ import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; -import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +/** + * TaskRunner that runs the task loop on one long-lived virtual thread. + * The thread parks when the task has no work and wakeup() unparks it. + * + * Creating a virtual thread per wakeup causes high thread creation rates + * for tasks that go idle every few iterations, such as dispatch queues + * with rapidly acking consumers. One thread per runner avoids the creation cost. + */ class VirtualThreadTaskRunner implements TaskRunner { private static final Logger LOG = LoggerFactory.getLogger(VirtualThreadTaskRunner.class); + private final int maxIterationsPerRun; - private final Executor executor; private final Task task; - private final Lock taskRunnerLock = new ReentrantLock(); - private final Runnable runable; - private final Condition runnableCondition; - private boolean queued; - private boolean shutdown; - private boolean iterating; - // [AMQ-9394] TODO: Remove references to the running thread where possible + private final ReentrantLock lock = new ReentrantLock(); + private final Condition wakeupCondition = lock.newCondition(); + private final Condition terminationCondition = lock.newCondition(); + // wakeup() sets this with a CAS and only the CAS winner takes the lock + // to signal. The loop clears it before calling iterate(), so a wakeup + // that arrives after iterate() returns false is seen before the park. + private final AtomicBoolean pending = new AtomicBoolean(); + private volatile boolean shutdown; + private boolean terminated; private volatile Thread runningThread; public VirtualThreadTaskRunner(Executor executor, final Task task, int maxIterationsPerRun) { - this.executor = executor; - this.maxIterationsPerRun = maxIterationsPerRun; this.task = task; - runable = new Runnable() { + this.maxIterationsPerRun = maxIterationsPerRun; + // The executor starts a virtual thread per submission. This is the + // only submission for this runner. + executor.execute(new Runnable() { @Override public void run() { runningThread = Thread.currentThread(); @@ -55,65 +66,46 @@ public void run() { runningThread = null; } } - }; - this.runnableCondition = taskRunnerLock.newCondition(); + }); } - /** - * We Expect MANY wakeup calls on the same TaskRunner. - */ @Override public void wakeup() throws InterruptedException { - taskRunnerLock.lock(); - try { - // When we get in here, we make some assumptions of state: - // queued=false, iterating=false: wakeup() has not be called and - // therefore task is not executing. - // queued=true, iterating=false: wakeup() was called but, task - // execution has not started yet - // queued=false, iterating=true : wakeup() was called, which caused - // task execution to start. - // queued=true, iterating=true : wakeup() called after task - // execution was started. - - if (queued || shutdown) { - return; - } - - queued = true; - - // The runTask() method will do this for me once we are done - // iterating. - if (!iterating) { - executor.execute(runable); + if (shutdown) { + return; + } + if (pending.compareAndSet(false, true)) { + lock.lock(); + try { + wakeupCondition.signal(); + } finally { + lock.unlock(); } - } finally { - taskRunnerLock.unlock(); } } - /** - * shut down the task - * - * @throws InterruptedException - */ @Override public void shutdown(long timeout) throws InterruptedException { LOG.trace("Shutdown timeout: {} task: {}", timeout, task); - taskRunnerLock.lock(); + lock.lock(); try { shutdown = true; - // the check on the thread is done - // because a call to iterate can result in - // shutDown() being called, which would wait forever - // waiting for iterating to finish - if (runningThread != Thread.currentThread()) { - if (iterating) { - runnableCondition.await(timeout, TimeUnit.MILLISECONDS); + pending.set(true); + wakeupCondition.signal(); + // skip the wait when the task thread calls shutdown on itself + if (runningThread != Thread.currentThread() && !terminated) { + if (timeout > 0) { + terminationCondition.await(timeout, TimeUnit.MILLISECONDS); + } else { + // zero waits for the loop to exit, matching Object.wait(0) + // in the other task runners + while (!terminated) { + terminationCondition.await(); + } } } } finally { - taskRunnerLock.unlock(); + lock.unlock(); } } @@ -123,53 +115,41 @@ public void shutdown() throws InterruptedException { } final void runTask() { - - taskRunnerLock.lock(); try { - queued = false; - if (shutdown) { - iterating = false; - runnableCondition.signalAll(); - return; - } - iterating = true; - } finally { - taskRunnerLock.unlock(); - } - - // Don't synchronize while we are iterating so that - // multiple wakeup() calls can be executed concurrently. - boolean done = false; - try { - for (int i = 0; i < maxIterationsPerRun; i++) { - LOG.trace("Running task iteration {} - {}", i, task); - if (!task.iterate()) { - done = true; - break; + int sinceYield = 0; + while (!shutdown) { + // clear before iterate() so a wakeup during the call leaves + // the flag set and the loop runs again instead of parking + pending.set(false); + if (task.iterate()) { + if (++sinceYield >= maxIterationsPerRun) { + sinceYield = 0; + Thread.yield(); + } + continue; + } + sinceYield = 0; + lock.lock(); + try { + while (!pending.get() && !shutdown) { + wakeupCondition.await(); + } + } finally { + lock.unlock(); } } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (Throwable t) { + LOG.error("Virtual Thread task runner terminating on unexpected error: {}", task, t); + throw t; } finally { - taskRunnerLock.lock(); + lock.lock(); try { - iterating = false; - runnableCondition.signalAll(); - if (shutdown) { - queued = false; - runnableCondition.signalAll(); - } else { - // If we could not iterate all the items - // then we need to re-queue. - if (!done) { - queued = true; - } - - if (queued) { - executor.execute(runable); - } - } - + terminated = true; + terminationCondition.signalAll(); } finally { - taskRunnerLock.unlock(); + lock.unlock(); } } } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java index 42a154739a5..9088feb9297 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java @@ -215,6 +215,27 @@ public void testFactorySerializable() throws Exception { assertEquals(cf.getClientID(), clientID); } + public void testTaskRunnerSettingsSurviveSerialization() throws Exception { + // Serialization and JNDI both go through populateProperties/buildFromProperties, + // so a setting missing from populateProperties is silently reset on the way back + var cf = new ActiveMQConnectionFactory(); + cf.setUseDedicatedTaskRunner(true); + cf.setUseVirtualThreadTaskRunner(true); + cf.setMaxThreadPoolSize(7); + + var bytesOut = new ByteArrayOutputStream(); + try (var objectsOut = new ObjectOutputStream(bytesOut)) { + objectsOut.writeObject(cf); + } + try (var objectsIn = new ObjectInputStream(new ByteArrayInputStream(bytesOut.toByteArray()))) { + cf = (ActiveMQConnectionFactory) objectsIn.readObject(); + } + + assertTrue("useDedicatedTaskRunner lost", cf.isUseDedicatedTaskRunner()); + assertTrue("useVirtualThreadTaskRunner lost", cf.isUseVirtualThreadTaskRunner()); + assertEquals(7, cf.getMaxThreadPoolSize()); + } + public void testSetExceptionListener() throws Exception { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); connection = (ActiveMQConnection)cf.createConnection(); diff --git a/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadConfigurationMatrixTest.java b/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadConfigurationMatrixTest.java new file mode 100644 index 00000000000..d5a0aaf4b48 --- /dev/null +++ b/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadConfigurationMatrixTest.java @@ -0,0 +1,189 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.test.annotations.ParallelTest; +import org.apache.activemq.thread.TaskRunnerFactory; +import org.apache.activemq.transport.failover.FailoverTransport; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +/** + * Every way of asking for virtual thread task runners, combined, with the + * resulting field on each object that acts on the answer: + *
    + *
  • the connection factory, set by setter or by a {@code jms.} URL option, + * last write wins;
  • + *
  • the connection, which copies the factory;
  • + *
  • the failover transport, where a URI value is final, a factory {@code true} + * fills a gap, and nothing at all falls back to the system property;
  • + *
  • the two task runner factories, whose executors also honour the system + * property on their own.
  • + *
+ */ +@Category(ParallelTest.class) +@RunWith(Parameterized.class) +public class VirtualThreadConfigurationMatrixTest { + + /** how the connection factory flag is set */ + enum FactorySetting { + UNSET, SETTER_TRUE, SETTER_FALSE, URI_TRUE, URI_FALSE, URI_TRUE_THEN_SETTER_FALSE + } + + private static final String PROPERTY = TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY; + private static BrokerService broker; + + @Parameterized.Parameters(name = "property={0} factory={1} transportUri={2}") + public static Collection matrix() { + List rows = new ArrayList<>(); + for (boolean property : new boolean[] {false, true}) { + for (FactorySetting factory : FactorySetting.values()) { + for (Boolean transportUri : new Boolean[] {null, Boolean.TRUE, Boolean.FALSE}) { + rows.add(new Object[] {property, factory, transportUri}); + } + } + } + return rows; + } + + @Parameterized.Parameter(0) + public boolean property; + + @Parameterized.Parameter(1) + public FactorySetting factory; + + @Parameterized.Parameter(2) + public Boolean transportUri; + + private String previousProperty; + + @BeforeClass + public static void startBroker() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.start(); + broker.waitUntilStarted(); + } + + @AfterClass + public static void stopBroker() throws Exception { + broker.stop(); + broker.waitUntilStopped(); + } + + @Before + public void setProperty() { + previousProperty = System.getProperty(PROPERTY); + if (property) { + System.setProperty(PROPERTY, "true"); + } else { + System.clearProperty(PROPERTY); + } + } + + @After + public void restoreProperty() { + if (previousProperty == null) { + System.clearProperty(PROPERTY); + } else { + System.setProperty(PROPERTY, previousProperty); + } + } + + @Test(timeout = 30000) + public void fieldsOnEveryObjectFollowThePrecedence() throws Exception { + var url = new StringBuilder("failover:(vm://localhost?create=false)?initialReconnectDelay=10&maxReconnectDelay=100"); + if (factory == FactorySetting.URI_TRUE || factory == FactorySetting.URI_TRUE_THEN_SETTER_FALSE) { + url.append("&jms.useVirtualThreadTaskRunner=true"); + } else if (factory == FactorySetting.URI_FALSE) { + url.append("&jms.useVirtualThreadTaskRunner=false"); + } + if (transportUri != null) { + url.append("&useVirtualThreadTaskRunner=").append(transportUri); + } + + var connectionFactory = new ActiveMQConnectionFactory(url.toString()); + if (factory == FactorySetting.SETTER_TRUE) { + connectionFactory.setUseVirtualThreadTaskRunner(true); + } else if (factory == FactorySetting.SETTER_FALSE || factory == FactorySetting.URI_TRUE_THEN_SETTER_FALSE) { + connectionFactory.setUseVirtualThreadTaskRunner(false); + } + + boolean expectedFactory = factory == FactorySetting.SETTER_TRUE || factory == FactorySetting.URI_TRUE; + assertEquals("connection factory field", expectedFactory, connectionFactory.isUseVirtualThreadTaskRunner()); + + try (var connection = (ActiveMQConnection) connectionFactory.createConnection()) { + connection.start(); + assertEquals("connection field copies the factory", expectedFactory, connection.isUseVirtualThreadTaskRunner()); + + // the transport: a URI value is final, a factory true fills a gap, otherwise the property + var failover = connection.getTransport().narrow(FailoverTransport.class); + Boolean expectedRecorded = transportUri != null ? transportUri : (expectedFactory ? Boolean.TRUE : null); + boolean expectedTransport = transportUri != null ? transportUri : (expectedFactory || property); + assertEquals("value recorded on the transport", expectedRecorded, failover.getUseVirtualThreadTaskRunner()); + assertEquals("effective transport setting", expectedTransport, failover.isUseVirtualThreadTaskRunner()); + + // start() creates the reconnect runner, so its factory carries the transport's answer + var reconnectFactory = (TaskRunnerFactory) field(FailoverTransport.class, "reconnectTaskFactory").get(failover); + assertNotNull("reconnect task factory created on start", reconnectFactory); + assertEquals("reconnect factory flag", expectedTransport, reconnectFactory.isVirtualThreadTaskRunner()); + // TaskRunnerFactory.init() applies the system property on its own, so the executor is + // virtual whenever either the flag or the property says so + assertEquals("reconnect executor", expectedTransport || property, runsOnVirtualThread(reconnectFactory.getExecutor())); + + var sessionFactory = connection.getSessionTaskRunner(); + sessionFactory.init(); + assertEquals("session factory flag", expectedFactory, sessionFactory.isVirtualThreadTaskRunner()); + assertEquals("session executor", expectedFactory || property, runsOnVirtualThread(sessionFactory.getExecutor())); + } + } + + /** + * Asks the executor itself: a task submitted to it reports whether it ran on + * a virtual thread. Thread.isVirtual() is read reflectively because this + * module compiles its tests for an older release. + */ + private static boolean runsOnVirtualThread(ExecutorService executor) throws Exception { + assertNotNull("executor initialised", executor); + return executor.submit(() -> (Boolean) Thread.class.getMethod("isVirtual").invoke(Thread.currentThread())) + .get(10, TimeUnit.SECONDS); + } + + private static Field field(Class type, String name) throws NoSuchFieldException { + Field field = type.getDeclaredField(name); + field.setAccessible(true); + return field; + } +} diff --git a/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadSessionTaskRunnerTest.java b/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadSessionTaskRunnerTest.java new file mode 100644 index 00000000000..5fc3fb20f9a --- /dev/null +++ b/activemq-unit-tests/src/test/java21/org/apache/activemq/VirtualThreadSessionTaskRunnerTest.java @@ -0,0 +1,318 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.URI; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + +import jakarta.jms.Session; + +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.command.ActiveMQQueue; +import org.apache.activemq.test.annotations.ParallelTest; +import org.apache.activemq.thread.TaskRunnerFactory; +import org.apache.activemq.transport.TransportFactory; +import org.apache.activemq.transport.TransportListener; +import org.apache.activemq.transport.failover.FailoverTransport; +import org.apache.activemq.util.Wait; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +/** + * Client side coverage for {@code useVirtualThreadTaskRunner}. The parameter + * runs every test with the flag on and off: message listeners and failover + * reconnects run on the configured thread type, {@code Connection.stop()} + * blocks until an in-progress listener returns, and TaskRunner.shutdown() + * waits for a running iteration. + */ +@Category(ParallelTest.class) +@RunWith(Parameterized.class) +public class VirtualThreadSessionTaskRunnerTest { + + private static final ActiveMQQueue QUEUE = new ActiveMQQueue("TEST.VIRTUAL.THREAD"); + + @Parameterized.Parameters(name = "virtual={0}") + public static Object[] parameters() { + return new Object[] {true, false}; + } + + @Parameterized.Parameter + public boolean virtual; + + private BrokerService broker; + + @Before + public void setUp() throws Exception { + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.start(); + broker.waitUntilStarted(); + } + + @After + public void tearDown() throws Exception { + broker.stop(); + broker.waitUntilStopped(); + } + + @Test(timeout = 30000) + public void testListenerThreadMatchesSetting() throws Exception { + assertEquals(virtual, dispatchThreadIsVirtual(virtual)); + } + + /** + * JMS requires stop() to block until message listeners in progress have + * completed, however the session task is run. + */ + @Test(timeout = 30000) + public void testStopWaitsForInProgressListener() throws Exception { + var listenerEntered = new CountDownLatch(1); + var releaseListener = new CountDownLatch(1); + var stopReturned = new CountDownLatch(1); + var stopError = new AtomicReference(); + + try (var connection = factory(virtual).createConnection(); + var session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + var consumer = session.createConsumer(QUEUE); + var producer = session.createProducer(QUEUE)) { + + consumer.setMessageListener(message -> { + listenerEntered.countDown(); + try { + releaseListener.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + connection.start(); + producer.send(session.createTextMessage("block")); + assertTrue("listener never invoked", listenerEntered.await(10, TimeUnit.SECONDS)); + + var stopper = new Thread(() -> { + try { + connection.stop(); + } catch (Throwable t) { + stopError.set(t); + } finally { + stopReturned.countDown(); + } + }, "connection-stopper"); + stopper.start(); + + assertFalse("stop() returned while the listener was still running", + stopReturned.await(500, TimeUnit.MILLISECONDS)); + + releaseListener.countDown(); + assertTrue("stop() did not return after the listener completed", + stopReturned.await(10, TimeUnit.SECONDS)); + assertEquals(null, stopError.get()); + } + } + + /** + * TaskRunner.shutdown() must block until a running iterate() returns. + * A Condition.await(0) returns at once where Object.wait(0) waits, so + * the virtual runner has to special case zero. + */ + @Test(timeout = 30000) + public void testShutdownWaitsForRunningIteration() throws Exception { + var factory = new TaskRunnerFactory("ActiveMQ Test Task", Thread.NORM_PRIORITY, true, 1000, false, + virtual); + var iterating = new CountDownLatch(1); + var release = new CountDownLatch(1); + var shutdownReturned = new CountDownLatch(1); + try { + var runner = factory.createTaskRunner(() -> { + iterating.countDown(); + try { + release.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return false; + }, "blocking-task"); + runner.wakeup(); + assertTrue("iterate() never started", iterating.await(10, TimeUnit.SECONDS)); + + var stopper = new Thread(() -> { + try { + runner.shutdown(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + shutdownReturned.countDown(); + } + }, "runner-stopper"); + stopper.start(); + + assertFalse("shutdown() returned while iterate() was still running", + shutdownReturned.await(500, TimeUnit.MILLISECONDS)); + + release.countDown(); + assertTrue("shutdown() did not return after iterate() completed", + shutdownReturned.await(10, TimeUnit.SECONDS)); + } finally { + factory.shutdownNow(); + } + } + + /** + * The failover reconnect task is a separate TaskRunnerFactory. The factory + * level flag has to reach it, and transportResumed() is delivered from the + * reconnect task's thread, which is how the test observes it. + */ + @Test(timeout = 60000) + public void testFailoverReconnectThreadMatchesSetting() throws Exception { + assertEquals(virtual, failoverReconnectThreadIsVirtual(virtual, null)); + } + + /** a value on the failover URI wins over the factory flag in both directions */ + @Test(timeout = 60000) + public void testFailoverUriOptionOverridesFactoryFlag() throws Exception { + assertEquals(!virtual, failoverReconnectThreadIsVirtual(virtual, !virtual)); + } + + /** + * A transport built outside a connection factory has nothing pushing a + * value, so the system property is the default; a URI value still wins + * over the property. Independent of the class parameter. + */ + @Test(timeout = 30000) + public void testUnsetTransportFollowsSystemPropertyDefault() throws Exception { + var previous = System.getProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY); + System.setProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY, "true"); + try { + var unset = (FailoverTransport) TransportFactory.connect(new URI("failover:(vm://localhost?create=false)")).narrow(FailoverTransport.class); + assertEquals("nothing recorded", null, unset.getUseVirtualThreadTaskRunner()); + assertTrue("property default applies", unset.isUseVirtualThreadTaskRunner()); + + var uriFalse = (FailoverTransport) TransportFactory.connect(new URI("failover:(vm://localhost?create=false)?useVirtualThreadTaskRunner=false")).narrow(FailoverTransport.class); + assertFalse("URI false wins over the property", uriFalse.isUseVirtualThreadTaskRunner()); + } finally { + if (previous == null) { + System.clearProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY); + } else { + System.setProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY, previous); + } + } + } + + private boolean failoverReconnectThreadIsVirtual(boolean factoryFlag, Boolean transportOption) throws Exception { + var url = "failover:(vm://localhost?create=false)?initialReconnectDelay=10&maxReconnectDelay=100" + + "&jms.useVirtualThreadTaskRunner=" + factoryFlag + + (transportOption == null ? "" : "&useVirtualThreadTaskRunner=" + transportOption); + var expected = transportOption != null ? transportOption : factoryFlag; + // a factory left at false says nothing, so the transport records only true or the URI value + var recorded = transportOption != null ? transportOption : (factoryFlag ? Boolean.TRUE : null); + var resumed = new CountDownLatch(1); + var observedVirtual = new AtomicBoolean(); + + try (var connection = (ActiveMQConnection) new ActiveMQConnectionFactory(url).createConnection()) { + connection.start(); + var failover = connection.getTransport().narrow(FailoverTransport.class); + assertEquals(expected, failover.isUseVirtualThreadTaskRunner()); + assertEquals("what was recorded on the transport", recorded, failover.getUseVirtualThreadTaskRunner()); + + connection.addTransportListener(new TransportListener() { + @Override + public void onCommand(Object command) { + } + + @Override + public void onException(IOException error) { + } + + @Override + public void transportInterupted() { + } + + @Override + public void transportResumed() { + observedVirtual.set(isVirtual(Thread.currentThread())); + resumed.countDown(); + } + }); + + // Bounce the broker so failover has to reconnect + restartBroker(); + assertTrue("failover never reconnected", resumed.await(30, TimeUnit.SECONDS)); + } + return observedVirtual.get(); + } + + private void restartBroker() throws Exception { + broker.stop(); + broker.waitUntilStopped(); + broker = new BrokerService(); + broker.setPersistent(false); + broker.setUseJmx(false); + broker.start(); + broker.waitUntilStarted(); + } + + private boolean dispatchThreadIsVirtual(boolean useVirtualThreadTaskRunner) throws Exception { + var received = new CountDownLatch(1); + var observedVirtual = new AtomicBoolean(); + + try (var connection = factory(useVirtualThreadTaskRunner).createConnection(); + var session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + var consumer = session.createConsumer(QUEUE); + var producer = session.createProducer(QUEUE)) { + + consumer.setMessageListener(message -> { + observedVirtual.set(isVirtual(Thread.currentThread())); + received.countDown(); + }); + connection.start(); + producer.send(session.createTextMessage("probe")); + assertTrue("message not delivered", received.await(10, TimeUnit.SECONDS)); + assertTrue(Wait.waitFor(() -> received.getCount() == 0, 1000, 10)); + } + return observedVirtual.get(); + } + + /** + * Thread.isVirtual() is a JDK 21 API and this source root compiles at the + * module release, so it is read reflectively. The build only adds this + * source root on JDK 21 and later. + */ + private static boolean isVirtual(Thread thread) { + try { + return (Boolean) Thread.class.getMethod("isVirtual").invoke(thread); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException("Thread.isVirtual() unavailable; JDK 21+ required", e); + } + } + + private static ActiveMQConnectionFactory factory(boolean useVirtualThreadTaskRunner) { + return new ActiveMQConnectionFactory( + "vm://localhost?create=false&jms.useVirtualThreadTaskRunner=" + useVirtualThreadTaskRunner); + } +} diff --git a/activemq-unit-tests/src/test/java21/org/apache/activemq/thread/VirtualThreadTaskRunnerTest.java b/activemq-unit-tests/src/test/java21/org/apache/activemq/thread/VirtualThreadTaskRunnerTest.java new file mode 100644 index 00000000000..bb8db65f072 --- /dev/null +++ b/activemq-unit-tests/src/test/java21/org/apache/activemq/thread/VirtualThreadTaskRunnerTest.java @@ -0,0 +1,417 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.thread; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.lang.ref.WeakReference; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; +import java.util.function.BooleanSupplier; + +import org.apache.activemq.test.annotations.ParallelTest; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.Property; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +/** + * Behavior tests for the virtual thread TaskRunner: one thread per runner, + * wakeup delivery under contention, shutdown bounds and contract, failure + * logging and thread release. Runners come from TaskRunnerFactory, so the + * multi-release class loading is covered, and the parameter runs every test + * through both ways of enabling virtual threads: the factory flag and the + * system property. + */ +@Category(ParallelTest.class) +@RunWith(Parameterized.class) +public class VirtualThreadTaskRunnerTest { + + /** how the factory is told to use virtual threads */ + public enum Setup { + FACTORY_FLAG, SYSTEM_PROPERTY + } + + private static final String RUNNER_LOGGER = "org.apache.activemq.thread.VirtualThreadTaskRunner"; + private static final Thread.UncaughtExceptionHandler SWALLOW = (thread, error) -> { }; + + @Parameterized.Parameters(name = "{0}") + public static Object[] setups() { + return Setup.values(); + } + + @Parameterized.Parameter + public Setup setup; + + private TaskRunnerFactory factory; + private String previousProperty; + + @Before + public void setUp() { + previousProperty = System.getProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY); + if (setup == Setup.SYSTEM_PROPERTY) { + System.setProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY, "true"); + factory = new TaskRunnerFactory("VTTest", Thread.NORM_PRIORITY, true, 1000, false, false); + } else { + System.clearProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY); + factory = new TaskRunnerFactory("VTTest", Thread.NORM_PRIORITY, true, 1000, false, true); + } + } + + @After + public void tearDown() { + factory.shutdownNow(); + if (previousProperty == null) { + System.clearProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY); + } else { + System.setProperty(TaskRunnerFactory.USE_VIRTUAL_THREAD_TASK_RUNNER_PROPERTY, previousProperty); + } + } + + @Test(timeout = 30000) + public void testFactoryCreatesVirtualThreadRunner() throws Exception { + var virtual = new AtomicBoolean(); + var ran = new CountDownLatch(1); + var runner = factory.createTaskRunner(() -> { + virtual.set(isVirtual(Thread.currentThread())); + ran.countDown(); + return false; + }, "factory-check"); + try { + assertEquals("VirtualThreadTaskRunner", runner.getClass().getSimpleName()); + assertTrue(ran.await(5, TimeUnit.SECONDS)); + assertTrue("task should run on a virtual thread", virtual.get()); + } finally { + runner.shutdown(1000); + } + } + + @Test(timeout = 30000) + public void testRunsOnSingleLongLivedThread() throws Exception { + var threads = ConcurrentHashMap.newKeySet(); + var completed = new AtomicInteger(); + // returning false after every call parks the runner, so every + // wakeup is an idle to busy transition + var runner = factory.createTaskRunner(() -> { + threads.add(Thread.currentThread()); + completed.incrementAndGet(); + return false; + }, "single-thread"); + try { + for (var i = 0; i < 50; i++) { + var target = completed.get() + 1; + runner.wakeup(); + within(5000, () -> completed.get() >= target, "iteration " + target); + } + assertEquals("all iterations should run on one thread", 1, threads.size()); + assertTrue(isVirtual(threads.iterator().next())); + } finally { + runner.shutdown(1000); + } + } + + @Test(timeout = 30000) + public void testWakeupDuringStartup() throws Exception { + var ran = new CountDownLatch(1); + var runner = factory.createTaskRunner(() -> { + ran.countDown(); + return false; + }, "startup"); + try { + runner.wakeup(); + assertTrue(ran.await(5, TimeUnit.SECONDS)); + } finally { + runner.shutdown(1000); + } + } + + @Test(timeout = 60000) + public void testNoLostWakeupUnderConcurrentWakeups() throws Exception { + final var producerCount = 4; + final var wakeupsPerProducer = 5000; + var produced = new AtomicLong(); + var consumed = new AtomicLong(); + var runner = factory.createTaskRunner(() -> { + if (consumed.get() < produced.get()) { + consumed.incrementAndGet(); + return true; + } + return false; + }, "storm"); + try { + var producers = new Thread[producerCount]; + for (var p = 0; p < producerCount; p++) { + producers[p] = new Thread(() -> { + try { + for (var i = 0; i < wakeupsPerProducer; i++) { + produced.incrementAndGet(); + runner.wakeup(); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }, "storm-producer"); + producers[p].start(); + } + for (var producer : producers) { + producer.join(20000); + } + var expected = (long) producerCount * wakeupsPerProducer; + assertEquals(expected, produced.get()); + within(20000, () -> consumed.get() == expected, + "consumed " + consumed.get() + " of " + expected); + } finally { + runner.shutdown(1000); + } + } + + @Test(timeout = 30000) + public void testSelfShutdownDoesNotDeadlock() throws Exception { + var runnerRef = new AtomicReference(); + var armed = new AtomicBoolean(); + var selfShutdownReturned = new CountDownLatch(1); + var runner = factory.createTaskRunner(() -> { + if (!armed.get()) { + return false; + } + armed.set(false); + try { + runnerRef.get().shutdown(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + selfShutdownReturned.countDown(); + return false; + }, "self-shutdown"); + runnerRef.set(runner); + armed.set(true); + runner.wakeup(); + assertTrue("shutdown called from the task thread must return", + selfShutdownReturned.await(10, TimeUnit.SECONDS)); + var elapsed = timedShutdown(runner, 5000); + assertTrue("external shutdown after self shutdown took " + elapsed + "ms", elapsed < 2500); + } + + @Test(timeout = 30000) + public void testShutdownBoundedWhileBusy() throws Exception { + var iterations = new AtomicLong(); + var runner = factory.createTaskRunner(() -> { + iterations.incrementAndGet(); + return true; + }, "busy"); + within(5000, () -> iterations.get() > 0, "task to start"); + var elapsed = timedShutdown(runner, 10000); + assertTrue("shutdown of a busy runner took " + elapsed + "ms", elapsed < 5000); + assertStopped(iterations); + } + + @Test(timeout = 30000) + public void testShutdownNoArgWaitsForRunningIteration() throws Exception { + var iterating = new CountDownLatch(1); + var release = new CountDownLatch(1); + var shutdownReturned = new CountDownLatch(1); + var runner = factory.createTaskRunner(() -> { + iterating.countDown(); + try { + release.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + return false; + }, "blocking-noarg"); + assertTrue(iterating.await(5, TimeUnit.SECONDS)); + var stopper = new Thread(() -> { + try { + runner.shutdown(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + shutdownReturned.countDown(); + } + }, "noarg-stopper"); + stopper.start(); + // same contract as the pooled and dedicated runners: zero timeout + // waits for the running iteration + assertFalse("no-arg shutdown returned while iterate() was running", + shutdownReturned.await(500, TimeUnit.MILLISECONDS)); + release.countDown(); + assertTrue("no-arg shutdown did not return after iterate() completed", + shutdownReturned.await(10, TimeUnit.SECONDS)); + } + + @Test(timeout = 30000) + public void testNeverWokenRunnerShutsDownPromptly() throws Exception { + var runner = factory.createTaskRunner(() -> false, "never-woken"); + // give the parked thread time to start and park + Thread.sleep(100); + var elapsed = timedShutdown(runner, 5000); + assertTrue("shutdown of a parked runner took " + elapsed + "ms", elapsed < 2500); + } + + @Test(timeout = 30000) + public void testTaskFailureLogsErrorAndReleasesShutdown() throws Exception { + var appender = CapturingAppender.attach(); + try { + var thrown = new CountDownLatch(1); + var runner = factory.createTaskRunner(() -> { + Thread.currentThread().setUncaughtExceptionHandler(SWALLOW); + thrown.countDown(); + throw new RuntimeException("simulated task failure"); + }, "failing"); + assertTrue(thrown.await(5, TimeUnit.SECONDS)); + within(5000, () -> appender.contains("Virtual Thread task runner terminating on unexpected error"), + "termination error log"); + var elapsed = timedShutdown(runner, 5000); + assertTrue("shutdown after task failure took " + elapsed + "ms", elapsed < 2500); + // a wakeup after the runner died must be a silent no-op + runner.wakeup(); + } finally { + appender.detach(); + } + } + + @Test(timeout = 30000) + public void testInterruptTerminatesQuietly() throws Exception { + var appender = CapturingAppender.attach(); + try { + var taskThread = new AtomicReference(); + var runner = factory.createTaskRunner(() -> { + taskThread.set(Thread.currentThread()); + return false; + }, "interrupted"); + within(5000, () -> taskThread.get() != null, "task to start"); + taskThread.get().interrupt(); + var elapsed = timedShutdown(runner, 5000); + assertTrue("shutdown after interrupt took " + elapsed + "ms", elapsed < 2500); + assertTrue("interrupt must not log an error: " + appender.messages, + appender.messages.isEmpty()); + } finally { + appender.detach(); + } + } + + @Test(timeout = 30000) + public void testThreadReleasedAfterShutdown() throws Exception { + var taskThread = new AtomicReference(); + var runner = factory.createTaskRunner(() -> { + taskThread.set(Thread.currentThread()); + return false; + }, "release"); + within(5000, () -> taskThread.get() != null, "task to start"); + runner.shutdown(5000); + var weak = new WeakReference<>(taskThread.getAndSet(null)); + var deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10); + while (weak.get() != null) { + if (System.nanoTime() > deadline) { + fail("runner thread was not released after shutdown"); + } + System.gc(); + Thread.sleep(100); + } + } + + private static long timedShutdown(TaskRunner runner, long timeout) throws InterruptedException { + var start = System.nanoTime(); + runner.shutdown(timeout); + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); + } + + private static void assertStopped(AtomicLong iterations) throws InterruptedException { + var deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); + while (true) { + var snapshot = iterations.get(); + Thread.sleep(200); + if (iterations.get() == snapshot) { + return; + } + if (System.nanoTime() > deadline) { + fail("task kept iterating after shutdown"); + } + } + } + + private static void within(long millis, BooleanSupplier condition, String description) + throws InterruptedException { + var deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(millis); + while (!condition.getAsBoolean()) { + if (System.nanoTime() > deadline) { + fail("timed out waiting for " + description); + } + Thread.sleep(10); + } + } + + /** + * Thread.isVirtual() is a JDK 21 API and this source root compiles at the + * module release, so it is read reflectively. The build only adds this + * source root on JDK 21 and later. + */ + private static boolean isVirtual(Thread thread) { + try { + return (Boolean) Thread.class.getMethod("isVirtual").invoke(thread); + } catch (ReflectiveOperationException e) { + throw new IllegalStateException("Thread.isVirtual() unavailable; JDK 21+ required", e); + } + } + + private static final class CapturingAppender extends AbstractAppender { + + final List messages = new CopyOnWriteArrayList<>(); + + private CapturingAppender() { + super("vt-runner-capture", null, null, true, Property.EMPTY_ARRAY); + } + + static CapturingAppender attach() { + var appender = new CapturingAppender(); + appender.start(); + ((Logger) LogManager.getLogger(RUNNER_LOGGER)).addAppender(appender); + return appender; + } + + void detach() { + ((Logger) LogManager.getLogger(RUNNER_LOGGER)).removeAppender(this); + stop(); + } + + boolean contains(String fragment) { + return messages.stream().anyMatch(message -> message.contains(fragment)); + } + + @Override + public void append(LogEvent event) { + messages.add(event.getMessage().getFormattedMessage()); + } + } +}