| 1 |
|
| #ifndef DBIL_POOL_H |
| 2 |
|
| #define DBIL_POOL_H |
| 3 |
|
| |
| 4 |
|
| /* Backend B - the forked worker pool (the universal backend). |
| 5 |
|
| * |
| 6 |
|
| * Each worker is a forked process holding its own DBI connection (a live dbh |
| 7 |
|
| * cannot be shared across fork). Parent and worker share a duplex socketpair; |
| 8 |
|
| * requests and results cross it as length-prefixed frames whose payload is a |
| 9 |
|
| * Storable-frozen structure. The parent registers each worker's fd with the |
| 10 |
|
| * event loop (via the adapter seam) through a magic-CV closure, so a response |
| 11 |
|
| * resolves the matching future without blocking the loop. |
| 12 |
|
| * |
| 13 |
|
| * request frame : [ is_query, sql, [ @bind ] ] |
| 14 |
|
| * response frame : [ ok, data ] ok=1 -> result hashref, ok=0 -> error string |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #include <sys/socket.h> |
| 18 |
|
| #include <sys/wait.h> |
| 19 |
|
| #include <sys/resource.h> |
| 20 |
|
| #include <signal.h> |
| 21 |
|
| #include <unistd.h> |
| 22 |
|
| #include <string.h> |
| 23 |
|
| #include <errno.h> |
| 24 |
|
| #include <fcntl.h> |
| 25 |
|
| |
| 26 |
|
| #include "dbil_vt.h" /* the pure-C loop seam (adapter vtable) */ |
| 27 |
|
| |
| 28 |
|
| /* ---- Storable (core) serialisation, via cached CVs ------------------------- */ |
| 29 |
|
| |
| 30 |
|
| static SV *DBIL_FREEZE = NULL; /* \&Storable::nfreeze */ |
| 31 |
|
| static SV *DBIL_THAW = NULL; /* \&Storable::thaw */ |
| 32 |
|
| |
| 33 |
23 |
| static void dbil_storable_init(pTHX) { |
| 34 |
23 |
▶ | if (DBIL_FREEZE) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (DBIL_FREEZE) return; |
7 |
16 |
23 |
|
| 35 |
16 |
| eval_pv("require Storable;", 0); |
| 36 |
16 |
| DBIL_FREEZE = (SV *)get_cv("Storable::nfreeze", 0); |
| 37 |
16 |
| DBIL_THAW = (SV *)get_cv("Storable::thaw", 0); |
| 38 |
16 |
▶ | if (!DBIL_FREEZE || !DBIL_THAW) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!DBIL_FREEZE || !DBIL_THAW) |
16 |
0 |
16 |
| if (!DBIL_FREEZE || !DBIL_THAW) |
0 |
16 |
16 |
|
| 39 |
0 |
| croak("DBIx::Loop: Storable (nfreeze/thaw) is required for the pool"); |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
730 |
| static SV *dbil_freeze(pTHX_ SV *data) { /* mortal PV of bytes, or NULL */ |
| 43 |
730 |
| dSP; int n; SV *r = NULL; |
| 44 |
730 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
730 |
730 |
|
| 45 |
730 |
▶ | EXTEND(SP, 1); PUSHs(data); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); PUSHs(data); PUTBACK; |
0 |
730 |
730 |
|
| 46 |
730 |
| n = call_sv(DBIL_FREEZE, G_SCALAR | G_EVAL); |
| 47 |
730 |
| SPAGAIN; |
| 48 |
730 |
▶ | if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
730 |
0 |
730 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
730 |
0 |
730 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
730 |
0 |
730 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
730 |
0 |
730 |
|
| 49 |
0 |
▶ | else if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 50 |
730 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
730 |
0 |
730 |
|
| 51 |
730 |
▶ | return r ? sv_2mortal(r) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return r ? sv_2mortal(r) : NULL; |
730 |
0 |
730 |
|
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
727 |
| static SV *dbil_thaw(pTHX_ const char *p, STRLEN len) { /* mortal ref, or NULL */ |
| 55 |
727 |
| dSP; int n; SV *r = NULL; |
| 56 |
727 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
727 |
727 |
|
| 57 |
727 |
▶ | EXTEND(SP, 1); mPUSHp(p, len); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); mPUSHp(p, len); PUTBACK; |
0 |
727 |
727 |
|
| 58 |
727 |
| n = call_sv(DBIL_THAW, G_SCALAR | G_EVAL); |
| 59 |
727 |
| SPAGAIN; |
| 60 |
727 |
▶ | if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
727 |
0 |
727 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
727 |
0 |
727 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
727 |
0 |
727 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvOK(s)) r = newSVsv(s); } |
727 |
0 |
727 |
|
| 61 |
0 |
▶ | else if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 62 |
727 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
727 |
0 |
727 |
|
| 63 |
727 |
▶ | return r ? sv_2mortal(r) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return r ? sv_2mortal(r) : NULL; |
727 |
0 |
727 |
|
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
|
| /* ---- frame I/O ------------------------------------------------------------- */ |
| 67 |
|
| |
| 68 |
|
| /* write all n bytes (EINTR-safe). 0 ok, -1 on error. |
| 69 |
|
| * |
| 70 |
|
| * A peer that died mid-conversation must surface as the -1 the callers |
| 71 |
|
| * treat as a death notice - NOT as SIGPIPE, whose default disposition |
| 72 |
|
| * kills the whole process before write() can return EPIPE (CPAN Testers: |
| 73 |
|
| * t/02-pool.t exited with signal 13 after every assertion had passed). |
| 74 |
|
| * Linux suppresses it per-send with MSG_NOSIGNAL; where that is missing |
| 75 |
|
| * (macOS, the BSDs) dbil_pool_spawn sets SO_NOSIGPIPE on the pair. */ |
| 76 |
1459 |
| static int dbil_write_all(int fd, const char *p, STRLEN n) { |
| 77 |
1459 |
| STRLEN off = 0; |
| 78 |
2917 |
▶ | while (off < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < n) { |
1459 |
1458 |
2917 |
|
| 79 |
|
| ssize_t w; |
| 80 |
|
| #ifdef MSG_NOSIGNAL |
| 81 |
1459 |
| w = send(fd, p + off, n - off, MSG_NOSIGNAL); |
| 82 |
|
| #else |
| 83 |
|
| w = write(fd, p + off, n - off); |
| 84 |
|
| #endif |
| 85 |
1459 |
▶ | if (w < 0) { if (errno == EINTR) continue; return -1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (w < 0) { if (errno == EINTR) continue; return -1; } |
1 |
1458 |
1459 |
| if (w < 0) { if (errno == EINTR) continue; return -1; } |
0 |
1 |
1 |
|
| 86 |
1458 |
| off += (STRLEN)w; |
| 87 |
|
| } |
| 88 |
1458 |
| return 0; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
|
| /* write a length-prefixed frame (4-byte big-endian len + payload) */ |
| 92 |
730 |
| static int dbil_write_frame(pTHX_ int fd, SV *bytes) { |
| 93 |
730 |
| STRLEN len; const char *p = SvPV_const(bytes, len); |
| 94 |
|
| unsigned char hdr[4]; |
| 95 |
730 |
| hdr[0] = (unsigned char)(len >> 24); hdr[1] = (unsigned char)(len >> 16); |
| 96 |
730 |
| hdr[2] = (unsigned char)(len >> 8); hdr[3] = (unsigned char)(len); |
| 97 |
730 |
▶ | if (dbil_write_all(fd, (const char *)hdr, 4) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_write_all(fd, (const char *)hdr, 4) < 0) return -1; |
1 |
729 |
730 |
|
| 98 |
729 |
| return dbil_write_all(fd, p, len); |
| 99 |
|
| } |
| 100 |
|
| |
| 101 |
|
| /* blocking read of exactly n bytes into buf; 1 ok, 0 EOF, -1 error */ |
| 102 |
0 |
| static int dbil_read_exact(int fd, char *buf, STRLEN n) { |
| 103 |
0 |
| STRLEN off = 0; |
| 104 |
0 |
▶ | while (off < n) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (off < n) { |
0 |
0 |
0 |
|
| 105 |
0 |
| ssize_t r = read(fd, buf + off, n - off); |
| 106 |
0 |
▶ | if (r < 0) { if (errno == EINTR) continue; return -1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (r < 0) { if (errno == EINTR) continue; return -1; } |
0 |
0 |
0 |
| if (r < 0) { if (errno == EINTR) continue; return -1; } |
0 |
0 |
0 |
|
| 107 |
0 |
▶ | if (r == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) return 0; |
0 |
0 |
0 |
|
| 108 |
0 |
| off += (STRLEN)r; |
| 109 |
|
| } |
| 110 |
0 |
| return 1; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| /* blocking read of one frame; returns a mortal PV of the payload, or NULL on |
| 114 |
|
| * EOF / error (used by the worker child) */ |
| 115 |
0 |
| static SV *dbil_read_frame_blocking(pTHX_ int fd) { |
| 116 |
|
| unsigned char hdr[4]; |
| 117 |
|
| STRLEN len; |
| 118 |
|
| SV *payload; |
| 119 |
0 |
| int rc = dbil_read_exact(fd, (char *)hdr, 4); |
| 120 |
0 |
▶ | if (rc <= 0) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc <= 0) return NULL; |
0 |
0 |
0 |
|
| 121 |
0 |
| len = ((STRLEN)hdr[0] << 24) | ((STRLEN)hdr[1] << 16) |
| 122 |
0 |
| | ((STRLEN)hdr[2] << 8) | (STRLEN)hdr[3]; |
| 123 |
0 |
▶ | payload = newSV(len ? len : 1); |
Branch: 1 branch
| Branch | True | False | Total |
| payload = newSV(len ? len : 1); |
0 |
0 |
0 |
|
| 124 |
0 |
| SvPOK_on(payload); |
| 125 |
0 |
▶ | if (len && dbil_read_exact(fd, SvPVX(payload), len) <= 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (len && dbil_read_exact(fd, SvPVX(payload), len) <= 0) { |
0 |
0 |
0 |
| if (len && dbil_read_exact(fd, SvPVX(payload), len) <= 0) { |
0 |
0 |
0 |
|
| 126 |
0 |
| SvREFCNT_dec(payload); return NULL; |
| 127 |
|
| } |
| 128 |
0 |
| SvCUR_set(payload, len); |
| 129 |
0 |
| return sv_2mortal(payload); |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| /* ---- magic-CV closures (the rp_closure / oa_closure pattern) --------------- */ |
| 133 |
|
| |
| 134 |
|
| typedef struct dbil_clos { AV *cap; } dbil_clos; |
| 135 |
|
| |
| 136 |
122 |
| static int dbil_clos_free(pTHX_ SV *sv, MAGIC *mg) { |
| 137 |
122 |
| dbil_clos *c = (dbil_clos *)mg->mg_ptr; |
| 138 |
|
| PERL_UNUSED_ARG(sv); |
| 139 |
122 |
▶ | if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
Branch: 2 branches
| Branch | True | False | Total |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
122 |
0 |
122 |
| if (c) { if (c->cap) SvREFCNT_dec((SV *)c->cap); Safefree(c); } |
122 |
0 |
122 |
|
| 140 |
122 |
| return 0; |
| 141 |
|
| } |
| 142 |
|
| static MGVTBL dbil_clos_vtbl = |
| 143 |
|
| { NULL, NULL, NULL, NULL, dbil_clos_free, NULL, NULL, NULL }; |
| 144 |
|
| |
| 145 |
122 |
| static SV *dbil_closure(pTHX_ XSUBADDR_t body, AV *cap) { |
| 146 |
122 |
| CV *cv = (CV *)newXS(NULL, body, __FILE__); |
| 147 |
|
| dbil_clos *c; |
| 148 |
122 |
| Newxz(c, 1, dbil_clos); |
| 149 |
122 |
| c->cap = cap; |
| 150 |
122 |
| sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &dbil_clos_vtbl, (char *)c, 0); |
| 151 |
122 |
| return newRV_noinc((SV *)cv); |
| 152 |
|
| } |
| 153 |
847 |
| static AV *dbil_clos_cap(pTHX_ CV *cv) { |
| 154 |
847 |
| MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &dbil_clos_vtbl); |
| 155 |
847 |
▶ | return mg ? ((dbil_clos *)mg->mg_ptr)->cap : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return mg ? ((dbil_clos *)mg->mg_ptr)->cap : NULL; |
847 |
0 |
847 |
|
| 156 |
|
| } |
| 157 |
|
| |
| 158 |
|
| /* ---- pool + workers -------------------------------------------------------- */ |
| 159 |
|
| |
| 160 |
|
| typedef struct dbil_worker { |
| 161 |
|
| pid_t pid; |
| 162 |
|
| int fd; /* parent's end of the duplex socketpair */ |
| 163 |
|
| int busy; |
| 164 |
|
| int reserved; /* checked out to a transaction */ |
| 165 |
|
| int gen; /* bumped on respawn: a txn pinned to an */ |
| 166 |
|
| /* earlier gen is lost, not silently moved */ |
| 167 |
|
| SV *future; /* in-flight future (+1), or NULL */ |
| 168 |
|
| SV *rbuf; /* accumulated response bytes (+1) */ |
| 169 |
|
| AV *txq; /* reserved-slot queue [bytes,future,...] (+1) */ |
| 170 |
|
| struct dbil_pool *p; /* back-pointers: the C read-ready */ |
| 171 |
|
| int wi; /* callback's ud is the worker */ |
| 172 |
|
| } dbil_worker; |
| 173 |
|
| |
| 174 |
|
| typedef struct dbil_pool { |
| 175 |
|
| pid_t pid; /* the process that forked these workers */ |
| 176 |
|
| SV *loop; /* the loop adapter SV (+1) */ |
| 177 |
|
| dbil_vt *vt; /* adapter's C vtable, or NULL (Perl seam) */ |
| 178 |
|
| SV *cargs; /* connect args arrayref (+1) */ |
| 179 |
|
| dbil_worker *w; |
| 180 |
|
| int nw; |
| 181 |
|
| int restarts; /* lifetime respawn count (storm cap) */ |
| 182 |
|
| int max_queue; /* pending-request cap; 0 = unlimited */ |
| 183 |
|
| AV *queue; /* pending [bytes, future, bytes, future, ...] */ |
| 184 |
|
| AV *acq; /* futures waiting to acquire a slot (+1 each) */ |
| 185 |
|
| } dbil_pool; |
| 186 |
|
| |
| 187 |
|
| /* ---- fork ownership -------------------------------------------------------- |
| 188 |
|
| * |
| 189 |
|
| * A pool belongs to the process that forked its workers, and to no other. If |
| 190 |
|
| * that process forks - which is exactly what a preforking server does - the |
| 191 |
|
| * child inherits `p`, the worker pids and, worst of all, the parent's ends of |
| 192 |
|
| * the socketpairs. Two processes then read() the same socket, so a response |
| 193 |
|
| * lands in whichever happens to read first and a request can be answered with |
| 194 |
|
| * ANOTHER request's rows. And on the child's exit, dbil_pool_free would |
| 195 |
|
| * kill(SIGTERM) workers the parent and every sibling are still using. |
| 196 |
|
| * |
| 197 |
|
| * So: every entry point checks the owning pid, and an inherited pool is |
| 198 |
|
| * disowned - its fds closed and its memory freed, its processes left alone - |
| 199 |
|
| * and rebuilt for this process. dbil_pool_owned() is the one predicate. */ |
| 200 |
698 |
| static int dbil_pool_owned(const dbil_pool *p) { |
| 201 |
698 |
▶ | return p && p->pid == getpid(); |
Branch: 2 branches
| Branch | True | False | Total |
| return p && p->pid == getpid(); |
698 |
0 |
698 |
| return p && p->pid == getpid(); |
693 |
5 |
698 |
|
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
|
| static void dbil_worker_main(pTHX_ int fd, SV *cargs); /* child; below */ |
| 205 |
|
| static void dbil_pool_dispatch(pTHX_ dbil_pool *p, int wi); |
| 206 |
|
| static void dbil_pool_on_readable(pTHX_ dbil_pool *p, int wi); |
| 207 |
|
| /* dbil_pool_send needs this: a failed write is a death notice, and the slot |
| 208 |
|
| * has to be taken out of service there and then. */ |
| 209 |
|
| static void dbil_pool_worker_died(pTHX_ dbil_pool *p, int wi); |
| 210 |
|
| |
| 211 |
|
| /* In a freshly forked worker: close every descriptor except stdio and `keep`. |
| 212 |
|
| * Called before any Perl runs in the child, so nothing here can be observed by |
| 213 |
|
| * a DESTROY or an END block. */ |
| 214 |
0 |
| static void dbil_close_inherited_fds(int keep) { |
| 215 |
0 |
| int max = -1; |
| 216 |
|
| #ifdef RLIMIT_NOFILE |
| 217 |
|
| struct rlimit rl; |
| 218 |
0 |
▶ | if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_cur != RLIM_INFINITY) |
Branch: 2 branches
| Branch | True | False | Total |
| if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_cur != RLIM_INFINITY) |
0 |
0 |
0 |
| if (getrlimit(RLIMIT_NOFILE, &rl) == 0 && rl.rlim_cur != RLIM_INFINITY) |
0 |
0 |
0 |
|
| 219 |
0 |
| max = (int)rl.rlim_cur; |
| 220 |
|
| #endif |
| 221 |
0 |
▶ | if (max < 0) max = (int)sysconf(_SC_OPEN_MAX); |
Branch: 1 branch
| Branch | True | False | Total |
| if (max < 0) max = (int)sysconf(_SC_OPEN_MAX); |
0 |
0 |
0 |
|
| 222 |
0 |
▶ | if (max < 0 || max > 4096) max = 4096; /* bounded: this is a loop */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (max < 0 || max > 4096) max = 4096; /* bounded: this is a loop */ |
0 |
0 |
0 |
| if (max < 0 || max > 4096) max = 4096; /* bounded: this is a loop */ |
0 |
0 |
0 |
|
| 223 |
|
| { |
| 224 |
|
| int fd; |
| 225 |
0 |
▶ | for (fd = 3; fd < max; fd++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (fd = 3; fd < max; fd++) |
0 |
0 |
0 |
|
| 226 |
0 |
▶ | if (fd != keep) (void)close(fd); |
Branch: 1 branch
| Branch | True | False | Total |
| if (fd != keep) (void)close(fd); |
0 |
0 |
0 |
|
| 227 |
|
| } |
| 228 |
0 |
| } |
| 229 |
|
| |
| 230 |
|
| /* the C-vtable read-ready callback: ud is the worker itself, no Perl frame */ |
| 231 |
0 |
| static void dbil_pool_reader_c(pTHX_ int fd, int mask, void *ud) { |
| 232 |
0 |
| dbil_worker *w = (dbil_worker *)ud; |
| 233 |
|
| PERL_UNUSED_VAR(fd); |
| 234 |
|
| PERL_UNUSED_VAR(mask); |
| 235 |
0 |
| dbil_pool_on_readable(aTHX_ w->p, w->wi); |
| 236 |
0 |
| } |
| 237 |
|
| |
| 238 |
|
| /* the loop's read-ready callback for worker wi: capture = [ poolIV, wiIV ] */ |
| 239 |
|
| XS_INTERNAL(dbil_pool_reader_cb); |
| 240 |
770 |
| XS_INTERNAL(dbil_pool_reader_cb) { |
| 241 |
770 |
| dXSARGS; |
| 242 |
770 |
| AV *cap = dbil_clos_cap(aTHX_ cv); |
| 243 |
|
| dbil_pool *p; |
| 244 |
|
| int wi; |
| 245 |
|
| PERL_UNUSED_VAR(items); |
| 246 |
770 |
▶ | if (!cap) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cap) XSRETURN_EMPTY; |
0 |
770 |
770 |
|
| 247 |
770 |
| p = INT2PTR(dbil_pool *, SvIV(*av_fetch(cap, 0, 0))); |
| 248 |
770 |
| wi = (int)SvIV(*av_fetch(cap, 1, 0)); |
| 249 |
770 |
| dbil_pool_on_readable(aTHX_ p, wi); |
| 250 |
770 |
| XSRETURN_EMPTY; |
| 251 |
|
| } |
| 252 |
|
| |
| 253 |
|
| /* spawn worker wi: socketpair + fork; child runs dbil_worker_main. Registers |
| 254 |
|
| * the parent fd with the loop. Returns 0 ok, -1 on failure. */ |
| 255 |
52 |
| static int dbil_pool_spawn(pTHX_ dbil_pool *p, int wi) { |
| 256 |
|
| int sp[2]; |
| 257 |
|
| pid_t pid; |
| 258 |
52 |
| dbil_worker *w = &p->w[wi]; |
| 259 |
52 |
▶ | if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (socketpair(AF_UNIX, SOCK_STREAM, 0, sp) < 0) return -1; |
0 |
52 |
52 |
|
| 260 |
|
| #ifdef SO_NOSIGPIPE |
| 261 |
|
| /* no MSG_NOSIGNAL on this platform: suppress SIGPIPE at the socket, |
| 262 |
|
| * so a write to a dead peer returns EPIPE (see dbil_write_all) */ |
| 263 |
|
| { |
| 264 |
|
| int one = 1; |
| 265 |
|
| (void)setsockopt(sp[0], SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof one); |
| 266 |
|
| (void)setsockopt(sp[1], SOL_SOCKET, SO_NOSIGPIPE, &one, sizeof one); |
| 267 |
|
| } |
| 268 |
|
| #endif |
| 269 |
52 |
| pid = fork(); |
| 270 |
52 |
▶ | if (pid < 0) { close(sp[0]); close(sp[1]); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pid < 0) { close(sp[0]); close(sp[1]); return -1; } |
0 |
52 |
52 |
|
| 271 |
52 |
▶ | if (pid == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pid == 0) { |
0 |
52 |
52 |
|
| 272 |
|
| int j; |
| 273 |
0 |
| close(sp[0]); |
| 274 |
0 |
▶ | for (j = 0; j < wi; j++) /* drop siblings' parent fds so their */ |
Branch: 1 branch
| Branch | True | False | Total |
| for (j = 0; j < wi; j++) /* drop siblings' parent fds so their */ |
0 |
0 |
0 |
|
| 275 |
0 |
▶ | if (p->w[j].fd >= 0) close(p->w[j].fd); /* EOF is seen when they die */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->w[j].fd >= 0) close(p->w[j].fd); /* EOF is seen when they die */ |
0 |
0 |
0 |
|
| 276 |
|
| /* Drop everything else this process inherited. A DB worker needs one |
| 277 |
|
| * fd - its end of the socketpair - and nothing more. Without this it |
| 278 |
|
| * holds a duplicate of whatever the embedding program had open, and in |
| 279 |
|
| * a web server that includes the listening socket: the port then stays |
| 280 |
|
| * bound for the worker's whole life, long after the HTTP worker that |
| 281 |
|
| * started it has exited, and a graceful drain that waits for the last |
| 282 |
|
| * holder to close never completes. */ |
| 283 |
0 |
| dbil_close_inherited_fds(sp[1]); |
| 284 |
0 |
| dbil_worker_main(aTHX_ sp[1], p->cargs); /* never returns */ |
| 285 |
0 |
| _exit(0); |
| 286 |
|
| } |
| 287 |
52 |
| close(sp[1]); |
| 288 |
|
| /* The parent's end must not survive an exec either. */ |
| 289 |
|
| { |
| 290 |
52 |
| int fl = fcntl(sp[0], F_GETFD); |
| 291 |
52 |
▶ | if (fl >= 0) (void)fcntl(sp[0], F_SETFD, fl | FD_CLOEXEC); |
Branch: 1 branch
| Branch | True | False | Total |
| if (fl >= 0) (void)fcntl(sp[0], F_SETFD, fl | FD_CLOEXEC); |
52 |
0 |
52 |
|
| 292 |
|
| } |
| 293 |
52 |
| w->pid = pid; |
| 294 |
52 |
| w->fd = sp[0]; |
| 295 |
52 |
| w->busy = 0; |
| 296 |
52 |
| w->reserved = 0; |
| 297 |
52 |
| w->gen++; /* txns pinned to the old gen fail */ |
| 298 |
52 |
| w->future = NULL; |
| 299 |
52 |
▶ | if (!w->rbuf) w->rbuf = newSVpvs(""); /* reused across respawns */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->rbuf) w->rbuf = newSVpvs(""); /* reused across respawns */ |
50 |
2 |
52 |
|
| 300 |
52 |
▶ | if (!w->txq) w->txq = newAV(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!w->txq) w->txq = newAV(); |
50 |
2 |
52 |
|
| 301 |
52 |
| w->p = p; |
| 302 |
52 |
| w->wi = wi; |
| 303 |
52 |
▶ | if (p->vt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->vt) { |
0 |
52 |
52 |
|
| 304 |
|
| /* C-vtable adapter: readiness dispatches C-to-C, no Perl frame */ |
| 305 |
0 |
| p->vt->add_reader(aTHX_ p->vt->ctx, w->fd, dbil_pool_reader_c, w); |
| 306 |
|
| } else { |
| 307 |
|
| /* $loop->add_reader($fd, $cb) */ |
| 308 |
52 |
| AV *cap = newAV(); |
| 309 |
|
| SV *cb; |
| 310 |
52 |
| av_push(cap, newSViv(PTR2IV(p))); |
| 311 |
52 |
| av_push(cap, newSViv(wi)); |
| 312 |
52 |
| cb = sv_2mortal(dbil_closure(aTHX_ dbil_pool_reader_cb, cap)); |
| 313 |
|
| { |
| 314 |
52 |
| dSP; |
| 315 |
52 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
52 |
52 |
|
| 316 |
52 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
52 |
52 |
|
| 317 |
52 |
| PUSHs(p->loop); |
| 318 |
52 |
| mPUSHi(w->fd); |
| 319 |
52 |
| PUSHs(cb); |
| 320 |
52 |
| PUTBACK; |
| 321 |
52 |
| call_method("add_reader", G_VOID | G_DISCARD | G_EVAL); |
| 322 |
52 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
52 |
0 |
52 |
|
| 323 |
|
| } |
| 324 |
|
| } |
| 325 |
52 |
| return 0; |
| 326 |
|
| } |
| 327 |
|
| |
| 328 |
|
| /* the worker child: its own dbh, then serve request frames until EOF */ |
| 329 |
0 |
| static void dbil_worker_main(pTHX_ int fd, SV *cargs) { |
| 330 |
0 |
| SV *dbh = NULL; |
| 331 |
0 |
| DBIL_PREPARE_CACHED = 1; /* statement reuse inside the worker */ |
| 332 |
0 |
▶ | AV *ca = (cargs && SvROK(cargs)) ? (AV *)SvRV(cargs) : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| AV *ca = (cargs && SvROK(cargs)) ? (AV *)SvRV(cargs) : NULL; |
0 |
0 |
0 |
| AV *ca = (cargs && SvROK(cargs)) ? (AV *)SvRV(cargs) : NULL; |
0 |
0 |
0 |
|
| 333 |
|
| /* DBI->connect(@$cargs) */ |
| 334 |
0 |
▶ | if (ca) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ca) { |
0 |
0 |
0 |
|
| 335 |
0 |
| dSP; int n; |
| 336 |
0 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
0 |
0 |
|
| 337 |
0 |
▶ | EXTEND(SP, 5); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 5); |
0 |
0 |
0 |
|
| 338 |
0 |
| PUSHs(sv_2mortal(newSVpvs("DBI"))); |
| 339 |
0 |
| PUSHs(*av_fetch(ca, 0, 0)); /* dsn */ |
| 340 |
0 |
▶ | PUSHs(av_exists(ca, 1) ? *av_fetch(ca, 1, 0) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(av_exists(ca, 1) ? *av_fetch(ca, 1, 0) : &PL_sv_undef); |
0 |
0 |
0 |
|
| 341 |
0 |
▶ | PUSHs(av_exists(ca, 2) ? *av_fetch(ca, 2, 0) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(av_exists(ca, 2) ? *av_fetch(ca, 2, 0) : &PL_sv_undef); |
0 |
0 |
0 |
|
| 342 |
0 |
▶ | PUSHs(av_exists(ca, 3) ? *av_fetch(ca, 3, 0) : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(av_exists(ca, 3) ? *av_fetch(ca, 3, 0) : &PL_sv_undef); |
0 |
0 |
0 |
|
| 343 |
0 |
| PUTBACK; |
| 344 |
0 |
| n = call_method("connect", G_SCALAR | G_EVAL); |
| 345 |
0 |
| SPAGAIN; |
| 346 |
0 |
▶ | if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvROK(s)) dbh = SvREFCNT_inc(s); } |
Branch: 4 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvROK(s)) dbh = SvREFCNT_inc(s); } |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvROK(s)) dbh = SvREFCNT_inc(s); } |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvROK(s)) dbh = SvREFCNT_inc(s); } |
0 |
0 |
0 |
| if (!SvTRUE(ERRSV) && n > 0) { SV *s = POPs; if (SvROK(s)) dbh = SvREFCNT_inc(s); } |
0 |
0 |
0 |
|
| 347 |
0 |
▶ | else if (n > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (n > 0) (void)POPs; |
0 |
0 |
0 |
|
| 348 |
0 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
0 |
0 |
0 |
|
| 349 |
|
| } |
| 350 |
0 |
| for (;;) { |
| 351 |
0 |
| SV *req = dbil_read_frame_blocking(aTHX_ fd); |
| 352 |
|
| SV *reqd, *resp, *frozen; |
| 353 |
|
| AV *ra; |
| 354 |
|
| int is_query; |
| 355 |
0 |
| SV *sql, *err = NULL, *res; |
| 356 |
0 |
▶ | if (!req) break; /* parent closed: exit */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!req) break; /* parent closed: exit */ |
0 |
0 |
0 |
|
| 357 |
0 |
| { STRLEN l; const char *pv = SvPV_const(req, l); reqd = dbil_thaw(aTHX_ pv, l); } |
| 358 |
0 |
▶ | if (!reqd || !SvROK(reqd) || SvTYPE(SvRV(reqd)) != SVt_PVAV) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!reqd || !SvROK(reqd) || SvTYPE(SvRV(reqd)) != SVt_PVAV) continue; |
0 |
0 |
0 |
| if (!reqd || !SvROK(reqd) || SvTYPE(SvRV(reqd)) != SVt_PVAV) continue; |
0 |
0 |
0 |
| if (!reqd || !SvROK(reqd) || SvTYPE(SvRV(reqd)) != SVt_PVAV) continue; |
0 |
0 |
0 |
|
| 359 |
0 |
| ra = (AV *)SvRV(reqd); |
| 360 |
0 |
| is_query = (int)SvIV(*av_fetch(ra, 0, 0)); |
| 361 |
0 |
| sql = *av_fetch(ra, 1, 0); |
| 362 |
|
| { |
| 363 |
0 |
| SV **bp = av_fetch(ra, 2, 0); |
| 364 |
0 |
▶ | AV *bind = (bp && *bp && SvROK(*bp)) ? (AV *)SvRV(*bp) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| AV *bind = (bp && *bp && SvROK(*bp)) ? (AV *)SvRV(*bp) : NULL; |
0 |
0 |
0 |
| AV *bind = (bp && *bp && SvROK(*bp)) ? (AV *)SvRV(*bp) : NULL; |
0 |
0 |
0 |
| AV *bind = (bp && *bp && SvROK(*bp)) ? (AV *)SvRV(*bp) : NULL; |
0 |
0 |
0 |
|
| 365 |
0 |
| res = dbh ? dbil_run_dbi(aTHX_ dbh, is_query, sql, bind, &err) |
| 366 |
0 |
▶ | : (err = sv_2mortal(newSVpvs("worker has no database handle")), NULL); |
Branch: 1 branch
| Branch | True | False | Total |
| : (err = sv_2mortal(newSVpvs("worker has no database handle")), NULL); |
0 |
0 |
0 |
|
| 367 |
|
| } |
| 368 |
|
| { |
| 369 |
0 |
| AV *out = newAV(); |
| 370 |
0 |
| av_push(out, newSViv(res ? 1 : 0)); |
| 371 |
0 |
▶ | av_push(out, res ? newSVsv(res) |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(out, res ? newSVsv(res) |
0 |
0 |
0 |
| av_push(out, res ? newSVsv(res) |
0 |
0 |
0 |
|
| 372 |
|
| : newSVsv(err ? err : sv_2mortal(newSVpvs("failed")))); |
| 373 |
0 |
| resp = sv_2mortal(newRV_noinc((SV *)out)); |
| 374 |
|
| } |
| 375 |
0 |
| frozen = dbil_freeze(aTHX_ resp); |
| 376 |
0 |
▶ | if (frozen) (void)dbil_write_frame(aTHX_ fd, frozen); |
Branch: 1 branch
| Branch | True | False | Total |
| if (frozen) (void)dbil_write_frame(aTHX_ fd, frozen); |
0 |
0 |
0 |
|
| 377 |
|
| } |
| 378 |
0 |
| close(fd); |
| 379 |
0 |
| _exit(0); |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
|
| /* send request bytes to worker wi and mark it busy with `future` (+1 taken) */ |
| 383 |
|
| /* Can anything still take general (non-transaction) work? */ |
| 384 |
1 |
| static int dbil_pool_has_live_worker(const dbil_pool *p) { |
| 385 |
|
| int wi; |
| 386 |
1 |
▶ | for (wi = 0; wi < p->nw; wi++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (wi = 0; wi < p->nw; wi++) |
1 |
0 |
1 |
|
| 387 |
1 |
▶ | if (p->w[wi].fd >= 0 && !p->w[wi].reserved) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p->w[wi].fd >= 0 && !p->w[wi].reserved) return 1; |
1 |
0 |
1 |
| if (p->w[wi].fd >= 0 && !p->w[wi].reserved) return 1; |
1 |
0 |
1 |
|
| 388 |
0 |
| return 0; |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
730 |
| static void dbil_pool_send(pTHX_ dbil_pool *p, int wi, SV *bytes, SV *future) { |
| 392 |
730 |
| dbil_worker *w = &p->w[wi]; |
| 393 |
|
| int reserved; |
| 394 |
|
| |
| 395 |
730 |
| w->busy = 1; |
| 396 |
730 |
| w->future = SvREFCNT_inc(future); |
| 397 |
730 |
▶ | if (dbil_write_frame(aTHX_ w->fd, bytes) >= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_write_frame(aTHX_ w->fd, bytes) >= 0) return; |
729 |
1 |
730 |
|
| 398 |
|
| |
| 399 |
|
| /* The write failed, so the frame did not arrive whole and the statement |
| 400 |
|
| * provably never ran: dbil_write_frame only returns < 0 having written |
| 401 |
|
| * less than all of it. |
| 402 |
|
| * |
| 403 |
|
| * What matters here is the SLOT, not this one request. A worker killed |
| 404 |
|
| * while idle is invisible until something is written to it - death is |
| 405 |
|
| * otherwise only ever noticed as EOF on the read side, and an idle |
| 406 |
|
| * worker's fd is not readable - so this is the first and only moment we |
| 407 |
|
| * learn about it. Leaving the slot alive meant dbil_pool_run kept |
| 408 |
|
| * choosing it, being the lowest-numbered idle worker with a live fd, and |
| 409 |
|
| * failed every future from then on while the healthy workers sat idle. |
| 410 |
|
| * Treat a failed write as the death notice it is. */ |
| 411 |
1 |
| reserved = w->reserved; |
| 412 |
|
| |
| 413 |
1 |
▶ | if (!reserved) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!reserved) { |
1 |
0 |
1 |
|
| 414 |
|
| /* Nothing executed, so this is a retry and not a loss. Put it back at |
| 415 |
|
| * the head of the queue so it keeps its place, and let the respawn |
| 416 |
|
| * inside worker_died pick it up. */ |
| 417 |
1 |
| SvREFCNT_dec(w->future); |
| 418 |
1 |
| w->future = NULL; |
| 419 |
1 |
| w->busy = 0; |
| 420 |
1 |
| av_unshift(p->queue, 2); |
| 421 |
1 |
| av_store(p->queue, 0, newSVsv(bytes)); |
| 422 |
1 |
| av_store(p->queue, 1, SvREFCNT_inc(future)); |
| 423 |
|
| } |
| 424 |
|
| /* A reserved slot is a transaction pinned to that one connection. It |
| 425 |
|
| * cannot be moved, so leave the future in place for worker_died to fail |
| 426 |
|
| * alongside the rest of the transaction. */ |
| 427 |
|
| |
| 428 |
1 |
| dbil_pool_worker_died(aTHX_ p, wi); |
| 429 |
|
| |
| 430 |
|
| /* Respawn is capped and can fail. If it did, and nothing is left to run |
| 431 |
|
| * on, fail what we just queued rather than leave callers waiting on a |
| 432 |
|
| * pool that can no longer answer. */ |
| 433 |
1 |
▶ | if (!reserved && !dbil_pool_has_live_worker(p)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!reserved && !dbil_pool_has_live_worker(p)) { |
1 |
0 |
1 |
| if (!reserved && !dbil_pool_has_live_worker(p)) { |
0 |
1 |
1 |
|
| 434 |
0 |
▶ | while (av_len(p->queue) >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(p->queue) >= 1) { |
0 |
0 |
0 |
|
| 435 |
0 |
| SV *qb = av_shift(p->queue); |
| 436 |
0 |
| SV *qf = av_shift(p->queue); |
| 437 |
0 |
| SvREFCNT_dec(qb); |
| 438 |
0 |
| dbil_future_settle_fail(aTHX_ qf, |
| 439 |
|
| sv_2mortal(newSVpvs("pool has no live workers"))); |
| 440 |
0 |
| SvREFCNT_dec(qf); |
| 441 |
|
| } |
| 442 |
|
| } |
| 443 |
|
| } |
| 444 |
|
| |
| 445 |
|
| /* run one statement on the pool; returns the future (+1) */ |
| 446 |
684 |
| static SV *dbil_pool_run(pTHX_ dbil_pool *p, int is_query, SV *sql, AV *bind) { |
| 447 |
684 |
| SV *future = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 448 |
|
| SV *bytes; |
| 449 |
|
| int wi; |
| 450 |
|
| { /* freeze the request tuple */ |
| 451 |
684 |
| AV *req = newAV(); |
| 452 |
684 |
| av_push(req, newSViv(is_query)); |
| 453 |
684 |
| av_push(req, newSVsv(sql)); |
| 454 |
684 |
| av_push(req, dbil_bind_rv(aTHX_ bind)); |
| 455 |
684 |
| bytes = dbil_freeze(aTHX_ sv_2mortal(newRV_noinc((SV *)req))); |
| 456 |
|
| } |
| 457 |
684 |
▶ | if (!bytes) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bytes) { |
0 |
684 |
684 |
|
| 458 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 459 |
|
| sv_2mortal(newSVpvs("could not serialise request"))); |
| 460 |
0 |
| return future; |
| 461 |
|
| } |
| 462 |
756 |
▶ | for (wi = 0; wi < p->nw; wi++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (wi = 0; wi < p->nw; wi++) |
736 |
20 |
756 |
|
| 463 |
736 |
▶ | if (!p->w[wi].busy && !p->w[wi].reserved && p->w[wi].fd >= 0) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!p->w[wi].busy && !p->w[wi].reserved && p->w[wi].fd >= 0) |
665 |
71 |
736 |
| if (!p->w[wi].busy && !p->w[wi].reserved && p->w[wi].fd >= 0) |
664 |
1 |
665 |
| if (!p->w[wi].busy && !p->w[wi].reserved && p->w[wi].fd >= 0) |
664 |
0 |
664 |
|
| 464 |
664 |
| { dbil_pool_send(aTHX_ p, wi, bytes, future); return future; } |
| 465 |
|
| /* all busy: queue (bytes, future) - unless the backpressure cap is hit */ |
| 466 |
20 |
▶ | if (p->max_queue > 0 && (av_len(p->queue) + 1) / 2 >= p->max_queue) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (p->max_queue > 0 && (av_len(p->queue) + 1) / 2 >= p->max_queue) { |
3 |
17 |
20 |
| if (p->max_queue > 0 && (av_len(p->queue) + 1) / 2 >= p->max_queue) { |
1 |
2 |
3 |
|
| 467 |
1 |
| dbil_future_settle_fail(aTHX_ future, |
| 468 |
|
| sv_2mortal(newSVpvs("queue full (max_queue exceeded)"))); |
| 469 |
1 |
| return future; |
| 470 |
|
| } |
| 471 |
19 |
| av_push(p->queue, newSVsv(bytes)); |
| 472 |
19 |
| av_push(p->queue, SvREFCNT_inc(future)); |
| 473 |
19 |
| return future; |
| 474 |
|
| } |
| 475 |
|
| |
| 476 |
|
| /* pull the next queued request onto the now-free worker wi. A reserved |
| 477 |
|
| * worker only drains its own transaction queue; a free worker first serves a |
| 478 |
|
| * waiting acquire() (reserving itself), then the general queue. */ |
| 479 |
743 |
| static void dbil_pool_dispatch(pTHX_ dbil_pool *p, int wi) { |
| 480 |
743 |
| dbil_worker *w = &p->w[wi]; |
| 481 |
743 |
▶ | if (w->reserved) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->reserved) { |
34 |
709 |
743 |
|
| 482 |
34 |
▶ | if (!w->busy && av_len(w->txq) >= 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!w->busy && av_len(w->txq) >= 1) { |
2 |
32 |
34 |
| if (!w->busy && av_len(w->txq) >= 1) { |
1 |
1 |
2 |
|
| 483 |
1 |
| SV *bytes = av_shift(w->txq); |
| 484 |
1 |
| SV *future = av_shift(w->txq); |
| 485 |
1 |
| dbil_pool_send(aTHX_ p, wi, sv_2mortal(bytes), future); |
| 486 |
1 |
| SvREFCNT_dec(future); /* send took its own +1 */ |
| 487 |
|
| } |
| 488 |
34 |
| return; |
| 489 |
|
| } |
| 490 |
709 |
▶ | if (av_len(p->acq) >= 0) { /* a txn is waiting for a slot */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(p->acq) >= 0) { /* a txn is waiting for a slot */ |
2 |
707 |
709 |
|
| 491 |
2 |
| SV *fut = av_shift(p->acq); |
| 492 |
2 |
| w->reserved = 1; |
| 493 |
2 |
| dbil_future_settle_done1(aTHX_ fut, sv_2mortal(newSViv(wi))); |
| 494 |
2 |
| SvREFCNT_dec(fut); |
| 495 |
2 |
| return; |
| 496 |
|
| } |
| 497 |
707 |
▶ | if (av_len(p->queue) >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(p->queue) >= 1) { |
20 |
687 |
707 |
|
| 498 |
20 |
| SV *bytes = av_shift(p->queue); |
| 499 |
20 |
| SV *future = av_shift(p->queue); |
| 500 |
20 |
| dbil_pool_send(aTHX_ p, wi, sv_2mortal(bytes), future); |
| 501 |
20 |
| SvREFCNT_dec(future); /* send took its own +1 */ |
| 502 |
|
| } |
| 503 |
|
| } |
| 504 |
|
| |
| 505 |
|
| /* ---- slot checkout (transactions) ------------------------------------------ */ |
| 506 |
|
| |
| 507 |
|
| /* acquire a slot: future resolves with the worker index (reserved for the |
| 508 |
|
| * caller). Resolves immediately when a slot is free. (+1) */ |
| 509 |
14 |
| static SV *dbil_pool_acquire(pTHX_ dbil_pool *p) { |
| 510 |
14 |
| SV *future = dbil_future_new(aTHX_ "DBIx::Loop::Future"); |
| 511 |
|
| int wi; |
| 512 |
23 |
▶ | for (wi = 0; wi < p->nw; wi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (wi = 0; wi < p->nw; wi++) { |
21 |
2 |
23 |
|
| 513 |
21 |
| dbil_worker *w = &p->w[wi]; |
| 514 |
21 |
▶ | if (!w->busy && !w->reserved && w->fd >= 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!w->busy && !w->reserved && w->fd >= 0) { |
12 |
9 |
21 |
| if (!w->busy && !w->reserved && w->fd >= 0) { |
12 |
0 |
12 |
| if (!w->busy && !w->reserved && w->fd >= 0) { |
12 |
0 |
12 |
|
| 515 |
12 |
| w->reserved = 1; |
| 516 |
12 |
| dbil_future_settle_done1(aTHX_ future, sv_2mortal(newSViv(wi))); |
| 517 |
12 |
| return future; |
| 518 |
|
| } |
| 519 |
|
| } |
| 520 |
2 |
| av_push(p->acq, SvREFCNT_inc(future)); |
| 521 |
2 |
| return future; |
| 522 |
|
| } |
| 523 |
|
| |
| 524 |
|
| /* release a reserved slot back to the pool and let it pick up work */ |
| 525 |
14 |
| static void dbil_pool_release(pTHX_ dbil_pool *p, int wi) { |
| 526 |
14 |
▶ | if (wi < 0 || wi >= p->nw) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (wi < 0 || wi >= p->nw) return; |
14 |
0 |
14 |
| if (wi < 0 || wi >= p->nw) return; |
0 |
14 |
14 |
|
| 527 |
14 |
| p->w[wi].reserved = 0; |
| 528 |
14 |
▶ | if (!p->w[wi].busy) dbil_pool_dispatch(aTHX_ p, wi); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p->w[wi].busy) dbil_pool_dispatch(aTHX_ p, wi); |
14 |
0 |
14 |
|
| 529 |
|
| } |
| 530 |
|
| |
| 531 |
|
| /* send a statement on a reserved slot (FIFO behind any in-flight one). |
| 532 |
|
| * gen guards a txn whose worker died and respawned: that txn is lost, not |
| 533 |
|
| * silently continued on a fresh connection. */ |
| 534 |
46 |
| static void dbil_pool_send_tx(pTHX_ dbil_pool *p, int wi, int gen, |
| 535 |
|
| SV *bytes, SV *future) { |
| 536 |
|
| dbil_worker *w; |
| 537 |
46 |
▶ | if (wi < 0 || wi >= p->nw) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (wi < 0 || wi >= p->nw) { |
46 |
0 |
46 |
| if (wi < 0 || wi >= p->nw) { |
0 |
46 |
46 |
|
| 538 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 539 |
|
| sv_2mortal(newSVpvs("transaction has no slot"))); |
| 540 |
0 |
| return; |
| 541 |
|
| } |
| 542 |
46 |
| w = &p->w[wi]; |
| 543 |
46 |
▶ | if (w->fd < 0 || w->gen != gen) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->fd < 0 || w->gen != gen) { |
46 |
0 |
46 |
| if (w->fd < 0 || w->gen != gen) { |
0 |
46 |
46 |
|
| 544 |
0 |
| dbil_future_settle_fail(aTHX_ future, |
| 545 |
|
| sv_2mortal(newSVpvs("transaction lost (worker died)"))); |
| 546 |
0 |
| return; |
| 547 |
|
| } |
| 548 |
46 |
▶ | if (w->busy) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->busy) { |
1 |
45 |
46 |
|
| 549 |
1 |
| av_push(w->txq, newSVsv(bytes)); |
| 550 |
1 |
| av_push(w->txq, SvREFCNT_inc(future)); |
| 551 |
1 |
| return; |
| 552 |
|
| } |
| 553 |
45 |
| dbil_pool_send(aTHX_ p, wi, bytes, future); |
| 554 |
|
| } |
| 555 |
|
| |
| 556 |
|
| /* unregister fd from the loop via whichever seam the adapter has */ |
| 557 |
2 |
| static void dbil_pool_loop_remove(pTHX_ dbil_pool *p, int fd) { |
| 558 |
2 |
▶ | if (p->vt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->vt) { |
0 |
2 |
2 |
|
| 559 |
0 |
| p->vt->remove(aTHX_ p->vt->ctx, fd); |
| 560 |
|
| } else { |
| 561 |
2 |
| dSP; |
| 562 |
2 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
2 |
2 |
|
| 563 |
2 |
▶ | EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(fd); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(fd); PUTBACK; |
0 |
2 |
2 |
|
| 564 |
2 |
| call_method("remove", G_VOID | G_DISCARD | G_EVAL); |
| 565 |
2 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
2 |
0 |
2 |
|
| 566 |
|
| } |
| 567 |
2 |
| } |
| 568 |
|
| |
| 569 |
|
| /* worker wi died (EOF/error on its fd): fail the in-flight future, drop the |
| 570 |
|
| * dead fd from the loop, reap the zombie, and spawn a replacement in place |
| 571 |
|
| * (capped, so a fast-crashing worker cannot respawn-storm). Queued work is |
| 572 |
|
| * re-dispatched onto the replacement. */ |
| 573 |
|
| #define DBIL_MAX_RESTARTS 64 |
| 574 |
|
| |
| 575 |
2 |
| static void dbil_pool_worker_died(pTHX_ dbil_pool *p, int wi) { |
| 576 |
2 |
| dbil_worker *w = &p->w[wi]; |
| 577 |
2 |
▶ | if (w->future) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->future) { |
1 |
1 |
2 |
|
| 578 |
1 |
| dbil_future_settle_fail(aTHX_ w->future, |
| 579 |
|
| sv_2mortal(newSVpvs("worker exited unexpectedly"))); |
| 580 |
1 |
| SvREFCNT_dec(w->future); w->future = NULL; |
| 581 |
|
| } |
| 582 |
2 |
| dbil_pool_loop_remove(aTHX_ p, w->fd); |
| 583 |
2 |
| close(w->fd); |
| 584 |
2 |
| w->fd = -1; |
| 585 |
2 |
| w->busy = 0; |
| 586 |
2 |
▶ | if (w->pid > 0) { waitpid(w->pid, NULL, 0); w->pid = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->pid > 0) { waitpid(w->pid, NULL, 0); w->pid = 0; } |
2 |
0 |
2 |
|
| 587 |
2 |
▶ | if (w->rbuf) SvCUR_set(w->rbuf, 0); /* drop any partial frame */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->rbuf) SvCUR_set(w->rbuf, 0); /* drop any partial frame */ |
2 |
0 |
2 |
|
| 588 |
|
| /* a transaction pinned here is lost: fail everything queued on the slot |
| 589 |
|
| * (the gen bump in spawn stops later tx statements too) */ |
| 590 |
2 |
▶ | while (av_len(w->txq) >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(w->txq) >= 1) { |
0 |
2 |
2 |
|
| 591 |
0 |
| SV *bytes = av_shift(w->txq); |
| 592 |
0 |
| SV *fut = av_shift(w->txq); |
| 593 |
0 |
| SvREFCNT_dec(bytes); |
| 594 |
0 |
| dbil_future_settle_fail(aTHX_ fut, |
| 595 |
|
| sv_2mortal(newSVpvs("transaction lost (worker died)"))); |
| 596 |
0 |
| SvREFCNT_dec(fut); |
| 597 |
|
| } |
| 598 |
2 |
| w->reserved = 0; |
| 599 |
2 |
▶ | if (p->restarts >= DBIL_MAX_RESTARTS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->restarts >= DBIL_MAX_RESTARTS) { |
0 |
2 |
2 |
|
| 600 |
0 |
| warn("DBIx::Loop: worker restart limit reached; slot %d left dead", wi); |
| 601 |
0 |
| return; |
| 602 |
|
| } |
| 603 |
2 |
| p->restarts++; |
| 604 |
2 |
▶ | if (dbil_pool_spawn(aTHX_ p, wi) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_pool_spawn(aTHX_ p, wi) < 0) { |
0 |
2 |
2 |
|
| 605 |
0 |
| warn("DBIx::Loop: could not respawn pool worker %d: %s", |
| 606 |
|
| wi, strerror(errno)); |
| 607 |
0 |
| return; |
| 608 |
|
| } |
| 609 |
2 |
| dbil_pool_dispatch(aTHX_ p, wi); /* pick up queued work */ |
| 610 |
|
| } |
| 611 |
|
| |
| 612 |
|
| /* worker wi is readable: drain bytes, settle any complete frame's future */ |
| 613 |
770 |
| static void dbil_pool_on_readable(pTHX_ dbil_pool *p, int wi) { |
| 614 |
770 |
| dbil_worker *w = &p->w[wi]; |
| 615 |
|
| char chunk[65536]; |
| 616 |
|
| ssize_t r; |
| 617 |
771 |
▶ | if (w->fd < 0) return; /* already dead */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->fd < 0) return; /* already dead */ |
0 |
770 |
770 |
|
| 618 |
770 |
| r = read(w->fd, chunk, sizeof chunk); |
| 619 |
770 |
▶ | if (r <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r <= 0) { |
1 |
769 |
770 |
|
| 620 |
1 |
▶ | if (r < 0 && errno == EINTR) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (r < 0 && errno == EINTR) return; |
0 |
1 |
1 |
| if (r < 0 && errno == EINTR) return; |
0 |
0 |
0 |
|
| 621 |
1 |
| dbil_pool_worker_died(aTHX_ p, wi); |
| 622 |
1 |
| return; |
| 623 |
|
| } |
| 624 |
769 |
| sv_catpvn(w->rbuf, chunk, (STRLEN)r); |
| 625 |
727 |
| for (;;) { |
| 626 |
1496 |
| STRLEN have = SvCUR(w->rbuf); |
| 627 |
1496 |
| const unsigned char *b = (const unsigned char *)SvPVX(w->rbuf); |
| 628 |
|
| STRLEN len; |
| 629 |
1496 |
▶ | if (have < 4) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (have < 4) break; |
727 |
769 |
1496 |
|
| 630 |
769 |
| len = ((STRLEN)b[0] << 24) | ((STRLEN)b[1] << 16) |
| 631 |
769 |
| | ((STRLEN)b[2] << 8) | (STRLEN)b[3]; |
| 632 |
769 |
▶ | if (have < 4 + len) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (have < 4 + len) break; |
42 |
727 |
769 |
|
| 633 |
|
| { |
| 634 |
727 |
| SV *resp = dbil_thaw(aTHX_ (const char *)b + 4, len); |
| 635 |
727 |
| SV *fut = w->future; |
| 636 |
727 |
| w->future = NULL; |
| 637 |
727 |
| w->busy = 0; |
| 638 |
727 |
▶ | if (fut) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fut) { |
727 |
0 |
727 |
|
| 639 |
1454 |
▶ | if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { |
727 |
0 |
727 |
| if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { |
727 |
0 |
727 |
| if (resp && SvROK(resp) && SvTYPE(SvRV(resp)) == SVt_PVAV) { |
727 |
0 |
727 |
|
| 640 |
727 |
| AV *ra = (AV *)SvRV(resp); |
| 641 |
727 |
| int ok = (int)SvIV(*av_fetch(ra, 0, 0)); |
| 642 |
727 |
| SV *data = *av_fetch(ra, 1, 0); |
| 643 |
727 |
▶ | if (ok) dbil_future_settle_done1(aTHX_ fut, data); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) dbil_future_settle_done1(aTHX_ fut, data); |
715 |
12 |
727 |
|
| 644 |
12 |
| else dbil_future_settle_fail(aTHX_ fut, data); |
| 645 |
|
| } else { |
| 646 |
0 |
| dbil_future_settle_fail(aTHX_ fut, |
| 647 |
|
| sv_2mortal(newSVpvs("bad response frame from worker"))); |
| 648 |
|
| } |
| 649 |
727 |
| SvREFCNT_dec(fut); |
| 650 |
|
| } |
| 651 |
|
| } |
| 652 |
|
| /* consume the frame from the buffer */ |
| 653 |
727 |
| sv_chop(w->rbuf, SvPVX(w->rbuf) + 4 + len); |
| 654 |
727 |
| dbil_pool_dispatch(aTHX_ p, wi); |
| 655 |
|
| } |
| 656 |
|
| } |
| 657 |
|
| |
| 658 |
|
| /* create + spawn the pool for this object (stored back as _pool IV) */ |
| 659 |
23 |
| static dbil_pool *dbil_pool_new(pTHX_ SV *loop, SV *cargs, int nworkers) { |
| 660 |
|
| dbil_pool *p; |
| 661 |
|
| int i; |
| 662 |
23 |
| dbil_storable_init(aTHX); |
| 663 |
23 |
| Newxz(p, 1, dbil_pool); |
| 664 |
23 |
| p->pid = getpid(); /* the owner; see dbil_pool_owned */ |
| 665 |
23 |
| p->loop = SvREFCNT_inc(loop); |
| 666 |
23 |
| p->vt = dbil_vt_of(aTHX_ loop); /* C seam when the adapter has one */ |
| 667 |
23 |
| p->cargs = SvREFCNT_inc(cargs); |
| 668 |
23 |
| p->nw = nworkers; |
| 669 |
23 |
| p->queue = newAV(); |
| 670 |
23 |
| p->acq = newAV(); |
| 671 |
23 |
| Newxz(p->w, nworkers, dbil_worker); |
| 672 |
73 |
▶ | for (i = 0; i < nworkers; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nworkers; i++) { |
50 |
23 |
73 |
|
| 673 |
50 |
▶ | if (dbil_pool_spawn(aTHX_ p, i) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dbil_pool_spawn(aTHX_ p, i) < 0) |
0 |
50 |
50 |
|
| 674 |
0 |
| croak("DBIx::Loop: could not spawn pool worker %d: %s", i, strerror(errno)); |
| 675 |
|
| } |
| 676 |
23 |
| return p; |
| 677 |
|
| } |
| 678 |
|
| |
| 679 |
|
| /* Release an INHERITED pool: close the fds this process got by forking, free |
| 680 |
|
| * the memory, and leave the workers alone - they are not ours to signal, and |
| 681 |
|
| * the process that forked them is still using them. No futures are settled and |
| 682 |
|
| * no loop callbacks are removed, because none of that state belongs to us |
| 683 |
|
| * either; it is the parent's, and we are only holding copies of the pointers. */ |
| 684 |
5 |
| static void dbil_pool_disown(pTHX_ dbil_pool *p) { |
| 685 |
|
| int i; |
| 686 |
5 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
5 |
5 |
|
| 687 |
15 |
▶ | for (i = 0; i < p->nw; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < p->nw; i++) { |
10 |
5 |
15 |
|
| 688 |
10 |
| dbil_worker *w = &p->w[i]; |
| 689 |
10 |
▶ | if (w->fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->fd >= 0) { |
10 |
0 |
10 |
|
| 690 |
|
| /* Unregister before closing. The loop object was copied by the |
| 691 |
|
| * fork, so this watcher list is OURS - the parent's is untouched - |
| 692 |
|
| * and a registration left behind here would still name this fd |
| 693 |
|
| * number after the close. The very next socketpair() hands that |
| 694 |
|
| * number straight back for the replacement pool, and the stale |
| 695 |
|
| * entry would then be pointing at this freed struct. */ |
| 696 |
10 |
▶ | if (!PL_dirty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) { |
10 |
0 |
10 |
|
| 697 |
10 |
▶ | if (p->vt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->vt) { |
0 |
10 |
10 |
|
| 698 |
0 |
| p->vt->remove(aTHX_ p->vt->ctx, w->fd); |
| 699 |
10 |
▶ | } else if (p->loop) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (p->loop) { |
10 |
0 |
10 |
|
| 700 |
10 |
| dSP; |
| 701 |
10 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
10 |
10 |
|
| 702 |
10 |
▶ | EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(w->fd); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(w->fd); PUTBACK; |
0 |
10 |
10 |
|
| 703 |
10 |
| call_method("remove", G_VOID | G_DISCARD | G_EVAL); |
| 704 |
10 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
10 |
0 |
10 |
|
| 705 |
|
| } |
| 706 |
|
| } |
| 707 |
10 |
| close(w->fd); /* our copy only; the parent keeps its own */ |
| 708 |
|
| } |
| 709 |
10 |
▶ | if (w->rbuf) SvREFCNT_dec(w->rbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->rbuf) SvREFCNT_dec(w->rbuf); |
10 |
0 |
10 |
|
| 710 |
10 |
▶ | if (w->txq) SvREFCNT_dec((SV *)w->txq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->txq) SvREFCNT_dec((SV *)w->txq); |
10 |
0 |
10 |
|
| 711 |
10 |
▶ | if (w->future) SvREFCNT_dec(w->future); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->future) SvREFCNT_dec(w->future); |
0 |
10 |
10 |
|
| 712 |
|
| } |
| 713 |
5 |
| Safefree(p->w); |
| 714 |
5 |
▶ | if (p->acq) SvREFCNT_dec((SV *)p->acq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->acq) SvREFCNT_dec((SV *)p->acq); |
5 |
0 |
5 |
|
| 715 |
5 |
▶ | if (p->queue) SvREFCNT_dec((SV *)p->queue); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->queue) SvREFCNT_dec((SV *)p->queue); |
5 |
0 |
5 |
|
| 716 |
5 |
▶ | if (p->loop) SvREFCNT_dec(p->loop); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->loop) SvREFCNT_dec(p->loop); |
5 |
0 |
5 |
|
| 717 |
5 |
▶ | if (p->cargs) SvREFCNT_dec(p->cargs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->cargs) SvREFCNT_dec(p->cargs); |
5 |
0 |
5 |
|
| 718 |
5 |
| Safefree(p); |
| 719 |
|
| } |
| 720 |
|
| |
| 721 |
18 |
| static void dbil_pool_free(pTHX_ dbil_pool *p) { |
| 722 |
|
| int i; |
| 723 |
18 |
▶ | if (!p) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!p) return; |
0 |
18 |
18 |
|
| 724 |
|
| /* Not ours: never signal another process's workers. This is the whole |
| 725 |
|
| * reason a child exiting used to take down the parent's pool. */ |
| 726 |
18 |
▶ | if (!dbil_pool_owned(p)) { dbil_pool_disown(aTHX_ p); return; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dbil_pool_owned(p)) { dbil_pool_disown(aTHX_ p); return; } |
0 |
18 |
18 |
|
| 727 |
58 |
▶ | for (i = 0; i < p->nw; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < p->nw; i++) { |
40 |
18 |
58 |
|
| 728 |
40 |
| dbil_worker *w = &p->w[i]; |
| 729 |
40 |
▶ | if (w->fd >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->fd >= 0) { |
40 |
0 |
40 |
|
| 730 |
|
| /* only call back into the loop when the interpreter is healthy; |
| 731 |
|
| * during global destruction (PL_dirty) the loop object may already |
| 732 |
|
| * be gone, so just close the fd and reap. */ |
| 733 |
40 |
▶ | if (!PL_dirty) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) { |
38 |
2 |
40 |
|
| 734 |
38 |
▶ | if (p->vt) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->vt) { |
0 |
38 |
38 |
|
| 735 |
0 |
| p->vt->remove(aTHX_ p->vt->ctx, w->fd); |
| 736 |
|
| } else { |
| 737 |
38 |
| dSP; |
| 738 |
38 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); |
0 |
38 |
38 |
|
| 739 |
38 |
▶ | EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(w->fd); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); PUSHs(p->loop); mPUSHi(w->fd); PUTBACK; |
0 |
38 |
38 |
|
| 740 |
38 |
| call_method("remove", G_VOID | G_DISCARD | G_EVAL); |
| 741 |
38 |
▶ | SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| SPAGAIN; PUTBACK; FREETMPS; LEAVE; |
38 |
0 |
38 |
|
| 742 |
|
| } |
| 743 |
|
| } |
| 744 |
40 |
| close(w->fd); |
| 745 |
|
| } |
| 746 |
40 |
▶ | if (w->pid > 0) { kill(w->pid, SIGTERM); waitpid(w->pid, NULL, 0); } |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->pid > 0) { kill(w->pid, SIGTERM); waitpid(w->pid, NULL, 0); } |
40 |
0 |
40 |
|
| 747 |
40 |
▶ | if (w->future) { /* never leave an in-flight future pending forever */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->future) { /* never leave an in-flight future pending forever */ |
1 |
39 |
40 |
|
| 748 |
1 |
▶ | if (!PL_dirty) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) |
1 |
0 |
1 |
|
| 749 |
1 |
| dbil_future_settle_fail(aTHX_ w->future, |
| 750 |
|
| sv_2mortal(newSVpvs("disconnected with query in flight"))); |
| 751 |
1 |
| SvREFCNT_dec(w->future); |
| 752 |
|
| } |
| 753 |
40 |
▶ | while (av_len(w->txq) >= 1) { /* and nothing queued on the slot */ |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(w->txq) >= 1) { /* and nothing queued on the slot */ |
0 |
40 |
40 |
|
| 754 |
0 |
| SV *b = av_shift(w->txq); SV *f = av_shift(w->txq); |
| 755 |
0 |
| SvREFCNT_dec(b); |
| 756 |
0 |
▶ | if (!PL_dirty) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) |
0 |
0 |
0 |
|
| 757 |
0 |
| dbil_future_settle_fail(aTHX_ f, |
| 758 |
|
| sv_2mortal(newSVpvs("disconnected with query in flight"))); |
| 759 |
0 |
| SvREFCNT_dec(f); |
| 760 |
|
| } |
| 761 |
40 |
▶ | if (w->rbuf) SvREFCNT_dec(w->rbuf); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->rbuf) SvREFCNT_dec(w->rbuf); |
40 |
0 |
40 |
|
| 762 |
40 |
▶ | if (w->txq) SvREFCNT_dec((SV *)w->txq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->txq) SvREFCNT_dec((SV *)w->txq); |
40 |
0 |
40 |
|
| 763 |
|
| } |
| 764 |
18 |
| Safefree(p->w); |
| 765 |
18 |
▶ | if (p->acq) { /* anyone still waiting for a slot loses */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->acq) { /* anyone still waiting for a slot loses */ |
18 |
0 |
18 |
|
| 766 |
18 |
▶ | while (av_len(p->acq) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(p->acq) >= 0) { |
0 |
18 |
18 |
|
| 767 |
0 |
| SV *fut = av_shift(p->acq); |
| 768 |
0 |
| dbil_future_settle_fail(aTHX_ fut, |
| 769 |
|
| sv_2mortal(newSVpvs("pool destroyed"))); |
| 770 |
0 |
| SvREFCNT_dec(fut); |
| 771 |
|
| } |
| 772 |
18 |
| SvREFCNT_dec((SV *)p->acq); |
| 773 |
|
| } |
| 774 |
18 |
▶ | if (p->queue) { /* settle anything still waiting for a worker */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->queue) { /* settle anything still waiting for a worker */ |
18 |
0 |
18 |
|
| 775 |
18 |
▶ | while (av_len(p->queue) >= 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (av_len(p->queue) >= 1) { |
0 |
18 |
18 |
|
| 776 |
0 |
| SV *b = av_shift(p->queue); SV *f = av_shift(p->queue); |
| 777 |
0 |
| SvREFCNT_dec(b); |
| 778 |
0 |
▶ | if (!PL_dirty) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PL_dirty) |
0 |
0 |
0 |
|
| 779 |
0 |
| dbil_future_settle_fail(aTHX_ f, |
| 780 |
|
| sv_2mortal(newSVpvs("disconnected with query queued"))); |
| 781 |
0 |
| SvREFCNT_dec(f); |
| 782 |
|
| } |
| 783 |
18 |
| SvREFCNT_dec((SV *)p->queue); |
| 784 |
|
| } |
| 785 |
18 |
▶ | if (p->loop) SvREFCNT_dec(p->loop); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->loop) SvREFCNT_dec(p->loop); |
18 |
0 |
18 |
|
| 786 |
18 |
▶ | if (p->cargs) SvREFCNT_dec(p->cargs); |
Branch: 1 branch
| Branch | True | False | Total |
| if (p->cargs) SvREFCNT_dec(p->cargs); |
18 |
0 |
18 |
|
| 787 |
18 |
| Safefree(p); |
| 788 |
|
| } |
| 789 |
|
| |
| 790 |
|
| #endif /* DBIL_POOL_H */ |