Commit 20eea8f0 authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Revert Folly NestedRequestContextGuard

Summary:
This attempted to provide a deep copy guard for folly request context.

Per brainstorming (see task), we don't think that deep copying all metadata has legitimate use cases. And in fact, this has no usage in our codebase currently. Because it default to reset (when createChild is not overriden), the behaviour is also very confusing.

Guards's goal is usually to copy 1 metadata. The solution for that is to provide a shallow copy guard. Something that I will do on top of this revert.
I'll wait for both changes to be discussed and accepted before landing this one.

Reviewed By: yfeldblum, LeeHowes

Differential Revision: D8906912

fbshipit-source-id: e8b9eed04cbe539009037ff75d51e28941502d88
parent dec334ce
......@@ -104,19 +104,6 @@ void RequestContext::onUnset() {
}
}
std::shared_ptr<RequestContext> RequestContext::createChild() {
auto child = std::make_shared<RequestContext>();
auto rlock = state_.rlock();
for (const auto& entry : rlock->requestData_) {
auto& key = entry.first;
auto childData = entry.second->createChild();
if (childData) {
child->setContextData(key, std::move(childData));
}
}
return child;
}
void RequestContext::clearContextData(const std::string& val) {
std::unique_ptr<RequestData> requestData;
// Delete the RequestData after giving up the wlock just in case one of the
......
......@@ -45,12 +45,6 @@ class RequestData {
// instance overrides the hasCallback method to return true otherwise
// the callback will not be executed
virtual void onUnset() {}
// Create a child RequestData of this one, or return nullptr (in which case
// this RequestData will not exist in the child RequestContext)
virtual std::unique_ptr<RequestData> createChild() {
return nullptr;
}
};
// If you do not call create() to create a unique request context,
......@@ -104,8 +98,6 @@ class RequestContext {
void onSet();
void onUnset();
std::shared_ptr<RequestContext> createChild();
// The following API is used to pass the context through queues / threads.
// saveContext is called to get a shared_ptr to the context, and
// setContext is used to reset it on the other side of the queue.
......@@ -163,24 +155,4 @@ class RequestContextScopeGuard {
RequestContext::setContext(std::move(prev_));
}
};
class RootRequestContextGuard : public RequestContextScopeGuard {
public:
RootRequestContextGuard() : RequestContextScopeGuard() {}
};
class NestedRequestContextGuard : public RequestContextScopeGuard {
public:
NestedRequestContextGuard() : RequestContextScopeGuard(createNested()) {}
private:
static std::shared_ptr<RequestContext> createNested() {
RequestContext* curr = RequestContext::get();
if (curr) {
return curr->createChild();
}
return std::make_shared<RequestContext>();
}
};
} // namespace folly
......@@ -206,48 +206,3 @@ TEST(RequestContext, deadlockTest) {
"test", std::make_unique<DeadlockTestData>("test2"));
RequestContext::get()->clearContextData("test");
}
TEST(RequestContext, Nested) {
class NestableRequestData : public RequestData {
public:
explicit NestableRequestData() {}
explicit NestableRequestData(int val) : val_(val) {}
bool hasCallback() override {
return false;
}
std::unique_ptr<RequestData> createChild() override {
return std::make_unique<NestableRequestData>(val_ + 1);
}
int val_{0};
static NestableRequestData* get() {
return static_cast<NestableRequestData*>(
RequestContext::get()->getContextData("nestable"));
}
};
RootRequestContextGuard root;
RequestContext::get()->setContextData(
"nestable", make_unique<NestableRequestData>());
RequestContext::get()->setContextData(
"unnestable", make_unique<TestData>(42));
EXPECT_EQ(NestableRequestData::get()->val_, 0);
EXPECT_NE(RequestContext::get()->getContextData("unnestable"), nullptr);
EXPECT_NE(NestableRequestData::get(), nullptr);
{
NestedRequestContextGuard nested1;
EXPECT_EQ(NestableRequestData::get()->val_, 1);
EXPECT_EQ(RequestContext::get()->getContextData("unnestable"), nullptr);
NestedRequestContextGuard nested2;
EXPECT_EQ(NestableRequestData::get()->val_, 2);
RootRequestContextGuard newRoot;
EXPECT_EQ(NestableRequestData::get(), nullptr);
}
{
NestedRequestContextGuard nested1;
EXPECT_EQ(NestableRequestData::get()->val_, 1);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment