-
Notifications
You must be signed in to change notification settings - Fork 10
/
mf_priority.cpp
1132 lines (952 loc) · 39.7 KB
/
mf_priority.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 2021 Lawrence Livermore National Security, LLC
* (c.f. AUTHORS, NOTICE.LLNS, COPYING)
*
* This file is part of the Flux resource manager framework.
* For details, see https://github.com/flux-framework.
*
* SPDX-License-Identifier: LGPL-3.0
\************************************************************/
/* mf_priority.cpp - custom basic job priority plugin
*
*/
extern "C" {
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <flux/core.h>
#include <flux/jobtap.h>
#include <jansson.h>
}
#include <map>
#include <iterator>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <cinttypes>
#include <vector>
#include <sstream>
// custom bank_info class file
#include "accounting.hpp"
// the plugin does not know about the association who submitted a job and will
// assign default values to the association until it receives information from
// flux-accounting
#define BANK_INFO_MISSING 999
// a queue is specified for a submitted job that flux-accounting does not know
// about
#define UNKNOWN_QUEUE 0
// no queue is specified for a submitted job
#define NO_QUEUE_SPECIFIED 0
// a queue was specified for a submitted job that flux-accounting knows about and
// the association does not have permission to run jobs under
#define INVALID_QUEUE -6
// different codes to return as a result of looking up user/bank information:
//
// BANK_SUCCESS: we found an entry for the passed-in user/bank
// BANK_USER_NOT_FOUND: the user could not be found in the plugin map
// BANK_INVALID: the user specified a bank they don't belong to
// BANK_NO_DEFAULT: the user does not have a default bank in the plugin map
enum bank_info_codes {
BANK_SUCCESS,
BANK_USER_NOT_FOUND,
BANK_INVALID,
BANK_NO_DEFAULT
};
typedef std::pair<bank_info_codes, std::map<std::string, Association>::iterator> bank_info_result;
std::map<int, std::map<std::string, Association>> users;
std::map<std::string, struct queue_info> queues;
std::map<int, std::string> users_def_bank;
// min_nodes_per_job, max_nodes_per_job, and max_time_per_job are not
// currently used or enforced in this plugin, so their values have no
// effect in queue limit enforcement.
struct queue_info {
int min_nodes_per_job;
int max_nodes_per_job;
int max_time_per_job;
int priority;
};
/******************************************************************************
* *
* Helper Functions *
* *
*****************************************************************************/
/*
* Calculate a user's job priority using the following factors:
*
* fairshare: the ratio between the amount of resources allocated vs. resources
* consumed.
*
* urgency: a user-controlled factor to prioritize their own jobs.
*
* queue: a factor that can further affect the priority of a job based on the
* queue passed in.
*/
int64_t priority_calculation (flux_plugin_t *p,
flux_plugin_arg_t *args,
int userid,
char *bank,
int urgency)
{
double fshare_factor = 0.0, priority = 0.0;
int queue_factor = 0;
int fshare_weight, queue_weight;
Association *b;
fshare_weight = 100000;
queue_weight = 10000;
if (urgency == FLUX_JOB_URGENCY_HOLD)
return FLUX_JOB_PRIORITY_MIN;
if (urgency == FLUX_JOB_URGENCY_EXPEDITE)
return FLUX_JOB_PRIORITY_MAX;
b = static_cast<Association *> (flux_jobtap_job_aux_get (
p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b == NULL) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB, "mf_priority",
0, "job.state.priority: bank info is " \
"missing");
return -1;
}
fshare_factor = b->fairshare;
queue_factor = b->queue_factor;
priority = round ((fshare_weight * fshare_factor) +
(queue_weight * queue_factor) +
(urgency - 16));
if (priority < 0)
return FLUX_JOB_PRIORITY_MIN;
return priority;
}
static int get_queue_info (char *queue,
std::vector<std::string> permissible_queues)
{
std::map<std::string, struct queue_info>::iterator q_it;
// make sure that if a queue is passed in, it is a valid queue for the
// user to run jobs in
if (queue != NULL) {
// check #1) the queue passed in exists in the queues map;
// if the queue cannot be found, this means that flux-accounting
// does not know about the queue, and thus should return a default
// factor
q_it = queues.find (queue);
if (q_it == queues.end ())
return UNKNOWN_QUEUE;
// check #2) the queue passed in is a valid option to pass for user
std::vector<std::string>::iterator vect_it;
vect_it = std::find (permissible_queues.begin (),
permissible_queues.end (), queue);
if (vect_it == permissible_queues.end ())
return INVALID_QUEUE;
else
// add priority associated with the passed in queue to bank_info
return queues[queue].priority;
} else {
// no queue was specified, so just use a default queue factor
return NO_QUEUE_SPECIFIED;
}
}
static void split_string (char *queues, Association *b)
{
std::stringstream s_stream;
s_stream << queues; // create string stream from string
while (s_stream.good ()) {
std::string substr;
getline (s_stream, substr, ','); // get string delimited by comma
b->queues.push_back (substr);
}
}
int check_queue_factor (flux_plugin_t *p,
int queue_factor,
char *queue,
char *prefix = (char *) "")
{
if (queue_factor == INVALID_QUEUE) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"%sQueue not valid for user: %s",
prefix, queue);
return -1;
}
return 0;
}
// Scan the users map and look at each user's default bank to see if any one
// of them have a valid bank (i.e one that is not "DNE"; if any of the users do
// do have a valid bank, it will return false)
static bool check_map_for_dne_only ()
{
// the users map iterated through in this for-loop, along with the
// users_def_bank map used to look up a user's default bank, are
// both global variables
for (const auto& entry : users) {
auto def_bank_it = users_def_bank.find(entry.first);
if (def_bank_it != users_def_bank.end() &&
def_bank_it->second != "DNE")
return false;
}
return true;
}
/*
* Update the jobspec with the default bank the association used to
* submit their job under.
*/
static int update_jobspec_bank (flux_plugin_t *p, int userid)
{
char *bank = NULL;
std::map<int, std::map<std::string, Association>>::iterator it;
it = users.find (userid);
if (it == users.end ()) {
return -1;
}
// look up default bank
bank = const_cast<char*> (users_def_bank[userid].c_str ());
// post jobspec-update event
if (flux_jobtap_jobspec_update_pack (p,
"{s:s}",
"attributes.system.bank",
bank) < 0)
return -1;
return 0;
}
// Given a userid and an optional bank, locate the associated information in
// the plugin's internal users map. The return value is a pair: the first value
// is a return code to indicate success or the type of failure, and the second
// value is an iterator that points to the appropriate user/bank's information
// associated with the submitted job
static bank_info_result get_bank_info (int userid, char *bank)
{
std::map<int, std::map<std::string, Association>>::iterator it;
std::map<std::string, Association>::iterator bank_it;
it = users.find (userid);
if (it == users.end ()) {
return {BANK_USER_NOT_FOUND, bank_it};
}
// make sure user belongs to bank they specified; if no bank was passed in,
// look up their default bank
if (bank != NULL) {
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ())
return {BANK_INVALID, bank_it};
} else {
bank = const_cast<char*> (users_def_bank[userid].c_str ());
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ())
return {BANK_NO_DEFAULT, bank_it};
}
return {BANK_SUCCESS, bank_it};
}
/******************************************************************************
* *
* Callbacks *
* *
*****************************************************************************/
/*
* Get state of all user and bank information from plugin
*/
static int query_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
flux_t *h = flux_jobtap_get_flux (p);
json_t *accounting_data = convert_map_to_json (users);
if (!accounting_data)
return -1;
if (flux_plugin_arg_pack (args,
FLUX_PLUGIN_ARG_OUT,
"{s:O}",
"mf_priority_map",
accounting_data) < 0)
flux_log_error (flux_jobtap_get_flux (p),
"mf_priority: query_cb: flux_plugin_arg_pack: %s",
flux_plugin_arg_strerror (args));
json_decref (accounting_data);
return 0;
}
/*
* Unpack a payload from an external bulk update service and place it in the
* multimap datastructure.
*/
static void rec_update_cb (flux_t *h,
flux_msg_handler_t *mh,
const flux_msg_t *msg,
void *arg)
{
char *bank, *def_bank, *queues = NULL;
int uid, max_running_jobs, max_active_jobs = 0;
double fshare = 0.0;
json_t *data, *jtemp = NULL;
json_error_t error;
int num_data = 0;
int active = 1;
std::stringstream s_stream;
if (flux_request_unpack (msg, NULL, "{s:o}", "data", &data) < 0) {
flux_log_error (h, "failed to unpack custom_priority.trigger msg");
goto error;
}
if (flux_respond (h, msg, NULL) < 0)
flux_log_error (h, "flux_respond");
if (!data || !json_is_array (data)) {
flux_log (h, LOG_ERR, "mf_priority: invalid bulk_update payload");
goto error;
}
num_data = json_array_size (data);
for (int i = 0; i < num_data; i++) {
json_t *el = json_array_get(data, i);
if (json_unpack_ex (el, &error, 0,
"{s:i, s:s, s:s, s:F, s:i, s:i, s:s, s:i}",
"userid", &uid,
"bank", &bank,
"def_bank", &def_bank,
"fairshare", &fshare,
"max_running_jobs", &max_running_jobs,
"max_active_jobs", &max_active_jobs,
"queues", &queues,
"active", &active) < 0)
flux_log (h, LOG_ERR, "mf_priority unpack: %s", error.text);
Association *b;
b = &users[uid][bank];
b->bank_name = bank;
b->fairshare = fshare;
b->max_run_jobs = max_running_jobs;
b->max_active_jobs = max_active_jobs;
b->active = active;
// split queues comma-delimited string and add it to b->queues vector
b->queues.clear ();
split_string (queues, b);
users_def_bank[uid] = def_bank;
}
return;
error:
flux_respond_error (h, msg, errno, flux_msg_last_error (msg));
}
/*
* Unpack a payload from an external bulk update service and place it in the
* multimap datastructure.
*/
static void rec_q_cb (flux_t *h,
flux_msg_handler_t *mh,
const flux_msg_t *msg,
void *arg)
{
char *queue = NULL;
int min_nodes_per_job, max_nodes_per_job, max_time_per_job, priority = 0;
json_t *data, *jtemp = NULL;
json_error_t error;
int num_data = 0;
if (flux_request_unpack (msg, NULL, "{s:o}", "data", &data) < 0) {
flux_log_error (h, "failed to unpack custom_priority.trigger msg");
goto error;
}
if (flux_respond (h, msg, NULL) < 0)
flux_log_error (h, "flux_respond");
if (!data || !json_is_array (data)) {
flux_log (h, LOG_ERR, "mf_priority: invalid queue info payload");
goto error;
}
num_data = json_array_size (data);
// clear queues map
queues.clear ();
for (int i = 0; i < num_data; i++) {
json_t *el = json_array_get(data, i);
if (json_unpack_ex (el, &error, 0,
"{s:s, s:i, s:i, s:i, s:i}",
"queue", &queue,
"min_nodes_per_job", &min_nodes_per_job,
"max_nodes_per_job", &max_nodes_per_job,
"max_time_per_job", &max_time_per_job,
"priority", &priority) < 0)
flux_log (h, LOG_ERR, "mf_priority unpack: %s", error.text);
struct queue_info *q;
q = &queues[queue];
q->min_nodes_per_job = min_nodes_per_job;
q->max_nodes_per_job = max_nodes_per_job;
q->max_time_per_job = max_time_per_job;
q->priority = priority;
}
return;
error:
flux_respond_error (h, msg, errno, flux_msg_last_error (msg));
}
static void reprior_cb (flux_t *h,
flux_msg_handler_t *mh,
const flux_msg_t *msg,
void *arg)
{
flux_plugin_t *p = (flux_plugin_t*) arg;
if (flux_jobtap_reprioritize_all (p) < 0)
goto error;
if (flux_respond (h, msg, NULL) < 0)
flux_log_error (h, "flux_respond");
return;
error:
flux_respond_error (h, msg, errno, flux_msg_last_error (msg));
}
/*
* Unpack the urgency and userid from a submitted job and call
* priority_calculation (), which will return a new job priority to be packed.
*/
static int priority_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
int urgency, userid;
char *bank = NULL;
char *queue = NULL;
int64_t priority;
Association *b;
flux_t *h = flux_jobtap_get_flux (p);
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i, s:i, s{s{s{s?s, s?s}}}}",
"urgency", &urgency,
"userid", &userid,
"jobspec", "attributes", "system",
"bank", &bank, "queue", &queue) < 0) {
flux_log (h,
LOG_ERR,
"flux_plugin_arg_unpack: %s",
flux_plugin_arg_strerror (args));
return -1;
}
b = static_cast<Association *> (flux_jobtap_job_aux_get (
p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b == NULL) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB, "mf_priority",
0, "internal error: bank info is missing");
return -1;
}
std::map<int, std::map<std::string, Association>>::iterator it;
std::map<std::string, Association>::iterator bank_it;
if (b->max_run_jobs == BANK_INFO_MISSING) {
// try to look up user again
it = users.find (userid);
if (it == users.end () || check_map_for_dne_only () == true) {
// the plugin could still be waiting on flux-accounting data
// to be loaded in; keep the job in PRIORITY state
return flux_jobtap_priority_unavail (p, args);
} else {
// make sure user belongs to bank they specified; if no bank was
// passed in, look up their default bank
if (bank != NULL) {
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ()) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"not a member of %s", bank);
return -1;
}
} else {
bank = const_cast<char*> (users_def_bank[userid].c_str ());
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ()) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"user/default bank entry "
"does not exist");
return -1;
}
}
if (bank_it->second.max_run_jobs == BANK_INFO_MISSING) {
return flux_jobtap_priority_unavail (p, args);
}
// fetch priority associated with passed-in queue (or default queue)
bank_it->second.queue_factor = get_queue_info (queue,
bank_it->second.queues);
if (check_queue_factor (p,
bank_it->second.queue_factor,
queue) < 0)
return -1;
// if we get here, the bank was unknown when this job was first
// accepted, and therefore the active job counts for this
// job need to be incremented here
bank_it->second.cur_active_jobs++;
// update current job with user/bank information
if (flux_jobtap_job_aux_set (p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info",
&bank_it->second,
NULL) < 0)
flux_log_error (h, "flux_jobtap_job_aux_set");
// now that we know the user/bank info associated with this job,
// we need to update jobspec with the default bank used to
// submit this job under
if (update_jobspec_bank (p, userid) < 0) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"failed to update jobspec "
"with bank name");
return -1;
}
}
}
priority = priority_calculation (p, args, userid, bank, urgency);
if (flux_plugin_arg_pack (args,
FLUX_PLUGIN_ARG_OUT,
"{s:I}",
"priority",
priority) < 0) {
flux_log (h,
LOG_ERR,
"flux_plugin_arg_pack: %s",
flux_plugin_arg_strerror (args));
return -1;
}
return 0;
}
static void add_missing_bank_info (flux_plugin_t *p, flux_t *h, int userid)
{
Association *b;
b = &users[userid]["DNE"];
users_def_bank[userid] = "DNE";
b->bank_name = "DNE";
b->fairshare = 0.1;
b->max_run_jobs = BANK_INFO_MISSING;
b->cur_run_jobs = 0;
b->max_active_jobs = 1000;
b->cur_active_jobs = 0;
b->active = 1;
b->held_jobs = std::vector<long int>();
if (flux_jobtap_job_aux_set (p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info",
b,
NULL) < 0)
flux_log_error (h, "flux_jobtap_job_aux_set");
}
/*
* Perform basic validation of a user/bank's submitted job. If a bank or
* queue is specified on submission, ensure that the user is allowed to
* submit a job under them. Check the active job limits for the user/bank
* on submission as well to make sure that they are under this limit when
* the job is submitted.
*
* This callback will also make sure that the user/bank belongs to
* the flux-accounting DB; there are two behaviors supported here:
*
* if the plugin has SOME data about users/banks and the user does not have
* an entry in the plugin, the job will be rejected.
*
* if the plugin has NO data about users/banks and the user does not have an
* entry in the plugin, the job will be held until data is received by the
* plugin.
*/
static int validate_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
int userid;
char *bank = NULL;
char *queue = NULL;
flux_job_state_t state;
int max_run_jobs, cur_active_jobs, max_active_jobs, queue_factor = 0;
double fairshare = 0.0;
bool only_dne_data;
Association *a;
// unpack the attributes of the user/bank's submitted job when it
// enters job.validate and place them into their respective variables
flux_t *h = flux_jobtap_get_flux (p);
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i, s:i, s{s{s{s?s, s?s}}}}",
"userid", &userid,
"state", &state,
"jobspec", "attributes", "system",
"bank", &bank, "queue", &queue) < 0) {
return flux_jobtap_reject_job (p, args, "unable to unpack bank arg");
}
// perform a lookup in the users map of the unpacked user/bank
a = get_association (userid, bank, users, users_def_bank);
if (a == nullptr) {
// the assocation could not be found in the plugin's internal map,
// so perform a check to see if the map has any loaded
// flux-accounting data before rejecting the job
bool only_dne_data = check_map_for_dne_only ();
if (users.empty () || only_dne_data) {
add_missing_bank_info (p, h, userid);
return 0;
} else {
return flux_jobtap_reject_job (p,
args,
"cannot find user/bank or "
"user/default bank entry "
"for uid: %i", userid);
}
}
if (a->active == 0)
// the association entry was disabled; reject the job
return flux_jobtap_reject_job (p, args, "user/bank entry has been "
"disabled from flux-accounting DB");
if (get_queue_info (queue, a->queues) == INVALID_QUEUE)
// the user/bank specified a queue that they do not belong to;
// reject the job
return flux_jobtap_reject_job (p, args, "Queue not valid for user: %s",
queue);
cur_active_jobs = a->cur_active_jobs;
max_active_jobs = a->max_active_jobs;
if (state == FLUX_JOB_STATE_NEW) {
if (max_active_jobs > 0 && cur_active_jobs >= max_active_jobs)
// the association is already at their max_active_jobs limit;
// reject the job
return flux_jobtap_reject_job (p,
args,
"user has max active jobs");
}
return 0;
}
static int new_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
int userid;
char *bank = NULL;
char *queue = NULL;
int max_run_jobs, cur_active_jobs, max_active_jobs = 0;
double fairshare = 0.0;
Association *b;
std::map<int, std::map<std::string, Association>>::iterator it;
std::map<std::string, Association>::iterator bank_it;
flux_t *h = flux_jobtap_get_flux (p);
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i, s{s{s{s?s, s?s}}}}",
"userid", &userid,
"jobspec", "attributes", "system",
"bank", &bank, "queue", &queue) < 0) {
return flux_jobtap_reject_job (p, args, "unable to unpack bank arg");
}
b = static_cast<Association *> (flux_jobtap_job_aux_get (
p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b != NULL) {
max_run_jobs = b->max_run_jobs;
fairshare = b->fairshare;
cur_active_jobs = b->cur_active_jobs;
max_active_jobs = b->max_active_jobs;
} else {
// make sure user belongs to flux-accounting DB
it = users.find (userid);
if (it == users.end ()) {
// user does not exist in internal map yet, so create a bank_info
// struct that signifies it's going to be held in PRIORITY
add_missing_bank_info (p, h, userid);
return 0;
}
// make sure user belongs to bank they specified; if no bank was passed
// in, look up their default bank
if (bank != NULL) {
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ()) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"job.new: not a member of %s",
bank);
return -1;
}
} else {
bank = const_cast<char*> (users_def_bank[userid].c_str ());
bank_it = it->second.find (std::string (bank));
if (bank_it == it->second.end ()) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"job.new: user/default bank "
"entry does not exist");
return -1;
}
// update jobspec with default bank
if (update_jobspec_bank (p, userid) < 0) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"failed to update jobspec "
"with bank name");
return -1;
}
}
max_run_jobs = bank_it->second.max_run_jobs;
fairshare = bank_it->second.fairshare;
cur_active_jobs = bank_it->second.cur_active_jobs;
max_active_jobs = bank_it->second.max_active_jobs;
b = &bank_it->second;
}
// assign priority associated with validated queue to bank_info struct
// associated with job
b->queue_factor = get_queue_info (queue, b->queues);
// if a user/bank has reached their max_active_jobs limit, subsequently
// submitted jobs will be rejected
if (max_active_jobs > 0 && cur_active_jobs >= max_active_jobs)
return flux_jobtap_reject_job (p, args, "user has max active jobs");
// special case where the user/bank bank_info struct is set to NULL; used
// for testing the "if (b == NULL)" checks
if (max_run_jobs == -1) {
if (flux_jobtap_job_aux_set (p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info",
NULL,
NULL) < 0)
flux_log_error (h, "flux_jobtap_job_aux_set");
return 0;
}
if (flux_jobtap_job_aux_set (p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info",
b,
NULL) < 0)
flux_log_error (h, "flux_jobtap_job_aux_set");
b->cur_active_jobs++;
return 0;
}
static int depend_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
int userid;
long int id;
Association *b;
flux_t *h = flux_jobtap_get_flux (p);
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i, s:I}",
"userid", &userid, "id", &id) < 0) {
flux_log (h,
LOG_ERR,
"flux_plugin_arg_unpack: %s",
flux_plugin_arg_strerror (args));
return -1;
}
b = static_cast<Association *> (flux_jobtap_job_aux_get (
p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b == NULL) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB, "mf_priority",
0, "job.state.depend: bank info is " \
"missing");
return -1;
}
// if user has already hit their max running jobs count, add a job
// dependency to hold job until an already running job has finished
if ((b->max_run_jobs > 0) && (b->cur_run_jobs == b->max_run_jobs)) {
if (flux_jobtap_dependency_add (p,
id,
"max-running-jobs-user-limit") < 0) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0, "failed to add " \
"job dependency");
return -1;
}
b->held_jobs.push_back (id);
}
return 0;
}
static int run_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
int userid;
Association *b;
b = static_cast<Association *>
(flux_jobtap_job_aux_get (p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b == NULL) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB, "mf_priority",
0, "job.state.run: bank info is " \
"missing");
return -1;
}
// increment the user's current running jobs count
b->cur_run_jobs++;
return 0;
}
/*
* apply an update on a job with regard to its queue once it has been
* validated.
*/
static int job_updated (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
std::map<std::string, Association>::iterator bank_it;
int userid;
char *bank = NULL;
char *queue = NULL;
Association *b;
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i, s{s{s{s?s}}}, s:{s?s}}",
"userid", &userid,
"jobspec", "attributes", "system", "bank", &bank,
"updates",
"attributes.system.queue", &queue) < 0)
return flux_jobtap_error (p, args, "unable to unpack plugin args");
// grab bank_info struct for user/bank (if any)
b = static_cast<Association *> (flux_jobtap_job_aux_get (
p,
FLUX_JOBTAP_CURRENT_JOB,
"mf_priority:bank_info"));
if (b == NULL) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB, "mf_priority",
0, "job.update: bank info is missing");
return -1;
}
// look up user/bank info based on unpacked information
bank_info_result lookup_result = get_bank_info (userid, bank);
if (lookup_result.first == BANK_USER_NOT_FOUND) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"job.update: cannot find info for user: ",
userid);
} else if (lookup_result.first == BANK_INVALID) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"job.update: not a member of %s",
bank);
} else if (lookup_result.first == BANK_NO_DEFAULT) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"mf_priority", 0,
"job.update: user/default bank "
"entry does not exist");
} else if (lookup_result.first == BANK_SUCCESS) {
bank_it = lookup_result.second;
}
// if the queue for the job has been updated, fetch the priority of the
// validated queue and assign it to the associated bank_info struct
if (queue != NULL) {
int queue_factor = get_queue_info (queue, bank_it->second.queues);
b->queue_factor = queue_factor;
}
return 0;
}
/*
* check for an updated queue and validate it for a user/bank; if the
* user/bank does not have access to the queue they are trying to update
* their job for, reject the update and keep the job in its current queue.
*/
static int update_queue_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *data)
{
std::map<std::string, Association>::iterator bank_it;
int userid;
char *bank = NULL;
char *queue = NULL;
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,