include/pq_lock.h

filter: Statementstmt 95.5% 106 / 111 Branchbran 75.0% 33 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 89.6% 139 / 155 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_sql.h ↑ Summary include/pq_repair.h »
1 #ifndef PQ_LOCK_H
2 #define PQ_LOCK_H
3  
4 /* pq_lock.h - distributed locks, two disciplines behind one API.
5 *
6 * The two have genuinely different races and genuinely different arbiters:
7 *
8 * limit 1, the LEASE: an owned row under the partial unique index
9 * pq_locks_name (name WHERE owner IS NOT NULL). DELETE the expired
10 * holder, then INSERT - exactly one insert wins, the rest take a
11 * constraint violation and back off. No count-then-insert race exists,
12 * which is what makes this the phase-9 leader-election arbiter.
13 *
14 * limit N, the COUNTED lock (Minion's semantics): ownerless rows, count
15 * the live ones, insert if under the limit. Count-then-insert needs a
16 * serialisation point: BEGIN IMMEDIATE is already one on SQLite; on Pg
17 * the counted path takes a transaction-scoped advisory lock keyed on a
18 * C hash of the name, so two counters cannot both see "under the limit".
19 *
20 * Expiry is honoured on READ everywhere - an expired row never counts and
21 * never blocks - not just swept by repair, so a stalled repair cannot
22 * deadlock the queue.
23 *
24 * Include after pq_backend.h. */
25  
26 /* FNV-1a over the name, folded positive for pg_advisory_xact_lock. A
27 * collision only costs false serialisation on the counted path, never
28 * correctness, so the 32-bit fallback on a 32-bit-UV perl is fine. */
29 10 static IV pq_lock_hash(const char *s, STRLEN len) {
30 #if UVSIZE >= 8
31 10 UV h = (UV)UINT64_C(14695981039346656037);
32 10 const UV prime = (UV)UINT64_C(1099511628211);
33 #else
34 UV h = 2166136261u;
35 const UV prime = 16777619u;
36 #endif
37 STRLEN i;
38 65 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 55 10 65
39 55 h ^= (U8)s[i];
40 55 h *= prime;
41 }
42 10 return (IV)(h >> 1); /* clear the sign bit portably */
43 }
44  
45 6 static void pq_lock_serialise(pTHX_ SV *self, SV *name) {
46 6 pq_txn_begin(aTHX_ self);
47 6 ▶ if (pq_is_pg(aTHX_ self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pq_is_pg(aTHX_ self)) { 0 6 6
48 STRLEN len;
49 0 const char *s = SvPV_const(name, len);
50 0 AV *b = pq_binds(aTHX);
51 0 pq_bind_iv(aTHX_ b, pq_lock_hash(s, len));
52 0 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
53 "SELECT pg_advisory_xact_lock(?)")), b);
54 }
55 6 }
56  
57 /* Acquire. Returns 1 on success, 0 when the lock is held to its limit.
58 * duration is seconds from now; owner identifies the holder for renewal
59 * (leases only - counted locks are ownerless by design). */
60 27 static IV pq_lock(pTHX_ SV *self, SV *name, double duration, IV limit,
61 IV owner) {
62 double now, expires;
63  
64 27 pq_name_check(aTHX_ name, "lock");
65 27 ▶ if (duration <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (duration <= 0) 0 27 27
66 0 croak("Punk::Queue: lock duration must be positive");
67 27 ▶ if (limit < 1) limit = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (limit < 1) limit = 1; 0 27 27
68 27 now = pq_now(aTHX_ self);
69 27 expires = now + duration;
70  
71 27 ▶ if (limit == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (limit == 1) { 21 6 27
72 /* the lease: sweep the expired holder, then let the unique index
73 * arbitrate. Autocommit on both statements - a trapped violation
74 * inside a transaction would abort it on Pg. */
75 {
76 21 AV *b = pq_binds(aTHX);
77 21 pq_bind_sv(aTHX_ b, name);
78 21 pq_bind_nv(aTHX_ b, now);
79 21 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
80 "DELETE FROM pq_locks WHERE name = ? AND expires <= ?"
81 " AND owner IS NOT NULL")), b);
82 }
83 {
84 21 int died = 0;
85 21 SV *sth = pq_sth(aTHX_ self, sv_2mortal(newSVpvs(
86 "INSERT INTO pq_locks (name, owner, expires)"
87 " VALUES (?, ?, ?)")));
88 SV *ea[3], *r;
89 21 ea[0] = name;
90 21 ea[1] = sv_2mortal(newSViv(owner));
91 21 ea[2] = sv_2mortal(newSVnv(expires));
92 21 r = pq_call_meth_ev(aTHX_ sth, "execute", ea, 3, 1, &died);
93 21 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 19 2 21
94 21 return died ? 0 : 1; /* the violation IS the losing ticket */
95 }
96 }
97  
98 /* the counted lock */
99 {
100 6 IV live = 0, got = 0;
101 6 pq_lock_serialise(aTHX_ self, name);
102 {
103 6 AV *b = pq_binds(aTHX), *row;
104 6 pq_bind_sv(aTHX_ b, name);
105 6 pq_bind_nv(aTHX_ b, now);
106 6 row = pq_selectrow(aTHX_ self, sv_2mortal(newSVpvs(
107 "SELECT count(*) FROM pq_locks"
108 " WHERE name = ? AND expires > ? AND owner IS NULL")), b);
109 6 ▶ if (row) { live = pq_col_iv(aTHX_ row, 0); SvREFCNT_dec((SV *)row); }
Branch: 1 branch
BranchTrueFalseTotal
if (row) { live = pq_col_iv(aTHX_ row, 0); SvREFCNT_dec((SV *)row); } 6 0 6
110 }
111 6 ▶ if (live < limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (live < limit) { 5 1 6
112 5 AV *b = pq_binds(aTHX);
113 5 pq_bind_sv(aTHX_ b, name);
114 5 pq_bind_nv(aTHX_ b, expires);
115 5 (void)pq_do(aTHX_ self, sv_2mortal(newSVpvs(
116 "INSERT INTO pq_locks (name, owner, expires)"
117 " VALUES (?, NULL, ?)")), b);
118 5 got = 1;
119 }
120 6 pq_txn_commit(aTHX_ self);
121 6 return got;
122 }
123 }
124  
125 /* Extend a held lease. 0 rows means the lease was LOST - expired and
126 * possibly taken by someone else - and the caller must re-acquire, not
127 * assume. This return-value honesty is what phase 9's leader loop leans
128 * on: a leader that pauses past its expiry discovers it. */
129 6 static IV pq_renew_lock(pTHX_ SV *self, SV *name, IV owner,
130 double duration) {
131 6 AV *b = pq_binds(aTHX);
132 6 double now = pq_now(aTHX_ self);
133 6 pq_name_check(aTHX_ name, "lock");
134 6 pq_bind_nv(aTHX_ b, now + duration);
135 6 pq_bind_sv(aTHX_ b, name);
136 6 pq_bind_iv(aTHX_ b, owner);
137 6 pq_bind_nv(aTHX_ b, now);
138 6 return pq_do(aTHX_ self, sv_2mortal(newSVpvs(
139 "UPDATE pq_locks SET expires = ?"
140 6 " WHERE name = ? AND owner = ? AND expires > ?")), b) > 0 ? 1 : 0;
141 }
142  
143 /* Release. With an owner: that holder's lease. Without: one counted slot,
144 * oldest first (Minion's semantics - unlock releases A hold, not all of
145 * them). Returns 1 when a row was released. */
146 16 static IV pq_unlock(pTHX_ SV *self, SV *name, IV owner, int has_owner) {
147 16 AV *b = pq_binds(aTHX);
148 16 pq_name_check(aTHX_ name, "lock");
149 16 ▶ if (has_owner) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_owner) { 15 1 16
150 15 pq_bind_sv(aTHX_ b, name);
151 15 pq_bind_iv(aTHX_ b, owner);
152 15 return pq_do(aTHX_ self, sv_2mortal(newSVpvs(
153 "DELETE FROM pq_locks WHERE name = ? AND owner = ?")), b) > 0
154 15 ? 1 : 0;
155 }
156 1 pq_bind_sv(aTHX_ b, name);
157 1 pq_bind_sv(aTHX_ b, name);
158 1 return pq_do(aTHX_ self, sv_2mortal(newSVpvs(
159 "DELETE FROM pq_locks WHERE id ="
160 " (SELECT min(id) FROM pq_locks WHERE name = ?)"
161 1 " AND name = ?")), b) > 0 ? 1 : 0;
162 }
163  
164 10 static SV *pq_list_locks(pTHX_ SV *self, IV offset, IV limit, HV *filter) {
165 10 SV *where = pq_sql_new(aTHX_ "");
166 10 AV *fbind = pq_binds(aTHX);
167 10 HV *out = newHV();
168 10 AV *ls = newAV();
169 SV *v;
170  
171 10 ▶ if (filter && (v = pq_get(aTHX_ filter, "name")) && SvOK(v)) {
Branch: 3 branches
BranchTrueFalseTotal
if (filter && (v = pq_get(aTHX_ filter, "name")) && SvOK(v)) { 10 0 10
if (filter && (v = pq_get(aTHX_ filter, "name")) && SvOK(v)) { 7 3 10
if (filter && (v = pq_get(aTHX_ filter, "name")) && SvOK(v)) { 7 0 7
172 7 pq_sql_cat(aTHX_ where, " WHERE name = ?");
173 7 pq_bind_sv(aTHX_ fbind, v);
174 }
175 {
176 static const char *const cols[] =
177 { "name", "CAST(owner AS TEXT)", NULL };
178 17 ▶ int first = !(filter && (v = pq_get(aTHX_ filter, "name"))
Branch: 2 branches
BranchTrueFalseTotal
int first = !(filter && (v = pq_get(aTHX_ filter, "name")) 10 0 10
int first = !(filter && (v = pq_get(aTHX_ filter, "name")) 7 3 10
179 7 ▶ && SvOK(v));
Branch: 1 branch
BranchTrueFalseTotal
&& SvOK(v)); 0 7 7
180 10 pq_search_filter(aTHX_ where, fbind, &first, filter, cols);
181 }
182  
183 {
184 10 SV *sql = pq_sql_new(aTHX_ "SELECT count(*) FROM pq_locks");
185 AV *row;
186 10 pq_sql_cat_all(aTHX_ sql, where);
187 10 row = pq_selectrow(aTHX_ self, sql, fbind);
188 10 ▶ (void)hv_stores(out, "total",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(out, "total", 10 0 10
189 newSViv(row ? pq_col_iv(aTHX_ row, 0) : 0));
190 10 ▶ if (row) SvREFCNT_dec((SV *)row);
Branch: 1 branch
BranchTrueFalseTotal
if (row) SvREFCNT_dec((SV *)row); 10 0 10
191 }
192  
193 {
194 10 SV *sql = pq_sql_new(aTHX_
195 "SELECT id, name, owner, expires FROM pq_locks");
196 SV *sth;
197 10 AV *bind = pq_binds(aTHX), *row;
198 10 SSize_t n = av_len(fbind) + 1, k;
199 21 ▶ for (k = 0; k < n; k++) pq_bind_sv(aTHX_ bind, *av_fetch(fbind, k, 0));
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < n; k++) pq_bind_sv(aTHX_ bind, *av_fetch(fbind, k, 0)); 11 10 21
200 10 pq_sql_cat_all(aTHX_ sql, where);
201 10 pq_sql_cat(aTHX_ sql, " ORDER BY id LIMIT ? OFFSET ?");
202 10 ▶ pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 50);
Branch: 1 branch
BranchTrueFalseTotal
pq_bind_iv(aTHX_ bind, limit > 0 ? limit : 50); 1 9 10
203 10 pq_bind_iv(aTHX_ bind, offset > 0 ? offset : 0);
204  
205 10 sth = pq_sth(aTHX_ self, sql);
206 10 (void)pq_execute(aTHX_ sth, bind);
207 22 ▶ while ((row = pq_fetchrow(aTHX_ sth))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((row = pq_fetchrow(aTHX_ sth))) { 12 10 22
208 12 HV *h = newHV();
209 12 SV *o = pq_col(aTHX_ row, 2);
210 12 pq_hv_set_iv(aTHX_ h, "id", pq_col_iv(aTHX_ row, 0));
211 12 pq_hv_set_sv(aTHX_ h, "name", pq_col(aTHX_ row, 1));
212 12 ▶ (void)hv_stores(h, "owner",
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "owner", 5 7 12
213 SvOK(o) ? newSViv(SvIV(o)) : newSV(0));
214 12 pq_hv_set_nv_or_undef(aTHX_ h, "expires",
215 pq_col(aTHX_ row, 3));
216 12 av_push(ls, newRV_noinc((SV *)h));
217 12 SvREFCNT_dec((SV *)row);
218 }
219 10 { SV *r = pq_call_meth(aTHX_ sth, "finish", NULL, 0, 1);
220 10 ▶ if (r) SvREFCNT_dec(r); }
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); } 10 0 10
221 }
222  
223 10 (void)hv_stores(out, "locks", newRV_noinc((SV *)ls));
224 10 return newRV_noinc((SV *)out);
225 }
226  
227 #endif /* PQ_LOCK_H */
« include/pq_sql.h ↑ Summary include/pq_repair.h »