You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/05/11 08:34:33 UTC

[GitHub] [pulsar] heesung-sn commented on pull request #15540: [Fix][broker] Fix JDK17 compatibility issues.

heesung-sn commented on PR #15540:
URL: https://github.com/apache/pulsar/pull/15540#issuecomment-1123354744

   Thank you for taking care of this issue. I am wondering if removing reflection could resolve the issue too. Adding `--add-opens` seems radical unless required.
   
   For example, can we directly use the static functions in `InetAddressCachePolicy` for this code?, https://github.com/openjdk/jdk17/blob/master/src/java.base/share/classes/sun/net/InetAddressCachePolicy.java#L154-L160
   
   ```
   static {
           int ttl = DEFAULT_TTL;
           int negativeTtl = DEFAULT_NEGATIVE_TTL;
           try {
               // use reflection to call sun.net.InetAddressCachePolicy's get and getNegative methods for getting
               // effective JDK settings for DNS caching
               Class<?> inetAddressCachePolicyClass = Class.forName("sun.net.InetAddressCachePolicy");
               Method getTTLMethod = inetAddressCachePolicyClass.getMethod("get");
               ttl = (Integer) getTTLMethod.invoke(null);
               Method getNegativeTTLMethod = inetAddressCachePolicyClass.getMethod("getNegative");
               negativeTtl = (Integer) getNegativeTTLMethod.invoke(null);
           } catch (NoSuchMethodException | ClassNotFoundException | InvocationTargetException
                    | IllegalAccessException e) {
               log.warn("Cannot get DNS TTL settings from sun.net.InetAddressCachePolicy class", e);
           }
           TTL = useDefaultTTLWhenSetToForever(ttl, DEFAULT_TTL);
           NEGATIVE_TTL = useDefaultTTLWhenSetToForever(negativeTtl, DEFAULT_NEGATIVE_TTL);
       }
   
   ```
   
   Similarly for this code path, we can use the static functions in `ManagementFactoryHelper`
   https://github.com/openjdk/jdk17/blob/master/src/java.management/share/classes/sun/management/ManagementFactoryHelper.java
   
   
   ```
       private static Method getTotalSafepointTimeHandle;
       private static Method getSafepointCountHandle;
   
       private Map<String, GCMetrics> gcMetricsMap = new HashMap<>();
   
       static {
           try {
               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);
           }
       }
   
   ```
   
   


-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org