From bbe7ba05ac01601c094629564af08c004385e144 Mon Sep 17 00:00:00 2001 From: Albert Chu Date: Tue, 29 Aug 2023 22:05:03 -0700 Subject: [PATCH] job-list: add R update helper function Problem: In the near future we would like to update R with new information. Add a new helper function job_R_update() to update R. Add unit tests. --- src/modules/job-list/job_data.c | 23 ++++++++++++ src/modules/job-list/job_data.h | 5 +++ src/modules/job-list/test/job_data.c | 56 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/src/modules/job-list/job_data.c b/src/modules/job-list/job_data.c index 1bb2227cb38a..a30286fbbaf1 100644 --- a/src/modules/job-list/job_data.c +++ b/src/modules/job-list/job_data.c @@ -504,6 +504,29 @@ int job_jobspec_update (struct job *job, json_t *updates) return parse_jobspec (job, false); } +int job_R_update (struct job *job, json_t *updates) +{ + const char *key; + json_t *value; + + if (!updates) + return 0; + + json_object_foreach (updates, key, value) { + /* RFC 21 resource-update event only allows update + * to: + * - expiration + */ + if (streq (key, "expiration")) + if (jpath_set (job->R, "execution.expiration", value) < 0) + flux_log (job->h, LOG_INFO, + "%s: job %s failed to update R key %s", + __FUNCTION__, idf58 (job->id), key); + } + + return parse_R (job, false); +} + /* * vi:tabstop=4 shiftwidth=4 expandtab */ diff --git a/src/modules/job-list/job_data.h b/src/modules/job-list/job_data.h index 713efd5a8e8d..98c5e96af298 100644 --- a/src/modules/job-list/job_data.h +++ b/src/modules/job-list/job_data.h @@ -134,6 +134,11 @@ int job_parse_R (struct job *job, const char *s); */ int job_parse_R_fatal (struct job *job, const char *s); +/* Update R with RFC21 defined keys + * (i.e. "expiration") and value. + */ +int job_R_update (struct job *job, json_t *updates); + #endif /* ! _FLUX_JOB_LIST_JOB_DATA_H */ /* diff --git a/src/modules/job-list/test/job_data.c b/src/modules/job-list/test/job_data.c index 39d90b020a8d..aad5c289c428 100644 --- a/src/modules/job-list/test/job_data.c +++ b/src/modules/job-list/test/job_data.c @@ -806,6 +806,61 @@ static void test_jobspec_update (void) free (data); } +static void test_R_update (void) +{ + struct job *job = job_create (NULL, FLUX_JOBID_ANY); + const char *filename = TEST_SRCDIR "/R/1node_1core.R"; + char *data; + int ret; + double expiration; + const char *tmp = NULL; + json_t *o; + + if (!job) + BAIL_OUT ("job_create failed"); + + read_file (filename, (void **)&data); + + if (job_parse_R (job, data) < 0) + BAIL_OUT ("cannot load basic R"); + + ret = json_unpack (job->R, + "{s:{s:F}}", + "execution", + "expiration", &expiration); + ok (ret == 0, "parsed initial R expiration"); + + ok (expiration == 0.0, "initial R expiration == 0.0"); + ok (job->expiration == 0.0, "initial job->expiration == 0.0"); + + ret = job_R_update (job, NULL); + ok (ret == 0, "job_R_update success with no update"); + + if (!(o = json_pack ("{s:f s:s}", + "expiration", 100.0, + "dummy", "dummy"))) + BAIL_OUT ("json_pack failed"); + ret = job_R_update (job, o); + ok (ret == 0, "job_R_update"); + json_decref (o); + + ret = json_unpack (job->R, + "{s:{s:F}}", + "execution", + "expiration", &expiration); + ok (ret == 0, "parsed updated R expiration"); + + ok (expiration == 100.0, "R expiration == 100.0"); + ok (job->expiration == 100.0, "job->expiration == 100.0"); + + ret = json_unpack (job->R, "{s?s}", "dummy", &tmp); + ok (ret == 0, "parsed updated R dummy"); + + ok (tmp == NULL, "R not updated with illegal update key"); + + free (data); +} + int main (int argc, char *argv[]) { plan (NO_PLAN); @@ -823,6 +878,7 @@ int main (int argc, char *argv[]) test_ntasks (); test_ncores (); test_jobspec_update (); + test_R_update (); done_testing (); }