Search in sources :

Example 6 with SegmentLock

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

Example 7 with SegmentLock

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

the class TaskLockbox method unlock.

/**
 * Release lock held for a task on a particular interval. Does nothing if the task does not currently
 * hold the mentioned lock.
 *
 * @param task task to unlock
 * @param interval interval to unlock
 */
public void unlock(final Task task, final Interval interval, @Nullable Integer partitionId) {
    giant.lock();
    try {
        final String dataSource = task.getDataSource();
        final NavigableMap<DateTime, SortedMap<Interval, List<TaskLockPosse>>> dsRunning = running.get(task.getDataSource());
        if (dsRunning == null || dsRunning.isEmpty()) {
            return;
        }
        final SortedMap<Interval, List<TaskLockPosse>> intervalToPosses = dsRunning.get(interval.getStart());
        if (intervalToPosses == null || intervalToPosses.isEmpty()) {
            return;
        }
        final List<TaskLockPosse> possesHolder = intervalToPosses.get(interval);
        if (possesHolder == null || possesHolder.isEmpty()) {
            return;
        }
        final List<TaskLockPosse> posses = possesHolder.stream().filter(posse -> posse.containsTask(task)).collect(Collectors.toList());
        for (TaskLockPosse taskLockPosse : posses) {
            final TaskLock taskLock = taskLockPosse.getTaskLock();
            final boolean match = (partitionId == null && taskLock.getGranularity() == LockGranularity.TIME_CHUNK) || (partitionId != null && taskLock.getGranularity() == LockGranularity.SEGMENT && ((SegmentLock) taskLock).getPartitionId() == partitionId);
            if (match) {
                // Remove task from live list
                log.info("Removing task[%s] from TaskLock[%s]", task.getId(), taskLock);
                final boolean removed = taskLockPosse.removeTask(task);
                if (taskLockPosse.isTasksEmpty()) {
                    log.info("TaskLock is now empty: %s", taskLock);
                    possesHolder.remove(taskLockPosse);
                }
                if (possesHolder.isEmpty()) {
                    intervalToPosses.remove(interval);
                }
                if (intervalToPosses.isEmpty()) {
                    dsRunning.remove(interval.getStart());
                }
                if (running.get(dataSource).size() == 0) {
                    running.remove(dataSource);
                }
                // Wake up blocking-lock waiters
                lockReleaseCondition.signalAll();
                // Remove lock from storage. If it cannot be removed, just ignore the failure.
                try {
                    taskStorage.removeLock(task.getId(), taskLock);
                } catch (Exception e) {
                    log.makeAlert(e, "Failed to clean up lock from storage").addData("task", task.getId()).addData("dataSource", taskLock.getDataSource()).addData("interval", taskLock.getInterval()).addData("version", taskLock.getVersion()).emit();
                }
                if (!removed) {
                    log.makeAlert("Lock release without acquire").addData("task", task.getId()).addData("interval", interval).emit();
                }
            }
        }
    } finally {
        giant.unlock();
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Comparators(org.apache.druid.java.util.common.guava.Comparators) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) Pair(org.apache.druid.java.util.common.Pair) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Interval(org.joda.time.Interval) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) TimeChunkLock(org.apache.druid.indexing.common.TimeChunkLock) Task(org.apache.druid.indexing.common.task.Task) Map(java.util.Map) TaskLock(org.apache.druid.indexing.common.TaskLock) StreamSupport(java.util.stream.StreamSupport) Objects(com.google.common.base.Objects) Nullable(javax.annotation.Nullable) Function(com.google.common.base.Function) EmittingLogger(org.apache.druid.java.util.emitter.EmittingLogger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Collection(java.util.Collection) DateTime(org.joda.time.DateTime) SegmentIdWithShardSpec(org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec) Set(java.util.Set) ISE(org.apache.druid.java.util.common.ISE) NavigableSet(java.util.NavigableSet) NavigableMap(java.util.NavigableMap) ComparisonChain(com.google.common.collect.ComparisonChain) Collectors(java.util.stream.Collectors) LockGranularity(org.apache.druid.indexing.common.LockGranularity) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) Condition(java.util.concurrent.locks.Condition) Ordering(com.google.common.collect.Ordering) TreeMap(java.util.TreeMap) SegmentLock(org.apache.druid.indexing.common.SegmentLock) TaskLockType(org.apache.druid.indexing.common.TaskLockType) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) SortedMap(java.util.SortedMap) SegmentLock(org.apache.druid.indexing.common.SegmentLock) DateTime(org.joda.time.DateTime) TaskLock(org.apache.druid.indexing.common.TaskLock) SortedMap(java.util.SortedMap) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Interval(org.joda.time.Interval)

