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

[GitHub] [samza] lakshmi-manasa-g commented on a diff in pull request #1636: SAMZA-2762: new cpu usage metric which counts child processes usage

lakshmi-manasa-g commented on code in PR #1636:
URL: https://github.com/apache/samza/pull/1636#discussion_r1014326963


##########
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/)

Review Comment:
   would be good to more docs on how the Stat getter works - aka previous snapshots, 
   we should also call out if the impl is thread safe and if not, clarify why its not a problem



##########
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:
   should we use ConcurrentHashMap?



##########
samza-core/src/main/java/org/apache/samza/container/host/ProcessCPUStatistics.java:
##########
@@ -0,0 +1,63 @@
+/*
+ * 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 java.util.Objects;
+
+
+/**
+ * A {@link ProcessCPUStatistics} object represents recent CPU usage percentage about the container process(including its child processes)

Review Comment:
   small clarification might be good to say that the percentage is against total cpu of host



##########
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;

Review Comment:
   samza already uses `UnsupportedOperationException` to indicate these kind of scenarios. can we use the same?



##########
samza-core/src/main/java/org/apache/samza/container/host/PosixCommandBasedStatisticsGetter.java:
##########
@@ -21,6 +21,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import org.apache.commons.lang3.NotImplementedException;

Review Comment:
   same as above.
   samza already uses `UnsupportedOperationException` to indicate these kind of scenarios. can we use the same?



##########
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:
   wondering if we should get this list fresh every time?
   one scenario could be that the first time(s) this method is called not all child processes are up and they may come up later times the method is called.
   just looking to see if this can be optimized. 



##########
samza-core/src/main/java/org/apache/samza/container/host/SystemStatistics.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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 java.util.Objects;
+
+/**
+ * A {@link SystemStatistics} object represents system related information about the physical process that runs the
+ * {@link org.apache.samza.container.SamzaContainer}.
+ */
+public class SystemStatistics {
+
+  private final ProcessCPUStatistics cpuStatistics;
+  private final SystemMemoryStatistics memoryStatistics;
+
+  public SystemStatistics(ProcessCPUStatistics cpuStatistics, SystemMemoryStatistics memoryStatistics) {

Review Comment:
   what if either or both stats are null?
   how do we handle it?



##########
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:
   similar to the memory one in PosxCommandBasedStatsGetter, should we catch exceptions and return a null?



##########
samza-core/src/main/java/org/apache/samza/container/host/SystemStatisticsGetter.java:
##########
@@ -30,4 +30,11 @@ public interface SystemStatisticsGetter {
    * @return {@link SystemMemoryStatistics} for the Samza container
    */
   SystemMemoryStatistics getSystemMemoryStatistics();
+
+  /**
+   * Returns the {@link ProcessCPUStatistics} for the current Samza container process(includes its child processes)
+   *
+   * @return {@link ProcessCPUStatistics} for the Samza container process
+   */
+  ProcessCPUStatistics getProcessCPUStatistics();

Review Comment:
   
   should we call ProcessCPUStats as SystemCPUStats similar to the mem one? 
   but maybe that is confusing since process and system cpu mean different things. 
   not a big concern at all. feel free to drop this one if it was chosen to avoid confusion



##########
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:
   this accounts for absolute cpu usage right as it takes into account the num cores on the host (if the answer to the previous q is yes)
   
   if so, we should add to the PR desc saying this is the case as the currently existing cpu reporting does not take into account core-count on host. This will impact downstream usage if users look to replace existing cpu metric with the new one.



##########
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:
   seems like a good check to have to prevent null stats from causing issues in a listener.
   



-- 
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@samza.apache.org

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