Search in sources :

Example 56 with Task

use of org.apache.druid.indexing.common.task.Task in project druid by druid-io.

the class ForkingTaskRunnerTest method testTaskStatusWhenTaskProcessSucceedsTaskSucceeds.

@Test
public void testTaskStatusWhenTaskProcessSucceedsTaskSucceeds() throws ExecutionException, InterruptedException {
    ObjectMapper mapper = new DefaultObjectMapper();
    Task task = NoopTask.create();
    ForkingTaskRunner forkingTaskRunner = new ForkingTaskRunner(new ForkingTaskRunnerConfig(), new TaskConfig(null, null, null, null, ImmutableList.of(), false, new Period("PT0S"), new Period("PT10S"), ImmutableList.of(), false, false, TaskConfig.BATCH_PROCESSING_MODE_DEFAULT.name()), new WorkerConfig(), new Properties(), new NoopTaskLogs(), mapper, new DruidNode("middleManager", "host", false, 8091, null, true, false), new StartupLoggingConfig()) {

        @Override
        ProcessHolder runTaskProcess(List<String> command, File logFile, TaskLocation taskLocation) throws IOException {
            ProcessHolder processHolder = Mockito.mock(ProcessHolder.class);
            Mockito.doNothing().when(processHolder).registerWithCloser(ArgumentMatchers.any());
            Mockito.doNothing().when(processHolder).shutdown();
            for (String param : command) {
                if (param.endsWith("status.json")) {
                    mapper.writeValue(new File(param), TaskStatus.success(task.getId()));
                    break;
                }
            }
            return processHolder;
        }

        @Override
        int waitForTaskProcessToComplete(Task task, ProcessHolder processHolder, File logFile, File reportsFile) {
            return 0;
        }
    };
    final TaskStatus status = forkingTaskRunner.run(task).get();
    Assert.assertEquals(TaskState.SUCCESS, status.getStatusCode());
    Assert.assertNull(status.getErrorMsg());
}
Also used : NoopTaskLogs(org.apache.druid.tasklogs.NoopTaskLogs) Task(org.apache.druid.indexing.common.task.Task) NoopTask(org.apache.druid.indexing.common.task.NoopTask) Period(org.joda.time.Period) TaskConfig(org.apache.druid.indexing.common.config.TaskConfig) Properties(java.util.Properties) TaskStatus(org.apache.druid.indexer.TaskStatus) TaskLocation(org.apache.druid.indexer.TaskLocation) StartupLoggingConfig(org.apache.druid.server.log.StartupLoggingConfig) ForkingTaskRunnerConfig(org.apache.druid.indexing.overlord.config.ForkingTaskRunnerConfig) WorkerConfig(org.apache.druid.indexing.worker.config.WorkerConfig) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) DruidNode(org.apache.druid.server.DruidNode) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 57 with Task

use of org.apache.druid.indexing.common.task.Task in project druid by druid-io.

the class RemoteTaskRunnerRunPendingTasksConcurrencyTest method testConcurrency.

