kfaraz commented on code in PR #18860: URL: https://github.com/apache/druid/pull/18860#discussion_r2639012911
########## 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: > because we're testing abstract autoscaler actions during the task rollover, no specifically the cost-based one. I am not sure if this is entirely true, since cost-based auto-scaler is the only one that supports task count change on rollover right now. The new test should be in the existing `CostBasedAutoScalerIntegrationTest` since it uses the same cluster setup (AFAICT) and verifies an important aspect of the cost-based auto-scaler. In the future, when we add more auto-scalers, we can add separate tests for them. -- 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]
