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

GH-45266: [C++][Acero] Fix the running tasks count of Scheduler when get error tasks in multi-threads #45268

Merged
merged 10 commits into from
Feb 12, 2025
24 changes: 17 additions & 7 deletions cpp/src/arrow/acero/task_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ Status TaskSchedulerImpl::ExecuteMore(size_t thread_id, int num_tasks_to_execute
bool task_group_finished = false;
Status status = ExecuteTask(thread_id, group_id, task_id, &task_group_finished);
if (!status.ok()) {
// Mark the remaining picked tasks as finished
for (size_t j = i + 1; j < tasks.size(); ++j) {
// Mark the current and remaining picked tasks as finished
for (size_t j = i; j < tasks.size(); ++j) {
if (PostExecuteTask(thread_id, tasks[j].first)) {
bool all_task_groups_finished = false;
RETURN_NOT_OK(
Expand Down Expand Up @@ -369,17 +369,25 @@ Status TaskSchedulerImpl::ScheduleMore(size_t thread_id, int num_tasks_finished)
int group_id = tasks[i].first;
int64_t task_id = tasks[i].second;
RETURN_NOT_OK(schedule_impl_([this, group_id, task_id](size_t thread_id) -> Status {
RETURN_NOT_OK(ScheduleMore(thread_id, 1));

bool task_group_finished = false;
RETURN_NOT_OK(ExecuteTask(thread_id, group_id, task_id, &task_group_finished));
// PostExecuteTask must be called later if any error ocurres during task execution
// (including ScheduleMore), so we preserve the status.
auto status = [&]() {
RETURN_NOT_OK(ScheduleMore(thread_id, 1));
return ExecuteTask(thread_id, group_id, task_id, &task_group_finished);
}();

if (!status.ok()) {
task_group_finished = PostExecuteTask(thread_id, group_id);
}

if (task_group_finished) {
bool all_task_groups_finished = false;
return OnTaskGroupFinished(thread_id, group_id, &all_task_groups_finished);
RETURN_NOT_OK(
OnTaskGroupFinished(thread_id, group_id, &all_task_groups_finished));
Comment on lines +368 to +382
Copy link
Contributor

Choose a reason for hiding this comment

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

I made some refinement on top of your original change, PTAL.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, I am not sure which one should be returned when we get two error status: CurrentTask's and OnTaskGroupFinished's. I think maybe we can just return the first error status(CurrentTask always error before OnTaskGroupFinished).
Anyway, it is ok for each one be returned :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I don't think it really matters which error to return. So I chose the one can most simplify the code :)

}

return Status::OK();
return status;
}));
}

Expand Down Expand Up @@ -413,6 +421,8 @@ void TaskSchedulerImpl::Abort(AbortContinuationImpl impl) {
all_finished = false;
task_group.state_ = TaskGroupState::ALL_TASKS_STARTED;
}
} else if (task_group.state_ == TaskGroupState::ALL_TASKS_STARTED) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is needed for allowing the Abort to be called in a task itself, otherwise the abort continuation will be called twice.

I suppose the Abort function is not necessarily to be called inside a task body to trigger the issue. In other words, it could happen when Abort is called in a timing that is subtle enough.

Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: can we change this to a switch?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, done.

all_finished = false;
}
}
}
Expand Down
91 changes: 91 additions & 0 deletions cpp/src/arrow/acero/task_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,96 @@ TEST(TaskScheduler, StressTwo) {
}
}

