-
Notifications
You must be signed in to change notification settings - Fork 39
/
resource_reader_rv1exec.cpp
1061 lines (942 loc) · 35.9 KB
/
resource_reader_rv1exec.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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
* Copyright 2022 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, LICENSE)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\*****************************************************************************/
extern "C" {
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <flux/idset.h>
}
#include <map>
#include <string>
#include <jansson.h>
#include "resource/readers/resource_reader_rv1exec.hpp"
#include "resource/planner/c/planner.h"
using namespace Flux;
using namespace Flux::resource_model;
/********************************************************************************
* *
* Public RV1EXEC Resource Reader Interface *
* *
********************************************************************************/
int resource_reader_rv1exec_t::properties_t::insert (
const std::string &res_type,
const std::string &property)
{
if (m_properties.find (res_type) == m_properties.end ()) {
auto res = m_properties.insert (
std::pair<std::string,
std::map<std::string,
std::string>> (
res_type,
std::map<std::string,
std::string> ()));
if (!res.second) {
errno = ENOMEM;
return -1;
}
}
auto res2 = m_properties[res_type].insert (std::pair<std::string,
std::string> (property,
""));
if (!res2.second) {
errno = ENOMEM;
return -1;
}
return 0;
}
bool resource_reader_rv1exec_t::properties_t::exist (
const std::string &res_type)
{
return (m_properties.find (res_type) != m_properties.end ())? true : false;
}
int resource_reader_rv1exec_t::properties_t::copy (
const std::string &res_type,
std::map<std::string,
std::string> &properties)
{
int rc = -1;
if (m_properties.find (res_type) != m_properties.end ()) {
properties = m_properties[res_type];
rc = 0;
}
return rc;
}
vtx_t resource_reader_rv1exec_t::add_vertex (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent, int64_t id,
const std::string &subsys,
const std::string &type,
const std::string &basename,
const std::string &name,
const std::map<std::string,
std::string> &props,
int size, int rank)
{
planner_t *plan = nullptr;
planner_t *x_checker = nullptr;
if ( !(plan = planner_new (0, INT64_MAX, size, type.c_str ())))
return boost::graph_traits<resource_graph_t>::null_vertex ();
if ( !(x_checker = planner_new (0, INT64_MAX,
X_CHECKER_NJOBS, X_CHECKER_JOBS_STR)))
return boost::graph_traits<resource_graph_t>::null_vertex ();
vtx_t v = boost::add_vertex (g);
// Set properties of the new vertex
bool is_root = false;
if (parent == boost::graph_traits<resource_graph_t>::null_vertex ())
is_root = true;
std::string istr = (id != -1)? std::to_string (id) : "";
std::string prefix = is_root ? "" : g[parent].paths[subsys];
g[v].type = type;
g[v].basename = basename;
g[v].size = size;
g[v].uniq_id = v;
g[v].rank = rank;
g[v].schedule.plans = plan;
g[v].idata.x_checker = x_checker;
g[v].id = id;
g[v].name = (name != "")? name : basename + istr;
g[v].paths[subsys] = prefix + "/" + g[v].name;
g[v].idata.member_of[subsys] = "*";
g[v].status = resource_pool_t::status_t::UP;
g[v].properties = props;
// Indexing for fast look-up
m.by_path[g[v].paths[subsys]].push_back (v);
m.by_type[g[v].type].push_back (v);
m.by_name[g[v].name].push_back (v);
m.by_rank[rank].push_back (v);
return v;
}
int resource_reader_rv1exec_t::add_metadata (resource_graph_t &g,
resource_graph_metadata_t &m,
edg_t e, vtx_t src, vtx_t dst)
{
// add this edge to by_outedges metadata
auto iter = m.by_outedges.find (src);
if (iter == m.by_outedges.end ()) {
auto ret = m.by_outedges.insert (
std::make_pair (
src,
std::map<std::pair<uint64_t, int64_t>, edg_t,
std::greater<
std::pair<uint64_t,
int64_t>>> ()));
if (!ret.second) {
errno = ENOMEM;
m_err_msg += __FUNCTION__;
m_err_msg += "error creating out-edge metadata map: "
+ g[src].name + " -> " + g[dst].name + "; ";
return -1;
}
iter = m.by_outedges.find (src);
}
std::pair<uint64_t, int64_t> key = std::make_pair (
g[e].idata.get_weight (),
g[dst].uniq_id);
auto ret = iter->second.insert (std::make_pair (key, e));
if (!ret.second) {
errno = ENOMEM;
m_err_msg += __FUNCTION__;
m_err_msg += "error inserting an edge to out-edge metadata map: "
+ g[src].name + " -> " + g[dst].name + "; ";
return -1;
}
return 0;
}
int resource_reader_rv1exec_t::add_edges (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t src, vtx_t dst,
const std::string &subsys,
const std::string &relation,
const std::string &rev_relation)
{
edg_t e;
bool inserted;
tie (e, inserted) = add_edge (src, dst, g);
if (!inserted) {
errno = ENOMEM;
goto error;
}
g[e].idata.member_of[subsys] = relation;
g[e].name[subsys] = relation;
if (add_metadata (g, m, e, src, dst) < 0)
goto error;
tie (e, inserted) = add_edge (dst, src, g);
if (!inserted) {
errno = ENOMEM;
goto error;
}
g[e].idata.member_of[subsys] = rev_relation;
g[e].name[subsys] = rev_relation;
if (add_metadata (g, m, e, dst, src) < 0)
goto error;
return 0;
error:
return -1;
}
int resource_reader_rv1exec_t::add_cluster_vertex (resource_graph_t &g,
resource_graph_metadata_t &m)
{
vtx_t v;
const std::map<std::string, std::string> p;
if ( (v = add_vertex (g, m,
boost::graph_traits<resource_graph_t>::null_vertex (),
0, "containment", "cluster", "cluster", "", p, 1, -1))
== boost::graph_traits<resource_graph_t>::null_vertex ())
return -1;
m.roots.emplace ("containment", v);
m.v_rt_edges.emplace ("containment", relation_infra_t ());
return 0;
}
vtx_t resource_reader_rv1exec_t::find_vertex (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent, int64_t id,
const std::string &subsys,
const std::string &type,
const std::string &basename,
const std::string &name,
int size, int rank)
{
bool is_root = false;
std::string path = "";
std::string vtx_name = "";
vtx_t vtx;
// Get properties of the vertex
if (parent == boost::graph_traits<resource_graph_t>::null_vertex ())
is_root = true;
std::string idstr = (id != -1)? std::to_string (id) : "";
std::string prefix = is_root ? "" : g[parent].paths[subsys];
vtx_name = (name != "")? name : basename + idstr;
path = prefix + "/" + vtx_name;
// Search graph metadata for vertex
auto vtx_iter = m.by_path.find (path);
if (vtx_iter != m.by_path.end ()) {
for (vtx_t v : vtx_iter->second) {
if (g[v].rank == rank) {
vtx = v;
break;
}
}
} else // Not found in by_path map
return boost::graph_traits<resource_graph_t>::null_vertex ();
// Found the vertex in graph metadata; check properties are the same
if (g[vtx].id != id || g[vtx].size != size || g[vtx].type != type)
return boost::graph_traits<resource_graph_t>::null_vertex ();
return vtx;
}
int resource_reader_rv1exec_t::update_vertex (resource_graph_t &g,
vtx_t vtx,
updater_data &update_data)
{
int rc = -1;
int64_t span = -1;
int64_t avail = -1;
planner_t *plans = NULL;
// Check and update plan
if ( (plans = g[vtx].schedule.plans) == NULL) {
errno = EINVAL;
m_err_msg += __FUNCTION__;
m_err_msg += ": plan for " + g[vtx].name + " is null.\n";
goto error;
}
if ( (avail = planner_avail_resources_during (plans,
update_data.at,
update_data.duration)) == -1) {
m_err_msg += __FUNCTION__;
m_err_msg += ": planner_avail_resource_during return -1 for ";
m_err_msg += g[vtx].name + ".\n";
goto error;
}
if (avail < g[vtx].size) {
// if g[v] has already been allocated/reserved, this is an error
m_err_msg += __FUNCTION__;
m_err_msg += ": " + g[vtx].name + " is unavailable.\n";
goto error;
}
// Update the vertex plan here (not in traverser code).
// Traverser update () will handle aggregate filters and
// exclusivity checking filter.
// Can't update the rank-level (node) vertex yet- we
// don't know if all its children are allocated.
// Note this is a hard-coded option. Support for more flexible
// types may require extending rv1exec.
if (g[vtx].type != "node") {
if ( (span = planner_add_span (plans,
update_data.at,
update_data.duration,
static_cast<const uint64_t> (g[vtx].size))) == -1) {
m_err_msg += __FUNCTION__;
m_err_msg += ": can't add span into " + g[vtx].name + ".\n";
goto error;
}
if (update_data.reserved)
g[vtx].schedule.reservations[update_data.jobid] = span;
else
g[vtx].schedule.allocations[update_data.jobid] = span;
update_data.updated_vertices[g[vtx].rank].push_back (vtx);
}
rc = 0;
error:
return rc;
}
int resource_reader_rv1exec_t::undo_vertices (resource_graph_t &g,
updater_data &update_data)
{
int rc = -1;
int64_t span = -1;
planner_t *plans = NULL;
for (auto rank : update_data.updated_vertices) {
for (vtx_t vtx : update_data.updated_vertices.at (rank.first)) {
// Check plan
if ( (plans = g[vtx].schedule.plans) == NULL) {
errno = EINVAL;
m_err_msg += __FUNCTION__;
m_err_msg += ": plan for " + g[vtx].name + " is null.\n";
goto error;
}
// Remove job tags
if (update_data.reserved) {
span = g[vtx].schedule.reservations.at (update_data.jobid);
g[vtx].schedule.reservations.erase (update_data.jobid);
} else {
span = g[vtx].schedule.allocations.at (update_data.jobid);
g[vtx].schedule.allocations.erase (update_data.jobid);
}
// Remove the span.
if (planner_rem_span (plans, span) == -1) {
m_err_msg += __FUNCTION__;
m_err_msg += ": can't remove span from " + g[vtx].name + ".\n";
goto error;
}
}
}
rc = 0;
error:
return rc;
}
int resource_reader_rv1exec_t::update_edges (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t src, vtx_t dst,
const std::string &subsys,
const std::string &relation,
const std::string &rev_relation,
updater_data &update_data)
{
edg_t e;
int rc = -1;
bool found = false;
boost::graph_traits<resource_graph_t>::out_edge_iterator ei, ei_end;
boost::tie (ei, ei_end) = boost::out_edges (src, g);
for (; ei != ei_end; ++ei) {
if (boost::target (*ei, g) == dst) {
e = *ei;
found = true;
break;
}
}
if (!found) {
errno = EINVAL;
m_err_msg += __FUNCTION__;
m_err_msg += ": JGF edge not found in resource graph.\n";
goto error;
}
g[e].idata.set_for_trav_update (g[dst].size,
true, update_data.token);
return 0;
error:
return -1;
}
int resource_reader_rv1exec_t::update_exclusivity (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t vtx,
updater_data &update_data)
{
// idata tag and exclusive checker update
int64_t span = -1;
planner_t *plans = NULL;
auto rank_ex = update_data.updated_vertices.find (g[vtx].rank);
if (rank_ex == update_data.updated_vertices.end ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": rank not found in agfilters map.\n";
return -1;
}
// This check enforces a rigid constraint on rank equivalence
// between graph initialization and rank in rv1exec string.
auto by_rank = m.by_rank.find (g[vtx].rank);
if (by_rank == m.by_rank.end ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": rank not found in by_rank graph map.\n";
return -1;
}
// If all child vertices allocated, allocate this vertex.
// Subtract one since the current node hasn't been added to the
// updated_vertices map.
if (rank_ex->second.size () == (by_rank->second.size () - 1)) {
if ( (plans = g[vtx].schedule.plans) == NULL) {
errno = EINVAL;
m_err_msg += __FUNCTION__;
m_err_msg += ": plan for " + g[vtx].name + " is null.\n";
return -1;
}
// Update the vertex plan here (not in traverser code).
// Traverser update () will handle aggregate filters and
// exclusivity checking filter.
if ( (span = planner_add_span (plans, update_data.at, update_data.duration,
static_cast<const uint64_t> (g[vtx].size))) == -1) {
m_err_msg += __FUNCTION__;
m_err_msg += ": can't add span into " + g[vtx].name + ".\n";
return -1;
}
// Add job tags
if (update_data.reserved)
g[vtx].schedule.reservations[update_data.jobid] = span;
else
g[vtx].schedule.allocations[update_data.jobid] = span;
// Add to the updated vertices vector to undo upon error.
update_data.updated_vertices[g[vtx].rank].push_back (vtx);
}
return 0;
}
int resource_reader_rv1exec_t::build_rmap (json_t *rlite,
std::map<unsigned, unsigned> &rmap)
{
int i;
size_t index;
unsigned rank;
json_t *entry = nullptr;
const char *ranks = nullptr;
struct idset *ids = nullptr;
struct idset *r_ids = nullptr;
if (!rlite) {
errno = EINVAL;
goto error;
}
if ( !(ids = idset_create (0, IDSET_FLAG_AUTOGROW)))
goto error;
// Create a global rank set that combines all ranks from R_lite array
json_array_foreach (rlite, index, entry) {
if (json_unpack (entry, "{s:s}", "rank", &ranks) < 0) {
errno = EINVAL;
goto error;
}
if ( !(r_ids = idset_decode (ranks)))
goto error;
rank = idset_first (r_ids);
while (rank != IDSET_INVALID_ID) {
if (idset_set (ids, rank) < 0)
goto error;
rank = idset_next (r_ids, rank);
}
idset_destroy (r_ids);
}
// A rank map mapping mononically increasing ranks to consecutive IDs from 0
i = 0;
rank = idset_first (ids);
while (rank != IDSET_INVALID_ID) {
if (rmap.find (rank) != rmap.end ()) {
errno = EEXIST;
m_err_msg += __FUNCTION__;
m_err_msg += "rmap has no rank=" + std::to_string (rank) + "; ";
goto error;
}
auto res = rmap.insert (std::pair<unsigned, unsigned> (rank, i));
if (!res.second) {
errno = ENOMEM;
goto error;
}
rank = idset_next (ids, rank);
i++;
}
idset_destroy (ids);
return 0;
error:
idset_destroy (r_ids);
idset_destroy (ids);
return -1;
}
int resource_reader_rv1exec_t::build_pmap (json_t *properties,
std::map<unsigned,
properties_t> &pmap)
{
const char *key = nullptr;
json_t *val = nullptr;
struct idset *ranks = nullptr;
json_object_foreach (properties, key, val) {
unsigned rank = IDSET_INVALID_ID;
if (!json_is_string (val)) {
errno = EINVAL;
goto error;
}
if ( !(ranks = idset_decode (json_string_value (val)))) {
errno = ENOMEM;
goto error;
}
rank = idset_first (ranks);
while (rank != IDSET_INVALID_ID) {
std::string prop = key;
// When fine-grained resource property tagging use case comes along,
// the following code can be extended: fetching @resource_type and
// add the property to the corresponding resource_type vertices.
// For now, we just add the property to compute node vertices.
if (pmap.find (rank) != pmap.end ()) {
if (pmap[rank].insert ("node", key) < 0)
goto error;
} else {
auto res = pmap.insert (
std::pair<unsigned,
properties_t> (rank,
properties_t ()));
if (!res.second) {
errno = ENOMEM;
goto error;
}
if (pmap[rank].insert ("node", key) < 0)
goto error;
}
rank = idset_next (ranks, rank);
}
idset_destroy (ranks);
ranks = nullptr;
}
return 0;
error:
idset_destroy (ranks);
return -1;
}
int resource_reader_rv1exec_t::unpack_child (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent,
const char *resource_type,
const char *resource_ids,
unsigned rank,
std::map<unsigned,
properties_t> &pmap,
updater_data &update_data)
{
int rc = -1;
unsigned id;
struct idset *ids = nullptr;
if (!resource_type || !resource_ids) {
errno = EINVAL;
goto error;
}
if ( !(ids = idset_decode (resource_ids)))
goto error;
id = idset_first (ids);
while (id != IDSET_INVALID_ID) {
edg_t e;
vtx_t vtx;
std::string name = resource_type + std::to_string (id);
std::map<std::string, std::string> properties;
if (pmap.find (rank) != pmap.end ()) {
if (pmap[rank].exist (resource_type)) {
if (pmap[rank].copy (resource_type, properties) < 0)
goto error;
}
}
if (!update_data.update) {
// vtx = find_vertex (g, m, parent, id, "containment", resource_type,
// resource_type, name, 1, rank);
// // Shouldn't be found
// if (vtx != boost::graph_traits<resource_graph_t>::null_vertex ()) {
// m_err_msg += __FUNCTION__;
// m_err_msg += ": found duplicate vertex in graph for ";
// m_err_msg += name + ".\n";
// goto error;
// }
// Add resources
vtx = add_vertex (g, m, parent, id,
"containment", resource_type,
resource_type, name, properties, 1, rank);
if (vtx == boost::graph_traits<resource_graph_t>::null_vertex ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": failed to add vertex for ";
m_err_msg += name + ".\n";
goto error;
}
if (add_edges (g, m, parent, vtx, "containment",
"contains", "in") < 0) {
m_err_msg += __FUNCTION__;
m_err_msg += ": failed to add edges for ";
m_err_msg += name + ".\n";
goto error;
}
} else {
// Update resources
vtx = find_vertex (g, m, parent, id, "containment", resource_type,
resource_type, name, 1, rank);
// Not found
if (vtx == boost::graph_traits<resource_graph_t>::null_vertex ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": couldn't find vertex in graph for ";
m_err_msg += name + ".\n";
goto error;
}
if (update_vertex (g, vtx, update_data) == -1)
goto error;
if (update_edges (g, m, parent, vtx, "containment", "contains",
"in", update_data) == -1)
goto error;
}
id = idset_next (ids, id);
}
rc = 0;
error:
idset_destroy (ids);
return rc;
}
int resource_reader_rv1exec_t::unpack_children (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent,
json_t *children,
unsigned rank,
std::map<unsigned,
properties_t> &pmap,
updater_data &update_data)
{
json_t *res_ids = nullptr;
const char *res_type = nullptr;
if (!children) {
errno = EINVAL;
goto error;
}
json_object_foreach (children, res_type, res_ids) {
if (!json_is_string (res_ids)) {
errno = EINVAL;
goto error;
}
const char *ids_str = json_string_value (res_ids);
if (unpack_child (g, m, parent, res_type, ids_str, rank, pmap,
update_data) < 0)
goto error;
}
return 0;
error:
return -1;
}
int resource_reader_rv1exec_t::unpack_rank (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent,
unsigned rank,
json_t *children,
struct hostlist *hlist,
std::map<unsigned, unsigned> &rmap,
std::map<unsigned,
properties_t> &pmap,
updater_data &update_data)
{
edg_t e;
vtx_t vtx;
int64_t id;
const char *hostname = nullptr;
std::string basename;
std::map<std::string, std::string> properties;
if (!children || !hlist) {
errno = EINVAL;
goto error;
}
if (rmap.find (rank) == rmap.end ()) {
errno = EINVAL;
goto error;
}
if ( !(hostname = hostlist_nth (hlist, static_cast<int> (rmap[rank]))))
goto error;
if (get_hostname_suffix (hostname, id) < 0
|| get_host_basename (hostname, basename) < 0) {
m_err_msg += __FUNCTION__;
m_err_msg += ": error splitting hostname=";
m_err_msg += hostname + std::string ("; ");
goto error;
}
if (pmap.find (rank) != pmap.end ()) {
if (pmap[rank].exist ("node")) {
if (pmap[rank].copy ("node", properties) < 0)
goto error;
}
}
if (!update_data.update) {
// vtx_t tmp_vtx;
// tmp_vtx = find_vertex (g, m, parent, id, "containment", "node", basename,
// hostname, 1, rank);
// // Shouldn't be in graph
// if (tmp_vtx != boost::graph_traits<resource_graph_t>::null_vertex ()) {
// m_err_msg += __FUNCTION__;
// m_err_msg += ": found duplicate vertex in graph for ";
// m_err_msg += std::string (hostname) + ".\n";
// goto error;
// }
// Create and add a node vertex and link with cluster vertex
vtx = add_vertex (g, m, parent, id, "containment",
"node", basename, hostname, properties, 1, rank);
if (vtx == boost::graph_traits<resource_graph_t>::null_vertex ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": failed to add vertex for ";
m_err_msg += std::string (hostname) + ".\n";
goto error;
}
if (add_edges (g, m, parent, vtx, "containment", "contains", "in") < 0) {
m_err_msg += __FUNCTION__;
m_err_msg += ": failed to add edges for ";
m_err_msg += std::string (hostname) + ".\n";
goto error;
}
} else {
// Update resources
vtx = find_vertex (g, m, parent, id, "containment", "node", basename,
hostname, 1, rank);
// Not found
if (vtx == boost::graph_traits<resource_graph_t>::null_vertex ()) {
m_err_msg += __FUNCTION__;
m_err_msg += ": couldn't find vertex in graph for ";
m_err_msg += std::string (hostname) + ".\n";
goto error;
}
if (update_vertex (g, vtx, update_data) == -1)
goto error;
if (update_edges (g, m, parent, vtx, "containment", "contains",
"in", update_data) == -1)
goto error;
}
// Unpack children node-local resources
if (unpack_children (g, m, vtx, children, rank, pmap, update_data) < 0)
goto error;
if (update_data.update) {
// Update the rank's planner if all children allocated
if (update_exclusivity (g, m, vtx, update_data) == -1) {
m_err_msg += __FUNCTION__;
m_err_msg += ": exclusive filter update failed for ";
m_err_msg += std::string (hostname) + ".\n";
goto error;
}
}
return 0;
error:
return -1;
}
int resource_reader_rv1exec_t::unpack_rlite_entry (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t parent,
json_t *entry,
struct hostlist *hlist,
std::map<unsigned,
unsigned> &rmap,
std::map<unsigned,
properties_t> &pmap,
updater_data &update_data)
{
int rc = -1;
unsigned rank;
json_t *children = nullptr;
const char *ranks = nullptr;
struct idset *r_ids = nullptr;
if (!entry || !hlist) {
errno = EINVAL;
goto error;
}
if (json_unpack (entry, "{s:s s:o}",
"rank", &ranks,
"children", &children) < 0) {
errno = EINVAL;
goto error;
}
if ( !(r_ids = idset_decode (ranks)))
goto error;
rank = idset_first (r_ids);
while (rank != IDSET_INVALID_ID) {
if (unpack_rank (g, m, parent, rank, children, hlist, rmap, pmap,
update_data) < 0)
goto error;
rank = idset_next (r_ids, rank);
}
rc = 0;
error:
idset_destroy (r_ids);
return rc;
}
int resource_reader_rv1exec_t::unpack_rlite (resource_graph_t &g,
resource_graph_metadata_t &m,
json_t *rlite,
struct hostlist *hlist,
std::map<unsigned, unsigned> &rmap,
std::map<unsigned,
properties_t> &pmap,
updater_data &update_data)
{
size_t index;
vtx_t cluster_vtx;
json_t *entry = nullptr;
if (!rlite || !hlist) {
errno = EINVAL;
goto error;
}
if (m.roots.find ("containment") == m.roots.end ()) {
errno = ENOENT;
goto error;
}
cluster_vtx = m.roots["containment"];
// Set the cluster "needs" and make the update shared access to the cluster
m.v_rt_edges["containment"].set_for_trav_update (g[cluster_vtx].size,
false, update_data.token);
json_array_foreach (rlite, index, entry) {
if (unpack_rlite_entry (g, m, cluster_vtx,
entry, hlist, rmap, pmap, update_data) < 0)
goto error;
}
return 0;
error:
return -1;
}
int resource_reader_rv1exec_t::unpack_internal (resource_graph_t &g,
resource_graph_metadata_t &m,
json_t *rv1,
updater_data &update_data)
{
int rc = -1;
int version;
size_t index;
json_t *val = nullptr;
json_t *rlite = nullptr;
json_t *nodelist = nullptr;
json_t *properties = nullptr;
struct hostlist *hlist = nullptr;
std::map<unsigned, unsigned> rmap;
std::map<unsigned, properties_t> pmap;
if (json_unpack (rv1, "{s:i s:{s:o s:o s?o}}",
"version", &version,
"execution",
"R_lite", &rlite,
"nodelist", &nodelist,
"properties", &properties) < 0) {
errno = EINVAL;
goto ret;
}
if (version != 1) {
errno = EINVAL;
goto ret;
}
// Create rank-to-0-based-index mapping.
if (build_rmap (rlite, rmap) < 0)
goto ret;
if (build_pmap (properties, pmap) < 0)
goto ret;
if ( !(hlist = hostlist_create ()))
goto ret;
// Encode all nodes in nodelist array into a hostlist object.
json_array_foreach (nodelist, index, val) {
const char *hlist_str = nullptr;
if (!json_is_string (val)) {
errno = EINVAL;
goto ret;
}
if ( !(hlist_str = json_string_value (val))) {
errno = EINVAL;
goto ret;
}
if (hostlist_append (hlist, hlist_str) < 0)
goto ret;
}
if (unpack_rlite (g, m, rlite, hlist, rmap, pmap, update_data) < 0)
goto ret;
rc = 0;
ret:
hostlist_destroy (hlist);
return rc;
}
/********************************************************************************
* *
* Public RV1EXEC Resource Reader Interface *
* *
********************************************************************************/
resource_reader_rv1exec_t::~resource_reader_rv1exec_t ()
{
}
int resource_reader_rv1exec_t::unpack (resource_graph_t &g,
resource_graph_metadata_t &m,
const std::string &str, int rank)
{
int rc = -1;
json_error_t error;
json_t *rv1 = nullptr;
int saved_errno;
updater_data null_data;
// Indicate adding, not updating
null_data.update = false;
if (str == "") {
errno = EINVAL;
goto ret;
}
if (add_cluster_vertex (g, m) < 0)
goto ret;
if ( !(rv1 = json_loads (str.c_str (), 0, &error))) {
errno = ENOMEM;
goto ret;
}
rc = unpack_internal (g, m, rv1, null_data);
ret:
saved_errno = errno;
json_decref (rv1);
errno = saved_errno;
return rc;
}
int resource_reader_rv1exec_t::unpack_at (resource_graph_t &g,
resource_graph_metadata_t &m,
vtx_t &vtx,
const std::string &str, int rank)