Search in sources :

Example 26 with TaskLock

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

the class TaskLockboxTest method testTrySharedLock.

@Test
public void testTrySharedLock() {
    final Interval interval = Intervals.of("2017-01/2017-02");
    final List<Task> tasks = new ArrayList<>();
    final Set<TaskLock> actualLocks = new HashSet<>();
    // test creating new locks
    for (int i = 0; i < 5; i++) {
        // the first two tasks have the same priority
        final Task task = NoopTask.create(Math.min(0, (i - 1) * 10));
        tasks.add(task);
        lockbox.add(task);
        final TaskLock lock = tryTimeChunkLock(TaskLockType.SHARED, task, interval).getTaskLock();
        Assert.assertNotNull(lock);
        actualLocks.add(lock);
    }
    Assert.assertEquals(5, getAllLocks(tasks).size());
    Assert.assertEquals(getAllLocks(tasks), actualLocks);
}
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) ArrayList(java.util.ArrayList) Interval(org.joda.time.Interval) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 27 with TaskLock

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

the class TaskLockboxTest method testRevokedLockSyncFromStorage.

@Test
public void testRevokedLockSyncFromStorage() throws EntryExistsException {
    final TaskLockbox originalBox = new TaskLockbox(taskStorage, metadataStorageCoordinator);
    final Task task1 = NoopTask.create("task1", 10);
    taskStorage.insert(task1, TaskStatus.running(task1.getId()));
    originalBox.add(task1);
    Assert.assertTrue(originalBox.tryLock(task1, new TimeChunkLockRequest(TaskLockType.EXCLUSIVE, task1, Intervals.of("2017/2018"), null)).isOk());
    // task2 revokes task1
    final Task task2 = NoopTask.create("task2", 100);
    taskStorage.insert(task2, TaskStatus.running(task2.getId()));
    originalBox.add(task2);
    Assert.assertTrue(originalBox.tryLock(task2, new TimeChunkLockRequest(TaskLockType.EXCLUSIVE, task2, Intervals.of("2017/2018"), null)).isOk());
    final Map<String, List<TaskLock>> beforeLocksInStorage = taskStorage.getActiveTasks().stream().collect(Collectors.toMap(Task::getId, task -> taskStorage.getLocks(task.getId())));
    final List<TaskLock> task1Locks = beforeLocksInStorage.get("task1");
    Assert.assertEquals(1, task1Locks.size());
    Assert.assertTrue(task1Locks.get(0).isRevoked());
    final List<TaskLock> task2Locks = beforeLocksInStorage.get("task1");
    Assert.assertEquals(1, task2Locks.size());
    Assert.assertTrue(task2Locks.get(0).isRevoked());
    final TaskLockbox newBox = new TaskLockbox(taskStorage, metadataStorageCoordinator);
    newBox.syncFromStorage();
    final Set<TaskLock> afterLocksInStorage = taskStorage.getActiveTasks().stream().flatMap(task -> taskStorage.getLocks(task.getId()).stream()).collect(Collectors.toSet());
    Assert.assertEquals(beforeLocksInStorage.values().stream().flatMap(Collection::stream).collect(Collectors.toSet()), afterLocksInStorage);
}
Also used : TaskToolbox(org.apache.druid.indexing.common.TaskToolbox) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) TaskConfig(org.apache.druid.indexing.common.config.TaskConfig) AbstractTask(org.apache.druid.indexing.common.task.AbstractTask) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) TaskActionClient(org.apache.druid.indexing.common.actions.TaskActionClient) Task(org.apache.druid.indexing.common.task.Task) Map(java.util.Map) TaskLock(org.apache.druid.indexing.common.TaskLock) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) DerbyMetadataStorageActionHandlerFactory(org.apache.druid.metadata.DerbyMetadataStorageActionHandlerFactory) HashBasedNumberedPartialShardSpec(org.apache.druid.timeline.partition.HashBasedNumberedPartialShardSpec) DateTimes(org.apache.druid.java.util.common.DateTimes) MetadataStorageTablesConfig(org.apache.druid.metadata.MetadataStorageTablesConfig) TaskLockPosse(org.apache.druid.indexing.overlord.TaskLockbox.TaskLockPosse) NumberedShardSpec(org.apache.druid.timeline.partition.NumberedShardSpec) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Collectors(java.util.stream.Collectors) LockGranularity(org.apache.druid.indexing.common.LockGranularity) IndexerSQLMetadataStorageCoordinator(org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator) NoopTask(org.apache.druid.indexing.common.task.NoopTask) List(java.util.List) TaskLockType(org.apache.druid.indexing.common.TaskLockType) PartitionIds(org.apache.druid.timeline.partition.PartitionIds) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) TestDerbyConnector(org.apache.druid.metadata.TestDerbyConnector) PartialShardSpec(org.apache.druid.timeline.partition.PartialShardSpec) NumberedPartialShardSpec(org.apache.druid.timeline.partition.NumberedPartialShardSpec) Iterables(com.google.common.collect.Iterables) Intervals(org.apache.druid.java.util.common.Intervals) TaskStorageConfig(org.apache.druid.indexing.common.config.TaskStorageConfig) HashBasedNumberedShardSpec(org.apache.druid.timeline.partition.HashBasedNumberedShardSpec) HashMap(java.util.HashMap) TaskStatus(org.apache.druid.indexer.TaskStatus) ArrayList(java.util.ArrayList) EntryExistsException(org.apache.druid.metadata.EntryExistsException) HashSet(java.util.HashSet) Interval(org.joda.time.Interval) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) NumberedOverwritePartialShardSpec(org.apache.druid.timeline.partition.NumberedOverwritePartialShardSpec) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SegmentIdWithShardSpec(org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) TestHelper(org.apache.druid.segment.TestHelper) Rule(org.junit.Rule) SegmentLock(org.apache.druid.indexing.common.SegmentLock) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Assert(org.junit.Assert) Collections(java.util.Collections) 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) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 28 with TaskLock

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

