This repository was archived by the owner on Dec 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathTreadmillFB303.cpp
267 lines (230 loc) · 7.92 KB
/
TreadmillFB303.cpp
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
/*
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#include "TreadmillFB303.h"
#include "thrift/lib/cpp/util/EnumUtils.h"
#include "Scheduler.h"
#include <memory>
#include <folly/Conv.h>
#include <folly/Singleton.h>
#include <thrift/lib/cpp2/server/ThriftServer.h>
#include "common/services/cpp/TLSConfig.h"
#include "common/time/ClockGettimeNS.h"
DEFINE_bool(
require_configuration_on_resume,
false,
"If true, 'resume' only when configuration is available");
DEFINE_bool(
enable_watchdog_timer,
false,
"If true, a watchdog timer will be maintained during a run.");
using fb_status = facebook::fb303::cpp2::fb_status;
using ::treadmill::RateResponse;
using ::treadmill::ResumeRequest;
using ::treadmill::ResumeResponse;
using namespace facebook::services;
namespace facebook {
namespace windtunnel {
namespace treadmill {
TreadmillFB303::TreadmillFB303(Scheduler& scheduler)
: FacebookBase2("Treadmill"),
status_(fb_status::STARTING),
aliveSince_(time(nullptr)),
scheduler_(scheduler),
configuration_(std::make_unique<std::map<std::string, std::string>>()),
watchdogDurationSec_(0),
lastHeartbeat_(0) {}
TreadmillFB303::~TreadmillFB303() {}
void TreadmillFB303::setStatus(fb_status status) {
folly::SharedMutex::WriteHolder guard(mutex_);
status_ = status;
}
fb_status TreadmillFB303::getStatus() {
folly::SharedMutex::ReadHolder guard(mutex_);
return status_;
}
void TreadmillFB303::getStatusDetails(std::string& _return) {
_return = apache::thrift::util::enumNameOrThrow(getStatus());
}
int64_t TreadmillFB303::aliveSince() {
folly::SharedMutex::ReadHolder guard(mutex_);
return aliveSince_;
}
void TreadmillFB303::getCounters(std::map<std::string, int64_t>& _return) {
fb303::FacebookBase2::getCounters(_return);
}
bool TreadmillFB303::pause() {
LOG(INFO) << "TreadmillHandler::pause";
scheduler_.pause();
watchdogDurationSec_ = 0;
return true;
}
bool TreadmillFB303::resume() {
LOG(INFO) << "TreadmillHandler::resume";
watchdogUpdate();
folly::SharedMutex::ReadHolder guard(mutex_);
if (FLAGS_require_configuration_on_resume && configuration_->empty()) {
LOG(WARNING) << "refusing resume without configuration";
return false;
}
return scheduler_.resume();
}
folly::Future<std::unique_ptr<ResumeResponse>> TreadmillFB303::future_resume2(
std::unique_ptr<ResumeRequest> req) {
// Get the phase name being super paranoid.
auto phaseName = req != nullptr ? req->get_phaseName() : "UNKNOWN_PHASE";
LOG(INFO) << "TreadmillHandler::resume2 with phase " << phaseName;
watchdogUpdate();
scheduler_.setPhase(phaseName);
auto resp = std::make_unique<ResumeResponse>();
auto running = scheduler_.resume();
LOG(INFO) << "Scheduler is currently "
<< (running ? "Running" : "Not Running");
resp->success_ref() = running;
return folly::makeFuture(std::move(resp));
}
void TreadmillFB303::setRps(int32_t rps) {
LOG(INFO) << "TreadmillHandler::setRps to " << rps;
watchdogUpdate();
scheduler_.setRps(rps);
}
void TreadmillFB303::setMaxOutstanding(int32_t max_outstanding) {
LOG(INFO) << "TreadmillHandler::setMaxOutstanding to " << max_outstanding;
watchdogUpdate();
scheduler_.setMaxOutstandingRequests(max_outstanding);
}
folly::Future<std::unique_ptr<::treadmill::RateResponse>>
TreadmillFB303::future_getRate() {
auto response = std::make_unique<RateResponse>();
response->scheduler_running_ref() = scheduler_.isRunning();
response->rps_ref() = scheduler_.getRps();
response->max_outstanding_ref() = scheduler_.getMaxOutstandingRequests();
return folly::makeFuture(std::move(response));
}
folly::Future<std::unique_ptr<std::string>>
TreadmillFB303::future_getConfiguration(std::unique_ptr<std::string> key) {
LOG(INFO) << "TreadmillHandler::getConfiguration: " << *key;
watchdogUpdate();
folly::SharedMutex::ReadHolder guard(mutex_);
if (configuration_->count(*key) > 0) {
auto value = std::make_unique<std::string>(configuration_->at(*key));
LOG(INFO) << "returning " << *key << " = " << *value;
return folly::makeFuture(std::move(value));
}
auto value = std::make_unique<std::string>();
return folly::makeFuture(std::move(value));
}
void TreadmillFB303::setConfiguration(
std::unique_ptr<std::string> key,
std::unique_ptr<std::string> value) {
LOG(INFO) << "TreadmillHandler::setConfiguration: " << *key << " = "
<< *value;
watchdogUpdate();
folly::SharedMutex::WriteHolder guard(mutex_);
configuration_->insert_or_assign(*key, *value);
if (FLAGS_enable_watchdog_timer && *key == "watchdog_sec") {
LOG(INFO) << "TreadmillHandler::watchdog timer value (secs) = " << *value;
if (auto result = folly::tryTo<uint32_t>(*value)) {
watchdogDurationSec_ = result.value();
} else {
watchdogDurationSec_ = 0; // disabled
}
}
}
uint32_t TreadmillFB303::getConfigurationValue(
const std::string& key,
uint32_t defaultValue) {
folly::SharedMutex::ReadHolder guard(mutex_);
if (configuration_->count(key) > 0) {
auto value = std::make_unique<std::string>(configuration_->at(key));
if (auto result = folly::tryTo<uint32_t>(*value)) {
return result.value();
}
LOG(WARNING) << "failed to convert value [" << *value << "]";
// fall through
}
return defaultValue;
}
std::unique_ptr<std::string> TreadmillFB303::getConfigurationValue(
const std::string& key,
const std::string& defaultValue) {
folly::SharedMutex::ReadHolder guard(mutex_);
if (configuration_->count(key) > 0) {
return std::make_unique<std::string>(configuration_->at(key));
} else {
return std::make_unique<std::string>(defaultValue);
}
}
void TreadmillFB303::clearConfiguration() {
LOG(INFO) << "TreadmillHandler::clearConfiguration";
watchdogUpdate();
folly::SharedMutex::WriteHolder guard(mutex_);
configuration_->clear();
}
bool TreadmillFB303::configurationEmpty() const {
folly::SharedMutex::ReadHolder guard(mutex_);
return configuration_->empty();
}
void TreadmillFB303::watchdogUpdate() {
if (FLAGS_enable_watchdog_timer && watchdogDurationSec_ > 0) {
lastHeartbeat_ = fb_time_seconds();
LOG(INFO) << "watchdog update = " << lastHeartbeat_;
}
}
bool TreadmillFB303::watchdogTimeoutCheck(bool raise) {
if (FLAGS_enable_watchdog_timer && watchdogDurationSec_ > 0) {
time_t now = fb_time_seconds();
if (now - watchdogDurationSec_ > lastHeartbeat_) {
LOG(WARNING) << "watchdog timeout: no contact since " << lastHeartbeat_;
if (raise) {
abort();
}
return true;
}
}
return false;
}
namespace {
folly::SharedMutex instance_mutex;
std::shared_ptr<TreadmillFB303> instance;
} // namespace
std::shared_ptr<TreadmillFB303> getGlobalTreadmillFB303() {
folly::SharedMutex::ReadHolder guard(instance_mutex);
if (!instance) {
LOG(FATAL) << "No global Treadmill FB303 instance set";
}
return instance;
}
void TreadmillFB303::make_fb303(
std::shared_ptr<std::thread>& server_thread,
int server_port,
Scheduler& scheduler) {
{
folly::SharedMutex::WriteHolder guard(instance_mutex);
if (instance) {
LOG(FATAL) << "Global Treadmill FB303 instance was already set";
}
instance = std::make_shared<TreadmillFB303>(scheduler);
}
auto server = std::make_shared<apache::thrift::ThriftServer>();
LOG(INFO) << "FB303 running on port " << server_port;
server->setPort(server_port);
server->setInterface(getGlobalTreadmillFB303());
TLSConfig::applyDefaultsToThriftServer(*server);
server_thread.reset(
new std::thread([server]() { server->serve(); }),
[server](std::thread* t) {
server->stop();
t->join();
delete t;
});
}
} // namespace treadmill
} // namespace windtunnel
} // namespace facebook