forked from dongahn/resource-query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobspec.cpp
540 lines (489 loc) · 16.3 KB
/
jobspec.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
/*****************************************************************************\
* Copyright (c) 2017 Lawrence Livermore National Security, LLC. Produced at
* the Lawrence Livermore National Laboratory (cf, AUTHORS, DISCLAIMER.LLNS).
* LLNL-CODE-658032 All rights reserved.
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the license, or (at your option)
* any later version.
*
* Flux is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the IMPLIED WARRANTY OF MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the terms and conditions of the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
* See also: http://www.gnu.org/licenses/
\*****************************************************************************/
#include "jobspec.hpp"
#include <iostream>
#include <string>
#include <unordered_map>
extern "C" {
#if HAVE_CONFIG_H
#include "config.h"
#endif
}
using namespace std;
using namespace Flux::Jobspec;
parse_error::parse_error(const char *msg)
: runtime_error(msg),
position(-1),
line(-1),
column(-1)
{}
parse_error::parse_error(const YAML::Node &node, const char *msg)
: runtime_error(msg),
#ifdef HAVE_YAML_MARK
position(node.Mark().pos),
line(node.Mark().line+1),
column(node.Mark().column)
#else
position(-1),
line(-1),
column(-1)
#endif
{}
namespace {
void parse_yaml_count (Resource& res, const YAML::Node &cnode)
{
/* count can have an unsigned interger value */
if (cnode.IsScalar()) {
res.count.min = cnode.as<unsigned>();
res.count.max = res.count.min;
return;
}
/* or count may be a more complicated verbose form */
if (!cnode.IsMap()) {
throw parse_error (cnode, "count is not a mapping");
}
/* Verify existance of required entries */
if (!cnode["min"]) {
throw parse_error (cnode, "Key \"min\" missing from count");
}
if (!cnode["min"].IsScalar()) {
throw parse_error (cnode["min"], "Value of \"min\" must be a scalar");
}
if (!cnode["max"]) {
throw parse_error (cnode, "Key \"max\" missing from count");
}
if (!cnode["max"].IsScalar()) {
throw parse_error (cnode["max"], "Value of \"max\" must be a scalar");
}
if (!cnode["operator"]) {
throw parse_error (cnode, "Key \"operator\" missing from count");
}
if (!cnode["operator"].IsScalar()) {
throw parse_error (cnode["operator"],
"Value of \"operator\" must be a scalar");
}
if (!cnode["operand"]) {
throw parse_error (cnode, "Key \"operand\" missing from count");
}
if (!cnode["operand"].IsScalar()) {
throw parse_error (cnode["operand"],
"Value of \"operand\" must be a scalar");
}
/* Validate values of entries */
res.count.min = cnode["min"].as<unsigned>();
if (res.count.min < 1) {
throw parse_error (cnode["min"], "\"min\" must be greater than zero");
}
res.count.max = cnode["max"].as<unsigned>();
if (res.count.max < 1) {
throw parse_error (cnode["max"], "\"max\" must be greater than zero");
}
if (res.count.max < res.count.min) {
throw parse_error (cnode["max"],
"\"max\" must be greater than or equal to \"min\"");
}
res.count.oper = cnode["operator"].as<char>();
switch (res.count.oper) {
case '+':
case '*':
case '^':
break;
default:
throw parse_error (cnode["operator"], "Invalid count operator");
}
res.count.operand = cnode["operand"].as<int>();
}
}
namespace {
vector<Resource> parse_yaml_resources (const YAML::Node &resources);
}
Resource::Resource (const YAML::Node &resnode)
{
unsigned field_count = 0;
/* The resource must be a mapping */
if (!resnode.IsMap()) {
throw parse_error (resnode, "resource is not a mapping");
}
if (!resnode["type"]) {
throw parse_error (resnode, "Key \"type\" missing from resource");
}
if (!resnode["type"].IsScalar()) {
throw parse_error (resnode["type"],
"Value of \"type\" must be a scalar");
}
type = resnode["type"].as<string>();
field_count++;
if (!resnode["count"]) {
throw parse_error (resnode, "Key \"count\" missing from resource");
}
parse_yaml_count (*this, resnode["count"]);
field_count++;
if (resnode["unit"]) {
if (!resnode["unit"].IsScalar()) {
throw parse_error (resnode["unit"],
"Value of \"unit\" must be a scalar");
}
field_count++;
unit = resnode["unit"].as<string>();
}
if (resnode["exclusive"]) {
if (!resnode["exclusive"].IsScalar()) {
throw parse_error (resnode["exclusive"],
"Value of \"exclusive\" must be a scalar");
}
field_count++;
string val = resnode["exclusive"].as<string>();
if (val == "false") {
exclusive = tristate_t::FALSE;
} else if (val == "true") {
exclusive = tristate_t::TRUE;
} else {
throw parse_error (resnode["exclusive"],
"Value of \"exclusive\" must be either \"true\" or \"false\"");
}
}
if (resnode["with"]) {
field_count++;
with = parse_yaml_resources (resnode["with"]);
}
if (resnode["label"]) {
if (!resnode["label"].IsScalar()) {
throw parse_error (resnode["label"],
"Value of \"label\" must be a scalar");
}
field_count++;
label = resnode["label"].as<string>();
} else if (type == "slot") {
throw parse_error (resnode, "All slots must be labeled");
}
if (resnode["id"]) {
if (!resnode["id"].IsScalar()) {
throw parse_error (resnode["id"],
"Value of \"id\" must be a scalar");
}
field_count++;
id = resnode["id"].as<string>();
}
if (field_count != resnode.size()) {
throw parse_error (resnode, "Unrecognized key in resource mapping");
}
if (resnode.size() < 2 || resnode.size() > 10) {
throw parse_error (resnode,
"impossible number of entries in resource mapping");
}
}
Task::Task (const YAML::Node &tasknode)
{
/* The task node must be a mapping */
if (!tasknode.IsMap()) {
throw parse_error (tasknode, "task is not a mapping");
}
if (!tasknode["command"]) {
throw parse_error (tasknode, "Key \"command\" missing from task");
}
if (tasknode["command"].IsSequence()) {
command = tasknode["command"].as<vector<string>>();
} else if (tasknode["command"].IsScalar()) {
command.push_back(tasknode["command"].as<string>());
} else {
throw parse_error (tasknode["command"],
"\"command\" value must be a scalar or a sequence");
}
/* Import slot */
if (!tasknode["slot"]) {
throw parse_error (tasknode, "Key \"slot\" missing from task");
}
if (!tasknode["slot"].IsScalar()) {
throw parse_error (tasknode["slot"],
"Value of task \"slot\" must be a YAML scalar");
}
slot = tasknode["slot"].as<string>();
/* Import count mapping */
if (tasknode["count"]) {
YAML::Node count_node = tasknode["count"];
if (!count_node.IsMap()) {
throw parse_error (count_node,
"\"count\" in task is not a mapping");
}
for (auto&& entry : count_node) {
count[entry.first.as<string>()] = entry.second.as<string>();
}
}
/* Import distribution if it is present */
if (tasknode["distribution"]) {
if (!tasknode["distribution"].IsScalar()) {
throw parse_error (tasknode["distribution"],
"Value of task \"distribution\" must be a YAML scalar");
}
distribution = tasknode["distribution"].as<string>();
}
/* Import attributes mapping if it is present */
if (tasknode["attributes"]) {
YAML::Node attrs = tasknode["attributes"];
if (!attrs.IsMap()) {
throw parse_error (attrs, "\"attributes\" in task is not a mapping");
}
for (auto&& attr : attrs) {
attributes[attr.first.as<string>()] = attr.second.as<string>();
}
}
if (tasknode.size() < 3 || tasknode.size() > 5) {
throw parse_error (tasknode,
"impossible number of entries in task mapping");
}
}
namespace {
vector<Task> parse_yaml_tasks (const YAML::Node &tasks)
{
vector<Task> taskvec;
/* "tasks" must be a sequence */
if (!tasks.IsSequence()) {
throw parse_error (tasks, "\"tasks\" is not a sequence");
}
for (auto&& task : tasks) {
taskvec.push_back (Task (task));
}
return taskvec;
}
}
namespace {
vector<Resource> parse_yaml_resources (const YAML::Node &resources)
{
vector<Resource> resvec;
/* "resources" must be a sequence */
if (!resources.IsSequence()) {
throw parse_error (resources, "\"resources\" is not a sequence");
}
for (auto&& resource : resources) {
resvec.push_back (Resource (resource));
}
return resvec;
}
}
Jobspec::Jobspec(const YAML::Node &top)
{
try {
/* The top yaml node of the jobspec must be a mapping */
if (!top.IsMap()) {
throw parse_error (top, "Top level of jobspec is not a mapping");
}
/* The four keys must be the following */
if (!top["version"]) {
throw parse_error (top, "Missing key \"version\" in top level mapping");
}
if (!top["resources"]) {
throw parse_error (top, "Missing key \"resource\" in top level mapping");
}
if (!top["tasks"]) {
throw parse_error (top, "Missing key \"tasks\" in top level mapping");
}
if (!top["attributes"]) {
throw parse_error (top, "Missing key \"attributes\" in top level mapping");
}
/* There must be exactly four entries in the mapping */
if (top.size() != 4) {
throw parse_error (top, "Top mapping in jobspec must have exactly four entries");
}
/* Import version */
if (!top["version"].IsScalar()) {
throw parse_error (top["version"],
"\"version\" must be an unsigned integer");
}
version = top["version"].as<unsigned int>();
if (version != 1) {
throw parse_error (top["version"],
"Only jobspec \"version\" 1 is supported");
}
/* Import attributes mappings */
YAML::Node attrs = top["attributes"];
/* allow attributes to be present and empty */
if (!attrs.IsNull()) {
if (!attrs.IsMap()) {
throw parse_error (attrs, "\"attributes\" is not a mapping");
}
if (attrs.size() > 0) {
for (auto&& i : attrs) {
if (!i.second.IsMap()) {
throw parse_error (i.second,
"value of attribute is not a mapping");
}
for (auto&& j : i.second) {
attributes[i.first.as<string>()][j.first.as<string>()] =
j.second.as<string>();
}
}
}
}
/* Import resources section */
resources = parse_yaml_resources (top["resources"]);
/* Import tasks section */
tasks = parse_yaml_tasks (top["tasks"]);
} catch (YAML::Exception& e) {
throw parse_error(e.what());
}
}
Jobspec::Jobspec(std::istream &is)
try
: Jobspec {YAML::Load (is)}
{
}
catch (YAML::Exception& e) {
throw parse_error(e.what());
}
Jobspec::Jobspec(std::string &s)
try
: Jobspec {YAML::Load (s)}
{
}
catch (YAML::Exception& e) {
throw parse_error(e.what());
}
namespace {
/*
* This class magically makes everything in 'dest' stream
* indented as long as this class is in scope. Once it
* it goes out of scope and is destroyed, the indenting
* disappears.
*/
class IndentingOStreambuf : public std::streambuf
{
std::streambuf* myDest;
bool myIsAtStartOfLine;
std::string myIndent;
std::ostream* myOwner;
protected:
virtual int overflow( int ch )
{
if (myIsAtStartOfLine && ch != '\n') {
myDest->sputn (myIndent.data(), myIndent.size());
}
myIsAtStartOfLine = ch == '\n';
return myDest->sputc (ch);
}
public:
explicit IndentingOStreambuf (std::streambuf* dest, int indent = 2)
: myDest (dest)
, myIsAtStartOfLine (true)
, myIndent (indent, ' ')
, myOwner(NULL)
{
}
explicit IndentingOStreambuf (std::ostream& dest, int indent = 2)
: myDest (dest.rdbuf ())
, myIsAtStartOfLine (true)
, myIndent (indent, ' ')
, myOwner (&dest)
{
myOwner->rdbuf (this);
}
virtual ~IndentingOStreambuf()
{
if ( myOwner != NULL ) {
myOwner->rdbuf (myDest);
}
}
};
}
std::ostream& Flux::Jobspec::operator<<(std::ostream& s, Jobspec const& jobspec)
{
s << "version: " << jobspec.version << endl;
s << "resources: " << endl;
for (auto&& resource : jobspec.resources) {
IndentingOStreambuf indent (s);
s << resource;
}
s << "tasks: " << endl;
for (auto&& task : jobspec.tasks) {
IndentingOStreambuf indent (s);
s << task;
}
s << "attributes:" << endl;
for (auto&& subattr : jobspec.attributes) {
s << " " << subattr.first << ":" << endl;
for (auto&& attr : subattr.second) {
s << " " << attr.first << " = " << attr.second << endl;
}
}
return s;
}
std::ostream& Flux::Jobspec::operator<<(std::ostream& s,
Resource const& resource)
{
s << "- type: " << resource.type << endl;
s << " count:" << endl;
s << " min: " << resource.count.min << endl;
s << " max: " << resource.count.max << endl;
s << " operator: " << resource.count.oper << endl;
s << " operand: " << resource.count.operand << endl;
if (resource.unit.size() > 0)
s << " unit: " << resource.unit << endl;
if (resource.label.size() > 0)
s << " label: " << resource.label << endl;
if (resource.id.size() > 0)
s << " id: " << resource.id << endl;
if (resource.exclusive == tristate_t::TRUE)
s << " exclusive: true" << endl;
else if (resource.exclusive == tristate_t::FALSE)
s << " exclusive: false" << endl;
if (resource.with.size() > 0) {
s << " with:" << endl;
IndentingOStreambuf indent (s, 4);
for (auto&& child_resource : resource.with) {
s << child_resource;
}
}
return s;
}
std::ostream& Flux::Jobspec::operator<<(std::ostream& s,
Task const& task)
{
bool first = true;
s << "command: [ ";
for (auto&& field : task.command) {
if (!first)
s << ", ";
else
first = false;
s << "\"" << field << "\"";
}
s << " ]" << endl;
s << "slot: " << task.slot << endl;
if (task.count.size() > 0) {
s << "count:" << endl;
IndentingOStreambuf indent (s);
for (auto&& c : task.count) {
s << c.first << ": " << c.second << endl;
}
}
if (task.distribution.size() > 0)
s << "distribution: " << task.distribution << endl;
if (task.attributes.size() > 0) {
s << "attributes:" << endl;
IndentingOStreambuf indent (s);
for (auto&& attr : task.attributes) {
s << attr.first << ": " << attr.second;
}
}
return s;
}