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

eolivelli pushed a commit to branch 2.7.2_ds_rootless
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 7e1f9f06f2a4dd031169a8890151eda8042bfc0b
Author: Enrico Olivelli <eolive...@gmail.com>
AuthorDate: Fri Feb 12 21:55:32 2021 +0100

    Allow to build Pulsar with JDK11 and -Dmaven.compiler.release=8 (#9580)
    
    * Allow to build Pulsar with JDK11 and -Dmaven.compiler.release=8
    
    * fix style
    
    Co-authored-by: Enrico Olivelli <eolive...@apache.org>
    (cherry picked from commit 731c18f5b98513ca9018e45ed1fdeff298a5312b)
---
 .../pulsar/client/impl/MultiMessageIdImpl.java     |  3 +-
 .../common/stats/JvmDefaultGCMetricsLogger.java    | 45 ++++++++++++++++++----
 .../stats/JvmDefaultGCMetricsLoggerTest.java       | 39 +++++++++++++++++++
 3 files changed, 78 insertions(+), 9 deletions(-)

diff --git 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiMessageIdImpl.java
 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiMessageIdImpl.java
index e6e7557..dfe995e 100644
--- 
a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiMessageIdImpl.java
+++ 
b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MultiMessageIdImpl.java
@@ -23,7 +23,6 @@ import java.util.Map.Entry;
 import java.util.Objects;
 import lombok.Getter;
 import org.apache.pulsar.client.api.MessageId;
-import sun.reflect.generics.reflectiveObjects.NotImplementedException;
 
 /**
  * A MessageId implementation that contains a map of <partitionName, 
MessageId>.
@@ -42,7 +41,7 @@ public class MultiMessageIdImpl implements MessageId {
     //  https://github.com/apache/pulsar/issues/4940
     @Override
     public byte[] toByteArray() {
-        throw new NotImplementedException();
+        throw new UnsupportedOperationException();
     }
 
     @Override
diff --git 
a/pulsar-common/src/main/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLogger.java
 
b/pulsar-common/src/main/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLogger.java
index 307daa1..4726a2c 100644
--- 
a/pulsar-common/src/main/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLogger.java
+++ 
b/pulsar-common/src/main/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLogger.java
@@ -19,14 +19,18 @@
 package org.apache.pulsar.common.stats;
 
 import com.google.common.collect.Maps;
+
 import java.lang.management.GarbageCollectorMXBean;
 import java.lang.management.ManagementFactory;
+import java.lang.reflect.Method;
 import java.util.List;
 import java.util.Map;
+
+import lombok.SneakyThrows;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@SuppressWarnings({"restriction", "checkstyle:JavadocType"})
+@SuppressWarnings({"checkstyle:JavadocType"})
 public class JvmDefaultGCMetricsLogger implements JvmGCMetricsLogger {
 
     private static final Logger log = 
LoggerFactory.getLogger(JvmDefaultGCMetricsLogger.class);
@@ -36,19 +40,46 @@ public class JvmDefaultGCMetricsLogger implements 
JvmGCMetricsLogger {
     private volatile long accumulatedFullGcTime = 0;
     private volatile long currentFullGcTime = 0;
 
-    @SuppressWarnings("restriction")
-    private static sun.management.HotspotRuntimeMBean runtime;
+    private static Object /*sun.management.HotspotRuntimeMBean*/ runtime;
+    private static Method getTotalSafepointTimeHandle;
+    private static Method getSafepointCountHandle;
 
     private Map<String, GCMetrics> gcMetricsMap = Maps.newHashMap();
 
     static {
         try {
-            runtime = 
sun.management.ManagementFactoryHelper.getHotspotRuntimeMBean();
-        } catch (Exception e) {
+            runtime = Class.forName("sun.management.ManagementFactoryHelper")
+                    .getMethod("getHotspotRuntimeMBean")
+                    .invoke(null);
+            getTotalSafepointTimeHandle = 
runtime.getClass().getMethod("getTotalSafepointTime");
+            getTotalSafepointTimeHandle.setAccessible(true);
+            getSafepointCountHandle = 
runtime.getClass().getMethod("getSafepointCount");
+            getSafepointCountHandle.setAccessible(true);
+
+            // try to use the methods
+            getTotalSafepointTimeHandle.invoke(runtime);
+            getSafepointCountHandle.invoke(runtime);
+        } catch (Throwable e) {
             log.warn("Failed to get Runtime bean", e);
         }
     }
 
+    @SneakyThrows
+    static long getTotalSafepointTime() {
+        if (getTotalSafepointTimeHandle == null) {
+            return -1;
+        }
+        return (long) getTotalSafepointTimeHandle.invoke(runtime);
+    }
+
+    @SneakyThrows
+    static long getSafepointCount() {
+        if (getTotalSafepointTimeHandle == null) {
+            return -1;
+        }
+        return (long) getSafepointCountHandle.invoke(runtime);
+    }
+
     /**
      * Metrics for the Garbage Collector.
      */
@@ -92,8 +123,8 @@ public class JvmDefaultGCMetricsLogger implements 
JvmGCMetricsLogger {
              * that the application has been stopped for safepoint operations.
              * 
http://www.docjar.com/docs/api/sun/management/HotspotRuntimeMBean.html
              */
-            long newSafePointTime = runtime.getTotalSafepointTime();
-            long newSafePointCount = runtime.getSafepointCount();
+            long newSafePointTime = getTotalSafepointTime();
+            long newSafePointCount = getSafepointCount();
             currentFullGcTime = newSafePointTime - accumulatedFullGcTime;
             currentFullGcCount = newSafePointCount - accumulatedFullGcCount;
             accumulatedFullGcTime = newSafePointTime;
diff --git 
a/pulsar-common/src/test/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLoggerTest.java
 
b/pulsar-common/src/test/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLoggerTest.java
new file mode 100644
index 0000000..a51329d
--- /dev/null
+++ 
b/pulsar-common/src/test/java/org/apache/pulsar/common/stats/JvmDefaultGCMetricsLoggerTest.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.common.stats;
+
+import lombok.extern.slf4j.Slf4j;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertNotEquals;
+
+
+@Slf4j
+public class JvmDefaultGCMetricsLoggerTest {
+
+    @Test
+    public void testInvokeJVMInternals() {
+      long safePointCount = JvmDefaultGCMetricsLogger.getSafepointCount();
+      long totalSafePointTime = 
JvmDefaultGCMetricsLogger.getTotalSafepointTime();
+      log.info("safePointCount {} totalSafePointTime {}", safePointCount, 
totalSafePointTime);
+      assertNotEquals(safePointCount, -1);
+      assertNotEquals(totalSafePointTime, -1);
+    }
+}

Reply via email to