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


##########
server/src/test/java/org/apache/druid/server/coordinator/duty/CompactSegmentsMinorTaskScalingTest.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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.server.coordinator.duty;
+
+import com.google.common.collect.ImmutableList;
+import org.apache.druid.client.indexing.ClientCompactionTaskQuery;
+import org.apache.druid.client.indexing.ClientMSQContext;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.indexer.CompactionEngine;
+import org.apache.druid.java.util.common.Intervals;
+import org.apache.druid.server.compaction.CompactionCandidate;
+import org.apache.druid.server.compaction.CompactionStatistics;
+import org.apache.druid.server.compaction.CompactionStatus;
+import org.apache.druid.server.compaction.Eligibility;
+import org.apache.druid.server.coordinator.DataSourceCompactionConfig;
+import 
org.apache.druid.server.coordinator.InlineSchemaDataSourceCompactionConfig;
+import org.apache.druid.timeline.DataSegment;
+import org.apache.druid.timeline.partition.NumberedShardSpec;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Tests for the {@code minorCompactionTaskPercent} task-context key applied by
+ * {@link CompactSegments#createCompactionTask}.
+ */
+public class CompactSegmentsMinorTaskScalingTest
+{
+  private static final String DATA_SOURCE = "wiki";
+
+  @Test
+  public void testMinor_msq_defaultDoesNotScale()
+  {
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.MSQ,
+        contextWithMaxNumTasks(10)
+    );
+    Assert.assertEquals(10, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_msq_explicitPercentInTaskContext()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(10);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, 25);
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.MSQ,
+        ctx
+    );
+    Assert.assertEquals(3, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_msq_floorsAtTwo()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(3);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, 40);
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.MSQ,
+        ctx
+    );
+    Assert.assertEquals(2, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_msq_percentOneHundredIsNoOp()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(5);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, 100);
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.MSQ,
+        ctx
+    );
+    Assert.assertEquals(5, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_msq_outOfRangePercentBelowOneThrows()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(10);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, 0);
+    final DruidException thrown = Assert.assertThrows(
+        DruidException.class,
+        () -> buildTask(
+            Eligibility.minor("uncompacted ratio below threshold"),
+            CompactionEngine.MSQ,
+            ctx
+        )
+    );
+    Assert.assertEquals(DruidException.Category.INVALID_INPUT, 
thrown.getCategory());
+  }
+
+  @Test
+  public void testFull_msq_doesNotScale()
+  {
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.FULL,
+        CompactionEngine.MSQ,
+        contextWithMaxNumTasks(5)
+    );
+    Assert.assertEquals(5, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_native_doesNotScale()
+  {
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.NATIVE,
+        contextWithMaxNumTasks(5)
+    );
+    Assert.assertEquals(5, getMaxNumTasks(task));
+  }
+
+  @Test
+  public void testMinor_msq_outOfRangePercentThrows()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(10);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, 200);
+    final DruidException thrown = Assert.assertThrows(
+        DruidException.class,
+        () -> buildTask(
+            Eligibility.minor("uncompacted ratio below threshold"),
+            CompactionEngine.MSQ,
+            ctx
+        )
+    );
+    Assert.assertEquals(DruidException.Category.INVALID_INPUT, 
thrown.getCategory());
+  }
+
+  @Test
+  public void testMinor_msq_stringPercentIsParsed()
+  {
+    final Map<String, Object> ctx = contextWithMaxNumTasks(10);
+    ctx.put(ClientMSQContext.CTX_MINOR_COMPACTION_TASK_PERCENT, "50");
+    final ClientCompactionTaskQuery task = buildTask(
+        Eligibility.minor("uncompacted ratio below threshold"),
+        CompactionEngine.MSQ,
+        ctx
+    );
+    Assert.assertEquals(5, getMaxNumTasks(task));
+  }
+
+  private static ClientCompactionTaskQuery buildTask(
+      Eligibility eligibility,
+      CompactionEngine engine,
+      Map<String, Object> taskContext
+  )
+  {
+    final DataSegment segment = new DataSegment(
+        DATA_SOURCE,
+        Intervals.of("2024-01-01/2024-01-02"),
+        "v1",
+        null,
+        ImmutableList.of(),
+        ImmutableList.of(),
+        new NumberedShardSpec(0, 1),
+        0,
+        100L
+    );

Review Comment:
   ## CodeQL / Deprecated method or constructor invocation
   
   Invoking [DataSegment.DataSegment](1) should be avoided because it has been 
deprecated.
   
   [Show more 
details](https://github.com/apache/druid/security/code-scanning/11175)



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