kfaraz commented on code in PR #18860:
URL: https://github.com/apache/druid/pull/18860#discussion_r2638884967


##########
indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/autoscaler/CostBasedAutoScaler.java:
##########
@@ -204,50 +218,54 @@ public int computeOptimalTaskCount(CostMetrics metrics)
       return -1;
     }
 
-    // If idle is already in the ideal range [0.2, 0.6], optimal utilization 
has been achieved.
-    // No scaling is needed - maintain stability by staying at the current 
task count.
-    final double currentIdleRatio = metrics.getPollIdleRatio();
-    if (currentIdleRatio >= 0 && 
WeightedCostFunction.isIdleInIdealRange(currentIdleRatio)) {
-      log.debug(
-          "Idle ratio [%.3f] is in ideal range for supervisorId [%s], no 
scaling needed",
-          currentIdleRatio,
-          supervisorId
-      );
-      return -1;
-    }
-
     int optimalTaskCount = -1;
-    double optimalCost = Double.POSITIVE_INFINITY;
+    Tuple3<Double, Double, Double> optimalCost = new 
Tuple3<>(Double.POSITIVE_INFINITY,

Review Comment:
   Better use a dedicated class than a Tuple or Pair.



##########
indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/SeekableStreamSupervisor.java:
##########
@@ -596,7 +601,12 @@ private boolean changeTaskCount(int desiredActiveTaskCount)
                                          AUTOSCALER_SCALING_TIME_METRIC,
                                          scaleActionStopwatch.millisElapsed()
                                      ));
-      log.info("Changed taskCount to [%s] for supervisor[%s] for 
dataSource[%s].", desiredActiveTaskCount, supervisorId, dataSource);
+      log.info(
+          "Changed taskCount to [%s] for supervisor[%s] for dataSource[%s].",
+          desiredActiveTaskCount,
+          supervisorId,
+          dataSource
+      );

Review Comment:
   Nit: @Fly-Style , could we please revert all the formatting changes from 
this PR, to keep the focus on the actual changes?
   
   I think we can leave out the formatting changes for now.



##########
embedded-tests/src/test/java/org/apache/druid/testing/embedded/indexing/TaskScaleDownRolloverTest.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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.testing.embedded.indexing;
+
+import org.apache.druid.data.input.impl.TimestampSpec;
+import org.apache.druid.indexing.kafka.KafkaIndexTaskModule;
+import org.apache.druid.indexing.kafka.simulate.KafkaResource;
+import org.apache.druid.indexing.kafka.supervisor.KafkaSupervisorSpec;
+import 
org.apache.druid.indexing.seekablestream.supervisor.autoscaler.CostBasedAutoScalerConfig;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.query.DruidMetrics;
+import org.apache.druid.testing.embedded.EmbeddedBroker;
+import org.apache.druid.testing.embedded.EmbeddedClusterApis;
+import org.apache.druid.testing.embedded.EmbeddedCoordinator;
+import org.apache.druid.testing.embedded.EmbeddedDruidCluster;
+import org.apache.druid.testing.embedded.EmbeddedHistorical;
+import org.apache.druid.testing.embedded.EmbeddedIndexer;
+import org.apache.druid.testing.embedded.EmbeddedOverlord;
+import org.apache.druid.testing.embedded.EmbeddedRouter;
+import org.apache.druid.testing.embedded.junit5.EmbeddedClusterTestBase;
+import org.apache.kafka.clients.producer.ProducerRecord;
+import org.hamcrest.Matchers;
+import org.joda.time.DateTime;
+import org.joda.time.DateTimeZone;
+import org.joda.time.Seconds;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import static 
org.apache.druid.indexing.seekablestream.supervisor.autoscaler.CostBasedAutoScaler.OPTIMAL_TASK_COUNT_METRIC;
+
+@SuppressWarnings("SameParameterValue")
+public class TaskScaleDownRolloverTest extends EmbeddedClusterTestBase
+{
+  private static final String TOPIC = 
EmbeddedClusterApis.createTestDatasourceName();
+  private static final String EVENT_TEMPLATE = 
"{\"timestamp\":\"%s\",\"dimension\":\"value%d\",\"metric\":%d}";
+  private static final int PARTITION_COUNT = 50;
+
+  private final EmbeddedIndexer indexer = new EmbeddedIndexer();
+  private final EmbeddedOverlord overlord = new EmbeddedOverlord();
+  private KafkaResource kafkaServer;
+
+  @Override
+  public EmbeddedDruidCluster createCluster()
+  {
+    final EmbeddedDruidCluster cluster = 
EmbeddedDruidCluster.withEmbeddedDerbyAndZookeeper();
+
+    kafkaServer = new KafkaResource()
+    {
+      @Override
+      public void start()
+      {
+        super.start();
+        createTopicWithPartitions(TOPIC, PARTITION_COUNT);
+        produceRecordsToKafka(500, 1);
+      }
+
+      @Override
+      public void stop()
+      {
+        deleteTopic(TOPIC);
+        super.stop();
+      }
+    };
+
+    // Increase worker capacity to handle more tasks
+    indexer.addProperty("druid.segment.handoff.pollDuration", "PT0.1s")
+           .addProperty("druid.worker.capacity", "60");
+
+    overlord.addProperty("druid.indexer.task.default.context", 
"{\"useConcurrentLocks\": true}")
+            .addProperty("druid.manager.segments.pollDuration", "PT0.1s");
+
+    cluster.useLatchableEmitter()
+           .useDefaultTimeoutForLatchableEmitter(20)
+           .addServer(new EmbeddedCoordinator())
+           .addServer(overlord)
+           .addServer(indexer)
+           .addServer(new EmbeddedRouter())
+           .addServer(new EmbeddedBroker())
+           .addServer(new EmbeddedHistorical())
+           .addExtension(KafkaIndexTaskModule.class)
+           .addCommonProperty("druid.monitoring.emissionPeriod", "PT1s")
+           .addResource(kafkaServer);
+
+    return cluster;
+  }
+
+  /**
+   * Tests that scale down happen during task rollover via checkTaskDuration().
+   *
+   * <p>Test flow:</p>
+   * <ol>
+   *   <li>Start supervisor with 20 tasks and 50 partitions, minimal data (500 
records)</li>
+   *   <li>Wait for initial tasks to start running</li>
+   *   <li>Wait for the first task rollover to complete (task duration is 10 
seconds)</li>
+   *   <li>Verify that after rollover, fewer tasks are running due to 
cost-based autoscaler (no ingestion at all)</li>
+   * </ol>
+   *
+   * <p>Scale down during rollover is triggered in {@code 
SeekableStreamSupervisor.checkTaskDuration()}
+   * when all task groups have rolled over and the autoscaler recommends a 
lower task count.</p>
+   */
+  @SuppressWarnings("resource")
+  @Test
+  @Timeout(300)
+  void test_scaleDownDuringTaskRollover()

Review Comment:
   Can this test not be moved to the `CostBasedAutoScalerIntegrationTest` 
itself?



##########
indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/SeekableStreamSupervisor.java:
##########
@@ -468,15 +469,19 @@ public void handle()
             );
             return;
           }
