Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -108,17 +108,19 @@ public TaskLockbox(

/**
* Wipe out our current in-memory state and resync it from our bundled {@link TaskStorage}.
*
* @return SyncResult which needs to be processed by the caller
*/
public void syncFromStorage()
public TaskLockboxSyncResult syncFromStorage()
{
giant.lock();

try {
// Load stuff from taskStorage first. If this fails, we don't want to lose all our locks.
final Set<String> storedActiveTasks = new HashSet<>();
Comment thread
kfaraz marked this conversation as resolved.
final Set<Task> storedActiveTasks = new HashSet<>();
final List<Pair<Task, TaskLock>> storedLocks = new ArrayList<>();
for (final Task task : taskStorage.getActiveTasks()) {
storedActiveTasks.add(task.getId());
storedActiveTasks.add(task);
for (final TaskLock taskLock : taskStorage.getLocks(task.getId())) {
storedLocks.add(Pair.of(task, taskLock));
}
Expand All @@ -138,7 +140,12 @@ public int compare(Pair<Task, TaskLock> left, Pair<Task, TaskLock> right)
};
running.clear();
activeTasks.clear();
activeTasks.addAll(storedActiveTasks);
activeTasks.addAll(storedActiveTasks.stream()
.map(Task::getId)
.collect(Collectors.toSet())
);
// Set of task groups in which at least one task failed to re-acquire a lock
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the comments!

final Set<String> failedToReacquireLockTaskGroups = new HashSet<>();
// Bookkeeping for a log message at the end
int taskLockCount = 0;
for (final Pair<Task, TaskLock> taskAndLock : byVersionOrdering.sortedCopy(storedLocks)) {
Expand Down Expand Up @@ -183,20 +190,39 @@ public int compare(Pair<Task, TaskLock> left, Pair<Task, TaskLock> right)
);
}
} else {
throw new ISE(
"Could not reacquire lock on interval[%s] version[%s] for task: %s",
failedToReacquireLockTaskGroups.add(task.getGroupId());
log.error(
"Could not reacquire lock on interval[%s] version[%s] for task: %s from group %s.",
savedTaskLockWithPriority.getInterval(),
savedTaskLockWithPriority.getVersion(),
task.getId()
task.getId(),
task.getGroupId()
);
continue;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: probably not needed as we are already at the end of the loop.

}
}

Set<Task> tasksToFail = new HashSet<>();
for (Task task : storedActiveTasks) {
if (failedToReacquireLockTaskGroups.contains(task.getGroupId())) {
tasksToFail.add(task);
activeTasks.remove(task.getId());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: Style: You could choose to remove all of them in one go, thus retaining the sense of atomic update to activeTasks.

}
}

log.info(
"Synced %,d locks for %,d activeTasks from storage (%,d locks ignored).",
taskLockCount,
activeTasks.size(),
storedLocks.size() - taskLockCount
);

if (!failedToReacquireLockTaskGroups.isEmpty()) {
log.warn("Marking all tasks from task groups[%s] to be failed "
+ "as they failed to reacquire at least one lock.", failedToReacquireLockTaskGroups);
}

return new TaskLockboxSyncResult(tasksToFail);
}
finally {
giant.unlock();
Expand All @@ -207,7 +233,8 @@ public int compare(Pair<Task, TaskLock> left, Pair<Task, TaskLock> right)
* 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)
@VisibleForTesting
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: is there a way to avoid this and still be able to test it? (without too much hassle)

