Skip to content

Commit

Permalink
Add API to fetch conflicting task locks (apache#16799)
Browse files Browse the repository at this point in the history
* Add API to fetch conflicting active locks
  • Loading branch information
AmatyaAvadhanula authored and sreemanamala committed Aug 6, 2024
1 parent d4e603d commit ff1e5c7
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@
import org.apache.druid.indexing.common.task.Task;
import org.apache.druid.indexing.common.task.Tasks;
import org.apache.druid.java.util.common.ISE;
import org.apache.druid.java.util.common.Intervals;
import org.apache.druid.java.util.common.Pair;
import org.apache.druid.java.util.common.StringUtils;
import org.apache.druid.java.util.common.UOE;
import org.apache.druid.java.util.common.guava.Comparators;
import org.apache.druid.java.util.emitter.EmittingLogger;
import org.apache.druid.metadata.LockFilterPolicy;
import org.apache.druid.metadata.ReplaceTaskLock;
import org.apache.druid.query.QueryContexts;
import org.apache.druid.segment.realtime.appenderator.SegmentIdWithShardSpec;
import org.joda.time.DateTime;
import org.joda.time.Interval;
Expand Down Expand Up @@ -992,50 +994,76 @@ public Map<String, List<Interval>> getLockedIntervals(List<LockFilterPolicy> loc
}

/**
* Gets a List of Intervals locked by higher priority tasks for each datasource.
* Here, Segment Locks are being treated the same as Time Chunk Locks i.e.
* a Task with a Segment Lock is assumed to lock a whole Interval and not just
* the corresponding Segment.
*
* @param minTaskPriority Minimum task priority for each datasource. Only the
* Intervals that are locked by Tasks with equal or
* higher priority than this are returned. Locked intervals
* for datasources that are not present in this Map are
* not returned.
* @return Map from Datasource to List of Intervals locked by Tasks that have
* priority greater than or equal to the {@code minTaskPriority} for that datasource.
* @param lockFilterPolicies Lock filters for the given datasources
* @return Map from datasource to list of non-revoked locks with at least as much priority and an overlapping interval
*/
public Map<String, List<Interval>> getLockedIntervals(Map<String, Integer> minTaskPriority)
public Map<String, List<TaskLock>> getActiveLocks(List<LockFilterPolicy> lockFilterPolicies)
{
final Map<String, Set<Interval>> datasourceToIntervals = new HashMap<>();
final Map<String, List<TaskLock>> datasourceToLocks = new HashMap<>();

// Take a lock and populate the maps
giant.lock();

try {
running.forEach(
(datasource, datasourceLocks) -> {
// If this datasource is not requested, do not proceed
if (!minTaskPriority.containsKey(datasource)) {
lockFilterPolicies.forEach(
lockFilter -> {
final String datasource = lockFilter.getDatasource();
if (!running.containsKey(datasource)) {
return;
}

datasourceLocks.forEach(
final int priority = lockFilter.getPriority();
final List<Interval> intervals;
if (lockFilter.getIntervals() != null) {
intervals = lockFilter.getIntervals();
} else {
intervals = Collections.singletonList(Intervals.ETERNITY);
}

final Map<String, Object> context = lockFilter.getContext();
final boolean ignoreAppendLocks;
final Boolean useConcurrentLocks = QueryContexts.getAsBoolean(
Tasks.USE_CONCURRENT_LOCKS,
context.get(Tasks.USE_CONCURRENT_LOCKS)
);
if (useConcurrentLocks == null) {
TaskLockType taskLockType = QueryContexts.getAsEnum(
Tasks.TASK_LOCK_TYPE,
context.get(Tasks.TASK_LOCK_TYPE),
TaskLockType.class
);
if (taskLockType == null) {
ignoreAppendLocks = Tasks.DEFAULT_USE_CONCURRENT_LOCKS;
} else {
ignoreAppendLocks = taskLockType == TaskLockType.APPEND;
}
} else {
ignoreAppendLocks = useConcurrentLocks;
}

running.get(datasource).forEach(
(startTime, startTimeLocks) -> startTimeLocks.forEach(
(interval, taskLockPosses) -> taskLockPosses.forEach(
taskLockPosse -> {
if (taskLockPosse.getTaskLock().isRevoked()) {
// Do not proceed if the lock is revoked
return;
// do nothing
} else if (taskLockPosse.getTaskLock().getPriority() == null
|| taskLockPosse.getTaskLock().getPriority() < minTaskPriority.get(datasource)) {
// Do not proceed if the lock has a priority strictly less than the minimum
return;
|| taskLockPosse.getTaskLock().getPriority() < priority) {
// do nothing
} else if (ignoreAppendLocks
&& taskLockPosse.getTaskLock().getType() == TaskLockType.APPEND) {
// do nothing
} else {
for (Interval filterInterval : intervals) {
if (interval.overlaps(filterInterval)) {
datasourceToLocks.computeIfAbsent(datasource, ds -> new ArrayList<>())
.add(taskLockPosse.getTaskLock());
break;
}
}
}

datasourceToIntervals
.computeIfAbsent(datasource, k -> new HashSet<>())
.add(interval);
})
}
)
)
);
}
Expand All @@ -1045,11 +1073,7 @@ public Map<String, List<Interval>> getLockedIntervals(Map<String, Integer> minTa
giant.unlock();
}

return datasourceToIntervals.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new ArrayList<>(entry.getValue())
));
return datasourceToLocks;
}

public void unlock(final Task task, final Interval interval)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.druid.indexer.TaskInfo;
import org.apache.druid.indexer.TaskStatus;
import org.apache.druid.indexer.TaskStatusPlus;
import org.apache.druid.indexing.common.TaskLock;
import org.apache.druid.indexing.common.task.Task;
import org.apache.druid.indexing.overlord.autoscaling.ProvisioningStrategy;
import org.apache.druid.indexing.overlord.http.TaskStateLookup;
Expand Down Expand Up @@ -94,19 +95,12 @@ public Map<String, List<Interval>> getLockedIntervals(List<LockFilterPolicy> loc
}

/**
* Gets a List of Intervals locked by higher priority tasks for each datasource.
*
* @param minTaskPriority Minimum task priority for each datasource. Only the
* Intervals that are locked by Tasks with equal or
* higher priority than this are returned. Locked intervals
* for datasources that are not present in this Map are
* not returned.
* @return Map from Datasource to List of Intervals locked by Tasks that have
* priority greater than or equal to the {@code minTaskPriority} for that datasource.
* @param lockFilterPolicies Requests for active locks for various datasources
* @return Map from datasource to conflicting lock infos
*/
public Map<String, List<Interval>> getLockedIntervals(Map<String, Integer> minTaskPriority)
public Map<String, List<TaskLock>> getActiveLocks(List<LockFilterPolicy> lockFilterPolicies)
{
return taskLockbox.getLockedIntervals(minTaskPriority);
return taskLockbox.getActiveLocks(lockFilterPolicies);
}

public List<TaskInfo<Task, TaskStatus>> getActiveTaskInfo(@Nullable String dataSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,33 +241,32 @@ public Response isLeader()
}
}

@Deprecated
@POST
@Path("/lockedIntervals")
@Path("/lockedIntervals/v2")
@Produces(MediaType.APPLICATION_JSON)
@ResourceFilters(StateResourceFilter.class)
public Response getDatasourceLockedIntervals(Map<String, Integer> minTaskPriority)
public Response getDatasourceLockedIntervals(List<LockFilterPolicy> lockFilterPolicies)
{
if (minTaskPriority == null || minTaskPriority.isEmpty()) {
return Response.status(Status.BAD_REQUEST).entity("No Datasource provided").build();
if (lockFilterPolicies == null || lockFilterPolicies.isEmpty()) {
return Response.status(Status.BAD_REQUEST).entity("No filter provided").build();
}

// Build the response
return Response.ok(taskQueryTool.getLockedIntervals(minTaskPriority)).build();
return Response.ok(taskQueryTool.getLockedIntervals(lockFilterPolicies)).build();
}

@POST
@Path("/lockedIntervals/v2")
@Path("/activeLocks")
@Produces(MediaType.APPLICATION_JSON)
@ResourceFilters(StateResourceFilter.class)
public Response getDatasourceLockedIntervalsV2(List<LockFilterPolicy> lockFilterPolicies)
public Response getActiveLocks(List<LockFilterPolicy> lockFilterPolicies)
{
if (lockFilterPolicies == null || lockFilterPolicies.isEmpty()) {
return Response.status(Status.BAD_REQUEST).entity("No filter provided").build();
}

// Build the response
return Response.ok(taskQueryTool.getLockedIntervals(lockFilterPolicies)).build();
return Response.ok(new TaskLockResponse(taskQueryTool.getActiveLocks(lockFilterPolicies))).build();
}

@GET
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.druid.indexing.overlord.http;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.druid.indexing.common.TaskLock;

import java.util.List;
import java.util.Map;

public class TaskLockResponse
{
private final Map<String, List<TaskLock>> datasourceToLocks;

@JsonCreator
public TaskLockResponse(
@JsonProperty("datasourceToLocks") final Map<String, List<TaskLock>> datasourceToLocks
)
{
this.datasourceToLocks = datasourceToLocks;
}

@JsonProperty
public Map<String, List<TaskLock>> getDatasourceToLocks()
{
return datasourceToLocks;
}

@Override
public String toString()
{
return "TaskLockResponse{" +
"datasourceToLocks='" + datasourceToLocks +
'}';
}
}
Loading

0 comments on commit ff1e5c7

Please sign in to comment.