From f5b7fb1e7513b9b603853c58583423549c74c81e Mon Sep 17 00:00:00 2001 From: David Radley <39792797+davidradl@users.noreply.github.com> Date: Thu, 5 Feb 2026 15:13:24 +0000 Subject: [PATCH] [FLINK-21672][test] Amend unit test so it works with IBM java (#27515) Signed-off-by: david_radley@uk.ibm.com --- ...ickyAllocationAndLocalRecoveryTestJob.java | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/flink-end-to-end-tests/flink-local-recovery-and-allocation-test/src/main/java/org/apache/flink/streaming/tests/StickyAllocationAndLocalRecoveryTestJob.java b/flink-end-to-end-tests/flink-local-recovery-and-allocation-test/src/main/java/org/apache/flink/streaming/tests/StickyAllocationAndLocalRecoveryTestJob.java index 65e331e781412..2951d0a19a90a 100644 --- a/flink-end-to-end-tests/flink-local-recovery-and-allocation-test/src/main/java/org/apache/flink/streaming/tests/StickyAllocationAndLocalRecoveryTestJob.java +++ b/flink-end-to-end-tests/flink-local-recovery-and-allocation-test/src/main/java/org/apache/flink/streaming/tests/StickyAllocationAndLocalRecoveryTestJob.java @@ -413,13 +413,34 @@ private boolean isScheduledToCorrectAllocation( private static int getJvmPid() throws Exception { java.lang.management.RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean(); - java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm"); - jvm.setAccessible(true); - sun.management.VMManagement mgmt = (sun.management.VMManagement) jvm.get(runtime); - java.lang.reflect.Method pidMethod = mgmt.getClass().getDeclaredMethod("getProcessId"); - pidMethod.setAccessible(true); - return (int) (Integer) pidMethod.invoke(mgmt); + // Try to use reflection to access sun.management.VMManagement + // This avoids compile-time dependency on internal JDK classes + try { + java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm"); + jvm.setAccessible(true); + Object mgmt = jvm.get(runtime); + + // Check if the class exists (it may not in some JDK distributions like IBM Semeru) + if (mgmt != null) { + java.lang.reflect.Method pidMethod = + mgmt.getClass().getDeclaredMethod("getProcessId"); + pidMethod.setAccessible(true); + return (int) (Integer) pidMethod.invoke(mgmt); + } + } catch (NoSuchFieldException | NoSuchMethodException | ClassCastException e) { + // Fall through to alternative method + } + + // Fallback: parse PID from RuntimeMXBean name (format: "pid@hostname") + String jvmName = runtime.getName(); + int atIndex = jvmName.indexOf('@'); + if (atIndex > 0) { + return Integer.parseInt(jvmName.substring(0, atIndex)); + } + + throw new UnsupportedOperationException( + "Unable to determine JVM PID. This JDK distribution may not support the required internal APIs."); } /** Records the information required to check sticky scheduling after a restart. */