westonpace commented on a change in pull request #9095:
URL: https://github.com/apache/arrow/pull/9095#discussion_r564778430



##########
File path: cpp/src/arrow/csv/reader_test.cc
##########
@@ -0,0 +1,169 @@
+// 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 <cstdint>
+#include <string>
+#include <thread>
+#include <utility>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "arrow/csv/options.h"
+#include "arrow/csv/reader.h"
+#include "arrow/csv/test_common.h"
+#include "arrow/io/interfaces.h"
+#include "arrow/status.h"
+#include "arrow/table.h"
+#include "arrow/testing/gtest_util.h"
+#include "arrow/util/future.h"
+#include "arrow/util/thread_pool.h"
+
+namespace arrow {
+namespace csv {
+
+void StressTableReader(
+    
std::function<Result<std::shared_ptr<TableReader>>(std::shared_ptr<io::InputStream>)>
+        reader_factory) {
+  const int NTASKS = 100;
+  const int NROWS = 1000;
+  ASSERT_OK_AND_ASSIGN(auto table_buffer, MakeSampleCsvBuffer(NROWS));
+
+  std::vector<Future<std::shared_ptr<Table>>> task_futures(NTASKS);
+  for (int i = 0; i < NTASKS; i++) {
+    auto input = std::make_shared<io::BufferReader>(table_buffer);
+    ASSERT_OK_AND_ASSIGN(auto reader, reader_factory(input));
+    task_futures[i] = reader->ReadAsync();
+  }
+  auto combined_future = All(task_futures);
+  combined_future.Wait();
+
+  ASSERT_OK_AND_ASSIGN(std::vector<Result<std::shared_ptr<Table>>> results,
+                       combined_future.result());
+  for (auto&& result : results) {
+    ASSERT_OK_AND_ASSIGN(auto table, result);
+    ASSERT_EQ(NROWS, table->num_rows());
+  }
+}
+
+void TestNestedParallelism(
+    std::shared_ptr<internal::ThreadPool> thread_pool,
+    
std::function<Result<std::shared_ptr<TableReader>>(std::shared_ptr<io::InputStream>)>
+        reader_factory) {
+  const int NROWS = 1000;
+  ASSERT_OK_AND_ASSIGN(auto table_buffer, MakeSampleCsvBuffer(NROWS));
+  auto input = std::make_shared<io::BufferReader>(table_buffer);
+  ASSERT_OK_AND_ASSIGN(auto reader, reader_factory(input));
+
+  Future<std::shared_ptr<Table>> table_future;
+
+  auto read_task = [&reader, &table_future]() mutable {
+    table_future = reader->ReadAsync();
+    return Status::OK();
+  };
+  ASSERT_OK_AND_ASSIGN(auto future, thread_pool->Submit(read_task));
+  ASSERT_TRUE(future.Wait(1));
+
+  if (future.is_finished()) {
+    ASSERT_TRUE(table_future.Wait(1));
+    if (table_future.is_finished()) {
+      ASSERT_OK_AND_ASSIGN(auto table, table_future.result());
+      ASSERT_EQ(table->num_rows(), NROWS);
+    }
+  }
+}  // namespace csv
+
+TEST(SerialReaderTests, Stress) {
+  auto task_factory = [](std::shared_ptr<io::InputStream> input_stream) {
+    return TableReader::Make(default_memory_pool(), io::AsyncContext(), 
input_stream,
+                             ReadOptions::Defaults(), ParseOptions::Defaults(),
+                             ConvertOptions::Defaults());
+  };
+  StressTableReader(task_factory);
+}
+
+TEST(SerialReaderTests, NestedParallelism) {
+  ASSERT_OK_AND_ASSIGN(auto thread_pool, internal::ThreadPool::Make(1));
+  auto task_factory = [](std::shared_ptr<io::InputStream> input_stream) {
+    return TableReader::Make(default_memory_pool(), io::AsyncContext(), 
input_stream,
+                             ReadOptions::Defaults(), ParseOptions::Defaults(),
+                             ConvertOptions::Defaults());
+  };
+  TestNestedParallelism(thread_pool, task_factory);
+}
+
+TEST(ThreadedReaderTests, Stress) {
+  ASSERT_OK_AND_ASSIGN(auto thread_pool, internal::ThreadPool::Make(1));
+  auto task_factory = [&thread_pool](std::shared_ptr<io::InputStream> 
input_stream)
+      -> Result<std::shared_ptr<TableReader>> {
+    ReadOptions read_options = ReadOptions::Defaults();
+    read_options.use_threads = true;
+    read_options.legacy_blocking_reads = true;
+    auto table_reader = TableReader::Make(
+        default_memory_pool(), io::AsyncContext(thread_pool.get()), 
input_stream,

Review comment:
       Hmm...if that is the purpose of `io::AsyncContext` then I have misused 
it.  I don't think that *should* be the purpose though.  It's possible I am 
wrong.  Although looking at the existing implementation I think I am indeed in 
misalignment...
   ```
   // Default ReadAsync() implementation: simply issue the read on the 
context's executor
   Future<std::shared_ptr<Buffer>> RandomAccessFile::ReadAsync(const 
AsyncContext& ctx, ...) {
     return DeferNotOk(ctx.executor->Submit(...);
   }
   ```
   ...this logic doesn't work I don't think.  Well, it puts the burden on the 
user to transfer the continuation to the CPU context.  As it stands, if the 
user added a continuation, that continuation would run on the IO context.
   
   When I see something like...
   
   ```
   Future SomeIOOperation(AsyncContext ctx)
   ```
   
   In my mind `ctx` is "This is the context on which work should resume when 
the I/O operation completes".  Indeed, it is the CPU context which has to be 
async most of the time.  There is typically not much harm in having a 
synchronous I/O context.
   
   However, it sounds like the existing definition is "This is the context on 
which the I/O operation should run".  However, I think that should be owned by 
the FS.  Maybe AWS has a thread pool of its own that manages its operations.  
Maybe a memory wrapping filesystem uses a single synchronous thread.  Maybe the 
FS is using libuv which has its own background thread pool.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to