JinwooHwang commented on code in PR #7957:
URL: https://github.com/apache/geode/pull/7957#discussion_r2585819674
##########
geode-core/src/main/java/org/apache/geode/internal/stats50/VMStats50.java:
##########
@@ -150,52 +153,50 @@ public class VMStats50 implements VMStatsContract {
static {
clBean = ManagementFactory.getClassLoadingMXBean();
memBean = ManagementFactory.getMemoryMXBean();
- osBean = ManagementFactory.getOperatingSystemMXBean();
- {
- Method m1 = null;
- Method m2 = null;
- Method m3 = null;
- Object bean = null;
- try {
- Class c =
-
ClassPathLoader.getLatest().forName("com.sun.management.UnixOperatingSystemMXBean");
- if (c.isInstance(osBean)) {
- m1 = c.getMethod("getMaxFileDescriptorCount");
- m2 = c.getMethod("getOpenFileDescriptorCount");
- bean = osBean;
- } else {
- // leave them null
- }
- // Always set ProcessCpuTime
- m3 = osBean.getClass().getMethod("getProcessCpuTime");
- if (m3 != null) {
- m3.setAccessible(true);
- }
- } catch (VirtualMachineError err) {
- SystemFailure.initiateFailure(err);
- // If this ever returns, rethrow the error. We're poisoned
- // now, so don't let this thread continue.
- throw err;
- } catch (Throwable ex) {
- // Whenever you catch Error or Throwable, you must also
- // catch VirtualMachineError (see above). However, there is
- // _still_ a possibility that you are dealing with a cascading
- // error condition, so you also need to check to see if the JVM
- // is still usable:
- logger.warn(ex.getMessage());
- SystemFailure.checkFailure();
- // must be on a platform that does not support unix mxbean
- bean = null;
- m1 = null;
- m2 = null;
- m3 = null;
- } finally {
- unixBean = bean;
- getMaxFileDescriptorCount = m1;
- getOpenFileDescriptorCount = m2;
- getProcessCpuTime = m3;
+
+ // Initialize platform-specific MXBeans using direct interface casting.
+ // This approach eliminates the need for reflection and --add-opens flags.
+ // The com.sun.management package is exported by jdk.management module,
+ // making these interfaces accessible without module violations.
+ OperatingSystemMXBean tempPlatformBean = null;
+ UnixOperatingSystemMXBean tempUnixBean = null;
+
+ try {
+ // Get the standard OperatingSystemMXBean
+ java.lang.management.OperatingSystemMXBean stdOsBean =
+ ManagementFactory.getOperatingSystemMXBean();
+
+ // Cast to com.sun.management.OperatingSystemMXBean for extended metrics
+ // This interface is in the exported com.sun.management package
+ if (stdOsBean instanceof OperatingSystemMXBean) {
+ tempPlatformBean = (OperatingSystemMXBean) stdOsBean;
}
+
+ // Check for Unix-specific interface
+ // This is only available on Unix-like platforms (Linux, macOS, Solaris)
+ if (stdOsBean instanceof UnixOperatingSystemMXBean) {
+ tempUnixBean = (UnixOperatingSystemMXBean) stdOsBean;
+ }
+ } catch (VirtualMachineError err) {
+ SystemFailure.initiateFailure(err);
+ // If this ever returns, rethrow the error. We're poisoned
+ // now, so don't let this thread continue.
+ throw err;
+ } catch (Throwable ex) {
+ // Whenever you catch Error or Throwable, you must also
+ // catch VirtualMachineError (see above). However, there is
+ // _still_ a possibility that you are dealing with a cascading
+ // error condition, so you also need to check to see if the JVM
+ // is still usable:
+ logger.warn("Unable to access platform OperatingSystemMXBean: {}",
ex.getMessage());
Review Comment:
Excellent suggestion, @sboorlagadda. The enhanced error message now clearly
communicates that this affects statistics collection but not core
functionality, which will help operators understand the severity. Updated in
the latest commit. Thank you for improving the operational clarity!
--
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]