Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for RFC19 F58 encoded JOBIDs #3045

Merged
merged 19 commits into from
Jul 24, 2020
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
libjob: add flux_job_id_parse(), flux_job_id_encode()
Problem: FLUID string encoding is not exported via a public API,
but this functionality may be useful and/or necessary for callers,
especially for the case of converting JOBIDs between different
encodings.

Add flux_job_id_parse() and flux_jobid_encode() to libjob to allow
Flux API users to easily parse a Flux JOBID from any supported
string representation, or encode a `flux_jobid_t` to any supported
representation. The supported representations currently include
all standard FLUID encodings:

 - decimal integer
 - hexidecimal integer, prefixed with "0x"
 - dotted hex format, "XXXX.XXXX.XXXX.XXXX"
 - KVS path, "job.XXXX.XXXX.XXXX.XXXX"
 - Words, "X-X-X-X--X-X-X-X"
 - RFC 19 F58 encoding, prefixed with ƒ or f

Addition of these functions to the libflux API will also make it
easy to add support to bindings for encoding and decoding JOBIDs.

The functions are named `flux_job_id_*` instead of `flux_jobid_*`
to keep consistent naming with the other libjob functions, as well
as to make it easy to call the functions from Python cffi.
grondo committed Jul 24, 2020
commit f5bfe0b7de8258c0e714e7e5f89e34a84b5a892b
80 changes: 80 additions & 0 deletions src/common/libjob/job.c
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@
#endif
#include <unistd.h>
#include <sys/types.h>
#include <ctype.h>
#include <flux/core.h>
#if HAVE_FLUX_SECURITY
#include <flux/security/sign.h>
@@ -558,6 +559,85 @@ int flux_job_strtoresult (const char *s, flux_job_result_t *result)
return -1;
}

int flux_job_id_parse (const char *s, flux_jobid_t *idp)
{
int len;
const char *p = s;
if (s == NULL
|| idp == NULL
|| (len = strlen (s)) == 0) {
errno = EINVAL;
return -1;
}
/* Remove leading whitespace
*/
while (isspace(*p))
p++;
/* Ignore any `job.` prefix. This allows a "kvs" encoding
* created by flux_job_id_encode(3) to properly decode.
*/
if (strncmp (p, "job.", 4) == 0)
p += 4;
return fluid_parse (p, idp);
}

int flux_job_id_encode (flux_jobid_t id,
const char *type,
char *buf,
size_t bufsz)
{
fluid_string_type_t t;
if (buf == NULL) {
errno = EINVAL;
return -1;
}
if (type == NULL || strcasecmp (type, "dec") == 0) {
int len = snprintf (buf, bufsz, "%ju", (uintmax_t) id);
if (len >= bufsz) {
errno = ENOSPC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: EOVERFLOW?

return -1;
}
return 0;
}
if (strcasecmp (type, "hex") == 0) {
int len = snprintf (buf, bufsz, "0x%jx", (uintmax_t) id);
if (len >= bufsz) {
errno = ENOSPC;
return -1;
}
return 0;
}

/* The following encodings all use fluid_encode(3).
*/
if (strcasecmp (type, "kvs") == 0) {
/* kvs: prepend "job." to "dothex" encoding.
*/
int len = snprintf (buf, bufsz, "job.");
if (len >= bufsz) {
errno = ENOSPC;
return -1;
}
buf += len;
bufsz -= len;
type = "dothex";
}
if (strcasecmp (type, "dothex") == 0)
t = FLUID_STRING_DOTHEX;
else if (strcasecmp (type, "words") == 0)
t = FLUID_STRING_MNEMONIC;
else if (strcasecmp (type, "f58") == 0)
t = FLUID_STRING_F58;
else {
/* Return EPROTO for invalid type to differentiate from
* other invalid arguments.
*/
errno = EPROTO;
return -1;
}
return fluid_encode (buf, bufsz, id, t);
}

/*
* vi:tabstop=4 shiftwidth=4 expandtab
*/
16 changes: 16 additions & 0 deletions src/common/libjob/job.h
Original file line number Diff line number Diff line change
@@ -63,6 +63,22 @@ typedef enum {

typedef uint64_t flux_jobid_t;

/* Parse a jobid from NULL-teminated string 's' in any supported encoding.
* Returns 0 on success, -1 on failure.
*/
int flux_job_id_parse (const char *s, flux_jobid_t *id);

/* Encode a jobid into encoding "type", writing the result to buffer
* buf of size bufsz.
* Supported encoding types include:
* "dec", "hex", "kvs", "dothex", "words", or "f58".
* Returns 0 on success, -1 on failure with errno set:
* EPROTO: Invalid encoding type
* EINVAL: Invalid other argument
*/
int flux_job_id_encode (flux_jobid_t id, const char *type,
char *buf, size_t bufsz);

const char *flux_job_statetostr (flux_job_state_t state, bool single_char);

int flux_job_strtostate (const char *s, flux_job_state_t *state);