lidavidm commented on a change in pull request #10968: URL: https://github.com/apache/arrow/pull/10968#discussion_r692938917
########## File path: cpp/src/arrow/util/async_nursery.cc ########## @@ -0,0 +1,127 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/util/async_nursery.h" + +#include "arrow/util/logging.h" + +namespace arrow { +namespace util { + +AsyncCloseable::AsyncCloseable(AsyncCloseable* parent) : on_closed_(Future<>::Make()) { + if (parent) { + Mutex::Guard guard = mutex_.Lock(); + parent->children_.push_back(this); + self_itr_ = --parent->children_.end(); + } +} + +AsyncCloseable::~AsyncCloseable() { DCHECK(close_complete_.load()); } + +Future<> AsyncCloseable::OnClosed() { return on_closed_; } + +Future<> AsyncCloseable::Close() { + { + Mutex::Guard guard = mutex_.Lock(); + if (closed_.load()) { + return Future<>::MakeFinished(); + } + closed_.store(true); + } + return DoClose() + .Then([this] { + close_complete_.store(true); + on_closed_.MarkFinished(); + return CloseChildren(); + }) + .Then([] {}, + [this](const Status& err) { + close_complete_.store(true); + on_closed_.MarkFinished(err); + return err; + }); +} + +Future<> AsyncCloseable::CloseChildren() { + for (auto& child : children_) { + tasks_.push_back(child->Close()); + } + return AllComplete(tasks_); +} + +Status AsyncCloseable::CheckClosed() const { + if (closed_.load()) { + return Status::Invalid("Invalid operation after Close"); + } + return Status::OK(); +} + +void AsyncCloseable::AssertNotCloseComplete() const { DCHECK(!close_complete_); } + +void AsyncCloseable::AddDependentTask(Future<> task) { + tasks_.push_back(std::move(task)); +} + +OwnedAsyncCloseable::OwnedAsyncCloseable(AsyncCloseable* parent) + : AsyncCloseable(parent) { + parent_ = parent; +} + +void OwnedAsyncCloseable::Init() { + Mutex::Guard lock = parent_->mutex_.Lock(); + parent_->owned_children_.push_back(shared_from_this()); + owned_self_itr_ = --parent_->owned_children_.end(); +} + +void OwnedAsyncCloseable::Evict() { + { + Mutex::Guard lock = parent_->mutex_.Lock(); + if (parent_->closed_) { + // Parent is already closing, no need to do anything, the parent will call close on + // this instance eventually + return; + } + parent_->children_.erase(self_itr_); + } + // We need to add a dependent task to make sure our parent does not close itself Review comment: Isn't it possible for Close() to get called between this block and AddDependentTask? ########## File path: cpp/src/arrow/util/async_nursery.h ########## @@ -0,0 +1,144 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#ifndef ARROW_ASYNC_NURSERY_H +#define ARROW_ASYNC_NURSERY_H + +#include <list> + +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/util/future.h" +#include "arrow/util/mutex.h" + +namespace arrow { +namespace util { + +class OwnedAsyncCloseable; + +/// An object which should be asynchronously closed before it is destroyed +/// +/// Any AsyncCloseable must be kept alive until its parent is destroyed (this is a given +/// if the parent is a nursery). For shorter lived tasks/objects consider +/// OwnedAsyncCloseable adding a dependent task. +class AsyncCloseable { + public: + /// \brief Construct an AsyncCloseable as a child of `parent` + explicit AsyncCloseable(AsyncCloseable* parent); + virtual ~AsyncCloseable(); + + /// Returns a future that is completed when this object is finished closing + Future<> OnClosed(); + + protected: + /// Closes the AsyncCloseable + /// This will first call DoClose and then simultaneously close all children and + /// tasks_ + virtual Future<> Close(); + /// Subclasses should override this and perform any cleanup that is not captured by + /// tasks_. Once the future returned by this method finishes then this object is + /// eligible for destruction and any reference to `this` may be invalid + virtual Future<> DoClose() = 0; + + /// This method is called by subclasses to add tasks which must complete before the + /// object can be safely deleted + void AddDependentTask(Future<> task); + /// This method can be called by subclasses for error checking purposes. It will + /// return an invalid status if this object has started closing + Status CheckClosed() const; + /// This can be used for sanity checking that a callback is not run after close has + /// been finished. It will assert if the object has been fully closed (and `this` + /// references are unsafe) + void AssertNotCloseComplete() const; + + private: + Future<> CloseChildren(); + + std::list<AsyncCloseable*> children_; + std::list<AsyncCloseable*>::iterator self_itr_; + std::list<std::shared_ptr<AsyncCloseable>> owned_children_; + std::vector<Future<>> tasks_; + Future<> on_closed_; + std::atomic<bool> closed_{false}; + std::atomic<bool> close_complete_{false}; + util::Mutex mutex_; + + friend OwnedAsyncCloseable; +}; + +/// An override of AsyncCloseable which is eligible for eviction. Instances must be Review comment: eviction == may be destroyed before the parent? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
