You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Nicolas BONAMY <ni...@gmail.com> on 2023/12/07 19:55:34 UTC

Virtual Thread with Http11Nio2Protocol

Hi,

I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:

    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               useVirtualThreads="true"
               />
But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.

If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1

    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
    

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               useVirtualThreads="true"
               />
Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?

Re: Virtual Thread with Http11Nio2Protocol

Posted by Nicolas <ni...@gmail.com>.
Hi,

When the property "useVirtualThreads" is true, Tomcat create a VirtualThreadExecutor (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/net/AbstractEndpoint.java#L1047) so a virtual thread is using (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java#L38) to execute  org.apache.tomcat.util.net.Nio2Endpoint$Nio2Acceptor

but you are right that the AsynchronousServerSocketChannel in Nio2Endpoint is not using a VirtualThreadExecutor (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/net/Nio2Endpoint.java#L120) because it is not an ExecutorService but an Executor, so AsynchronousServerSocketChannel is using a ThreadPoolExecutor.

A code like this is executing AsynchronousServerSocketChannel with Virtual Threads (fast code, so probably bad) : 
if (getExecutor() == null) {
    createExecutor();
}
if (getExecutor() instanceof ExecutorService) {
    threadGroup = AsynchronousChannelGroup.withThreadPool((ExecutorService) getExecutor());
} else if (getExecutor() instanceof VirtualThreadExecutor) {
    threadGroup = AsynchronousChannelGroup.withThreadPool(Executors.newVirtualThreadPerTaskExecutor());
}

What do you think about this ? It is a bug or a choice of tomcat teams that only Nio2Acceptor are on virtual thread and not AsynchronousServerSocketChannel?



To test this, I duplicate the TestMaxConnections test and add Assert.assertTrue(tomcat.getConnector().setProperty("useVirtualThreads", "true"));

Regards,

Nicolas

> Le 8 déc. 2023 à 03:49, Han Li <li...@apache.org> a écrit :x
> 
> Hi Nicolas,
> 
> I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.
> 
> So I think this is a Tomcat bug.
> 
> Han
> 
>> On Dec 8, 2023, at 03:55, Nicolas BONAMY <ni...@gmail.com> wrote:
>> 
>> Hi,
>> 
>> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
>> 
>>   <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>> 
>>   <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>>              connectionTimeout="20000"
>>              redirectPort="8443"
>>              maxParameterCount="1000"
>>              useVirtualThreads="true"
>>              />
>> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
>> 
>> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
>> 
>>   <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>> 
>> 
>>   <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>>              connectionTimeout="20000"
>>              redirectPort="8443"
>>              maxParameterCount="1000"
>>              useVirtualThreads="true"
>>              />
>> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


Re: Virtual Thread with Http11Nio2Protocol

Posted by Nicolas <ni...@gmail.com>.
Hi Mark,

Of course I tried with 

    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
    
    <Connector executor="tomcatThreadPoolVirtual" port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               useVirtualThreads="true"
               />

To reference the executor in the connector ;)

> Le 8 déc. 2023 à 10:51, Mark Thomas <ma...@apache.org> a écrit :
> 
> On 08/12/2023 02:49, Han Li wrote:
>> Hi Nicolas,
>> I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.
>> So I think this is a Tomcat bug.
> 
> +1
> 
>>> On Dec 8, 2023, at 03:55, Nicolas BONAMY <nicolas.bonamy2@gmail.com <ma...@gmail.com>> wrote:
>>> 
>>> Hi,
>>> 
>>> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
>>> 
>>>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
> 
> Note that the above configuration is a) unnecessary and b) doesn't do anything as the following Connector does not reference the Executor
> 
> Mark
> 
> 
>>>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>>>               connectionTimeout="20000"
>>>               redirectPort="8443"
>>>               maxParameterCount="1000"
>>>               useVirtualThreads="true"
>>>               />
>>> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
>>> 
>>> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
>>> 
>>>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>>> 
>>> 
>>>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>>>               connectionTimeout="20000"
>>>               redirectPort="8443"
>>>               maxParameterCount="1000"
>>>               useVirtualThreads="true"
>>>               />
>>> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org <ma...@tomcat.apache.org>
> For additional commands, e-mail: users-help@tomcat.apache.org <ma...@tomcat.apache.org>

