This is an automated email from the ASF dual-hosted git repository.

smiklosovic pushed a commit to branch CASSANDRA-21037-4.1
in repository https://gitbox.apache.org/repos/asf/cassandra.git

commit c8d2fbca20db2daa08e3328e085ce886b6501b78
Author: Stefan Miklosovic <[email protected]>
AuthorDate: Thu Nov 27 16:57:29 2025 +0100

    patch
---
 .../org/apache/cassandra/service/GCInspector.java  | 78 +++++++++++++++-------
 1 file changed, 55 insertions(+), 23 deletions(-)

diff --git a/src/java/org/apache/cassandra/service/GCInspector.java 
b/src/java/org/apache/cassandra/service/GCInspector.java
index 8f922156ca..214a8a172f 100644
--- a/src/java/org/apache/cassandra/service/GCInspector.java
+++ b/src/java/org/apache/cassandra/service/GCInspector.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.atomic.AtomicReference;
 
 import javax.management.MBeanServer;
@@ -56,36 +57,34 @@ public class GCInspector implements NotificationListener, 
GCInspectorMXBean
 
     /*
      * The field from java.nio.Bits that tracks the total number of allocated
-     * bytes of direct memory requires via ByteBuffer.allocateDirect that have 
not been GCed.
+     * bytes of direct memory requested via ByteBuffer.allocateDirect that 
have not been GCed.
      */
-    final static Field BITS_TOTAL_CAPACITY;
+    final static Field BITS_TOTAL_CAPACITY_JAVA_8;
+    final static Field BITS_TOTAL_CAPACITY_JAVA_11;
 
-    
     static
     {
-        Field temp = null;
+        Class<?> bitsClass = null;
+
         try
         {
-            Class<?> bitsClass = Class.forName("java.nio.Bits");
-            Field f;
-            try
-            {
-                f = bitsClass.getDeclaredField("totalCapacity");
-            }
-            catch (NoSuchFieldException ex)
-            {
-                // in Java11 it changed name to "TOTAL_CAPACITY"
-                f = bitsClass.getDeclaredField("TOTAL_CAPACITY");
-            }
-            f.setAccessible(true);
-            temp = f;
+            bitsClass = Class.forName("java.nio.Bits");
         }
         catch (Throwable t)
         {
-            logger.debug("Error accessing field of java.nio.Bits", t);
-            //Don't care, will just return the dummy value -1 if we can't get 
at the field in this JVM
+            logger.debug("Error returning class of java.nio.Bits", t);
+        }
+
+        if (bitsClass != null)
+        {
+            BITS_TOTAL_CAPACITY_JAVA_8 = getField(bitsClass, "totalCapacity");
+            BITS_TOTAL_CAPACITY_JAVA_11 = getField(bitsClass, 
"TOTAL_CAPACITY");
+        }
+        else
+        {
+            BITS_TOTAL_CAPACITY_JAVA_8 = null;
+            BITS_TOTAL_CAPACITY_JAVA_11 = null;
         }
-        BITS_TOTAL_CAPACITY = temp;
     }
 
     static final class State
@@ -326,14 +325,47 @@ public class GCInspector implements NotificationListener, 
GCInspectorMXBean
 
     private static long getAllocatedDirectMemory()
     {
-        if (BITS_TOTAL_CAPACITY == null) return -1;
+        long fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_8, true);
+
+        if (fieldValue == -1)
+            fieldValue = getFieldValue(BITS_TOTAL_CAPACITY_JAVA_11, true);
+
+        return fieldValue;
+    }
+
+    private static Field getField(Class<?> clazz, String fieldName)
+    {
+        try
+        {
+            Field field = clazz.getDeclaredField(fieldName);
+            field.setAccessible(true);
+            return field;
+        }
+        catch (Throwable t)
+        {
+            logger.trace("Error accessing field {} of {}", fieldName, 
clazz.getName(), t);
+            // Return null to indicate failure
+            return null;
+        }
+    }
+
+    /**
+     * Retrieves the value of a Field, handling both regular long fields and 
AtomicLong fields.
+     *
+     * @param field the Field to retrieve the value from
+     * @param isAtomicLong true if the field is an AtomicLong, false if it's a 
regular long
+     * @return the field value, or -1 if retrieval fails or field is null.
+     */
+    private static long getFieldValue(Field field, boolean isAtomicLong)
+    {
+        if (field == null) return -1;
         try
         {
-            return BITS_TOTAL_CAPACITY.getLong(null);
+            return isAtomicLong ? ((AtomicLong) field.get(null)).get() : 
field.getLong(null);
         }
         catch (Throwable t)
         {
-            logger.trace("Error accessing field of java.nio.Bits", t);
+            logger.trace("Error accessing field value of {}", field.getName(), 
t);
             //Don't care how or why we failed to get the value in this JVM. 
Return -1 to indicate failure
             return -1;
         }


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

Reply via email to