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 2024/04/16 13:42:40 UTC

[PR] GH-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   <!--
   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
   
   Support GCS Async Close
   
   <!--
    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.  
   -->
   
   ### 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.
   -->
   
   change gcs close to be executed in a async task
   
   ### Are these changes tested?
   
   Yes
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. 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)?
   -->
   
   ### Are there any user-facing changes?
   
   Maybe change blocking to async
   
   <!--
   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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());

Review Comment:
   Can't you do this without the cast here? In the body of the lambda you can cast the `self.get()` pointer to `GcsOutputStream` to de-virtualize the call.



##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   This class is not thread-safe, so the fact that the current thread is handing a shared_ptr to another thread can only be safe if the caller of this function forgets about all the shared pointers to this class after this call.
   
   I wonder how many potential bugs we could find by adding the following WARNING:
   
   ```cpp
    Future<> CloseAsync() override {
      auto self = shared_from_this();
      if (self.use_count() > 2) {
        ARROW_LOG(WARNING) << "Too many strong references to GcsOutputStream before CloseAsync() call";
      }
      if (closed_) return Status::OK();
      ...
   ```
   
   In other words, the caller of `CloseAsync` should have unique ownership of `CloseAsync` so it can give the IO thread unique ownership for the `Close` call.
   
   Without this, every use of the class after `CloseAsync` call is a data race. Thankfully, the shared_ptr destructor checks an atomic ref count, so there is not risk of double destructor calls.



##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -233,6 +244,7 @@ class GcsOutputStream : public arrow::io::OutputStream {
 
  private:
   gcs::ObjectWriteStream stream_;
+  const io::IOContext io_context_;

Review Comment:
   I don't know if this matters now, but since the context might have indirect references to `stream_`, it's better to put it before.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   > Oh, another problem here is bool closed() is weird after this patch
   
   @mapleFU that weirdness is related to what I was referring to in the thread above. What `closed_` means after you called `CloseAsync`? It's not `false` (immediately) and even if the I/O thread sets it to `true` in the background, there is no guarantee the thread that called `CloseAsync` **can see** that mutation made by the other thread that called `Close`.
   
   I took a look at the history of `CloseAsync`. This implementation was added to fix hanging of the dataset loads.
   
   https://github.com/apache/arrow/blob/73386cb806a429875f1846b69e21beccfd339b21/cpp/src/arrow/io/interfaces.cc#L70-L73
   
   It's called from `Future<> FileWriter::Finish`
   
   https://github.com/apache/arrow/blob/73386cb806a429875f1846b69e21beccfd339b21/cpp/src/arrow/dataset/file_base.cc#L355-L360
   
   Note that `this` is captured by pointer in `CloseAsync` and relies on the caller keeping the shared_ptr alive until the `Future<>` completes.
   
   To fix this kind of problem, you added `enabled_shared_from_this` on `FileInterface`
   
   https://github.com/apache/arrow/blob/73386cb806a429875f1846b69e21beccfd339b21/cpp/src/arrow/io/interfaces.h#L99
   
   A complement to that change to avoid potential user-after-free (UAF) in `FileInterface::CloseAsync`
   
   ```diff
   -   return DeferNotOk( 
   -       default_io_context().executor()->Submit([this]() { return Close(); })); 
   +   return DeferNotOk(default_io_context().executor()->Submit(
   +       [self = shared_from_this()]() { return self->Close(); }));
   ```
   
   Back to the data-race on `this->closed_` described above. I don't think it's common that after calling `CloseAsync`, the handle is used, but this would give us at least a runtime hint that we're doing something wrong (could be made debug-only).
   
   ```diff
     Future<> FileInterface::CloseAsync() {
   +   // To avoid data races, after CloseAsync is called, this instance should not
   +   // be used anymore. File handle implementations are not thread-safe -- they
   +   // are supposed to be used from a single thread. To catch *potential* misuse,
   +   // we log a WARNING if the caller still holds more than this reference to the file.
   +   auto use_count = weak_from_this().use_count();
   +   if (use_count > 1) {
   +     ARROW_LOG(WARNING) << "All shared_ptr references to the file should be "
   +                           "released before calling CloseAsync. At least "
   +                        << use_count - 1 << " references are still alive.";
   +   }
       return DeferNotOk(default_io_context().executor()->Submit(
           [self = shared_from_this()]() { return self->Close(); }));
     }
   ```
   
   @bkietz, what you think?
   
   


