aocsa commented on a change in pull request #11210:
URL: https://github.com/apache/arrow/pull/11210#discussion_r722858438



##########
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:
       `MapNode::MarkFinished()` does not mark finished right away. First It 
check if it is running in async mode or not, if it is async (has an executor) 
then it adds a callback  to the `task_group`. Maybe the name is not clear for 
the porpuse of this logic




-- 
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]


Reply via email to