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.
This commit is contained in:
Thiago Marcos P. Santos
2015-06-11 17:42:48 +03:00
parent d515f8d77d
commit 34ef7ac003
4 changed files with 104 additions and 45 deletions

View File

@@ -4,6 +4,8 @@
#include <mbgl/storage/request.hpp>
#include <algorithm>
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<std::mutex> 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<std::mutex> 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<v8::Object>(NodeRequest::Create(handle, req));
auto requestHandle = NanNew<v8::Object>(NodeRequest::Create(handle, resource));
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
const v8::UniquePersistent<v8::Object> requestPersistent(v8::Isolate::GetCurrent(), requestHandle);
#else
v8::Persistent<v8::Object> requestPersistent;
NanAssignPersistent(requestPersistent, requestHandle);
#endif
pending.emplace(req, std::move(requestPersistent));
pending.emplace(resource, std::move(requestPersistent));
v8::Local<v8::Value> 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<NodeRequest>(requestHandle)->cancel();
}
// Finally, destruct the request object
req->destruct();
}
void NodeFileSource::notify(mbgl::Request *req, const std::shared_ptr<const mbgl::Response>& response) {
void NodeFileSource::notify(const mbgl::Resource& resource, const std::shared_ptr<const mbgl::Response>& 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_ptr<const mbgl
}
}
req->notify(response);
std::vector<mbgl::Request*> observersList;
{
std::lock_guard<std::mutex> 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);
}
}
}

View File

@@ -10,8 +10,10 @@
#include <nan.h>
#pragma GCC diagnostic pop
#include <map>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>
namespace node_mbgl {
@@ -36,21 +38,32 @@ public:
void cancel(mbgl::Request*);
// visiblity?
void notify(mbgl::Request*, const std::shared_ptr<const mbgl::Response>&);
void notify(const mbgl::Resource&, const std::shared_ptr<const mbgl::Response>&);
private:
struct Action;
using Queue = util::AsyncQueue<Action>;
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<mbgl::Request*, const v8::UniquePersistent<v8::Object> &> pending;
std::unordered_map<mbgl::Resource, const v8::UniquePersistent<v8::Object> &, mbgl::Resource::Hash> pending;
#else
std::map<mbgl::Request*, v8::Persistent<v8::Object>> pending;
std::unordered_map<mbgl::Resource, v8::Persistent<v8::Object>, 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, std::vector<mbgl::Request*>, mbgl::Resource::Hash> observers;
std::mutex observersMutex;
Queue *queue = nullptr;
};

View File

@@ -35,35 +35,34 @@ NAN_METHOD(NodeRequest::New) {
return NanThrowTypeError("Cannot create Request objects explicitly");
}
auto request = reinterpret_cast<mbgl::Request *>(args[1].As<v8::External>()->Value());
auto req = new NodeRequest(args[0]->ToObject(), request);
auto resource = reinterpret_cast<mbgl::Resource*>(args[1].As<v8::External>()->Value());
auto req = new NodeRequest(args[0]->ToObject(), *resource);
req->Wrap(args.This());
NanReturnValue(args.This());
}
v8::Handle<v8::Object> NodeRequest::Create(v8::Handle<v8::Object> source, mbgl::Request *request) {
v8::Handle<v8::Object> NodeRequest::Create(v8::Handle<v8::Object> source, const mbgl::Resource& resource) {
NanEscapableScope();
v8::Local<v8::Value> argv[] = { NanNew<v8::Object>(source), NanNew<v8::External>(request) };
v8::Local<v8::Value> argv[] = { NanNew<v8::Object>(source),
NanNew<v8::External>(const_cast<mbgl::Resource*>(&resource)) };
auto instance = NanNew<v8::FunctionTemplate>(constructorTemplate)->GetFunction()->NewInstance(2, argv);
instance->ForceSet(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly);
instance->ForceSet(NanNew("kind"), NanNew<v8::Integer>(int(request->resource.kind)), v8::ReadOnly);
instance->ForceSet(NanNew("url"), NanNew(resource.url), v8::ReadOnly);
instance->ForceSet(NanNew("kind"), NanNew<v8::Integer>(int(resource.kind)), v8::ReadOnly);
return NanEscapeScope(instance);
}
NAN_METHOD(NodeRequest::Respond) {
auto nodeRequest = ObjectWrap::Unwrap<NodeRequest>(args.Holder());
if (!nodeRequest->request) {
if (!nodeRequest->resource) {
return NanThrowError("Request has already been responded to, or was canceled.");
}
auto source = ObjectWrap::Unwrap<NodeFileSource>(NanNew<v8::Object>(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<v8::Object> source_, mbgl::Request *request_)
: request(request_) {
NodeRequest::NodeRequest(v8::Local<v8::Object> source_, const mbgl::Resource& resource_)
: resource(std::make_unique<mbgl::Resource>(resource_)) {
NanAssignPersistent(source, source_);
}
@@ -139,7 +138,7 @@ NodeRequest::~NodeRequest() {
}
void NodeRequest::cancel() {
request = nullptr;
resource.reset();
}
}

View File

@@ -7,8 +7,11 @@
#include <nan.h>
#pragma GCC diagnostic pop
#include <mbgl/storage/resource.hpp>
#include <memory>
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<v8::Object> Create(v8::Handle<v8::Object> source, mbgl::Request *request);
static v8::Handle<v8::Object> Create(v8::Handle<v8::Object> source, const mbgl::Resource& resource);
static v8::Persistent<v8::FunctionTemplate> constructorTemplate;
////////////////////////////////////////////////////////////////////////////////////////////////
// Instance
public:
NodeRequest(v8::Local<v8::Object> source, mbgl::Request *request);
NodeRequest(v8::Local<v8::Object> source, const mbgl::Resource& resource);
~NodeRequest();
void cancel();
private:
v8::Persistent<v8::Object> source;
mbgl::Request *request = nullptr;
std::unique_ptr<mbgl::Resource> resource;
};
}