include/pq_pg.h

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 97 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Punk/Command/Queue.pm ↑ Summary xs/backend_pg.xs »
1 #ifndef PQ_PG_H
2 #define PQ_PG_H
3  
4 /* pq_pg.h - everything about the PostgreSQL backend that SQLite does
5 * differently. The shared claim lives in pq_claim.h and the shared
6 * operations in pq_backend.h; anything here that is not genuinely
7 * divergent is in the wrong file.
8 *
9 * Include after pq_dbi.h, pq_job.h, pq_claim.h, pq_migrate.h and
10 * pq_migrate_pg.h. */
11  
12 /* Server time as epoch seconds, probed once per connection. */
13 0 static double pq_pg_probe_clock(pTHX_ SV *dbh) {
14 SV *argv[1], *r;
15 double t0, server;
16 0 int died = 0;
17 0 argv[0] = sv_2mortal(newSVpvs("SELECT extract(epoch from now())"));
18 0 t0 = pq_now_local(aTHX);
19 0 r = pq_call_meth_ev(aTHX_ dbh, "selectrow_array", argv, 1, 1, &died);
20 0 ▶ if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; }
Branch: 4 branches
BranchTrueFalseTotal
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 0 0
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 0 0
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 0 0
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 0 0
21 0 server = SvNV(r);
22 0 SvREFCNT_dec(r);
23 0 return pq_probe_delta(aTHX_ t0, server);
24 }
25  
26 /* ---- transactions ----------------------------------------------------------
27 *
28 * DBD::Pg tracks transaction state itself, so this goes through begin_work
29 * and commit rather than raw BEGIN/COMMIT the way SQLite does - a raw BEGIN
30 * with AutoCommit on leaves DBI and the server disagreeing about whether a
31 * transaction is open, and the disagreement surfaces much later.
32 *
33 * The exclusive lock is a transaction-scoped advisory lock, released
34 * automatically at commit or rollback, so a process that dies mid-migration
35 * cannot leave the fleet wedged. The key is an arbitrary constant; it only
36 * has to be the same in every process running this code. */
37 #define PQ_PG_MIGRATE_LOCK "8273412"
38  
39 static void pq_pg_rollback(pTHX_ SV *self);
40  
41 /* A plain transaction, for the state transitions that adjust parents_left
42 * alongside the job row. The defensive rollback first is cheap insurance:
43 * if an earlier transition croaked mid-transaction (connection loss), the
44 * handle is still inside it, and begin_work would fail forever after. */
45 0 static void pq_pg_begin(pTHX_ SV *self) {
46 0 SV *dbh = pq_dbh(aTHX_ self);
47 SV *r;
48 0 pq_pg_rollback(aTHX_ self);
49 0 r = pq_call_meth(aTHX_ dbh, "begin_work", NULL, 0, 1);
50 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
51 0 }
52  
53 0 static void pq_pg_begin_exclusive(pTHX_ SV *self) {
54 0 pq_pg_begin(aTHX_ self);
55 0 (void)pq_do_pv(aTHX_ self,
56 "SELECT pg_advisory_xact_lock(" PQ_PG_MIGRATE_LOCK ")");
57 0 }
58  
59 0 static void pq_pg_commit(pTHX_ SV *self) {
60 0 SV *dbh = pq_dbh(aTHX_ self);
61 0 SV *r = pq_call_meth(aTHX_ dbh, "commit", NULL, 0, 1);
62 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
63 0 }
64  
65 0 static void pq_pg_rollback(pTHX_ SV *self) {
66 0 SV *dbh = pq_dbh(aTHX_ self);
67 0 int died = 0;
68 SV *r;
69 /* Inside a begin_work transaction AutoCommit reads false; outside,
70 * there is nothing to roll back and DBI would warn "rollback
71 * ineffective" - a warning eval cannot silence, so ask first. */
72 0 SV *ac = pq_attr(aTHX_ dbh, "AutoCommit");
73 0 ▶ if (ac && SvTRUE(ac)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (ac && SvTRUE(ac)) return; 0 0 0
if (ac && SvTRUE(ac)) return; 0 0 0
74 0 r = pq_call_meth_ev(aTHX_ dbh, "rollback", NULL, 0, 1, &died);
75 0 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 0 0
76 }
77  
78 /* FOR UPDATE SKIP LOCKED is 9.5. The whole claim depends on it, so check
79 * here where the message can name the cause rather than letting it surface
80 * as a syntax error inside the first dequeue. */
81 0 static void pq_pg_check_version(pTHX_ SV *self) {
82 0 AV *row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
83 "SELECT current_setting('server_version_num')")), NULL);
84 IV num;
85 0 ▶ if (!row) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return; 0 0 0
86 0 num = pq_col_iv(aTHX_ row, 0);
87 0 SvREFCNT_dec((SV *)row);
88 0 ▶ if (num && num < 90500)
Branch: 2 branches
BranchTrueFalseTotal
if (num && num < 90500) 0 0 0
if (num && num < 90500) 0 0 0
89 0 croak("Punk::Queue: PostgreSQL 9.5 or newer is required "
90 "(FOR UPDATE SKIP LOCKED); this server is %ld",
91 (long)num);
92 }
93  
94 0 static const char *const *pq_pg_steps(pTHX) {
95 0 return PQ_PG_UP;
96 }
97  
98 /* ---- the claim -------------------------------------------------------------
99 *
100 * One statement, on a connection with AutoCommit on, so the claim is its own
101 * transaction and no explicit BEGIN is needed. SKIP LOCKED lets contending
102 * workers step over each other's in-flight candidate rows instead of
103 * queueing behind them, which is the entire reason this backend scales past
104 * one machine.
105 *
106 * The redundant `AND state = 'inactive'` on the outer UPDATE closes the
107 * classic re-evaluation race. Minion omits it; a silently duplicated job is
108 * the worst failure a queue has, and the guard costs nothing. */
109 0 static SV *pq_pg_dequeue(pTHX_ SV *self, IV worker_id, AV *queues,
110 AV *tasks) {
111 SV *sql;
112 AV *bind, *row;
113 SSize_t nq, nt;
114 double now;
115 0 SV *out = NULL;
116  
117 0 pq_claim_check(aTHX_ queues);
118 0 nq = av_len(queues) + 1;
119 0 ▶ nt = tasks ? av_len(tasks) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
nt = tasks ? av_len(tasks) + 1 : 0; 0 0 0
120 0 now = pq_now(aTHX_ self);
121  
122 0 sql = pq_sql_new(aTHX_
123 "UPDATE pq_jobs SET state = 'active', started = ?, worker = ?"
124 " WHERE id = (SELECT id FROM pq_jobs WHERE state = 'inactive' AND ");
125 0 pq_claim_where(aTHX_ sql, nq, nt);
126 0 pq_claim_order(aTHX_ sql);
127 0 pq_sql_cat(aTHX_ sql,
128 " FOR UPDATE SKIP LOCKED) AND state = 'inactive'"
129 " RETURNING " PQ_JOB_COLS);
130  
131 0 bind = pq_binds(aTHX);
132 0 pq_bind_nv(aTHX_ bind, now); /* started */
133 0 ▶ if (worker_id) pq_bind_iv(aTHX_ bind, worker_id); /* worker */
Branch: 1 branch
BranchTrueFalseTotal
if (worker_id) pq_bind_iv(aTHX_ bind, worker_id); /* worker */ 0 0 0
134 0 else pq_bind_undef(aTHX_ bind);
135 0 pq_claim_binds(aTHX_ bind, queues, tasks, now);
136  
137 0 row = pq_selectrow(aTHX_ self, sql, bind);
138 0 ▶ if (!row) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!row) return NULL; 0 0 0
139  
140 0 out = pq_claim_row(aTHX_ row, worker_id, now);
141 0 SvREFCNT_dec((SV *)row);
142 0 return out;
143 }
144  
145 /* ---- wakeup ----------------------------------------------------------------
146 *
147 * Emit only; the LISTEN side is phase 5.
148 *
149 * pg_notify() rather than a NOTIFY statement, because it takes the channel
150 * as a text parameter and so needs no identifier quoting at all - the
151 * name-validation rule in pq_sql.h already makes a hostile queue name
152 * impossible, and this makes it impossible twice. NOTIFY is delivered at
153 * commit, so with AutoCommit on the ordering against the insert is free. */
154 0 static void pq_pg_notify(pTHX_ SV *self, SV *queue, IV id) {
155 0 AV *bind = pq_binds(aTHX);
156 0 SV *chan = sv_2mortal(Perl_newSVpvf(aTHX_ "pq.%s", SvPV_nolen(queue)));
157 0 pq_bind_sv(aTHX_ bind, chan);
158 0 pq_bind_iv(aTHX_ bind, id);
159 0 (void)pq_do(aTHX_ self,
160 sv_2mortal(newSVpvs("SELECT pg_notify(?, ?::text)")), bind);
161 0 }
162  
163 #endif /* PQ_PG_H */
« blib/lib/Punk/Command/Queue.pm ↑ Summary xs/backend_pg.xs »