Skip to content

Commit

Permalink
Support project loom as thread pool factory (#13111)
Browse files Browse the repository at this point in the history
fixes #10768
  • Loading branch information
AlbumenJ authored Sep 26, 2023
1 parent fb0911d commit 4f32e0a
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 0 deletions.
1 change: 1 addition & 0 deletions .artifacts
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,4 @@ dubbo-plugin-context
dubbo-plugin-classloader-filter
dubbo-plugin-proxy-bytebuddy
dubbo-plugin-qos-trace
dubbo-plugin-loom
48 changes: 48 additions & 0 deletions dubbo-plugin/dubbo-plugin-loom/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-plugin</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>com.aliyun.dubbo</groupId>
<artifactId>dubbo-plugin-loom</artifactId>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skip_maven_deploy>false</skip_maven_deploy>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
<version>${project.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.dubbo.common.threadpool.support.loom;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.threadpool.ThreadPool;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_THREAD_NAME;
import static org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;

/**
* Creates a thread pool that use virtual thread
*
* @see Executors#newVirtualThreadPerTaskExecutor()
*/
public class VirtualThreadPool implements ThreadPool {
@Override
public Executor getExecutor(URL url) {
String name = url.getParameter(THREAD_NAME_KEY, (String) url.getAttribute(THREAD_NAME_KEY, DEFAULT_THREAD_NAME));
return Executors.newThreadPerTaskExecutor(
Thread.ofVirtual()
.name(name, 1)
.factory());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
virtual=org.apache.dubbo.common.threadpool.support.loom.VirtualThreadPool
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.dubbo.common.threadpool.support.loom;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.threadpool.ThreadPool;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

import static org.apache.dubbo.common.constants.CommonConstants.QUEUES_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.THREAD_NAME_KEY;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class VirtualThreadPoolTest {

@Test
@EnabledForJreRange(min = JRE.JAVA_21)
void getExecutor1() throws Exception {
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" +
THREAD_NAME_KEY + "=demo");
ThreadPool threadPool = new VirtualThreadPool();
Executor executor = threadPool.getExecutor(url);

final CountDownLatch latch = new CountDownLatch(1);
executor.execute(() -> {
Thread thread = Thread.currentThread();
assertTrue(thread.isVirtual());
assertThat(thread.getName(), startsWith("demo"));
latch.countDown();
});

latch.await();
assertThat(latch.getCount(), is(0L));
}

@Test
@EnabledForJreRange(min = JRE.JAVA_21)
void getExecutor2() {
URL url = URL.valueOf("dubbo://10.20.130.230:20880/context/path?" + QUEUES_KEY + "=1");
ThreadPool threadPool = new VirtualThreadPool();
assertThat(threadPool.getExecutor(url).getClass().getName(), Matchers.is("java.util.concurrent.ThreadPerTaskExecutor"));
}
}
12 changes: 12 additions & 0 deletions dubbo-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,16 @@
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
<profile>
<id>loom</id>
<activation>
<jdk>[21,)</jdk>
</activation>
<modules>
<module>dubbo-plugin-loom</module>
</modules>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FileTest {
ignoredModules.add(Pattern.compile("dubbo-core-spi"));
ignoredModules.add(Pattern.compile("dubbo-demo.*"));
ignoredModules.add(Pattern.compile("dubbo-annotation-processor"));
ignoredModules.add(Pattern.compile("dubbo-plugin-loom.*"));

ignoredArtifacts.add(Pattern.compile("dubbo-demo.*"));
ignoredArtifacts.add(Pattern.compile("dubbo-test.*"));
Expand Down

0 comments on commit 4f32e0a

Please sign in to comment.