include/pq_sqlite.h

filter: Statementstmt 94.7% 72 / 76 Branchbran 50.0% 30 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 75.0% 102 / 136 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_queue.h ↑ Summary include/pq_tkr.h »
1 #ifndef PQ_SQLITE_H
2 #define PQ_SQLITE_H
3  
4 /* pq_sqlite.h - everything about the SQLite backend that Postgres does
5 * differently. The shared 70% lives in pq_backend.h; if something appears
6 * here that is not genuinely divergent, it is in the wrong file.
7 *
8 * Include after pq_dbi.h, pq_job.h, pq_migrate.h and pq_migrate_sqlite.h. */
9  
10 /* Epoch seconds from SQLite's julian day. Probed once per connection so the
11 * whole dist can bind its own clock plus a delta. */
12 201 static double pq_sqlite_probe_clock(pTHX_ SV *dbh) {
13 SV *argv[1], *r;
14 double t0, server;
15 201 int died = 0;
16 201 argv[0] = sv_2mortal(newSVpvs(
17 "SELECT (julianday('now') - 2440587.5) * 86400.0"));
18 201 t0 = pq_now_local(aTHX);
19 201 r = pq_call_meth_ev(aTHX_ dbh, "selectrow_array", argv, 1, 1, &died);
20 201 ▶ 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; } 201 0 201
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 201 0 201
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 201 201
if (died || !r || !SvOK(r)) { if (r) SvREFCNT_dec(r); return 0.0; } 0 0 0
21 201 server = SvNV(r);
22 201 SvREFCNT_dec(r);
23 /* The delta, not the time: what every later bind adds to its own clock. */
24 201 return pq_probe_delta(aTHX_ t0, server);
25 }
26  
27 /* SQLite's DDL and its transactions are both transactional, and BEGIN
28 * IMMEDIATE takes RESERVED up front - so two workers booting together
29 * serialise at BEGIN rather than deadlocking at COMMIT.
30 *
31 * These go through do() with AutoCommit left on rather than begin_work,
32 * because DBI's begin_work would fight the explicit IMMEDIATE we need. */
33 static void pq_sqlite_rollback(pTHX_ SV *self);
34  
35 555 static void pq_sqlite_begin_exclusive(pTHX_ SV *self) {
36 /* Defensive rollback first: if an earlier transition croaked
37 * mid-transaction (connection trouble), the handle is still inside it
38 * and BEGIN would fail forever after. A rollback with nothing open is
39 * trapped and free. */
40 555 pq_sqlite_rollback(aTHX_ self);
41 555 (void)pq_do_pv(aTHX_ self, "BEGIN IMMEDIATE TRANSACTION");
42 555 }
43  
44 555 static void pq_sqlite_commit(pTHX_ SV *self) {
45 555 (void)pq_do_pv(aTHX_ self, "COMMIT");
46 555 }
47  
48 555 static void pq_sqlite_rollback(pTHX_ SV *self) {
49 555 int died = 0;
50 555 SV *dbh = pq_dbh(aTHX_ self);
51 SV *argv[1], *r;
52 555 argv[0] = sv_2mortal(newSVpvs("ROLLBACK"));
53 /* A rollback with no transaction open is not an error worth
54 * propagating - it happens on the unwind path where something else
55 * already failed, and croaking here would mask that. */
56 555 r = pq_call_meth_ev(aTHX_ dbh, "do", argv, 1, 1, &died);
57 555 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 555 555
58 555 }
59  
60 /* Partial indexes are 3.8.0, and the whole schema depends on them; WAL is
61 * mandatory because a reader must not block the claim transaction. Both are
62 * checked here so the failure names the cause instead of surfacing as a
63 * syntax error inside migration step 1. */
64 332 static void pq_sqlite_check_version(pTHX_ SV *self) {
65 332 SV *dbh = pq_dbh(aTHX_ self);
66 332 SV *v = pq_attr(aTHX_ dbh, "sqlite_version");
67 332 int maj = 0, min = 0;
68  
69 332 ▶ if (v && SvOK(v)) {
Branch: 2 branches
BranchTrueFalseTotal
if (v && SvOK(v)) { 332 0 332
if (v && SvOK(v)) { 332 0 332
70 STRLEN vl;
71 332 const char *s = SvPV_const(v, vl);
72 332 STRLEN i = 0;
73 664 ▶ while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); 664 0 664
while (i < vl && isDIGIT((U8)s[i])) maj = maj * 10 + (s[i++] - '0'); 332 332 664
74 332 ▶ if (i < vl && s[i] == '.') {
Branch: 2 branches
BranchTrueFalseTotal
if (i < vl && s[i] == '.') { 332 0 332
if (i < vl && s[i] == '.') { 332 0 332
75 332 i++;
76 996 ▶ while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); 996 0 996
while (i < vl && isDIGIT((U8)s[i])) min = min * 10 + (s[i++] - '0'); 664 332 996
77 }
78 }
79 332 ▶ if (maj && (maj < 3 || (maj == 3 && min < 8)))
Branch: 4 branches
BranchTrueFalseTotal
if (maj && (maj < 3 || (maj == 3 && min < 8))) 332 0 332
if (maj && (maj < 3 || (maj == 3 && min < 8))) 332 0 332
if (maj && (maj < 3 || (maj == 3 && min < 8))) 332 0 332
if (maj && (maj < 3 || (maj == 3 && min < 8))) 0 332 332
80 0 croak("Punk::Queue: SQLite 3.8 or newer is required (partial "
81 "indexes); this is %d.%d", maj, min);
82  
83 /* WAL is set at connect (pq_after_connect). If it did not take - a
84 * read-only directory, a filesystem that cannot do it, :memory: - say
85 * so now rather than letting workers contend badly later. */
86 {
87 332 AV *row = pq_selectrow(aTHX_ self,
88 sv_2mortal(newSVpvs("PRAGMA journal_mode")), NULL);
89 332 ▶ if (row) {
Branch: 1 branch
BranchTrueFalseTotal
if (row) { 332 0 332
90 332 SV *m = pq_col(aTHX_ row, 0);
91 STRLEN ml;
92 664 ▶ const char *ms = SvOK(m) ? SvPV_const(m, ml) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *ms = SvOK(m) ? SvPV_const(m, ml) : ""; 332 0 332
93 332 ▶ int ok = SvOK(m) && ((ml == 3 && (memEQ(ms, "wal", 3) ||
Branch: 3 branches
BranchTrueFalseTotal
int ok = SvOK(m) && ((ml == 3 && (memEQ(ms, "wal", 3) || 332 0 332
int ok = SvOK(m) && ((ml == 3 && (memEQ(ms, "wal", 3) || 332 0 332
int ok = SvOK(m) && ((ml == 3 && (memEQ(ms, "wal", 3) || 0 332 332
94 0 ▶ memEQ(ms, "WAL", 3)))
Branch: 1 branch
BranchTrueFalseTotal
memEQ(ms, "WAL", 3))) 0 0 0
95 /* :memory: databases report "memory" and
96 * cannot do WAL; they are single-process
97 * by definition, so contention is moot */
98 0 ▶ || (ml == 6 && memEQ(ms, "memory", 6)));
Branch: 2 branches
BranchTrueFalseTotal
|| (ml == 6 && memEQ(ms, "memory", 6))); 0 0 0
|| (ml == 6 && memEQ(ms, "memory", 6))); 0 0 0
99 332 SvREFCNT_dec((SV *)row);
100 332 ▶ if (!ok)
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) 0 332 332
101 0 croak("Punk::Queue: SQLite journal_mode is '%s', not 'wal' - "
102 "a file-backed queue needs WAL so readers do not block "
103 "the claim transaction", ms);
104 }
105 }
106 332 }
107  
108 668 static const char *const *pq_sqlite_steps(pTHX) {
109 668 return PQ_SQLITE_UP;
110 }
111  
112 /* ---- the claim -------------------------------------------------------------
113 *
114 * BEGIN IMMEDIATE, select the best candidate, take it with a guarded
115 * update, commit. The guard (`AND state='inactive'`) should be redundant
116 * under IMMEDIATE - but if it ever is not, a silently duplicated job is the
117 * worst possible failure mode for a queue, and the guard turns it into a
118 * miss instead. It costs nothing.
119 *
120 * The wait behaviour is deliberately absent: phase 1 claims once and
121 * returns. The horizon query, the jittered backoff and the loop-driven
122 * sleep are phase 5, where they can be tested against a real worker. */
123 237 static SV *pq_sqlite_dequeue(pTHX_ SV *self, IV worker_id, AV *queues,
124 AV *tasks) {
125 SV *sql;
126 AV *bind, *row;
127 SSize_t nq, nt;
128 double now;
129 237 SV *out = NULL;
130 IV id, took;
131  
132 237 pq_claim_check(aTHX_ queues);
133 237 nq = av_len(queues) + 1;
134 237 ▶ nt = tasks ? av_len(tasks) + 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
nt = tasks ? av_len(tasks) + 1 : 0; 237 0 237
135 237 now = pq_now(aTHX_ self);
136  
137 237 sql = pq_sql_new(aTHX_ "SELECT " PQ_JOB_COLS
138 " FROM pq_jobs WHERE state = 'inactive' AND ");
139 237 pq_claim_where(aTHX_ sql, nq, nt);
140 237 pq_claim_order(aTHX_ sql);
141  
142 237 bind = pq_binds(aTHX);
143 237 pq_claim_binds(aTHX_ bind, queues, tasks, now);
144  
145 237 pq_sqlite_begin_exclusive(aTHX_ self);
146  
147 237 row = pq_selectrow(aTHX_ self, sql, bind);
148 237 ▶ if (!row) { pq_sqlite_commit(aTHX_ self); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!row) { pq_sqlite_commit(aTHX_ self); return NULL; } 37 200 237
149  
150 200 id = pq_col_iv(aTHX_ row, PQ_C_ID);
151 {
152 200 AV *ub = pq_binds(aTHX);
153 200 pq_bind_nv(aTHX_ ub, now);
154 200 ▶ if (worker_id) pq_bind_iv(aTHX_ ub, worker_id);
Branch: 1 branch
BranchTrueFalseTotal
if (worker_id) pq_bind_iv(aTHX_ ub, worker_id); 125 75 200
155 75 else pq_bind_undef(aTHX_ ub);
156 200 pq_bind_iv(aTHX_ ub, id);
157 200 took = pq_do(aTHX_ self, sv_2mortal(newSVpvs(
158 "UPDATE pq_jobs SET state = 'active', started = ?, worker = ?"
159 " WHERE id = ? AND state = 'inactive'")), ub);
160 }
161  
162 200 ▶ if (took > 0) out = pq_claim_row(aTHX_ row, worker_id, now);
Branch: 1 branch
BranchTrueFalseTotal
if (took > 0) out = pq_claim_row(aTHX_ row, worker_id, now); 200 0 200
163 200 SvREFCNT_dec((SV *)row);
164  
165 200 pq_sqlite_commit(aTHX_ self);
166 200 return out; /* NULL when the guarded update took nothing */
167 }
168  
169 /* SQLite has no LISTEN/NOTIFY and phase 5 does not invent one - a self-pipe
170 * poke would only help a single-process deployment, which is not the case
171 * that needs helping. The no-op keeps the backend contract total so callers
172 * never have to ask which backend they are on. */
173 978 static void pq_sqlite_notify(pTHX_ SV *self, SV *queue, IV id) {
174 PERL_UNUSED_ARG(self); PERL_UNUSED_ARG(queue); PERL_UNUSED_ARG(id);
175 978 }
176  
177 #endif /* PQ_SQLITE_H */
« include/pq_queue.h ↑ Summary include/pq_tkr.h »