the class TaskLockboxTest method testSyncFromStorageWithMissingTaskPriority.

@Test
public void testSyncFromStorageWithMissingTaskPriority() throws EntryExistsException {
    final Task task = NoopTask.create();
    taskStorage.insert(task, TaskStatus.running(task.getId()));
    taskStorage.addLock(task.getId(), new TimeChunkLock(TaskLockType.EXCLUSIVE, task.getGroupId(), task.getDataSource(), Intervals.of("2017/2018"), "v1", task.getPriority()));
    final List<TaskLock> beforeLocksInStorage = taskStorage.getActiveTasks().stream().flatMap(t -> taskStorage.getLocks(t.getId()).stream()).collect(Collectors.toList());
    final TaskLockbox lockbox = new TaskLockbox(taskStorage, metadataStorageCoordinator);
    lockbox.syncFromStorage();
    final List<TaskLock> afterLocksInStorage = taskStorage.getActiveTasks().stream().flatMap(t -> taskStorage.getLocks(t.getId()).stream()).collect(Collectors.toList());
    Assert.assertEquals(beforeLocksInStorage, afterLocksInStorage);
}
Also used : TaskToolbox(org.apache.druid.indexing.common.TaskToolbox) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Arrays(java.util.Arrays) TaskConfig(org.apache.druid.indexing.common.config.TaskConfig) AbstractTask(org.apache.druid.indexing.common.task.AbstractTask) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) TaskActionClient(org.apache.druid.indexing.common.actions.TaskActionClient) Task(org.apache.druid.indexing.common.task.Task) Map(java.util.Map) TaskLock(org.apache.druid.indexing.common.TaskLock) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) DerbyMetadataStorageActionHandlerFactory(org.apache.druid.metadata.DerbyMetadataStorageActionHandlerFactory) HashBasedNumberedPartialShardSpec(org.apache.druid.timeline.partition.HashBasedNumberedPartialShardSpec) DateTimes(org.apache.druid.java.util.common.DateTimes) MetadataStorageTablesConfig(org.apache.druid.metadata.MetadataStorageTablesConfig) TaskLockPosse(org.apache.druid.indexing.overlord.TaskLockbox.TaskLockPosse) NumberedShardSpec(org.apache.druid.timeline.partition.NumberedShardSpec) Collection(java.util.Collection) StringUtils(org.apache.druid.java.util.common.StringUtils) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) Collectors(java.util.stream.Collectors) LockGranularity(org.apache.druid.indexing.common.LockGranularity) IndexerSQLMetadataStorageCoordinator(org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator) NoopTask(org.apache.druid.indexing.common.task.NoopTask) List(java.util.List) TaskLockType(org.apache.druid.indexing.common.TaskLockType) PartitionIds(org.apache.druid.timeline.partition.PartitionIds) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) TestDerbyConnector(org.apache.druid.metadata.TestDerbyConnector) PartialShardSpec(org.apache.druid.timeline.partition.PartialShardSpec) NumberedPartialShardSpec(org.apache.druid.timeline.partition.NumberedPartialShardSpec) Iterables(com.google.common.collect.Iterables) Intervals(org.apache.druid.java.util.common.Intervals) TaskStorageConfig(org.apache.druid.indexing.common.config.TaskStorageConfig) HashBasedNumberedShardSpec(org.apache.druid.timeline.partition.HashBasedNumberedShardSpec) HashMap(java.util.HashMap) TaskStatus(org.apache.druid.indexer.TaskStatus) ArrayList(java.util.ArrayList) EntryExistsException(org.apache.druid.metadata.EntryExistsException) HashSet(java.util.HashSet) Interval(org.joda.time.Interval) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) ExpectedException(org.junit.rules.ExpectedException) Before(org.junit.Before) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) NumberedOverwritePartialShardSpec(org.apache.druid.timeline.partition.NumberedOverwritePartialShardSpec) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SegmentIdWithShardSpec(org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec) Test(org.junit.Test) EasyMock(org.easymock.EasyMock) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) TestHelper(org.apache.druid.segment.TestHelper) Rule(org.junit.Rule) SegmentLock(org.apache.druid.indexing.common.SegmentLock) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) Assert(org.junit.Assert) Collections(java.util.Collections) 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) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) Test(org.junit.Test)

