github-advanced-security[bot] commented on code in PR #18705:
URL: https://github.com/apache/druid/pull/18705#discussion_r2476950584


##########
processing/src/test/java/org/apache/druid/java/util/metrics/cgroups/CpuSetV2Test.java:
##########
@@ -0,0 +1,324 @@
+/*
+ * 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.druid.java.util.metrics.cgroups;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+public class CpuSetV2Test
+{
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  private File cgroupDir;
+  private File procDir;
+  private CgroupDiscoverer discoverer;
+
+  @Before
+  public void setUp() throws IOException
+  {
+    cgroupDir = temporaryFolder.newFolder();
+    procDir = temporaryFolder.newFolder();
+    TestUtils.setUpCgroupsV2(procDir, cgroupDir);
+    discoverer = new ProcCgroupV2Discoverer(procDir.toPath());
+  }
+
+  @Test
+  public void testCpuSetV2Snapshot() throws IOException
+  {
+    // Set up v2 cpuset files directly in cgroupDir (unified hierarchy root)
+    File cgroupRoot = cgroupDir;
+    
+    // Create v2 cpuset files with different names than v1
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.cpus"), 
+        "0-7\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.cpus.effective"), 
+        "0-3\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.mems"), 
+        "0-1\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.mems.effective"), 
+        "0\n".getBytes(StandardCharsets.UTF_8));
+
+    CpuSetV2 cpuSetV2 = new CpuSetV2(discoverer);
+    CpuSet.CpuSetMetric metrics = cpuSetV2.snapshot();
+
+    // Verify CPU set parsing
+    int[] expectedCpus = {0, 1, 2, 3, 4, 5, 6, 7};
+    int[] expectedEffectiveCpus = {0, 1, 2, 3};
+    int[] expectedMems = {0, 1};
+    int[] expectedEffectiveMems = {0};
+
+    Assert.assertArrayEquals("CPU set should be parsed correctly", 
expectedCpus, metrics.getCpuSetCpus());
+    Assert.assertArrayEquals("Effective CPU set should be parsed correctly", 
expectedEffectiveCpus, metrics.getEffectiveCpuSetCpus());
+    Assert.assertArrayEquals("Memory set should be parsed correctly", 
expectedMems, metrics.getCpuSetMems());
+    Assert.assertArrayEquals("Effective memory set should be parsed 
correctly", expectedEffectiveMems, metrics.getEffectiveCpuSetMems());
+  }
+
+  @Test
+  public void testComplexCpuRangesParsing() throws IOException
+  {
+    File cgroupRoot = cgroupDir;
+    
+    // Test complex CPU ranges with mixed single CPUs and ranges
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.cpus"), 
+        "0-2,7,12-14\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.cpus.effective"), 
+        "0,2,7\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.mems"), 
+        "0,2-3\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.mems.effective"), 
+        "0\n".getBytes(StandardCharsets.UTF_8));
+
+    CpuSetV2 cpuSetV2 = new CpuSetV2(discoverer);
+    CpuSet.CpuSetMetric metrics = cpuSetV2.snapshot();
+
+    // 0-2,7,12-14 should expand to [0,1,2,7,12,13,14]
+    int[] expectedCpus = {0, 1, 2, 7, 12, 13, 14};
+    // 0,2,7 should expand to [0,2,7]
+    int[] expectedEffectiveCpus = {0, 2, 7};
+    // 0,2-3 should expand to [0,2,3]
+    int[] expectedMems = {0, 2, 3};
+    // 0 should be [0]
+    int[] expectedEffectiveMems = {0};
+
+    Assert.assertArrayEquals("Complex CPU ranges should be parsed correctly", 
expectedCpus, metrics.getCpuSetCpus());
+    Assert.assertArrayEquals("Complex effective CPU ranges should be parsed 
correctly", expectedEffectiveCpus, metrics.getEffectiveCpuSetCpus());
+    Assert.assertArrayEquals("Complex memory ranges should be parsed 
correctly", expectedMems, metrics.getCpuSetMems());
+    Assert.assertArrayEquals("Single memory node should be parsed correctly", 
expectedEffectiveMems, metrics.getEffectiveCpuSetMems());
+  }
+
+  @Test
+  public void testEmptyCpuSetFiles() throws IOException
+  {
+    File cgroupRoot = cgroupDir;
+    
+    // Create empty cpuset files
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.cpus"), 
+        "\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.cpus.effective"), 
+        "\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpuset.mems"), 
+        "\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), 
"cpuset.mems.effective"), 
+        "\n".getBytes(StandardCharsets.UTF_8));
+
+    CpuSetV2 cpuSetV2 = new CpuSetV2(discoverer);
+    CpuSet.CpuSetMetric metrics = cpuSetV2.snapshot();
+
+    // Empty files should result in empty arrays
+    Assert.assertArrayEquals("Empty CPU file should result in empty array", 
new int[0], metrics.getCpuSetCpus());
+    Assert.assertArrayEquals("Empty effective CPU file should result in empty 
array", new int[0], metrics.getEffectiveCpuSetCpus());
+    Assert.assertArrayEquals("Empty memory file should result in empty array", 
new int[0], metrics.getCpuSetMems());
+    Assert.assertArrayEquals("Empty effective memory file should result in 
empty array", new int[0], metrics.getEffectiveCpuSetMems());
+  }
+
+  @Test
+  public void testMissingCpuSetFiles() throws IOException
+  {
+    // Set up directory but don't create the files
+    File cgroupRoot = cgroupDir;

Review Comment:
   ## Unread local variable
   
   Variable 'File cgroupRoot' is never read.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/10528)



##########
processing/src/test/java/org/apache/druid/java/util/metrics/cgroups/CpuV2Test.java:
##########
@@ -0,0 +1,350 @@
+/*
+ * 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.druid.java.util.metrics.cgroups;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+
+public class CpuV2Test
+{
+  @Rule
+  public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  private File cgroupDir;
+  private File procDir;
+  private CgroupDiscoverer discoverer;
+
+  @Before
+  public void setUp() throws IOException
+  {
+    cgroupDir = temporaryFolder.newFolder();
+    procDir = temporaryFolder.newFolder();
+    TestUtils.setUpCgroupsV2(procDir, cgroupDir);
+    discoverer = new ProcCgroupV2Discoverer(procDir.toPath());
+  }
+
+  @Test
+  public void testCpuV2Snapshot() throws IOException
+  {
+    // Set up v2 files directly in cgroupDir (unified hierarchy root)
+    File cgroupRoot = cgroupDir;
+    
+    // Create cpu.stat with microsecond values
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.stat"), 
+        "user_usec 123456789\nsystem_usec 
987654321\ncore_sched.force_idle_usec 0\n".getBytes(StandardCharsets.UTF_8));
+    
+    // Create cpu.weight (v2 weight)
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.weight"), 
+        "200\n".getBytes(StandardCharsets.UTF_8));
+    
+    // Create cpu.max (quota period)
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.max"), 
+        "150000 100000\n".getBytes(StandardCharsets.UTF_8));
+
+    CpuV2 cpuV2 = new CpuV2(discoverer);
+    Cpu.CpuMetrics metrics = cpuV2.snapshot();
+
+    // Verify the conversion from v2 to v1 format
+    Assert.assertEquals("Weight should be converted to shares", 2048L, 
metrics.getShares());
+    Assert.assertEquals("Quota should be preserved", 150000L, 
metrics.getQuotaUs());
+    Assert.assertEquals("Period should be preserved", 100000L, 
metrics.getPeriodUs());
+    
+    // V2 should not provide jiffies, only microseconds
+    Assert.assertEquals("V2 should not provide user jiffies", -1L, 
metrics.getUserJiffies());
+    Assert.assertEquals("V2 should not provide system jiffies", -1L, 
metrics.getSystemJiffies());
+    Assert.assertEquals("V2 should not provide total jiffies", -1L, 
metrics.getTotalJiffies());
+  }
+
+  @Test
+  public void testCpuV2SnapshotWithMaxQuota() throws IOException
+  {
+    // Set up v2 files with unlimited quota
+    File cgroupRoot = cgroupDir;
+    
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.stat"), 
+        "user_usec 50000000\nsystem_usec 
25000000\n".getBytes(StandardCharsets.UTF_8));
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.weight"), 
+        "100\n".getBytes(StandardCharsets.UTF_8));
+    
+    // cpu.max with "max" means no limit
+    Files.write(Paths.get(cgroupRoot.getAbsolutePath(), "cpu.max"), 
+        "max\n".getBytes(StandardCharsets.UTF_8));
+
+    CpuV2 cpuV2 = new CpuV2(discoverer);
+    Cpu.CpuMetrics metrics = cpuV2.snapshot();
+    Assert.assertEquals("Default weight should convert to default shares", 
1024L, metrics.getShares());
+    Assert.assertEquals("Max quota should be -1", -1L, metrics.getQuotaUs());
+    Assert.assertEquals("Max period should be -1", -1L, metrics.getPeriodUs());
+    Assert.assertEquals("V2 should not provide user jiffies", -1L, 
metrics.getUserJiffies());
+    Assert.assertEquals("V2 should not provide system jiffies", -1L, 
metrics.getSystemJiffies());
+  }
+
+  @Test
+  public void testCpuV2SnapshotWithMissingFiles() throws IOException
+  {
+    // Set up directory but don't create the files
+    File cgroupRoot = cgroupDir;

Review Comment:
   ## Unread local variable
   
   Variable 'File cgroupRoot' is never read.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/10529)



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to