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
ARROW-17509: [C++] Simplify async scheduler by removing the need to call End #14524
Merged
westonpace
merged 11 commits into
apache:master
from
westonpace:feature/ARROW-17509--async-task-scheduler-without-end
Nov 9, 2022
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
743a10f
ARROW-17509: Modified the AsyncTaskScheduler so that it does not requ…
westonpace c86b451
ARROW-17509: Lint and fixing a memory leak in a test
westonpace 3b51219
ARROW-17509: Reworked dataset writer integration into exec plan sligh…
westonpace 3b50224
ARROW-17509: WIP
westonpace 8bad34c
ARROW-17509: Reverted back to simpler approach. I tried various atte…
westonpace 884d9e0
ARROW-17509: Extensive rework of AsyncTaskScheduler, splitting the sc…
westonpace 63de6a5
ARROW-17509: Add some missing windows exports
westonpace 45d952a
ARROW-17509: Cleaned up a few CI failures, mostly focused on ASAN lea…
westonpace b382413
ARROW-17509: Windows dll linkage
westonpace f5ae084
Update cpp/src/arrow/util/async_util.h
westonpace 42fe57d
ARROW-17509: Address review comments
westonpace File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,7 +114,7 @@ struct ExecPlanImpl : public ExecPlan { | |
return task_scheduler_->StartTaskGroup(GetThreadIndex(), task_group_id, num_tasks); | ||
} | ||
|
||
util::AsyncTaskScheduler* async_scheduler() { return async_scheduler_.get(); } | ||
util::AsyncTaskScheduler* async_scheduler() { return async_scheduler_; } | ||
|
||
Status Validate() const { | ||
if (nodes_.empty()) { | ||
|
@@ -127,85 +127,89 @@ struct ExecPlanImpl : public ExecPlan { | |
} | ||
|
||
Status StartProducing() { | ||
START_COMPUTE_SPAN(span_, "ExecPlan", {{"plan", ToString()}}); | ||
#ifdef ARROW_WITH_OPENTELEMETRY | ||
if (HasMetadata()) { | ||
auto pairs = metadata().get()->sorted_pairs(); | ||
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span = | ||
::arrow::internal::tracing::UnwrapSpan(span_.details.get()); | ||
std::for_each(std::begin(pairs), std::end(pairs), | ||
[span](std::pair<std::string, std::string> const& pair) { | ||
span->SetAttribute(pair.first, pair.second); | ||
}); | ||
} | ||
#endif | ||
if (started_) { | ||
return Status::Invalid("restarted ExecPlan"); | ||
} | ||
|
||
std::vector<Future<>> futures; | ||
for (auto& n : nodes_) { | ||
RETURN_NOT_OK(n->Init()); | ||
futures.push_back(n->finished()); | ||
} | ||
|
||
AllFinished(futures).AddCallback([this](const Status& st) { | ||
error_st_ = st; | ||
EndTaskGroup(); | ||
}); | ||
|
||
task_scheduler_->RegisterEnd(); | ||
int num_threads = 1; | ||
bool sync_execution = true; | ||
if (auto executor = exec_context()->executor()) { | ||
num_threads = executor->GetCapacity(); | ||
sync_execution = false; | ||
} | ||
RETURN_NOT_OK(task_scheduler_->StartScheduling( | ||
0 /* thread_index */, | ||
[this](std::function<Status(size_t)> fn) -> Status { | ||
return this->ScheduleTask(std::move(fn)); | ||
}, | ||
/*concurrent_tasks=*/2 * num_threads, sync_execution)); | ||
|
||
started_ = true; | ||
// producers precede consumers | ||
sorted_nodes_ = TopoSort(); | ||
|
||
Status st = Status::OK(); | ||
|
||
using rev_it = std::reverse_iterator<NodeVector::iterator>; | ||
for (rev_it it(sorted_nodes_.end()), end(sorted_nodes_.begin()); it != end; ++it) { | ||
auto node = *it; | ||
|
||
EVENT(span_, "StartProducing:" + node->label(), | ||
{{"node.label", node->label()}, {"node.kind_name", node->kind_name()}}); | ||
st = node->StartProducing(); | ||
EVENT(span_, "StartProducing:" + node->label(), {{"status", st.ToString()}}); | ||
if (!st.ok()) { | ||
// Stop nodes that successfully started, in reverse order | ||
stopped_ = true; | ||
StopProducingImpl(it.base(), sorted_nodes_.end()); | ||
for (NodeVector::iterator fw_it = sorted_nodes_.begin(); fw_it != it.base(); | ||
++fw_it) { | ||
Future<> fut = (*fw_it)->finished(); | ||
if (!fut.is_finished()) fut.MarkFinished(); | ||
} | ||
return st; | ||
} | ||
} | ||
return st; | ||
} | ||
|
||
void EndTaskGroup() { | ||
bool expected = false; | ||
if (group_ended_.compare_exchange_strong(expected, true)) { | ||
async_scheduler_->End(); | ||
async_scheduler_->OnFinished().AddCallback([this](const Status& st) { | ||
MARK_SPAN(span_, error_st_ & st); | ||
END_SPAN(span_); | ||
finished_.MarkFinished(error_st_ & st); | ||
}); | ||
// We call StartProducing on each of the nodes. The source nodes should generally | ||
// start scheduling some tasks during this call. | ||
// | ||
// If no source node schedules any tasks (e.g. they do all their word synchronously as | ||
// part of StartProducing) then the plan may be finished before we return from this | ||
// call. | ||
Future<> scheduler_finished = | ||
util::AsyncTaskScheduler::Make([this](util::AsyncTaskScheduler* async_scheduler) { | ||
this->async_scheduler_ = async_scheduler; | ||
START_COMPUTE_SPAN(span_, "ExecPlan", {{"plan", ToString()}}); | ||
#ifdef ARROW_WITH_OPENTELEMETRY | ||
if (HasMetadata()) { | ||
auto pairs = metadata().get()->sorted_pairs(); | ||
opentelemetry::nostd::shared_ptr<opentelemetry::trace::Span> span = | ||
::arrow::internal::tracing::UnwrapSpan(span_.details.get()); | ||
std::for_each(std::begin(pairs), std::end(pairs), | ||
[span](std::pair<std::string, std::string> const& pair) { | ||
span->SetAttribute(pair.first, pair.second); | ||
}); | ||
} | ||
#endif | ||
// TODO(weston) The entire concept of ExecNode::finished() will hopefully go | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep my simplification PR gets rid of those finished futures. |
||
// away soon (or at least be replaced by a sub-scheduler to facilitate OT) | ||
for (auto& n : nodes_) { | ||
RETURN_NOT_OK(n->Init()); | ||
async_scheduler->AddSimpleTask([&] { return n->finished(); }); | ||
} | ||
|
||
task_scheduler_->RegisterEnd(); | ||
int num_threads = 1; | ||
bool sync_execution = true; | ||
if (auto executor = exec_context()->executor()) { | ||
num_threads = executor->GetCapacity(); | ||
sync_execution = false; | ||
} | ||
RETURN_NOT_OK(task_scheduler_->StartScheduling( | ||
0 /* thread_index */, | ||
[this](std::function<Status(size_t)> fn) -> Status { | ||
return this->ScheduleTask(std::move(fn)); | ||
}, | ||
/*concurrent_tasks=*/2 * num_threads, sync_execution)); | ||
|
||
// producers precede consumers | ||
sorted_nodes_ = TopoSort(); | ||
|
||
Status st = Status::OK(); | ||
|
||
using rev_it = std::reverse_iterator<NodeVector::iterator>; | ||
for (rev_it it(sorted_nodes_.end()), end(sorted_nodes_.begin()); it != end; | ||
++it) { | ||
auto node = *it; | ||
|
||
EVENT(span_, "StartProducing:" + node->label(), | ||
{{"node.label", node->label()}, {"node.kind_name", node->kind_name()}}); | ||
st = node->StartProducing(); | ||
EVENT(span_, "StartProducing:" + node->label(), {{"status", st.ToString()}}); | ||
if (!st.ok()) { | ||
// Stop nodes that successfully started, in reverse order | ||
stopped_ = true; | ||
StopProducingImpl(it.base(), sorted_nodes_.end()); | ||
for (NodeVector::iterator fw_it = sorted_nodes_.begin(); fw_it != it.base(); | ||
++fw_it) { | ||
Future<> fut = (*fw_it)->finished(); | ||
if (!fut.is_finished()) fut.MarkFinished(); | ||
} | ||
return st; | ||
} | ||
} | ||
return st; | ||
}); | ||
scheduler_finished.AddCallback( | ||
[this](const Status& st) { finished_.MarkFinished(st); }); | ||
// TODO(weston) Do we really need to return status here? Could we change this return | ||
// to void? | ||
if (finished_.is_finished()) { | ||
return finished_.status(); | ||
} else { | ||
return Status::OK(); | ||
} | ||
} | ||
|
||
|
@@ -331,9 +335,7 @@ struct ExecPlanImpl : public ExecPlan { | |
std::shared_ptr<const KeyValueMetadata> metadata_; | ||
|
||
ThreadIndexer thread_indexer_; | ||
std::atomic<bool> group_ended_{false}; | ||
std::unique_ptr<util::AsyncTaskScheduler> async_scheduler_ = | ||
util::AsyncTaskScheduler::Make(); | ||
util::AsyncTaskScheduler* async_scheduler_ = nullptr; | ||
std::unique_ptr<TaskScheduler> task_scheduler_ = TaskScheduler::Make(); | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we move this giant lambda into its own function? The extra layer of indentation is pretty confusing