From 1c149e85924410e3609fb216d3c9a2c33f63e498 Mon Sep 17 00:00:00 2001 From: ruotiantang Date: Mon, 16 Dec 2024 15:22:15 +0800 Subject: [PATCH 1/3] =?UTF-8?q?bugfix:=20=E5=8D=87=E7=BA=A7JDK17=E5=AF=BC?= =?UTF-8?q?=E8=87=B4worker=E6=97=A0=E6=B3=95=E5=BC=BA=E6=9D=80=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=20#11320?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/process/BkProcessTree.java | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java index 99a1c1c1878..3042452201b 100644 --- a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java +++ b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java @@ -630,18 +630,9 @@ private static final class UnixReflection { private UnixReflection() { } + // TODO: 升级到JDK17后,这里可以使用 java.lang.Process 重构 public static void destroy(int pid, boolean forceFlag) throws IllegalAccessException, InvocationTargetException { - if (isPreJava8()) { - DESTROY_PROCESS.invoke((Object) null, pid); - } else { - DESTROY_PROCESS.invoke((Object) null, pid, forceFlag); - } - - } - - private static boolean isPreJava8() { - int javaVersionAsAnInteger = Integer.parseInt(System.getProperty("java.version").replaceAll("\\.", "").replaceAll("_", "").substring(0, 2)); - return javaVersionAsAnInteger < 18; + DESTROY_PROCESS.invoke((Object) null, pid, forceFlag); } static { @@ -650,11 +641,7 @@ private static boolean isPreJava8() { Class clazz = Class.forName("java.lang.UNIXProcess"); PID_FIELD = clazz.getDeclaredField("pid"); PID_FIELD.setAccessible(true); - if (isPreJava8()) { - DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE); - } else { - DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE, Boolean.TYPE); - } + DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE, Boolean.TYPE); DESTROY_PROCESS.setAccessible(true); } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { From d392f91871684956fab89c1eadd702d0931e7259 Mon Sep 17 00:00:00 2001 From: ruotiantang Date: Mon, 16 Dec 2024 17:30:39 +0800 Subject: [PATCH 2/3] =?UTF-8?q?bugfix:=20=E5=8D=87=E7=BA=A7JDK17=E5=AF=BC?= =?UTF-8?q?=E8=87=B4worker=E6=97=A0=E6=B3=95=E5=BC=BA=E6=9D=80=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=20#11320?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/process/BkProcessTree.java | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java index 3042452201b..99a756e4460 100644 --- a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java +++ b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java @@ -632,7 +632,19 @@ private UnixReflection() { // TODO: 升级到JDK17后,这里可以使用 java.lang.Process 重构 public static void destroy(int pid, boolean forceFlag) throws IllegalAccessException, InvocationTargetException { - DESTROY_PROCESS.invoke((Object) null, pid, forceFlag); + if (isJava17()) { + destroyProcessJava17(pid, forceFlag); + } else { + DESTROY_PROCESS.invoke((Object) null, pid, forceFlag); + } + } + + private static boolean isJava17() { + String javaVersion = System.getProperty("java.version"); + if (javaVersion.startsWith("17")) { + return true; + } + return false; } static { @@ -642,7 +654,6 @@ public static void destroy(int pid, boolean forceFlag) throws IllegalAccessExcep PID_FIELD = clazz.getDeclaredField("pid"); PID_FIELD.setAccessible(true); DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE, Boolean.TYPE); - DESTROY_PROCESS.setAccessible(true); } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { x = new LinkageError(); @@ -650,6 +661,39 @@ public static void destroy(int pid, boolean forceFlag) throws IllegalAccessExcep throw x; } } + + private static void destroyProcessJava17(int pid, boolean forceFlag) { + try { + // 获取ProcessHandle类 + Class processHandleClass = Class.forName("java.lang.ProcessHandle"); + + // 获取ProcessHandle.of方法 + Method ofMethod = processHandleClass.getMethod("of", long.class); + Object optionalProcessHandle = ofMethod.invoke(null, pid); + + // 获取Optional.isPresent方法 + Class optionalClass = Class.forName("java.util.Optional"); + Method isPresentMethod = optionalClass.getMethod("isPresent"); + boolean isPresent = (boolean) isPresentMethod.invoke(optionalProcessHandle); + + if (isPresent) { + // 获取Optional.get方法 + Method getMethod = optionalClass.getMethod("get"); + Object processHandle = getMethod.invoke(optionalProcessHandle); + + // 获取ProcessHandle.destroy方法 + Method destroyMethod; + if (forceFlag) { + destroyMethod = processHandleClass.getMethod("destroyForcibly"); + } else { + destroyMethod = processHandleClass.getMethod("destroy"); + } + destroyMethod.invoke(processHandle); + } + } catch (Exception e) { + BkProcessTree.log("Failed to terminate pid=" + pid, e); + } + } } public abstract class UnixProcess extends BkProcessTree.OSProcess { From 0a6ca50ebc7b4f88c7e3279340090745aecfe84f Mon Sep 17 00:00:00 2001 From: ruotiantang Date: Tue, 17 Dec 2024 09:58:44 +0800 Subject: [PATCH 3/3] =?UTF-8?q?bugfix:=20=E5=8D=87=E7=BA=A7JDK17=E5=AF=BC?= =?UTF-8?q?=E8=87=B4worker=E6=97=A0=E6=B3=95=E5=BC=BA=E6=9D=80=E8=BF=9B?= =?UTF-8?q?=E7=A8=8B=20#11320?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/tencent/process/BkProcessTree.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java index 99a756e4460..1b190197b83 100644 --- a/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java +++ b/src/backend/ci/core/worker/worker-common/src/main/java/com/tencent/process/BkProcessTree.java @@ -624,8 +624,8 @@ public synchronized EnvVars getEnvironmentVariables() { } private static final class UnixReflection { - private static final Field PID_FIELD; - private static final Method DESTROY_PROCESS; + private static Field PID_FIELD; + private static Method DESTROY_PROCESS; private UnixReflection() { } @@ -633,8 +633,10 @@ private UnixReflection() { // TODO: 升级到JDK17后,这里可以使用 java.lang.Process 重构 public static void destroy(int pid, boolean forceFlag) throws IllegalAccessException, InvocationTargetException { if (isJava17()) { + BkProcessTree.log("Killing by jdk17"); destroyProcessJava17(pid, forceFlag); } else { + BkProcessTree.log("Killing by jdk8"); DESTROY_PROCESS.invoke((Object) null, pid, forceFlag); } } @@ -656,9 +658,13 @@ private static boolean isJava17() { DESTROY_PROCESS = clazz.getDeclaredMethod("destroyProcess", Integer.TYPE, Boolean.TYPE); DESTROY_PROCESS.setAccessible(true); } catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) { - x = new LinkageError(); - x.initCause(e); - throw x; + if (isJava17()) { + BkProcessTree.log("java17 ignore java8 class error"); + } else { + x = new LinkageError(); + x.initCause(e); + throw x; + } } } @@ -689,6 +695,8 @@ private static void destroyProcessJava17(int pid, boolean forceFlag) { destroyMethod = processHandleClass.getMethod("destroy"); } destroyMethod.invoke(processHandle); + } else { + BkProcessTree.log("Failed to terminate pid=" + pid + "no present"); } } catch (Exception e) { BkProcessTree.log("Failed to terminate pid=" + pid, e);