-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   I've check the s3fs, maybe it also doesn't prevent from `FinishPartUploadAfterFlush()` being called multiple times. It's possible to make gcs safe by holding a future here.
   
   For gcs, maybe the code below is thread safe
   
   ```
     Status Close() override {
       if (closed_) {
         return Status::OK();
       }
       return CloseAsync().status();
     }
   
     Future<> CloseAsync() override {
       if (closed_) return Status::OK();
       if (this->close_future_.is_valid()) {
         return close_future_;
       }
       auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
       auto deferred = [self = std::move(self)]() -> Status {
         self->stream_.Close();
         self->closed_ = true;
         return internal::ToArrowStatus(self->stream_.last_status());
       };
       ARROW_ASSIGN_OR_RAISE(close_future_,
                             io::internal::SubmitIO(io_context_, std::move(deferred)));
       return close_future_;
     }
   ```



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   I've update the logic to use `future_` as a synchronize tool. This still doesn't prevent from concurrent Close or CloseAsync, however, this can avoid multiple Close called
   
   And considering https://github.com/apache/arrow/pull/41232#issuecomment-2061176540 . Maybe we can close this issue if not neccessary


-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   Oh, another problem here is `bool closed()` is weird after this patch



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   I don't think you should complicate the code before concrete bug reports, hence my suggestion for the WARNING as a way to catch potential misuse.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   > Oh, the FileInterface has already implement `CloseAsync` in this manner( but in default io thread pool), maybe I should rethink the syntax here
   
   Why are you doing this since it already exists?
   


