Search in sources :

Example 1 with TimeChunkLock

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

the class TaskLocks method findLocksForSegments.

public static List<TaskLock> findLocksForSegments(final Task task, final TaskLockbox taskLockbox, final Collection<DataSegment> segments) {
    final NavigableMap<DateTime, List<TaskLock>> taskLockMap = getTaskLockMap(taskLockbox, task);
    if (taskLockMap.isEmpty()) {
        return Collections.emptyList();
    }
    final List<TaskLock> found = new ArrayList<>();
    segments.forEach(segment -> {
        final Entry<DateTime, List<TaskLock>> entry = taskLockMap.floorEntry(segment.getInterval().getStart());
        if (entry == null) {
            throw new ISE("Can't find lock for the interval of segment[%s]", segment.getId());
        }
        final List<TaskLock> locks = entry.getValue();
        locks.forEach(lock -> {
            if (lock.getGranularity() == LockGranularity.TIME_CHUNK) {
                final TimeChunkLock timeChunkLock = (TimeChunkLock) lock;
                if (timeChunkLock.getInterval().contains(segment.getInterval()) && timeChunkLock.getDataSource().equals(segment.getDataSource()) && timeChunkLock.getVersion().compareTo(segment.getVersion()) >= 0) {
                    found.add(lock);
                }
            } else {
                final SegmentLock segmentLock = (SegmentLock) lock;
                if (segmentLock.getInterval().contains(segment.getInterval()) && segmentLock.getDataSource().equals(segment.getDataSource()) && segmentLock.getVersion().compareTo(segment.getVersion()) >= 0 && segmentLock.getPartitionId() == segment.getShardSpec().getPartitionNum()) {
                    found.add(lock);
                }
            }
        });
    });
    return found;
}
Also used : TaskLock(org.apache.druid.indexing.common.TaskLock) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) SegmentLock(org.apache.druid.indexing.common.SegmentLock) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ISE(org.apache.druid.java.util.common.ISE) DateTime(org.joda.time.DateTime)

Example 2 with TimeChunkLock

use of org.apache.druid.indexing.common.TimeChunkLock 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 3 with TimeChunkLock

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

the class TaskLockboxTest method testSyncFromStorageWithInvalidPriority.

@Test
public void testSyncFromStorageWithInvalidPriority() 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", 10));
    final TaskLockbox lockbox = new TaskLockbox(taskStorage, metadataStorageCoordinator);
    expectedException.expect(IllegalArgumentException.class);
    expectedException.expectMessage("lock priority[10] is different from task priority[50]");
    lockbox.syncFromStorage();
}
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) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) Test(org.junit.Test)

Example 4 with TimeChunkLock

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

the class TaskLockboxTest method testGetTimeChunkAndSegmentLockForSameGroup.

@Test
public void testGetTimeChunkAndSegmentLockForSameGroup() {
    final Task task1 = NoopTask.withGroupId("groupId");
    final Task task2 = NoopTask.withGroupId("groupId");
    lockbox.add(task1);
    lockbox.add(task2);
    Assert.assertTrue(lockbox.tryLock(task1, new TimeChunkLockRequest(TaskLockType.EXCLUSIVE, task1, Intervals.of("2017/2018"), null)).isOk());
    Assert.assertTrue(lockbox.tryLock(task2, new SpecificSegmentLockRequest(TaskLockType.EXCLUSIVE, task2, Intervals.of("2017/2018"), "version", 0)).isOk());
    final List<TaskLockPosse> posses = lockbox.getAllLocks().get(task1.getDataSource()).get(DateTimes.of("2017")).get(Intervals.of("2017/2018"));
    Assert.assertEquals(2, posses.size());
    Assert.assertEquals(LockGranularity.TIME_CHUNK, posses.get(0).getTaskLock().getGranularity());
    final TimeChunkLock timeChunkLock = (TimeChunkLock) posses.get(0).getTaskLock();
    Assert.assertEquals("none", timeChunkLock.getDataSource());
    Assert.assertEquals("groupId", timeChunkLock.getGroupId());
    Assert.assertEquals(Intervals.of("2017/2018"), timeChunkLock.getInterval());
    Assert.assertEquals(LockGranularity.SEGMENT, posses.get(1).getTaskLock().getGranularity());
    final SegmentLock segmentLock = (SegmentLock) posses.get(1).getTaskLock();
    Assert.assertEquals("none", segmentLock.getDataSource());
    Assert.assertEquals("groupId", segmentLock.getGroupId());
    Assert.assertEquals(Intervals.of("2017/2018"), segmentLock.getInterval());
    Assert.assertEquals(0, segmentLock.getPartitionId());
}
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) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) SegmentLock(org.apache.druid.indexing.common.SegmentLock) TaskLockPosse(org.apache.druid.indexing.overlord.TaskLockbox.TaskLockPosse) Test(org.junit.Test)