protected TaskLockPosse verifyAndCreateOrFindLockPosse(Task task, TaskLock taskLock)
{
giant.lock();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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;

import org.apache.druid.indexing.common.task.Task;

import java.util.Set;

/**
* Result of TaskLockbox#syncFromStorage()
* Contains tasks which need to be forcefully failed to let the overlord become the leader
*/
class TaskLockboxSyncResult
{
private final Set<Task> tasksToFail;

TaskLockboxSyncResult(Set<Task> tasksToFail)
{
this.tasksToFail = tasksToFail;
}

/**
* Return set of tasks which need to be forcefully failed due to lock re-acquisition failure
*/
Set<Task> getTasksToFail()
{
return tasksToFail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public void becomeLeader()
log.info("By the power of Grayskull, I have the power!");

try {
taskLockbox.syncFromStorage();
taskRunner = runnerFactory.build();
taskQueue = new TaskQueue(
taskLockConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.druid.indexer.TaskLocation;
import org.apache.druid.indexer.TaskStatus;
import org.apache.druid.indexing.common.Counters;
import org.apache.druid.indexing.common.TaskLock;
import org.apache.druid.indexing.common.actions.TaskActionClientFactory;
import org.apache.druid.indexing.common.task.IndexTaskUtils;
import org.apache.druid.indexing.common.task.Task;
Expand Down Expand Up @@ -173,6 +174,13 @@ public void start()
Preconditions.checkState(!active, "queue must be stopped");
active = true;
syncFromStorage();
// Mark these tasks as failed as they could not reacuire the lock
// Clean up needs to happen after tasks have been synced from storage
Set<Task> tasksToFail = taskLockbox.syncFromStorage().getTasksToFail();
for (Task task : tasksToFail) {
shutdown(task.getId(),
"Shutting down forcefully as task failed to reacquire lock while becoming leader");
}
managerExec.submit(
new Runnable()
{
Expand Down Expand Up @@ -228,6 +236,13 @@ public ScheduledExecutors.Signal call()
}
);
requestManagement();
// Remove any unacquired locks from storage (shutdown only clears entries for which a TaskLockPosse was acquired)
// This is called after requesting management as locks need to be cleared after notifyStatus is processed
for (Task task : tasksToFail) {
for (TaskLock lock : taskStorage.getLocks(task.getId())) {
taskStorage.removeLock(task.getId(), lock);
}
}
}
finally {
giant.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import org.apache.druid.indexer.TaskStatus;
import org.apache.druid.indexing.common.LockGranularity;
Expand Down Expand Up @@ -1258,6 +1259,47 @@ public void testGetLockedIntervalsForRevokedLocks() throws Exception
);
}

@Test
public void testFailedToReacquireTaskLock() throws Exception
{
// Tasks to be failed have a group id with the substring "FailingLockAcquisition"
// Please refer to NullLockPosseTaskLockbox
final Task taskWithFailingLockAcquisition0 = NoopTask.withGroupId("FailingLockAcquisition");
final Task taskWithFailingLockAcquisition1 = NoopTask.withGroupId("FailingLockAcquisition");
final Task taskWithSuccessfulLockAcquisition = NoopTask.create();
taskStorage.insert(taskWithFailingLockAcquisition0, TaskStatus.running(taskWithFailingLockAcquisition0.getId()));
taskStorage.insert(taskWithFailingLockAcquisition1, TaskStatus.running(taskWithFailingLockAcquisition1.getId()));
taskStorage.insert(taskWithSuccessfulLockAcquisition, TaskStatus.running(taskWithSuccessfulLockAcquisition.getId()));

TaskLockbox testLockbox = new NullLockPosseTaskLockbox(taskStorage, metadataStorageCoordinator);
testLockbox.add(taskWithFailingLockAcquisition0);
testLockbox.add(taskWithFailingLockAcquisition1);
testLockbox.add(taskWithSuccessfulLockAcquisition);

testLockbox.tryLock(taskWithFailingLockAcquisition0,
new TimeChunkLockRequest(TaskLockType.EXCLUSIVE,
taskWithFailingLockAcquisition0,
Intervals.of("2017-07-01/2017-08-01"),
null
)
);

testLockbox.tryLock(taskWithSuccessfulLockAcquisition,
new TimeChunkLockRequest(TaskLockType.EXCLUSIVE,
taskWithSuccessfulLockAcquisition,
Intervals.of("2017-07-01/2017-08-01"),
null
)
);

Assert.assertEquals(3, taskStorage.getActiveTasks().size());

// The tasks must be marked for failure
TaskLockboxSyncResult result = testLockbox.syncFromStorage();
Assert.assertEquals(ImmutableSet.of(taskWithFailingLockAcquisition0, taskWithFailingLockAcquisition1),
result.getTasksToFail());
}

private Set<TaskLock> getAllLocks(List<Task> tasks)
{
return tasks.stream()
Expand Down Expand Up @@ -1383,4 +1425,25 @@ public TaskStatus run(TaskToolbox toolbox)
return TaskStatus.failure("how?", "Dummy task status err msg");
}
}

/**
* Extends TaskLockbox to return a null TaskLockPosse when the task's group name contains "FailingLockAcquisition".
*/
private static class NullLockPosseTaskLockbox extends TaskLockbox
{
public NullLockPosseTaskLockbox(
TaskStorage taskStorage,
IndexerMetadataStorageCoordinator metadataStorageCoordinator
)
{
super(taskStorage, metadataStorageCoordinator);
}

@Override
protected TaskLockPosse verifyAndCreateOrFindLockPosse(Task task, TaskLock taskLock)
{
return task.getGroupId()
.contains("FailingLockAcquisition") ? null : super.verifyAndCreateOrFindLockPosse(task, taskLock);
}
}
}
Loading