Copilot commented on code in PR #8023:
URL: https://github.com/apache/incubator-seata/pull/8023#discussion_r3009231650


##########
threadpool/src/main/java/org/apache/seata/common/thread/ThreadPoolExecutorFactory.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.seata.common.thread;
+
+import org.apache.seata.common.loader.EnhancedServiceLoader;
+import org.apache.seata.common.loader.EnhancedServiceNotFoundException;
+
+import java.util.Objects;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Central factory used by Seata managed thread pools.
+ */
+public final class ThreadPoolExecutorFactory {
+
+    private ThreadPoolExecutorFactory() {}
+
+    public static ThreadFactory newThreadFactory(String threadPrefix, int 
totalSize) {
+        return newThreadFactory(threadPrefix, totalSize, true);
+    }
+
+    public static ThreadFactory newThreadFactory(String threadPrefix, int 
totalSize, boolean daemon) {
+        Objects.requireNonNull(threadPrefix, "threadPrefix must not be null");
+        return new NamedThreadFactory(threadPrefix, totalSize, daemon);
+    }
+
+    public static ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue) {
+        return newThreadPoolExecutor(threadPrefix, corePoolSize, 
maximumPoolSize, keepAliveTime, unit, workQueue, true);
+    }
+
+    public static ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue,
+            boolean daemon) {
+        return newThreadPoolExecutor(
+                threadPrefix,
+                corePoolSize,
+                maximumPoolSize,
+                keepAliveTime,
+                unit,
+                workQueue,
+                daemon,
+                new ThreadPoolExecutor.AbortPolicy());
+    }
+
+    public static ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue,
+            RejectedExecutionHandler rejectedHandler) {
+        return newThreadPoolExecutor(
+                threadPrefix, corePoolSize, maximumPoolSize, keepAliveTime, 
unit, workQueue, true, rejectedHandler);
+    }
+
+    public static ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue,
+            boolean daemon,
+            RejectedExecutionHandler rejectedHandler) {
+        validateThreadPoolArguments(threadPrefix, corePoolSize, 
maximumPoolSize, keepAliveTime, unit);
+        return resolveThreadPoolProvider()
+                .newThreadPoolExecutor(
+                        threadPrefix,
+                        corePoolSize,
+                        maximumPoolSize,
+                        keepAliveTime,
+                        unit,
+                        Objects.requireNonNull(workQueue, "workQueue must not 
be null"),
+                        daemon,
+                        Objects.requireNonNull(rejectedHandler, 
"rejectedHandler must not be null"));
+    }
+
+    public static ScheduledThreadPoolExecutor 
newScheduledThreadPoolExecutor(String threadPrefix, int corePoolSize) {
+        return newScheduledThreadPoolExecutor(threadPrefix, corePoolSize, 
true);
+    }
+
+    public static ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor(
+            String threadPrefix, int corePoolSize, boolean daemon) {
+        return newScheduledThreadPoolExecutor(threadPrefix, corePoolSize, 
daemon, new ThreadPoolExecutor.AbortPolicy());
+    }
+
+    public static ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor(
+            String threadPrefix, int corePoolSize, boolean daemon, 
RejectedExecutionHandler rejectedHandler) {
+        validateScheduledThreadPoolArguments(threadPrefix, corePoolSize);
+        return resolveThreadPoolProvider()
+                .newScheduledThreadPoolExecutor(
+                        threadPrefix,
+                        corePoolSize,
+                        daemon,
+                        Objects.requireNonNull(rejectedHandler, 
"rejectedHandler must not be null"));
+    }
+
+    private static void validateThreadPoolArguments(
+            String threadPrefix, int corePoolSize, int maximumPoolSize, long 
keepAliveTime, TimeUnit unit) {
+        Objects.requireNonNull(threadPrefix, "threadPrefix must not be null");
+        Objects.requireNonNull(unit, "timeUnit must not be null");
+        if (corePoolSize <= 0) {
+            throw new IllegalArgumentException("corePoolSize must be greater 
than zero");
+        }
+        if (maximumPoolSize <= 0) {
+            throw new IllegalArgumentException("maximumPoolSize must be 
greater than zero");
+        }
+        if (maximumPoolSize < corePoolSize) {
+            throw new IllegalArgumentException("maximumPoolSize must be 
greater than or equal to corePoolSize");
+        }
+        if (keepAliveTime < 0) {
+            throw new IllegalArgumentException("keepAliveTime must not be 
negative");
+        }
+    }
+
+    private static void validateScheduledThreadPoolArguments(String 
threadPrefix, int corePoolSize) {
+        Objects.requireNonNull(threadPrefix, "threadPrefix must not be null");
+        if (corePoolSize <= 0) {
+            throw new IllegalArgumentException("corePoolSize must be greater 
than zero");
+        }

Review Comment:
   ThreadPoolExecutorFactory currently rejects corePoolSize==0 for both regular 
and scheduled executors. The underlying JDK 
ThreadPoolExecutor/ScheduledThreadPoolExecutor constructors allow a zero core 
size, and some existing configuration knobs (e.g., min pool sizes) may 
plausibly be set to 0 for scale-to-zero semantics. Consider allowing 0 here 
(corePoolSize >= 0) to avoid introducing a new runtime IllegalArgumentException 
in previously-valid configurations.



##########
threadpool-loom/src/main/java/org/apache/seata/common/thread/VirtualThreadPoolExecutor.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.seata.common.thread;
+
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.SynchronousQueue;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Virtual-thread-backed thread pool implementation.
+ */
+public class VirtualThreadPoolExecutor extends ThreadPoolExecutor {
+
+    public VirtualThreadPoolExecutor(
+            String threadPrefix, int corePoolSize, boolean daemon, 
RejectedExecutionHandler rejectedHandler) {
+        super(
+                corePoolSize,
+                Integer.MAX_VALUE,
+                0L,
+                TimeUnit.MILLISECONDS,
+                new SynchronousQueue<>(),
+                Thread.ofVirtual().name(normalizePrefix(threadPrefix), 
1).factory(),
+                rejectedHandler);

Review Comment:
   VirtualThreadPoolExecutor hard-codes Integer.MAX_VALUE + SynchronousQueue, 
which ignores caller-provided maximumPoolSize/keepAliveTime/workQueue. In 
"auto" mode on JDK 25+ (with loom present) this can effectively remove existing 
backpressure/queueing and allow unbounded concurrent task execution, 
potentially leading to CPU/memory exhaustion under load. Consider honoring 
maximumPoolSize and/or the provided workQueue as a concurrency/backpressure 
control (or at least documenting and warning when the passed parameters are 
ignored).



##########
threadpool-loom/src/main/java/org/apache/seata/common/thread/VirtualThreadPoolProvider.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.seata.common.thread;
+
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * JDK 21+ SPI implementation that creates virtual-thread-backed business 
pools.
+ */
+@LoadLevel(name = "virtual", order = Integer.MIN_VALUE)
+public class VirtualThreadPoolProvider implements ThreadPoolProvider {
+
+    @Override
+    public ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue,
+            boolean daemon,
+            RejectedExecutionHandler rejectedHandler) {
+        return new VirtualThreadPoolExecutor(threadPrefix, corePoolSize, 
daemon, rejectedHandler);
+    }

Review Comment:
   VirtualThreadPoolProvider ignores several parameters from the 
ThreadPoolProvider contract (maximumPoolSize, keepAliveTime, unit, workQueue, 
daemon) when creating virtual executors. This makes behavior diverge sharply 
from platform mode and can surprise callers that rely on those parameters for 
sizing/backpressure. Consider either enforcing/validating expected values 
(e.g., reject unsupported configurations) or clearly documenting/logging that 
these settings are ignored in virtual mode.



##########
threadpool-loom/src/main/java/org/apache/seata/common/thread/VirtualScheduledThreadPoolExecutor.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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.seata.common.thread;
+
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+
+/**
+ * Virtual-thread-backed scheduled thread pool implementation.
+ */
+public class VirtualScheduledThreadPoolExecutor extends 
ScheduledThreadPoolExecutor {
+
+    public VirtualScheduledThreadPoolExecutor(
+            String threadPrefix, int corePoolSize, RejectedExecutionHandler 
rejectedHandler) {
+        super(
+                corePoolSize,
+                Thread.ofVirtual().name(normalizePrefix(threadPrefix), 
1).factory(),
+                rejectedHandler);
+    }
+
+    private static String normalizePrefix(String threadPrefix) {
+        return threadPrefix.endsWith("-") ? threadPrefix : threadPrefix + "-";
+    }
+}

Review Comment:
   normalizePrefix(String) is duplicated in both VirtualThreadPoolExecutor and 
VirtualScheduledThreadPoolExecutor. Consider extracting this into a small 
shared utility (or a package-private helper) to keep naming behavior consistent 
and avoid future divergence.



##########
threadpool-loom/src/main/java/org/apache/seata/common/thread/VirtualThreadPoolProvider.java:
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.seata.common.thread;
+
+import org.apache.seata.common.loader.LoadLevel;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.ThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * JDK 21+ SPI implementation that creates virtual-thread-backed business 
pools.
+ */
+@LoadLevel(name = "virtual", order = Integer.MIN_VALUE)
+public class VirtualThreadPoolProvider implements ThreadPoolProvider {
+
+    @Override
+    public ThreadPoolExecutor newThreadPoolExecutor(
+            String threadPrefix,
+            int corePoolSize,
+            int maximumPoolSize,
+            long keepAliveTime,
+            TimeUnit unit,
+            BlockingQueue<Runnable> workQueue,
+            boolean daemon,
+            RejectedExecutionHandler rejectedHandler) {
+        return new VirtualThreadPoolExecutor(threadPrefix, corePoolSize, 
daemon, rejectedHandler);
+    }
+
+    @Override
+    public ScheduledThreadPoolExecutor newScheduledThreadPoolExecutor(
+            String threadPrefix, int corePoolSize, boolean daemon, 
RejectedExecutionHandler rejectedHandler) {
+        return new VirtualScheduledThreadPoolExecutor(threadPrefix, 
corePoolSize, rejectedHandler);
+    }
+}

Review Comment:
   The loom-backed provider/executor path (VirtualThreadPoolProvider / 
VirtualThreadPoolExecutor / VirtualScheduledThreadPoolExecutor) is not 
exercised by tests here. Consider adding JDK21+-gated tests (e.g., in 
threadpool-loom) that assert the factory selects the virtual provider when 
present and that threads created are virtual (and named as expected).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to