TEST(TaskScheduler, AbortContOnTaskErrorSerial) {
constexpr int kNumTasks = 16;

auto scheduler = TaskScheduler::Make();
auto task = [&](std::size_t, int64_t task_id) {
if (task_id == kNumTasks / 2) {
return Status::Invalid("Task failed");
}
return Status::OK();
};

int task_group =
scheduler->RegisterTaskGroup(task, [](std::size_t) { return Status::OK(); });
scheduler->RegisterEnd();

ASSERT_OK(scheduler->StartScheduling(
0, [](TaskScheduler::TaskGroupContinuationImpl) { return Status::OK(); }, 1, true));
Copy link
Member

Choose a reason for hiding this comment

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

/Can we annotate non-obvious parameters with their names?

Suggested change
ASSERT_OK(scheduler->StartScheduling(
0, [](TaskScheduler::TaskGroupContinuationImpl) { return Status::OK(); }, 1, true));
ASSERT_OK(scheduler->StartScheduling(
/*xxx=*/0, [](TaskScheduler::TaskGroupContinuationImpl) { return Status::OK(); },
/*xxx=*/1, /*xxx=*/true));

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you. Done.

ASSERT_RAISES_WITH_MESSAGE(Invalid, "Invalid: Task failed",
scheduler->StartTaskGroup(0, task_group, kNumTasks));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ASSERT_RAISES_WITH_MESSAGE(Invalid, "Invalid: Task failed",
scheduler->StartTaskGroup(0, task_group, kNumTasks));
ASSERT_RAISES_WITH_MESSAGE(Invalid, "Invalid: Task failed",
scheduler->StartTaskGroup(/*xxx=*/0, task_group, kNumTasks));

Copy link
Contributor

Choose a reason for hiding this comment

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

Done.


bool abort_cont_called = false;
auto abort_cont = [&]() {
ASSERT_FALSE(abort_cont_called);
abort_cont_called = true;
};

scheduler->Abort(abort_cont);

ASSERT_TRUE(abort_cont_called);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
bool abort_cont_called = false;
auto abort_cont = [&]() {
ASSERT_FALSE(abort_cont_called);
abort_cont_called = true;
};
scheduler->Abort(abort_cont);
ASSERT_TRUE(abort_cont_called);
int num_abort_calls = 0;
auto abort_cont = [&]() {
++num_abort_calls;
};
scheduler->Abort(abort_cont);
ASSERT_EQ(num_abort_calls, 1);

Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea, done! Thank you.

}

TEST(TaskScheduler, AbortContOnTaskErrorParallel) {
#ifndef ARROW_ENABLE_THREADING
GTEST_SKIP() << "Test requires threading support";
#endif
constexpr int kNumThreads = 16;

ThreadIndexer thread_indexer;
int num_threads = std::min(static_cast<int>(thread_indexer.Capacity()), kNumThreads);
ASSERT_OK_AND_ASSIGN(std::shared_ptr<ThreadPool> thread_pool,
MakePrimedThreadPool(num_threads));
TaskScheduler::ScheduleImpl schedule =
[&](TaskScheduler::TaskGroupContinuationImpl task) {
return thread_pool->Spawn([&, task] {
std::size_t thread_id = thread_indexer();
auto status = task(thread_id);
ASSERT_TRUE(status.ok() || status.IsInvalid() || status.IsCancelled());
});
};

for (int num_tasks :
{2, num_threads - 1, num_threads, num_threads + 1, 2 * num_threads}) {
ARROW_SCOPED_TRACE("num_tasks = ", num_tasks);
for (int num_concurrent_tasks :
{1, num_tasks - 1, num_tasks, num_tasks + 1, 2 * num_tasks}) {
ARROW_SCOPED_TRACE("num_concurrent_tasks = ", num_concurrent_tasks);
for (int aborting_task_id = 0; aborting_task_id < num_tasks; ++aborting_task_id) {
ARROW_SCOPED_TRACE("aborting_task_id = ", aborting_task_id);
auto scheduler = TaskScheduler::Make();

bool abort_cont_called = false;
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps use a counter as suggested above as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

Done.

auto abort_cont = [&]() {
ASSERT_FALSE(abort_cont_called);
abort_cont_called = true;
};

auto task = [&](std::size_t, int64_t task_id) {
if (task_id == aborting_task_id) {
scheduler->Abort(abort_cont);
}
if (task_id % 2 == 0) {
return Status::Invalid("Task failed");
}
return Status::OK();
};

int task_group =
scheduler->RegisterTaskGroup(task, [](std::size_t) { return Status::OK(); });
scheduler->RegisterEnd();

ASSERT_OK(scheduler->StartScheduling(0, schedule, num_concurrent_tasks, false));
ASSERT_OK(scheduler->StartTaskGroup(0, task_group, num_tasks));
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so the Status::Invalid that's returned above is never reported?

Copy link
Contributor

Choose a reason for hiding this comment

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

At least not here. This StartTaskGroup merely schedules the parallel tasks into the underlying executor (the raw thread pool in this specific case, and the more sophisticated AsyncScheduler in official acero code path). The status returned here is merely if the tasks are successfully scheduled. The error of a specific task will be propagated thru the abort continuation of this TaskScheduler.


thread_pool->WaitForIdle();

ASSERT_TRUE(abort_cont_called);
}
}
}
}

} // namespace acero
} // namespace arrow
Loading