Vivek1106-04 commented on code in PR #12277: URL: https://github.com/apache/seatunnel/pull/12277#discussion_r3998540758
########## seatunnel-engine/seatunnel-engine-server/src/test/java/org/apache/seatunnel/engine/server/TaskExecutionServiceCooperativeBudgetTest.java: ########## @@ -0,0 +1,168 @@ +/* + * 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.seatunnel.engine.server; + +import org.apache.seatunnel.engine.common.utils.PassiveCompletableFuture; +import org.apache.seatunnel.engine.server.execution.CooperativeWorkerBudget; +import org.apache.seatunnel.engine.server.execution.FixedCallTestTimeTask; +import org.apache.seatunnel.engine.server.execution.Task; +import org.apache.seatunnel.engine.server.execution.TaskExecutionState; +import org.apache.seatunnel.engine.server.execution.TaskGroup; +import org.apache.seatunnel.engine.server.execution.TaskGroupDefaultImpl; +import org.apache.seatunnel.engine.server.execution.TaskGroupLocation; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import lombok.NonNull; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static org.apache.seatunnel.engine.server.execution.ExecutionState.FINISHED; +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Covers the promotion budget of cooperative workers, configured by {@code + * seatunnel_cooperative_worker_budget.yaml} with a global limit of 2 and a per job limit of 1. + * + * <p>Every cooperative task here needs much longer than the call timer allows, so without a budget + * each one of them would promote its own worker thread. + */ +public class TaskExecutionServiceCooperativeBudgetTest + extends AbstractSeaTunnelServerTest<TaskExecutionServiceCooperativeBudgetTest> { + + private static final int MAX_PROMOTED_WORKERS_PER_JOB = 1; + private static final long SLOW_CALL_TIME_MILLIS = 300; + private static final int SLOW_TASK_COUNT = 8; + + private static String previousConfigFile; + + @BeforeAll + public void before() { + previousConfigFile = System.getProperty("seatunnel.config"); + System.setProperty("seatunnel.config", configFilePath()); + super.before(); + } + + @AfterAll + public void after() { + try { + super.after(); + } finally { + if (previousConfigFile == null) { + System.clearProperty("seatunnel.config"); + } else { + System.setProperty("seatunnel.config", previousConfigFile); + } + } + } + + @Test + public void testSlowCooperativeTasksCannotPromoteMoreWorkersThanTheBudget() + throws InterruptedException { + TaskExecutionService taskExecutionService = server.getTaskExecutionService(); + CooperativeWorkerBudget budget = taskExecutionService.getCooperativeWorkerBudget(); + assertEquals(2, budget.getMaxPromotedWorkers()); + assertEquals(MAX_PROMOTED_WORKERS_PER_JOB, budget.getMaxPromotedWorkersPerJob()); + + long testJobId = System.currentTimeMillis(); + AtomicBoolean stop = new AtomicBoolean(false); + CopyOnWriteArrayList<Long> lagList = new CopyOnWriteArrayList<>(); + List<Task> tasks = new ArrayList<>(); + for (int i = 0; i < SLOW_TASK_COUNT; i++) { + tasks.add( + new FixedCallTestTimeTask( + SLOW_CALL_TIME_MILLIS, "slow-task-" + i, stop, lagList)); + } + + TaskGroupDefaultImpl taskGroup = + new TaskGroupDefaultImpl( + new TaskGroupLocation(testJobId, 1, 1), "cooperative-budget", tasks); + + PassiveCompletableFuture<TaskExecutionState> future = + deployLocalTask(taskExecutionService, taskGroup); + + // The budget must deny promotions once the job holds its single promoted worker, and the + // tasks that were denied must keep running instead of being dropped. + await().atMost(30, TimeUnit.SECONDS) + .untilAsserted( + () -> { + assertTrue(budget.getDeniedPromotions() > 0); + assertEquals( + MAX_PROMOTED_WORKERS_PER_JOB, + budget.getPromotedWorkers(testJobId)); + }); + + // Every task keeps making progress while the budget is exhausted, so the shared queue is + // still served: each task must be called more than once. + await().atMost(30, TimeUnit.SECONDS) + .untilAsserted(() -> assertTrue(lagList.size() >= SLOW_TASK_COUNT)); Review Comment: Agreed, the shared `lagList` assertion proved nothing about individual tasks. Replaced in f6d0251. The test tasks are now `CooperativeProbeTask`, which keeps its own start latch and call counter, so the test asserts per task instead of in aggregate: every one of the 8 tasks must enter its first call, and every one must reach a second call, with the failure message naming the task that stopped. The shared list is gone. The readiness case you asked for is the second test, `testQueuedTaskStartsWhileDeniedPromotionsBlockEveryWorker`: three gated tasks whose calls block, plus one task queued behind them whose start is what opens the gate. It fails on the old logic (verified locally by disabling the guard) and passes with the availability accounting described in the other thread. -- 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]