Example 5 with TimeChunkLock

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

the class TaskLockbox method verifyAndCreateOrFindLockPosse.

/**
 * This method is called only in {@link #syncFromStorage()} and verifies the given task and the taskLock have the same
 * groupId, dataSource, and priority.
 */
private TaskLockPosse verifyAndCreateOrFindLockPosse(Task task, TaskLock taskLock) {
    giant.lock();
    try {
        Preconditions.checkArgument(task.getGroupId().equals(taskLock.getGroupId()), "lock groupId[%s] is different from task groupId[%s]", taskLock.getGroupId(), task.getGroupId());
        Preconditions.checkArgument(task.getDataSource().equals(taskLock.getDataSource()), "lock dataSource[%s] is different from task dataSource[%s]", taskLock.getDataSource(), task.getDataSource());
        final int taskPriority = task.getPriority();
        final int lockPriority = taskLock.getNonNullPriority();
        Preconditions.checkArgument(lockPriority == taskPriority, "lock priority[%s] is different from task priority[%s]", lockPriority, taskPriority);
        final LockRequest request;
        switch(taskLock.getGranularity()) {
            case SEGMENT:
                final SegmentLock segmentLock = (SegmentLock) taskLock;
                request = new SpecificSegmentLockRequest(segmentLock.getType(), segmentLock.getGroupId(), segmentLock.getDataSource(), segmentLock.getInterval(), segmentLock.getVersion(), segmentLock.getPartitionId(), taskPriority, segmentLock.isRevoked());
                break;
            case TIME_CHUNK:
                final TimeChunkLock timeChunkLock = (TimeChunkLock) taskLock;
                request = new TimeChunkLockRequest(timeChunkLock.getType(), timeChunkLock.getGroupId(), timeChunkLock.getDataSource(), timeChunkLock.getInterval(), timeChunkLock.getVersion(), taskPriority, timeChunkLock.isRevoked());
                break;
            default:
                throw new ISE("Unknown lockGranularity[%s]", taskLock.getGranularity());
        }
        return createOrFindLockPosse(request);
    } finally {
        giant.unlock();
    }
}
Also used : TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) SegmentLock(org.apache.druid.indexing.common.SegmentLock) ISE(org.apache.druid.java.util.common.ISE)

Aggregations

TimeChunkLock (org.apache.druid.indexing.common.TimeChunkLock)8 SegmentLock (org.apache.druid.indexing.common.SegmentLock)5 TaskLock (org.apache.druid.indexing.common.TaskLock)5 NoopTask (org.apache.druid.indexing.common.task.NoopTask)5 Task (org.apache.druid.indexing.common.task.Task)5 Test (org.junit.Test)5 AbstractTask (org.apache.druid.indexing.common.task.AbstractTask)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 TaskLockPosse (org.apache.druid.indexing.overlord.TaskLockbox.TaskLockPosse)3 ISE (org.apache.druid.java.util.common.ISE)3 HashMap (java.util.HashMap)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)1 Iterables (com.google.common.collect.Iterables)1 URL (java.net.URL)1