include/pq_repair.h

filter: Statementstmt 97.7% 128 / 131 Branchbran 71.4% 40 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.8% 168 / 187 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_lock.h ↑ Summary include/pq_job.h »
1 #ifndef PQ_REPAIR_H
2 #define PQ_REPAIR_H
3  
4 /* pq_repair.h - broadcast/receive, and the repair passes.
5 *
6 * Repair's contract has two halves and the second is the harder one:
7 * every corruption a crash can leave gets fixed, AND a healthy queue is
8 * left completely untouched. Every pass below is predicated - there is no
9 * blanket rewrite anywhere - and the battery asserts the no-op with a
10 * row-by-row comparison.
11 *
12 * Include after pq_lock.h. */
13  
14 /* ---- broadcast / receive ---------------------------------------------------
15 *
16 * The inbox is a JSON array of [command, @args] tuples on the worker row.
17 * broadcast appends (to every worker, or a named set); receive drains
18 * atomically inside a transaction - a plain read-then-clear would drop a
19 * command that arrived between the two, and "the operator's stop was
20 * silently lost" is a miserable bug to chase. */
21  
22 6 static IV pq_broadcast(pTHX_ SV *self, SV *cmd, AV *args, AV *ids) {
23 6 AV *tuple = (AV *)sv_2mortal((SV *)newAV());
24 SV *tuple_rv;
25 6 IV hit = 0;
26  
27 6 ▶ if (!(cmd && SvOK(cmd)))
Branch: 2 branches
BranchTrueFalseTotal
if (!(cmd && SvOK(cmd))) 6 0 6
if (!(cmd && SvOK(cmd))) 0 6 6
28 0 croak("Punk::Queue: broadcast needs a command name");
29 6 av_push(tuple, newSVsv(cmd));
30 6 ▶ if (args) {
Branch: 1 branch
BranchTrueFalseTotal
if (args) { 5 1 6
31 5 SSize_t n = av_len(args) + 1, i;
32 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 1 5 6
33 1 SV **e = av_fetch(args, i, 0);
34 1 ▶ av_push(tuple, newSVsv((e && *e) ? *e : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(tuple, newSVsv((e && *e) ? *e : &PL_sv_undef)); 1 0 1
av_push(tuple, newSVsv((e && *e) ? *e : &PL_sv_undef)); 1 0 1
35 }
36 }
37 6 tuple_rv = sv_2mortal(newRV_inc((SV *)tuple));
38  
39 6 pq_txn_begin(aTHX_ self);
40 {
41 /* which workers */
42 6 SV *sql = pq_sql_new(aTHX_ "SELECT id, inbox FROM pq_workers");
43 6 AV *bind = pq_binds(aTHX), *row;
44 SV *sth;
45 6 ▶ SSize_t nids = ids ? av_len(ids) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
SSize_t nids = ids ? av_len(ids) + 1 : 0; 5 1 6
46 6 AV *rows = (AV *)sv_2mortal((SV *)newAV());
47 SSize_t k, nrows;
48  
49 6 ▶ if (nids > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (nids > 0) { 5 1 6
50 SSize_t i;
51 5 pq_sql_cat(aTHX_ sql, " WHERE ");
52 5 pq_sql_in(aTHX_ sql, "id", nids);
53 10 ▶ for (i = 0; i < nids; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nids; i++) 5 5 10
54 5 pq_bind_sv(aTHX_ bind, *av_fetch(ids, i, 0));
55 }
56 6 sth = pq_sth(aTHX_ self, sql);
57 6 (void)pq_execute(aTHX_ sth, bind);
58 13 ▶ while ((row = pq_fetchrow(aTHX_ sth)))
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) 7 6 13
59 7 av_push(rows, newRV_noinc((SV *)row));
60 6 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
61 6 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 6 0 6
62  
63 6 nrows = av_len(rows) + 1;
64 13 ▶ for (k = 0; k < nrows; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < nrows; k++) { 7 6 13
65 7 AV *r2 = (AV *)SvRV(*av_fetch(rows, k, 0));
66 7 IV wid = pq_col_iv(aTHX_ r2, 0);
67 7 SV *decoded = sv_2mortal(pq_json_decode(aTHX_
68 pq_col(aTHX_ r2, 1)));
69 14 AV *inbox = (SvROK(decoded)
70 7 ▶ && SvTYPE(SvRV(decoded)) == SVt_PVAV)
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(decoded)) == SVt_PVAV) 7 0 7
71 ? (AV *)SvRV(decoded)
72 14 ▶ : (AV *)sv_2mortal((SV *)newAV());
Branch: 1 branch
BranchTrueFalseTotal
: (AV *)sv_2mortal((SV *)newAV()); 7 0 7
73 SV *json;
74 7 AV *ub = pq_binds(aTHX);
75  
76 7 av_push(inbox, newSVsv(tuple_rv));
77 7 json = sv_2mortal(pq_json_encode(aTHX_
78 sv_2mortal(newRV_inc((SV *)inbox)), "[]"));
79 7 pq_bind_sv(aTHX_ ub, json);
80 7 pq_bind_iv(aTHX_ ub, wid);
81 7 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
82 "UPDATE pq_workers SET inbox = ? WHERE id = ?")), ub);
83 7 hit++;
84 }
85 }
86 6 pq_txn_commit(aTHX_ self);
87 6 return hit;
88 }
89  
90 /* Drain and return this worker's inbox (+1 arrayref, possibly empty). */
91 13 static SV *pq_receive(pTHX_ SV *self, IV worker_id) {
92 13 SV *out = NULL;
93  
94 13 pq_txn_begin(aTHX_ self);
95 {
96 13 AV *b = pq_binds(aTHX), *row;
97 13 pq_bind_iv(aTHX_ b, worker_id);
98 13 row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
99 "SELECT inbox FROM pq_workers WHERE id = ?")), b);
100 13 ▶ if (row) {
Branch: 1 branch
BranchTrueFalseTotal
if (row) { 13 0 13
101 13 out = pq_json_decode(aTHX_ pq_col(aTHX_ row, 0));
102 13 SvREFCNT_dec((SV *)row);
103  
104 13 b = pq_binds(aTHX);
105 13 pq_bind_iv(aTHX_ b, worker_id);
106 13 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
107 "UPDATE pq_workers SET inbox = '[]' WHERE id = ?")), b);
108 }
109 }
110 13 pq_txn_commit(aTHX_ self);
111  
112 13 ▶ if (!(out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV)) { 13 0 13
if (!(out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV)) { 13 0 13
if (!(out && SvROK(out) && SvTYPE(SvRV(out)) == SVt_PVAV)) { 0 13 13
113 0 ▶ if (out) SvREFCNT_dec(out);
Branch: 1 branch
BranchTrueFalseTotal
if (out) SvREFCNT_dec(out); 0 0 0
114 0 out = newRV_noinc((SV *)newAV());
115 }
116 13 return out;
117 }
118  
119 /* ---- repair ----------------------------------------------------------------
120 *
121 * The seven passes, in dependency order (stale workers first, because the
122 * orphaned-jobs pass defines "orphaned" as "worker row gone"). Returns a
123 * hashref of per-pass counts, so `punk-queue repair` can report what it
124 * actually did and the battery can assert "exactly this and nothing else".
125 *
126 * Knobs (constructor options): missing_after (default 1800s) - a worker
127 * silent this long is dead; remove_after (default 172800s) - terminal
128 * jobs older than this are history, not queue. */
129  
130 5 static SV *pq_repair(pTHX_ SV *self, int deep) {
131 5 HV *out = newHV();
132 5 double now = pq_now(aTHX_ self);
133 5 double missing_after = pq_opt_num(aTHX_ self, "missing_after", 1800.0);
134 5 double remove_after = pq_opt_num(aTHX_ self, "remove_after", 172800.0);
135 IV n;
136  
137 /* 1: workers that stopped heartbeating are unregistered. */
138 {
139 5 AV *b = pq_binds(aTHX);
140 5 pq_bind_nv(aTHX_ b, now - missing_after);
141 5 n = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
142 "DELETE FROM pq_workers WHERE notified < ?")), b);
143 5 (void)hv_stores(out, "stale_workers", newSViv(n));
144 }
145  
146 /* 2: active jobs whose worker row is gone go back to inactive with
147 * the retries bump - the bump is the point: it invalidates whatever
148 * the vanished (or zombie) worker might still report through the
149 * optimistic guard. This is the pass that picks up children killed
150 * mid-job by the supervisor's hard timeout. */
151 {
152 5 AV *b = pq_binds(aTHX);
153 5 pq_bind_nv(aTHX_ b, now);
154 5 n = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
155 "UPDATE pq_jobs SET state = 'inactive',"
156 " retries = retries + 1, retried = ?, started = NULL,"
157 " worker = NULL WHERE state = 'active' AND (worker IS NULL"
158 " OR worker NOT IN (SELECT id FROM pq_workers))")), b);
159 5 (void)hv_stores(out, "orphaned_jobs", newSViv(n));
160 }
161  
162 /* 3: expired inactive jobs are deleted. The claim predicate already
163 * refuses them (correctness is immediate); this keeps the table
164 * small. Children adjust exactly as remove_job's inactive case. */
165 {
166 SV *sth;
167 5 AV *b = pq_binds(aTHX), *row;
168 5 AV *victims = (AV *)sv_2mortal((SV *)newAV());
169 SSize_t i, nv;
170  
171 5 pq_bind_nv(aTHX_ b, now);
172 5 sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
173 "SELECT id FROM pq_jobs WHERE state = 'inactive'"
174 " AND expires IS NOT NULL AND expires <= ?")));
175 5 (void)pq_execute(aTHX_ sth, b);
176 6 ▶ while ((row = pq_fetchrow(aTHX_ sth))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) { 1 5 6
177 1 av_push(victims, newSViv(pq_col_iv(aTHX_ row, 0)));
178 1 SvREFCNT_dec((SV *)row);
179 }
180 5 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
181 5 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 5 0 5
182  
183 5 nv = av_len(victims) + 1;
184 6 ▶ for (i = 0; i < nv; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nv; i++) 1 5 6
185 1 (void)pq_remove_job(aTHX_ self,
186 1 SvIV(*av_fetch(victims, i, 0)));
187 5 (void)hv_stores(out, "expired_jobs", newSViv((IV)nv));
188 }
189  
190 /* 4: ancient terminal jobs are history, not queue. Straight DELETE -
191 * a finished parent's children got their decrements long ago, and
192 * failed parents this old have failed children right next to them
193 * (pass 5 already ran on them in an earlier repair). Dep rows
194 * cascade. */
195 {
196 5 AV *b = pq_binds(aTHX);
197 5 pq_bind_nv(aTHX_ b, now - remove_after);
198 5 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
199 "DELETE FROM pq_job_logs WHERE job_id IN"
200 " (SELECT id FROM pq_jobs"
201 " WHERE state IN ('finished', 'failed') AND finished < ?)")),
202 b);
203 5 b = pq_binds(aTHX);
204 5 pq_bind_nv(aTHX_ b, now - remove_after);
205 5 n = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
206 "DELETE FROM pq_jobs"
207 " WHERE state IN ('finished', 'failed') AND finished < ?")), b);
208 5 (void)hv_stores(out, "ancient_jobs", newSViv(n));
209 }
210  
211 /* 5: a non-lax child of a terminally failed parent will never run;
212 * hanging silently is the one unforgivable outcome, so it fails with
213 * a reason. (Terminally failed = state 'failed'; a backoff retry sits
214 * in 'inactive' and does not strand anyone.) Each newly failed child
215 * is itself a terminally failed parent - its lax children unblock via
216 * the same pq_children_adjust every other transition uses - so the
217 * pass iterates until a whole chain has settled. */
218 {
219 5 IV total = 0;
220 1 for (;;) {
221 SV *sth;
222 6 AV *row, *victims = (AV *)sv_2mortal((SV *)newAV());
223 SSize_t i, nv;
224  
225 6 sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
226 "SELECT id FROM pq_jobs WHERE state = 'inactive'"
227 " AND lax = 0 AND id IN (SELECT d.job_id"
228 " FROM pq_job_deps d JOIN pq_jobs p ON p.id = d.parent_id"
229 " WHERE p.state = 'failed')")));
230 6 (void)pq_execute(aTHX_ sth, NULL);
231 7 ▶ while ((row = pq_fetchrow(aTHX_ sth))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) { 1 6 7
232 1 av_push(victims, newSViv(pq_col_iv(aTHX_ row, 0)));
233 1 SvREFCNT_dec((SV *)row);
234 }
235 6 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
236 6 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 6 0 6
237  
238 6 nv = av_len(victims) + 1;
239 6 ▶ if (nv == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (nv == 0) break; 5 1 6
240  
241 2 ▶ for (i = 0; i < nv; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nv; i++) { 1 1 2
242 1 IV cid = SvIV(*av_fetch(victims, i, 0));
243 1 AV *b = pq_binds(aTHX);
244 IV took;
245 1 pq_txn_begin(aTHX_ self);
246 1 pq_bind_nv(aTHX_ b, now);
247 1 pq_bind_iv(aTHX_ b, cid);
248 1 took = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
249 "UPDATE pq_jobs SET state = 'failed', finished = ?,"
250 " result = '\"Punk::Queue: a parent this job depends"
251 " on has failed\"'"
252 " WHERE id = ? AND state = 'inactive'")), b);
253 1 ▶ if (took > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (took > 0) { 1 0 1
254 1 pq_children_adjust(aTHX_ self, cid, -1, PQ_KIDS_LAX);
255 1 total++;
256 }
257 1 pq_txn_commit(aTHX_ self);
258 }
259 }
260 5 (void)hv_stores(out, "stranded_children", newSViv(total));
261 }
262  
263 /* 6: stale locks. Reads honour expiry everywhere already; this is
264 * the table-size half. */
265 {
266 5 AV *b = pq_binds(aTHX);
267 5 pq_bind_nv(aTHX_ b, now);
268 5 n = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
269 "DELETE FROM pq_locks WHERE expires <= ?")), b);
270 5 (void)hv_stores(out, "stale_locks", newSViv(n));
271 }
272  
273 /* 7: orphaned log lines. Every in-tree delete path cascades, so a
274 * hit here means something ELSE deleted job rows (manual SQL, an
275 * older binary) - which is exactly the drift repair exists to mop
276 * up. */
277 {
278 5 n = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
279 "DELETE FROM pq_job_logs WHERE job_id NOT IN"
280 " (SELECT id FROM pq_jobs)")), NULL);
281 5 (void)hv_stores(out, "orphaned_logs", newSViv(n));
282 }
283  
284 /* 7 (--deep): recompute every parents_left from the deps table - the
285 * correctness backstop for the whole counter design. Off by default
286 * because it is a full scan. */
287 5 ▶ if (deep) {
Branch: 1 branch
BranchTrueFalseTotal
if (deep) { 2 3 5
288 2 n = pq_do_pv(aTHX_ self,
289 "UPDATE pq_jobs SET parents_left ="
290 " (SELECT count(*) FROM pq_job_deps d"
291 " JOIN pq_jobs p ON p.id = d.parent_id"
292 " WHERE d.job_id = pq_jobs.id AND CASE WHEN pq_jobs.lax = 1"
293 " THEN p.state NOT IN ('finished', 'failed')"
294 " ELSE p.state <> 'finished' END)"
295 " WHERE state = 'inactive' AND parents_left <>"
296 " (SELECT count(*) FROM pq_job_deps d"
297 " JOIN pq_jobs p ON p.id = d.parent_id"
298 " WHERE d.job_id = pq_jobs.id AND CASE WHEN pq_jobs.lax = 1"
299 " THEN p.state NOT IN ('finished', 'failed')"
300 " ELSE p.state <> 'finished' END)");
301 2 (void)hv_stores(out, "recomputed_counters", newSViv(n));
302 }
303  
304 5 return newRV_noinc((SV *)out);
305 }
306  
307 #endif /* PQ_REPAIR_H */
« include/pq_lock.h ↑ Summary include/pq_job.h »