github-advanced-security[bot] commented on code in PR #18254: URL: https://github.com/apache/druid/pull/18254#discussion_r2216407502
########## extensions-core/multi-stage-query/src/test/java/org/apache/druid/msq/indexing/MSQWorkerTaskLauncherRetryTests.java: ########## @@ -0,0 +1,524 @@ +/* + * 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.msq.indexing; + +import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import org.apache.druid.client.indexing.IndexingTotalWorkerCapacityInfo; +import org.apache.druid.client.indexing.IndexingWorkerInfo; +import org.apache.druid.client.indexing.TaskPayloadResponse; +import org.apache.druid.client.indexing.TaskStatusResponse; +import org.apache.druid.error.DruidException; +import org.apache.druid.indexer.RunnerTaskState; +import org.apache.druid.indexer.TaskLocation; +import org.apache.druid.indexer.TaskState; +import org.apache.druid.indexer.TaskStatus; +import org.apache.druid.indexer.TaskStatusPlus; +import org.apache.druid.indexer.report.TaskReport; +import org.apache.druid.indexing.overlord.TaskQueue; +import org.apache.druid.indexing.overlord.supervisor.SupervisorSpec; +import org.apache.druid.indexing.overlord.supervisor.SupervisorStatus; +import org.apache.druid.java.util.common.UOE; +import org.apache.druid.java.util.common.parsers.CloseableIterator; +import org.apache.druid.metadata.LockFilterPolicy; +import org.apache.druid.msq.exec.MSQTasks; +import org.apache.druid.msq.exec.WorkerFailureListener; +import org.apache.druid.rpc.ServiceRetryPolicy; +import org.apache.druid.rpc.UpdateResponse; +import org.apache.druid.rpc.indexing.OverlordClient; +import org.apache.druid.rpc.indexing.SegmentUpdateResponse; +import org.apache.druid.server.coordinator.ClusterCompactionConfig; +import org.apache.druid.server.http.SegmentsToUpdateFilter; +import org.apache.druid.timeline.SegmentId; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.Interval; +import org.junit.Assert; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import javax.annotation.Nullable; +import java.net.URI; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +public class MSQWorkerTaskLauncherRetryTests +{ + + private static final TaskLocation RUNNING_TASK_LOCATION = new TaskLocation("host", 1, 2, null); + + @Test + public void mainThreadBlockingSimulationTest() throws Exception + { + final ExecutorService executors = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(false) + .setNameFormat( + "Controller-simulator-%d") + .build()); + + final TestOverlordClient overlordClient = new TestOverlordClient(); + final int failedWorkerNumber = 2; + final CountDownLatch workerFailedLatch = new CountDownLatch(1); + final CountDownLatch workerStartedLatch = new CountDownLatch(1); + overlordClient.addFailedWorker(2); + overlordClient.addUnknownLocationWorker(1); + + final MSQWorkerTaskLauncher msqWorkerTaskLauncher = new MSQWorkerTaskLauncher( + "controller-id", + "foo", + overlordClient, + ImmutableMap.of(), + TimeUnit.SECONDS.toMillis(5), + new MSQWorkerTaskLauncher.MSQWorkerTaskLauncherConfig() + ); + + try { + + msqWorkerTaskLauncher.start((task, fault) -> { + Assert.assertEquals(failedWorkerNumber, task.getWorkerNumber()); + workerFailedLatch.countDown(); + overlordClient.removeUnknownLocationWorker(1); + + }); + + MockBlockingConsumer mockBlockingConsumer = new MockBlockingConsumer( + msqWorkerTaskLauncher, + 3, + workerStartedLatch + ); + Future<?> futures = executors.submit(mockBlockingConsumer); + workerFailedLatch.await(); + overlordClient.removefailedWorker(failedWorkerNumber); + msqWorkerTaskLauncher.submitForRelaunch(failedWorkerNumber); + workerStartedLatch.countDown(); + // future should be completed in 5 seconds else throw an exception. + Assertions.assertNull(futures.get(5, TimeUnit.SECONDS)); + + } + finally { + msqWorkerTaskLauncher.stop(true); + executors.shutdownNow(); + } + } + + + @Test + public void mainThreadNonBlockingSimulationTest() throws Exception + { + final ExecutorService executors = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setDaemon(false) + .setNameFormat( + "Controller-simulator-%d") + .build()); + final TestOverlordClient overlordClient = new TestOverlordClient(); + final int failedWorkerNumber = 2; + final CountDownLatch workerFailedLatch = new CountDownLatch(1); + final CountDownLatch workerStartedLatch = new CountDownLatch(1); + overlordClient.addFailedWorker(2); + overlordClient.addUnknownLocationWorker(1); + + final MSQWorkerTaskLauncher msqWorkerTaskLauncher = new MSQWorkerTaskLauncher( + "controller-id", + "foo", + overlordClient, + ImmutableMap.of(), + TimeUnit.SECONDS.toMillis(5), + new MSQWorkerTaskLauncher.MSQWorkerTaskLauncherConfig() + ); + try { + msqWorkerTaskLauncher.start((task, fault) -> { + Assert.assertEquals(failedWorkerNumber, task.getWorkerNumber()); + overlordClient.removeUnknownLocationWorker(1); + }); + + MockNonBlockingConsumer mockNonBlockingConsumer = new MockNonBlockingConsumer( + msqWorkerTaskLauncher, + 3, + workerStartedLatch, + (task, fault) -> { + Assert.assertEquals(failedWorkerNumber, task.getWorkerNumber()); + workerFailedLatch.countDown(); + overlordClient.removefailedWorker(failedWorkerNumber); + msqWorkerTaskLauncher.submitForRelaunch(failedWorkerNumber); + } + ); + + Future<?> futures = executors.submit(mockNonBlockingConsumer); + workerFailedLatch.await(); + workerStartedLatch.countDown(); + // future should be completed in 5 seconds else throw an exception. + Assertions.assertNull(futures.get(5, TimeUnit.SECONDS)); + + } + finally { + msqWorkerTaskLauncher.stop(true); + executors.shutdownNow(); + } + } + + + private static class MockBlockingConsumer implements Runnable + { + + private final MSQWorkerTaskLauncher msqWorkerTaskLauncher; + private final int taskCount; + private final CountDownLatch workerStartedLatch; + + public MockBlockingConsumer( + MSQWorkerTaskLauncher msqWorkerTaskLauncher, + int tasksCount, + CountDownLatch workerStartedLatch + ) + { + this.msqWorkerTaskLauncher = msqWorkerTaskLauncher; + this.taskCount = tasksCount; + this.workerStartedLatch = workerStartedLatch; + } + + + @Override + public void run() + { + + // start stages + try { + msqWorkerTaskLauncher.launchWorkersIfNeeded(taskCount); + workerStartedLatch.await(); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + Set<Integer> workerNumbers = new HashSet<>(); + for (int i = 0; i < taskCount; i++) { + workerNumbers.add(i); + } + + // submit work worders + try { + msqWorkerTaskLauncher.waitForWorkers(workerNumbers); + } + catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + private static class MockNonBlockingConsumer implements Runnable + { + + private final MSQWorkerTaskLauncher msqWorkerTaskLauncher; + private final int taskCount; + private final CountDownLatch workerStartedLatch; + + public MockNonBlockingConsumer( + MSQWorkerTaskLauncher msqWorkerTaskLauncher, + int tasksCount, + CountDownLatch workerStartedLatch, + WorkerFailureListener failureListener Review Comment: ## Useless parameter The parameter 'failureListener' is never used. [Show more details](https://github.com/apache/druid/security/code-scanning/9921) -- 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]
