-
Notifications
You must be signed in to change notification settings - Fork 644
Expand file tree
/
Copy pathRequestContext.h
More file actions
292 lines (247 loc) · 9.84 KB
/
Copy pathRequestContext.h
File metadata and controls
292 lines (247 loc) · 9.84 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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.
*/
#pragma once
#include <chrono>
#include <functional>
#include <memory>
#include <utility>
#include <boost/intrusive/unordered_set.hpp>
#include <folly/IntrusiveList.h>
#include <folly/Likely.h>
#include <folly/Portability.h>
#include <folly/Traits.h>
#include <folly/fibers/Baton.h>
#include <thrift/lib/cpp2/transport/rocket/Types.h>
#include <thrift/lib/cpp2/transport/rocket/framing/FrameType.h>
#include <thrift/lib/cpp2/transport/rocket/framing/Frames.h>
#include <thrift/lib/cpp2/transport/rocket/framing/Serializer.h>
namespace apache::thrift {
struct RpcTransportStats;
} // namespace apache::thrift
namespace apache::thrift::rocket {
class RocketClient;
class RequestContextQueue;
class RequestContext {
private:
template <typename T>
using payload_method_t = decltype(std::declval<const T&>().payload());
public:
class WriteSuccessCallback {
public:
virtual ~WriteSuccessCallback() = default;
virtual void onWriteSuccess() noexcept = 0;
};
enum class State : uint8_t {
DEFERRED_INIT, /* still needs to be initialized with server version */
WRITE_NOT_SCHEDULED,
WRITE_SCHEDULED,
WRITE_SENDING, /* AsyncSocket::writeChain() called, but WriteCallback has
not yet fired */
WRITE_SENT, /* Write to socket completed (possibly with error) */
COMPLETE, /* Terminal state. Result stored in responsePayload_ */
};
template <class Frame>
RequestContext(
Frame&& frame,
RequestContextQueue& queue,
SetupFrame* setupFrame = nullptr,
WriteSuccessCallback* writeSuccessCallback = nullptr,
folly::IOBufFactory* ioBufFactory = nullptr,
RpcTransportStats* channelStats = nullptr)
: queue_(queue),
streamId_(frame.streamId()),
frameType_(Frame::frameType()),
writeSuccessCallback_(writeSuccessCallback),
ioBufFactory_(ioBufFactory),
channelStats_(channelStats) {
// Some `Frame`s lack a `payload()` method -- `RequestNFrame`,
// `CancelFrame`, etc -- but those that do should have `.fds`.
if constexpr (folly::is_detected<payload_method_t, Frame>::value) {
fds = std::move(frame.payload().fds.dcheckToSendOrEmpty());
}
serialize(std::forward<Frame>(frame), setupFrame);
}
template <class InitFunc>
RequestContext(
InitFunc&& initFunc,
int32_t serverVersion,
StreamId streamId,
RequestContextQueue& queue,
WriteSuccessCallback* writeSuccessCallback = nullptr,
RpcTransportStats* channelStats = nullptr)
: queue_(queue),
streamId_(streamId),
writeSuccessCallback_(writeSuccessCallback),
channelStats_(channelStats) {
if (UNLIKELY(serverVersion == -1)) {
deferredInit_ = std::forward<InitFunc>(initFunc);
state_ = State::DEFERRED_INIT;
} else {
std::tie(serializedFrame_, frameType_) = initFunc(serverVersion);
}
}
RequestContext(const RequestContext&) = delete;
RequestContext(RequestContext&&) = delete;
RequestContext& operator=(const RequestContext&) = delete;
RequestContext& operator=(RequestContext&&) = delete;
// For REQUEST_RESPONSE contexts, where an immediate matching response is
// expected
[[nodiscard]] folly::Try<Payload> waitForResponse(
std::chrono::milliseconds timeout);
[[nodiscard]] folly::Try<Payload> getResponse() &&;
// For request types for which an immediate matching response is not
// necessarily expected, e.g., REQUEST_FNF and REQUEST_STREAM
[[nodiscard]] folly::Try<void> waitForWriteToComplete();
void waitForWriteToCompleteSchedule(folly::fibers::Baton::Waiter* waiter);
[[nodiscard]] folly::Try<void> waitForWriteToCompleteResult();
void setTimeoutInfo(
folly::HHWheelTimer& timer,
folly::HHWheelTimer::Callback& callback,
std::chrono::milliseconds timeout) {
timer_ = &timer;
timeoutCallback_ = &callback;
requestTimeout_ = timeout;
}
void scheduleTimeoutForResponse() {
DCHECK(isRequestResponse());
// In some edge cases, response may arrive before write to socket finishes.
if (state_ != State::COMPLETE &&
requestTimeout_ != std::chrono::milliseconds::zero()) {
timer_->scheduleTimeout(timeoutCallback_, requestTimeout_);
}
}
std::unique_ptr<folly::IOBuf> releaseSerializedChain() {
DCHECK(serializedFrame_);
return std::move(serializedFrame_);
}
size_t endOffsetInBatch() const {
DCHECK_GT(endOffsetInBatch_, 0);
return endOffsetInBatch_;
}
void setEndOffsetInBatch(ssize_t offset) { endOffsetInBatch_ = offset; }
State state() const { return state_; }
StreamId streamId() const { return streamId_; }
bool isRequestResponse() const {
return frameType_ == FrameType::REQUEST_RESPONSE;
}
void onPayloadFrame(PayloadFrame&& payloadFrame);
void onErrorFrame(ErrorFrame&& errorFrame);
void onWriteSuccess() noexcept;
bool hasPartialPayload() const { return responsePayload_.hasValue(); }
void initWithVersion(int32_t serverVersion) {
if (!deferredInit_) {
return;
}
DCHECK(state_ == State::DEFERRED_INIT);
std::tie(serializedFrame_, frameType_) = deferredInit_(serverVersion);
DCHECK(serializedFrame_ && frameType_ != FrameType::RESERVED);
state_ = State::WRITE_NOT_SCHEDULED;
}
folly::SocketFds fds;
protected:
friend class RocketClient;
void markLastInWriteBatch() { lastInWriteBatch_ = true; }
private:
RequestContextQueue& queue_;
folly::SafeIntrusiveListHook queueHook_;
std::unique_ptr<folly::IOBuf> serializedFrame_;
ssize_t endOffsetInBatch_{};
StreamId streamId_;
FrameType frameType_;
State state_{State::WRITE_NOT_SCHEDULED};
bool lastInWriteBatch_{false};
bool isDummyEndOfBatchMarker_{false};
boost::intrusive::unordered_set_member_hook<> setHook_;
folly::fibers::Baton baton_;
std::chrono::milliseconds requestTimeout_{1000};
folly::HHWheelTimer* timer_{nullptr};
folly::HHWheelTimer::Callback* timeoutCallback_{nullptr};
folly::Try<Payload> responsePayload_;
WriteSuccessCallback* const writeSuccessCallback_{nullptr};
folly::IOBufFactory* ioBufFactory_{nullptr};
// Non-owning pointer to the channel-side RpcTransportStats that the
// rocket layer writes the first-payload-frame latency into. May be
// nullptr for non-REQUEST_RESPONSE contexts and for REQUEST_RESPONSE
// contexts where the caller did not opt in to stats collection.
RpcTransportStats* channelStats_{nullptr};
// Steady-clock timestamp of when the AsyncSocket WriteCallback fired
// (the same epoch RpcTransportStats::responseRoundTripLatency starts
// from) -- sampled on the same thread as the channel callback's own
// timeEndSend_ but strictly after the channel callback returns.
std::chrono::steady_clock::time_point timeEndSend_{};
// Steady-clock timestamp of when the first PAYLOAD frame for this
// response was received by the Rocket client (set in onPayloadFrame
// on the first emplace; not updated by subsequent append() calls so
// the value reflects the first frame, not the last).
std::chrono::steady_clock::time_point firstResponsePayloadFrameTime_{};
folly::Function<std::pair<std::unique_ptr<folly::IOBuf>, FrameType>(int32_t)>
deferredInit_{nullptr};
// Computes RpcTransportStats::firstResponsePayloadFrameLatency from
// timeEndSend_ and firstResponsePayloadFrameTime_ and writes it into
// *channelStats_. No-op when channelStats_ is null or when no payload
// frame was received. Called by RequestContextQueue::markAsResponded
// between the synthesized onWriteSuccess() and baton_.post() so the
// write lands BEFORE the synchronous response delivery.
void finalizeRpcTransportStats() noexcept;
template <class Frame>
void serialize(Frame&& frame, SetupFrame* setupFrame) {
DCHECK(!serializedFrame_);
serializedFrame_ = std::move(frame).serialize(ioBufFactory_);
if (UNLIKELY(setupFrame != nullptr)) {
Serializer writer(ioBufFactory_);
std::move(*setupFrame).serialize(writer);
auto setupBuffer = std::move(writer).move();
setupBuffer->prependChain(std::move(serializedFrame_));
serializedFrame_ = std::move(setupBuffer);
}
}
explicit RequestContext(RequestContextQueue& queue)
: queue_(queue), frameType_(FrameType::REQUEST_RESPONSE) {}
static RequestContext& createDummyEndOfBatchMarker(
RequestContextQueue& queue) {
auto* rctx = new RequestContext(queue);
rctx->lastInWriteBatch_ = true;
rctx->isDummyEndOfBatchMarker_ = true;
rctx->state_ = State::WRITE_SENDING;
return *rctx;
}
struct Equal {
bool operator()(
const RequestContext& ctxa, const RequestContext& ctxb) const noexcept {
return ctxa.streamId_ == ctxb.streamId_;
}
};
struct Hash {
size_t operator()(const RequestContext& ctx) const noexcept {
return std::hash<StreamId::underlying_type>()(
static_cast<uint32_t>(ctx.streamId_));
}
};
public:
using Queue =
folly::CountedIntrusiveList<RequestContext, &RequestContext::queueHook_>;
using UnorderedSet = boost::intrusive::unordered_set<
RequestContext,
boost::intrusive::member_hook<
RequestContext,
decltype(setHook_),
&RequestContext::setHook_>,
boost::intrusive::equal<Equal>,
boost::intrusive::hash<Hash>>;
private:
friend class RequestContextQueue;
};
} // namespace apache::thrift::rocket