You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2023/05/12 09:45:19 UTC

[tomcat] branch 10.1.x updated: Back-port virtual thread support

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 10.1.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/10.1.x by this push:
     new 3949154e2f Back-port virtual thread support
3949154e2f is described below

commit 3949154e2fab7e6ac40f67a8a708aeea1476a0f3
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri May 12 09:54:55 2023 +0100

    Back-port virtual thread support
---
 .../apache/catalina/core/LocalStrings.properties   |  2 +
 .../catalina/core/VirtualThreadExecutor.java       | 92 ++++++++++++++++++++++
 .../org/apache/tomcat/util/compat/Jre21Compat.java | 82 +++++++++++++++++++
 java/org/apache/tomcat/util/compat/JreCompat.java  | 41 +++++++++-
 .../tomcat/util/compat/LocalStrings.properties     |  6 +-
 webapps/docs/changelog.xml                         |  7 ++
 webapps/docs/config/executor.xml                   | 28 ++++++-
 7 files changed, 254 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/catalina/core/LocalStrings.properties b/java/org/apache/catalina/core/LocalStrings.properties
index dd7e15ee2d..07b88b476e 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -304,3 +304,5 @@ standardWrapper.waiting=Waiting for [{0}] instance(s) to be deallocated for Serv
 
 threadLocalLeakPreventionListener.containerEvent.error=Exception processing container event [{0}]
 threadLocalLeakPreventionListener.lifecycleEvent.error=Exception processing lifecycle event [{0}]
