skip to content
==> include/mesh/jobs/table.h <==
1/* @ajax/mesh-jobs-table: the job table (jobs.rs `Job`, `Table`): every job with what is known of
2 * it, changed only by applying ops (@ajax/mesh-jobs-op).
3 *
4 * The table is a join-semilattice: every op is idempotent and applying two tables' ops in either
5 * order gives the same table, their union, in which an item's first result wins. That is what lets
6 * a new leader merge a majority's tables without matching logs. */
7#ifndef MESH_JOBS_TABLE_H
8#define MESH_JOBS_TABLE_H
9
10#include "mesh/jobs/id.h"
11#include "mesh/jobs/op.h"
12#include "mesh/jobs/ranges.h"
13#include "mesh/jobs/spec.h"
14#include "mesh/omap.h"
15
16#include <stdbool.h>
17#include <stddef.h>
18#include <stdint.h>
19
20#define MESH_JOBS_FIN_DONE 1u
21#define MESH_JOBS_FIN_CANCELLED 2u
22#define MESH_JOBS_FIN_DEADLINE 3u
23
24/* A run: done items [start, end) with no result kept, by one lease on one member. */
25typedef struct {
26    uint64_t end, lease;
27    uint32_t member;
28} mj_run;
29
30/* An item failed for good. */
31typedef struct {
32    uint32_t tries;
33    char *why;
34} mj_failed;
35
36typedef struct {
37    mj_spec spec;
38    mj_ranges done;     /* items done (whatever the mode) */
39    momap *results;     /* item -> mj_res: mode collect, every done item's; nonzero, those not 0 */
40    momap *runs;        /* start -> mj_run: done items with no result kept */
41    momap *failed;      /* item -> mj_failed: failed for good */
42    momap *fails;       /* item -> uint32_t: tries failed so far (max-merged) */
43    uint64_t dupes;
44    char *cancel;       /* NULL: not cancelled */
45    bool finished;
46    uint64_t finish_at; /* unix ms */
47    uint32_t finish_state; /* FIN_* */
48    momap *by_member;   /* member -> uint64_t items done (from the done ops as they apply) */
49    /* The runs folded away: every item from the job's first to fold_end is done, and `folded` has, per
50     * member, the items of the runs dropped (review finding 7). */
51    uint64_t fold_end;
52    momap *folded;      /* member -> uint64_t */
53    uint32_t tone_seq, tone; /* the presentation word and its change count (the highest count wins) */
54} mj_job;
55
56mj_job *mj_job_new(const mj_spec *spec); /* copies spec */
57void mj_job_free(mj_job *j);
58mj_job *mj_job_clone(const mj_job *j);
59bool mj_job_eq(const mj_job *a, const mj_job *b);
60
61uint64_t mj_job_total(const mj_job *j);
62bool mj_job_settled(const mj_job *j, uint64_t x);
63/* The lease that ran done item x; false if it has none on record. */
64bool mj_job_lease_of(const mj_job *j, uint64_t x, uint64_t *lease);
65bool mj_job_active(const mj_job *j);
66/* Every item done or failed (an endless job never is). */
67bool mj_job_complete(const mj_job *j);
68/* done, done-with-failures, cancelled, deadline, cancelling, running. */
69const char *mj_job_state(const mj_job *j);
70/* Items not done and not failed. */
71mj_ranges mj_job_left(const mj_job *j);
72/* Small-map helpers: a member's count (0 when absent). */
73uint64_t mj_count_get(const momap *m, uint32_t member);
74
75typedef struct {
76    momap *jobs;           /* id -> mj_job * */
77    mj_job_id *forgotten;  /* jobs forgotten (`job forget`): their ops are ignored */
78    size_t nforgotten, capforgotten;
79} mj_table;
80
81void mj_table_init(mj_table *t);
82void mj_table_free(mj_table *t);
83mj_table mj_table_clone(const mj_table *t);
84bool mj_table_eq(const mj_table *a, const mj_table *b);
85mj_job *mj_table_get(const mj_table *t, mj_job_id id);
86bool mj_table_forgot(const mj_table *t, mj_job_id id);
87
88void mj_table_apply(mj_table *t, const mj_op *op);
89/* The whole table as ops (a snapshot, a pull's answer), appended to out. */
90void mj_table_ops(const mj_table *t, mj_ops *out);
91/* The next job id of term `term`. */
92mj_job_id mj_table_next_id(const mj_table *t, uint64_t term);
93
94/* Iteration in id order: mj_job *j; uint64_t id; for (j = mj_table_first(t, &id); j; j = mj_table_next(t, &id)). */
95mj_job *mj_table_first(const mj_table *t, uint64_t *id);
96mj_job *mj_table_next(const mj_table *t, uint64_t *id);
97
98#endif