Skip to content
Open
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 @@ -564,9 +564,16 @@ private void ackTask(OptimizingTaskId taskId, OptimizerThread thread) {

private void completeTask(OptimizerThread thread, OptimizingTaskResult result) {
TaskRuntime<?> taskRuntime = getTaskRuntime(result.getTaskId());
// Complete taskRuntime first (this acquires the database lock, but not under
// TableOptimizingProcess.this.lock protection)
// This avoids deadlock by ensuring consistent lock acquisition order (acquire
// TableOptimizingProcess.this.lock first, then database lock)
taskRuntime.complete(thread, result);
// After taskRuntime.complete() returns (database lock is released), acquire
// TableOptimizingProcess.this.lock and call acceptResult
lock.lock();
try {
taskRuntime.complete(thread, result);
acceptResult(taskRuntime);
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -891,7 +898,14 @@ private void persistAndSetCompleted(boolean success) {
}

private void cancelTasks() {
// Cancel all tasks first (this acquires the database lock, but not under
// TableOptimizingProcess.this.lock protection)
taskMap.values().forEach(TaskRuntime::tryCanceling);
// Then call acceptResult for each canceled task (while holding
// TableOptimizingProcess.this.lock)
// This avoids deadlock by ensuring consistent lock acquisition order (acquire
// TableOptimizingProcess.this.lock first, then database lock)
taskMap.values().forEach(this::acceptResult);
}

private void loadTaskRuntimes(OptimizingProcess optimizingProcess) {
Expand All @@ -905,7 +919,9 @@ private void loadTaskRuntimes(OptimizingProcess optimizingProcess) {
Map<Integer, RewriteFilesInput> inputs = TaskFilesPersistence.loadTaskInputs(processId);
taskRuntimes.forEach(
taskRuntime -> {
taskRuntime.getCompletedFuture().whenCompleted(() -> acceptResult(taskRuntime));
// Remove whenCompleted callback registration to avoid deadlock caused by
// synchronously calling acceptResult while holding database lock
// acceptResult will be explicitly called in completeTask
taskRuntime
.getTaskDescriptor()
.setInput(inputs.get(taskRuntime.getTaskId().getTaskId()));
Expand Down Expand Up @@ -935,7 +951,9 @@ private void loadTaskRuntimes(List<RewriteStageTask> taskDescriptors) {
tableRuntime.getTableIdentifier(),
taskRuntime.getTaskId(),
taskRuntime.getSummary());
taskRuntime.getCompletedFuture().whenCompleted(() -> acceptResult(taskRuntime));
// Remove whenCompleted callback registration to avoid deadlock caused by synchronously
// calling acceptResult while holding database lock
// acceptResult will be explicitly called in completeTask
taskMap.put(taskRuntime.getTaskId(), taskRuntime);
taskQueue.offer(taskRuntime);
}
Expand Down