Skip to content

Commit

Permalink
LDEV-5020 - use virtual thread with each caller functions
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Jul 15, 2024
1 parent 94adcb6 commit 6f5e548
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 43 deletions.
60 changes: 32 additions & 28 deletions core/src/main/java/lucee/commons/io/SystemUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ public final class SystemUtil {
public static final int JAVA_VERSION_27 = 27; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14;
public static final int JAVA_VERSION_28 = 28; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14;
public static final int JAVA_VERSION_29 = 29; // FUTURE lucee.runtime.util.SystemUtil.JAVA_VERSION_14;
public static final int JAVA_VERSION_30 = 30;
public static final int JAVA_VERSION_31 = 31;
public static final int JAVA_VERSION_32 = 32;
public static final int JAVA_VERSION_33 = 33;
public static final int JAVA_VERSION_34 = 34;
public static final int JAVA_VERSION_35 = 35;

public static final int OUT = lucee.runtime.util.SystemUtil.OUT;
public static final int ERR = lucee.runtime.util.SystemUtil.ERR;
Expand Down Expand Up @@ -223,39 +229,37 @@ public final class SystemUtil {
MemoryPoolMXBean tmp = getPermGenSpaceBean();
if (tmp != permGenSpaceBean) permGenSpaceBean = null;

if (JAVA_VERSION_STRING.startsWith("1.14.") || JAVA_VERSION_STRING.startsWith("14")) JAVA_VERSION = JAVA_VERSION_14;
if (JAVA_VERSION_STRING.startsWith("35.")) JAVA_VERSION = JAVA_VERSION_35;
else if (JAVA_VERSION_STRING.startsWith("34.")) JAVA_VERSION = JAVA_VERSION_34;
else if (JAVA_VERSION_STRING.startsWith("33.")) JAVA_VERSION = JAVA_VERSION_33;
else if (JAVA_VERSION_STRING.startsWith("32.")) JAVA_VERSION = JAVA_VERSION_32;
else if (JAVA_VERSION_STRING.startsWith("31.")) JAVA_VERSION = JAVA_VERSION_31;
else if (JAVA_VERSION_STRING.startsWith("30.")) JAVA_VERSION = JAVA_VERSION_30;
else if (JAVA_VERSION_STRING.startsWith("29.")) JAVA_VERSION = JAVA_VERSION_29;
else if (JAVA_VERSION_STRING.startsWith("28.")) JAVA_VERSION = JAVA_VERSION_28;
else if (JAVA_VERSION_STRING.startsWith("27.")) JAVA_VERSION = JAVA_VERSION_27;
else if (JAVA_VERSION_STRING.startsWith("26.")) JAVA_VERSION = JAVA_VERSION_26;
else if (JAVA_VERSION_STRING.startsWith("25.")) JAVA_VERSION = JAVA_VERSION_25;
else if (JAVA_VERSION_STRING.startsWith("24.")) JAVA_VERSION = JAVA_VERSION_24;
else if (JAVA_VERSION_STRING.startsWith("23.")) JAVA_VERSION = JAVA_VERSION_23;
else if (JAVA_VERSION_STRING.startsWith("22.")) JAVA_VERSION = JAVA_VERSION_22;
else if (JAVA_VERSION_STRING.startsWith("21.")) JAVA_VERSION = JAVA_VERSION_21;
else if (JAVA_VERSION_STRING.startsWith("20.")) JAVA_VERSION = JAVA_VERSION_20;
else if (JAVA_VERSION_STRING.startsWith("19.")) JAVA_VERSION = JAVA_VERSION_19;
else if (JAVA_VERSION_STRING.startsWith("18.")) JAVA_VERSION = JAVA_VERSION_18;
else if (JAVA_VERSION_STRING.startsWith("17.")) JAVA_VERSION = JAVA_VERSION_17;
else if (JAVA_VERSION_STRING.startsWith("16.")) JAVA_VERSION = JAVA_VERSION_16;
else if (JAVA_VERSION_STRING.startsWith("15.")) JAVA_VERSION = JAVA_VERSION_15;

else if (JAVA_VERSION_STRING.startsWith("1.14.") || JAVA_VERSION_STRING.startsWith("14")) JAVA_VERSION = JAVA_VERSION_14;
else if (JAVA_VERSION_STRING.startsWith("1.13.") || JAVA_VERSION_STRING.startsWith("13")) JAVA_VERSION = JAVA_VERSION_13;
else if (JAVA_VERSION_STRING.startsWith("1.12.") || JAVA_VERSION_STRING.startsWith("12")) JAVA_VERSION = JAVA_VERSION_12;
else if (JAVA_VERSION_STRING.startsWith("1.11.") || JAVA_VERSION_STRING.startsWith("11")) JAVA_VERSION = JAVA_VERSION_11;
else if (JAVA_VERSION_STRING.startsWith("1.10.") || JAVA_VERSION_STRING.startsWith("10")) JAVA_VERSION = JAVA_VERSION_10;
else if (JAVA_VERSION_STRING.startsWith("1.9.") || JAVA_VERSION_STRING.startsWith("9.")) JAVA_VERSION = JAVA_VERSION_9;
else if (JAVA_VERSION_STRING.startsWith("1.8.")) JAVA_VERSION = JAVA_VERSION_8;
else if (JAVA_VERSION_STRING.startsWith("1.7.")) JAVA_VERSION = JAVA_VERSION_7;
else if (JAVA_VERSION_STRING.startsWith("1.6.")) JAVA_VERSION = JAVA_VERSION_6;
else if (JAVA_VERSION_STRING.startsWith("1.6.")) JAVA_VERSION = JAVA_VERSION_6;

else if (JAVA_VERSION_STRING.startsWith("8.")) JAVA_VERSION = JAVA_VERSION_8;
else if (JAVA_VERSION_STRING.startsWith("9.")) JAVA_VERSION = JAVA_VERSION_9;
else if (JAVA_VERSION_STRING.startsWith("10.")) JAVA_VERSION = JAVA_VERSION_10;
else if (JAVA_VERSION_STRING.startsWith("11.")) JAVA_VERSION = JAVA_VERSION_11;
else if (JAVA_VERSION_STRING.startsWith("12.")) JAVA_VERSION = JAVA_VERSION_12;
else if (JAVA_VERSION_STRING.startsWith("13.")) JAVA_VERSION = JAVA_VERSION_13;
else if (JAVA_VERSION_STRING.startsWith("14.")) JAVA_VERSION = JAVA_VERSION_14;
else if (JAVA_VERSION_STRING.startsWith("15.")) JAVA_VERSION = JAVA_VERSION_15;
else if (JAVA_VERSION_STRING.startsWith("16.")) JAVA_VERSION = JAVA_VERSION_16;
else if (JAVA_VERSION_STRING.startsWith("17.")) JAVA_VERSION = JAVA_VERSION_17;
else if (JAVA_VERSION_STRING.startsWith("18.")) JAVA_VERSION = JAVA_VERSION_18;
else if (JAVA_VERSION_STRING.startsWith("19.")) JAVA_VERSION = JAVA_VERSION_19;
else if (JAVA_VERSION_STRING.startsWith("20.")) JAVA_VERSION = JAVA_VERSION_20;
else if (JAVA_VERSION_STRING.startsWith("21.")) JAVA_VERSION = JAVA_VERSION_21;
else if (JAVA_VERSION_STRING.startsWith("22.")) JAVA_VERSION = JAVA_VERSION_22;
else if (JAVA_VERSION_STRING.startsWith("23.")) JAVA_VERSION = JAVA_VERSION_23;
else if (JAVA_VERSION_STRING.startsWith("24.")) JAVA_VERSION = JAVA_VERSION_24;
else if (JAVA_VERSION_STRING.startsWith("25.")) JAVA_VERSION = JAVA_VERSION_25;
else if (JAVA_VERSION_STRING.startsWith("26.")) JAVA_VERSION = JAVA_VERSION_26;
else if (JAVA_VERSION_STRING.startsWith("27.")) JAVA_VERSION = JAVA_VERSION_27;
else if (JAVA_VERSION_STRING.startsWith("28.")) JAVA_VERSION = JAVA_VERSION_28;
else if (JAVA_VERSION_STRING.startsWith("29.")) JAVA_VERSION = JAVA_VERSION_29;
else if (JAVA_VERSION_STRING.startsWith("1.8.") || JAVA_VERSION_STRING.startsWith("8.")) JAVA_VERSION = JAVA_VERSION_8;
else if (JAVA_VERSION_STRING.startsWith("1.7.") || JAVA_VERSION_STRING.startsWith("7.")) JAVA_VERSION = JAVA_VERSION_7;
else if (JAVA_VERSION_STRING.startsWith("1.6.") || JAVA_VERSION_STRING.startsWith("6.")) JAVA_VERSION = JAVA_VERSION_6;

else JAVA_VERSION = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/functions/closure/Each.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lucee.runtime.PageContext;
Expand All @@ -40,6 +39,7 @@
import lucee.runtime.exp.ParentException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayPro;
import lucee.runtime.type.Collection.Key;
Expand Down Expand Up @@ -77,7 +77,7 @@ private static String _call(PageContext pc, Object obj, UDF udf, boolean paralle
else if (maxThreads == 1) parallel = false;

if (parallel) {
execute = Executors.newFixedThreadPool(maxThreads);
execute = ThreadUtil.createExecutorService(maxThreads);
futures = new ArrayList<Future<Data<Object>>>();
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/functions/closure/Every.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lucee.runtime.PageContext;
Expand All @@ -37,6 +36,7 @@
import lucee.runtime.exp.ParentException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayPro;
import lucee.runtime.type.Collection.Key;
Expand Down Expand Up @@ -80,7 +80,7 @@ private static boolean _call(PageContext pc, Object obj, UDF udf, boolean parall
else if (maxThreads == 1) parallel = false;

if (parallel) {
execute = Executors.newFixedThreadPool(maxThreads);
execute = ThreadUtil.createExecutorService(maxThreads);
futures = new ArrayList<Future<Data<Object>>>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lucee.commons.lang.Pair;
Expand All @@ -38,6 +37,7 @@
import lucee.runtime.exp.ParentException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayImpl;
import lucee.runtime.type.ArrayPro;
Expand Down Expand Up @@ -85,7 +85,7 @@ private static Collection _call(PageContext pc, Object obj, UDF udf, boolean par
// 1 == not parallel
else if (maxThreads == 1) parallel = false;
if (parallel) {
execute = Executors.newFixedThreadPool(maxThreads);
execute = ThreadUtil.createExecutorService(maxThreads);
futures = new ArrayList<Future<Data<Pair<Object, Object>>>>();
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/functions/closure/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lucee.runtime.PageContext;
Expand All @@ -38,6 +37,7 @@
import lucee.runtime.exp.ParentException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayImpl;
import lucee.runtime.type.ArrayPro;
Expand Down Expand Up @@ -85,7 +85,7 @@ private static Collection _call(PageContext pc, Object obj, UDF udf, boolean par
// 1 == not parallel
else if (maxThreads == 1) parallel = false;
if (parallel) {
execute = Executors.newFixedThreadPool(maxThreads);
execute = ThreadUtil.createExecutorService(maxThreads);
futures = new ArrayList<Future<Data<Object>>>();
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/functions/closure/Some.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.ListIterator;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import lucee.runtime.PageContext;
Expand All @@ -37,6 +36,7 @@
import lucee.runtime.exp.ParentException;
import lucee.runtime.ext.function.BIF;
import lucee.runtime.op.Caster;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayPro;
import lucee.runtime.type.Collection.Key;
Expand Down Expand Up @@ -79,7 +79,7 @@ private static boolean _call(PageContext pc, Object obj, UDF udf, boolean parall
// 1 == not parallel
else if (maxThreads == 1) parallel = false;
if (parallel) {
execute = Executors.newFixedThreadPool(maxThreads);
execute = ThreadUtil.createExecutorService(maxThreads);
futures = new ArrayList<Future<Data<Object>>>();
}

Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/lucee/runtime/mvn/MavenUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.apache.http.HttpEntity;
Expand All @@ -29,6 +28,7 @@
import lucee.commons.lang.ExceptionUtil;
import lucee.commons.lang.SerializableObject;
import lucee.runtime.mvn.POMReader.Dependency;
import lucee.runtime.thread.ThreadUtil;
import lucee.runtime.type.util.ListUtil;

public class MavenUtil {
Expand Down Expand Up @@ -109,7 +109,7 @@ public static List<POM> getDependencies(List<POMReader.Dependency> rawDependenci
List<POM> dependencies = new ArrayList<>();
List<POM> parentDendencyManagement = null;

ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
ExecutorService executor = ThreadUtil.createExecutorService(Runtime.getRuntime().availableProcessors());

if (parent != null) {
parentDendencyManagement = current.getDependencyManagement();
Expand Down
23 changes: 22 additions & 1 deletion core/src/main/java/lucee/runtime/thread/ThreadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
package lucee.runtime.thread;

import java.io.OutputStream;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -29,6 +34,7 @@

import lucee.aprint;
import lucee.commons.io.DevNullOutputStream;
import lucee.commons.io.SystemUtil;
import lucee.commons.io.res.Resource;
import lucee.commons.lang.ExceptionUtil;
import lucee.commons.lang.Pair;
Expand Down Expand Up @@ -58,7 +64,6 @@ public static PageContextImpl clonePageContext(PageContext pc, OutputStream os,
PageContextImpl pci = (PageContextImpl) pc;
PageContextImpl dest = factory.getPageContextImpl(factory.getServlet(), req, rsp, null, false, -1, false, register2Thread, true, pc.getRequestTimeout(),
register2RunningThreads, false, false, stateless ? null : pci);
// pci.copyStateTo(dest);
return dest;
}

Expand Down Expand Up @@ -202,4 +207,20 @@ public static boolean isInNativeMethod(Thread thread, boolean defaultValue) {
StackTraceElement ste = stes[0];
return ste.isNativeMethod();
}

public static ExecutorService createExecutorService(int maxThreads) {
if (SystemUtil.JAVA_VERSION >= SystemUtil.JAVA_VERSION_19) {
// FUTURE use newVirtualThreadPerTaskExecutor natively
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType methodType = MethodType.methodType(ExecutorService.class);
MethodHandle methodHandle = lookup.findStatic(Executors.class, "newVirtualThreadPerTaskExecutor", methodType);
return (ExecutorService) methodHandle.invoke();
}
catch (Throwable e) {
ExceptionUtil.rethrowIfNecessary(e);
}
}
return Executors.newFixedThreadPool(maxThreads);
}
}
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.16-SNAPSHOT"/>
<property name="version" value="6.2.0.17-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.16-SNAPSHOT</version>
<version>6.2.0.17-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 6f5e548

Please sign in to comment.