From 34ef7ac003b1f44fd1a1a1db052b0905b9a4a77c Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 11 Jun 2015 17:42:48 +0300 Subject: [PATCH] Implement request coalescing and synchronous request cancellation When requests are made to the FileSource, we now group them by resource, similarly to what is done on the native implementation. The file source will reuse the same Response for all the requests on the observer list. The NodeRequest no longer holds a reference to the mbgl::Request, so we can cancel it synchronously. This is needed to avoid a deadlock when releasing the NodeMap resources with pending requests. Before: - Main thread | NodeMap::release() - Main thread | mbgl::~Map() - Main thread | Blocks waiting for the Map thread to join - Map thread | mbgl::Request::cancel() - Map thread | NodeFileSource posts the "cancel" message to main thread - *deadlock* | Map thread cannot join because mbgl::Request holds a reference to the Map thread main loop. Main thread is blocked, never gets the "cancel" message. Now: - Main thread | NodeMap::release() - Main thread | mbgl::~Map() - Main thread | Blocks waiting for the Map thread to join - Map thread | Request::cancel() - Map thread | NodeFileSource::cancel(), destroys the mbgl::Request synchronously, mutex guarded access. We also send a notification to cancel the NodeRequest on the main thread. - Map thread | Map thread joins. - Main thread | NodeMap::release() returns - Main thread | NodeRequest gets canceled. If NodeRequest replies before it gets the "cancel" message, nothing happens, notification gets discarded. --- src/node_file_source.cpp | 84 ++++++++++++++++++++++++++++++---------- src/node_file_source.hpp | 25 +++++++++--- src/node_request.cpp | 29 +++++++------- src/node_request.hpp | 11 ++++-- 4 files changed, 104 insertions(+), 45 deletions(-) diff --git a/src/node_file_source.cpp b/src/node_file_source.cpp index 6f4a05bca..935bac427 100644 --- a/src/node_file_source.cpp +++ b/src/node_file_source.cpp @@ -4,6 +4,8 @@ #include +#include + namespace node_mbgl { //////////////////////////////////////////////////////////////////////////////////////////////// @@ -42,15 +44,15 @@ NAN_METHOD(NodeFileSource::New) { struct NodeFileSource::Action { const enum : bool { Add, Cancel } type; - mbgl::Request *const request; + mbgl::Resource const resource; }; NodeFileSource::NodeFileSource() : queue(new Queue(uv_default_loop(), [this](Action &action) { if (action.type == Action::Add) { - processAdd(action.request); + processAdd(action.resource); } else if (action.type == Action::Cancel) { - processCancel(action.request); + processCancel(action.resource); } })) { @@ -66,21 +68,50 @@ NodeFileSource::~NodeFileSource() { mbgl::Request* NodeFileSource::request(const mbgl::Resource& resource, uv_loop_t* loop, Callback callback) { auto req = new mbgl::Request(resource, loop, std::move(callback)); - // This function can be called from any thread. Make sure we're executing the actual call in the - // file source loop by sending it over the queue. It will be processed in processAction(). - queue->send(Action{ Action::Add, req }); + std::lock_guard lock(observersMutex); + auto it = observers.find(resource); + if (it == observers.end()) { + observers[resource] = { req }; + + // This function can be called from any thread. Make sure we're executing the actual call in the + // file source loop by sending it over the queue. It will be processed in processAction(). + queue->send(Action{ Action::Add, resource }); + } else { + it->second.emplace_back(req); + } + return req; } void NodeFileSource::cancel(mbgl::Request* req) { req->cancel(); - // This function can be called from any thread. Make sure we're executing the actual call in the - // file source loop by sending it over the queue. It will be processed in processAction(). - queue->send(Action{ Action::Cancel, req }); + std::lock_guard lock(observersMutex); + + auto observersIter = observers.find(req->resource); + if (observersIter == observers.end()) { + return; + } + + auto& observersList = observersIter->second; + auto observersListIter = std::find(observersList.begin(), observersList.end(), req); + if (observersListIter == observersList.end()) { + return; + } + + observersList.erase(observersListIter); + if (observersList.empty()) { + observers.erase(observersIter); + + // This function can be called from any thread. Make sure we're executing the actual call in the + // file source loop by sending it over the queue. It will be processed in processAction(). + queue->send(Action{ Action::Cancel, req->resource }); + } + + req->destruct(); } -void NodeFileSource::processAdd(mbgl::Request *req) { +void NodeFileSource::processAdd(const mbgl::Resource& resource) { NanScope(); // Make sure the loop stays alive as long as request is pending. @@ -89,23 +120,23 @@ void NodeFileSource::processAdd(mbgl::Request *req) { } auto handle = NanObjectWrapHandle(this); - auto requestHandle = NanNew(NodeRequest::Create(handle, req)); + auto requestHandle = NanNew(NodeRequest::Create(handle, resource)); #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) const v8::UniquePersistent requestPersistent(v8::Isolate::GetCurrent(), requestHandle); #else v8::Persistent requestPersistent; NanAssignPersistent(requestPersistent, requestHandle); #endif - pending.emplace(req, std::move(requestPersistent)); + pending.emplace(resource, std::move(requestPersistent)); v8::Local argv[] = { requestHandle }; NanMakeCallback(handle, NanNew("request"), 1, argv); } -void NodeFileSource::processCancel(mbgl::Request *req) { +void NodeFileSource::processCancel(const mbgl::Resource& resource) { NanScope(); - auto it = pending.find(req); + auto it = pending.find(resource); if (it == pending.end()) { // The response callback was already fired. There is no point in calling the cancelation // callback because the request is already completed. @@ -136,14 +167,11 @@ void NodeFileSource::processCancel(mbgl::Request *req) { // Set the request handle in the request wrapper handle to null ObjectWrap::Unwrap(requestHandle)->cancel(); } - - // Finally, destruct the request object - req->destruct(); } -void NodeFileSource::notify(mbgl::Request *req, const std::shared_ptr& response) { +void NodeFileSource::notify(const mbgl::Resource& resource, const std::shared_ptr& response) { // First, remove the request, since it might be destructed at any point now. - auto it = pending.find(req); + auto it = pending.find(resource); if (it != pending.end()) { #if (NODE_MODULE_VERSION <= NODE_0_10_MODULE_VERSION) NanDisposePersistent(it->second); @@ -156,7 +184,23 @@ void NodeFileSource::notify(mbgl::Request *req, const std::shared_ptrnotify(response); + std::vector observersList; + + { + std::lock_guard lock(observersMutex); + + auto observersIter = observers.find(resource); + if (observersIter == observers.end()) { + return; + } + + observersList.swap(observersIter->second); + observers.erase(observersIter); + } + + for (auto request : observersList) { + request->notify(response); + } } } diff --git a/src/node_file_source.hpp b/src/node_file_source.hpp index e69a8fcf1..5220cd235 100644 --- a/src/node_file_source.hpp +++ b/src/node_file_source.hpp @@ -10,8 +10,10 @@ #include #pragma GCC diagnostic pop -#include #include +#include +#include +#include namespace node_mbgl { @@ -36,21 +38,32 @@ public: void cancel(mbgl::Request*); // visiblity? - void notify(mbgl::Request*, const std::shared_ptr&); + void notify(const mbgl::Resource&, const std::shared_ptr&); private: struct Action; using Queue = util::AsyncQueue; - void processAdd(mbgl::Request*); - void processCancel(mbgl::Request*); + void processAdd(const mbgl::Resource&); + void processCancel(const mbgl::Resource&); private: #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) - std::map &> pending; + std::unordered_map &, mbgl::Resource::Hash> pending; #else - std::map> pending; + std::unordered_map, mbgl::Resource::Hash> pending; #endif + + // The observers list will hold pointers to all the requests waiting + // for a particular resource. It is also used for coalescing requests, + // so we don't ask for the same resources twice. The access must be + // guarded by a mutex because the list is also accessed by a thread + // from the mbgl::Map object and from the main thread when notifying + // requests of completion. Concurrent access is specially needed when + // canceling a request to avoid a deadlock (see #129). + std::unordered_map, mbgl::Resource::Hash> observers; + std::mutex observersMutex; + Queue *queue = nullptr; }; diff --git a/src/node_request.cpp b/src/node_request.cpp index ccac813d2..e01162ff5 100644 --- a/src/node_request.cpp +++ b/src/node_request.cpp @@ -35,35 +35,34 @@ NAN_METHOD(NodeRequest::New) { return NanThrowTypeError("Cannot create Request objects explicitly"); } - auto request = reinterpret_cast(args[1].As()->Value()); - auto req = new NodeRequest(args[0]->ToObject(), request); + auto resource = reinterpret_cast(args[1].As()->Value()); + auto req = new NodeRequest(args[0]->ToObject(), *resource); req->Wrap(args.This()); NanReturnValue(args.This()); } -v8::Handle NodeRequest::Create(v8::Handle source, mbgl::Request *request) { +v8::Handle NodeRequest::Create(v8::Handle source, const mbgl::Resource& resource) { NanEscapableScope(); - v8::Local argv[] = { NanNew(source), NanNew(request) }; + v8::Local argv[] = { NanNew(source), + NanNew(const_cast(&resource)) }; auto instance = NanNew(constructorTemplate)->GetFunction()->NewInstance(2, argv); - instance->ForceSet(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly); - instance->ForceSet(NanNew("kind"), NanNew(int(request->resource.kind)), v8::ReadOnly); + instance->ForceSet(NanNew("url"), NanNew(resource.url), v8::ReadOnly); + instance->ForceSet(NanNew("kind"), NanNew(int(resource.kind)), v8::ReadOnly); return NanEscapeScope(instance); } NAN_METHOD(NodeRequest::Respond) { auto nodeRequest = ObjectWrap::Unwrap(args.Holder()); - if (!nodeRequest->request) { + if (!nodeRequest->resource) { return NanThrowError("Request has already been responded to, or was canceled."); } auto source = ObjectWrap::Unwrap(NanNew(nodeRequest->source)); - - auto request = nodeRequest->request; - nodeRequest->request = nullptr; + auto resource = std::move(nodeRequest->resource); if (args.Length() < 1) { return NanThrowTypeError("First argument must be an error object"); @@ -76,7 +75,7 @@ NAN_METHOD(NodeRequest::Respond) { const NanUtf8String message { args[0]->ToString() }; response->message = std::string { *message, size_t(message.length()) }; - source->notify(request, response); + source->notify(*resource, response); } else if (args.Length() < 2 || !args[1]->IsObject()) { return NanThrowTypeError("Second argument must be a response object"); } else { @@ -120,7 +119,7 @@ NAN_METHOD(NodeRequest::Respond) { } // Send the response object to the NodeFileSource object - source->notify(request, response); + source->notify(*resource, response); } NanReturnUndefined(); @@ -129,8 +128,8 @@ NAN_METHOD(NodeRequest::Respond) { //////////////////////////////////////////////////////////////////////////////////////////////// // Instance -NodeRequest::NodeRequest(v8::Local source_, mbgl::Request *request_) - : request(request_) { +NodeRequest::NodeRequest(v8::Local source_, const mbgl::Resource& resource_) + : resource(std::make_unique(resource_)) { NanAssignPersistent(source, source_); } @@ -139,7 +138,7 @@ NodeRequest::~NodeRequest() { } void NodeRequest::cancel() { - request = nullptr; + resource.reset(); } } diff --git a/src/node_request.hpp b/src/node_request.hpp index 009855066..0fbf91fc6 100644 --- a/src/node_request.hpp +++ b/src/node_request.hpp @@ -7,8 +7,11 @@ #include #pragma GCC diagnostic pop +#include + +#include + class NodeFileSource; -namespace mbgl { class Request; } namespace node_mbgl { @@ -20,21 +23,21 @@ public: static NAN_METHOD(New); static NAN_METHOD(Respond); - static v8::Handle Create(v8::Handle source, mbgl::Request *request); + static v8::Handle Create(v8::Handle source, const mbgl::Resource& resource); static v8::Persistent constructorTemplate; //////////////////////////////////////////////////////////////////////////////////////////////// // Instance public: - NodeRequest(v8::Local source, mbgl::Request *request); + NodeRequest(v8::Local source, const mbgl::Resource& resource); ~NodeRequest(); void cancel(); private: v8::Persistent source; - mbgl::Request *request = nullptr; + std::unique_ptr resource; }; }