-
Notifications
You must be signed in to change notification settings - Fork 631
/
assoc_mgr.c
7636 lines (6504 loc) · 196 KB
/
assoc_mgr.c
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
/*****************************************************************************\
* assoc_mgr.c - File to keep track of associations/QOS used by the daemons
*****************************************************************************
* Copyright (C) 2004-2007 The Regents of the University of California.
* Copyright (C) 2008-2009 Lawrence Livermore National Security.
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
* Written by Danny Auble <da@llnl.gov>
*
* This file is part of Slurm, a resource management program.
* For details, see <https://slurm.schedmd.com/>.
* Please also read the included file: DISCLAIMER.
*
* Slurm 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.
*
* In addition, as a special exception, the copyright holders give permission
* to link the code of portions of this program with the OpenSSL library under
* certain conditions as described in each individual source file, and
* distribute linked combinations including the two. You must obey the GNU
* General Public License in all respects for all of the code used other than
* OpenSSL. If you modify file(s) with this exception, you may extend this
* exception to your version of the file(s), but you are not obligated to do
* so. If you do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source files in
* the program, then also delete it here.
*
* Slurm 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 GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along
* with Slurm; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
\*****************************************************************************/
#include "assoc_mgr.h"
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <ctype.h>
#include "src/common/slurmdbd_pack.h"
#include "src/common/uid.h"
#include "src/common/xstring.h"
#include "src/interfaces/gres.h"
#include "src/interfaces/priority.h"
#include "src/slurmdbd/read_config.h"
#define ASSOC_HASH_SIZE 1000
#define ASSOC_HASH_ID_INX(_assoc_id) (_assoc_id % ASSOC_HASH_SIZE)
typedef struct {
char *req;
list_t *ret_list;
} find_coord_t;
typedef struct {
bool locked;
bool relative;
uint64_t *relative_tres_cnt;
uint64_t **tres_cnt;
} foreach_tres_pos_t;
slurmdb_assoc_rec_t *assoc_mgr_root_assoc = NULL;
uint32_t g_qos_max_priority = 0;
uint32_t g_assoc_max_priority = 0;
uint32_t g_qos_count = 0;
uint32_t g_user_assoc_count = 0;
uint32_t g_tres_count = 0;
List assoc_mgr_tres_list = NULL;
slurmdb_tres_rec_t **assoc_mgr_tres_array = NULL;
char **assoc_mgr_tres_name_array = NULL;
List assoc_mgr_assoc_list = NULL;
List assoc_mgr_coord_list = NULL;
List assoc_mgr_res_list = NULL;
List assoc_mgr_qos_list = NULL;
List assoc_mgr_user_list = NULL;
List assoc_mgr_wckey_list = NULL;
static int setup_children = 0;
static pthread_rwlock_t assoc_mgr_locks[ASSOC_MGR_ENTITY_COUNT];
static pthread_mutex_t assoc_lock_init = PTHREAD_MUTEX_INITIALIZER;
static assoc_init_args_t init_setup;
static slurmdb_assoc_rec_t **assoc_hash_id = NULL;
static slurmdb_assoc_rec_t **assoc_hash = NULL;
static int *assoc_mgr_tres_old_pos = NULL;
static bool _running_cache(void)
{
if (init_setup.running_cache &&
(*init_setup.running_cache != RUNNING_CACHE_STATE_NOTRUNNING))
return true;
return false;
}
static int _get_str_inx(char *name)
{
int j, index = 0;
if (!name)
return 0;
for (j = 1; *name; name++, j++)
index += (int)tolower(*name) * j;
return index;
}
static int _assoc_hash_index(slurmdb_assoc_rec_t *assoc)
{
int index;
xassert(assoc);
/* Multiply each character by its numerical position in the
* name string to add a bit of entropy.
*/
index = assoc->uid;
/* only set on the slurmdbd */
if (slurmdbd_conf && assoc->cluster)
index += _get_str_inx(assoc->cluster);
if (assoc->acct)
index += _get_str_inx(assoc->acct);
if (assoc->partition)
index += _get_str_inx(assoc->partition);
index %= ASSOC_HASH_SIZE;
if (index < 0)
index += ASSOC_HASH_SIZE;
return index;
}
static void _add_assoc_hash(slurmdb_assoc_rec_t *assoc)
{
int inx = ASSOC_HASH_ID_INX(assoc->id);
if (!assoc_hash_id)
assoc_hash_id = xcalloc(ASSOC_HASH_SIZE,
sizeof(slurmdb_assoc_rec_t *));
if (!assoc_hash)
assoc_hash = xcalloc(ASSOC_HASH_SIZE,
sizeof(slurmdb_assoc_rec_t *));
assoc->assoc_next_id = assoc_hash_id[inx];
assoc_hash_id[inx] = assoc;
inx = _assoc_hash_index(assoc);
assoc->assoc_next = assoc_hash[inx];
assoc_hash[inx] = assoc;
}
static slurmdb_assoc_rec_t *_find_assoc_rec_id(uint32_t assoc_id,
char *cluster_name)
{
slurmdb_assoc_rec_t *assoc;
if (!assoc_hash_id) {
debug2("%s: no associations added yet", __func__);
return NULL;
}
assoc = assoc_hash_id[ASSOC_HASH_ID_INX(assoc_id)];
while (assoc) {
if ((!slurmdbd_conf ||
!xstrcmp(cluster_name, assoc->cluster)) &&
(assoc->id == assoc_id))
return assoc;
assoc = assoc->assoc_next_id;
}
return NULL;
}
static int _find_acct_by_name(void *x, void *y)
{
slurmdb_coord_rec_t *acct = (slurmdb_coord_rec_t*) x;
if (!xstrcmp(acct->name, (char*)y))
return 1;
return 0;
}
extern int assoc_mgr_find_nondirect_coord_by_name(void *x, void *y)
{
slurmdb_coord_rec_t *acct = x;
if (acct->direct)
return 0;
return _find_acct_by_name(x, y);
}
/*
* _find_assoc_rec - return a pointer to the assoc_ptr with the given
* contents of assoc.
* IN assoc - requested association info
* RET pointer to the assoc_ptr's record, NULL on error
*/
static slurmdb_assoc_rec_t *_find_assoc_rec(
slurmdb_assoc_rec_t *assoc)
{
slurmdb_assoc_rec_t *assoc_ptr;
int inx;
/* We can only use _find_assoc_rec_id if we are not on the slurmdbd */
if (assoc->id)
return _find_assoc_rec_id(assoc->id, assoc->cluster);
if (!assoc_hash) {
debug2("%s: no associations added yet", __func__);
return NULL;
}
inx = _assoc_hash_index(assoc);
assoc_ptr = assoc_hash[inx];
while (assoc_ptr) {
if ((!assoc->user && (assoc->uid == NO_VAL))
&& (assoc_ptr->user || (assoc_ptr->uid != NO_VAL))) {
debug3("%s: we are looking for a nonuser association",
__func__);
goto next;
} else if ((!assoc_ptr->user && (assoc_ptr->uid == NO_VAL))
&& (assoc->user || (assoc->uid != NO_VAL))) {
debug3("%s: we are looking for a user association",
__func__);
goto next;
} else if (assoc->user && assoc_ptr->user
&& ((assoc->uid == NO_VAL) ||
(assoc_ptr->uid == NO_VAL))) {
/* This means the uid isn't set in one of the
* associations, so use the name instead
*/
if (xstrcasecmp(assoc->user, assoc_ptr->user)) {
debug3("%s: 2 not the right user %u != %u",
__func__, assoc->uid, assoc_ptr->uid);
goto next;
}
} else if (assoc->uid != assoc_ptr->uid) {
debug3("%s: not the right user %u != %u",
__func__, assoc->uid, assoc_ptr->uid);
goto next;
}
if (assoc->acct &&
(!assoc_ptr->acct
|| xstrcasecmp(assoc->acct, assoc_ptr->acct))) {
debug3("%s: not the right account %s != %s",
__func__, assoc->acct, assoc_ptr->acct);
goto next;
}
/* only check for on the slurmdbd */
if (slurmdbd_conf && assoc->cluster
&& (!assoc_ptr->cluster
|| xstrcasecmp(assoc->cluster, assoc_ptr->cluster))) {
debug3("%s: not the right cluster", __func__);
goto next;
}
if (assoc->partition
&& (!assoc_ptr->partition
|| xstrcasecmp(assoc->partition,
assoc_ptr->partition))) {
debug3("%s: not the right partition", __func__);
goto next;
}
break;
next:
assoc_ptr = assoc_ptr->assoc_next;
}
return assoc_ptr;
}
/*
* _list_delete_assoc - delete a assoc record
* IN assoc_entry - pointer to assoc_record to delete
* global: assoc_list - pointer to global assoc list
* assoc_count - count of assoc list entries
* assoc_hash - hash table into assoc records
*/
static void _delete_assoc_hash(slurmdb_assoc_rec_t *assoc)
{
slurmdb_assoc_rec_t *assoc_ptr = assoc;
slurmdb_assoc_rec_t **assoc_pptr;
xassert(assoc);
/* Remove the record from assoc hash table */
assoc_pptr = &assoc_hash_id[ASSOC_HASH_ID_INX(assoc_ptr->id)];
while (assoc_pptr && ((assoc_ptr = *assoc_pptr) != assoc)) {
if (!assoc_ptr->assoc_next_id)
assoc_pptr = NULL;
else
assoc_pptr = &assoc_ptr->assoc_next_id;
}
if (!assoc_pptr) {
fatal("assoc id hash error");
return; /* Fix CLANG false positive error */
} else
*assoc_pptr = assoc_ptr->assoc_next_id;
assoc_ptr = assoc;
assoc_pptr = &assoc_hash[_assoc_hash_index(assoc_ptr)];
while (assoc_pptr && ((assoc_ptr = *assoc_pptr) != assoc)) {
if (!assoc_ptr->assoc_next)
assoc_pptr = NULL;
else
assoc_pptr = &assoc_ptr->assoc_next;
}
if (!assoc_pptr) {
fatal("assoc hash error");
return; /* Fix CLANG false positive error */
} else
*assoc_pptr = assoc_ptr->assoc_next;
}
static void _normalize_assoc_shares_fair_tree(
slurmdb_assoc_rec_t *assoc)
{
slurmdb_assoc_rec_t *fs_assoc = assoc;
double shares_norm = 0.0;
if ((assoc->shares_raw == SLURMDB_FS_USE_PARENT)
&& assoc->usage->fs_assoc_ptr)
fs_assoc = assoc->usage->fs_assoc_ptr;
if (fs_assoc->usage->level_shares)
shares_norm =
(double)fs_assoc->shares_raw /
(double)fs_assoc->usage->level_shares;
assoc->usage->shares_norm = shares_norm;
}
/* you should check for assoc == NULL before this function */
static void _normalize_assoc_shares_traditional(
slurmdb_assoc_rec_t *assoc)
{
slurmdb_assoc_rec_t *assoc2 = assoc;
xassert(assoc);
if ((assoc->shares_raw == SLURMDB_FS_USE_PARENT)
&& assoc->usage->fs_assoc_ptr) {
debug3("assoc %u(%s %s) normalize = %f from parent %u(%s %s)",
assoc->id, assoc->acct, assoc->user,
assoc->usage->fs_assoc_ptr->usage->shares_norm,
assoc->usage->fs_assoc_ptr->id,
assoc->usage->fs_assoc_ptr->acct,
assoc->usage->fs_assoc_ptr->user);
assoc->usage->shares_norm =
assoc->usage->fs_assoc_ptr->usage->shares_norm;
return;
}
assoc2->usage->shares_norm = 1.0;
while (assoc->usage->parent_assoc_ptr) {
if (assoc->shares_raw != SLURMDB_FS_USE_PARENT) {
if (!assoc->usage->level_shares)
assoc2->usage->shares_norm = 0;
else
assoc2->usage->shares_norm *=
(double)assoc->shares_raw /
(double)assoc->usage->level_shares;
debug3("assoc %u(%s %s) normalize = %f "
"from %u(%s %s) %u / %u = %f",
assoc2->id, assoc2->acct, assoc2->user,
assoc2->usage->shares_norm,
assoc->id, assoc->acct, assoc->user,
assoc->shares_raw,
assoc->usage->level_shares,
assoc->usage->level_shares ?
(double)assoc->shares_raw /
(double)assoc->usage->level_shares :
0);
}
assoc = assoc->usage->parent_assoc_ptr;
}
}
static int _addto_used_info(slurmdb_assoc_usage_t *usage1,
slurmdb_assoc_usage_t *usage2)
{
int i;
if (!usage1 || !usage2)
return SLURM_ERROR;
for (i=0; i < usage1->tres_cnt; i++) {
usage1->grp_used_tres[i] +=
usage2->grp_used_tres[i];
usage1->grp_used_tres_run_secs[i] +=
usage2->grp_used_tres_run_secs[i];
usage1->usage_tres_raw[i] +=
usage2->usage_tres_raw[i];
}
usage1->accrue_cnt += usage2->accrue_cnt;
usage1->grp_used_wall += usage2->grp_used_wall;
usage1->used_jobs += usage2->used_jobs;
usage1->used_submit_jobs += usage2->used_submit_jobs;
usage1->usage_raw += usage2->usage_raw;
slurmdb_merge_grp_node_usage(&usage1->grp_node_bitmap,
&usage1->grp_node_job_cnt,
usage2->grp_node_bitmap,
usage2->grp_node_job_cnt);
return SLURM_SUCCESS;
}
static int _clear_used_assoc_info(slurmdb_assoc_rec_t *assoc)
{
int i;
if (!assoc || !assoc->usage)
return SLURM_ERROR;
for (i=0; i<assoc->usage->tres_cnt; i++) {
assoc->usage->grp_used_tres[i] = 0;
assoc->usage->grp_used_tres_run_secs[i] = 0;
}
assoc->usage->accrue_cnt = 0;
assoc->usage->used_jobs = 0;
assoc->usage->used_submit_jobs = 0;
if (assoc->usage->grp_node_bitmap)
bit_clear_all(assoc->usage->grp_node_bitmap);
if (assoc->usage->grp_node_job_cnt)
memset(assoc->usage->grp_node_job_cnt, 0,
sizeof(uint16_t) * node_record_count);
/* do not reset usage_raw or grp_used_wall.
* if you need to reset it do it
* else where since sometimes we call this and do not want
* shares reset */
return SLURM_SUCCESS;
}
static void _clear_qos_used_limit_list(List used_limit_list, uint32_t tres_cnt)
{
slurmdb_used_limits_t *used_limits = NULL;
list_itr_t *itr = NULL;
int i;
if (!used_limit_list || !list_count(used_limit_list))
return;
itr = list_iterator_create(used_limit_list);
while ((used_limits = list_next(itr))) {
used_limits->accrue_cnt = 0;
used_limits->jobs = 0;
if (used_limits->node_bitmap)
bit_clear_all(used_limits->node_bitmap);
if (used_limits->node_job_cnt) {
memset(used_limits->node_job_cnt, 0,
sizeof(uint16_t) * node_record_count);
}
used_limits->submit_jobs = 0;
for (i=0; i<tres_cnt; i++) {
used_limits->tres[i] = 0;
used_limits->tres_run_secs[i] = 0;
}
}
list_iterator_destroy(itr);
return;
}
static void _clear_qos_acct_limit_info(slurmdb_qos_rec_t *qos_ptr)
{
_clear_qos_used_limit_list(qos_ptr->usage->acct_limit_list,
qos_ptr->usage->tres_cnt);
}
static void _clear_qos_user_limit_info(slurmdb_qos_rec_t *qos_ptr)
{
_clear_qos_used_limit_list(qos_ptr->usage->user_limit_list,
qos_ptr->usage->tres_cnt);
}
static int _clear_used_qos_info(slurmdb_qos_rec_t *qos)
{
int i;
if (!qos || !qos->usage)
return SLURM_ERROR;
qos->usage->accrue_cnt = 0;
qos->usage->grp_used_jobs = 0;
qos->usage->grp_used_submit_jobs = 0;
if (qos->usage->grp_node_bitmap)
bit_clear_all(qos->usage->grp_node_bitmap);
if (qos->usage->grp_node_job_cnt) {
memset(qos->usage->grp_node_job_cnt, 0,
sizeof(uint16_t) * node_record_count);
}
for (i=0; i<qos->usage->tres_cnt; i++) {
qos->usage->grp_used_tres[i] = 0;
qos->usage->grp_used_tres_run_secs[i] = 0;
}
/* do not reset usage_raw or grp_used_wall.
* if you need to reset it do it
* else where since sometimes we call this and do not want
* shares reset */
_clear_qos_acct_limit_info(qos);
_clear_qos_user_limit_info(qos);
return SLURM_SUCCESS;
}
/* Locks should be in place before calling this. */
static int _change_user_name(slurmdb_user_rec_t *user)
{
int rc = SLURM_SUCCESS;
list_itr_t *itr = NULL;
slurmdb_assoc_rec_t *assoc = NULL;
slurmdb_wckey_rec_t *wckey = NULL;
uid_t pw_uid;
xassert(user->name);
xassert(user->old_name);
if (uid_from_string(user->name, &pw_uid) < 0) {
debug("%s: couldn't get new uid for user %s",
__func__, user->name);
user->uid = NO_VAL;
} else
user->uid = pw_uid;
if (assoc_mgr_assoc_list) {
itr = list_iterator_create(assoc_mgr_assoc_list);
while ((assoc = list_next(itr))) {
if (!assoc->user)
continue;
if (!xstrcmp(user->old_name, assoc->user)) {
/* Since the uid changed the
hash as well will change. Remove
the assoc from the hash before the
change or you won't find it.
*/
_delete_assoc_hash(assoc);
xfree(assoc->user);
assoc->user = xstrdup(user->name);
assoc->uid = user->uid;
_add_assoc_hash(assoc);
debug3("changing assoc %d", assoc->id);
}
}
list_iterator_destroy(itr);
}
if (assoc_mgr_wckey_list) {
itr = list_iterator_create(assoc_mgr_wckey_list);
while ((wckey = list_next(itr))) {
if (!xstrcmp(user->old_name, wckey->user)) {
xfree(wckey->user);
wckey->user = xstrdup(user->name);
wckey->uid = user->uid;
debug3("changing wckey %d", wckey->id);
}
}
list_iterator_destroy(itr);
}
return rc;
}
static int _grab_parents_qos(slurmdb_assoc_rec_t *assoc)
{
slurmdb_assoc_rec_t *parent_assoc = NULL;
char *qos_char = NULL;
list_itr_t *itr = NULL;
if (!assoc)
return SLURM_ERROR;
if (assoc->qos_list)
list_flush(assoc->qos_list);
else
assoc->qos_list = list_create(xfree_ptr);
parent_assoc = assoc->usage->parent_assoc_ptr;
if (!parent_assoc || !parent_assoc->qos_list
|| !list_count(parent_assoc->qos_list))
return SLURM_SUCCESS;
itr = list_iterator_create(parent_assoc->qos_list);
while ((qos_char = list_next(itr)))
list_append(assoc->qos_list, xstrdup(qos_char));
list_iterator_destroy(itr);
return SLURM_SUCCESS;
}
static int _local_update_assoc_qos_list(slurmdb_assoc_rec_t *assoc,
List new_qos_list)
{
list_itr_t *new_qos_itr = NULL, *curr_qos_itr = NULL;
char *new_qos = NULL, *curr_qos = NULL;
int flushed = 0;
if (!assoc || !new_qos_list) {
error("need both new qos_list and an association to update");
return SLURM_ERROR;
}
if (!list_count(new_qos_list)) {
_grab_parents_qos(assoc);
return SLURM_SUCCESS;
}
/* Even though we only use the valid_qos bitstr for things we
need to keep the list around for now since we don't pack the
bitstr for state save.
*/
new_qos_itr = list_iterator_create(new_qos_list);
curr_qos_itr = list_iterator_create(assoc->qos_list);
while ((new_qos = list_next(new_qos_itr))) {
if (new_qos[0] == '-') {
while ((curr_qos = list_next(curr_qos_itr))) {
if (!xstrcmp(curr_qos, new_qos+1)) {
list_delete_item(curr_qos_itr);
break;
}
}
list_iterator_reset(curr_qos_itr);
} else if (new_qos[0] == '+') {
while ((curr_qos = list_next(curr_qos_itr)))
if (!xstrcmp(curr_qos, new_qos+1))
break;
if (!curr_qos) {
list_append(assoc->qos_list,
xstrdup(new_qos+1));
list_iterator_reset(curr_qos_itr);
}
} else if (new_qos[0] == '=') {
if (!flushed)
list_flush(assoc->qos_list);
list_append(assoc->qos_list, xstrdup(new_qos+1));
flushed = 1;
} else if (new_qos[0]) {
if (!flushed)
list_flush(assoc->qos_list);
list_append(assoc->qos_list, xstrdup(new_qos));
flushed = 1;
}
}
list_iterator_destroy(curr_qos_itr);
list_iterator_destroy(new_qos_itr);
return SLURM_SUCCESS;
}
static int _list_find_uid(void *x, void *key)
{
slurmdb_user_rec_t *user = (slurmdb_user_rec_t *) x;
uint32_t uid = *(uint32_t *) key;
if (user->uid == uid)
return 1;
return 0;
}
static int _list_find_user(void *x, void *key)
{
slurmdb_user_rec_t *found_user = x;
slurmdb_user_rec_t *user = key;
if (user->uid != NO_VAL)
return (found_user->uid == user->uid);
else if (!xstrcasecmp(found_user->name, user->name))
return 1;
return 0;
}
static int _list_find_coord(void *x, void *key)
{
slurmdb_user_rec_t *user = x;
find_coord_t *find_coord = key;
slurmdb_coord_rec_t *found_coord, *coord;
if (!user->coord_accts ||
!(found_coord = list_find_first(user->coord_accts,
_find_acct_by_name,
find_coord->req)))
return 0;
if (!find_coord->ret_list)
find_coord->ret_list = list_create(slurmdb_destroy_coord_rec);
coord = xmalloc(sizeof(*coord));
list_append(find_coord->ret_list, coord);
coord->name = xstrdup(user->name);
coord->direct = found_coord->direct;
return 0;
}
/* locks should be put in place before calling this function USER_WRITE */
static void _set_user_default_acct(slurmdb_assoc_rec_t *assoc,
slurmdb_user_rec_t *user)
{
xassert(assoc);
xassert(assoc->acct);
xassert(assoc_mgr_user_list);
/* set up the default if this is it */
if ((assoc->is_def == 1) && (assoc->uid != NO_VAL)) {
if (!user)
user = list_find_first(assoc_mgr_user_list,
_list_find_uid, &assoc->uid);
if (!user)
return;
if (!user->default_acct
|| xstrcmp(user->default_acct, assoc->acct)) {
xfree(user->default_acct);
if (assoc->is_def == 1) {
user->default_acct = xstrdup(assoc->acct);
debug2("user %s default acct is %s",
user->name, user->default_acct);
} else
debug2("user %s default acct %s removed",
user->name, assoc->acct);
}
/* cache user rec reference for backfill*/
assoc->user_rec = user;
}
}
/* locks should be put in place before calling this function USER_WRITE */
static void _clear_user_default_acct(slurmdb_assoc_rec_t *assoc)
{
xassert(assoc);
xassert(assoc->acct);
xassert(assoc_mgr_user_list);
/* set up the default if this is it */
if ((assoc->is_def == 0) && (assoc->uid != NO_VAL)) {
slurmdb_user_rec_t *user = list_find_first(
assoc_mgr_user_list, _list_find_uid, &assoc->uid);
if (!user)
return;
if (!user->default_acct
|| !xstrcmp(user->default_acct, assoc->acct)) {
xfree(user->default_acct);
debug2("user %s default acct %s removed",
user->name, assoc->acct);
}
/* cache user rec reference for backfill */
assoc->user_rec = user;
}
}
/* locks should be put in place before calling this function USER_WRITE */
static void _set_user_default_wckey(slurmdb_wckey_rec_t *wckey,
slurmdb_user_rec_t *user)
{
xassert(wckey);
xassert(wckey->name);
xassert(assoc_mgr_user_list);
/* set up the default if this is it */
if ((wckey->is_def == 1) && (wckey->uid != NO_VAL)) {
if (!user)
user = list_find_first(assoc_mgr_user_list,
_list_find_uid, &wckey->uid);
if (!user)
return;
if (!user->default_wckey
|| xstrcmp(user->default_wckey, wckey->name)) {
xfree(user->default_wckey);
user->default_wckey = xstrdup(wckey->name);
debug2("user %s default wckey is %s",
user->name, user->default_wckey);
}
}
}
/* Return first parent that is not SLURMDB_FS_USE_PARENT unless
* direct is set */
static slurmdb_assoc_rec_t* _find_assoc_parent(
slurmdb_assoc_rec_t *assoc, bool direct)
{
slurmdb_assoc_rec_t *parent = NULL, *prev_parent;
xassert(assoc);
parent = assoc;
while (parent) {
if (!parent->parent_id)
break;
prev_parent = parent;
if (!(parent = _find_assoc_rec_id(prev_parent->parent_id,
prev_parent->cluster))) {
error("Can't find parent id %u for assoc %u, "
"this should never happen.",
prev_parent->parent_id, prev_parent->id);
break;
}
/* See if we need to look for the next parent up the tree */
if (direct || (assoc->shares_raw != SLURMDB_FS_USE_PARENT) ||
(parent->shares_raw != SLURMDB_FS_USE_PARENT))
break;
}
if (parent)
debug2("assoc %u(%s, %s) has %s parent of %u(%s, %s) %s",
assoc->id, assoc->acct, assoc->user,
direct ? "direct" : "fs",
parent->id, parent->acct, parent->user, assoc->lineage);
else
debug2("assoc %u(%s, %s) doesn't have a %s "
"parent (probably root) %s",
assoc->id, assoc->acct, assoc->user,
direct ? "direct" : "fs", assoc->lineage);
return parent;
}
static int _set_assoc_parent_and_user(slurmdb_assoc_rec_t *assoc)
{
xassert(verify_assoc_lock(ASSOC_LOCK, WRITE_LOCK));
xassert(verify_assoc_lock(QOS_LOCK, READ_LOCK));
xassert(verify_assoc_lock(TRES_LOCK, READ_LOCK));
xassert(verify_assoc_lock(USER_LOCK, WRITE_LOCK));
xassert(assoc_mgr_user_list);
if (!assoc || !assoc_mgr_assoc_list) {
error("you didn't give me an association");
return SLURM_ERROR;
}
if (!assoc->usage)
assoc->usage = slurmdb_create_assoc_usage(g_tres_count);
/* Users have no children so leaf is same as total */
if (assoc->user)
assoc->leaf_usage = assoc->usage;
if (assoc->parent_id) {
/* Here we need the direct parent (parent_assoc_ptr)
* and also the first parent that doesn't have
* shares_raw == SLURMDB_FS_USE_PARENT (fs_assoc_ptr).
*/
assoc->usage->parent_assoc_ptr =
_find_assoc_parent(assoc, true);
if (!assoc->usage->parent_assoc_ptr) {
error("Can't find parent id %u for assoc %u, "
"this should never happen.",
assoc->parent_id, assoc->id);
assoc->usage->fs_assoc_ptr = NULL;
} else if (assoc->shares_raw == SLURMDB_FS_USE_PARENT)
assoc->usage->fs_assoc_ptr =
_find_assoc_parent(assoc, false);
else if (assoc->usage->parent_assoc_ptr->shares_raw
== SLURMDB_FS_USE_PARENT)
assoc->usage->fs_assoc_ptr = _find_assoc_parent(
assoc->usage->parent_assoc_ptr, false);
else
assoc->usage->fs_assoc_ptr =
assoc->usage->parent_assoc_ptr;
if (assoc->usage->fs_assoc_ptr && setup_children) {
if (!assoc->usage->fs_assoc_ptr->usage)
assoc->usage->fs_assoc_ptr->usage =
slurmdb_create_assoc_usage(
g_tres_count);
if (!assoc->usage->
fs_assoc_ptr->usage->children_list)
assoc->usage->
fs_assoc_ptr->usage->children_list =
list_create(NULL);
list_append(assoc->usage->
fs_assoc_ptr->usage->children_list,
assoc);
}
if (assoc == assoc->usage->parent_assoc_ptr) {
assoc->usage->parent_assoc_ptr = NULL;
assoc->usage->fs_assoc_ptr = NULL;
error("association %u was pointing to "
"itself as it's parent",
assoc->id);
}
} else if (!slurmdbd_conf && (assoc_mgr_root_assoc != assoc)) {
slurmdb_assoc_rec_t *last_root = assoc_mgr_root_assoc;
assoc_mgr_root_assoc = assoc;
/* set up new root since if running off cache the
total usage for the cluster doesn't get set up again */
if (last_root) {
assoc_mgr_root_assoc->usage->usage_raw =
last_root->usage->usage_raw;
assoc_mgr_root_assoc->usage->usage_norm =
last_root->usage->usage_norm;
memcpy(assoc_mgr_root_assoc->usage->usage_tres_raw,
last_root->usage->usage_tres_raw,
sizeof(long double) * g_tres_count);
}
}
if (assoc->user) {
uid_t pw_uid;
g_user_assoc_count++;
if (assoc->uid == NO_VAL || assoc->uid == INFINITE ||
assoc->uid == 0) {
if (uid_from_string(assoc->user, &pw_uid) < 0)
assoc->uid = NO_VAL;
else
assoc->uid = pw_uid;
}
_set_user_default_acct(assoc, NULL);
/* get the qos bitmap here */
if (g_qos_count > 0) {
if (!assoc->usage->valid_qos
|| (bit_size(assoc->usage->valid_qos)
!= g_qos_count)) {
FREE_NULL_BITMAP(assoc->usage->valid_qos);
assoc->usage->valid_qos =
bit_alloc(g_qos_count);
} else
bit_clear_all(assoc->usage->valid_qos);
set_qos_bitstr_from_list(assoc->usage->valid_qos,
assoc->qos_list);
if (((int32_t)assoc->def_qos_id > 0)
&& !bit_test(assoc->usage->valid_qos,
assoc->def_qos_id)) {
error("assoc %u doesn't have access "
"to it's default qos '%s'",
assoc->id,
slurmdb_qos_str(assoc_mgr_qos_list,
assoc->def_qos_id));
assoc->def_qos_id = 0;
}
} else
assoc->def_qos_id = 0;
} else {
assoc->uid = NO_VAL;
}
/* If you uncomment this below make sure you put READ_LOCK on
* the qos_list (the third lock) on calling functions.
*/
//log_assoc_rec(assoc);
return SLURM_SUCCESS;
}
static void _set_assoc_norm_priority(slurmdb_assoc_rec_t *assoc)
{
if (!assoc)
return;
if (assoc->priority == INFINITE)
assoc->priority = 0;
if (!assoc->usage)
assoc->usage = slurmdb_create_assoc_usage(g_tres_count);
/* Users have no children so leaf_usage is same as total */
if (assoc->user)
assoc->leaf_usage = assoc->usage;