1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
* Copyright 2016-present Facebook, Inc.
*
* Licensed 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 <folly/io/async/Request.h>
#include <folly/tracing/StaticTracepoint.h>
#include <glog/logging.h>
#include <folly/MapUtil.h>
#include <folly/SingletonThreadLocal.h>
namespace folly {
void RequestData::DestructPtr::operator()(RequestData* ptr) {
if (ptr) {
auto keepAliveCounter =
ptr->keepAliveCounter_.fetch_sub(1, std::memory_order_acq_rel);
// Note: this is the value before decrement, hence == 1 check
DCHECK(keepAliveCounter > 0);
if (keepAliveCounter == 1) {
delete ptr;
}
}
}
/* static */ RequestData::SharedPtr RequestData::constructPtr(
RequestData* ptr) {
if (ptr) {
auto keepAliveCounter =
ptr->keepAliveCounter_.fetch_add(1, std::memory_order_relaxed);
DCHECK(keepAliveCounter >= 0);
}
return SharedPtr(ptr);
}
bool RequestContext::doSetContextData(
const std::string& val,
std::unique_ptr<RequestData>& data,
DoSetBehaviour behaviour) {
auto ulock = state_.ulock();
bool conflict = false;
auto it = ulock->requestData_.find(val);
if (it != ulock->requestData_.end()) {
if (behaviour == DoSetBehaviour::SET_IF_ABSENT) {
return false;
} else if (behaviour == DoSetBehaviour::SET) {
LOG_FIRST_N(WARNING, 1) << "Calling RequestContext::setContextData for "
<< val << " but it is already set";
}
conflict = true;
}
auto wlock = ulock.moveFromUpgradeToWrite();
if (conflict) {
if (it->second) {
if (it->second->hasCallback()) {
it->second->onUnset();
wlock->callbackData_.erase(it->second.get());
}
it->second.reset(nullptr);
}
if (behaviour == DoSetBehaviour::SET) {
return true;
}
}
if (data && data->hasCallback()) {
wlock->callbackData_.insert(data.get());
data->onSet();
}
wlock->requestData_[val] = RequestData::constructPtr(data.release());
return true;
}
void RequestContext::setContextData(
const std::string& val,
std::unique_ptr<RequestData> data) {
doSetContextData(val, data, DoSetBehaviour::SET);
}
bool RequestContext::setContextDataIfAbsent(
const std::string& val,
std::unique_ptr<RequestData> data) {
return doSetContextData(val, data, DoSetBehaviour::SET_IF_ABSENT);
}
void RequestContext::overwriteContextData(
const std::string& val,
std::unique_ptr<RequestData> data) {
doSetContextData(val, data, DoSetBehaviour::OVERWRITE);
}
bool RequestContext::hasContextData(const std::string& val) const {
return state_.rlock()->requestData_.count(val);
}
RequestData* RequestContext::getContextData(const std::string& val) {
const RequestData::SharedPtr dflt{nullptr};
return get_ref_default(state_.rlock()->requestData_, val, dflt).get();
}
const RequestData* RequestContext::getContextData(
const std::string& val) const {
const RequestData::SharedPtr dflt{nullptr};
return get_ref_default(state_.rlock()->requestData_, val, dflt).get();
}
void RequestContext::onSet() {
auto rlock = state_.rlock();
for (const auto& data : rlock->callbackData_) {
data->onSet();
}
}
void RequestContext::onUnset() {
auto rlock = state_.rlock();
for (const auto& data : rlock->callbackData_) {
data->onUnset();
}
}
void RequestContext::clearContextData(const std::string& val) {
RequestData::SharedPtr requestData;
// Delete the RequestData after giving up the wlock just in case one of the
// RequestData destructors will try to grab the lock again.
{
auto ulock = state_.ulock();
auto it = ulock->requestData_.find(val);
if (it == ulock->requestData_.end()) {
return;
}
auto wlock = ulock.moveFromUpgradeToWrite();
if (it->second && it->second->hasCallback()) {
it->second->onUnset();
wlock->callbackData_.erase(it->second.get());
}
requestData = std::move(it->second);
wlock->requestData_.erase(it);
}
}
std::shared_ptr<RequestContext> RequestContext::setContext(
std::shared_ptr<RequestContext> ctx) {
auto& curCtx = getStaticContext();
if (ctx != curCtx) {
FOLLY_SDT(folly, request_context_switch_before, curCtx.get(), ctx.get());
if (curCtx) {
curCtx->onUnset();
}
std::swap(ctx, curCtx);
if (curCtx) {
curCtx->onSet();
}
}
return ctx;
}
std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
using SingletonT = SingletonThreadLocal<std::shared_ptr<RequestContext>>;
return SingletonT::get();
}
/* static */ std::shared_ptr<RequestContext>
RequestContext::setShallowCopyContext() {
auto* parent = get();
auto child = std::make_shared<RequestContext>();
{
auto parentLock = parent->state_.rlock();
auto childLock = child->state_.wlock();
childLock->callbackData_ = parentLock->callbackData_;
for (const auto& entry : parentLock->requestData_) {
childLock->requestData_[entry.first] =
RequestData::constructPtr(entry.second.get());
}
}
// Do not use setContext to avoid global set/unset
std::swap(child, getStaticContext());
return child;
}
/* static */ void RequestContext::unsetShallowCopyContext(
std::shared_ptr<RequestContext> ctx) {
// Do not use setContext to avoid set/unset
std::swap(ctx, getStaticContext());
// For readability
auto child = std::move(ctx);
// Handle the case where parent is actually default context
auto* parent = get();
// Call set/unset for all request data that differs
{
auto parentLock = parent->state_.rlock();
auto childLock = child->state_.rlock();
auto piter = parentLock->callbackData_.begin();
auto pend = parentLock->callbackData_.end();
auto citer = childLock->callbackData_.begin();
auto cend = childLock->callbackData_.end();
while (piter != pend || citer != cend) {
if (piter == pend || *citer < *piter) {
(*citer)->onUnset();
++citer;
} else if (citer == cend || *piter < *citer) {
(*piter)->onSet();
++piter;
} else {
DCHECK(*piter == *citer);
++piter;
++citer;
}
}
}
}
RequestContext* RequestContext::get() {
auto& context = getStaticContext();
if (!context) {
static RequestContext defaultContext;
return std::addressof(defaultContext);
}
return context.get();
}
} // namespace folly