westonpace commented on a change in pull request #11210: URL: https://github.com/apache/arrow/pull/11210#discussion_r722099929
########## File path: cpp/src/arrow/compute/exec/exec_node_runner_impl.cc ########## @@ -0,0 +1,149 @@ +// 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. +#include "arrow/compute/exec/exec_node_runner_impl.h" + +#include <functional> +#include <memory> +#include <vector> + +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/type.h" +#include "arrow/util/async_util.h" +#include "arrow/util/cancel.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { +namespace compute { + +class SimpleSyncRunnerImpl : public ExecNodeRunnerImpl { + public: + explicit SimpleSyncRunnerImpl(ExecContext* ctx) : ctx_(ctx) {} + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task(); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + this->finished_.MarkFinished(status); + } + + void InputFinished(int total_batches) override { + if (input_counter_.SetTotal(total_batches)) { + this->MarkFinished(); + } + } + + void StopProducing() override { + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + } + + protected: + // Executor context + ExecContext* ctx_; + + // Counter for the number of batches received + AtomicCounter input_counter_; +}; + +class SimpleParallelRunner : public SimpleSyncRunnerImpl { + public: + explicit SimpleParallelRunner(ExecContext* ctx) : SimpleSyncRunnerImpl(ctx) { + executor_ = ctx->executor(); + } + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task_group_.AddTask([this, task]() -> Result<Future<>> { + return this->executor_->Submit(this->stop_source_.token(), + [task]() { return task(); }); + }); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + if (!status.ok()) { + this->StopProducing(); + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + return; + } + task_group_.End().AddCallback([this](const Status& status) { + if (!status.ok()) { + this->finished_.MarkFinished(); + } else if (!this->finished_.is_finished()) { + this->finished_.MarkFinished(status); + } Review comment: `this->finished_.MarkFinished(Status::OK())` is synonymous with `this->finished_.MarkFinished()` I think (I might be wrong on this) so you could simplify this to... ``` if (!this->finished_.is_finished()) { this->finished_.MarkFinished(status); } ``` ########## File path: cpp/src/arrow/compute/exec/exec_node_runner_impl.cc ########## @@ -0,0 +1,149 @@ +// 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. +#include "arrow/compute/exec/exec_node_runner_impl.h" + +#include <functional> +#include <memory> +#include <vector> + +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/type.h" +#include "arrow/util/async_util.h" +#include "arrow/util/cancel.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { +namespace compute { + +class SimpleSyncRunnerImpl : public ExecNodeRunnerImpl { + public: + explicit SimpleSyncRunnerImpl(ExecContext* ctx) : ctx_(ctx) {} + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task(); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + this->finished_.MarkFinished(status); + } + + void InputFinished(int total_batches) override { + if (input_counter_.SetTotal(total_batches)) { + this->MarkFinished(); + } + } + + void StopProducing() override { + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + } + + protected: + // Executor context + ExecContext* ctx_; + + // Counter for the number of batches received + AtomicCounter input_counter_; +}; + +class SimpleParallelRunner : public SimpleSyncRunnerImpl { + public: + explicit SimpleParallelRunner(ExecContext* ctx) : SimpleSyncRunnerImpl(ctx) { + executor_ = ctx->executor(); + } + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task_group_.AddTask([this, task]() -> Result<Future<>> { + return this->executor_->Submit(this->stop_source_.token(), + [task]() { return task(); }); + }); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + if (!status.ok()) { + this->StopProducing(); + if (input_counter_.Cancel()) { + this->MarkFinished(); + } Review comment: `this->StopProducing()` is going to call `if (input_counter_.Cancel())` anyways so you can skip this second check. ########## File path: cpp/src/arrow/compute/exec/exec_node_runner_impl.cc ########## @@ -0,0 +1,149 @@ +// 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. +#include "arrow/compute/exec/exec_node_runner_impl.h" + +#include <functional> +#include <memory> +#include <vector> + +#include "arrow/compute/exec/options.h" +#include "arrow/compute/exec/util.h" +#include "arrow/record_batch.h" +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/type.h" +#include "arrow/util/async_util.h" +#include "arrow/util/cancel.h" +#include "arrow/util/thread_pool.h" + +namespace arrow { +namespace compute { + +class SimpleSyncRunnerImpl : public ExecNodeRunnerImpl { + public: + explicit SimpleSyncRunnerImpl(ExecContext* ctx) : ctx_(ctx) {} + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task(); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + this->finished_.MarkFinished(status); + } + + void InputFinished(int total_batches) override { + if (input_counter_.SetTotal(total_batches)) { + this->MarkFinished(); + } + } + + void StopProducing() override { + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + } + + protected: + // Executor context + ExecContext* ctx_; + + // Counter for the number of batches received + AtomicCounter input_counter_; +}; + +class SimpleParallelRunner : public SimpleSyncRunnerImpl { + public: + explicit SimpleParallelRunner(ExecContext* ctx) : SimpleSyncRunnerImpl(ctx) { + executor_ = ctx->executor(); + } + + Status SubmitTask(std::function<Status()> task) override { + if (finished_.is_finished()) { + return Status::OK(); + } + auto status = task_group_.AddTask([this, task]() -> Result<Future<>> { + return this->executor_->Submit(this->stop_source_.token(), + [task]() { return task(); }); + }); + if (!status.ok()) { + return status; + } + if (input_counter_.Increment()) { + this->MarkFinished(); + } + return Status::OK(); + } + + void MarkFinished(Status status = Status::OK()) override { + if (!status.ok()) { + this->StopProducing(); + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + return; + } + task_group_.End().AddCallback([this](const Status& status) { + if (!status.ok()) { + this->finished_.MarkFinished(); + } else if (!this->finished_.is_finished()) { + this->finished_.MarkFinished(status); + } + }); + } + + void StopProducing() override { + this->stop_source_.RequestStop(); + if (input_counter_.Cancel()) { + this->MarkFinished(); + } + } Review comment: `RequestStop` won't take hold immediately, it simply tells the task group to stop running any additional tasks but it won't/can't interrupt any currently running tasks. If we mark finished right away here then there is a risk that you discard the exec plan while tasks are still running. I think, even in error scenarios, we must wait for the task group to finish before marking `finished_` complete. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