-          log.debug("PendingCompletionTaskGroups is [%s] for supervisor[%s] 
for dataSource[%s]", pendingCompletionTaskGroups,
-                    supervisorId,
-                    dataSource
+          log.debug(
+              "PendingCompletionTaskGroups is [%s] for supervisor[%s] for 
dataSource[%s]", pendingCompletionTaskGroups,
+              supervisorId,
+              dataSource
           );
           final Integer desiredTaskCount = computeDesiredTaskCount.call();
           ServiceMetricEvent.Builder event = ServiceMetricEvent.builder()
                                                                
.setDimension(DruidMetrics.SUPERVISOR_ID, supervisorId)
                                                                
.setDimension(DruidMetrics.DATASOURCE, dataSource)
-                                                               
.setDimension(DruidMetrics.STREAM, getIoConfig().getStream());
+                                                               .setDimension(
+                                                                   
DruidMetrics.STREAM,
+                                                                   
getIoConfig().getStream()
+                                                               );

Review Comment:
   Nit: this change doesn't seem necessary. Did it cross the 120 char limit?



##########
indexing-service/src/main/java/org/apache/druid/indexing/seekablestream/supervisor/autoscaler/CostBasedAutoScaler.java:
##########
@@ -184,7 +198,7 @@ private CostMetrics collectMetrics()
    *
    * @return optimal task count for scale-up, or -1 if no scaling action needed
    */
-  public int computeOptimalTaskCount(CostMetrics metrics)
+  public int computeOptimalTaskCount(CostMetrics metrics, CostComputeMode 
costComputeMode)

Review Comment:
   Instead of keeping a cost compute mode, please keep two separate methods 
that may have some internal common implementation. Methods may be named 
something like:
   `computeOptimalTaskCount` and `computeOptimalTaskCountOnRollover`.



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