include/pq_time.h

filter: Statementstmt 87.5% 7 / 8 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.0% 8 / 10 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/pq_cron.h ↑ Summary include/pq_migrate.h »
1 #ifndef PQ_TIME_H
2 #define PQ_TIME_H
3  
4 /* Time in Punk::Queue is epoch seconds as a double, everywhere: the columns
5 * are DOUBLE PRECISION on Pg and REAL on SQLite, never TIMESTAMPTZ. That
6 * buys identical bind values and identical comparison semantics on both
7 * backends, no driver timestamp parsing in C, and a dequeue predicate of
8 * `delayed <= ?` with a bound value rather than a server-side now().
9 *
10 * What it costs is readability in psql, which the pq_jobs_human view
11 * mitigates, and it makes clock skew our problem instead of the server's.
12 * Hence the delta below.
13 *
14 * Include after pq_compat.h. */
15  
16 /* Not on Windows: XSUB.h has already made gettimeofday a macro there, so
17 * the CRT's own prototype in sys/time.h would expand into nonsense. perl.h
18 * has the timeval it needs. */
19 #if defined(HAS_GETTIMEOFDAY) && !defined(WIN32)
20 # include <sys/time.h>
21 #endif
22  
23 /* Wall clock on THIS host, epoch seconds. Not what you bind - see pq_now. */
24 9009 static double pq_now_local(pTHX) {
25 #ifdef HAS_GETTIMEOFDAY
26 struct timeval tv;
27 9009 ▶ if (gettimeofday(&tv, NULL) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (gettimeofday(&tv, NULL) == 0) 9009 0 9009
28 9009 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
29 #endif
30 0 return (double)time(NULL);
31 }
32  
33 /* ---- clock skew -----------------------------------------------------------
34 *
35 * Every worker binds its own idea of "now" into delayed/started/expires, and
36 * every other worker compares those against its own. Two hosts a minute apart
37 * is enough to claim a delayed job early or leave a fresh one sitting, and
38 * the symptom ("the job ran an hour before its delay") is baffling from the
39 * inside.
40 *
41 * So each connection probes the server's clock once, stores the difference,
42 * and every timestamp this process binds is local + delta. The probe SQL is
43 * per-backend (pq_sqlite.h, pq_pg.h); the arithmetic is here.
44 *
45 * The delta lives in the connection pool slot, so it is re-probed on
46 * reconnect and after a fork, which is exactly when it can have changed. */
47  
48 #define PQ_DELTA_KEY "clock_delta"
49  
50 /* now, in the database's frame of reference. This is what gets bound. */
51 1904 static double pq_now_delta(pTHX_ double delta) {
52 1904 return pq_now_local(aTHX) + delta;
53 }
54  
55 /* What a probe stores. The server read landed somewhere inside the round
56 * trip that fetched it - between t0, taken before the query, and now - so
57 * the local clock it is compared against is the midpoint of that trip. A
58 * stall on either side of the query then skews the delta by half its
59 * length rather than all of it, which on a loaded box is the difference
60 * between a probe that is off by seconds and one off by a fraction. */
61 201 static double pq_probe_delta(pTHX_ double t0, double server) {
62 201 return server - (t0 + pq_now_local(aTHX)) / 2.0;
63 }
64  
65 #endif /* PQ_TIME_H */
« include/pq_cron.h ↑ Summary include/pq_migrate.h »