Re: Virtual Thread with Http11Nio2Protocol

Posted by Nicolas BONAMY <ni...@gmail.com>.
Thanks!

Nicolas

> Le 8 déc. 2023 à 11:35, Mark Thomas <ma...@apache.org> a écrit :
> 
> On 08/12/2023 09:51, Mark Thomas wrote:
>>> On 08/12/2023 02:49, Han Li wrote:
>>> Hi Nicolas,
>>> 
>>> I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.
>>> 
>>> So I think this is a Tomcat bug.
>> +1
> 
> This has been fixed for all versions and will be included in the January release round (unless a regression is found in the December releases and  we need to re-do them).
> 
> Mark
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

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


Re: Virtual Thread with Http11Nio2Protocol

Posted by Mark Thomas <ma...@apache.org>.
On 08/12/2023 09:51, Mark Thomas wrote:
> On 08/12/2023 02:49, Han Li wrote:
>> Hi Nicolas,
>>
>> I took a quick look that Tomcat's VirtualThreadExecutor does not 
>> implement the ExecutorService interface, which leads to this result.
>>
>> So I think this is a Tomcat bug.
> 
> +1

This has been fixed for all versions and will be included in the January 
release round (unless a regression is found in the December releases and 
  we need to re-do them).

Mark

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


Re: Virtual Thread with Http11Nio2Protocol

Posted by Mark Thomas <ma...@apache.org>.
On 08/12/2023 02:49, Han Li wrote:
> Hi Nicolas,
> 
> I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.
> 
> So I think this is a Tomcat bug.

+1

>> On Dec 8, 2023, at 03:55, Nicolas BONAMY <ni...@gmail.com> wrote:
>>
>> Hi,
>>
>> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
>>
>>     <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>

Note that the above configuration is a) unnecessary and b) doesn't do 
anything as the following Connector does not reference the Executor

Mark


>>     <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>>                connectionTimeout="20000"
>>                redirectPort="8443"
>>                maxParameterCount="1000"
>>                useVirtualThreads="true"
>>                />
>> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
>>
>> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
>>
>>     <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>>
>>
>>     <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>>                connectionTimeout="20000"
>>                redirectPort="8443"
>>                maxParameterCount="1000"
>>                useVirtualThreads="true"
>>                />
>> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 

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


Re: Virtual Thread with Http11Nio2Protocol

Posted by Nicolas <ni...@gmail.com>.
Hi,