Example 29 with TaskLock

use of org.apache.druid.indexing.common.TaskLock 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)

Example 30 with TaskLock

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

the class TaskLockboxTest method testRequestForNewSegmentWithSegmentLock.

@Test
public void testRequestForNewSegmentWithSegmentLock() {
    final Task task = NoopTask.create();
    lockbox.add(task);
    allocateSegmentsAndAssert(task, "seq", 3, NumberedPartialShardSpec.instance());
    allocateSegmentsAndAssert(task, "seq2", 2, new NumberedOverwritePartialShardSpec(0, 3, (short) 1));
    final List<TaskLock> locks = lockbox.findLocksForTask(task);
    Assert.assertEquals(5, locks.size());
    int expectedPartitionId = 0;
    for (TaskLock lock : locks) {
        Assert.assertTrue(lock instanceof SegmentLock);
        final SegmentLock segmentLock = (SegmentLock) lock;
        Assert.assertEquals(expectedPartitionId++, segmentLock.getPartitionId());
        if (expectedPartitionId == 3) {
            expectedPartitionId = PartitionIds.NON_ROOT_GEN_START_PARTITION_ID;
        }
    }
}
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) NumberedOverwritePartialShardSpec(org.apache.druid.timeline.partition.NumberedOverwritePartialShardSpec) TaskLock(org.apache.druid.indexing.common.TaskLock) SegmentLock(org.apache.druid.indexing.common.SegmentLock) Test(org.junit.Test)

Aggregations

TaskLock (org.apache.druid.indexing.common.TaskLock)49 Task (org.apache.druid.indexing.common.task.Task)29 Test (org.junit.Test)28 NoopTask (org.apache.druid.indexing.common.task.NoopTask)22 Interval (org.joda.time.Interval)22 ISE (org.apache.druid.java.util.common.ISE)21 DataSegment (org.apache.druid.timeline.DataSegment)16 List (java.util.List)15 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 SegmentLock (org.apache.druid.indexing.common.SegmentLock)13 HashMap (java.util.HashMap)12 Collectors (java.util.stream.Collectors)12 TaskStatus (org.apache.druid.indexer.TaskStatus)12 AbstractTask (org.apache.druid.indexing.common.task.AbstractTask)12 LockGranularity (org.apache.druid.indexing.common.LockGranularity)11 TaskToolbox (org.apache.druid.indexing.common.TaskToolbox)11 TimeChunkLock (org.apache.druid.indexing.common.TimeChunkLock)11 DateTimes (org.apache.druid.java.util.common.DateTimes)11 HashSet (java.util.HashSet)10