-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   (I made a mistake here, previously I found it doesn't implement native closeAsync, maybe I should close this first)


-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -233,6 +244,7 @@ class GcsOutputStream : public arrow::io::OutputStream {
 
  private:
   gcs::ObjectWriteStream stream_;
+  const io::IOContext io_context_;

Review Comment:
   > since the context might have indirect references to `stream_`
   
   When would this happens?



##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -233,6 +244,7 @@ class GcsOutputStream : public arrow::io::OutputStream {
 
  private:
   gcs::ObjectWriteStream stream_;
+  const io::IOContext io_context_;

Review Comment:
   > since the context might have indirect references to `stream_`
   
   When would this happens?



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -233,6 +244,7 @@ class GcsOutputStream : public arrow::io::OutputStream {
 
  private:
   gcs::ObjectWriteStream stream_;
+  const io::IOContext io_context_;

Review Comment:
   It's a weak reference, not an owning reference. The goal is to not destroy the `stream_` before the context is destroyed.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   In even more words: safe use of this class relies on it being used by only one thread at once.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   Seems close would call this: https://github.com/googleapis/google-cloud-cpp/blob/230f9f929ce85e0dfb045d5f1ee0df74cedbd204/google/cloud/storage/object_write_stream.cc#L85
   
   Which is worth doing this



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   🤔 A called flag or a `Future<>` can be ok here. If `CloseAsync/Close` not being called after current `CloseAsync` it could be safe. Checking `shared_from_this()` is a bit hacking to be, think about something like Compressed Output Stream, which capture a stream as reference.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -233,6 +244,7 @@ class GcsOutputStream : public arrow::io::OutputStream {
 
  private:
   gcs::ObjectWriteStream stream_;
+  const io::IOContext io_context_;

Review Comment:
   > since the context might have indirect references to `stream_`
   
   Would the reference dtor should before this class dtor? (Since close would called)



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU closed pull request #41232: GH-41229: [C++] FS: Support naive GCS Async Close
URL: https://github.com/apache/arrow/pull/41232


-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   I've check the s3fs, maybe it also doesn't prevent from `FinishPartUploadAfterFlush()` being called multiple times. It's possible to make gcs safe by holding a future here.
   
   For gcs, maybe the code below is thread safe if we ensure `Close/CloseAsync` wouldn't be called concurrently
   
   ```
     Status Close() override {
       if (closed_) {
         return Status::OK();
       }
       return CloseAsync().status();
     }
   
     Future<> CloseAsync() override {
       if (closed_) return Status::OK();
       if (this->close_future_.is_valid()) {
         return close_future_;
       }
       auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
       auto deferred = [self = std::move(self)]() -> Status {
         self->stream_.Close();
         self->closed_ = true;
         return internal::ToArrowStatus(self->stream_.last_status());
       };
       ARROW_ASSIGN_OR_RAISE(close_future_,
                             io::internal::SubmitIO(io_context_, std::move(deferred)));
       return close_future_;
     }
   ```



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());

Review Comment:
   Can it be a `static_pointer_cast`, then?



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   :warning: GitHub issue #41229 **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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());

Review Comment:
   ```
   class ARROW_EXPORT OutputStream : virtual public FileInterface, public Writable {
    protected:
     OutputStream() = default;
   };
   ```
   
   The `shared_from_this` is in `FileInterface`. I think we cannot.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   > IMO, the CloseAsync() itself cannot be called concurrently or called multiple times. 
   
   It should not but it may be mistakenly called and the `if (closed_)` tests gives the impression that that code checks against that. We're relying on the users' discretion.
   
   ```cpp
     ASSERT_OK_AND_ASSIGN(auto output, fs->OpenOutputStream(path, /*metadata=*/{}));
     ASSERT_OK(output->Write(text.data(), text.size()));
     auto close_future = output->CloseAsync();
     // It's UB to use `output->...` from here.
     // `output->closed_` might be read as `false` by this thread
     // even after the I/O thread set it to `true`.
   ```
   
   



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   🤔 A called flag or a `Future<>` can be ok here. If `CloseAsync/Close` not being called after current `CloseAsync` it could be safe. Checking `shared_from_this()` is a bit hacking to be, think about something like Compressed Output Stream...



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   🤔 A called flag or a `Future<>` can be ok here. If `CloseAsync/Close` not being called after current `CloseAsync` it could be safe. Checking `use_count()` above `shared_from_this()` is a bit hacking to be, think about something like Compressed Output Stream, which capture a stream as reference.



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   ```
     /// \brief Close the stream asynchronously
     ///
     /// By default, this will just submit the synchronous Close() to the
     /// default I/O thread pool. Subclasses may implement this in a more
     /// efficient manner.
     virtual Future<> CloseAsync();
   
   Future<> FileInterface::CloseAsync() {
     return DeferNotOk(
         default_io_context().executor()->Submit([this]() { return Close(); }));
   }
   ```
   
   Oh, the FileInterface has already implement `CloseAsync` in this manner( but in default io thread pool), maybe I should rethink the syntax 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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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

   @pitrou @felipecrv 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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   I've check the s3fs, maybe it also doesn't prevent from `FinishPartUploadAfterFlush()` being called multiple times. It's possible to make gcs safe by holding a future here.
   
   For gcs, maybe the code below is thread safe
   
   ```
     Status Close() override {
       if (closed_) {
         return Status::OK();
       }
       return CloseAsync().status();
     }
   
     Future<> CloseAsync() override {
       if (closed_) return Status::OK();
       if (this->close_future_.is_valid()) {
         return close_future_;
       }
       auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
       auto deferred = [self = std::move(self)]() -> Status {
         self->stream_.Close();
         self->closed_ = true;
         return internal::ToArrowStatus(self->stream_.last_status());
       };
       ARROW_ASSIGN_OR_RAISE(close_future_,
                             io::internal::SubmitIO(io_context_, std::move(deferred)));
       return close_future_;
     }
   ```
   
   Maybe this 



-- 
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-41229: [C++] FS: Support naive GCS Async Close [arrow]

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


##########
cpp/src/arrow/filesystem/gcsfs.cc:
##########
@@ -200,6 +201,16 @@ class GcsOutputStream : public arrow::io::OutputStream {
     return internal::ToArrowStatus(stream_.last_status());
   }
 
+  Future<> CloseAsync() override {
+    if (closed_) return Status::OK();
+
+    auto self = std::dynamic_pointer_cast<GcsOutputStream>(shared_from_this());
+    auto deferred = [self = std::move(self)]() -> Status { return self->Close(); };

Review Comment:
   > This class is not thread-safe
   
   IMO, the `CloseAsync()` itself cannot be called concurrently or called multiple times. And `Close` will not called by dtor if the Defered function is still alive. If we concern about the `CloseAsync`  can add a `Future<>` and checks it during `Close` and `CloseAsync`
   
   



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