westonpace commented on code in PR #35902:
URL: https://github.com/apache/arrow/pull/35902#discussion_r1221603920


##########
cpp/src/arrow/acero/source_node.cc:
##########
@@ -263,6 +263,22 @@ struct SourceNode : ExecNode, public TracedNode {
     to_finish.MarkFinished();
   }
 
+  Status StopProducing() override {
+    // GH-35837: ensure node is not paused
+    Future<> to_finish;
+    {
+      std::lock_guard<std::mutex> lg(mutex_);
+      if (!backpressure_future_.is_finished()) {
+        to_finish = backpressure_future_;

Review Comment:
   ```suggestion
           to_finish = backpressure_future_;
           backpressure_future_ = Future<>::MakeFinished();
   ```
   
   We might want to do this here and in `ResumeProducing`.  Otherwise, if 
`ResumeProducing` and `StopProducing` are called at the same time then both 
calls might try and mark the same future finished (which isn't valid).



##########
cpp/src/arrow/acero/source_node_test.cc:
##########
@@ -0,0 +1,109 @@
+// 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 <gtest/gtest.h>
+
+#include "arrow/acero/exec_plan.h"
+#include "arrow/acero/map_node.h"
+#include "arrow/acero/options.h"
+#include "arrow/acero/test_nodes.h"
+
+namespace arrow {
+namespace acero {
+
+struct PauseThenStopNodeOptions : public ExecNodeOptions {
+  explicit PauseThenStopNodeOptions(int num_pass) : num_pass(num_pass) {}
+
+  int num_pass;
+};
+
+struct PauseThenStopNode : public MapNode {
+  static constexpr const char* kKindName = "PauseThenStopNode";
+  static constexpr const char* kFactoryName = "pause_then_stop";
+
+  static void Register() {
+    auto exec_reg = default_exec_factory_registry();
+    if (!exec_reg->GetFactory(kFactoryName).ok()) {
+      ASSERT_OK(exec_reg->AddFactory(kFactoryName, PauseThenStopNode::Make));
+    }
+  }
+
+  PauseThenStopNode(ExecPlan* plan, std::vector<ExecNode*> inputs,
+                    std::shared_ptr<Schema> output_schema,
+                    const PauseThenStopNodeOptions& options)
+      : MapNode(plan, inputs, output_schema), num_pass(options.num_pass) {}
+
+  static Result<ExecNode*> Make(ExecPlan* plan, std::vector<ExecNode*> inputs,
+                                const ExecNodeOptions& options) {
+    RETURN_NOT_OK(ValidateExecNodeInputs(plan, inputs, 1, kKindName));
+    auto bp_options = static_cast<const PauseThenStopNodeOptions&>(options);
+    if (bp_options.num_pass < 2) {
+      return Status::Invalid("num_pass must be at least 2");
+    }
+    return plan->EmplaceNode<PauseThenStopNode>(plan, inputs, 
inputs[0]->output_schema(),
+                                                bp_options);
+  }
+
+  const char* kind_name() const override { return kKindName; }
+  Result<ExecBatch> ProcessBatch(ExecBatch batch) override {

Review Comment:
   That sounds about right.  In practice, at any given moment, we assume the 
source node is in the middle of obtaining the next batch (e.g. reading a batch 
from flight or reading a batch from the disk or some other operation).  This is 
the primary loop.  If we strip away the async stuff (and ignore 
synchronization) it would look something like...
   
   ```
   void StartProducing() {
     while (true) {
       while (!paused_) {
         Batch next = source.Poll();
         if (!next) {
           // Finished reading the source
           return;
         }
         output_->InputReceived(next);
       }
       paused_cv_.wait();
     }
   }
   
   void PauseProducing() {
     paused_ = true;
   }
   
   void ResumeProducing() {
     paused_ = false;
     paused_cv_.notify_all();
   }
   ```
   
   Still, I would naively expect what Li said might technically be possible 
(e.g. 99% of the time when PauseProducing is called the above loop will be in 
`source.Poll` because that's the slow part. So you will get at least one batch 
after the pause.  However, if the timing is unlucky, maybe it is between 
`source.Poll` and the `!paused` check and you don't end up getting one more 
batch.
   
   However, this is all running with `use_threads=false` which means the 
polling loop and the loop here are maybe all the same thread and so maybe the 
behavior is deterministic and correct.  I'd have to look further.



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