skip to content
==> include/mesh/jobd/jobd.h <==
1/* @ajax/mesh-jobd: meshd's jobs thread and blob thread (jobd.rs `run`, blobd.rs `run` and its writers)
2 * over the daemon's shared state (@ajax/mesh-shared).
3 *
4 * - The jobs thread runs this meshd's job node (@ajax/mesh-jobs-node) -- its replica of the fleet's job
5 *   table, the leader's dispatcher when it leads, and its side of the leases -- over the peer links
6 *   (F_JOB_* frames, F_LEASE, F_RESULT), the driver (MESH_CMD_JOB_LEASE / JOB_CUT / JOB_PROGS,
7 *   MESH_JEV_DONE) and the job log (@ajax/mesh-jobd-store, fsynced before an answer or a send that
8 *   depends on it; a failure is a fail-stop). A single meshd runs it too: it is its own leader (term 1).
9 *   Every 250 ms it publishes the host block's S_JOB_* fields, the jobs view's records
10 *   (sh->jobs_view_recs) and blobd's news.
11 * - The blob thread runs @ajax/mesh-blobd on sh->blobs, with MESH_BLOBD_WRITERS writer threads making
12 *   the references durable in batches.
13 *
14 * Wiring (meshd): mjd_open after mesh_shared_new (it creates sh->blobs and sh->jobs_ext, and replays
15 * the job log and indexes the blob store before anything runs), `mesh_control_render = mjd_render`,
16 * mjd_start once the driver runs. The fleet thread (@ajax/mesh-peer-relay's mpr_host) calls mjd_set_view
17 * on every change of its view (jobs_view), mjd_jobs_sched with the scheduler's model (jobs_sched), and
18 * mjd_jobs_frame / mjd_blobs_frame for the job and blob frames (jobs_frame, blobs_frame). */
19#ifndef MESH_JOBD_JOBD_H
20#define MESH_JOBD_JOBD_H
21#include "mesh/jobs/node.h"
22#include "mesh/jobs/select.h"
23#include "mesh/fleet_doc.h"
24#include "mesh/sched_member.h"
25#include "mesh/sched_weights.h"
26#include "mesh/shared.h"
27#include <stdbool.h>
28#include <stddef.h>
29#include <stdint.h>
30
31/* A table frame waits this long for room in a link's queue before the rest of the batch to that
32 * member is dropped (a gap it reports, or a pull asked again). */
33#define MESH_JOBD_TABLE_SEND_WAIT_MS 2000
34/* The driver's job programs are asked, the view published and blobd told, this often. */
35#define MESH_JOBD_PROGS_MS 250
36#define MESH_JOBD_PUBLISH_MS 250
37
38typedef struct {
39    const char *jobs_state; /* --jobs-state: the job log (NULL: the table in memory) */
40    const char *blobs;      /* --blobs: the blob store's directory (NULL: in memory) */
41    uint64_t blobs_cap;     /* bytes */
42    uint64_t dead_ms;       /* a member silent this long is dead (the fleet's tuning; MESH_PEER_DEAD_MS) */
43} mjd_config;
44
45typedef struct mjd mjd;
46
47/* The job log replayed and the blob store indexed; sh->blobs and sh->jobs_ext made. NULL with *err
48 * (meshd fails). */
49mjd *mjd_open(mesh_shared *sh, const mjd_config *cfg, char **err);
50/* The jobs thread, the blob thread and its writers. */
51void mjd_start(mjd *d);
52/* Stops and joins them (tests; meshd exits without). sh->blobs and sh->jobs_ext stay until mjd_free. */
53void mjd_stop(mjd *d);
54/* After mjd_stop (or without mjd_start): frees what mjd_open made, sh->blobs and sh->jobs_ext with it. */
55void mjd_free(mjd *d);
56
57/* The fleet thread: its view (term, leader, alive, linked). Ignored before mjd_open and when single. */
58void mjd_set_view(mesh_shared *sh, mj_view v);
59/* The fleet thread: the scheduler's model for the lease choice (takes cx; the jobs thread picks it up). */
60void mjd_set_sched(mesh_shared *sh, mj_sched *cx);
61/* The fleet thread (the relay's jobs_sched): the scheduler's model -- the members, the committed
62 * document's programs' needs, the measured performance and the weights (all copied). */
63void mjd_jobs_sched(mesh_shared *sh, const msm_member *members, size_t n, const mfd_doc *doc, const msm_perfs *perf, const msw_weights *w);
64/* The fleet thread (the relay's jobs_frame and blobs_frame): a job frame (F_JOB_AVAIL..F_RESULT) to
65 * the jobs thread, a blob frame (F_BLOB_PUT..F_BLOB) to the blob thread; the body after the length
66 * (copied). False if the thread is gone. */
67bool mjd_jobs_frame(mesh_shared *sh, uint32_t from, uint8_t ty, const uint8_t *body, size_t n);
68bool mjd_blobs_frame(mesh_shared *sh, uint32_t from, uint8_t ty, const uint8_t *body, size_t n);
69/* A link frame (type byte, u32 length, body) from member from: a job frame to the jobs thread, a blob
70 * frame to the blob thread. False if it is neither or the thread is gone (the frame is dropped). */
71bool mjd_route_frame(mesh_shared *sh, uint32_t from, const uint8_t *frame, size_t n);
72/* `render [<job>|latest] [status|ppm|bmp|raw]` (the words after `render`) through the blob thread:
73 * the answer's bytes, or false with *err. The signature of @ajax/mesh-control's mesh_control_render. */
74bool mjd_render(mesh_shared *sh, char **words, size_t n, uint8_t **out, size_t *len, char **err);
75
76#endif