Search in sources :

Example 1 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class BaseRestorableTaskRunner method saveRunningTasks.

/**
 * Save running tasks to a file, so they can potentially be restored on next startup. Suppresses exceptions that
 * occur while saving.
 */
@GuardedBy("tasks")
protected void saveRunningTasks() {
    final File restoreFile = getRestoreFile();
    final List<String> theTasks = new ArrayList<>();
    for (TaskRunnerWorkItem forkingTaskRunnerWorkItem : tasks.values()) {
        theTasks.add(forkingTaskRunnerWorkItem.getTaskId());
    }
    try {
        Files.createParentDirs(restoreFile);
        jsonMapper.writeValue(restoreFile, new TaskRestoreInfo(theTasks));
    } catch (Exception e) {
        LOG.warn(e, "Failed to save tasks to restore file[%s]. Skipping this save.", restoreFile);
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) File(java.io.File) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Example 2 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class VersionedIntervalTimeline method remove.

@GuardedBy("lock")
private void remove(NavigableMap<Interval, TimelineEntry> timeline, Interval interval, TimelineEntry entry, boolean incompleteOk) {
    List<Interval> intervalsToRemove = new ArrayList<>();
    TimelineEntry removed = timeline.get(interval);
    if (removed == null) {
        Iterator<Entry<Interval, TimelineEntry>> iter = timeline.entrySet().iterator();
        while (iter.hasNext()) {
            Entry<Interval, TimelineEntry> timelineEntry = iter.next();
            if (timelineEntry.getValue() == entry) {
                intervalsToRemove.add(timelineEntry.getKey());
            }
        }
    } else {
        intervalsToRemove.add(interval);
    }
    for (Interval i : intervalsToRemove) {
        remove(timeline, i, incompleteOk);
    }
}
Also used : Entry(java.util.Map.Entry) ArrayList(java.util.ArrayList) Interval(org.joda.time.Interval) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Example 3 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class VersionedIntervalTimeline method add.

@GuardedBy("lock")
private void add(NavigableMap<Interval, TimelineEntry> timeline, Interval interval, TimelineEntry entry) {
    TimelineEntry existsInTimeline = timeline.get(interval);
    if (existsInTimeline != null) {
        int compare = versionComparator.compare(entry.getVersion(), existsInTimeline.getVersion());
        if (compare > 0) {
            addIntervalToTimeline(interval, entry, timeline);
        }
        return;
    }
    Interval lowerKey = timeline.lowerKey(interval);
    if (lowerKey != null) {
        if (addAtKey(timeline, lowerKey, entry)) {
            return;
        }
    }
    Interval higherKey = timeline.higherKey(interval);
    if (higherKey != null) {
        if (addAtKey(timeline, higherKey, entry)) {
            return;
        }
    }
    addIntervalToTimeline(interval, entry, timeline);
}
Also used : Interval(org.joda.time.Interval) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Example 4 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class VersionedIntervalTimeline method lookup.

@GuardedBy("lock")
private List<TimelineObjectHolder<VersionType, ObjectType>> lookup(Interval interval, Partitions completeness) {
    List<TimelineObjectHolder<VersionType, ObjectType>> retVal = new ArrayList<>();
    NavigableMap<Interval, TimelineEntry> timeline;
    if (completeness == Partitions.INCOMPLETE_OK) {
        timeline = incompletePartitionsTimeline;
    } else {
        timeline = completePartitionsTimeline;
    }
    for (Entry<Interval, TimelineEntry> entry : timeline.entrySet()) {
        Interval timelineInterval = entry.getKey();
        TimelineEntry val = entry.getValue();
        if (timelineInterval.overlaps(interval)) {
            retVal.add(new TimelineObjectHolder<>(timelineInterval, val.getTrueInterval(), val.getVersion(), PartitionHolder.copyWithOnlyVisibleChunks(val.getPartitionHolder())));
        }
    }
    if (retVal.isEmpty()) {
        return retVal;
    }
    TimelineObjectHolder<VersionType, ObjectType> firstEntry = retVal.get(0);
    if (interval.overlaps(firstEntry.getInterval()) && interval.getStart().isAfter(firstEntry.getInterval().getStart())) {
        retVal.set(0, new TimelineObjectHolder<>(new Interval(interval.getStart(), firstEntry.getInterval().getEnd()), firstEntry.getTrueInterval(), firstEntry.getVersion(), firstEntry.getObject()));
    }
    TimelineObjectHolder<VersionType, ObjectType> lastEntry = retVal.get(retVal.size() - 1);
    if (interval.overlaps(lastEntry.getInterval()) && interval.getEnd().isBefore(lastEntry.getInterval().getEnd())) {
        retVal.set(retVal.size() - 1, new TimelineObjectHolder<>(new Interval(lastEntry.getInterval().getStart(), interval.getEnd()), lastEntry.getTrueInterval(), lastEntry.getVersion(), lastEntry.getObject()));
    }
    return retVal;
}
Also used : ArrayList(java.util.ArrayList) Interval(org.joda.time.Interval) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Example 5 with GuardedBy

use of com.google.errorprone.annotations.concurrent.GuardedBy in project druid by druid-io.

the class VersionedIntervalTimeline method addAtKey.

/**
 * @return boolean flag indicating whether or not we inserted or discarded something
 */
@GuardedBy("lock")
private boolean addAtKey(NavigableMap<Interval, TimelineEntry> timeline, Interval key, TimelineEntry entry) {
    boolean retVal = false;
    Interval currKey = key;
    Interval entryInterval = entry.getTrueInterval();
    if (!currKey.overlaps(entryInterval)) {
        return false;
    }
    while (entryInterval != null && currKey != null && currKey.overlaps(entryInterval)) {
        final Interval nextKey = timeline.higherKey(currKey);
        final int versionCompare = versionComparator.compare(entry.getVersion(), timeline.get(currKey).getVersion());
        if (versionCompare < 0) {
            // if overlapped.
            if (currKey.contains(entryInterval)) {
                // the version of the entry of currKey is larger than that of the given entry. Discard it
                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                // | entry |
                // | cur |
                // =>        |new|
                entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
            } else {
                // | entry |
                // | cur |
                // =>  |new|
                addIntervalToTimeline(new Interval(entryInterval.getStart(), currKey.getStart()), entry, timeline);
                // =>          |new|
                if (entryInterval.getEnd().isAfter(currKey.getEnd())) {
                    entryInterval = new Interval(currKey.getEnd(), entryInterval.getEnd());
                } else {
                    // Discard this entry since there is no portion of the entry interval that goes past the end of the curr
                    // key interval.
                    entryInterval = null;
                }
            }
        } else if (versionCompare > 0) {
            // since the entry version is greater than the existing one, the given entry overwrites the existing one
            // if overlapped.
            final TimelineEntry oldEntry = timeline.remove(currKey);
            if (currKey.contains(entryInterval)) {
                // |      cur      |
                // | entry |
                // =>  |old|  new  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
                addIntervalToTimeline(entryInterval, entry, timeline);
                return true;
            } else if (currKey.getStart().isBefore(entryInterval.getStart())) {
                // |   cur  |
                // |   entry   |
                // =>  |old|
                addIntervalToTimeline(new Interval(currKey.getStart(), entryInterval.getStart()), oldEntry, timeline);
            } else if (entryInterval.getEnd().isBefore(currKey.getEnd())) {
                // |   cur  |
                // |   entry   |
                // =>              |old|
                addIntervalToTimeline(new Interval(entryInterval.getEnd(), currKey.getEnd()), oldEntry, timeline);
            }
        } else {
            if (timeline.get(currKey).equals(entry)) {
                // This occurs when restoring segments
                timeline.remove(currKey);
            } else {
                throw new UOE("Cannot add overlapping segments [%s and %s] with the same version [%s]", currKey, entryInterval, entry.getVersion());
            }
        }
        currKey = nextKey;
        retVal = true;
    }
    addIntervalToTimeline(entryInterval, entry, timeline);
    return retVal;
}
Also used : UOE(org.apache.druid.java.util.common.UOE) Interval(org.joda.time.Interval) GuardedBy(com.google.errorprone.annotations.concurrent.GuardedBy)

Aggregations

GuardedBy (com.google.errorprone.annotations.concurrent.GuardedBy)6 Interval (org.joda.time.Interval)4 ArrayList (java.util.ArrayList)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Entry (java.util.Map.Entry)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 UOE (org.apache.druid.java.util.common.UOE)1