+
+virtualThreadExecutor.noVirtualThreads=Virtual threads require a minimum Java version of Java 21
\ No newline at end of file
diff --git a/java/org/apache/catalina/core/VirtualThreadExecutor.java b/java/org/apache/catalina/core/VirtualThreadExecutor.java
new file mode 100644
index 0000000000..dd8739a179
--- /dev/null
+++ b/java/org/apache/catalina/core/VirtualThreadExecutor.java
@@ -0,0 +1,92 @@
+/*
+ *  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.catalina.core;
+
+import org.apache.catalina.Executor;
+import org.apache.catalina.LifecycleException;
+import org.apache.catalina.LifecycleState;
+import org.apache.catalina.util.LifecycleMBeanBase;
+import org.apache.tomcat.util.compat.JreCompat;
+import org.apache.tomcat.util.res.StringManager;
+
+/**
+ * An executor that uses a new virtual thread for each task.
+ */
+public class VirtualThreadExecutor extends LifecycleMBeanBase implements Executor {
+
+    private static final StringManager sm = StringManager.getManager(VirtualThreadExecutor.class);
+
+    private final JreCompat jreCompat = JreCompat.getInstance();
+
+    private String name;
+    private Object threadBuilder;
+    private String namePrefix;
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+
+    public String getNamePrefix() {
+        return namePrefix;
+    }
+
+    public void setNamePrefix(String namePrefix) {
+        this.namePrefix = namePrefix;
+    }
+
+    @Override
+    public void execute(Runnable command) {
+        JreCompat.getInstance().threadBuilderStart(threadBuilder, command);
+    }
+
+
+    @Override
+    protected void initInternal() throws LifecycleException {
+        super.initInternal();
+        if (!JreCompat.isJre21Available()) {
+            throw new LifecycleException(sm.getString("virtualThreadExecutor.noVirtualThreads"));
+        }
+    }
+
+    @Override
+    protected void startInternal() throws LifecycleException {
+        threadBuilder = jreCompat.createVirtualThreadBuilder(getNamePrefix());
+        setState(LifecycleState.STARTING);
+    }
+
+    @Override
+    protected void stopInternal() throws LifecycleException {
+        threadBuilder = null;
+        setState(LifecycleState.STOPPING);
+    }
+
+    @Override
+    protected String getDomainInternal() {
+        // No way to navigate to Engine. Needs to have domain set.
+        return null;
+    }
+
+    @Override
+    protected String getObjectNameKeyProperties() {
+        return "type=Executor,name=" + getName();
+    }
+}
\ No newline at end of file
diff --git a/java/org/apache/tomcat/util/compat/Jre21Compat.java b/java/org/apache/tomcat/util/compat/Jre21Compat.java
new file mode 100644
index 0000000000..d06c8519a7
--- /dev/null
+++ b/java/org/apache/tomcat/util/compat/Jre21Compat.java
@@ -0,0 +1,82 @@
+/*
+ *  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.tomcat.util.compat;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.apache.juli.logging.Log;
+import org.apache.juli.logging.LogFactory;
+import org.apache.tomcat.util.res.StringManager;
+
+public class Jre21Compat extends Jre19Compat {
+
+    private static final Log log = LogFactory.getLog(Jre21Compat.class);
+    private static final StringManager sm = StringManager.getManager(Jre21Compat.class);
+
+    private static final Method nameMethod;
+    private static final Method startMethod;
+    private static final Method ofVirtualMethod;
+
+
+    static {
+        Class<?> c1 = null;
+        Method m1 = null;
+        Method m2 = null;
+        Method m3 = null;
+
+        try {
+            c1 = Class.forName("java.lang.Thread$Builder");
+            m1 = c1.getMethod("name", String.class, long.class);
+            m2 = c1.getMethod("start", Runnable.class);
+            m3 = Thread.class.getMethod("ofVirtual", (Class<?>[]) null);
+        } catch (ClassNotFoundException e) {
+            // Must be pre-Java 21
+            log.debug(sm.getString("jre21Compat.javaPre21"), e);
+        } catch (ReflectiveOperationException e) {
+            // Should never happen
+            log.error(sm.getString("jre21Compat.unexpected"), e);
+        }
+        nameMethod = m1;
+        startMethod = m2;
+        ofVirtualMethod = m3;
+    }
+
+    static boolean isSupported() {
+        return ofVirtualMethod != null;
+    }
+
+    @Override
+    public Object createVirtualThreadBuilder(String name) {
+        try {
+            Object threadBuilder = ofVirtualMethod.invoke(null, (Object[]) null);
+            nameMethod.invoke(threadBuilder, name, Long.valueOf(0));
+            return threadBuilder;
+        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+
+    @Override
+    public void threadBuilderStart(Object threadBuilder, Runnable command) {
+        try {
+            startMethod.invoke(threadBuilder, command);
+        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
+            throw new UnsupportedOperationException(e);
+        }
+    }
+}
diff --git a/java/org/apache/tomcat/util/compat/JreCompat.java b/java/org/apache/tomcat/util/compat/JreCompat.java
index e1f442995a..79d6a15ed9 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -34,6 +34,7 @@ public class JreCompat {
     private static final boolean graalAvailable;
     private static final boolean jre16Available;
     private static final boolean jre19Available;
+    private static final boolean jre21Available;
     private static final StringManager sm = StringManager.getManager(JreCompat.class);
 
     static {
@@ -50,16 +51,24 @@ public class JreCompat {
 
         // This is Tomcat 10.1.x with a minimum Java version of Java 11.
         // Look for the highest supported JVM first
-        if (Jre19Compat.isSupported()) {
+        if (Jre21Compat.isSupported()) {
+            instance = new Jre21Compat();
+            jre21Available = true;
+            jre19Available = true;
+            jre16Available = true;
+        } else if (Jre19Compat.isSupported()) {
             instance = new Jre19Compat();
+            jre21Available = false;
             jre19Available = true;
             jre16Available = true;
         } else if (Jre16Compat.isSupported()) {
             instance = new Jre16Compat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = true;
         } else {
             instance = new JreCompat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = false;
         }
@@ -86,6 +95,11 @@ public class JreCompat {
     }
 
 
+    public static boolean isJre21Available() {
+        return jre21Available;
+    }
+
+
     // Java 11 implementations of Java 16 methods
 
     /**
@@ -173,4 +187,29 @@ public class JreCompat {
 
         return result;
     }
+
+
+    // Java 11 implementations of Java 21 methods
+
+    /**
+     * Create a thread builder for virtual threads using the given name to name the threads.
+     *
+     * @param name The base name for the threads
+     *
+     * @return The thread buidler for virtual threads
+     */
+    public Object createVirtualThreadBuilder(String name) {
+        throw new UnsupportedOperationException(sm.getString("jreCompat.noVirtualThreads"));
+    }
+
+
+    /**
+     * Create a thread with the given thread builder and use it to execute the given runnable.
+     *
+     * @param threadBuilder The thread builder to use to create a thread
+     * @param command       The command to run
+     */
+    public void threadBuilderStart(Object threadBuilder, Runnable command) {
+        throw new UnsupportedOperationException(sm.getString("jreCompat.noVirtualThreads"));
+    }
 }
