-
Notifications
You must be signed in to change notification settings - Fork 26
/
accounting_storage_mysql.c
2697 lines (2420 loc) · 81.4 KB
/
accounting_storage_mysql.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
/*****************************************************************************\
* accounting_storage_mysql.c - accounting interface to as_mysql.
*
* $Id: accounting_storage_mysql.c 13061 2008-01-22 21:23:56Z da $
*****************************************************************************
* Copyright (C) 2004-2007 The Regents of the University of California.
* Copyright (C) 2008-2010 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://computing.llnl.gov/linux/slurm/>.
* 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.
*****************************************************************************
* Notes on as_mysql configuration
* Assumes mysql is installed as user root
* Assumes SlurmUser is configured as user slurm
* # mysql --user=root -p
* mysql> GRANT ALL ON *.* TO 'slurm'@'localhost' IDENTIFIED BY PASSWORD 'pw';
* mysql> GRANT SELECT, INSERT ON *.* TO 'slurm'@'localhost';
\*****************************************************************************/
#include "accounting_storage_mysql.h"
#include "as_mysql_acct.h"
#include "as_mysql_archive.h"
#include "as_mysql_assoc.h"
#include "as_mysql_cluster.h"
#include "as_mysql_convert.h"
#include "as_mysql_job.h"
#include "as_mysql_jobacct_process.h"
#include "as_mysql_problems.h"
#include "as_mysql_qos.h"
#include "as_mysql_resv.h"
#include "as_mysql_rollup.h"
#include "as_mysql_txn.h"
#include "as_mysql_usage.h"
#include "as_mysql_user.h"
#include "as_mysql_wckey.h"
/* These are defined here so when we link with something other than
* the slurmctld we will have these symbols defined. They will get
* overwritten when linking with the slurmctld.
*/
#if defined (__APPLE__)
char *slurmctld_cluster_name __attribute__((weak_import)) = NULL;
#else
char *slurmctld_cluster_name = NULL;
#endif
List as_mysql_cluster_list = NULL;
/* This total list is only used for converting things, so no
need to keep it upto date even though it lives until the
end of the life of the slurmdbd.
*/
List as_mysql_total_cluster_list = NULL;
pthread_mutex_t as_mysql_cluster_list_lock = PTHREAD_MUTEX_INITIALIZER;
/*
* These variables are required by the generic plugin interface. If they
* are not found in the plugin, the plugin loader will ignore it.
*
* plugin_name - a string giving a human-readable description of the
* plugin. There is no maximum length, but the symbol must refer to
* a valid string.
*
* plugin_type - a string suggesting the type of the plugin or its
* applicability to a particular form of data or method of data handling.
* If the low-level plugin API is used, the contents of this string are
* unimportant and may be anything. SLURM uses the higher-level plugin
* interface which requires this string to be of the form
*
* <application>/<method>
*
* where <application> is a description of the intended application of
* the plugin (e.g., "accounting_storage" for SLURM job completion
* logging) and <method>
* is a description of how this plugin satisfies that application. SLURM will
* only load job completion logging plugins if the plugin_type string has a
* prefix of "accounting_storage/".
*
* plugin_version - an unsigned 32-bit integer giving the version number
* of the plugin. If major and minor revisions are desired, the major
* version number may be multiplied by a suitable magnitude constant such
* as 100 or 1000. Various SLURM versions will likely require a certain
* minimum version for their plugins as the job accounting API
* matures.
*/
const char plugin_name[] = "Accounting storage MYSQL plugin";
const char plugin_type[] = "accounting_storage/as_mysql";
const uint32_t plugin_version = 100;
static mysql_db_info_t *mysql_db_info = NULL;
static char *mysql_db_name = NULL;
#define DELETE_SEC_BACK 86400
char *acct_coord_table = "acct_coord_table";
char *acct_table = "acct_table";
char *assoc_day_table = "assoc_usage_day_table";
char *assoc_hour_table = "assoc_usage_hour_table";
char *assoc_month_table = "assoc_usage_month_table";
char *assoc_table = "assoc_table";
char *cluster_day_table = "usage_day_table";
char *cluster_hour_table = "usage_hour_table";
char *cluster_month_table = "usage_month_table";
char *cluster_table = "cluster_table";
char *event_table = "event_table";
char *job_table = "job_table";
char *last_ran_table = "last_ran_table";
char *qos_table = "qos_table";
char *resv_table = "resv_table";
char *step_table = "step_table";
char *txn_table = "txn_table";
char *user_table = "user_table";
char *suspend_table = "suspend_table";
char *wckey_day_table = "wckey_usage_day_table";
char *wckey_hour_table = "wckey_usage_hour_table";
char *wckey_month_table = "wckey_usage_month_table";
char *wckey_table = "wckey_table";
static char *default_qos_str = NULL;
enum {
JASSOC_JOB,
JASSOC_ACCT,
JASSOC_USER,
JASSOC_PART,
JASSOC_COUNT
};
extern int acct_storage_p_close_connection(mysql_conn_t **mysql_conn);
static List _get_cluster_names(mysql_conn_t *mysql_conn, bool with_deleted)
{
MYSQL_RES *result = NULL;
MYSQL_ROW row;
List ret_list = NULL;
char *cluster_name = NULL;
bool found = 0;
char *query = xstrdup_printf("select name from %s", cluster_table);
if (!with_deleted)
xstrcat(query, " where deleted=0");
if (!(result = mysql_db_query_ret(mysql_conn, query, 0))) {
xfree(query);
return ret_list;
}
xfree(query);
if (!slurmdbd_conf) {
/* If not running with the slurmdbd we need to make
the correct tables for this cluster. (Since it
doesn't have to be added like usual.)
*/
cluster_name = slurm_get_cluster_name();
if (!cluster_name)
fatal("No cluster name defined in slurm.conf");
} else
found = 1;
ret_list = list_create(slurm_destroy_char);
while ((row = mysql_fetch_row(result))) {
if (row[0] && row[0][0]) {
if (cluster_name && !strcmp(cluster_name, row[0]))
found = 1;
list_append(ret_list, xstrdup(row[0]));
}
}
mysql_free_result(result);
if (cluster_name && !found)
list_append(ret_list, cluster_name);
else if (cluster_name)
xfree(cluster_name);
return ret_list;
}
static int _set_qos_cnt(mysql_conn_t *mysql_conn)
{
MYSQL_RES *result = NULL;
MYSQL_ROW row;
char *query = xstrdup_printf("select MAX(id) from %s", qos_table);
if (!(result = mysql_db_query_ret(mysql_conn, query, 0))) {
xfree(query);
return SLURM_ERROR;
}
xfree(query);
if (!(row = mysql_fetch_row(result))) {
mysql_free_result(result);
return SLURM_ERROR;
}
/* Set the current qos_count on the system for
generating bitstr of that length. Since 0 isn't
possible as an id we add 1 to the total to burn 0 and
start at the 1 bit.
*/
g_qos_count = slurm_atoul(row[0]) + 1;
mysql_free_result(result);
return SLURM_SUCCESS;
}
static void _process_running_jobs_result(char *cluster_name,
MYSQL_RES *result, List ret_list)
{
MYSQL_ROW row;
char *object;
while ((row = mysql_fetch_row(result))) {
if (!row[JASSOC_USER][0]) {
/* This should never happen */
error("How did we get a job running on an association "
"that isn't a user assocation job %s cluster "
"'%s' acct '%s'?", row[JASSOC_JOB],
cluster_name, row[JASSOC_ACCT]);
continue;
}
object = xstrdup_printf(
"JobID = %-10s C = %-10s A = %-10s U = %-9s",
row[JASSOC_JOB], cluster_name, row[JASSOC_ACCT],
row[JASSOC_USER]);
if (row[JASSOC_PART][0])
// see if there is a partition name
xstrfmtcat(object, " P = %s", row[JASSOC_PART]);
list_append(ret_list, object);
}
}
/* this function is here to see if any of what we are trying to remove
* has jobs that are or were once running. So if we have jobs and the
* object is less than a day old we don't want to delete it only set
* the deleted flag.
*/
static bool _check_jobs_before_remove(mysql_conn_t *mysql_conn,
char *cluster_name,
char *assoc_char,
List ret_list,
bool *already_flushed)
{
char *query = NULL, *object = NULL;
bool rc = 0;
int i;
MYSQL_RES *result = NULL;
/* if this changes you will need to edit the corresponding
* enum above in the global settings */
static char *jassoc_req_inx[] = {
"t0.id_job",
"t1.acct",
"t1.user",
"t1.partition"
};
if (ret_list) {
xstrcat(object, jassoc_req_inx[0]);
for(i=1; i<JASSOC_COUNT; i++)
xstrfmtcat(object, ", %s", jassoc_req_inx[i]);
query = xstrdup_printf(
"select distinct %s "
"from \"%s_%s\" as t0, "
"\"%s_%s\" as t1, \"%s_%s\" as t2 "
"where t1.lft between "
"t2.lft and t2.rgt && (%s) "
"and t0.id_assoc=t1.id_assoc "
"and t0.time_end=0 && t0.state=%d;",
object, cluster_name, job_table,
cluster_name, assoc_table,
cluster_name, assoc_table,
assoc_char, JOB_RUNNING);
xfree(object);
} else {
query = xstrdup_printf(
"select t0.id_assoc from \"%s_%s\" as t0, "
"\"%s_%s\" as t1, \"%s_%s\" as t2 "
"where t1.lft between "
"t2.lft and t2.rgt && (%s) "
"and t0.id_assoc=t1.id_assoc limit 1;",
cluster_name, job_table,
cluster_name, assoc_table,
cluster_name, assoc_table,
assoc_char);
}
debug3("%d(%s:%d) query\n%s",
mysql_conn->conn, THIS_FILE, __LINE__, query);
if (!(result = mysql_db_query_ret(mysql_conn, query, 0))) {
xfree(query);
return rc;
}
xfree(query);
if (mysql_num_rows(result)) {
debug4("We have jobs for this combo");
rc = true;
if (ret_list && !(*already_flushed)) {
list_flush(ret_list);
(*already_flushed) = 1;
reset_mysql_conn(mysql_conn);
}
}
if (ret_list)
_process_running_jobs_result(cluster_name, result, ret_list);
mysql_free_result(result);
return rc;
}
/* Same as above but for associations instead of other tables */
static bool _check_jobs_before_remove_assoc(mysql_conn_t *mysql_conn,
char *cluster_name,
char *assoc_char,
List ret_list,
bool *already_flushed)
{
char *query = NULL, *object = NULL;
bool rc = 0;
int i;
MYSQL_RES *result = NULL;
/* if this changes you will need to edit the corresponding
* enum above in the global settings */
static char *jassoc_req_inx[] = {
"t1.id_job",
"t2.acct",
"t2.user",
"t2.partition"
};
if (ret_list) {
xstrcat(object, jassoc_req_inx[0]);
for(i=1; i<JASSOC_COUNT; i++)
xstrfmtcat(object, ", %s", jassoc_req_inx[i]);
query = xstrdup_printf("select %s "
"from \"%s_%s\" as t1, \"%s_%s\" as t2 "
"where (%s) and t1.id_assoc=t2.id_assoc "
"and t1.time_end=0 && t1.state=%d;",
object, cluster_name, job_table,
cluster_name, assoc_table,
assoc_char, JOB_RUNNING);
xfree(object);
} else {
query = xstrdup_printf(
"select t1.id_assoc from \"%s_%s\" as t1, "
"\"%s_%s\" as t2 where (%s) "
"and t1.id_assoc=t2.id_assoc limit 1;",
cluster_name, job_table,
cluster_name, assoc_table,
assoc_char);
}
debug3("%d(%s:%d) query\n%s",
mysql_conn->conn, THIS_FILE, __LINE__, query);
if (!(result = mysql_db_query_ret(
mysql_conn, query, 0))) {
xfree(query);
return rc;
}
xfree(query);
if (mysql_num_rows(result)) {
debug4("We have jobs for this combo");
rc = true;
if (ret_list && !(*already_flushed)) {
list_flush(ret_list);
(*already_flushed) = 1;
reset_mysql_conn(mysql_conn);
}
}
if (ret_list)
_process_running_jobs_result(cluster_name, result, ret_list);
mysql_free_result(result);
return rc;
}
/* Same as above but for things having nothing to do with associations
* like qos or wckey */
static bool _check_jobs_before_remove_without_assoctable(
mysql_conn_t *mysql_conn, char *cluster_name, char *where_char)
{
char *query = NULL;
bool rc = 0;
MYSQL_RES *result = NULL;
query = xstrdup_printf("select id_assoc from \"%s_%s\" "
"where (%s) limit 1;",
cluster_name, job_table, where_char);
debug3("%d(%s:%d) query\n%s",
mysql_conn->conn, THIS_FILE, __LINE__, query);
if (!(result = mysql_db_query_ret(
mysql_conn, query, 0))) {
xfree(query);
return rc;
}
xfree(query);
if (mysql_num_rows(result)) {
debug4("We have jobs for this combo");
rc = true;
}
mysql_free_result(result);
return rc;
}
/* Any time a new table is added set it up here */
static int _as_mysql_acct_check_tables(mysql_conn_t *mysql_conn)
{
storage_field_t acct_coord_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0" },
{ "acct", "tinytext not null" },
{ "user", "tinytext not null" },
{ NULL, NULL}
};
storage_field_t acct_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0" },
{ "name", "tinytext not null" },
{ "description", "text not null" },
{ "organization", "text not null" },
{ NULL, NULL}
};
storage_field_t cluster_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0" },
{ "name", "tinytext not null" },
{ "control_host", "tinytext not null default ''" },
{ "control_port", "int unsigned not null default 0" },
{ "rpc_version", "smallint unsigned not null default 0" },
{ "classification", "smallint unsigned default 0" },
{ "dimensions", "smallint unsigned default 1" },
{ "plugin_id_select", "smallint unsigned default 0" },
{ "flags", "int unsigned default 0" },
{ NULL, NULL}
};
storage_field_t qos_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0" },
{ "id", "int not null auto_increment" },
{ "name", "tinytext not null" },
{ "description", "text" },
{ "flags", "int unsigned default 0" },
{ "max_jobs_per_user", "int default NULL" },
{ "max_submit_jobs_per_user", "int default NULL" },
{ "max_cpus_per_job", "int default NULL" },
{ "max_nodes_per_job", "int default NULL" },
{ "max_wall_duration_per_job", "int default NULL" },
{ "max_cpu_mins_per_job", "bigint default NULL" },
{ "max_cpu_run_mins_per_user", "bigint default NULL" },
{ "grp_jobs", "int default NULL" },
{ "grp_submit_jobs", "int default NULL" },
{ "grp_cpus", "int default NULL" },
{ "grp_nodes", "int default NULL" },
{ "grp_wall", "int default NULL" },
{ "grp_cpu_mins", "bigint default NULL" },
{ "grp_cpu_run_mins", "bigint default NULL" },
{ "preempt", "text not null default ''" },
{ "preempt_mode", "int default 0" },
{ "priority", "int default 0" },
{ "usage_factor", "double default 1.0 not null" },
{ "usage_thres", "double default NULL" },
{ NULL, NULL}
};
storage_field_t txn_table_fields[] = {
{ "id", "int not null auto_increment" },
{ "timestamp", "int unsigned default 0 not null" },
{ "action", "smallint not null" },
{ "name", "text not null" },
{ "actor", "tinytext not null" },
{ "cluster", "tinytext not null default ''" },
{ "info", "blob" },
{ NULL, NULL}
};
storage_field_t user_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0" },
{ "name", "tinytext not null" },
{ "admin_level", "smallint default 1 not null" },
{ NULL, NULL}
};
char *get_parent_proc =
"drop procedure if exists get_parent_limits; "
"create procedure get_parent_limits("
"my_table text, acct text, cluster text, without_limits int) "
"begin "
"set @par_id = NULL; "
"set @mj = NULL; "
"set @msj = NULL; "
"set @mcpj = NULL; "
"set @mnpj = NULL; "
"set @mwpj = NULL; "
"set @mcmpj = NULL; "
"set @mcrm = NULL; "
"set @def_qos_id = NULL; "
"set @qos = ''; "
"set @delta_qos = ''; "
"set @my_acct = acct; "
"if without_limits then "
"set @mj = 0; "
"set @msj = 0; "
"set @mcpj = 0; "
"set @mnpj = 0; "
"set @mwpj = 0; "
"set @mcmpj = 0; "
"set @mcrm = 0; "
"set @def_qos_id = 0; "
"set @qos = 0; "
"set @delta_qos = 0; "
"end if; "
"REPEAT "
"set @s = 'select '; "
"if @par_id is NULL then set @s = CONCAT("
"@s, '@par_id := id_assoc, '); "
"end if; "
"if @mj is NULL then set @s = CONCAT("
"@s, '@mj := max_jobs, '); "
"end if; "
"if @msj is NULL then set @s = CONCAT("
"@s, '@msj := max_submit_jobs, '); "
"end if; "
"if @mcpj is NULL then set @s = CONCAT("
"@s, '@mcpj := max_cpus_pj, ') ;"
"end if; "
"if @mnpj is NULL then set @s = CONCAT("
"@s, '@mnpj := max_nodes_pj, ') ;"
"end if; "
"if @mwpj is NULL then set @s = CONCAT("
"@s, '@mwpj := max_wall_pj, '); "
"end if; "
"if @mcmpj is NULL then set @s = CONCAT("
"@s, '@mcmpj := max_cpu_mins_pj, '); "
"end if; "
"if @mcrm is NULL then set @s = CONCAT("
"@s, '@mcrm := max_cpu_run_mins, '); "
"end if; "
"if @def_qos_id is NULL then set @s = CONCAT("
"@s, '@def_qos_id := def_qos_id, '); "
"end if; "
"if @qos = '' then set @s = CONCAT("
"@s, '@qos := qos, "
"@delta_qos := CONCAT(delta_qos, @delta_qos), '); "
"end if; "
"set @s = concat(@s, '@my_acct := parent_acct from \"', "
"cluster, '_', my_table, '\" where "
"acct = \\\'', @my_acct, '\\\' && user=\\\'\\\''); "
"prepare query from @s; "
"execute query; "
"deallocate prepare query; "
"UNTIL (@mj != -1 && @msj != -1 && @mcpj != -1 "
"&& @mnpj != -1 && @mwpj != -1 && @mcmpj != -1 "
"&& @mcrm != -1 && @def_qos_id != -1 && @qos != '') "
"|| @my_acct = '' END REPEAT; "
"END;";
char *query = NULL;
time_t now = time(NULL);
char *cluster_name = NULL;
int rc = SLURM_SUCCESS;
ListIterator itr = NULL;
/* Make the cluster table first since we build other tables
built off this one */
if (mysql_db_create_table(mysql_conn, cluster_table,
cluster_table_fields,
", primary key (name(20)))") == SLURM_ERROR)
return SLURM_ERROR;
/* This table needs to be made before conversions also since
we add a cluster column.
*/
if (mysql_db_create_table(mysql_conn, txn_table, txn_table_fields,
", primary key (id))") == SLURM_ERROR)
return SLURM_ERROR;
slurm_mutex_lock(&as_mysql_cluster_list_lock);
if (!(as_mysql_cluster_list = _get_cluster_names(mysql_conn, 0))) {
error("issue getting contents of %s", cluster_table);
slurm_mutex_unlock(&as_mysql_cluster_list_lock);
return SLURM_ERROR;
}
/* This total list is only used for converting things, so no
need to keep it upto date even though it lives until the
end of the life of the slurmdbd.
*/
if (!(as_mysql_total_cluster_list =
_get_cluster_names(mysql_conn, 1))) {
error("issue getting total contents of %s", cluster_table);
slurm_mutex_unlock(&as_mysql_cluster_list_lock);
return SLURM_ERROR;
}
/* might as well do all the cluster centric tables inside this
* lock. We need to do this on all the clusters deleted or
* other wise just to make sure everything is kept up to
* date. */
itr = list_iterator_create(as_mysql_total_cluster_list);
while ((cluster_name = list_next(itr))) {
if ((rc = create_cluster_tables(mysql_conn, cluster_name))
!= SLURM_SUCCESS)
break;
}
list_iterator_destroy(itr);
slurm_mutex_unlock(&as_mysql_cluster_list_lock);
if (rc != SLURM_SUCCESS)
return rc;
/* DEF_TIMERS; */
/* START_TIMER; */
if (as_mysql_convert_tables(mysql_conn) != SLURM_SUCCESS)
return SLURM_ERROR;
/* END_TIMER; */
/* info("conversion took %s", TIME_STR); */
if (mysql_db_create_table(mysql_conn, acct_coord_table,
acct_coord_table_fields,
", primary key (acct(20), user(20)))")
== SLURM_ERROR)
return SLURM_ERROR;
if (mysql_db_create_table(mysql_conn, acct_table, acct_table_fields,
", primary key (name(20)))") == SLURM_ERROR)
return SLURM_ERROR;
if (mysql_db_create_table(mysql_conn, qos_table,
qos_table_fields,
", primary key (id), "
"unique index (name(20)))")
== SLURM_ERROR)
return SLURM_ERROR;
else {
int qos_id = 0;
if (slurmdbd_conf && slurmdbd_conf->default_qos) {
List char_list = list_create(slurm_destroy_char);
char *qos = NULL;
ListIterator itr = NULL;
slurm_addto_char_list(char_list,
slurmdbd_conf->default_qos);
/* NOTE: you can not use list_pop, or list_push
anywhere either, since as_mysql is
exporting something of the same type as a macro,
which messes everything up
(my_list.h is the bad boy).
*/
itr = list_iterator_create(char_list);
while ((qos = list_next(itr))) {
query = xstrdup_printf(
"insert into %s "
"(creation_time, mod_time, name, "
"description) "
"values (%ld, %ld, '%s', "
"'Added as default') "
"on duplicate key update "
"id=LAST_INSERT_ID(id), deleted=0;",
qos_table, now, now, qos);
qos_id = mysql_db_insert_ret_id(
mysql_conn, query);
if (!qos_id)
fatal("problem added qos '%s", qos);
xstrfmtcat(default_qos_str, ",%d", qos_id);
xfree(query);
}
list_iterator_destroy(itr);
list_destroy(char_list);
} else {
query = xstrdup_printf(
"insert into %s "
"(creation_time, mod_time, name, description) "
"values (%ld, %ld, 'normal', "
"'Normal QOS default') "
"on duplicate key update "
"id=LAST_INSERT_ID(id), deleted=0;",
qos_table, now, now);
//debug3("%s", query);
qos_id = mysql_db_insert_ret_id(mysql_conn, query);
if (!qos_id)
fatal("problem added qos 'normal");
xstrfmtcat(default_qos_str, ",%d", qos_id);
xfree(query);
}
if (_set_qos_cnt(mysql_conn) != SLURM_SUCCESS)
return SLURM_ERROR;
}
/* This must be ran after create_cluster_tables() */
if (mysql_db_create_table(mysql_conn, user_table, user_table_fields,
", primary key (name(20)))") == SLURM_ERROR)
return SLURM_ERROR;
rc = mysql_db_query(mysql_conn, get_parent_proc);
/* Add user root to be a user by default and have this default
* account be root. If already there just update
* name='root'. That way if the admins delete it it will
* remain deleted. Creation time will be 0 so it will never
* really be deleted.
*/
query = xstrdup_printf(
"insert into %s (creation_time, mod_time, name, "
"admin_level) values (%ld, %ld, 'root', %d) "
"on duplicate key update name='root';",
user_table, (long)now, (long)now, SLURMDB_ADMIN_SUPER_USER);
xstrfmtcat(query,
"insert into %s (creation_time, mod_time, name, "
"description, organization) values (%ld, %ld, 'root', "
"'default root account', 'root') on duplicate key "
"update name='root';",
acct_table, (long)now, (long)now);
//debug3("%s", query);
mysql_db_query(mysql_conn, query);
xfree(query);
return rc;
}
/* This should be added to the beginning of each function to make sure
* we have a connection to the database before we try to use it.
*/
extern int check_connection(mysql_conn_t *mysql_conn)
{
if (!mysql_conn) {
error("We need a connection to run this");
errno = SLURM_ERROR;
return SLURM_ERROR;
} else if (mysql_db_ping(mysql_conn) != 0) {
if (mysql_db_get_db_connection(
mysql_conn, mysql_db_name, mysql_db_info)
!= SLURM_SUCCESS) {
error("unable to re-connect to as_mysql database");
errno = ESLURM_DB_CONNECTION;
return ESLURM_DB_CONNECTION;
} else {
int rc;
if (mysql_conn->rollback)
mysql_autocommit(mysql_conn->db_conn, 0);
rc = mysql_db_query(mysql_conn,
"SET session "
"sql_mode='ANSI_QUOTES';");
if (rc != SLURM_SUCCESS) {
error("couldn't set sql_mode on reconnect");
acct_storage_p_close_connection(&mysql_conn);
errno = ESLURM_DB_CONNECTION;
return ESLURM_DB_CONNECTION;
}
}
}
if (mysql_conn->cluster_deleted) {
errno = ESLURM_CLUSTER_DELETED;
return ESLURM_CLUSTER_DELETED;
}
return SLURM_SUCCESS;
}
/* Let me know if the last statement had rows that were affected.
* This only gets called by a non-threaded connection, so there is no
* need to worry about locks.
*/
extern int last_affected_rows(mysql_conn_t *mysql_conn)
{
int status=0, rows=0;
MYSQL_RES *result = NULL;
do {
result = mysql_store_result(mysql_conn->db_conn);
if (result)
mysql_free_result(result);
else
if (mysql_field_count(mysql_conn->db_conn) == 0) {
status = mysql_affected_rows(
mysql_conn->db_conn);
if (status > 0)
rows = status;
}
if ((status = mysql_next_result(mysql_conn->db_conn)) > 0)
debug3("Could not execute statement\n");
} while (status == 0);
return rows;
}
extern void reset_mysql_conn(mysql_conn_t *mysql_conn)
{
if (mysql_conn->rollback)
mysql_db_rollback(mysql_conn);
xfree(mysql_conn->pre_commit_query);
list_flush(mysql_conn->update_list);
}
extern int create_cluster_tables(mysql_conn_t *mysql_conn, char *cluster_name)
{
storage_field_t assoc_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0 not null" },
{ "is_def", "tinyint default 0 not null" },
{ "id_assoc", "int not null auto_increment" },
{ "user", "tinytext not null default ''" },
{ "acct", "tinytext not null" },
{ "partition", "tinytext not null default ''" },
{ "parent_acct", "tinytext not null default ''" },
{ "lft", "int not null" },
{ "rgt", "int not null" },
{ "shares", "int default 1 not null" },
{ "max_jobs", "int default NULL" },
{ "max_submit_jobs", "int default NULL" },
{ "max_cpus_pj", "int default NULL" },
{ "max_nodes_pj", "int default NULL" },
{ "max_wall_pj", "int default NULL" },
{ "max_cpu_mins_pj", "bigint default NULL" },
{ "max_cpu_run_mins", "bigint default NULL" },
{ "grp_jobs", "int default NULL" },
{ "grp_submit_jobs", "int default NULL" },
{ "grp_cpus", "int default NULL" },
{ "grp_nodes", "int default NULL" },
{ "grp_wall", "int default NULL" },
{ "grp_cpu_mins", "bigint default NULL" },
{ "grp_cpu_run_mins", "bigint default NULL" },
{ "def_qos_id", "int default NULL" },
{ "qos", "blob not null default ''" },
{ "delta_qos", "blob not null default ''" },
{ NULL, NULL}
};
storage_field_t assoc_usage_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0 not null" },
{ "id_assoc", "int not null" },
{ "time_start", "int unsigned not null" },
{ "alloc_cpu_secs", "bigint default 0 not null" },
{ NULL, NULL}
};
storage_field_t cluster_usage_table_fields[] = {
{ "creation_time", "int unsigned not null" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0 not null" },
{ "time_start", "int unsigned not null" },
{ "cpu_count", "int default 0 not null" },
{ "alloc_cpu_secs", "bigint default 0 not null" },
{ "down_cpu_secs", "bigint default 0 not null" },
{ "pdown_cpu_secs", "bigint default 0 not null" },
{ "idle_cpu_secs", "bigint default 0 not null" },
{ "resv_cpu_secs", "bigint default 0 not null" },
{ "over_cpu_secs", "bigint default 0 not null" },
{ NULL, NULL}
};
storage_field_t event_table_fields[] = {
{ "time_start", "int unsigned not null" },
{ "time_end", "int unsigned default 0 not null" },
{ "node_name", "tinytext default '' not null" },
{ "cluster_nodes", "text not null default ''" },
{ "cpu_count", "int not null" },
{ "reason", "tinytext not null" },
{ "reason_uid", "int unsigned default 0xfffffffe not null" },
{ "state", "smallint unsigned default 0 not null" },
{ NULL, NULL}
};
storage_field_t job_table_fields[] = {
{ "job_db_inx", "int not null auto_increment" },
{ "mod_time", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0 not null" },
{ "account", "tinytext" },
{ "cpus_req", "int unsigned not null" },
{ "cpus_alloc", "int unsigned not null" },
{ "derived_ec", "int unsigned default 0 not null" },
{ "derived_es", "text" },
{ "exit_code", "int unsigned default 0 not null" },
{ "job_name", "tinytext not null" },
{ "id_assoc", "int unsigned not null" },
{ "id_block", "tinytext" },
{ "id_job", "int unsigned not null" },
{ "id_qos", "int unsigned default 0 not null" },
{ "id_resv", "int unsigned not null" },
{ "id_wckey", "int unsigned not null" },
{ "id_user", "int unsigned not null" },
{ "id_group", "int unsigned not null" },
{ "kill_requid", "int default -1 not null" },
{ "nodelist", "text" },
{ "nodes_alloc", "int unsigned not null" },
{ "node_inx", "text" },
{ "partition", "tinytext not null" },
{ "priority", "int not null" },
{ "state", "smallint unsigned not null" },
{ "timelimit", "int unsigned default 0 not null" },
{ "time_submit", "int unsigned default 0 not null" },
{ "time_eligible", "int unsigned default 0 not null" },
{ "time_start", "int unsigned default 0 not null" },
{ "time_end", "int unsigned default 0 not null" },
{ "time_suspended", "int unsigned default 0 not null" },
{ "wckey", "tinytext not null default ''" },
{ "track_steps", "tinyint not null" },
{ NULL, NULL}
};
storage_field_t last_ran_table_fields[] = {
{ "hourly_rollup", "int unsigned default 0 not null" },
{ "daily_rollup", "int unsigned default 0 not null" },
{ "monthly_rollup", "int unsigned default 0 not null" },
{ NULL, NULL}
};
storage_field_t resv_table_fields[] = {
{ "id_resv", "int unsigned default 0 not null" },
{ "deleted", "tinyint default 0 not null" },
{ "assoclist", "text not null default ''" },
{ "cpus", "int unsigned not null" },
{ "flags", "smallint unsigned default 0 not null" },
{ "nodelist", "text not null default ''" },
{ "node_inx", "text not null default ''" },
{ "resv_name", "text not null" },
{ "time_start", "int unsigned default 0 not null"},
{ "time_end", "int unsigned default 0 not null" },
{ NULL, NULL}
};
storage_field_t step_table_fields[] = {
{ "job_db_inx", "int not null" },
{ "deleted", "tinyint default 0 not null" },
{ "cpus_alloc", "int unsigned not null" },
{ "exit_code", "int default 0 not null" },
{ "id_step", "int not null" },
{ "kill_requid", "int default -1 not null" },
{ "nodelist", "text not null" },
{ "nodes_alloc", "int unsigned not null" },
{ "node_inx", "text" },
{ "state", "smallint unsigned not null" },
{ "step_name", "text not null" },
{ "task_cnt", "int unsigned not null" },
{ "task_dist", "smallint default 0 not null" },
{ "time_start", "int unsigned default 0 not null" },
{ "time_end", "int unsigned default 0 not null" },
{ "time_suspended", "int unsigned default 0 not null" },
{ "user_sec", "int unsigned default 0 not null" },
{ "user_usec", "int unsigned default 0 not null" },
{ "sys_sec", "int unsigned default 0 not null" },
{ "sys_usec", "int unsigned default 0 not null" },
{ "max_pages", "int unsigned default 0 not null" },
{ "max_pages_task", "smallint unsigned default 0 not null" },
{ "max_pages_node", "int unsigned default 0 not null" },
{ "ave_pages", "double unsigned default 0.0 not null" },
{ "max_rss", "bigint unsigned default 0 not null" },
{ "max_rss_task", "smallint unsigned default 0 not null" },
{ "max_rss_node", "int unsigned default 0 not null" },
{ "ave_rss", "double unsigned default 0.0 not null" },
{ "max_vsize", "bigint unsigned default 0 not null" },
{ "max_vsize_task", "smallint unsigned default 0 not null" },