lakshmi-manasa-g commented on code in PR #1636: URL: https://github.com/apache/samza/pull/1636#discussion_r1015641830
########## samza-core/src/main/java/org/apache/samza/container/host/OshiBasedStatisticsGetter.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.samza.container.host; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.NotImplementedException; +import oshi.SystemInfo; +import oshi.software.os.OSProcess; +import oshi.software.os.OperatingSystem; + + +/** + * An implementation of {@link SystemStatisticsGetter} that relies on using oshi framework(https://www.oshi.ooo/) + */ +public class OshiBasedStatisticsGetter implements SystemStatisticsGetter { + // the snapshots of current JVM process and its child processes + private final Map<Integer, OSProcess> previousProcessSnapshots = new HashMap<>(); + + private final OperatingSystem os; + private final int cpuCount; + + public OshiBasedStatisticsGetter() { + this(new SystemInfo()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(SystemInfo si) { + this(si.getOperatingSystem(), si.getHardware().getProcessor().getPhysicalProcessorCount()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(OperatingSystem os, int cpuCount) { + this.os = os; + this.cpuCount = cpuCount; + } + + @Override + public SystemMemoryStatistics getSystemMemoryStatistics() { + throw new NotImplementedException("Not implemented"); + } + + @Override + public ProcessCPUStatistics getProcessCPUStatistics() { + final List<OSProcess> currentProcessAndChildProcesses = getCurrentProcessAndChildProcesses(); + final double totalCPUUsage = getTotalCPUUsage(currentProcessAndChildProcesses); + refreshProcessSnapshots(currentProcessAndChildProcesses); + return new ProcessCPUStatistics(100d * totalCPUUsage / cpuCount); Review Comment: from the desc above, it seems the OperatingSystemMXBean is actually giving out the **fraction** of the total available cpu that the jvm process is using. hence it is between [0,1]. My understanding is based on the following excerpt pasted above >A value of 0.0 means that none of the CPUs were running threads from the JVM process during the recent period of time observed, while a value of 1.0 means that all CPUs were actively running threads from the JVM 100% of the time during the recent period being observed. this to me indicates fraction of the num CPUs being used by jvm process. Also matches the way `getProccessCpuLoad` is used within Samza's JVMMetrics ``` val processCpuLoad = operatingSystemMXBean.getProcessCpuLoad gProcessCpuUsage.set(processCpuLoad * PCT) gProcessCpuUsageProcessors.set(processCpuLoad * operatingSystemMXBean.getAvailableProcessors) gSystemCpuUsage.set(operatingSystemMXBean.getSystemCpuLoad * PCT) ``` Note how we multiply processCpuLoad with 100 to get the percent of total cpu being used by the process. ########## samza-core/src/main/java/org/apache/samza/container/host/OshiBasedStatisticsGetter.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.samza.container.host; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.NotImplementedException; +import oshi.SystemInfo; +import oshi.software.os.OSProcess; +import oshi.software.os.OperatingSystem; + + +/** + * An implementation of {@link SystemStatisticsGetter} that relies on using oshi framework(https://www.oshi.ooo/) + */ +public class OshiBasedStatisticsGetter implements SystemStatisticsGetter { + // the snapshots of current JVM process and its child processes + private final Map<Integer, OSProcess> previousProcessSnapshots = new HashMap<>(); Review Comment: agree that the current impl of the caller/user of this is single threaded but its always good to not assume things about downstream impl when doing an interface impl imo. marking Not thread safe is enough for now i thinkl ########## samza-core/src/main/java/org/apache/samza/container/host/OshiBasedStatisticsGetter.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.samza.container.host; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.NotImplementedException; +import oshi.SystemInfo; +import oshi.software.os.OSProcess; +import oshi.software.os.OperatingSystem; + + +/** + * An implementation of {@link SystemStatisticsGetter} that relies on using oshi framework(https://www.oshi.ooo/) + */ +public class OshiBasedStatisticsGetter implements SystemStatisticsGetter { + // the snapshots of current JVM process and its child processes + private final Map<Integer, OSProcess> previousProcessSnapshots = new HashMap<>(); + + private final OperatingSystem os; + private final int cpuCount; + + public OshiBasedStatisticsGetter() { + this(new SystemInfo()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(SystemInfo si) { + this(si.getOperatingSystem(), si.getHardware().getProcessor().getPhysicalProcessorCount()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(OperatingSystem os, int cpuCount) { + this.os = os; + this.cpuCount = cpuCount; + } + + @Override + public SystemMemoryStatistics getSystemMemoryStatistics() { + throw new NotImplementedException("Not implemented"); + } + + @Override + public ProcessCPUStatistics getProcessCPUStatistics() { + final List<OSProcess> currentProcessAndChildProcesses = getCurrentProcessAndChildProcesses(); Review Comment: ya that makes sense. ########## samza-core/src/main/java/org/apache/samza/container/host/OshiBasedStatisticsGetter.java: ########## @@ -0,0 +1,90 @@ +/* + * 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.samza.container.host; + +import com.google.common.annotations.VisibleForTesting; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.commons.lang3.NotImplementedException; +import oshi.SystemInfo; +import oshi.software.os.OSProcess; +import oshi.software.os.OperatingSystem; + + +/** + * An implementation of {@link SystemStatisticsGetter} that relies on using oshi framework(https://www.oshi.ooo/) + */ +public class OshiBasedStatisticsGetter implements SystemStatisticsGetter { + // the snapshots of current JVM process and its child processes + private final Map<Integer, OSProcess> previousProcessSnapshots = new HashMap<>(); + + private final OperatingSystem os; + private final int cpuCount; + + public OshiBasedStatisticsGetter() { + this(new SystemInfo()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(SystemInfo si) { + this(si.getOperatingSystem(), si.getHardware().getProcessor().getPhysicalProcessorCount()); + } + + @VisibleForTesting + OshiBasedStatisticsGetter(OperatingSystem os, int cpuCount) { + this.os = os; + this.cpuCount = cpuCount; + } + + @Override + public SystemMemoryStatistics getSystemMemoryStatistics() { + throw new NotImplementedException("Not implemented"); + } + + @Override + public ProcessCPUStatistics getProcessCPUStatistics() { + final List<OSProcess> currentProcessAndChildProcesses = getCurrentProcessAndChildProcesses(); Review Comment: for this use case of resource usage/metrics only because the current impl of samza invokes this from exactly one place, has only one listener, so it seems okay. but in general, an interface impl should have a clear contract instead of bubbling up exceptions that are internal to its impl and except the caller/user of the interface to deal with it. that is mostly why the earlier impl of memory stats returned a null in situations w/ exceptions ########## samza-core/src/main/java/org/apache/samza/container/host/StatisticsMonitorImpl.java: ########## @@ -117,23 +118,23 @@ public void run() { } private void sampleStatistics() { - SystemMemoryStatistics statistics = null; + SystemMemoryStatistics memoryStatistics = null; + ProcessCPUStatistics cpuStatistics = null; try { - statistics = statisticsGetter.getSystemMemoryStatistics(); + memoryStatistics = statisticsGetter.getSystemMemoryStatistics(); + cpuStatistics = statisticsGetter.getProcessCPUStatistics(); } catch (Throwable e) { LOG.error("Error during obtaining statistics: ", e); } - + SystemStatistics systemStatistics = new SystemStatistics(cpuStatistics, memoryStatistics); for (Listener listener : listenerSet.keySet()) { - if (statistics != null) { Review Comment: same reply as above. works for this use case and current impl of samza but in general pushing responsbility of interface impl into interface user -- 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-unsubscr...@samza.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org