Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API to fetch conflicting task locks #16799

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
kfaraz marked this conversation as resolved.
Show resolved Hide resolved
@Path("/activeLocks")
@Produces(MediaType.APPLICATION_JSON)
@ResourceFilters(StateResourceFilter.class)
public Response getDatasourceLockedIntervalsV2(List<LockFilterPolicy> lockFilterPolicies)
public Response getActiveLocks(List<LockFilterPolicy> lockFilterPolicies)
Copy link
Contributor

@kfaraz kfaraz Jul 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super Nit: If you reverse the order of these two methods in this class file, the diff might become smaller/simpler. 🙂
(consider doing this only if you are also fixing some other build issue in this PR)

{
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
Loading