-
Notifications
You must be signed in to change notification settings - Fork 49
/
alloc-bypass.c
230 lines (198 loc) · 7.06 KB
/
alloc-bypass.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
/************************************************************\
* 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
\************************************************************/
/* alloc-bypass.c - If attributes.system.R exists in jobspec, then
* bypass scheduler alloc protocol and use R directly (for instance
* owner use only)
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <sys/types.h>
#include <jansson.h>
#include <flux/core.h>
#include <flux/jobtap.h>
#include "src/common/librlist/rlist.h"
#include "src/common/libjob/idf58.h"
static void alloc_continuation (flux_future_t *f, void *arg)
{
flux_plugin_t *p = arg;
flux_jobid_t *idptr = flux_future_aux_get (f, "jobid");
if (flux_future_get (f, NULL) < 0) {
flux_jobtap_raise_exception (p,
*idptr,
"alloc", 0,
"failed to commit R to kvs: %s",
strerror (errno));
goto done;
}
if (flux_jobtap_event_post_pack (p,
*idptr,
"alloc",
"{s:b}",
"bypass", true) < 0) {
flux_jobtap_raise_exception (p,
*idptr,
"alloc", 0,
"failed to post alloc event: %s",
strerror (errno));
goto done;
}
done:
flux_future_destroy (f);
}
static flux_future_t *commit_R (flux_plugin_t *p,
flux_jobid_t id,
json_t *R)
{
flux_future_t *f = NULL;
flux_kvs_txn_t *txn = NULL;
flux_t *h = flux_jobtap_get_flux (p);
char key[64];
if (!h
|| flux_job_kvs_key (key, sizeof (key), id, "R") < 0
|| !(txn = flux_kvs_txn_create ()))
return NULL;
if (flux_kvs_txn_pack (txn, 0, key, "O", R) < 0)
goto out;
f = flux_kvs_commit (h, NULL, 0, txn);
out:
flux_kvs_txn_destroy (txn);
return f;
}
static int alloc_start (flux_plugin_t *p,
flux_jobid_t id,
json_t *R)
{
int saved_errno;
flux_future_t *f = NULL;
flux_jobid_t *idptr = NULL;
if (!(f = commit_R (p, id, R))
|| flux_future_then (f, -1, alloc_continuation, p) < 0)
goto error;
if (!(idptr = malloc (sizeof (*idptr)))
|| flux_future_aux_set (f, "jobid", idptr, free) < 0)
goto error;
*idptr = id;
return 0;
error:
saved_errno = errno;
flux_future_destroy (f);
free (idptr);
errno = saved_errno;
return -1;
}
static int sched_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *arg)
{
json_t *R;
flux_jobid_t id;
/* If alloc-bypass::R set on this job then commit R to KVS
* and set alloc-bypass flag
*/
if (!(R = flux_jobtap_job_aux_get (p,
FLUX_JOBTAP_CURRENT_JOB,
"alloc-bypass::R")))
return 0;
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:I}",
"id", &id) < 0) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"alloc", 0,
"alloc-bypass: %s: unpack: %s",
topic,
flux_plugin_arg_strerror (args));
return -1;
}
if (alloc_start (p, id, R) < 0)
flux_jobtap_raise_exception (p, id, "alloc", 0,
"failed to commit R to kvs");
if (flux_plugin_arg_pack (args, FLUX_PLUGIN_ARG_OUT, "{s:O}", "R", R) < 0)
return -1;
return 0;
}
static int validate_cb (flux_plugin_t *p,
const char *topic,
flux_plugin_arg_t *args,
void *arg)
{
json_t *R = NULL;
struct rlist *rl;
json_error_t error;
uint32_t userid = (uint32_t) -1;
if (flux_plugin_arg_unpack (args,
FLUX_PLUGIN_ARG_IN,
"{s:i s:{s:{s?{s?{s?o}}}}}",
"userid", &userid,
"jobspec",
"attributes",
"system",
"alloc-bypass",
"R", &R) < 0) {
return flux_jobtap_reject_job (p,
args,
"invalid system.alloc-bypass.R: %s",
flux_plugin_arg_strerror (args));
}
/* Nothing to do if no R provided
*/
if (R == NULL)
return 0;
if (userid != getuid ())
return flux_jobtap_reject_job (p,
args,
"Guest user cannot use alloc bypass");
/* Sanity check R for validity
*/
if (!(rl = rlist_from_json (R, &error)))
return flux_jobtap_reject_job (p,
args,
"alloc-bypass: invalid R: %s",
error.text);
rlist_destroy (rl);
/* Store R in job structure to avoid re-fetching from plugin args
* in job.state.sched callback.
*/
if (flux_jobtap_job_aux_set (p,
FLUX_JOBTAP_CURRENT_JOB,
"alloc-bypass::R",
json_incref (R),
(flux_free_f)json_decref) < 0) {
int saved_errno = errno;
json_decref (R);
return flux_jobtap_reject_job (p,
args,
"failed to capture alloc-bypass R: %s",
strerror (saved_errno));
}
if (flux_jobtap_job_set_flag (p,
FLUX_JOBTAP_CURRENT_JOB,
"alloc-bypass") < 0) {
flux_jobtap_raise_exception (p, FLUX_JOBTAP_CURRENT_JOB,
"alloc", 0,
"Failed to set alloc-bypass: %s",
strerror (errno));
return -1;
}
return 0;
}
static const struct flux_plugin_handler tab[] = {
{ "job.state.sched", sched_cb, NULL },
{ "job.validate", validate_cb, NULL },
{ 0 }
};
int flux_plugin_init (flux_plugin_t *p)
{
return flux_plugin_register (p, "alloc-bypass", tab);
}
// vi:ts=4 sw=4 expandtab