Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/553#discussion_r29662694
  
    --- Diff: 
flink-runtime/src/main/scala/org/apache/flink/runtime/taskmanager/TaskManager.scala
 ---
    @@ -1894,6 +1895,52 @@ object TaskManager {
           override def getValue: Double =
             ManagementFactory.getOperatingSystemMXBean().getSystemLoadAverage()
         })
    +    
    +    // Preprocessing steps for registering cpuLoad
    +    // fetch the method to get process CPU load
    +    val getCPULoadMethod: Method = getMethodToFetchCPULoad()
    +
    +    // define the fetchCPULoad method as per the fetched getCPULoadMethod
    +    // dummy initialisation
    +    var fetchCPULoad:(Any) => Double = (obj:Any) => -1
    +
    +    if(getCPULoadMethod != null){
    +      fetchCPULoad = (obj:Any) => 
getCPULoadMethod.invoke(obj).asInstanceOf[Double]
    +    } else {
    +      // Log getProcessCpuLoad method not available for Java 6
    +      LOG.warn("getProcessCpuLoad method not available in the Operating 
System Bean" +
    +        "implementation for this Java runtime environment\n" + 
Thread.currentThread().getStackTrace)
    +    }
    +
    +    metricRegistry.register("cpuLoad", new Gauge[Double] {
    +      override def getValue: Double = {
    +        try{
    +          val osMXBean = ManagementFactory.getOperatingSystemMXBean().
    +            asInstanceOf[com.sun.management.OperatingSystemMXBean]
    +          return fetchCPULoad(osMXBean)
    +        } catch {
    +          case t: Throwable => {
    +            LOG.warn("Error retrieving CPU Load through 
OperatingSystemMXBean", t)
    +            return -1
    +          }
    +        }
    +      }
    +    })
         metricRegistry
       }
    +
    +  /**
    +   * Fetches getProcessCpuLoad method if available in the
    +   *  OperatingSystemMXBean implementation else returns null
    +   * @return
    +   */
    +  private def getMethodToFetchCPULoad(): Method = {
    +    val methodsList = 
classOf[com.sun.management.OperatingSystemMXBean].getMethods()
    +    for(method <- methodsList){
    +      if(method.getName() == "getProcessCpuLoad") {
    +        return method
    +      }
    +    }
    +    return null
    --- End diff --
    
    null return values are not necessary in Scala. You can use Option return 
type.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to