-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathhttp.cpp
685 lines (543 loc) · 20 KB
/
http.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <iomanip>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <boost/array.hpp>
#include <process/help.hpp>
#include <process/metrics/metrics.hpp>
#include <stout/foreach.hpp>
#include <stout/json.hpp>
#include <stout/lambda.hpp>
#include <stout/memory.hpp>
#include <stout/net.hpp>
#include <stout/numify.hpp>
#include <stout/os.hpp>
#include <stout/result.hpp>
#include <stout/strings.hpp>
#include "common/attributes.hpp"
#include "common/build.hpp"
#include "common/http.hpp"
#include "common/protobuf_utils.hpp"
#include "common/type_utils.hpp"
#include "logging/logging.hpp"
#include "master/master.hpp"
#include "mesos/mesos.hpp"
#include "mesos/resources.hpp"
using process::Clock;
using process::DESCRIPTION;
using process::Future;
using process::HELP;
using process::TLDR;
using process::USAGE;
using process::http::BadRequest;
using process::http::InternalServerError;
using process::http::NotFound;
using process::http::OK;
using process::http::TemporaryRedirect;
using process::metrics::internal::MetricsProcess;
using std::map;
using std::string;
using std::vector;
namespace mesos {
namespace internal {
namespace master {
// Pull in model overrides from common.
using mesos::internal::model;
// Pull in definitions from process.
using process::http::Response;
using process::http::Request;
// TODO(bmahler): Kill these in favor of automatic Proto->JSON Conversion (when
// it becomes available).
// Returns a JSON object modeled on an Offer.
JSON::Object model(const Offer& offer)
{
JSON::Object object;
object.values["id"] = offer.id().value();
object.values["framework_id"] = offer.framework_id().value();
object.values["slave_id"] = offer.slave_id().value();
object.values["resources"] = model(offer.resources());
return object;
}
// Returns a JSON object modeled on a Framework.
JSON::Object model(const Framework& framework)
{
JSON::Object object;
object.values["id"] = framework.id.value();
object.values["name"] = framework.info.name();
object.values["user"] = framework.info.user();
object.values["failover_timeout"] = framework.info.failover_timeout();
object.values["checkpoint"] = framework.info.checkpoint();
object.values["role"] = framework.info.role();
object.values["registered_time"] = framework.registeredTime.secs();
object.values["unregistered_time"] = framework.unregisteredTime.secs();
object.values["active"] = framework.active;
object.values["resources"] = model(framework.resources);
object.values["hostname"] = framework.info.hostname();
// TODO(benh): Consider making reregisteredTime an Option.
if (framework.registeredTime != framework.reregisteredTime) {
object.values["reregistered_time"] = framework.reregisteredTime.secs();
}
// Model all of the tasks associated with a framework.
{
JSON::Array array;
foreachvalue (Task* task, framework.tasks) {
array.values.push_back(model(*task));
}
object.values["tasks"] = array;
}
// Model all of the completed tasks of a framework.
{
JSON::Array array;
foreach (const memory::shared_ptr<Task>& task, framework.completedTasks) {
array.values.push_back(model(*task));
}
object.values["completed_tasks"] = array;
}
// Model all of the offers associated with a framework.
{
JSON::Array array;
foreach (Offer* offer, framework.offers) {
array.values.push_back(model(*offer));
}
object.values["offers"] = array;
}
return object;
}
// Returns a JSON object modeled after a Slave.
JSON::Object model(const Slave& slave)
{
JSON::Object object;
object.values["id"] = slave.id.value();
object.values["pid"] = string(slave.pid);
object.values["hostname"] = slave.info.hostname();
object.values["registered_time"] = slave.registeredTime.secs();
if (slave.reregisteredTime.isSome()) {
object.values["reregistered_time"] = slave.reregisteredTime.get().secs();
}
object.values["resources"] = model(slave.info.resources());
object.values["attributes"] = model(slave.info.attributes());
return object;
}
// Returns a JSON object modeled after a Role.
JSON::Object model(const Role& role)
{
JSON::Object object;
object.values["name"] = role.info.name();
object.values["weight"] = role.info.weight();
object.values["resources"] = model(role.resources());
{
JSON::Array array;
foreachkey (const FrameworkID& frameworkId, role.frameworks) {
array.values.push_back(frameworkId.value());
}
object.values["frameworks"] = array;
}
return object;
}
const string Master::Http::HEALTH_HELP = HELP(
TLDR(
"Health check of the Master."),
USAGE(
"/master/health"),
DESCRIPTION(
"Returns 200 OK iff the Master is healthy.",
"Delayed responses are also indicative of poor health."));
Future<Response> Master::Http::health(const Request& request)
{
return OK();
}
const static string HOSTS_KEY = "hosts";
const static string LEVEL_KEY = "level";
const static string MONITOR_KEY = "monitor";
const string Master::Http::OBSERVE_HELP = HELP(
TLDR(
"Observe a monitor health state for host(s)."),
USAGE(
"/master/observe"),
DESCRIPTION(
"This endpoint receives information indicating host(s) ",
"health."
"",
"The following fields should be supplied in a POST:",
"1. " + MONITOR_KEY + " - name of the monitor that is being reported",
"2. " + HOSTS_KEY + " - comma seperated list of hosts",
"3. " + LEVEL_KEY + " - OK for healthy, anything else for unhealthy"));
Try<string> getFormValue(
const string& key,
const hashmap<string, string>& values)
{
Option<string> value = values.get(key);
if (value.isNone()) {
return Error("Missing value for '" + key + "'.");
}
// HTTP decode the value.
Try<string> decodedValue = process::http::decode(value.get());
if (decodedValue.isError()) {
return decodedValue;
}
// Treat empty string as an error.
if (decodedValue.isSome() && decodedValue.get().empty()) {
return Error("Empty string for '" + key + "'.");
}
return decodedValue.get();
}
Future<Response> Master::Http::observe(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
hashmap<string, string> values =
process::http::query::parse(request.body);
// Build up a JSON object of the values we recieved and send them back
// down the wire as JSON for validation / confirmation.
JSON::Object response;
// TODO(ccarson): As soon as RepairCoordinator is introduced it will
// consume these values. We should revisit if we still want to send the
// JSON down the wire at that point.
// Add 'monitor'.
Try<string> monitor = getFormValue(MONITOR_KEY, values);
if (monitor.isError()) {
return BadRequest(monitor.error());
}
response.values[MONITOR_KEY] = monitor.get();
// Add 'hosts'.
Try<string> hostsString = getFormValue(HOSTS_KEY, values);
if (hostsString.isError()) {
return BadRequest(hostsString.error());
}
vector<string> hosts = strings::split(hostsString.get(), ",");
JSON::Array hostArray;
hostArray.values.assign(hosts.begin(), hosts.end());
response.values[HOSTS_KEY] = hostArray;
// Add 'isHealthy'.
Try<string> level = getFormValue(LEVEL_KEY, values);
if (level.isError()) {
return BadRequest(level.error());
}
bool isHealthy = strings::upper(level.get()) == "OK";
response.values["isHealthy"] = isHealthy;
return OK(response);
}
const string Master::Http::REDIRECT_HELP = HELP(
TLDR(
"Redirects to the leading Master."),
USAGE(
"/master/redirect"),
DESCRIPTION(
"This returns a 307 Temporary Redirect to the leading Master.",
"If no Master is leading (according to this Master), then the",
"Master will redirect to itself.",
"",
"**NOTES:**",
"1. This is the recommended way to bookmark the WebUI when",
"running multiple Masters.",
"2. This is broken currently \"on the cloud\" (e.g. EC2) as",
"this will attempt to redirect to the private IP address."));
Future<Response> Master::Http::redirect(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
// If there's no leader, redirect to this master's base url.
MasterInfo info = master.leader.isSome() ? master.leader.get() : master.info_;
Try<string> hostname =
info.has_hostname() ? info.hostname() : net::getHostname(info.ip());
if (hostname.isError()) {
return InternalServerError(hostname.error());
}
return TemporaryRedirect(
"http://" + hostname.get() + ":" + stringify(info.port()));
}
// Declaration of 'stats' continuation.
static Future<Response> _stats(
const Request& request,
JSON::Object object,
const Response& response);
Future<Response> Master::Http::stats(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
JSON::Object object;
object.values["uptime"] = (Clock::now() - master.startTime).secs();
object.values["elected"] = master.elected(); // Note: using int not bool.
object.values["total_schedulers"] = master.frameworks.activated.size();
object.values["active_schedulers"] = master.getActiveFrameworks().size();
object.values["activated_slaves"] = master.slaves.activated.size();
object.values["deactivated_slaves"] = master.slaves.deactivated.size();
object.values["outstanding_offers"] = master.offers.size();
// NOTE: These are monotonically increasing counters.
object.values["staged_tasks"] = master.stats.tasks[TASK_STAGING];
object.values["started_tasks"] = master.stats.tasks[TASK_STARTING];
object.values["finished_tasks"] = master.stats.tasks[TASK_FINISHED];
object.values["killed_tasks"] = master.stats.tasks[TASK_KILLED];
object.values["failed_tasks"] = master.stats.tasks[TASK_FAILED];
object.values["lost_tasks"] = master.stats.tasks[TASK_LOST];
object.values["valid_status_updates"] = master.stats.validStatusUpdates;
object.values["invalid_status_updates"] = master.stats.invalidStatusUpdates;
// Get a count of all active tasks in the cluster i.e., the tasks
// that are launched (TASK_STAGING, TASK_STARTING, TASK_RUNNING) but
// haven't reached terminal state yet.
// NOTE: This is a gauge representing an instantaneous value.
int active_tasks = 0;
foreachvalue (Framework* framework, master.frameworks.activated) {
active_tasks += framework->tasks.size();
}
object.values["active_tasks_gauge"] = active_tasks;
// Get total and used (note, not offered) resources in order to
// compute capacity of scalar resources.
Resources totalResources;
Resources usedResources;
foreachvalue (Slave* slave, master.slaves.activated) {
// Instead of accumulating all types of resources (which is
// not necessary), we only accumulate scalar resources. This
// helps us bypass a performance problem caused by range
// additions (e.g. ports).
foreach (const Resource& resource, slave->info.resources()) {
if (resource.type() == Value::SCALAR) {
totalResources += resource;
}
}
foreach (const Resource& resource, slave->resourcesInUse) {
if (resource.type() == Value::SCALAR) {
usedResources += resource;
}
}
}
foreach (const Resource& resource, totalResources) {
CHECK(resource.has_scalar());
double total = resource.scalar().value();
object.values[resource.name() + "_total"] = total;
Option<Resource> option = usedResources.get(resource);
CHECK(!option.isSome() || option.get().has_scalar());
double used = option.isSome() ? option.get().scalar().value() : 0.0;
object.values[resource.name() + "_used"] = used;
double percent = used / total;
object.values[resource.name() + "_percent"] = percent;
}
// Include metrics from libprocess metrics while we sunset this
// endpoint in favor of libprocess metrics.
// TODO(benh): Remove this after transitioning to libprocess metrics.
return process::http::get(MetricsProcess::instance()->self(), "snapshot")
.then(lambda::bind(&_stats, request, object, lambda::_1));
}
static Future<Response> _stats(
const Request& request,
JSON::Object object,
const Response& response)
{
if (response.status != process::http::statuses[200]) {
return InternalServerError("Failed to get metrics: " + response.status);
}
Option<string> type = response.headers.get("Content-Type");
if (type.isNone() || type.get() != "application/json") {
return InternalServerError("Failed to get metrics: expecting JSON");
}
Try<JSON::Object> parse = JSON::parse<JSON::Object>(response.body);
if (parse.isError()) {
return InternalServerError("Failed to parse metrics: " + parse.error());
}
// Now add all the values from metrics.
// TODO(benh): Make sure we're not overwriting any values.
object.values.insert(parse.get().values.begin(), parse.get().values.end());
return OK(object, request.query.get("jsonp"));
}
Future<Response> Master::Http::state(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
JSON::Object object;
object.values["version"] = MESOS_VERSION;
if (build::GIT_SHA.isSome()) {
object.values["git_sha"] = build::GIT_SHA.get();
}
if (build::GIT_BRANCH.isSome()) {
object.values["git_branch"] = build::GIT_BRANCH.get();
}
if (build::GIT_TAG.isSome()) {
object.values["git_tag"] = build::GIT_TAG.get();
}
object.values["build_date"] = build::DATE;
object.values["build_time"] = build::TIME;
object.values["build_user"] = build::USER;
object.values["start_time"] = master.startTime.secs();
object.values["id"] = master.info().id();
object.values["pid"] = string(master.self());
object.values["hostname"] = master.info().hostname();
object.values["activated_slaves"] = master.slaves.activated.size();
object.values["deactivated_slaves"] = master.slaves.deactivated.size();
object.values["staged_tasks"] = master.stats.tasks[TASK_STAGING];
object.values["started_tasks"] = master.stats.tasks[TASK_STARTING];
object.values["finished_tasks"] = master.stats.tasks[TASK_FINISHED];
object.values["killed_tasks"] = master.stats.tasks[TASK_KILLED];
object.values["failed_tasks"] = master.stats.tasks[TASK_FAILED];
object.values["lost_tasks"] = master.stats.tasks[TASK_LOST];
if (master.flags.cluster.isSome()) {
object.values["cluster"] = master.flags.cluster.get();
}
if (master.leader.isSome()) {
object.values["leader"] = master.leader.get().pid();
}
if (master.flags.log_dir.isSome()) {
object.values["log_dir"] = master.flags.log_dir.get();
}
JSON::Object flags;
foreachpair (const string& name, const flags::Flag& flag, master.flags) {
Option<string> value = flag.stringify(master.flags);
if (value.isSome()) {
flags.values[name] = value.get();
}
}
object.values["flags"] = flags;
// Model all of the slaves.
{
JSON::Array array;
foreachvalue (Slave* slave, master.slaves.activated) {
array.values.push_back(model(*slave));
}
object.values["slaves"] = array;
}
// Model all of the frameworks.
{
JSON::Array array;
foreachvalue (Framework* framework, master.frameworks.activated) {
array.values.push_back(model(*framework));
}
object.values["frameworks"] = array;
}
// Model all of the completed frameworks.
{
JSON::Array array;
foreach (const memory::shared_ptr<Framework>& framework,
master.frameworks.completed) {
array.values.push_back(model(*framework));
}
object.values["completed_frameworks"] = array;
}
return OK(object, request.query.get("jsonp"));
}
Future<Response> Master::Http::roles(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
JSON::Object object;
// Model all of the roles.
{
JSON::Array array;
foreachvalue (Role* role, master.roles) {
array.values.push_back(model(*role));
}
object.values["roles"] = array;
}
return OK(object, request.query.get("jsonp"));
}
const string Master::Http::TASKS_HELP = HELP(
TLDR(
"Lists tasks from all active frameworks."),
USAGE(
"/master/tasks.json"),
DESCRIPTION(
"Lists known tasks.",
"",
"Query parameters:",
"",
"> limit=VALUE Maximum number of tasks returned "
"(default is " + stringify(TASK_LIMIT) + ").",
"> offset=VALUE Starts task list at offset.",
"> order=(asc|desc) Ascending or descending sort order "
"(default is descending)."
""));
struct TaskComparator
{
static bool ascending(const Task* lhs, const Task* rhs)
{
size_t lhsSize = lhs->statuses().size();
size_t rhsSize = rhs->statuses().size();
if ((lhsSize == 0) && (rhsSize == 0)) {
return false;
}
if (lhsSize == 0) {
return true;
}
if (rhsSize == 0) {
return false;
}
return (lhs->statuses(0).timestamp() < rhs->statuses(0).timestamp());
}
static bool descending(const Task* lhs, const Task* rhs)
{
size_t lhsSize = lhs->statuses().size();
size_t rhsSize = rhs->statuses().size();
if ((lhsSize == 0) && (rhsSize == 0)) {
return false;
}
if (rhsSize == 0) {
return true;
}
if (lhsSize == 0) {
return false;
}
return (lhs->statuses(0).timestamp() > rhs->statuses(0).timestamp());
}
};
Future<Response> Master::Http::tasks(const Request& request)
{
LOG(INFO) << "HTTP request for '" << request.path << "'";
// Get list options (limit and offset).
Result<int> result = numify<int>(request.query.get("limit"));
size_t limit = result.isSome() ? result.get() : TASK_LIMIT;
result = numify<int>(request.query.get("offset"));
size_t offset = result.isSome() ? result.get() : 0;
// TODO(nnielsen): Currently, formatting errors in offset and/or limit
// will silently be ignored. This could be reported to the user instead.
// Construct framework list with both active and completed framwworks.
vector<const Framework*> frameworks;
foreachvalue (Framework* framework, master.frameworks.activated) {
frameworks.push_back(framework);
}
foreach (const memory::shared_ptr<Framework>& framework,
master.frameworks.completed) {
frameworks.push_back(framework.get());
}
// Construct task list with both running and finished tasks.
vector<const Task*> tasks;
foreach (const Framework* framework, frameworks) {
foreachvalue (Task* task, framework->tasks) {
CHECK_NOTNULL(task);
tasks.push_back(task);
}
foreach (const memory::shared_ptr<Task>& task, framework->completedTasks) {
tasks.push_back(task.get());
}
}
// Sort tasks by task status timestamp. Default order is descending.
// The earlist timestamp is chosen for comparison when multiple are present.
Option<string> order = request.query.get("order");
if (order.isSome() && (order.get() == "asc")) {
sort(tasks.begin(), tasks.end(), TaskComparator::ascending);
} else {
sort(tasks.begin(), tasks.end(), TaskComparator::descending);
}
JSON::Array array;
size_t end = std::min(offset + limit, tasks.size());
for (size_t i = offset; i < end; i++) {
const Task* task = tasks[i];
array.values.push_back(model(*task));
}
JSON::Object object;
object.values["tasks"] = array;
return OK(object, request.query.get("jsonp"));
}
} // namespace master {
} // namespace internal {
} // namespace mesos {