When the property "useVirtualThreads" is true, Tomcat create a VirtualThreadExecutor (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/net/AbstractEndpoint.java#L1047) so a virtual thread is using (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/threads/VirtualThreadExecutor.java#L38) to execute  org.apache.tomcat.util.net.Nio2Endpoint$Nio2Acceptor

but you are right that the AsynchronousServerSocketChannel in Nio2Endpoint is not using a VirtualThreadExecutor (https://github.com/apache/tomcat/blob/10.1.x/java/org/apache/tomcat/util/net/Nio2Endpoint.java#L120) because it is not an ExecutorService but an Executor, so AsynchronousServerSocketChannel is using a ThreadPoolExecutor.

A code like this is executing AsynchronousServerSocketChannel with Virtual Threads (fast code, so probably bad) : 
if (getExecutor() == null) {
    createExecutor();
}
if (getExecutor() instanceof ExecutorService) {
    threadGroup = AsynchronousChannelGroup.withThreadPool((ExecutorService) getExecutor());
} else if (getExecutor() instanceof VirtualThreadExecutor) {
    threadGroup = AsynchronousChannelGroup.withThreadPool(Executors.newVirtualThreadPerTaskExecutor());
}

What do you think about this ? It is a bug or a choice of tomcat teams that only Nio2Acceptor are on virtual thread and not AsynchronousServerSocketChannel?



To test this, I duplicate the TestMaxConnections test and add Assert.assertTrue(tomcat.getConnector().setProperty("useVirtualThreads", "true"));

Regards,

Nicolas

> Le 8 déc. 2023 à 03:49, Han Li <li...@apache.org> a écrit :x
> 
> Hi Nicolas,
> 
> I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.
> 
> So I think this is a Tomcat bug.
> 
> Han
> 
>> On Dec 8, 2023, at 03:55, Nicolas BONAMY <ni...@gmail.com> wrote:
>> 
>> Hi,
>> 
>> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
>> 
>>   <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>> 
>>   <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>>              connectionTimeout="20000"
>>              redirectPort="8443"
>>              maxParameterCount="1000"
>>              useVirtualThreads="true"
>>              />
>> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
>> 
>> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
>> 
>>   <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
>> 
>> 
>>   <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>>              connectionTimeout="20000"
>>              redirectPort="8443"
>>              maxParameterCount="1000"
>>              useVirtualThreads="true"
>>              />
>> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


Re: Virtual Thread with Http11Nio2Protocol

Posted by Han Li <li...@apache.org>.
Hi Nicolas,

I took a quick look that Tomcat's VirtualThreadExecutor does not implement the ExecutorService interface, which leads to this result.

So I think this is a Tomcat bug.

Han

> On Dec 8, 2023, at 03:55, Nicolas BONAMY <ni...@gmail.com> wrote:
> 
> Hi,
> 
> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
> 
>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
> 
>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>               connectionTimeout="20000"
>               redirectPort="8443"
>               maxParameterCount="1000"
>               useVirtualThreads="true"
>               />
> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
> 
> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
> 
>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
> 
> 
>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>               connectionTimeout="20000"
>               redirectPort="8443"
>               maxParameterCount="1000"
>               useVirtualThreads="true"
>               />
> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?


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


Re: Virtual Thread with Http11Nio2Protocol

Posted by Nicolas BONAMY <ni...@gmail.com>.
William,

I’m using Adoptium Temurin 21.0.1+12

Regards,

Nicolas 

> Le 7 déc. 2023 à 21:00, William Crowell <WC...@perforce.com.invalid> a écrit :
> 
> Nicolas,
> 
> Which JDK version?
> 
> Regards,
> 
> William Crowell
> 
> From: Nicolas BONAMY <ni...@gmail.com>
> Date: Thursday, December 7, 2023 at 2:55 PM
> To: users@tomcat.apache.org <us...@tomcat.apache.org>
> Subject: Virtual Thread with Http11Nio2Protocol
> Hi,
> 
> I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:
> 
>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
> 
>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
>               connectionTimeout="20000"
>               redirectPort="8443"
>               maxParameterCount="1000"
>               useVirtualThreads="true"
>               />
> But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.
> 
> If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1
> 
>    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>
> 
> 
>    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
>               connectionTimeout="20000"
>               redirectPort="8443"
>               maxParameterCount="1000"
>               useVirtualThreads="true"
>               />
> Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?
> 
> 
> CAUTION: This email originated from outside of the organization. Do not click on links or open attachments unless you recognize the sender and know the content is safe.
> 
> 
> This e-mail may contain information that is privileged or confidential. If you are not the intended recipient, please delete the e-mail and any attachments and notify us immediately.
> 

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


Re: Virtual Thread with Http11Nio2Protocol

Posted by William Crowell <WC...@perforce.com.INVALID>.
Nicolas,

Which JDK version?

Regards,

William Crowell

From: Nicolas BONAMY <ni...@gmail.com>
Date: Thursday, December 7, 2023 at 2:55 PM
To: users@tomcat.apache.org <us...@tomcat.apache.org>
Subject: Virtual Thread with Http11Nio2Protocol
Hi,

I try to use virtual thread on Apache Tomcat 10.1.16 with this configuration on macOS or on Linux:

    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>

    <Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               useVirtualThreads="true"
               />
But when I make a request, I'm not on a virtual thread : Thread[#76,Thread-14,5,main] . I profiled my application too but no virtual threads are used.

If I use a Http11NioProtocol instead of Http11Nio2Protocol, all requests are on virtual thread : VirtualThread[#65,http-nio-8080-virt-0]/runnable@ForkJoinPool-1-worker-1

    <Executor name="tomcatThreadPoolVirtual" class="org.apache.catalina.core.StandardVirtualThreadExecutor"/>


    <Connector port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               useVirtualThreads="true"
               />
Http11Nio2Protocol is not working with virtual threads? Has anyone encountered this problem before?


CAUTION: This email originated from outside of the organization. Do not click on links or open attachments unless you recognize the sender and know the content is safe.


This e-mail may contain information that is privileged or confidential. If you are not the intended recipient, please delete the e-mail and any attachments and notify us immediately.