forked from rsocket/rsocket-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColdResumeManager.cpp
More file actions
200 lines (181 loc) · 7.35 KB
/
ColdResumeManager.cpp
File metadata and controls
200 lines (181 loc) · 7.35 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
// Copyright 2004-present Facebook. All Rights Reserved.
#include "ColdResumeManager.h"
#include <fstream>
#include <folly/json.h>
namespace {
constexpr folly::StringPiece FIRST_SENT_POSITION = "FirstSentPosition";
constexpr folly::StringPiece LAST_SENT_POSITION = "LastSentPosition";
constexpr folly::StringPiece IMPLIED_POSITION = "ImpliedPosition";
constexpr folly::StringPiece LARGEST_USED_STREAMID = "LargestUsedStreamId";
constexpr folly::StringPiece STREAM_RESUME_INFOS = "StreamResumeInfos";
constexpr folly::StringPiece FRAMES = "Frames";
constexpr folly::StringPiece STREAM_TYPE = "StreamType";
constexpr folly::StringPiece REQUESTER = "Requester";
constexpr folly::StringPiece STREAM_TOKEN = "StreamToken";
constexpr folly::StringPiece PROD_ALLOWANCE = "ProducerAllowance";
constexpr folly::StringPiece CONS_ALLOWANCE = "ConsumerAllowance";
}
namespace rsocket {
ColdResumeManager::ColdResumeManager(
std::shared_ptr<RSocketStats> stats,
std::string inputFile)
: WarmResumeManager(std::move(stats)) {
if (inputFile.empty()) {
return;
}
LOG(INFO) << "Reading state from " << inputFile;
try {
std::ifstream f(inputFile);
std::stringstream buffer;
buffer << f.rdbuf();
auto state = folly::parseJson(buffer.str());
f.close();
if (!state.isObject() || state.size() != 6) {
throw std::runtime_error(
"Invalid file content. Expected dynamic object of 6 elements");
}
if (state.count(FIRST_SENT_POSITION) != 1 ||
state.count(LAST_SENT_POSITION) != 1 ||
state.count(IMPLIED_POSITION) != 1 ||
state.count(LARGEST_USED_STREAMID) != 1 ||
state.count(STREAM_RESUME_INFOS) != 1 || state.count(FRAMES) != 1) {
throw std::runtime_error("Invalid file content. Keys Missing");
}
firstSentPosition_ = state[FIRST_SENT_POSITION].getInt();
lastSentPosition_ = state[LAST_SENT_POSITION].getInt();
impliedPosition_ = state[IMPLIED_POSITION].getInt();
largestUsedStreamId_ = state[LARGEST_USED_STREAMID].getInt();
for (const auto& item : state[STREAM_RESUME_INFOS].items()) {
auto streamId = folly::to<int64_t>(item.first.getString());
auto streamResumeInfoObj = item.second;
if (streamResumeInfoObj.count(STREAM_TYPE) != 1 ||
streamResumeInfoObj.count(STREAM_TOKEN) != 1 ||
streamResumeInfoObj.count(PROD_ALLOWANCE) != 1 ||
streamResumeInfoObj.count(CONS_ALLOWANCE) != 1 ||
streamResumeInfoObj.count(CONS_ALLOWANCE) != 1) {
throw std::runtime_error(
"Invalid file content. StreamResumeInfo Keys Missing");
}
StreamResumeInfo streamResumeInfo(
static_cast<StreamType>(streamResumeInfoObj[STREAM_TYPE].getInt()),
static_cast<RequestOriginator>(
streamResumeInfoObj[REQUESTER].getInt()),
streamResumeInfoObj[STREAM_TOKEN].getString());
streamResumeInfo.producerAllowance =
streamResumeInfoObj[PROD_ALLOWANCE].getInt();
streamResumeInfo.consumerAllowance =
streamResumeInfoObj[CONS_ALLOWANCE].getInt();
streamResumeInfos_.emplace(streamId, std::move(streamResumeInfo));
}
auto framesObj = state[FRAMES];
if (!framesObj.isArray()) {
throw std::runtime_error(
"Invalid file content. Frames not in right format");
}
for (const auto& item : framesObj) {
if (!item.isObject() || item.size() != 1) {
throw std::runtime_error(
"Invalid file content. Expected dynamic object of 1 element");
}
auto ioBuf = folly::IOBuf::copyBuffer(
item.values().begin()->getString().c_str(),
item.values().begin()->getString().size());
frames_.emplace_back(
folly::to<int64_t>(item.keys().begin()->getString()),
std::move(ioBuf));
}
} catch (const std::exception& ex) {
throw std::runtime_error(
folly::sformat("Failed parsing file {}. {}", inputFile, ex.what()));
}
}
void ColdResumeManager::persistState(std::string outputFile) {
VLOG(1) << "~ColdResumeManager";
if (outputFile.empty()) {
throw std::runtime_error("Persisting to file failed. Empty filename");
}
LOG(INFO) << "Persisting state to " << outputFile;
try {
folly::dynamic state = folly::dynamic::object();
state[FIRST_SENT_POSITION] = firstSentPosition_;
state[LAST_SENT_POSITION] = lastSentPosition_;
state[IMPLIED_POSITION] = impliedPosition_;
state[LARGEST_USED_STREAMID] = largestUsedStreamId_;
state[STREAM_RESUME_INFOS] = folly::dynamic::object();
for (const auto& streamResumeInfo : streamResumeInfos_) {
folly::dynamic val = folly::dynamic::object();
val[STREAM_TYPE] = folly::to<int>(streamResumeInfo.second.streamType);
val[STREAM_TOKEN] = streamResumeInfo.second.streamToken;
val[REQUESTER] = folly::to<int>(streamResumeInfo.second.requester);
val[CONS_ALLOWANCE] = streamResumeInfo.second.consumerAllowance;
val[PROD_ALLOWANCE] = streamResumeInfo.second.producerAllowance;
state[STREAM_RESUME_INFOS].insert(
folly::to<std::string>(streamResumeInfo.first), val);
}
state[FRAMES] = folly::dynamic::array();
for (const auto& frame : frames_) {
state[FRAMES].push_back(folly::dynamic::object(
folly::to<std::string>(frame.first),
frame.second->moveToFbString().toStdString()));
}
std::string jsonState = folly::toPrettyJson(state);
std::ofstream f(outputFile);
f << jsonState;
f.close();
} catch (const std::exception& ex) {
throw std::runtime_error(folly::sformat(
"Persisting state to {} failed. {}", outputFile, ex.what()));
}
LOG(INFO) << "Done persisting state to " << outputFile;
}
void ColdResumeManager::trackReceivedFrame(
size_t frameLength,
FrameType frameType,
StreamId streamId,
size_t consumerAllowance) {
if (!shouldTrackFrame(frameType)) {
return;
}
auto it = streamResumeInfos_.find(streamId);
// If streamId is not present in streamResumeInfo it likely means a
// COMPLETE/CANCEL/ERROR was received in this frame and the
// ResumeMananger::onCloseStream() was already invoked resulting in the entry
// being deleted.
if (it != streamResumeInfos_.end()) {
it->second.consumerAllowance = consumerAllowance;
}
WarmResumeManager::trackReceivedFrame(
frameLength, frameType, streamId, consumerAllowance);
}
void ColdResumeManager::trackSentFrame(
const folly::IOBuf& serializedFrame,
FrameType frameType,
StreamId streamId,
size_t consumerAllowance) {
if (!shouldTrackFrame(frameType)) {
return;
}
auto it = streamResumeInfos_.find(streamId);
CHECK(it != streamResumeInfos_.end());
it->second.consumerAllowance = consumerAllowance;
WarmResumeManager::trackSentFrame(
std::move(serializedFrame), frameType, streamId, consumerAllowance);
}
void ColdResumeManager::onStreamClosed(StreamId streamId) {
streamResumeInfos_.erase(streamId);
}
void ColdResumeManager::onStreamOpen(
StreamId streamId,
RequestOriginator requester,
std::string streamToken,
StreamType streamType) {
CHECK(streamType != StreamType::FNF);
CHECK(streamResumeInfos_.find(streamId) == streamResumeInfos_.end());
if (requester == RequestOriginator::LOCAL &&
streamId > largestUsedStreamId_) {
largestUsedStreamId_ = streamId;
}
streamResumeInfos_.emplace(
streamId, StreamResumeInfo(streamType, requester, streamToken));
}
} // reactivesocket