diff --git a/java/org/apache/tomcat/util/compat/LocalStrings.properties b/java/org/apache/tomcat/util/compat/LocalStrings.properties
index 17f9ce4304..945ec00371 100644
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
@@ -18,4 +18,8 @@ jre16Compat.unexpected=Failed to create references to Java 16 classes and method
 
 jre19Compat.javaPre19=Class not found so assuming code is running on a pre-Java 19 JVM
 
-jreCompat.noUnixDomainSocket=Java Runtime does not support Unix domain sockets. You must use Java 16 to use this feature.
+jre21Compat.javaPre16=Class not found so assuming code is running on a pre-Java 21 JVM
+jre21Compat.unexpected=Failed to create references to Java 21 classes and methods
+
+jreCompat.noUnixDomainSocket=Java Runtime does not support Unix domain sockets. You must use Java 16 or later to use this feature.
+jreCompat.noVirtualThreads=Java Runtime does not support virtual threads. You must use Java 21 or later to use this feature.
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 8da0442af0..caf1baf2e9 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -112,6 +112,13 @@
         <code>init()</code>/<code>destroy()</code> methods of components to the
         <code>start()</code>/<code>stop()</code> methods. (markt)
       </scode>
+      <add>
+        Add <code>org.apache.catalina.core.VirtualThreadExecutor</code>, a
+        virtual thread based executor that may be used with one or more
+        Connectors to process requests received by those Connectors using
+        virtual threads. This Executor requires a minimum Java version of Java
+        21. (markt)
+      </add>
     </changelog>
   </subsection>
   <subsection name="Coyote">
diff --git a/webapps/docs/config/executor.xml b/webapps/docs/config/executor.xml
index 05b1420b8e..59cf023605 100644
--- a/webapps/docs/config/executor.xml
+++ b/webapps/docs/config/executor.xml
@@ -77,8 +77,12 @@
 
   <subsection name="Standard Implementation">
 
-  <p>
-  The default implementation supports the following attributes:</p>
+  <p>This implemtenation uses a pool of platform threads to execute the tasks assigned to the Executor.</p>
+
+  <p>The <code>className</code> attribute must be <code>org.apache.catalina.core.StandardThreadExecutor</code> to use
+     this implementation.</p>
+
+  <p>The standard implementation supports the following attributes:</p>
 
   <attributes>
 
@@ -118,6 +122,26 @@
 
 
   </subsection>
+
+  <subsection name="Virtual Thread Implementation">
+
+  <p>This implemtenation uses a new virtual thread to execute each task assigned to the Executor. This Executor requires
+     a minimum Java version of Java 21.</p>
+
+  <p>The <code>className</code> attribute must be <code>org.apache.catalina.core.VirtualThreadExecutor</code> to use
+     this implementation.</p>
+
+  <p>The virtual thread implementation supports the follow attributes:</p>
+
+  <attributes>
+    <attribute name="namePrefix" required="false">
+      <p>(String) The name prefix for each thread created by the executor.
+         The thread name for an individual thread will be <code>namePrefix+threadNumber</code></p>
+    </attribute>
+  </attributes>
+
+  </subsection>
+
 </section>
 
 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Re: [tomcat] branch 10.1.x updated: Back-port virtual thread support

Posted by Mark Thomas <ma...@apache.org>.
On 12/05/2023 10:45, markt@apache.org wrote:
> This is an automated email from the ASF dual-hosted git repository.
> 
> markt pushed a commit to branch 10.1.x
> in repository https://gitbox.apache.org/repos/asf/tomcat.git
> 
> 
> The following commit(s) were added to refs/heads/10.1.x by this push:
>       new 3949154e2f Back-port virtual thread support
> 3949154e2f is described below
> 
> commit 3949154e2fab7e6ac40f67a8a708aeea1476a0f3
> Author: Mark Thomas <ma...@apache.org>
> AuthorDate: Fri May 12 09:54:55 2023 +0100
> 
>      Back-port virtual thread support

Any reason not to back-port this all the way back to 8.5.x ? The 
required SocketWrapper sync -> lock changes have already been 
back-ported as they were needed for a WebSocket fix.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org