You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "mapleFU (via GitHub)" <gi...@apache.org> on 2023/11/26 11:09:29 UTC

[PR] GH-38884: [C++] DatasetWriter release resource when File is full [arrow]

mapleFU opened a new pull request, #38885:
URL: https://github.com/apache/arrow/pull/38885

   <!--
   Thanks for opening a pull request!
   If this is your first pull request you can find detailed information on how 
   to contribute here:
     * [New Contributor's Guide](https://arrow.apache.org/docs/dev/developers/guide/step_by_step/pr_lifecycle.html#reviews-and-merge-of-the-pull-request)
     * [Contributing Overview](https://arrow.apache.org/docs/dev/developers/overview.html)
   
   
   If this is not a [minor PR](https://github.com/apache/arrow/blob/main/CONTRIBUTING.md#Minor-Fixes). Could you open an issue for this pull request on GitHub? https://github.com/apache/arrow/issues/new/choose
   
   Opening GitHub issues ahead of time contributes to the [Openness](http://theapacheway.com/open/#:~:text=Openness%20allows%20new%20users%20the,must%20happen%20in%20the%20open.) of the Apache Arrow project.
   
   Then could you also rename the pull request title in the following format?
   
       GH-${GITHUB_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   or
   
       MINOR: [${COMPONENT}] ${SUMMARY}
   
   In the case of PARQUET issues on JIRA the title also supports:
   
       PARQUET-${JIRA_ISSUE_ID}: [${COMPONENT}] ${SUMMARY}
   
   -->
   
   ### Rationale for this change
   
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   When file-queue is fall or write failed, the `DatasetWriterImpl::DoWriteRecordBatch` might failed, however, the resources are not released.
   
   ### What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   1. When file-queue is full, release the `row` resources.
   2. Add `clean_up_back_pressure`.
   
   ### Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   3. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   no
   
   ### Are there any user-facing changes?
   
   no
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please uncomment the line below and explain which changes are breaking.
   -->
   <!-- **This PR includes breaking changes to public APIs.** -->
   
   <!--
   Please uncomment the line below (and provide explanation) if the changes fix either (a) a security vulnerability, (b) a bug that caused incorrect or invalid data to be produced, or (c) a bug that causes a crash (even when the API contract is upheld). We use this to highlight fixes to issues that may affect users without their knowledge. For this reason, fixing bugs that cause errors don't count, since those are usually obvious.
   -->
   <!-- **This PR contains a "Critical Fix".** -->


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1833001802

   @kou I've try to fix some comments, would you mind take a look?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1411644279


##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {
+  // GH-38884: This test is to make sure that the writer can handle
+  //  throttle resources in `WriteRecordBatch`.
+
+  // CalculateMaxRowsStaged will make at least 1 << 23 rows for
+  // `rows_in_flight` throttle. So we set the limit to 1 << 23.
+  constexpr auto FILE_SIZE_LIMIT = static_cast<uint64_t>(1 << 23);

Review Comment:
   Oh I made it wrong here. It easier than I expected.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1413579865


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Oh I got what you mean, sorry for that! I think I just test https://github.com/apache/arrow/pull/38885/files#diff-387ad04c2450a38044e667e07183b8265866cb3736d10acdce137c2b83737b16R624
   
   `DatasetWriterDirectoryQueue::StartWrite` is a bit hard to test, because `OpenFileQueue` always return OK now, `DatasetWriterFileQueue::Push` also merely failed



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1410079812


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,18 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Find it a bit hard to get related...



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1854113164

   I guess `DatasetWriterFileQueue::WriteNext` form a kind of deadlock or other in current impl, but I need time to checking it


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release resource when File is full [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1405438656


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,18 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   I'm considering should I release `open_files_throttle` here, I gothrough the impl, seems that `OpenFileQueue` would not failed, so I didn't release `open_files_throttle` 



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1405564397


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,18 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Could you add the note as a comment?



##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,18 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Can we move the `rows_in_flight_throttle.Acquire()` call to close to the existing `rows_in_flight_throttle.Release()` call and this `Release()` call to there? (`DatasetWriterFileQueue`?)
   I think that the `Acquire()`/`Release()` pair should be close for easy to maintain like `malloc()`/`free()` pair and `new`/`delete` pair.
   
   (Can we apply the same approach to the `open_files_throttle.Acquire()`/`Release()` pair too?)



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1854195702

   ```c++
     Future<> WriteNext(std::shared_ptr<RecordBatch> next) {
       // May want to prototype / measure someday pushing the async write down further
       return DeferNotOk(options_.filesystem->io_context().executor()->Submit(
           [self = this, batch = std::move(next)]() {
             int64_t rows_to_release = batch->num_rows();
             Status status = self->writer_->Write(batch);
             self->writer_state_->rows_in_flight_throttle.Release(rows_to_release);
             return status;
           }));
     }
   ```
   
   `self->writer_state_->rows_in_flight_throttle.Release` might trigger the callback, I guess this might trigger the problem. But I need some time to convince @kou 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1411557649


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Aha the added test hang forever on my machine😂 let me take a look...



##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Aha the added test hang forever on my machine😂 let me take a look...



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release resource when File is full [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1826754146

   cc @kou @bkietz 


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1411552495


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   It seems that the added test is passed without this.
   Is it difficult to write a test for this change?



##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {
+  // GH-38884: This test is to make sure that the writer can handle
+  //  throttle resources in `WriteRecordBatch`.
+
+  // CalculateMaxRowsStaged will make at least 1 << 23 rows for
+  // `rows_in_flight` throttle. So we set the limit to 1 << 23.
+  constexpr auto FILE_SIZE_LIMIT = static_cast<uint64_t>(1 << 23);

Review Comment:
   See also: https://google.github.io/styleguide/cppguide.html#Constant_Names
   
   ```suggestion
     constexpr auto kFileSizeLimit = static_cast<uint64_t>(1 << 23);
   ```



##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {

Review Comment:
   Can we use more meaningful test name? `MaxRowsOneWriteBackprresure`?



##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {
+  // GH-38884: This test is to make sure that the writer can handle
+  //  throttle resources in `WriteRecordBatch`.
+
+  // CalculateMaxRowsStaged will make at least 1 << 23 rows for
+  // `rows_in_flight` throttle. So we set the limit to 1 << 23.
+  constexpr auto FILE_SIZE_LIMIT = static_cast<uint64_t>(1 << 23);

Review Comment:
   Hmm. This test takes 8 secs on my local machine...
   How about making `CaluculateMaxRowsStaged` customizable something like the following and using small value for this test?
   
   ```diff
   diff --git a/cpp/src/arrow/dataset/dataset_writer.cc b/cpp/src/arrow/dataset/dataset_writer.cc
   index ae9fb3648..7c5a5befd 100644
   --- a/cpp/src/arrow/dataset/dataset_writer.cc
   +++ b/cpp/src/arrow/dataset/dataset_writer.cc
   @@ -500,8 +500,8 @@ Status EnsureDestinationValid(const FileSystemDatasetWriteOptions& options) {
    // Rule of thumb for the max rows to stage.  It will grow with max_rows_queued until
    // max_rows_queued starts to get too large and then it caps out at 8 million rows.
    // Feel free to replace with something more meaningful, this is just a random heuristic.
   -uint64_t CalculateMaxRowsStaged(uint64_t max_rows_queued) {
   -  return std::min(static_cast<uint64_t>(1 << 23), max_rows_queued / 4);
   +uint64_t CalculateMaxRowsStaged(const FileSystemDatasetWriteOptions& options, uint64_t max_rows_queued) {
   +  return std::min(options.max_rows_staged, max_rows_queued / 4);
    }
    
    }  // namespace
   @@ -522,7 +522,7 @@ class DatasetWriter::DatasetWriterImpl {
                })),
            write_options_(std::move(write_options)),
            writer_state_(max_rows_queued, write_options_.max_open_files,
   -                      CalculateMaxRowsStaged(max_rows_queued)),
   +                      CalculateMaxRowsStaged(write_options_, max_rows_queued)),
            pause_callback_(std::move(pause_callback)),
            resume_callback_(std::move(resume_callback)) {}
    
   diff --git a/cpp/src/arrow/dataset/file_base.h b/cpp/src/arrow/dataset/file_base.h
   index 46fc8ebc4..62fda95e2 100644
   --- a/cpp/src/arrow/dataset/file_base.h
   +++ b/cpp/src/arrow/dataset/file_base.h
   @@ -445,6 +445,9 @@ struct ARROW_DS_EXPORT FileSystemDatasetWriteOptions {
      /// This is mainly intended for filesystems that do not require directories such as S3.
      bool create_dir = true;
    
   +  /// ...
   +  uint64_t max_rows_staged = 1 << 23;
   +
      /// Callback to be invoked against all FileWriters before
      /// they are finalized with FileWriter::Finish().
      std::function<Status(FileWriter*)> writer_pre_finish = [](FileWriter*) {
   ```



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1413565985


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Thanks. I tried again without this change:
   
   ```diff
   diff --git a/cpp/src/arrow/dataset/dataset_writer.cc b/cpp/src/arrow/dataset/dataset_writer.cc
   index ae9fb3648..c38e899ab 100644
   --- a/cpp/src/arrow/dataset/dataset_writer.cc
   +++ b/cpp/src/arrow/dataset/dataset_writer.cc
   @@ -633,7 +633,7 @@ class DatasetWriter::DatasetWriterImpl {
            //
            // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
            // so we don't need to release it here.
   -        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
   +        // writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
            return s;
          }
          batch = std::move(remainder);
   ```
   
   And the added test is still passing.
    Is it difficult to write a test for this change too? If it's difficult, I'll merge this without a test for this change.
    (I confirmed that the added test hanged without another change https://github.com/apache/arrow/pull/38885/files#diff-387ad04c2450a38044e667e07183b8265866cb3736d10acdce137c2b83737b16R624 .)



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1850860145

   FYI: It passed in the second run: https://github.com/apache/arrow/actions/runs/7170522378?pr=39176


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release resource when File is full [arrow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1826754208

   :warning: GitHub issue #38884 **has been automatically assigned in GitHub** to PR creator.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1827047884

   > Can we add a test for this case?
   
   Sure I'll finish this after work tonight


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1411557649


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Aha the added test hang forever on my machine(with debug compile)😂 let me take a look...



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1832220576

   Sorry for late replying because so busy this week, during writing test, I found that `dataset_writer` might write chunk with `size == 0`, is this expected?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1411644127


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   I've update the test, and make it holding less time and easier to reproduce the problem. The test run in MacOS with RelWithDebInfo before this patch will hang forever



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1413582673


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,21 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());
           RETURN_NOT_OK(TryCloseLargestFile());
           break;
         }
       }
-      RETURN_NOT_OK(dir_queue->StartWrite(next_chunk));
+      auto s = dir_queue->StartWrite(next_chunk);
+      if (!s.ok()) {
+        // If `StartWrite` succeeded, it will Release the
+        // `rows_in_flight_throttle` when the write task is finished.
+        //
+        // `open_files_throttle` will be handed by `DatasetWriterDirectoryQueue`
+        // so we don't need to release it here.
+        writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   OK.



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1855047632

   ```
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:115: Failure
   Value of: _fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)
     Actual: false
   Expected: true
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/result.cc:28: ValueOrDie called on an error: Invalid: File is too small: 0
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   Expected: (found) != (actual_paths.end()), actual: 8-byte object <E8-9B 00-53 01-00 00-00> vs 8-byte object <E8-9B 00-53 01-00 00-00>
   The file testdir/chunk-63.arrow was not in the list of files visited
   Google Test trace:
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:183: pre_finish
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   Expected: (found) != (actual_paths.end()), actual: 8-byte object <E8-8B 00-54 01-00 00-00> vs 8-byte object <E8-8B 00-54 01-00 00-00>
   The file testdir/chunk-63.arrow was not in the list of files visited
   Google Test trace:
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:187: post_finish
   /Users/fuxuwei/workspace/CMakeLibs/arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:162: Failure
   Value of: _st.ok()
     Actual: false
   Expected: true
   '_error_or_value27.status()' failed with Invalid: File is too small: 0
   0   libarrow.1500.0.0.dylib             0x00000001082d1ecc _ZN5arrow4util7CerrLog14PrintBackTraceEv + 44
   1   libarrow.1500.0.0.dylib             0x00000001082d1e80 _ZN5arrow4util7CerrLogD2Ev + 184
   2   libarrow.1500.0.0.dylib             0x00000001082d1dc0 _ZN5arrow4util7CerrLogD0Ev + 12
   3   libarrow.1500.0.0.dylib             0x00000001082d1d5c _ZN5arrow4util8ArrowLogD1Ev + 48
   4   libarrow.1500.0.0.dylib             0x00000001081a7a30 _ZN5arrow8internal17InvalidValueOrDieERKNS_6StatusE + 240
   5   arrow-dataset-dataset-writer-test   0x0000000104560ca0 _ZN5arrow7dataset8internal24DatasetWriterTestFixture11ReadAsBatchENSt3__117basic_string_viewIcNS3_11char_traitsIcEEEEPi + 4952
   6   arrow-dataset-dataset-writer-test   0x0000000104550390 _ZN5arrow7dataset8internal24DatasetWriterTestFixture17AssertCreatedDataERKNSt3__16vectorINS2_12ExpectedFileENS3_9allocatorIS5_EEEEb + 192
   7   arrow-dataset-dataset-writer-test   0x0000000104551d14 _ZN5arrow7dataset8internal56DatasetWriterTestFixture_MaxRowsOneWriteBackpresure_Test8TestBodyEv + 652
   8   libarrow_gtest.1.11.0.dylib         0x00000001049d1bdc _ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS_4TestEvEET0_PT_MS4_FS3_vEPKc + 92
   9   libarrow_gtest.1.11.0.dylib         0x00000001049d1b30 _ZN7testing4Test3RunEv + 444
   10  libarrow_gtest.1.11.0.dylib         0x00000001049d329c _ZN7testing8TestInfo3RunEv + 492
   11  libarrow_gtest.1.11.0.dylib         0x00000001049d3e58 _ZN7testing9TestSuite3RunEv + 352
   12  libarrow_gtest.1.11.0.dylib         0x00000001049e3c3c _ZN7testing8internal12UnitTestImpl11RunAllTestsEv + 2120
   13  libarrow_gtest.1.11.0.dylib         0x00000001049e32d0 _ZN7testing8internal35HandleExceptionsInMethodIfSupportedINS0_12UnitTestImplEbEET0_PT_MS4_FS3_vEPKc + 92
   14  libarrow_gtest.1.11.0.dylib         0x00000001049e3240 _ZN7testing8UnitTest3RunEv + 124
   15  libarrow_gtest_main.1.11.0.dylib    0x0000000104627e9c main + 68
   16  dyld                                0x00000001046f50f4 start + 520
   Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
   ```
   
   Using diff:
   
   ```
        // May want to prototype / measure someday pushing the async write down further
        return DeferNotOk(options_.filesystem->io_context().executor()->Submit(
            [self = this, batch = std::move(next)]() {
   +          // Sleep 1 s
   +          std::this_thread::sleep_for(std::chrono::seconds(1));
              int64_t rows_to_release = batch->num_rows();
              Status status = self->writer_->Write(batch);
              self->writer_state_->rows_in_flight_throttle.Release(rows_to_release);
   ```
   
   This can produce similiar, but when enlarge test time-out, the problem disappear. I think that maybe just I'm writing too many files, making schedule writing so slow?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1848355405

   After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit f7286a95304a848033d96817f704ace0765c0ec7.
   
   There was 1 benchmark result indicating a performance regression:
   
   - Commit Run on `ursa-i9-9960x` at [2023-12-08 06:29:32Z](https://conbench.ursa.dev/compare/runs/20a8953749844a15bef49d8d55ceacc6...d495299999e146f2931ebc10caf52c17/)
     - [`file-read` (R) with compression=lz4, dataset=nyctaxi_2010-01, file_type=feather, language=R, output_type=dataframe](https://conbench.ursa.dev/compare/benchmarks/065729e7627b76ae8000bcc789c39808...06572c63d39c772180002d2a124ef1fe)
   
   The [full Conbench report](https://github.com/apache/arrow/runs/19473748542) has more details.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1827034363

   The associated issue mentions `DatasetWriterFileQueue::WriteNext` but this PR mentions `DatasetWriterImpl::DoWriteRecordBatch` not `DatasetWriterFileQueue::WriteNext`. What is the relation of them in this context?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1827047049

   Can we add a test for this case?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou merged PR #38885:
URL: https://github.com/apache/arrow/pull/38885


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1854065168

   `***Failed   64.16 sec`
   
   
   ```
   [ RUN      ] DatasetWriterTestFixture.MaxRowsOneWriteBackpresure
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:115: Failure
   Value of: _fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)
     Actual: false
   Expected: true
   ```
   
   Seems the writer hang when writing the 89th file, and causing the test failed. I need sometime to gothrough the whole codebase of the writer, it's a bit complex because so many async tool is introduced...


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1407194544


##########
cpp/src/arrow/dataset/dataset_writer.cc:
##########
@@ -621,11 +621,18 @@ class DatasetWriter::DatasetWriterImpl {
         backpressure = writer_state_.open_files_throttle.Acquire(1);
         if (!backpressure.is_finished()) {
           EVENT_ON_CURRENT_SPAN("DatasetWriter::Backpressure::TooManyOpenFiles");
+          writer_state_.rows_in_flight_throttle.Release(next_chunk->num_rows());

Review Comment:
   Hmm I think the best way is using something like a "RAII Guard" here. This might need kind of refactor the throttle type here



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1827037364

   Updated the issue


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #38885:
URL: https://github.com/apache/arrow/pull/38885#discussion_r1409540013


##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {
+  // GH-38884: This test is to make sure that the writer can handle
+  //  throttle resources in `WriteRecordBatch`.
+
+  // CalculateMaxRowsStaged will make at least 1 << 23 rows for
+  // `rows_in_flight` throttle. So we set the limit to 1 << 23.
+  constexpr auto FILE_SIZE_LIMIT = static_cast<uint64_t>(1 << 23);
+  write_options_.max_rows_per_file = FILE_SIZE_LIMIT;
+  write_options_.max_rows_per_group = FILE_SIZE_LIMIT;
+  write_options_.max_open_files = 2;
+  write_options_.min_rows_per_group = FILE_SIZE_LIMIT - 1;
+  auto dataset_writer = MakeDatasetWriter();
+  dataset_writer->WriteRecordBatch(MakeBatch(FILE_SIZE_LIMIT * 2), "");
+  dataset_writer->WriteRecordBatch(MakeBatch(FILE_SIZE_LIMIT * 2), "");
+  dataset_writer->WriteRecordBatch(MakeBatch(FILE_SIZE_LIMIT * 2), "");
+  EndWriterChecked(dataset_writer.get());
+  std::vector<ExpectedFile> expected_files;
+  for (int i = 0; i < 6; ++i) {
+    expected_files.emplace_back("testdir/chunk-" + std::to_string(i) + ".arrow",
+                                FILE_SIZE_LIMIT * i, FILE_SIZE_LIMIT);
+  }
+  counter_ = 0;
+  for (const auto& expected_file : expected_files) {
+    std::optional<MockFileInfo> written_file = FindFile(expected_file.filename);
+    AssertFileCreated(written_file, expected_file.filename);
+    int num_batches = 0;
+    AssertBatchesEqual(*MakeBatch(expected_file.start, expected_file.num_rows),
+                       *ReadAsBatch(written_file->data, &num_batches));

Review Comment:
   This is because `AssertCreatedData` will check the record-batch size. However, here it might write batch with size 0.



##########
cpp/src/arrow/dataset/dataset_writer_test.cc:
##########
@@ -277,6 +277,37 @@ TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite) {
                      {"testdir/chunk-3.arrow", 30, 5}});
 }
 
+TEST_F(DatasetWriterTestFixture, MaxRowsOneWrite2) {
+  // GH-38884: This test is to make sure that the writer can handle
+  //  throttle resources in `WriteRecordBatch`.
+
+  // CalculateMaxRowsStaged will make at least 1 << 23 rows for
+  // `rows_in_flight` throttle. So we set the limit to 1 << 23.
+  constexpr auto FILE_SIZE_LIMIT = static_cast<uint64_t>(1 << 23);

Review Comment:
   It's a bit large, hope here are better way for testing



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1837770686

   @kou I've update the code here. And avoid using large batch in testing. I can reproduce hang with same test in `main` branch with RelWithDebInfo mode


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "kou (via GitHub)" <gi...@apache.org>.
kou commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1850820365

   @mapleFU This may be unstable...
   It failed in "Java JNI" CI of #39176: https://github.com/apache/arrow/actions/runs/7170522378/job/19523443755?pr=39176#step:7:3284
   
   ```text
   [ RUN      ] DatasetWriterTestFixture.MaxRowsOneWriteBackpresure
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:115: Failure
   Value of: _fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)
     Actual: false
   Expected: true
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   Expected: (found) != (actual_paths.end()), actual: 8-byte object <88-4C 00-00 E9-7F 00-00> vs 8-byte object <88-4C 00-00 E9-7F 00-00>
   The file testdir/chunk-89.arrow was not in the list of files visited
   Google Test trace:
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:183: pre_finish
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   Expected: (found) != (actual_paths.end()), actual: 8-byte object <F8-34 00-F4 E8-7F 00-00> vs 8-byte object <F8-34 00-F4 E8-7F 00-00>
   The file testdir/chunk-89.arrow was not in the list of files visited
   Google Test trace:
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:187: post_finish
   /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:161: Failure
   Value of: _st.ok()
     Actual: false
   Expected: true
   '_error_or_value27.status()' failed with Invalid: File is too small: 0
   /arrow/cpp/src/arrow/result.cc:28: ValueOrDie called on an error: Invalid: File is too small: 0
   /build/cpp/release/arrow-dataset-dataset-writer-test[0xd7d0e8]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0xd7d43d]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0xe5f462]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0xe6b6e7]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0x5fb7fa]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0x5eeec9]
   /build/cpp/release/libarrow_gtest.so.1.11.0(void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)+0x5f)[0x7fe90eabfb6f]
   /build/cpp/release/libarrow_gtest.so.1.11.0(testing::Test::Run()+0xce)[0x7fe90eab489e]
   /build/cpp/release/libarrow_gtest.so.1.11.0(testing::TestInfo::Run()+0x155)[0x7fe90eab4a15]
   /build/cpp/release/libarrow_gtest.so.1.11.0(testing::TestSuite::Run()+0xd9)[0x7fe90eab4fd9]
   /build/cpp/release/libarrow_gtest.so.1.11.0(testing::internal::UnitTestImpl::RunAllTests()+0x51a)[0x7fe90eab56ba]
   /build/cpp/release/libarrow_gtest.so.1.11.0(bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)+0x5f)[0x7fe90eabffef]
   /build/cpp/release/libarrow_gtest.so.1.11.0(testing::UnitTest::Run()+0x7c)[0x7fe90eab4abc]
   /build/cpp/release/libarrow_gtest_main.so.1.11.0(main+0x3b)[0x7fe90eae70eb]
   /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fe90d6fe555]
   /build/cpp/release/arrow-dataset-dataset-writer-test[0x5d39c7]
   /build/cpp/src/arrow/dataset
   ```
   
   I re-run the job for now.


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] GH-38884: [C++] DatasetWriter release rows_in_flight_throttle when allocate writing failed [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on PR #38885:
URL: https://github.com/apache/arrow/pull/38885#issuecomment-1851903938

   > @mapleFU This may be unstable... It failed in "Java JNI" CI of #39176: https://github.com/apache/arrow/actions/runs/7170522378/job/19523443755?pr=39176#step:7:3284
   > 
   > ```
   > [ RUN      ] DatasetWriterTestFixture.MaxRowsOneWriteBackpresure
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:115: Failure
   > Value of: _fut.Wait(::arrow::kDefaultAssertFinishesWaitSeconds)
   >   Actual: false
   > Expected: true
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   > Expected: (found) != (actual_paths.end()), actual: 8-byte object <88-4C 00-00 E9-7F 00-00> vs 8-byte object <88-4C 00-00 E9-7F 00-00>
   > The file testdir/chunk-89.arrow was not in the list of files visited
   > Google Test trace:
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:183: pre_finish
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:154: Failure
   > Expected: (found) != (actual_paths.end()), actual: 8-byte object <F8-34 00-F4 E8-7F 00-00> vs 8-byte object <F8-34 00-F4 E8-7F 00-00>
   > The file testdir/chunk-89.arrow was not in the list of files visited
   > Google Test trace:
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:187: post_finish
   > /arrow/cpp/src/arrow/dataset/dataset_writer_test.cc:161: Failure
   > Value of: _st.ok()
   >   Actual: false
   > Expected: true
   > '_error_or_value27.status()' failed with Invalid: File is too small: 0
   > /arrow/cpp/src/arrow/result.cc:28: ValueOrDie called on an error: Invalid: File is too small: 0
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0xd7d0e8]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0xd7d43d]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0xe5f462]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0xe6b6e7]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0x5fb7fa]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0x5eeec9]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)+0x5f)[0x7fe90eabfb6f]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(testing::Test::Run()+0xce)[0x7fe90eab489e]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(testing::TestInfo::Run()+0x155)[0x7fe90eab4a15]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(testing::TestSuite::Run()+0xd9)[0x7fe90eab4fd9]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(testing::internal::UnitTestImpl::RunAllTests()+0x51a)[0x7fe90eab56ba]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)+0x5f)[0x7fe90eabffef]
   > /build/cpp/release/libarrow_gtest.so.1.11.0(testing::UnitTest::Run()+0x7c)[0x7fe90eab4abc]
   > /build/cpp/release/libarrow_gtest_main.so.1.11.0(main+0x3b)[0x7fe90eae70eb]
   > /lib64/libc.so.6(__libc_start_main+0xf5)[0x7fe90d6fe555]
   > /build/cpp/release/arrow-dataset-dataset-writer-test[0x5d39c7]
   > /build/cpp/src/arrow/dataset
   > ```
   > 
   > I re-run the job for now.
   
   I'll run in my local machine and find out why


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org