jobrec_write(), write_sidecar() and provstate's write_state() wrote into a
temp, ignored the result and renamed it over the live file. A full disk (or
EIO) then left a torn json where a job record or a key's quota state used to
be. For provstate that is the dangerous direction: a truncated state parses as
all zeroes, so "this key is out of quota until 19:00" became "admit it" and the
harness hammered a spent account.
All three now fflush+fsync+fclose, unlink the temp when any step fails, and
only then rename. Test proof, both tests fail on the old code:
test_provstate.c RLIMIT_FSIZE=8, write_state fails -> last_status still 429,
preflight still waits
test_jobs.c RLIMIT_FSIZE=8, delegate_retry cannot write -> the record on
disk is still a whole json, no .tmp left behind
make check: all tests passed. tests/check_asan.sh: all 45 binaries clean.
289 lines
8.7 KiB
C
289 lines
8.7 KiB
C
/*
|
|
* splinter -- durable delegation jobs and the scheduler's view.
|
|
*
|
|
* submit writes a job before it runs, result can be polled later, jobs lists
|
|
* the store, retry resumes a job, and sp_delegate_tick is the one step the
|
|
* scheduler plugin takes when a job is due. The fake ssh is the far side.
|
|
*/
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/resource.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "splinter_plugin.h"
|
|
#include "config.h"
|
|
|
|
static int failures;
|
|
|
|
#define CHECK(cond) do { \
|
|
if (!(cond)) { \
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
|
failures++; \
|
|
} \
|
|
} while (0)
|
|
|
|
static void
|
|
add(struct sp_config *c, const char *id, const char *about, const char *tags)
|
|
{
|
|
struct sp_delegate *d = &c->delegate[c->ndelegates++];
|
|
|
|
memset(d, 0, sizeof(*d));
|
|
snprintf(d->id, sizeof(d->id), "%s", id);
|
|
snprintf(d->about, sizeof(d->about), "%s", about);
|
|
snprintf(d->tags, sizeof(d->tags), "%s", tags);
|
|
snprintf(d->endpoint, sizeof(d->endpoint), "http://example/v1");
|
|
snprintf(d->model, sizeof(d->model), "m");
|
|
d->max_parallel = 1;
|
|
}
|
|
|
|
static void
|
|
job_id_from(const char *s, char *id, size_t cap)
|
|
{
|
|
const char *p = s != NULL ? strstr(s, "job job-") : NULL;
|
|
size_t n = 0;
|
|
|
|
id[0] = '\0';
|
|
if (p == NULL)
|
|
return;
|
|
p += 4; /* "job " */
|
|
while (p[n] != '\0' && p[n] != ' ' && p[n] != ',' &&
|
|
p[n] != '\n' && n + 1 < cap)
|
|
n++;
|
|
memcpy(id, p, n);
|
|
id[n] = '\0';
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
struct sp_config c;
|
|
struct sp_registry r;
|
|
struct sp_session_info sess;
|
|
struct sp_agent ag;
|
|
struct sp_host *host;
|
|
struct sp_buf out, err, tick;
|
|
const struct sp_tool *t;
|
|
char tdir[] = "/tmp/splinter-job-XXXXXX";
|
|
char path[600], line[700], id[80], args[256];
|
|
char manual[700], snap[512];
|
|
const char *oldpath = getenv("PATH");
|
|
FILE *f;
|
|
|
|
CHECK(mkdtemp(tdir) != NULL);
|
|
memset(&sess, 0, sizeof(sess));
|
|
snprintf(sess.dir, sizeof(sess.dir), "%s", tdir);
|
|
memset(&r, 0, sizeof(r));
|
|
memset(&ag, 0, sizeof(ag));
|
|
memset(&c, 0, sizeof(c));
|
|
|
|
/* the far side answers */
|
|
snprintf(path, sizeof(path), "%s/ssh", tdir);
|
|
f = fopen(path, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("#!/bin/sh\necho 'antwort vom job'\n", f);
|
|
fclose(f);
|
|
}
|
|
(void)chmod(path, 0755);
|
|
snprintf(line, sizeof(line), "%s:%s", tdir,
|
|
oldpath != NULL ? oldpath : "/usr/bin:/bin");
|
|
(void)setenv("PATH", line, 1);
|
|
|
|
add(&c, "jobber", "jobs erledigen", "job");
|
|
snprintf(c.delegate[0].host, sizeof(c.delegate[0].host), "jobsrv");
|
|
|
|
CHECK(sp_buf_init(&out) == 0);
|
|
CHECK(sp_buf_init(&err) == 0);
|
|
CHECK(sp_buf_init(&tick) == 0);
|
|
host = sp_host_new(&r, &ag, &sess);
|
|
CHECK(host != NULL);
|
|
CHECK(sp_delegate_register(&r, &c, host) == 0);
|
|
|
|
/* delegate_list names the configured delegate */
|
|
t = sp_registry_find(&r, "delegate_list");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run("{}", &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "jobber") != NULL);
|
|
|
|
/* submit: a job exists before and after the run, and it is done */
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_submit");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run("{\"task\":\"mach was\",\"tags\":\"job\"}", &out,
|
|
t->ctx);
|
|
if (getenv("TEST_VERBOSE") != NULL)
|
|
fprintf(stderr, "submit: %s\n", out.p);
|
|
CHECK(out.p != NULL && strstr(out.p, "[delegate_submit job ") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "state=done") != NULL);
|
|
job_id_from(out.p, id, sizeof(id));
|
|
CHECK(id[0] != '\0');
|
|
|
|
/* jobs: the store names it, its delegate and its state */
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_jobs");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run("{}", &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, id) != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "state=done") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "delegate=jobber") != NULL);
|
|
|
|
/* result: pollable after the fact */
|
|
snprintf(args, sizeof(args), "{\"id\":\"%s\"}", id);
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_result");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run(args, &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "state=done") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "antwort vom job") != NULL);
|
|
|
|
/* a done job is not retried by accident; force does it */
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_retry");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run(args, &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "already done") != NULL);
|
|
|
|
snprintf(args, sizeof(args), "{\"id\":\"%s\",\"force\":\"1\"}", id);
|
|
sp_buf_clear(&out);
|
|
if (t != NULL)
|
|
(void)t->run(args, &out, t->ctx);
|
|
if (getenv("TEST_VERBOSE") != NULL)
|
|
fprintf(stderr, "force: %s\n", out.p);
|
|
CHECK(out.p != NULL && strstr(out.p, "[retry job ") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "antwort vom job") != NULL);
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_result");
|
|
if (t != NULL)
|
|
(void)t->run(args, &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "attempts=2") != NULL);
|
|
|
|
/* a hand-written due job: the scheduler view sees it and the tick retries */
|
|
snprintf(path, sizeof(path), "%s/jobs", tdir);
|
|
CHECK(mkdir(path, 0755) == 0 || access(path, F_OK) == 0);
|
|
snprintf(manual, sizeof(manual), "%s/job-due.json", path);
|
|
f = fopen(manual, "w");
|
|
CHECK(f != NULL);
|
|
if (f != NULL) {
|
|
fputs("{\"id\":\"job-due\",\"state\":\"waiting_poll\","
|
|
"\"delegate\":\"jobber\",\"task\":\"retry me\","
|
|
"\"next_poll_at\":\"1\",\"run_after\":\"\","
|
|
"\"attempts\":1}\n", f);
|
|
fclose(f);
|
|
}
|
|
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_due");
|
|
CHECK(t != NULL);
|
|
if (t != NULL)
|
|
(void)t->run("{}", &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "job-due") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "state=waiting_poll") != NULL);
|
|
|
|
CHECK(sp_delegate_tick(&tick) == 0);
|
|
if (getenv("TEST_VERBOSE") != NULL)
|
|
fprintf(stderr, "tick: %s\n", tick.p);
|
|
CHECK(tick.p != NULL && strstr(tick.p, "scheduler: retrying job-due") != NULL);
|
|
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_result");
|
|
if (t != NULL)
|
|
(void)t->run("{\"id\":\"job-due\"}", &out, t->ctx);
|
|
CHECK(out.p != NULL && strstr(out.p, "state=done") != NULL);
|
|
CHECK(out.p != NULL && strstr(out.p, "antwort vom job") != NULL);
|
|
|
|
/*
|
|
* ---- a job record that cannot be written keeps the old one ----
|
|
*
|
|
* jobrec_write() used to fputs() into a temp, ignore fclose() and rename
|
|
* the temp over the record. A full disk then replaced a job's state and
|
|
* remote id with a torn json, and reading it back gave a job with no
|
|
* delegate and no state. RLIMIT_FSIZE makes the short write certain.
|
|
*/
|
|
{
|
|
struct rlimit rl;
|
|
struct sigaction sa, oldsa;
|
|
char rec[700], args2[200];
|
|
char fbuf[2048];
|
|
size_t got;
|
|
|
|
snprintf(rec, sizeof(rec), "%s/jobs/%s.json", tdir, id);
|
|
CHECK(access(rec, R_OK) == 0);
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
sa.sa_handler = SIG_IGN;
|
|
sigemptyset(&sa.sa_mask);
|
|
CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0);
|
|
CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0);
|
|
{
|
|
struct rlimit lim = rl;
|
|
|
|
lim.rlim_cur = 8; /* far less than a record */
|
|
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
|
|
}
|
|
/* this retry cannot write its record: the old one must survive */
|
|
snprintf(args2, sizeof(args2), "{\"id\":\"%s\",\"force\":\"1\"}", id);
|
|
sp_buf_clear(&out);
|
|
t = sp_registry_find(&r, "delegate_retry");
|
|
if (t != NULL)
|
|
(void)t->run(args2, &out, t->ctx);
|
|
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
|
|
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
|
|
|
|
/* the record on disk is still a whole one, not a truncated line */
|
|
f = fopen(rec, "r");
|
|
CHECK(f != NULL);
|
|
got = 0;
|
|
if (f != NULL) {
|
|
got = fread(fbuf, 1, sizeof(fbuf) - 1, f);
|
|
fclose(f);
|
|
}
|
|
fbuf[got] = '\0';
|
|
CHECK(got > 8);
|
|
CHECK(fbuf[0] == '{');
|
|
CHECK(strstr(fbuf, "\"id\"") != NULL);
|
|
CHECK(strchr(fbuf, '}') != NULL);
|
|
/* no debris beside it */
|
|
snprintf(rec, sizeof(rec), "%s/jobs/%s.json.tmp", tdir, id);
|
|
CHECK(access(rec, F_OK) != 0);
|
|
}
|
|
|
|
/* the delegate host is done; the scheduler gets its own registry */
|
|
sp_host_free(host);
|
|
sp_registry_free(&r);
|
|
|
|
/* the scheduler plugin is a widget around that tick: it starts idle */
|
|
{
|
|
struct sp_host *sh;
|
|
|
|
memset(&r, 0, sizeof(r));
|
|
sh = sp_host_new(&r, &ag, &sess);
|
|
CHECK(sh != NULL);
|
|
CHECK(sp_plugin_load(sh, "plugins/scheduler/scheduler.so", &err) == 0);
|
|
CHECK(sp_host_widget_snapshot(sh, snap, sizeof(snap)) > 0);
|
|
/* idle says nothing: the widget exists, its text is empty */
|
|
CHECK(strstr(snap, "scheduler") != NULL);
|
|
sp_host_free(sh);
|
|
sp_registry_free(&r);
|
|
}
|
|
|
|
sp_buf_free(&out);
|
|
sp_buf_free(&err);
|
|
sp_buf_free(&tick);
|
|
(void)setenv("PATH", oldpath != NULL ? oldpath : "/usr/bin:/bin", 1);
|
|
snprintf(line, sizeof(line), "rm -rf '%s'", tdir);
|
|
(void)system(line);
|
|
|
|
if (failures == 0)
|
|
printf("test_jobs: ok\n");
|
|
return failures == 0 ? 0 : 1;
|
|
}
|