| 1 |
|
| #ifndef PQ_WORKER_H |
| 2 |
|
| #define PQ_WORKER_H |
| 3 |
|
| |
| 4 |
|
| /* pq_worker.h - the worker child: register, claim, run, heartbeat, exit. |
| 5 |
|
| * |
| 6 |
|
| * Two execution paths behind one loop body: |
| 7 |
|
| * |
| 8 |
|
| * - With Hyperman (pq_hm resolves): one Hyperman::Loop->new Perl call at |
| 9 |
|
| * startup (hm_abi has no loop_new), then jobs run INSIDE a loop timer |
| 10 |
|
| * callback via run_until. Running them there is the point: cur_loop is |
| 11 |
|
| * live while the task body executes, so a Punk::Future the task creates |
| 12 |
|
| * detects the loop and awaits in loop mode - and because the heartbeat |
| 13 |
|
| * shares the loop, a task that awaits keeps the worker's own timers |
| 14 |
|
| * firing. |
| 15 |
|
| * |
| 16 |
|
| * - Without (or with PUNK_QUEUE_NO_HM_ABI=1): a plain C loop sleeping in |
| 17 |
|
| * poll(2). Fully functional; task-created futures fall back to |
| 18 |
|
| * Punk::Future's block mode. This is the degraded path t/ forces. |
| 19 |
|
| * |
| 20 |
|
| * Exactly one job in flight per child; the concurrency knob is the |
| 21 |
|
| * supervisor's -j, not per-child parallelism. |
| 22 |
|
| * |
| 23 |
|
| * Phase 3 waits by fixed interval. The horizon query, the decorrelated |
| 24 |
|
| * jitter and the NOTIFY wakeup replace that sleep in phase 5, in this file. |
| 25 |
|
| * |
| 26 |
|
| * Include after pq_hm.h, pq_queue.h. */ |
| 27 |
|
| |
| 28 |
|
| #ifndef WIN32 |
| 29 |
|
| # include <poll.h> |
| 30 |
|
| #endif |
| 31 |
|
| |
| 32 |
|
| /* Signal flags: handlers set these and do nothing else. File-scope because |
| 33 |
|
| * a signal handler cannot carry a context pointer. */ |
| 34 |
|
| static volatile sig_atomic_t PQW_STOP = 0; |
| 35 |
|
| |
| 36 |
1 |
| static void pqw_sig_stop(int sig) { (void)sig; PQW_STOP = 1; } |
| 37 |
|
| |
| 38 |
|
| /* The per-run state, carried through the loop callbacks. */ |
| 39 |
|
| typedef struct pq_worker_run { |
| 40 |
|
| SV *queue; /* the Punk::Queue, borrowed for the run */ |
| 41 |
|
| SV *backend; /* borrowed */ |
| 42 |
|
| AV *queues, *tasks; /* mortal-owned by the caller */ |
| 43 |
|
| IV worker_id; |
| 44 |
|
| IV max_jobs, jobs_done; |
| 45 |
|
| int oneshot; |
| 46 |
|
| double interval, hb_interval, hb_due, oneshot_deadline; |
| 47 |
|
| int sup_fd; /* supervision pipe, -1 when absent */ |
| 48 |
|
| /* loop path */ |
| 49 |
|
| const hm_abi *A; |
| 50 |
|
| void *loop; |
| 51 |
|
| SV *loop_sv; |
| 52 |
|
| SV *tick_future; /* settled by the timer or a NOTIFY */ |
| 53 |
|
| hm_abi_timer *tick_timer; |
| 54 |
|
| int timer_fired; |
| 55 |
|
| int had_work; |
| 56 |
|
| /* wakeup seam (phase 5) */ |
| 57 |
|
| int is_pg; |
| 58 |
|
| SV *notify_dbh; /* the dedicated LISTEN connection, or NULL */ |
| 59 |
|
| int notify_fd; /* its pg_socket, or -1 */ |
| 60 |
|
| int notify_watched; /* io_watch installed on notify_fd */ |
| 61 |
|
| int notify_broken; /* reconnect on the next pass */ |
| 62 |
|
| int wake; /* a NOTIFY arrived: claim now */ |
| 63 |
|
| double sleep_cur; /* SQLite decorrelated-jitter state */ |
| 64 |
|
| int stop_cmd; /* a broadcast said stop: exit after this */ |
| 65 |
|
| } pq_worker_run; |
| 66 |
|
| |
| 67 |
|
| #define PQW_JITTER_BASE 0.05 |
| 68 |
|
| |
| 69 |
|
| /* Write a supervision line; best-effort, EPIPE means the parent is gone |
| 70 |
|
| * and the TERM that follows will land shortly. */ |
| 71 |
33 |
| static void pqw_sup_write(pTHX_ pq_worker_run *w, const char *buf, |
| 72 |
|
| size_t len) { |
| 73 |
33 |
▶ | if (w->sup_fd < 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->sup_fd < 0) return; |
26 |
7 |
33 |
|
| 74 |
14 |
▶ | while (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len) { |
7 |
7 |
14 |
|
| 75 |
7 |
| ssize_t n = write(w->sup_fd, buf, len); |
| 76 |
7 |
▶ | if (n < 0) { if (errno == EINTR) continue; return; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n < 0) { if (errno == EINTR) continue; return; } |
0 |
7 |
7 |
| if (n < 0) { if (errno == EINTR) continue; return; } |
0 |
0 |
0 |
|
| 77 |
7 |
| buf += n; len -= (size_t)n; |
| 78 |
|
| } |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
|
| /* The claim line carries the job's timeout so the parent can enforce the |
| 82 |
|
| * hard kill without a database read per line. */ |
| 83 |
20 |
| static void pqw_sup_claim(pTHX_ pq_worker_run *w, IV id, double timeout) { |
| 84 |
|
| char line[96]; |
| 85 |
20 |
| int n = snprintf(line, sizeof line, "C %ld %.3f %.3f\n", |
| 86 |
|
| (long)id, pq_now_local(aTHX), timeout); |
| 87 |
20 |
▶ | if (n > 0) pqw_sup_write(aTHX_ w, line, (size_t)n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > 0) pqw_sup_write(aTHX_ w, line, (size_t)n); |
20 |
0 |
20 |
|
| 88 |
20 |
| } |
| 89 |
|
| |
| 90 |
13 |
| static void pqw_sup_done(pTHX_ pq_worker_run *w) { |
| 91 |
13 |
| pqw_sup_write(aTHX_ w, "D\n", 2); |
| 92 |
13 |
| } |
| 93 |
|
| |
| 94 |
|
| /* Claim and run at most one job. Returns 1 when a job ran. Never croaks: |
| 95 |
|
| * this body runs both from the plain loop and from inside a loop callback, |
| 96 |
|
| * and the hm_abi contract forbids a longjmp out of the latter. Everything |
| 97 |
|
| * that can raise goes through a G_EVAL method call (pq_call_meth_ev) - the |
| 98 |
|
| * same trap discipline the rest of the dist uses, not JMPENV. */ |
| 99 |
28 |
| static int pqw_one(pTHX_ pq_worker_run *w) { |
| 100 |
28 |
| SV *job = NULL; |
| 101 |
28 |
| int ran = 0, died = 0; |
| 102 |
|
| |
| 103 |
28 |
| ENTER; SAVETMPS; |
| 104 |
|
| |
| 105 |
|
| /* the claim can raise (connection loss); trap and treat as "nothing |
| 106 |
|
| * claimed" - the next pass reconnects via the pool's pid/Active check */ |
| 107 |
|
| { |
| 108 |
|
| SV *argv[3]; |
| 109 |
28 |
| argv[0] = sv_2mortal(newSViv(w->worker_id)); |
| 110 |
28 |
| argv[1] = sv_2mortal(newRV_inc((SV *)w->queues)); |
| 111 |
28 |
| argv[2] = sv_2mortal(newRV_inc((SV *)w->tasks)); |
| 112 |
28 |
| job = pq_call_meth_ev(aTHX_ w->queue, "dequeue_ref", argv, 3, 1, |
| 113 |
|
| &died); |
| 114 |
28 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
0 |
28 |
28 |
|
| 115 |
0 |
▶ | warn("Punk::Queue worker: claim failed: %s", SvPV_nolen(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Punk::Queue worker: claim failed: %s", SvPV_nolen(ERRSV)); |
0 |
0 |
0 |
|
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
28 |
▶ | if (job && SvROK(job)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (job && SvROK(job)) { |
28 |
0 |
28 |
| if (job && SvROK(job)) { |
20 |
8 |
28 |
|
| 119 |
20 |
| IV id = 0; |
| 120 |
20 |
| double jt = 0.0; |
| 121 |
20 |
▶ | { SV *idv = pq_job_field(aTHX_ job, "id"); if (idv) id = SvIV(idv); } |
Branch: 1 branch
| Branch | True | False | Total |
| { SV *idv = pq_job_field(aTHX_ job, "id"); if (idv) id = SvIV(idv); } |
20 |
0 |
20 |
|
| 122 |
20 |
| { SV *tv = pq_job_field(aTHX_ job, "timeout"); |
| 123 |
20 |
▶ | if (tv && SvOK(tv)) jt = SvNV(tv); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (tv && SvOK(tv)) jt = SvNV(tv); } |
20 |
0 |
20 |
| if (tv && SvOK(tv)) jt = SvNV(tv); } |
1 |
19 |
20 |
|
| 124 |
20 |
| pqw_sup_claim(aTHX_ w, id, jt); |
| 125 |
|
| |
| 126 |
|
| /* pq_perform traps the task body itself; the G_EVAL here is for a |
| 127 |
|
| * croak in the settle path (connection loss mid-finish) */ |
| 128 |
|
| { |
| 129 |
|
| SV *argv[1], *r; |
| 130 |
20 |
| argv[0] = job; |
| 131 |
20 |
| r = pq_call_meth_ev(aTHX_ w->queue, "perform", argv, 1, 1, &died); |
| 132 |
13 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
13 |
0 |
13 |
|
| 133 |
13 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
0 |
13 |
13 |
|
| 134 |
0 |
▶ | warn("Punk::Queue worker: perform raised: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| warn("Punk::Queue worker: perform raised: %s", |
0 |
0 |
0 |
|
| 135 |
|
| SvPV_nolen(ERRSV)); |
| 136 |
|
| } |
| 137 |
13 |
| pqw_sup_done(aTHX_ w); |
| 138 |
13 |
| w->jobs_done++; |
| 139 |
13 |
| ran = 1; |
| 140 |
|
| } |
| 141 |
21 |
▶ | if (job) SvREFCNT_dec(job); |
Branch: 1 branch
| Branch | True | False | Total |
| if (job) SvREFCNT_dec(job); |
21 |
0 |
21 |
|
| 142 |
|
| |
| 143 |
21 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
21 |
0 |
21 |
|
| 144 |
21 |
| return ran; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| /* Act on one inbox command. `stop` finishes the current work and exits |
| 148 |
|
| * gracefully; `kill` is documented as its impatient sibling but lands |
| 149 |
|
| * between jobs like everything here - a single-threaded worker cannot |
| 150 |
|
| * interrupt its own running task, and pretending otherwise would be a |
| 151 |
|
| * false promise (the supervisor's hard timeout is the real mid-job |
| 152 |
|
| * killer). `jobs` is accepted and logged rather than rejected: per-child |
| 153 |
|
| * concurrency is fixed at one in this release, but a fleet-wide broadcast |
| 154 |
|
| * must not error on the workers that cannot oblige yet. */ |
| 155 |
0 |
| static void pqw_command(pTHX_ pq_worker_run *w, AV *tuple) { |
| 156 |
0 |
| SV **e = av_fetch(tuple, 0, 0); |
| 157 |
|
| const char *cmd; |
| 158 |
0 |
▶ | if (!(e && *e && SvOK(*e))) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && *e && SvOK(*e))) return; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) return; |
0 |
0 |
0 |
| if (!(e && *e && SvOK(*e))) return; |
0 |
0 |
0 |
|
| 159 |
0 |
| cmd = SvPV_nolen(*e); |
| 160 |
0 |
▶ | if (strEQ(cmd, "stop") || strEQ(cmd, "kill")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (strEQ(cmd, "stop") || strEQ(cmd, "kill")) { |
0 |
0 |
0 |
| if (strEQ(cmd, "stop") || strEQ(cmd, "kill")) { |
0 |
0 |
0 |
|
| 161 |
0 |
| w->stop_cmd = 1; |
| 162 |
|
| } |
| 163 |
0 |
▶ | else if (strEQ(cmd, "jobs")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (strEQ(cmd, "jobs")) { |
0 |
0 |
0 |
|
| 164 |
0 |
| SV **n = av_fetch(tuple, 1, 0); |
| 165 |
0 |
▶ | warn("Punk::Queue worker %ld: 'jobs' command received (%s); " |
Branch: 3 branches
| Branch | True | False | Total |
| warn("Punk::Queue worker %ld: 'jobs' command received (%s); " |
0 |
0 |
0 |
| warn("Punk::Queue worker %ld: 'jobs' command received (%s); " |
0 |
0 |
0 |
| warn("Punk::Queue worker %ld: 'jobs' command received (%s); " |
0 |
0 |
0 |
|
| 166 |
|
| "per-child concurrency is fixed at 1 in this release", |
| 167 |
|
| (long)w->worker_id, |
| 168 |
|
| (n && *n && SvOK(*n)) ? SvPV_nolen(*n) : "no argument"); |
| 169 |
|
| } |
| 170 |
|
| /* unknown commands are ignored: a newer punk-queue broadcasting to an |
| 171 |
|
| * older worker is a mixed deployment, not an error */ |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
22 |
| static void pqw_heartbeat(pTHX_ pq_worker_run *w) { |
| 175 |
22 |
| double now = pq_now_local(aTHX); |
| 176 |
22 |
| int died = 0; |
| 177 |
|
| SV *argv[2], *r; |
| 178 |
22 |
▶ | if (now < w->hb_due) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (now < w->hb_due) return; |
14 |
8 |
22 |
|
| 179 |
8 |
| w->hb_due = now + w->hb_interval; |
| 180 |
8 |
| argv[0] = sv_2mortal(newSViv(w->worker_id)); |
| 181 |
8 |
| argv[1] = &PL_sv_undef; |
| 182 |
8 |
| r = pq_call_meth_ev(aTHX_ w->backend, "worker_heartbeat", argv, 2, 1, |
| 183 |
|
| &died); |
| 184 |
8 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
8 |
0 |
8 |
|
| 185 |
|
| |
| 186 |
|
| /* drain the broadcast inbox on the same cadence */ |
| 187 |
8 |
| argv[0] = sv_2mortal(newSViv(w->worker_id)); |
| 188 |
8 |
| r = pq_call_meth_ev(aTHX_ w->backend, "receive", argv, 1, 1, &died); |
| 189 |
8 |
▶ | if (!died && r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!died && r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { |
8 |
0 |
8 |
| if (!died && r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { |
8 |
0 |
8 |
| if (!died && r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { |
8 |
0 |
8 |
| if (!died && r && SvROK(r) && SvTYPE(SvRV(r)) == SVt_PVAV) { |
8 |
0 |
8 |
|
| 190 |
8 |
| AV *cmds = (AV *)SvRV(r); |
| 191 |
8 |
| SSize_t n = av_len(cmds) + 1, i; |
| 192 |
8 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
8 |
8 |
|
| 193 |
0 |
| SV **e = av_fetch(cmds, i, 0); |
| 194 |
0 |
▶ | if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) |
Branch: 4 branches
| Branch | True | False | Total |
| if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) |
0 |
0 |
0 |
| if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) |
0 |
0 |
0 |
| if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) |
0 |
0 |
0 |
| if (e && *e && SvROK(*e) && SvTYPE(SvRV(*e)) == SVt_PVAV) |
0 |
0 |
0 |
|
| 195 |
0 |
| pqw_command(aTHX_ w, (AV *)SvRV(*e)); |
| 196 |
|
| } |
| 197 |
|
| } |
| 198 |
8 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
8 |
0 |
8 |
|
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
78 |
| static int pqw_should_stop(pTHX_ pq_worker_run *w) { |
| 202 |
78 |
▶ | if (PQW_STOP) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (PQW_STOP) return 1; |
0 |
78 |
78 |
|
| 203 |
78 |
▶ | if (w->stop_cmd) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->stop_cmd) return 1; |
0 |
78 |
78 |
|
| 204 |
78 |
▶ | if (w->max_jobs && w->jobs_done >= w->max_jobs) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->max_jobs && w->jobs_done >= w->max_jobs) return 1; |
0 |
78 |
78 |
| if (w->max_jobs && w->jobs_done >= w->max_jobs) return 1; |
0 |
0 |
0 |
|
| 205 |
78 |
▶ | if (w->oneshot && w->jobs_done) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->oneshot && w->jobs_done) return 1; |
71 |
7 |
78 |
| if (w->oneshot && w->jobs_done) return 1; |
20 |
51 |
71 |
|
| 206 |
58 |
▶ | if (w->oneshot && pq_now_local(aTHX) > w->oneshot_deadline) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->oneshot && pq_now_local(aTHX) > w->oneshot_deadline) return 1; |
51 |
7 |
58 |
| if (w->oneshot && pq_now_local(aTHX) > w->oneshot_deadline) return 1; |
1 |
50 |
51 |
|
| 207 |
57 |
| return 0; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| /* ---- the LISTEN connection (Pg only) --------------------------------------- |
| 211 |
|
| * |
| 212 |
|
| * A second, dedicated, unpooled DBI handle whose only jobs are LISTEN and |
| 213 |
|
| * being watched. Not the pooled work handle: the read watcher would fire |
| 214 |
|
| * on every claim's own result traffic, which is the mistake the plan says |
| 215 |
|
| * to design out on day one. |
| 216 |
|
| * |
| 217 |
|
| * What the spike established (and the code below leans on): pg_notifies |
| 218 |
|
| * drains pending notifications and returns undef when done; an idle LISTEN |
| 219 |
|
| * connection needs no pokes; server-side termination makes the socket |
| 220 |
|
| * readable (EOF) but pg_notifies then returns undef WITHOUT dying and |
| 221 |
|
| * {Active} stays true - so death is detected as "readable but nothing |
| 222 |
|
| * drained", confirmed by a trapped ping. A reconnected handle has a |
| 223 |
|
| * different fd, so the old watch must be removed and a new one installed. */ |
| 224 |
|
| |
| 225 |
14 |
| static void pqw_notify_teardown(pTHX_ pq_worker_run *w) { |
| 226 |
14 |
▶ | if (w->notify_watched && w->A && w->loop && w->notify_fd >= 0) |
Branch: 4 branches
| Branch | True | False | Total |
| if (w->notify_watched && w->A && w->loop && w->notify_fd >= 0) |
0 |
14 |
14 |
| if (w->notify_watched && w->A && w->loop && w->notify_fd >= 0) |
0 |
0 |
0 |
| if (w->notify_watched && w->A && w->loop && w->notify_fd >= 0) |
0 |
0 |
0 |
| if (w->notify_watched && w->A && w->loop && w->notify_fd >= 0) |
0 |
0 |
0 |
|
| 227 |
0 |
| w->A->io_unwatch(aTHX_ w->loop, w->notify_fd, HM_ABI_READ); |
| 228 |
14 |
| w->notify_watched = 0; |
| 229 |
14 |
▶ | if (w->notify_dbh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->notify_dbh) { |
0 |
14 |
14 |
|
| 230 |
0 |
| int died = 0; |
| 231 |
0 |
| SV *r = pq_call_meth_ev(aTHX_ w->notify_dbh, "disconnect", |
| 232 |
|
| NULL, 0, 1, &died); |
| 233 |
0 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
0 |
0 |
|
| 234 |
0 |
| SvREFCNT_dec(w->notify_dbh); |
| 235 |
0 |
| w->notify_dbh = NULL; |
| 236 |
|
| } |
| 237 |
14 |
| w->notify_fd = -1; |
| 238 |
14 |
| w->notify_broken = 0; |
| 239 |
14 |
| } |
| 240 |
|
| |
| 241 |
|
| /* Drain pending notifications. Sets w->wake if any arrived; sets |
| 242 |
|
| * notify_broken when the fd was readable but nothing drained and a ping |
| 243 |
|
| * fails - the quiet-EOF shape the spike found. Never croaks. */ |
| 244 |
0 |
| static void pqw_notify_drain(pTHX_ pq_worker_run *w, int was_readable) { |
| 245 |
0 |
| int died = 0, got = 0; |
| 246 |
0 |
▶ | if (!w->notify_dbh) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->notify_dbh) return; |
0 |
0 |
0 |
|
| 247 |
0 |
| for (;;) { |
| 248 |
0 |
| SV *note = pq_call_meth_ev(aTHX_ w->notify_dbh, "pg_notifies", |
| 249 |
|
| NULL, 0, 1, &died); |
| 250 |
0 |
▶ | int have = !died && note && SvOK(note); |
Branch: 3 branches
| Branch | True | False | Total |
| int have = !died && note && SvOK(note); |
0 |
0 |
0 |
| int have = !died && note && SvOK(note); |
0 |
0 |
0 |
| int have = !died && note && SvOK(note); |
0 |
0 |
0 |
|
| 251 |
0 |
▶ | if (note) SvREFCNT_dec(note); |
Branch: 1 branch
| Branch | True | False | Total |
| if (note) SvREFCNT_dec(note); |
0 |
0 |
0 |
|
| 252 |
0 |
▶ | if (died) { w->notify_broken = 1; return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { w->notify_broken = 1; return; } |
0 |
0 |
0 |
|
| 253 |
0 |
▶ | if (!have) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!have) break; |
0 |
0 |
0 |
|
| 254 |
0 |
| got = 1; |
| 255 |
0 |
| w->wake = 1; |
| 256 |
|
| } |
| 257 |
0 |
▶ | if (was_readable && !got) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (was_readable && !got) { |
0 |
0 |
0 |
| if (was_readable && !got) { |
0 |
0 |
0 |
|
| 258 |
0 |
| SV *r = pq_call_meth_ev(aTHX_ w->notify_dbh, "ping", NULL, 0, 1, |
| 259 |
|
| &died); |
| 260 |
0 |
▶ | int alive = !died && r && SvTRUE(r); |
Branch: 3 branches
| Branch | True | False | Total |
| int alive = !died && r && SvTRUE(r); |
0 |
0 |
0 |
| int alive = !died && r && SvTRUE(r); |
0 |
0 |
0 |
| int alive = !died && r && SvTRUE(r); |
0 |
0 |
0 |
|
| 261 |
0 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
0 |
0 |
|
| 262 |
0 |
▶ | if (!alive) w->notify_broken = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!alive) w->notify_broken = 1; |
0 |
0 |
0 |
|
| 263 |
|
| } |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
|
| /* The loop-path readiness callback. Must not croak (hm contract). Settles |
| 267 |
|
| * the tick future on any event - a wake claims immediately, and a broken |
| 268 |
|
| * connection needs the main loop awake to reconnect. */ |
| 269 |
0 |
| static void pqw_notify_cb(pTHX_ int fd, int mask, void *ud) { |
| 270 |
0 |
| pq_worker_run *w = (pq_worker_run *)ud; |
| 271 |
|
| PERL_UNUSED_ARG(fd); |
| 272 |
|
| PERL_UNUSED_ARG(mask); |
| 273 |
0 |
| pqw_notify_drain(aTHX_ w, 1); |
| 274 |
0 |
▶ | if ((w->wake || w->notify_broken) && w->tick_future && w->A) |
Branch: 4 branches
| Branch | True | False | Total |
| if ((w->wake || w->notify_broken) && w->tick_future && w->A) |
0 |
0 |
0 |
| if ((w->wake || w->notify_broken) && w->tick_future && w->A) |
0 |
0 |
0 |
| if ((w->wake || w->notify_broken) && w->tick_future && w->A) |
0 |
0 |
0 |
| if ((w->wake || w->notify_broken) && w->tick_future && w->A) |
0 |
0 |
0 |
|
| 275 |
0 |
| w->A->future_done(aTHX_ w->tick_future, NULL, 0); |
| 276 |
0 |
| } |
| 277 |
|
| |
| 278 |
|
| /* (Re)establish the LISTEN connection: connect with the backend's own |
| 279 |
|
| * dsn/credentials, LISTEN each subscribed queue, fetch pg_socket, and on |
| 280 |
|
| * the loop path watch it. Failure is not fatal - the worker still works, |
| 281 |
|
| * at poll latency - so everything is trapped and a failure just leaves |
| 282 |
|
| * notify_dbh NULL until the next pass. */ |
| 283 |
28 |
| static void pqw_notify_ensure(pTHX_ pq_worker_run *w) { |
| 284 |
|
| HV *bh, *o; |
| 285 |
|
| SV *dsn, *user, *pass; |
| 286 |
28 |
| int died = 0; |
| 287 |
|
| |
| 288 |
28 |
▶ | if (!w->is_pg) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->is_pg) return; |
28 |
0 |
28 |
|
| 289 |
0 |
▶ | if (w->notify_dbh && !w->notify_broken) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->notify_dbh && !w->notify_broken) return; |
0 |
0 |
0 |
| if (w->notify_dbh && !w->notify_broken) return; |
0 |
0 |
0 |
|
| 290 |
0 |
| pqw_notify_teardown(aTHX_ w); |
| 291 |
|
| |
| 292 |
0 |
| bh = pq_hv(aTHX_ w->backend, "Punk::Queue::Backend"); |
| 293 |
0 |
| o = pq_get_hv(aTHX_ bh, "opts"); |
| 294 |
0 |
▶ | dsn = o ? pq_get(aTHX_ o, "dsn") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| dsn = o ? pq_get(aTHX_ o, "dsn") : NULL; |
0 |
0 |
0 |
|
| 295 |
0 |
▶ | user = o ? pq_get(aTHX_ o, "user") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| user = o ? pq_get(aTHX_ o, "user") : NULL; |
0 |
0 |
0 |
|
| 296 |
0 |
▶ | pass = o ? pq_get(aTHX_ o, "password") : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| pass = o ? pq_get(aTHX_ o, "password") : NULL; |
0 |
0 |
0 |
|
| 297 |
0 |
▶ | if (!(dsn && SvOK(dsn))) return; /* dbh-injected queue: no dsn */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(dsn && SvOK(dsn))) return; /* dbh-injected queue: no dsn */ |
0 |
0 |
0 |
| if (!(dsn && SvOK(dsn))) return; /* dbh-injected queue: no dsn */ |
0 |
0 |
0 |
|
| 298 |
|
| |
| 299 |
|
| { |
| 300 |
|
| SV *argv[4], *conn; |
| 301 |
0 |
| HV *attr = newHV(); |
| 302 |
0 |
| (void)hv_stores(attr, "RaiseError", newSViv(1)); |
| 303 |
0 |
| (void)hv_stores(attr, "AutoCommit", newSViv(1)); |
| 304 |
0 |
| (void)hv_stores(attr, "PrintError", newSViv(0)); |
| 305 |
0 |
| (void)hv_stores(attr, "AutoInactiveDestroy", newSViv(1)); |
| 306 |
0 |
| argv[0] = dsn; |
| 307 |
0 |
▶ | argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; |
0 |
0 |
0 |
| argv[1] = (user && SvOK(user)) ? user : &PL_sv_undef; |
0 |
0 |
0 |
|
| 308 |
0 |
▶ | argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; |
Branch: 2 branches
| Branch | True | False | Total |
| argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; |
0 |
0 |
0 |
| argv[2] = (pass && SvOK(pass)) ? pass : &PL_sv_undef; |
0 |
0 |
0 |
|
| 309 |
0 |
| argv[3] = sv_2mortal(newRV_noinc((SV *)attr)); |
| 310 |
0 |
| conn = pq_call_meth_ev(aTHX_ sv_2mortal(newSVpvs("DBI")), "connect", |
| 311 |
|
| argv, 4, 1, &died); |
| 312 |
0 |
▶ | if (died || !(conn && SvROK(conn))) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (died || !(conn && SvROK(conn))) { |
0 |
0 |
0 |
| if (died || !(conn && SvROK(conn))) { |
0 |
0 |
0 |
| if (died || !(conn && SvROK(conn))) { |
0 |
0 |
0 |
|
| 313 |
0 |
▶ | if (conn) SvREFCNT_dec(conn); |
Branch: 1 branch
| Branch | True | False | Total |
| if (conn) SvREFCNT_dec(conn); |
0 |
0 |
0 |
|
| 314 |
0 |
| return; |
| 315 |
|
| } |
| 316 |
0 |
| w->notify_dbh = conn; |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| /* LISTEN "pq.<queue>" per subscribed queue. The identifier is safe by |
| 320 |
|
| * construction: queue names passed validation at enqueue, and the |
| 321 |
|
| * worker re-checks so a hostile -q argument cannot smuggle a quote. */ |
| 322 |
|
| { |
| 323 |
0 |
| SSize_t nq = av_len(w->queues) + 1, i; |
| 324 |
0 |
▶ | for (i = 0; i < nq; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nq; i++) { |
0 |
0 |
0 |
|
| 325 |
0 |
| SV **e = av_fetch(w->queues, i, 0); |
| 326 |
|
| SV *stmt, *argv[1], *r; |
| 327 |
0 |
▶ | if (!(e && *e) || !pq_name_ok(aTHX_ *e)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(e && *e) || !pq_name_ok(aTHX_ *e)) continue; |
0 |
0 |
0 |
| if (!(e && *e) || !pq_name_ok(aTHX_ *e)) continue; |
0 |
0 |
0 |
| if (!(e && *e) || !pq_name_ok(aTHX_ *e)) continue; |
0 |
0 |
0 |
|
| 328 |
0 |
| stmt = sv_2mortal(Perl_newSVpvf(aTHX_ "LISTEN \"pq.%s\"", |
| 329 |
|
| SvPV_nolen(*e))); |
| 330 |
0 |
| argv[0] = stmt; |
| 331 |
0 |
| r = pq_call_meth_ev(aTHX_ w->notify_dbh, "do", argv, 1, 1, |
| 332 |
|
| &died); |
| 333 |
0 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
0 |
0 |
0 |
|
| 334 |
0 |
▶ | if (died) { pqw_notify_teardown(aTHX_ w); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) { pqw_notify_teardown(aTHX_ w); return; } |
0 |
0 |
0 |
|
| 335 |
|
| } |
| 336 |
|
| } |
| 337 |
|
| |
| 338 |
|
| { |
| 339 |
0 |
| SV *fd = pq_attr(aTHX_ w->notify_dbh, "pg_socket"); |
| 340 |
0 |
▶ | w->notify_fd = (fd && SvOK(fd)) ? (int)SvIV(fd) : -1; |
Branch: 2 branches
| Branch | True | False | Total |
| w->notify_fd = (fd && SvOK(fd)) ? (int)SvIV(fd) : -1; |
0 |
0 |
0 |
| w->notify_fd = (fd && SvOK(fd)) ? (int)SvIV(fd) : -1; |
0 |
0 |
0 |
|
| 341 |
|
| } |
| 342 |
0 |
▶ | if (w->notify_fd >= 0 && w->A && w->loop) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (w->notify_fd >= 0 && w->A && w->loop) { |
0 |
0 |
0 |
| if (w->notify_fd >= 0 && w->A && w->loop) { |
0 |
0 |
0 |
| if (w->notify_fd >= 0 && w->A && w->loop) { |
0 |
0 |
0 |
|
| 343 |
0 |
| w->A->io_watch(aTHX_ w->loop, w->notify_fd, HM_ABI_READ, |
| 344 |
|
| pqw_notify_cb, w); |
| 345 |
0 |
| w->notify_watched = 1; |
| 346 |
|
| } |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
|
| /* ---- how long to sleep ----------------------------------------------------- |
| 350 |
|
| * |
| 351 |
|
| * Both backends clamp to the delayed-job horizon, so a delayed job wakes |
| 352 |
|
| * the worker on time however long the interval. On top of that: |
| 353 |
|
| * |
| 354 |
|
| * Pg: sleep the whole (clamped) interval - NOTIFY provides the wakeup |
| 355 |
|
| * for new work, so a long interval costs nothing. |
| 356 |
|
| * SQLite: decorrelated jitter, sleep = min(cap, uniform(base, prev*3)), |
| 357 |
|
| * cap = min(1s, horizon, interval). The jitter is not decoration: N |
| 358 |
|
| * children polling one file in lockstep is the pathological case, |
| 359 |
|
| * and plain doubling keeps them in lockstep. */ |
| 360 |
15 |
| static double pqw_sleep_for(pTHX_ pq_worker_run *w) { |
| 361 |
15 |
| double cap = w->interval; |
| 362 |
|
| { |
| 363 |
15 |
| int died = 0; |
| 364 |
|
| SV *argv[1], *r; |
| 365 |
15 |
| argv[0] = sv_2mortal(newRV_inc((SV *)w->queues)); |
| 366 |
15 |
| r = pq_call_meth_ev(aTHX_ w->backend, "_ready_horizon", argv, 1, 1, |
| 367 |
|
| &died); |
| 368 |
15 |
▶ | if (!died && r && SvOK(r)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!died && r && SvOK(r)) { |
15 |
0 |
15 |
| if (!died && r && SvOK(r)) { |
15 |
0 |
15 |
| if (!died && r && SvOK(r)) { |
0 |
15 |
15 |
|
| 369 |
0 |
| double h = SvNV(r); |
| 370 |
0 |
▶ | if (h < cap) cap = h < 0.01 ? 0.01 : h; |
Branch: 2 branches
| Branch | True | False | Total |
| if (h < cap) cap = h < 0.01 ? 0.01 : h; |
0 |
0 |
0 |
| if (h < cap) cap = h < 0.01 ? 0.01 : h; |
0 |
0 |
0 |
|
| 371 |
|
| } |
| 372 |
15 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
15 |
0 |
15 |
|
| 373 |
|
| } |
| 374 |
|
| |
| 375 |
15 |
▶ | if (w->is_pg) return cap; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->is_pg) return cap; |
0 |
15 |
15 |
|
| 376 |
|
| |
| 377 |
|
| { |
| 378 |
15 |
▶ | double jcap = cap < 1.0 ? cap : 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| double jcap = cap < 1.0 ? cap : 1.0; |
8 |
7 |
15 |
|
| 379 |
15 |
| double hi = w->sleep_cur * 3.0; |
| 380 |
|
| double next; |
| 381 |
15 |
▶ | if (hi < PQW_JITTER_BASE) hi = PQW_JITTER_BASE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi < PQW_JITTER_BASE) hi = PQW_JITTER_BASE; |
0 |
15 |
15 |
|
| 382 |
15 |
| next = PQW_JITTER_BASE |
| 383 |
15 |
| + pq_rand01(aTHX) * (hi - PQW_JITTER_BASE); |
| 384 |
15 |
▶ | if (next > jcap) next = jcap; |
Branch: 1 branch
| Branch | True | False | Total |
| if (next > jcap) next = jcap; |
3 |
12 |
15 |
|
| 385 |
15 |
| w->sleep_cur = next; |
| 386 |
15 |
| return next; |
| 387 |
|
| } |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
|
| /* ---- the loop path --------------------------------------------------------- |
| 391 |
|
| * |
| 392 |
|
| * Jobs run inside this timer callback, on the loop thread, inside |
| 393 |
|
| * run_until - which is what puts a live cur_loop under the task body. The |
| 394 |
|
| * callback drains ready work, heartbeats, then settles the tick future so |
| 395 |
|
| * run_until returns to the outer while, which checks the stop conditions |
| 396 |
|
| * and re-arms. |
| 397 |
|
| * |
| 398 |
|
| * The tick future can be settled by two hands - this timer, or a NOTIFY |
| 399 |
|
| * arriving in pqw_notify_cb - and settling twice is a no-op. What is NOT |
| 400 |
|
| * harmless is the timer handle: it is freed when it fires and must not be |
| 401 |
|
| * cancelled after, so the fired flag decides whether the main loop |
| 402 |
|
| * cancels it. */ |
| 403 |
7 |
| static void pqw_tick_cb(pTHX_ void *ud) { |
| 404 |
7 |
| pq_worker_run *w = (pq_worker_run *)ud; |
| 405 |
7 |
| w->timer_fired = 1; |
| 406 |
7 |
| w->tick_timer = NULL; |
| 407 |
7 |
| w->had_work = 0; |
| 408 |
14 |
▶ | while (!pqw_should_stop(aTHX_ w) && pqw_one(aTHX_ w)) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (!pqw_should_stop(aTHX_ w) && pqw_one(aTHX_ w)) { |
7 |
7 |
14 |
| while (!pqw_should_stop(aTHX_ w) && pqw_one(aTHX_ w)) { |
7 |
0 |
7 |
|
| 409 |
7 |
| w->had_work = 1; |
| 410 |
7 |
| w->sleep_cur = PQW_JITTER_BASE; |
| 411 |
|
| } |
| 412 |
7 |
| pqw_heartbeat(aTHX_ w); |
| 413 |
7 |
▶ | if (w->tick_future) |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->tick_future) |
7 |
0 |
7 |
|
| 414 |
7 |
| w->A->future_done(aTHX_ w->tick_future, NULL, 0); |
| 415 |
7 |
| } |
| 416 |
|
| |
| 417 |
|
| /* A sleep chunk: settle the tick future and nothing else. Long sleeps are |
| 418 |
|
| * built from these so the worker re-checks its stop flag every second - |
| 419 |
|
| * run_until sleeps in the kernel, EINTR is swallowed by the loop, and |
| 420 |
|
| * without the chunking a TERM would wait out the whole dequeue interval |
| 421 |
|
| * (a 60s interval blew the graceful window; t/40 caught it). A chunk |
| 422 |
|
| * costs one timer fire and no database traffic. */ |
| 423 |
7 |
| static void pqw_chunk_cb(pTHX_ void *ud) { |
| 424 |
7 |
| pq_worker_run *w = (pq_worker_run *)ud; |
| 425 |
7 |
| w->timer_fired = 1; |
| 426 |
7 |
| w->tick_timer = NULL; |
| 427 |
7 |
▶ | if (w->tick_future) |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->tick_future) |
7 |
0 |
7 |
|
| 428 |
7 |
| w->A->future_done(aTHX_ w->tick_future, NULL, 0); |
| 429 |
7 |
| } |
| 430 |
|
| |
| 431 |
|
| /* One run_until pass around a timer. Returns after the timer fires or |
| 432 |
|
| * something else settles the tick future (a NOTIFY, a broken-connection |
| 433 |
|
| * report), cancelling the timer in the latter case. */ |
| 434 |
14 |
| static void pqw_loop_wait(pTHX_ pq_worker_run *w, double delay, |
| 435 |
|
| hm_abi_timer_cb cb) { |
| 436 |
14 |
| SV *f = w->A->future_new(aTHX); |
| 437 |
14 |
| w->tick_future = f; |
| 438 |
14 |
| w->timer_fired = 0; |
| 439 |
14 |
| w->tick_timer = w->A->timer(aTHX_ w->loop, delay, cb, w); |
| 440 |
14 |
| w->A->run_until(aTHX_ w->loop, f); |
| 441 |
14 |
| w->tick_future = NULL; |
| 442 |
14 |
▶ | if (!w->timer_fired && w->tick_timer) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!w->timer_fired && w->tick_timer) |
0 |
14 |
14 |
| if (!w->timer_fired && w->tick_timer) |
0 |
0 |
0 |
|
| 443 |
0 |
| w->A->timer_cancel(aTHX_ w->loop, w->tick_timer); |
| 444 |
14 |
| w->tick_timer = NULL; |
| 445 |
14 |
| SvREFCNT_dec(f); |
| 446 |
14 |
| } |
| 447 |
|
| |
| 448 |
7 |
| static void pqw_run_loop(pTHX_ pq_worker_run *w) { |
| 449 |
14 |
▶ | while (!pqw_should_stop(aTHX_ w)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!pqw_should_stop(aTHX_ w)) { |
7 |
7 |
14 |
|
| 450 |
7 |
| pqw_notify_ensure(aTHX_ w); |
| 451 |
|
| |
| 452 |
|
| /* sleep, in interruptible chunks */ |
| 453 |
7 |
▶ | if (!w->had_work && !w->wake && !w->notify_broken) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!w->had_work && !w->wake && !w->notify_broken) { |
7 |
0 |
7 |
| if (!w->had_work && !w->wake && !w->notify_broken) { |
7 |
0 |
7 |
| if (!w->had_work && !w->wake && !w->notify_broken) { |
7 |
0 |
7 |
|
| 454 |
7 |
| double left = pqw_sleep_for(aTHX_ w); |
| 455 |
14 |
▶ | while (left > 0 && !pqw_should_stop(aTHX_ w) |
Branch: 1 branch
| Branch | True | False | Total |
| while (left > 0 && !pqw_should_stop(aTHX_ w) |
7 |
0 |
7 |
|
| 456 |
21 |
▶ | && !w->wake && !w->notify_broken) { |
Branch: 3 branches
| Branch | True | False | Total |
| && !w->wake && !w->notify_broken) { |
7 |
7 |
14 |
| && !w->wake && !w->notify_broken) { |
7 |
0 |
7 |
| && !w->wake && !w->notify_broken) { |
7 |
0 |
7 |
|
| 457 |
7 |
▶ | double chunk = left < 1.0 ? left : 1.0; |
Branch: 1 branch
| Branch | True | False | Total |
| double chunk = left < 1.0 ? left : 1.0; |
7 |
0 |
7 |
|
| 458 |
7 |
| pqw_loop_wait(aTHX_ w, chunk, pqw_chunk_cb); |
| 459 |
7 |
| left -= chunk; |
| 460 |
7 |
| pqw_heartbeat(aTHX_ w); /* time-gated; long sleeps |
| 461 |
|
| * must not starve it */ |
| 462 |
|
| } |
| 463 |
|
| } |
| 464 |
7 |
▶ | if (pqw_should_stop(aTHX_ w)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pqw_should_stop(aTHX_ w)) break; |
0 |
7 |
7 |
|
| 465 |
7 |
▶ | if (w->notify_broken) continue; /* reconnect first */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->notify_broken) continue; /* reconnect first */ |
0 |
7 |
7 |
|
| 466 |
7 |
| w->wake = 0; |
| 467 |
|
| |
| 468 |
|
| /* the work tick: claims run inside run_until so cur_loop is live |
| 469 |
|
| * under the task bodies */ |
| 470 |
7 |
| pqw_loop_wait(aTHX_ w, 0.0, pqw_tick_cb); |
| 471 |
|
| } |
| 472 |
7 |
| pqw_notify_teardown(aTHX_ w); |
| 473 |
7 |
| } |
| 474 |
|
| |
| 475 |
|
| /* ---- the poll path --------------------------------------------------------- |
| 476 |
|
| * |
| 477 |
|
| * The same state machine without a loop: poll(2) sleeps, and on Pg the |
| 478 |
|
| * notify fd sits in the pollfd set, so LISTEN works - at identical |
| 479 |
|
| * latency - without Hyperman installed. */ |
| 480 |
|
| |
| 481 |
14 |
| static void pqw_run_poll(pTHX_ pq_worker_run *w) { |
| 482 |
28 |
▶ | while (!pqw_should_stop(aTHX_ w)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!pqw_should_stop(aTHX_ w)) { |
21 |
7 |
28 |
|
| 483 |
21 |
| pqw_notify_ensure(aTHX_ w); |
| 484 |
|
| |
| 485 |
21 |
▶ | if (pqw_one(aTHX_ w)) { /* drain without sleeping */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (pqw_one(aTHX_ w)) { /* drain without sleeping */ |
6 |
8 |
14 |
|
| 486 |
6 |
| w->sleep_cur = PQW_JITTER_BASE; |
| 487 |
6 |
| continue; |
| 488 |
|
| } |
| 489 |
8 |
| pqw_heartbeat(aTHX_ w); |
| 490 |
8 |
▶ | if (pqw_should_stop(aTHX_ w)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pqw_should_stop(aTHX_ w)) break; |
0 |
8 |
8 |
|
| 491 |
|
| |
| 492 |
|
| { |
| 493 |
8 |
| int ms = (int)(pqw_sleep_for(aTHX_ w) * 1000.0); |
| 494 |
8 |
▶ | if (ms < 10) ms = 10; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ms < 10) ms = 10; |
0 |
8 |
8 |
|
| 495 |
|
| #ifdef WIN32 |
| 496 |
|
| /* no poll(2), no EINTR, and a Pg socket is not a CRT fd: sleep |
| 497 |
|
| * in short steps, checking the stop flag and draining NOTIFY |
| 498 |
|
| * (pg_notifies never blocks) after each */ |
| 499 |
|
| while (ms > 0 && !PQW_STOP) { |
| 500 |
|
| int step = ms < 100 ? ms : 100; |
| 501 |
|
| Sleep((DWORD)step); |
| 502 |
|
| ms -= step; |
| 503 |
|
| if (w->notify_dbh && w->notify_fd >= 0) { |
| 504 |
|
| pqw_notify_drain(aTHX_ w, 0); |
| 505 |
|
| if (w->wake || w->notify_broken) break; |
| 506 |
|
| } |
| 507 |
|
| } |
| 508 |
|
| #else |
| 509 |
|
| /* a signal interrupts the sleep (EINTR), so a TERM lands |
| 510 |
|
| * within one syscall, not one interval */ |
| 511 |
8 |
▶ | if (w->notify_dbh && w->notify_fd >= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->notify_dbh && w->notify_fd >= 0) { |
0 |
8 |
8 |
| if (w->notify_dbh && w->notify_fd >= 0) { |
0 |
0 |
0 |
|
| 512 |
|
| struct pollfd pfd; |
| 513 |
0 |
| pfd.fd = w->notify_fd; |
| 514 |
0 |
| pfd.events = POLLIN; |
| 515 |
0 |
| pfd.revents = 0; |
| 516 |
0 |
▶ | if (poll(&pfd, 1, ms) > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (poll(&pfd, 1, ms) > 0) |
0 |
0 |
0 |
|
| 517 |
0 |
| pqw_notify_drain(aTHX_ w, |
| 518 |
0 |
| (pfd.revents & (POLLIN | POLLERR | POLLHUP)) ? 1 : 0); |
| 519 |
|
| } |
| 520 |
|
| else { |
| 521 |
8 |
| (void)poll(NULL, 0, ms); |
| 522 |
|
| } |
| 523 |
|
| #endif |
| 524 |
|
| } |
| 525 |
8 |
| w->wake = 0; /* the claim at loop top */ |
| 526 |
|
| } |
| 527 |
7 |
| pqw_notify_teardown(aTHX_ w); |
| 528 |
7 |
| } |
| 529 |
|
| |
| 530 |
|
| /* ---- entry ----------------------------------------------------------------- |
| 531 |
|
| * |
| 532 |
|
| * Returns the number of jobs run. The worker object is a blessed hashref |
| 533 |
|
| * {queue, opts}; worker_id is written back onto it so the caller can see |
| 534 |
|
| * which row this run owned. */ |
| 535 |
21 |
| static IV pqw_run(pTHX_ SV *self) { |
| 536 |
21 |
| HV *h = pq_hv(aTHX_ self, "Punk::Queue::Worker"); |
| 537 |
21 |
| SV *queue = pq_get(aTHX_ h, "queue"); |
| 538 |
21 |
| HV *opts = pq_get_hv(aTHX_ h, "opts"); |
| 539 |
|
| pq_worker_run w; |
| 540 |
|
| #ifdef WIN32 |
| 541 |
|
| void (*old_term)(int), (*old_int)(int); |
| 542 |
|
| #else |
| 543 |
|
| struct sigaction sa, old_term, old_int; |
| 544 |
|
| #endif |
| 545 |
|
| SV *v; |
| 546 |
|
| |
| 547 |
21 |
▶ | if (!(queue && SvROK(queue))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(queue && SvROK(queue))) |
21 |
0 |
21 |
| if (!(queue && SvROK(queue))) |
0 |
21 |
21 |
|
| 548 |
0 |
| croak("Punk::Queue::Worker: no queue - construct via $q->worker"); |
| 549 |
|
| |
| 550 |
21 |
| memset(&w, 0, sizeof w); |
| 551 |
21 |
| w.queue = queue; |
| 552 |
21 |
| w.backend = pq_backend_of(aTHX_ queue); |
| 553 |
21 |
| w.sup_fd = -1; |
| 554 |
|
| |
| 555 |
21 |
▶ | w.queues = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "queues") : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| w.queues = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "queues") : NULL); |
21 |
0 |
21 |
|
| 556 |
21 |
▶ | if (av_len(w.queues) < 0) av_push(w.queues, newSVpvs("default")); |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(w.queues) < 0) av_push(w.queues, newSVpvs("default")); |
21 |
0 |
21 |
|
| 557 |
21 |
▶ | w.tasks = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "tasks") : NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| w.tasks = pq_sql_list(aTHX_ opts ? pq_get(aTHX_ opts, "tasks") : NULL); |
21 |
0 |
21 |
|
| 558 |
|
| |
| 559 |
|
| /* dequeue_interval, and only a ceiling now: the horizon clamps it for |
| 560 |
|
| * delayed jobs, NOTIFY undercuts it on Pg, and the jitter caps at 1s |
| 561 |
|
| * on SQLite - which is what makes 5s a safe default where phase 3's |
| 562 |
|
| * 0.5s was load-bearing. */ |
| 563 |
21 |
| w.interval = 5.0; |
| 564 |
21 |
| w.hb_interval = 10.0; |
| 565 |
21 |
| w.sleep_cur = PQW_JITTER_BASE; |
| 566 |
21 |
| w.notify_fd = -1; |
| 567 |
21 |
▶ | if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) |
21 |
0 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) |
1 |
20 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "interval")) && SvOK(v)) |
1 |
0 |
1 |
|
| 568 |
1 |
▶ | w.interval = SvNV(v) > 0.01 ? SvNV(v) : 0.01; |
Branch: 1 branch
| Branch | True | False | Total |
| w.interval = SvNV(v) > 0.01 ? SvNV(v) : 0.01; |
1 |
0 |
1 |
|
| 569 |
21 |
▶ | if (opts && (v = pq_get(aTHX_ opts, "heartbeat_interval")) && SvOK(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && (v = pq_get(aTHX_ opts, "heartbeat_interval")) && SvOK(v)) |
21 |
0 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "heartbeat_interval")) && SvOK(v)) |
0 |
21 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "heartbeat_interval")) && SvOK(v)) |
0 |
0 |
0 |
|
| 570 |
0 |
| w.hb_interval = SvNV(v); |
| 571 |
21 |
▶ | if (opts && (v = pq_get(aTHX_ opts, "max_jobs")) && SvOK(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && (v = pq_get(aTHX_ opts, "max_jobs")) && SvOK(v)) |
21 |
0 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "max_jobs")) && SvOK(v)) |
0 |
21 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "max_jobs")) && SvOK(v)) |
0 |
0 |
0 |
|
| 572 |
0 |
| w.max_jobs = SvIV(v); |
| 573 |
21 |
▶ | if (opts && (v = pq_get(aTHX_ opts, "sup_fd")) && SvOK(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && (v = pq_get(aTHX_ opts, "sup_fd")) && SvOK(v)) |
21 |
0 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "sup_fd")) && SvOK(v)) |
7 |
14 |
21 |
| if (opts && (v = pq_get(aTHX_ opts, "sup_fd")) && SvOK(v)) |
7 |
0 |
7 |
|
| 574 |
7 |
| w.sup_fd = (int)SvIV(v); |
| 575 |
|
| |
| 576 |
|
| { |
| 577 |
21 |
| const char *one = PerlEnv_getenv("PUNK_QUEUE_ONESHOT"); |
| 578 |
21 |
▶ | if (one && *one && strNE(one, "0")) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (one && *one && strNE(one, "0")) { |
14 |
7 |
21 |
| if (one && *one && strNE(one, "0")) { |
14 |
0 |
14 |
| if (one && *one && strNE(one, "0")) { |
14 |
0 |
14 |
|
| 579 |
14 |
| w.oneshot = 1; |
| 580 |
14 |
| w.oneshot_deadline = pq_now_local(aTHX) + 10.0; |
| 581 |
14 |
▶ | if (opts && (v = pq_get(aTHX_ opts, "oneshot_wait")) && SvOK(v)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (opts && (v = pq_get(aTHX_ opts, "oneshot_wait")) && SvOK(v)) |
14 |
0 |
14 |
| if (opts && (v = pq_get(aTHX_ opts, "oneshot_wait")) && SvOK(v)) |
1 |
13 |
14 |
| if (opts && (v = pq_get(aTHX_ opts, "oneshot_wait")) && SvOK(v)) |
1 |
0 |
1 |
|
| 582 |
1 |
| w.oneshot_deadline = pq_now_local(aTHX) + SvNV(v); |
| 583 |
|
| } |
| 584 |
|
| } |
| 585 |
|
| |
| 586 |
|
| /* register before installing handlers, so a TERM during registration |
| 587 |
|
| * still dies loudly rather than being swallowed by a half-set-up run */ |
| 588 |
|
| { |
| 589 |
21 |
| HV *ro = (HV *)sv_2mortal((SV *)newHV()); |
| 590 |
21 |
| SV *qlist = sv_2mortal(newRV_inc((SV *)w.queues)); |
| 591 |
21 |
| (void)hv_stores(ro, "role", newSVpvs("child")); |
| 592 |
21 |
| (void)hv_stores(ro, "queues", newSVsv(qlist)); |
| 593 |
21 |
| w.worker_id = pq_register_worker(aTHX_ w.backend, 0, ro); |
| 594 |
21 |
| (void)hv_stores(h, "worker_id", newSViv(w.worker_id)); |
| 595 |
|
| } |
| 596 |
21 |
| w.hb_due = pq_now_local(aTHX); /* first pass heartbeats immediately */ |
| 597 |
|
| |
| 598 |
21 |
| PQW_STOP = 0; |
| 599 |
|
| #ifdef WIN32 |
| 600 |
|
| /* perl's own signal table, which its console handler consults */ |
| 601 |
|
| old_term = signal(SIGTERM, pqw_sig_stop); |
| 602 |
|
| old_int = signal(SIGINT, pqw_sig_stop); |
| 603 |
|
| #else |
| 604 |
21 |
| memset(&sa, 0, sizeof sa); |
| 605 |
21 |
| sa.sa_handler = pqw_sig_stop; |
| 606 |
21 |
| sigemptyset(&sa.sa_mask); |
| 607 |
21 |
| sa.sa_flags = 0; /* no SA_RESTART: wake the sleep */ |
| 608 |
21 |
| sigaction(SIGTERM, &sa, &old_term); |
| 609 |
21 |
| sigaction(SIGINT, &sa, &old_int); |
| 610 |
|
| #endif |
| 611 |
|
| |
| 612 |
21 |
| w.is_pg = pq_is_pg(aTHX_ w.backend); |
| 613 |
|
| |
| 614 |
|
| /* loop selection */ |
| 615 |
21 |
| w.A = pq_hm(aTHX); |
| 616 |
21 |
▶ | if (w.A) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w.A) { |
7 |
14 |
21 |
|
| 617 |
7 |
| SV *cls = sv_2mortal(newSVpvs("Hyperman::Loop")); |
| 618 |
7 |
| int died = 0; |
| 619 |
7 |
| w.loop_sv = pq_call_meth_ev(aTHX_ cls, "new", NULL, 0, 1, &died); |
| 620 |
7 |
▶ | if (!died && w.loop_sv && SvROK(w.loop_sv)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!died && w.loop_sv && SvROK(w.loop_sv)) |
7 |
0 |
7 |
| if (!died && w.loop_sv && SvROK(w.loop_sv)) |
7 |
0 |
7 |
| if (!died && w.loop_sv && SvROK(w.loop_sv)) |
7 |
0 |
7 |
|
| 621 |
7 |
| w.loop = w.A->loop_of_sv(aTHX_ w.loop_sv); |
| 622 |
0 |
| else { w.A = NULL; } /* degrade to poll */ |
| 623 |
|
| } |
| 624 |
|
| |
| 625 |
21 |
▶ | if (w.A && w.loop) pqw_run_loop(aTHX_ &w); |
Branch: 2 branches
| Branch | True | False | Total |
| if (w.A && w.loop) pqw_run_loop(aTHX_ &w); |
7 |
14 |
21 |
| if (w.A && w.loop) pqw_run_loop(aTHX_ &w); |
7 |
0 |
7 |
|
| 626 |
14 |
| else pqw_run_poll(aTHX_ &w); |
| 627 |
|
| |
| 628 |
|
| #ifdef WIN32 |
| 629 |
|
| (void)signal(SIGTERM, old_term); |
| 630 |
|
| (void)signal(SIGINT, old_int); |
| 631 |
|
| #else |
| 632 |
14 |
| sigaction(SIGTERM, &old_term, NULL); |
| 633 |
14 |
| sigaction(SIGINT, &old_int, NULL); |
| 634 |
|
| #endif |
| 635 |
|
| |
| 636 |
|
| { |
| 637 |
14 |
| int died = 0; |
| 638 |
|
| SV *argv[1], *r; |
| 639 |
14 |
| argv[0] = sv_2mortal(newSViv(w.worker_id)); |
| 640 |
14 |
| r = pq_call_meth_ev(aTHX_ w.backend, "unregister_worker", argv, 1, 1, |
| 641 |
|
| &died); |
| 642 |
14 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
14 |
0 |
14 |
|
| 643 |
|
| } |
| 644 |
14 |
▶ | if (w.loop_sv) SvREFCNT_dec(w.loop_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w.loop_sv) SvREFCNT_dec(w.loop_sv); |
7 |
7 |
14 |
|
| 645 |
|
| |
| 646 |
14 |
| return w.jobs_done; |
| 647 |
|
| } |
| 648 |
|
| |
| 649 |
|
| #endif /* PQ_WORKER_H */ |