// This task reproduces the races described in https://github.com/apache/druid/issues/2842
@Test(timeout = 60_000L)
public void testConcurrency() throws Exception {
    rtrTestUtils.makeWorker("worker0", 3);
    rtrTestUtils.makeWorker("worker1", 3);
    remoteTaskRunner = rtrTestUtils.makeRemoteTaskRunner(new TestRemoteTaskRunnerConfig(new Period("PT3600S")) {

        @Override
        public int getPendingTasksRunnerNumThreads() {
            return 2;
        }
    });
    int numTasks = 6;
    ListenableFuture<TaskStatus>[] results = new ListenableFuture[numTasks];
    Task[] tasks = new Task[numTasks];
    // 2 tasks
    for (int i = 0; i < 2; i++) {
        tasks[i] = TestTasks.unending("task" + i);
        results[i] = (remoteTaskRunner.run(tasks[i]));
    }
    waitForBothWorkersToHaveUnackedTasks();
    // 3 more tasks, all of which get queued up
    for (int i = 2; i < 5; i++) {
        tasks[i] = TestTasks.unending("task" + i);
        results[i] = (remoteTaskRunner.run(tasks[i]));
    }
    // simulate completion of task0 and task1
    mockWorkerRunningAndCompletionSuccessfulTasks(tasks[0], tasks[1]);
    Assert.assertEquals(TaskState.SUCCESS, results[0].get().getStatusCode());
    Assert.assertEquals(TaskState.SUCCESS, results[1].get().getStatusCode());
    // now both threads race to run the last 3 tasks. task2 and task3 are being assigned
    waitForBothWorkersToHaveUnackedTasks();
    if (remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[2].getId()) && remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[3].getId())) {
        remoteTaskRunner.shutdown("task4", "test");
        mockWorkerRunningAndCompletionSuccessfulTasks(tasks[3], tasks[2]);
        Assert.assertEquals(TaskState.SUCCESS, results[3].get().getStatusCode());
        Assert.assertEquals(TaskState.SUCCESS, results[2].get().getStatusCode());
    } else if (remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[3].getId()) && remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[4].getId())) {
        remoteTaskRunner.shutdown("task2", "test");
        mockWorkerRunningAndCompletionSuccessfulTasks(tasks[4], tasks[3]);
        Assert.assertEquals(TaskState.SUCCESS, results[4].get().getStatusCode());
        Assert.assertEquals(TaskState.SUCCESS, results[3].get().getStatusCode());
    } else if (remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[4].getId()) && remoteTaskRunner.getWorkersWithUnacknowledgedTask().containsValue(tasks[2].getId())) {
        remoteTaskRunner.shutdown("task3", "test");
        mockWorkerRunningAndCompletionSuccessfulTasks(tasks[4], tasks[2]);
        Assert.assertEquals(TaskState.SUCCESS, results[4].get().getStatusCode());
        Assert.assertEquals(TaskState.SUCCESS, results[2].get().getStatusCode());
    } else {
        throw new ISE("two out of three tasks 2,3 and 4 must be waiting for ack.");
    }
    // ensure that RTR is doing OK and still making progress
    tasks[5] = TestTasks.unending("task5");
    results[5] = remoteTaskRunner.run(tasks[5]);
    waitForOneWorkerToHaveUnackedTasks();
    if (rtrTestUtils.taskAnnounced("worker0", tasks[5].getId())) {
        rtrTestUtils.mockWorkerRunningTask("worker0", tasks[5]);
        rtrTestUtils.mockWorkerCompleteSuccessfulTask("worker0", tasks[5]);
    } else {
        rtrTestUtils.mockWorkerRunningTask("worker1", tasks[5]);
        rtrTestUtils.mockWorkerCompleteSuccessfulTask("worker1", tasks[5]);
    }
    Assert.assertEquals(TaskState.SUCCESS, results[5].get().getStatusCode());
}
Also used : Task(org.apache.druid.indexing.common.task.Task) Period(org.joda.time.Period) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ISE(org.apache.druid.java.util.common.ISE) Test(org.junit.Test)

Example 58 with Task

use of org.apache.druid.indexing.common.task.Task in project druid by druid-io.

the class TaskLockBoxConcurrencyTest method testDoInCriticalSectionWithDifferentTasks.

@Test(timeout = 60_000L)
public void testDoInCriticalSectionWithDifferentTasks() throws ExecutionException, InterruptedException, EntryExistsException {
    final Interval interval = Intervals.of("2017-01-01/2017-01-02");
    final Task lowPriorityTask = NoopTask.create(10);
    final Task highPriorityTask = NoopTask.create(100);
    lockbox.add(lowPriorityTask);
    lockbox.add(highPriorityTask);
    taskStorage.insert(lowPriorityTask, TaskStatus.running(lowPriorityTask.getId()));
    taskStorage.insert(highPriorityTask, TaskStatus.running(highPriorityTask.getId()));
    final SettableSupplier<Integer> intSupplier = new SettableSupplier<>(0);
    final CountDownLatch latch = new CountDownLatch(1);
    // lowPriorityTask acquires a lock first and increases the int of intSupplier in the critical section
    final Future<Integer> lowPriorityFuture = service.submit(() -> {
        final LockResult result = tryTimeChunkLock(TaskLockType.EXCLUSIVE, lowPriorityTask, interval);
        Assert.assertTrue(result.isOk());
        Assert.assertFalse(result.isRevoked());
        return lockbox.doInCriticalSection(lowPriorityTask, Collections.singletonList(interval), CriticalAction.<Integer>builder().onValidLocks(() -> {
            latch.countDown();
            Thread.sleep(100);
            intSupplier.set(intSupplier.get() + 1);
            return intSupplier.get();
        }).onInvalidLocks(() -> {
            Assert.fail();
            return null;
        }).build());
    });
    // highPriorityTask awaits for the latch, acquires a lock, and increases the int of intSupplier in the critical
    // section
    final Future<Integer> highPriorityFuture = service.submit(() -> {
        latch.await();
        final LockResult result = acquireTimeChunkLock(TaskLockType.EXCLUSIVE, highPriorityTask, interval);
        Assert.assertTrue(result.isOk());
        Assert.assertFalse(result.isRevoked());
        return lockbox.doInCriticalSection(highPriorityTask, Collections.singletonList(interval), CriticalAction.<Integer>builder().onValidLocks(() -> {
            Thread.sleep(100);
            intSupplier.set(intSupplier.get() + 1);
            return intSupplier.get();
        }).onInvalidLocks(() -> {
            Assert.fail();
            return null;
        }).build());
    });
    Assert.assertEquals(1, lowPriorityFuture.get().intValue());
    Assert.assertEquals(2, highPriorityFuture.get().intValue());
    // the lock for lowPriorityTask must be revoked by the highPriorityTask after its work is done in critical section
    final LockResult result = tryTimeChunkLock(TaskLockType.EXCLUSIVE, lowPriorityTask, interval);
    Assert.assertFalse(result.isOk());
    Assert.assertTrue(result.isRevoked());
}
Also used : SettableSupplier(org.apache.druid.common.guava.SettableSupplier) Task(org.apache.druid.indexing.common.task.Task) NoopTask(org.apache.druid.indexing.common.task.NoopTask) CountDownLatch(java.util.concurrent.CountDownLatch) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 59 with Task

