include/pq_inserver.h

filter: Statementstmt 90.3% 56 / 62 Branchbran 50.0% 31 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 70.1% 87 / 124 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/job.xs ↑ Summary include/pq_hm.h »
1 #ifndef PQ_INSERVER_H
2 #define PQ_INSERVER_H
3  
4 /* pq_inserver.h - the in-server worker: claims on a Hyperman WEB worker's
5 * own loop, attached lazily from the plugin's before_dispatch hook (to_app
6 * runs in the pre-fork parent, and hm_abi has no post-fork hook).
7 *
8 * This is the internal seam that replaced the deferred ABI's worker_new:
9 * the plugin is same-dist code, so one XSUB call per process attaches the
10 * machinery and the hot path (the repeating claim timer) is C from there.
11 *
12 * The rails, enforced here rather than documented and hoped for:
13 * - a task allowlist is required (the caller croaks without one)
14 * - one job in flight per web worker, hard (the busy flag)
15 * - claims fire only from the timer, never from the request path
16 * - a wall-clock cap: jobs claimed here get their timeout clamped to
17 * it, so perform's loop-timer layer fails a slow future, and the
18 * wall time of every run is measured regardless
19 * - two cap breaches disable the mode in this process, loudly
20 *
21 * One instance per process, by design: the web worker is the unit.
22 *
23 * Include after pq_hm.h and pq_queue.h. */
24  
25 typedef struct pq_inserver {
26 SV *queue; /* the Punk::Queue, owned */
27 AV *queues, *tasks; /* owned */
28 const hm_abi *A;
29 void *loop;
30 double interval, cap;
31 int busy, breaches, disabled;
32 } pq_inserver;
33  
34 static pq_inserver *PQIS = NULL;
35  
36 static void pq_inserver_tick(pTHX_ void *ud);
37  
38 10 static void pq_inserver_arm(pTHX_ pq_inserver *is) {
39 10 ▶ if (is->disabled) return;
Branch: 1 branch
BranchTrueFalseTotal
if (is->disabled) return; 0 10 10
40 10 (void)is->A->timer(aTHX_ is->loop, is->interval, pq_inserver_tick, is);
41 }
42  
43 /* The claim tick. Runs on the loop thread inside the dispatch loop, so
44 * everything is trapped (the hm contract: never croak out of a callback)
45 * and the re-arm happens FIRST, so a die anywhere below cannot silently
46 * stop the mode without tripping the disable path. */
47 9 static void pq_inserver_tick(pTHX_ void *ud) {
48 9 pq_inserver *is = (pq_inserver *)ud;
49 9 SV *job = NULL;
50 9 int died = 0;
51  
52 9 pq_inserver_arm(aTHX_ is);
53 9 ▶ if (is->busy || is->disabled) return;
Branch: 2 branches
BranchTrueFalseTotal
if (is->busy || is->disabled) return; 9 0 9
if (is->busy || is->disabled) return; 0 9 9
54  
55 9 ENTER; SAVETMPS;
56 {
57 SV *argv[3];
58 9 argv[0] = sv_2mortal(newSViv(0));
59 9 argv[1] = sv_2mortal(newRV_inc((SV *)is->queues));
60 9 argv[2] = sv_2mortal(newRV_inc((SV *)is->tasks));
61 9 job = pq_call_meth_ev(aTHX_ is->queue, "dequeue_ref", argv, 3, 1,
62 &died);
63 }
64  
65 9 ▶ if (!died && job && SvROK(job)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!died && job && SvROK(job)) { 9 0 9
if (!died && job && SvROK(job)) { 9 0 9
if (!died && job && SvROK(job)) { 1 8 9
66 1 double t0 = pq_now_local(aTHX), dt;
67 1 is->busy = 1;
68  
69 /* clamp the in-memory timeout to the cap, so perform's loop-timer
70 * layer enforces it for future-returning tasks; a synchronous
71 * blocking task on a loop cannot be stopped by anyone (documented)
72 * and is what the breach counter is for */
73 {
74 1 HV *jh = pq_hv(aTHX_ job, "Punk::Queue::Job");
75 1 HV *row = pq_get_hv(aTHX_ jh, "row");
76 1 ▶ if (row) {
Branch: 1 branch
BranchTrueFalseTotal
if (row) { 1 0 1
77 1 SV *t = pq_get(aTHX_ row, "timeout");
78 1 ▶ double to = (t && SvOK(t)) ? SvNV(t) : 0.0;
Branch: 2 branches
BranchTrueFalseTotal
double to = (t && SvOK(t)) ? SvNV(t) : 0.0; 1 0 1
double to = (t && SvOK(t)) ? SvNV(t) : 0.0; 0 1 1
79 1 ▶ if (to <= 0 || to > is->cap)
Branch: 2 branches
BranchTrueFalseTotal
if (to <= 0 || to > is->cap) 0 1 1
if (to <= 0 || to > is->cap) 0 0 0
80 1 (void)hv_stores(row, "timeout", newSVnv(is->cap));
81 }
82 }
83  
84 {
85 SV *argv[1], *r;
86 1 argv[0] = job;
87 1 r = pq_call_meth_ev(aTHX_ is->queue, "perform", argv, 1, 1,
88 &died);
89 1 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 1 0 1
90 }
91  
92 1 dt = pq_now_local(aTHX) - t0;
93 1 ▶ if (dt > is->cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (dt > is->cap) { 0 1 1
94 0 is->breaches++;
95 0 warn("Punk::Queue in-server: a job ran %.1" NVff "s against a %.1" NVff "s "
96 "cap (breach %d of 2)", (NV)dt, (NV)is->cap, is->breaches);
97 0 ▶ if (is->breaches >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (is->breaches >= 2) { 0 0 0
98 0 is->disabled = 1;
99 0 warn("Punk::Queue in-server: disabled in this process "
100 "after two cap breaches - run punk-queue worker for "
101 "this workload");
102 }
103 }
104 1 is->busy = 0;
105 }
106 9 ▶ if (job) SvREFCNT_dec(job);
Branch: 1 branch
BranchTrueFalseTotal
if (job) SvREFCNT_dec(job); 9 0 9
107 9 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 9 0 9
108 }
109  
110 /* Attach, once per process. Returns 1 when armed, 0 when there is no
111 * live loop (not a Hyperman worker) - the caller warns and the app keeps
112 * serving; in-server is an enhancement, never a requirement. */
113 3 static int pq_inserver_attach(pTHX_ SV *queue, HV *opts) {
114 3 const hm_abi *A = pq_hm(aTHX);
115 3 ▶ void *loop = A ? A->cur_loop(aTHX) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
void *loop = A ? A->cur_loop(aTHX) : NULL; 3 0 3
116 SV *v;
117  
118 3 ▶ if (!loop) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!loop) return 0; 2 1 3
119 1 ▶ if (PQIS) return 1; /* one per process */
Branch: 1 branch
BranchTrueFalseTotal
if (PQIS) return 1; /* one per process */ 0 1 1
120  
121 1 PQIS = (pq_inserver *)pq_xcalloc(aTHX_ 1, sizeof(pq_inserver));
122 1 PQIS->queue = SvREFCNT_inc(queue);
123 1 PQIS->A = A;
124 1 PQIS->loop = loop;
125  
126 1 ▶ PQIS->queues = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "queues")
Branch: 1 branch
BranchTrueFalseTotal
PQIS->queues = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "queues") 1 0 1
127 : NULL);
128 1 ▶ if (av_len(PQIS->queues) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(PQIS->queues) < 0) 1 0 1
129 1 av_push(PQIS->queues, newSVpvs("default"));
130 1 SvREFCNT_inc((SV *)PQIS->queues); /* outlive the mortal scope */
131  
132 1 ▶ PQIS->tasks = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "tasks")
Branch: 1 branch
BranchTrueFalseTotal
PQIS->tasks = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "tasks") 1 0 1
133 : NULL);
134 1 ▶ if (av_len(PQIS->tasks) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (av_len(PQIS->tasks) < 0) 0 1 1
135 0 croak("Punk::Queue: in-server needs a task allowlist");
136 1 SvREFCNT_inc((SV *)PQIS->tasks);
137  
138 1 PQIS->interval = 1.0;
139 1 PQIS->cap = 5.0;
140 1 ▶ if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v))
Branch: 3 branches
BranchTrueFalseTotal
if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) 1 0 1
if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) 1 0 1
if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) 1 0 1
141 1 ▶ PQIS->interval = SvNV(v) > 0.1 ? SvNV(v) : 0.1;
Branch: 1 branch
BranchTrueFalseTotal
PQIS->interval = SvNV(v) > 0.1 ? SvNV(v) : 0.1; 0 1 1
142 1 ▶ if (opts && (v = pq_get(aTHX_ opts, "cap")) && SvOK(v))
Branch: 3 branches
BranchTrueFalseTotal
if (opts && (v = pq_get(aTHX_ opts, "cap")) && SvOK(v)) 1 0 1
if (opts && (v = pq_get(aTHX_ opts, "cap")) && SvOK(v)) 1 0 1
if (opts && (v = pq_get(aTHX_ opts, "cap")) && SvOK(v)) 1 0 1
143 1 ▶ PQIS->cap = SvNV(v) > 0.1 ? SvNV(v) : 0.1;
Branch: 1 branch
BranchTrueFalseTotal
PQIS->cap = SvNV(v) > 0.1 ? SvNV(v) : 0.1; 1 0 1
144  
145 1 pq_inserver_arm(aTHX_ PQIS);
146 1 return 1;
147 }
148  
149 #endif /* PQ_INSERVER_H */
« xs/job.xs ↑ Summary include/pq_hm.h »