Example 8 with SegmentLock

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

the class TaskLocks method isLockCoversSegments.

public static boolean isLockCoversSegments(NavigableMap<DateTime, List<TaskLock>> taskLockMap, Collection<DataSegment> segments) {
    return segments.stream().allMatch(segment -> {
        final Entry<DateTime, List<TaskLock>> entry = taskLockMap.floorEntry(segment.getInterval().getStart());
        if (entry == null) {
            return false;
        }
        final List<TaskLock> locks = entry.getValue();
        return locks.stream().anyMatch(lock -> {
            if (lock.getGranularity() == LockGranularity.TIME_CHUNK) {
                final TimeChunkLock timeChunkLock = (TimeChunkLock) lock;
                return timeChunkLock.getInterval().contains(segment.getInterval()) && timeChunkLock.getDataSource().equals(segment.getDataSource()) && timeChunkLock.getVersion().compareTo(segment.getVersion()) >= 0;
            } else {
                final SegmentLock segmentLock = (SegmentLock) lock;
                return segmentLock.getInterval().contains(segment.getInterval()) && segmentLock.getDataSource().equals(segment.getDataSource()) && segmentLock.getVersion().compareTo(segment.getVersion()) >= 0 && segmentLock.getPartitionId() == segment.getShardSpec().getPartitionNum();
            }
        });
    });
}
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) List(java.util.List) DateTime(org.joda.time.DateTime)

Example 9 with SegmentLock

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

the class TaskLockboxTest method assertAllocatedSegments.

private void assertAllocatedSegments(LockRequestForNewSegment lockRequest, LockResult result) {
    Assert.assertTrue(result.isOk());
    Assert.assertNotNull(result.getTaskLock());
    Assert.assertTrue(result.getTaskLock() instanceof SegmentLock);
    Assert.assertNotNull(result.getNewSegmentId());
    final SegmentLock segmentLock = (SegmentLock) result.getTaskLock();
    final SegmentIdWithShardSpec segmentId = result.getNewSegmentId();
    Assert.assertEquals(lockRequest.getType(), segmentLock.getType());
    Assert.assertEquals(lockRequest.getGroupId(), segmentLock.getGroupId());
    Assert.assertEquals(lockRequest.getDataSource(), segmentLock.getDataSource());
    Assert.assertEquals(lockRequest.getInterval(), segmentLock.getInterval());
    Assert.assertEquals(lockRequest.getPartialShardSpec().getShardSpecClass(), segmentId.getShardSpec().getClass());
    Assert.assertEquals(lockRequest.getPriority(), lockRequest.getPriority());
}
Also used : SegmentLock(org.apache.druid.indexing.common.SegmentLock) SegmentIdWithShardSpec(org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec)

Aggregations

SegmentLock (org.apache.druid.indexing.common.SegmentLock)9 TaskLock (org.apache.druid.indexing.common.TaskLock)5 TimeChunkLock (org.apache.druid.indexing.common.TimeChunkLock)5 Task (org.apache.druid.indexing.common.task.Task)5 List (java.util.List)4 NoopTask (org.apache.druid.indexing.common.task.NoopTask)4 DateTime (org.joda.time.DateTime)4 ArrayList (java.util.ArrayList)3 AbstractTask (org.apache.druid.indexing.common.task.AbstractTask)3 ISE (org.apache.druid.java.util.common.ISE)3 SegmentIdWithShardSpec (org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec)3 Test (org.junit.Test)3 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables (com.google.common.collect.Iterables)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 LockGranularity (org.apache.druid.indexing.common.LockGranularity)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1