Skip to content

Commit ffc419f

Browse files
authored
Actually retrieve the batch script contents (PySlurm#258)
* Fixes "incomplete type" complaints cython always complaint about "incomplete type" for pthread_mutex_t and sockaddr_storage. They must be atleast defined with a "pass", which makes the complaint go away. Also reformat the imports a bit. * Actually return the batch-script contents as a string. Previously it was just printed to stdout, so it wasn't really useful when wanting to process the contents any further in a script for example.
1 parent 2617092 commit ffc419f

File tree

4 files changed

+220
-20
lines changed

4 files changed

+220
-20
lines changed

UPGRADE_C_API.rst

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,8 @@ Then, simply generate the header definitions like in this example:
5151
scripts/pyslurm_bindgen.py -D /directoy/with/slurm/headers > pyslurm/slurm/header.pxi
5252
5353
The script outputs everything to `stdout`. Simply redirect the output to the file: :code:`pyslurm/slurm/header.pxi`.
54+
The headers should now be fully translated.
5455

55-
Now, 99% of the work is done for generating the headers. For the 1% left, you now need to open the generated file, search for the two follwowing statements and comment them out:
56-
57-
- `slurm_addr_t control_addr`
58-
- `phtread_mutex_t lock`
59-
60-
The compiler will otherwise complain that these are incomplete type definitions.
6156

6257
Compiling, Updating, Testing
6358
----------------------------

pyslurm/pyslurm.pyx

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,19 +2312,71 @@ cdef class job:
23122312

23132313
def slurm_job_batch_script(self, jobid):
23142314
"""
2315-
Retrieve the batch script for a given jobid.
2315+
Return the contents of the batch-script for a Job.
23162316
2317-
:param str jobid: Job id key string to search
2318-
:returns: String output of a jobid's batch script
2317+
Note: The string returned also includes all the "\n" characters
2318+
(new-line).
2319+
2320+
:param jobid: ID of the Job for which the script should be retrieved.
2321+
:type jobid: Union[str, int]
2322+
:raises: [ValueError]: When retrieving the Batch-Script for the Job was
2323+
not successful.
2324+
:returns: The content of the batch script.
23192325
:rtype: `str`
23202326
"""
2327+
# This reimplements the slurm_job_batch_script API call. Otherwise we
2328+
# would have to parse the FILE* ptr we get from it back into a
2329+
# char* which would be a bit silly. Source:
2330+
# https://github.com/SchedMD/slurm/blob/7162f15af8deaf02c3bbf940d59e818cdeb5c69d/src/api/job_info.c#L1319
2331+
cdef:
2332+
slurm.job_id_msg_t msg
2333+
slurm.slurm_msg_t req
2334+
slurm.slurm_msg_t resp
2335+
int rc = slurm.SLURM_SUCCESS
2336+
str script = None
2337+
23212338
if isinstance(jobid, int) or isinstance(jobid, long):
23222339
jobid = str(jobid).encode("UTF-8")
23232340
else:
23242341
jobid = jobid.encode("UTF-8")
23252342

23262343
jobid_xlate = slurm.slurm_xlate_job_id(jobid)
2327-
return slurm.slurm_job_batch_script(slurm.stdout, jobid_xlate)
2344+
2345+
slurm.slurm_msg_t_init(&req)
2346+
slurm.slurm_msg_t_init(&resp)
2347+
2348+
memset(&msg, 0, sizeof(msg))
2349+
msg.job_id = jobid_xlate
2350+
req.msg_type = slurm.REQUEST_BATCH_SCRIPT
2351+
req.data = &msg
2352+
2353+
rc = slurm.slurm_send_recv_controller_msg(&req, &resp,
2354+
slurm.working_cluster_rec)
2355+
if rc < 0:
2356+
err = slurm.slurm_get_errno()
2357+
raise ValueError(slurm.stringOrNone(slurm.slurm_strerror(err), ''),
2358+
err)
2359+
2360+
if resp.msg_type == slurm.RESPONSE_BATCH_SCRIPT:
2361+
script = slurm.stringOrNone(<char*>resp.data, None)
2362+
slurm.xfree(resp.data)
2363+
2364+
elif resp.msg_type == slurm.RESPONSE_SLURM_RC:
2365+
rc = (<slurm.return_code_msg_t*> resp.data).return_code
2366+
slurm.slurm_free_return_code_msg(<slurm.return_code_msg_t*>resp.data)
2367+
2368+
if rc == slurm.SLURM_ERROR:
2369+
rc = slurm.slurm_get_errno()
2370+
2371+
raise ValueError(slurm.stringOrNone(slurm.slurm_strerror(rc), ''),
2372+
rc)
2373+
2374+
else:
2375+
rc = slurm.slurm_get_errno()
2376+
raise ValueError(slurm.stringOrNone(slurm.slurm_strerror(rc), ''),
2377+
rc)
2378+
2379+
return script
23282380

23292381
cdef int fill_job_desc_from_opts(self, dict job_opts, slurm.job_desc_msg_t *desc):
23302382
"""

pyslurm/slurm/__init__.pxd

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,37 @@
11
from libcpp cimport bool
2-
from posix.unistd cimport uid_t, pid_t, gid_t
3-
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t
4-
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t
52
from cpython.version cimport PY_MAJOR_VERSION
6-
from libc.string cimport strlen, memcpy
73

8-
cdef extern from "<netinet/in.h>" nogil:
9-
ctypedef struct sockaddr_in
10-
ctypedef struct sockaddr_storage
4+
from posix.unistd cimport (
5+
uid_t,
6+
pid_t,
7+
gid_t,
8+
)
9+
10+
from libc.stdint cimport (
11+
int8_t,
12+
int16_t,
13+
int32_t,
14+
int64_t,
15+
uint8_t,
16+
uint16_t,
17+
uint32_t,
18+
uint64_t,
19+
)
20+
21+
from libc.string cimport (
22+
strlen,
23+
memcpy,
24+
)
25+
26+
cdef extern from '<netinet/in.h>' nogil:
27+
ctypedef struct sockaddr_storage:
28+
pass
1129

1230
cdef extern from '<stdio.h>' nogil:
1331
ctypedef struct FILE
1432
cdef FILE *stdout
1533

16-
cdef extern from 'time.h' nogil:
34+
cdef extern from '<time.h>' nogil:
1735
ctypedef long time_t
1836
double difftime(time_t time1, time_t time2)
1937
time_t time(time_t *t)
@@ -24,8 +42,15 @@ cdef extern from '<Python.h>' nogil:
2442
cdef int __LINE__
2543
char *__FUNCTION__
2644

27-
cdef extern from "<pthread.h>" nogil:
28-
ctypedef union pthread_mutex_t
45+
cdef extern from '<pthread.h>' nogil:
46+
ctypedef struct pthread_mutex_t:
47+
pass
48+
49+
ctypedef struct pthread_cond_t:
50+
pass
51+
52+
ctypedef struct pthread_t:
53+
pass
2954

3055
cdef extern from *:
3156
ctypedef struct slurm_job_credential

pyslurm/slurm/extra.pxi

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,132 @@
1+
#
2+
# Structs that are not in the Slurm headers, which need to be redefined
3+
# in order to implement certain features.
4+
#
5+
# For example: to communicate with the slurmctld directly in order
6+
# to retrieve the actual batch-script as a string.
7+
#
8+
9+
# https://github.com/SchedMD/slurm/blob/26abe9188ea8712ba1eab4a8eb6322851f06a108/src/common/slurm_persist_conn.h#L51
10+
ctypedef enum persist_conn_type_t:
11+
PERSIST_TYPE_NONE = 0
12+
PERSIST_TYPE_DBD
13+
PERSIST_TYPE_FED
14+
PERSIST_TYPE_HA_CTL
15+
PERSIST_TYPE_HA_DBD
16+
PERSIST_TYPE_ACCT_UPDATE
17+
18+
# https://github.com/SchedMD/slurm/blob/26abe9188ea8712ba1eab4a8eb6322851f06a108/src/common/slurm_persist_conn.h#L59
19+
ctypedef struct persist_msg_t:
20+
void *conn
21+
void *data
22+
uint32_t data_size
23+
uint16_t msg_type
24+
25+
ctypedef int (*_slurm_persist_conn_t_callback_proc) (void *arg, persist_msg_t *msg, buf_t **out_buffer, uint32_t *uid)
26+
ctypedef void (*_slurm_persist_conn_t_callback_fini)(void *arg)
27+
28+
# https://github.com/SchedMD/slurm/blob/26abe9188ea8712ba1eab4a8eb6322851f06a108/src/common/slurm_persist_conn.h#L66
29+
ctypedef struct slurm_persist_conn_t:
30+
void *auth_cred
31+
_slurm_persist_conn_t_callback_proc callback_proc
32+
_slurm_persist_conn_t_callback_fini callback_fini
33+
char *cluster_name
34+
time_t comm_fail_time
35+
uint16_t my_port
36+
int fd
37+
uint16_t flags
38+
bool inited
39+
persist_conn_type_t persist_type
40+
uid_t r_uid
41+
char *rem_host
42+
uint16_t rem_port
43+
time_t *shutdown
44+
pthread_t thread_id
45+
int timeout
46+
slurm_trigger_callbacks_t trigger_callbacks;
47+
uint16_t version
48+
49+
# https://github.com/SchedMD/slurm/blob/20e2b354168aeb0f76d67f80122d80925c2ef32b/src/common/pack.h#L68
50+
ctypedef struct buf_t:
51+
uint32_t magic
52+
char *head
53+
uint32_t size
54+
uint32_t processed
55+
bool mmaped
56+
57+
# https://github.com/SchedMD/slurm/blob/20e2b354168aeb0f76d67f80122d80925c2ef32b/src/common/pack.h#L68
58+
ctypedef struct return_code_msg_t:
59+
uint32_t return_code
60+
61+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.h#L650
62+
ctypedef struct job_id_msg_t:
63+
uint32_t job_id
64+
uint16_t show_flags
65+
66+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.h#L216
67+
# Only partially defined - not everything needed at the moment.
68+
ctypedef enum slurm_msg_type_t:
69+
REQUEST_SHARE_INFO = 2022
70+
REQUEST_BATCH_SCRIPT = 2051
71+
RESPONSE_BATCH_SCRIPT = 2052
72+
RESPONSE_SLURM_RC = 8001
73+
74+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.h#L469
75+
ctypedef struct forward_t:
76+
uint16_t cnt
77+
uint16_t init
78+
char *nodelist
79+
uint32_t timeout
80+
uint16_t tree_width
81+
82+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.h#L491
83+
ctypedef struct forward_struct_t:
84+
char *buf
85+
int buf_len
86+
uint16_t fwd_cnt
87+
pthread_mutex_t forward_mutex
88+
pthread_cond_t notify
89+
List ret_list
90+
uint32_t timeout
91+
92+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.h#L514
93+
ctypedef struct slurm_msg_t:
94+
slurm_addr_t address
95+
void *auth_cred
96+
int auth_index
97+
uid_t auth_uid
98+
bool auth_uid_set
99+
uid_t restrict_uid
100+
bool restrict_uid_set
101+
uint32_t body_offset
102+
buf_t *buffer
103+
slurm_persist_conn_t *conn
104+
int conn_fd
105+
void *data
106+
uint32_t data_size
107+
uint16_t flags
108+
uint8_t hash_index
109+
uint16_t msg_type
110+
uint16_t protocol_version
111+
forward_t forward
112+
forward_struct_t *forward_struct
113+
slurm_addr_t orig_addr
114+
List ret_list
115+
116+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.c#L865
117+
cdef extern void slurm_free_return_code_msg(return_code_msg_t *msg)
118+
119+
# https://github.com/SchedMD/slurm/blob/2d2e83674b59410a7ed8ab6fc8d8acfcfa8beaf9/src/common/slurm_protocol_api.c#L2401
120+
cdef extern int slurm_send_recv_controller_msg(slurm_msg_t *request_msg,
121+
slurm_msg_t *response_msg,
122+
slurmdb_cluster_rec_t *working_cluster_rec)
123+
124+
# https://github.com/SchedMD/slurm/blob/fe82218def7b57f5ecda9222e80662ebbb6415f8/src/common/slurm_protocol_defs.c#L168
125+
cdef extern void slurm_msg_t_init(slurm_msg_t *msg)
126+
127+
1128
# Global Environment
129+
2130
cdef extern char **environ
3131

4132
#

0 commit comments

Comments
 (0)