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/22 12:40:54 UTC

[tomcat] 01/02: Back-port virtual thread support

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

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

commit cc69f1b82478cbb9bfcd431bb02ab5448c7c7d44
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       | 100 +++++++++++++++++++++
 .../org/apache/tomcat/util/compat/Jre21Compat.java |  82 +++++++++++++++++
 java/org/apache/tomcat/util/compat/JreCompat.java  |  45 +++++++++-
 .../tomcat/util/compat/LocalStrings.properties     |   5 ++
 webapps/docs/changelog.xml                         |  11 +++
 webapps/docs/config/executor.xml                   |  28 +++++-
 7 files changed, 270 insertions(+), 3 deletions(-)

diff --git a/java/org/apache/catalina/core/LocalStrings.properties b/java/org/apache/catalina/core/LocalStrings.properties
index 7cbf76d05b..64fc860dac 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -309,3 +309,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..496aeb3303
--- /dev/null
+++ b/java/org/apache/catalina/core/VirtualThreadExecutor.java
@@ -0,0 +1,100 @@
+/*
+ *  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 java.util.concurrent.TimeUnit;
+
+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
+    public void execute(Runnable command, long timeout, TimeUnit unit) {
+        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 13b30b23ea..e68ffb8044 100644
--- a/java/org/apache/tomcat/util/compat/JreCompat.java
+++ b/java/org/apache/tomcat/util/compat/JreCompat.java
@@ -47,6 +47,7 @@ public class JreCompat {
     private static final boolean jre11Available;
     private static final boolean jre16Available;
     private static final boolean jre19Available;
+    private static final boolean jre21Available;
     private static final StringManager sm = StringManager.getManager(JreCompat.class);
 
 
@@ -54,32 +55,44 @@ public class JreCompat {
     static {
         // This is Tomcat 8 with a minimum Java version of Java 7.
         // Look for the highest supported JVM first
-        if (Jre19Compat.isSupported()) {
+        if (Jre21Compat.isSupported()) {
+            instance = new Jre21Compat();
+            jre21Available = true;
+            jre19Available = true;
+            jre16Available = true;
+            jre9Available = true;
+            jre8Available = true;
+        } else if (Jre19Compat.isSupported()) {
             instance = new Jre19Compat();
+            jre21Available = false;
             jre19Available = true;
             jre16Available = true;
             jre9Available = true;
             jre8Available = true;
         } else if (Jre16Compat.isSupported()) {
             instance = new Jre16Compat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = true;
             jre9Available = true;
             jre8Available = true;
         } else if (Jre9Compat.isSupported()) {
             instance = new Jre9Compat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = false;
             jre9Available = true;
             jre8Available = true;
         } else if (Jre8Compat.isSupported()) {
             instance = new Jre8Compat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = false;
             jre9Available = false;
             jre8Available = true;
         } else {
             instance = new JreCompat();
+            jre21Available = false;
             jre19Available = false;
             jre16Available = false;
             jre9Available = false;
@@ -124,6 +137,11 @@ public class JreCompat {
     }
 
 
+    public static boolean isJre21Available() {
+        return jre21Available;
+    }
+
+
     // Java 7 implementation of Java 8 methods
 
     @SuppressWarnings("unused")
@@ -357,4 +375,29 @@ public class JreCompat {
 
         return result;
     }
+
+
+    // Java 7 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 e8cdea670e..dddec0c999 100644
--- a/java/org/apache/tomcat/util/compat/LocalStrings.properties
+++ b/java/org/apache/tomcat/util/compat/LocalStrings.properties
@@ -15,6 +15,9 @@
 
 jre19Compat.javaPre19=Class not found so assuming code is running on a pre-Java 19 JVM
 
+jre21Compat.javaPre21=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
+
 jre8Compat.javaPre8=Class not found so assuming code is running on a pre-Java 8 JVM
 jre8Compat.unexpected=Failed to create references to Java 8 classes and methods
 
@@ -26,3 +29,5 @@ jreCompat.noApplicationProtocol=Java Runtime does not support SSLEngine.getAppli
 jreCompat.noApplicationProtocols=Java Runtime does not support SSLParameters.setApplicationProtocols(). You must use Java 9 to use this feature.
 jreCompat.noDomainLoadStoreParameter=Java Runtime does not support DKS key store type. You must use Java 8 or later to use this feature.
 jreCompat.noServerCipherSuiteOrder=Java Runtime does not support "useServerCipherSuitesOrder". You must use Java 8 or later to use this feature.
+jreCompat.noUnixDomainSocket=Java Runtime does not support Unix domain sockets. You must use Java 16 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 6a2afaa8b7..52b9fd2a5c 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -105,6 +105,17 @@
   issues do not "pop up" wrt. others).
 -->
 <section name="Tomcat 8.5.90 (schultz)" rtext="in development">
+  <subsection name="Catalina">
+    <changelog>
+      <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">
     <changelog>
       <update>
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