use of org.apache.druid.indexing.common.task.Task in project druid by druid-io.

the class TaskLockboxTest method testTimeoutForLock.

@Test
public void testTimeoutForLock() throws InterruptedException {
    Task task1 = NoopTask.create();
    Task task2 = NoopTask.create();
    lockbox.add(task1);
    lockbox.add(task2);
    Assert.assertTrue(acquireTimeChunkLock(TaskLockType.EXCLUSIVE, task1, Intervals.of("2015-01-01/2015-01-02"), 5000).isOk());
    Assert.assertFalse(acquireTimeChunkLock(TaskLockType.EXCLUSIVE, task2, Intervals.of("2015-01-01/2015-01-15"), 1000).isOk());
}
Also used : AbstractTask(org.apache.druid.indexing.common.task.AbstractTask) Task(org.apache.druid.indexing.common.task.Task) NoopTask(org.apache.druid.indexing.common.task.NoopTask) Test(org.junit.Test)

Example 60 with Task

use of org.apache.druid.indexing.common.task.Task in project druid by druid-io.

the class TaskLockboxTest method testDoInCriticalSectionWithExclusiveLock.

@Test
public void testDoInCriticalSectionWithExclusiveLock() throws Exception {
    final Interval interval = Intervals.of("2017-01-01/2017-01-02");
    final Task task = NoopTask.create();
    lockbox.add(task);
    final TaskLock lock = tryTimeChunkLock(TaskLockType.EXCLUSIVE, task, interval).getTaskLock();
    Assert.assertNotNull(lock);
    Assert.assertTrue(lockbox.doInCriticalSection(task, Collections.singletonList(interval), CriticalAction.<Boolean>builder().onValidLocks(() -> true).onInvalidLocks(() -> false).build()));
}
Also used : AbstractTask(org.apache.druid.indexing.common.task.AbstractTask) Task(org.apache.druid.indexing.common.task.Task) NoopTask(org.apache.druid.indexing.common.task.NoopTask) TaskLock(org.apache.druid.indexing.common.TaskLock) Interval(org.joda.time.Interval) Test(org.junit.Test)

Aggregations

Task (org.apache.druid.indexing.common.task.Task)383 Test (org.junit.Test)307 NoopTask (org.apache.druid.indexing.common.task.NoopTask)177 HashMap (java.util.HashMap)132 Map (java.util.Map)132 RealtimeIndexTask (org.apache.druid.indexing.common.task.RealtimeIndexTask)120 ArrayList (java.util.ArrayList)114 ImmutableMap (com.google.common.collect.ImmutableMap)104 TreeMap (java.util.TreeMap)100 TaskStatus (org.apache.druid.indexer.TaskStatus)100 TaskRunnerListener (org.apache.druid.indexing.overlord.TaskRunnerListener)98 Executor (java.util.concurrent.Executor)86 List (java.util.List)78 AbstractTask (org.apache.druid.indexing.common.task.AbstractTask)78 Collection (java.util.Collection)70 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)68 TaskLocation (org.apache.druid.indexer.TaskLocation)62 TaskLock (org.apache.druid.indexing.common.TaskLock)60 ImmutableList (com.google.common.collect.ImmutableList)58 ISE (org.apache.druid.java.util.common.ISE)58